Skip to content

Commit d1e89ba

Browse files
committed
Re-organize
1 parent a658fe5 commit d1e89ba

File tree

7 files changed

+33
-48
lines changed

7 files changed

+33
-48
lines changed

delay.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cmd="$@"
77

88
until curl $BOOT_NODE; do
99
>&2 echo "Boot is unavailable - sleeping"
10-
sleep 10
10+
sleep 5
1111
done
1212

1313
>&2 echo "Boot is up - executing command"

project.clj

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
:min-lein-version "2.0.0"
55
:dependencies [[org.clojure/clojure "1.8.0"]
66
[compojure "1.5.1"]
7-
[ring/ring-defaults "0.2.1"]
87
[digest "1.4.8"]
98
[ring/ring-json "0.4.0"]
109
[clj-http "3.9.0"]]

src/blockchain/agent.clj

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
(ns blockchain.agent
22
(:require [clj-http.client :as client]
3-
[clojure.string :refer [starts-with?]]
43
[blockchain.init :refer :all]
5-
[blockchain.worker :refer
6-
[resolve-chain-conflict
7-
remove-from-nodes
8-
update-node-list
9-
add-node]]))
4+
[blockchain.helper :refer [fix-prc]]
5+
[blockchain.worker :refer [resolve-chain-conflict]]))
106

117

12-
(defn- fix-prc [addr]
13-
(if (starts-with? addr "http")
14-
addr
15-
(str "http://" addr)))
8+
;; Behind-the-scene works
9+
(defn- add-node [address]
10+
(when-not (or (.contains @nodes address)
11+
(= address "0:0:0:0:0:0:0:1")
12+
(= address (System/getenv "GATEWAY")))
13+
(swap! nodes conj address)))
1614

15+
(defn- remove-from-nodes [node]
16+
(->> (remove #(= node %) @nodes)
17+
(reset! nodes)))
18+
19+
(defn- update-node-list [remote-nodes]
20+
(run! add-node remote-nodes))
1721

1822
(defn- submit-chain [addr]
1923
(when (string? addr)
@@ -27,14 +31,16 @@
2731
(remove-from-nodes addr))))
2832
(catch Exception e (remove-from-nodes addr)))))
2933

30-
(defn broadcast []
31-
"Submit the new chain to other nodes in the network."
32-
(run! submit-chain @nodes))
33-
3434
(defn- update-node-chain [{:keys [chain nodes]}]
3535
(resolve-chain-conflict chain)
3636
(update-node-list nodes))
3737

38+
39+
;; Public services
40+
(defn broadcast []
41+
"Submit the new chain to other nodes in the network."
42+
(run! submit-chain @nodes))
43+
3844
(defn fetch-remote-chain [address]
3945
(println "Fetching from >>" address)
4046
(try

src/blockchain/core.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
[compojure.route :as route]
44
[compojure.handler :as handler]
55
[clojure.walk :refer [keywordize-keys]]
6-
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
76
[ring.middleware.json :refer [wrap-json-response wrap-json-body]]
87
[ring.util.response :as resp]
9-
[blockchain.init :refer [chain nodes]]
8+
[blockchain.init :refer :all]
109
[blockchain.worker :as worker]
1110
[blockchain.agent :as agent]))
1211

@@ -23,6 +22,7 @@
2322
(POST "/" {chain :body}
2423
(do (println "Someone is submitting a chain...")
2524
(-> (keywordize-keys chain)
25+
(vec)
2626
(worker/resolve-chain-conflict)))
2727
(generate-response {:status 200}))
2828
(GET "/update" [] (do (run! agent/fetch-remote-chain @nodes)

src/blockchain/helper.clj

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
(ns blockchain.helper)
1+
(ns blockchain.helper
2+
(:require [clojure.string :refer [starts-with?]]))
23

3-
(defn add-tail [vect item]
4-
"Conj is consistent about where to add new element."
5-
"Make our own function to do that."
6-
(vec (concat vect [item])))
4+
(defn now []
5+
(quot (System/currentTimeMillis) 1000))
76

87
(defn fix-prc [addr]
98
"Fix address without http protocol"

src/blockchain/init.clj

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
(ns blockchain.init
2-
(:require [digest :refer [sha-256]]
3-
[clojure.java.io :refer [as-url]]))
2+
(:require [blockchain.helper :refer [now]]))
43

5-
(defn now [] (quot (System/currentTimeMillis) 1000))
64

7-
(def genesis-block {:index 1
8-
:time (now)
9-
:proof "vutr.io"})
5+
(defonce ^:private genesis-block {:index 1
6+
:time (now)
7+
:proof "vutr.io"})
108

119
(def chain (atom [genesis-block]))
12-
(def current-transactions (atom []))
1310
(def nodes (atom []))

src/blockchain/worker.clj

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
(ns blockchain.worker
22
(:require [blockchain.init :refer :all]
33
[digest :refer [sha-256]]
4-
[clojure.java.io :refer [as-url]]))
4+
[blockchain.helper :refer [now]]))
55

6-
(defn- add-tail [vect item]
7-
"Conj is consistent about where to add new element. Make our own function to do that."
8-
(vec (concat vect [item])))
96

107
(defn- hashing-block [block]
118
(sha-256 (str block)))
@@ -36,16 +33,6 @@
3633
(recur (inc id) (inc idx)) false)
3734
true)))
3835

39-
(defn add-node [address]
40-
(when-not (or (.contains @nodes address)
41-
(= address "0:0:0:0:0:0:0:1")
42-
(= address (System/getenv "GATEWAY")))
43-
(swap! nodes conj address)))
44-
45-
(defn remove-from-nodes [node]
46-
(->> (remove #(= node %) @nodes)
47-
(reset! nodes)))
48-
4936
(defn forge-new-block []
5037
(let [last-block (last @chain)]
5138
{:index (inc (count @chain))
@@ -57,7 +44,7 @@
5744
(let [proof (:proof block)
5845
last-hashed (hashing-block (last @chain))]
5946
(when (validate-proof proof last-hashed)
60-
(do (swap! chain add-tail block) chain))))
47+
(do (swap! chain conj block) chain))))
6148

6249
(defn resolve-chain-conflict [remote-chain]
6350
"Chain with greater length will replace the existing chain."
@@ -67,6 +54,3 @@
6754
(when (validate-chain remote-chain)
6855
(do (reset! chain remote-chain)
6956
(println "Applied new chain..."))))))
70-
71-
(defn update-node-list [remote-nodes]
72-
(run! add-node remote-nodes))

0 commit comments

Comments
 (0)