Repository navigation
Expand file tree
/
Copy pathcore.cljs
More file actions
228 lines (199 loc) · 7.76 KB
/
Copy pathcore.cljs
File metadata and controls
228 lines (199 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
(ns status-im.node.core
(:require [re-frame.core :as re-frame]
[status-im.constants :as constants]
[status-im.fleet.core :as fleet]
[status-im.native-module.core :as status]
[status-im.utils.config :as config]
[status-im.utils.types :as types]
[status-im.utils.platform :as utils.platform]
[status-im.utils.utils :as utils]
[taoensso.timbre :as log]
[status-im.utils.fx :as fx]))
(defn- add-custom-bootnodes [config network all-bootnodes]
(let [bootnodes (as-> all-bootnodes $
(get $ network)
(vals $)
(map :address $))]
(if (seq bootnodes)
(assoc-in config [:ClusterConfig :BootNodes] bootnodes)
config)))
(defn- add-log-level [config log-level]
(if (empty? log-level)
(assoc config
:LogEnabled false)
(assoc config
:LogLevel log-level
:LogEnabled true)))
(defn get-network-genesis-hash-prefix
"returns the hex representation of the first 8 bytes of a network's genesis hash"
[network]
(case network
1 "d4e56740f876aef8"
3 "41941023680923e0"
4 "6341fd3daf94b748"
nil))
(defn get-les-topic
"returns discovery v5 topic derived from genesis of the provided network"
[network]
(let [les-discovery-identifier "LES2@"
hash-prefix (get-network-genesis-hash-prefix network)]
(when hash-prefix
(str les-discovery-identifier hash-prefix))))
(defn get-topics
[network]
(let [les-topic (get-les-topic network)]
(cond-> {"whisper" {:Min 2, :Max 2}}
les-topic (assoc les-topic {:Min 2, :Max 2}))))
(defn get-account-network [db address]
(get-in db [:accounts/accounts address :network]))
(defn- get-base-node-config [config]
(let [initial-props @(re-frame/subscribe [:initial-props])
status-node-port (get initial-props :STATUS_NODE_PORT)]
(cond-> (assoc config
:Name "StatusIM")
config/dev-build?
(assoc :ListenAddr ":30304"
:DataDir (str (:DataDir config) "_dev"))
status-node-port
(assoc :ListenAddr (str ":" status-node-port)))))
(defn- pick-nodes
"Picks `limit` different nodes randomly from the list of nodes
if there is more than `limit` nodes in the list, otherwise return the list
of nodes"
[limit nodes]
(take limit (shuffle nodes)))
(defn get-log-level
[account-settings]
(or (:log-level account-settings)
(if utils.platform/desktop? ""
config/log-level-status-go)))
(defn- ulc-network? [config]
(get-in config [:LightEthConfig :ULC] false))
(defn- add-ulc-trusted-nodes [config trusted-nodes]
(if (ulc-network? config)
(-> config
(assoc-in [:LightEthConfig :TrustedNodes] trusted-nodes)
(assoc-in [:LightEthConfig :MinTrustedFraction] 50))
config))
(defn- get-account-node-config [db address]
(let [accounts (get db :accounts/accounts)
current-fleet-key (fleet/current-fleet db address)
current-fleet (get fleet/fleets current-fleet-key)
rendezvous-nodes (pick-nodes 3 (vals (:rendezvous current-fleet)))
{:keys [network installation-id settings bootnodes networks]}
(merge
{:network config/default-network
:networks (:networks/networks db)
:settings (constants/default-account-settings)
:installation-id (get db :accounts/new-installation-id)}
(get accounts address))
use-custom-bootnodes (get-in settings [:bootnodes network])
log-level (get-log-level settings)]
(cond-> (get-in networks [network :config])
:always
(get-base-node-config)
current-fleet
(assoc :NoDiscovery false
:Rendezvous (not (empty? rendezvous-nodes))
:ClusterConfig {:Enabled true
:Fleet (name current-fleet-key)
:BootNodes (pick-nodes 4 (vals (:boot current-fleet)))
:TrustedMailServers (pick-nodes 6 (vals (:mail current-fleet)))
:StaticNodes (into (pick-nodes 2 (vals (:whisper current-fleet))) (vals (:static current-fleet)))
:RendezvousNodes rendezvous-nodes})
:always
(assoc :WhisperConfig {:Enabled true
:LightClient true
:MinimumPoW 0.001
:EnableNTPSync true}
:ShhextConfig {:BackupDisabledDataDir (utils.platform/no-backup-directory)
:InstallationID installation-id
:MailServerConfirmations config/mailserver-confirmations-enabled?
:PFSEnabled true}
:RequireTopics (get-topics network))
(and
config/bootnodes-settings-enabled?
use-custom-bootnodes)
(add-custom-bootnodes network bootnodes)
:always
(add-ulc-trusted-nodes (vals (:static current-fleet)))
:always
(add-log-level log-level))))
(defn get-verify-account-config
"Is used when the node has to be started before
`VerifyAccountPassword` call."
[db network]
(-> (get-in (:networks/networks db) [network :config])
(get-base-node-config)
(assoc :ShhextConfig {:BackupDisabledDataDir (utils.platform/no-backup-directory)})
(assoc :PFSEnabled false
:NoDiscovery true)
(add-log-level config/log-level-status-go)))
(fx/defn update-sync-state
[{:keys [db]} error sync-state]
{:db (assoc db :node/chain-sync-state
(if error
{:error error}
(when sync-state (js->clj sync-state :keywordize-keys true))))})
(fx/defn update-block-number
[{:keys [db]} error block-number]
(when-not error
{:db (assoc db :node/latest-block-number block-number)}))
(fx/defn start
[{:keys [db]} address]
(let [network (if address
(get-account-network db address)
(:network db))
node-config (if (= (:node/on-ready db) :verify-account)
(get-verify-account-config db network)
(get-account-node-config db address))
node-config-json (types/clj->json node-config)]
(log/info "Node config: " node-config-json)
{:db (assoc db
:network network
:node/status :starting)
:node/start node-config-json}))
(fx/defn stop
[{:keys [db]}]
{:db (assoc db :node/status :stopping)
:node/stop nil})
(fx/defn initialize
[{{:node/keys [status] :as db} :db :as cofx} address]
(let [restart {:db (assoc db :node/restart? true :node/address address)}]
(case status
:started nil
:starting restart
:stopping restart
(start cofx address))))
(re-frame/reg-fx
:node/start
(fn [config]
(status/start-node config)))
(re-frame/reg-fx
:node/ready
(fn [config]
(status/node-ready)))
(re-frame/reg-fx
:node/stop
(fn []
(status/stop-node)))
(re-frame/reg-fx
:node/les-show-debug-info
(fn [[web3 account chain-sync-state]]
(.getBalance
(.-eth web3)
(:address account)
(fn [error-balance balance]
(.getBlockNumber
(.-eth web3)
(fn
[error-block block]
(utils/show-popup
"LES sync status"
(str
"* account=" (:address account) "\n"
"* latest block=" (or error-block block) "\n"
"* balance=" (or error-balance balance) "\n"
"* eth_getSyncing=" (or chain-sync-state "false")))))))))
(defn display-les-debug-info [{{:keys [web3] :account/keys [account] :node/keys [chain-sync-state]} :db}]
{:node/les-show-debug-info [web3 account]})