|
1 | 1 | (ns blockchain.worker
|
2 | 2 | (:require [blockchain.init :refer :all]
|
3 | 3 | [digest :refer [sha-256]]
|
4 |
| - [clojure.java.io :refer [as-url]])) |
| 4 | + [blockchain.helper :refer [now]])) |
5 | 5 |
|
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]))) |
9 | 6 |
|
10 | 7 | (defn- hashing-block [block]
|
11 | 8 | (sha-256 (str block)))
|
|
36 | 33 | (recur (inc id) (inc idx)) false)
|
37 | 34 | true)))
|
38 | 35 |
|
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 |
| - |
49 | 36 | (defn forge-new-block []
|
50 | 37 | (let [last-block (last @chain)]
|
51 | 38 | {:index (inc (count @chain))
|
|
57 | 44 | (let [proof (:proof block)
|
58 | 45 | last-hashed (hashing-block (last @chain))]
|
59 | 46 | (when (validate-proof proof last-hashed)
|
60 |
| - (do (swap! chain add-tail block) chain)))) |
| 47 | + (do (swap! chain conj block) chain)))) |
61 | 48 |
|
62 | 49 | (defn resolve-chain-conflict [remote-chain]
|
63 | 50 | "Chain with greater length will replace the existing chain."
|
|
67 | 54 | (when (validate-chain remote-chain)
|
68 | 55 | (do (reset! chain remote-chain)
|
69 | 56 | (println "Applied new chain..."))))))
|
70 |
| - |
71 |
| -(defn update-node-list [remote-nodes] |
72 |
| - (run! add-node remote-nodes)) |
0 commit comments