-
Notifications
You must be signed in to change notification settings - Fork 127
/
node.go
553 lines (495 loc) · 15.2 KB
/
node.go
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package kernel
import (
"encoding/binary"
"encoding/json"
"fmt"
"os"
"sort"
"sync"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/kernel/internal/clock"
"github.com/MixinNetwork/mixin/logger"
"github.com/MixinNetwork/mixin/network"
"github.com/MixinNetwork/mixin/storage"
"github.com/VictoriaMetrics/fastcache"
)
type Node struct {
IdForNetwork crypto.Hash
Signer common.Address
Listener string
Peer *network.Peer
TopoCounter *TopologicalSequence
SyncPoints *syncMap
SyncPointsMap map[crypto.Hash]*network.SyncPoint
GraphTimestamp uint64
Epoch uint64
chains *chainsMap
allNodesSortedWithState []*CNode
nodeStateSequences []*NodeStateSequence
acceptedNodeStateSequences []*NodeStateSequence
chain *Chain
genesisNodesMap map[crypto.Hash]bool
genesisNodes []crypto.Hash
startAt time.Time
networkId crypto.Hash
persistStore storage.Store
cacheStore *fastcache.Cache
custom *config.Custom
configDir string
addr string
done chan struct{}
elc chan struct{}
mlc chan struct{}
cqc chan struct{}
}
type NodeStateSequence struct {
Timestamp uint64
NodesWithoutState []*CNode
}
type CNode struct {
IdForNetwork crypto.Hash
Signer common.Address
Payee common.Address
Transaction crypto.Hash
Timestamp uint64
State string
ConsensusIndex int
}
func SetupNode(custom *config.Custom, persistStore storage.Store, cacheStore *fastcache.Cache, addr string, dir string) (*Node, error) {
var node = &Node{
SyncPoints: &syncMap{mutex: new(sync.RWMutex), m: make(map[crypto.Hash]*network.SyncPoint)},
chains: &chainsMap{m: make(map[crypto.Hash]*Chain)},
genesisNodesMap: make(map[crypto.Hash]bool),
persistStore: persistStore,
cacheStore: cacheStore,
custom: custom,
configDir: dir,
addr: addr,
startAt: clock.Now(),
done: make(chan struct{}),
elc: make(chan struct{}),
mlc: make(chan struct{}),
cqc: make(chan struct{}),
}
node.LoadNodeConfig()
err := node.LoadGenesis(dir)
if err != nil {
return nil, err
}
node.TopoCounter = getTopologyCounter(persistStore)
logger.Println("Validating graph entries...")
start := clock.Now()
total, invalid, err := node.persistStore.ValidateGraphEntries(node.networkId, 10)
if err != nil {
return nil, err
} else if invalid > 0 {
return nil, fmt.Errorf("validate graph with %d/%d invalid entries", invalid, total)
}
logger.Printf("Validate graph with %d total entries in %s\n", total, clock.Now().Sub(start).String())
err = node.LoadConsensusNodes()
if err != nil {
return nil, err
}
err = node.LoadAllChains(node.persistStore, node.networkId)
if err != nil {
return nil, err
}
logger.Printf("Listen:\t%s\n", addr)
logger.Printf("Signer:\t%s\n", node.Signer.String())
logger.Printf("Network:\t%s\n", node.networkId.String())
logger.Printf("Node Id:\t%s\n", node.IdForNetwork.String())
logger.Printf("Topology:\t%d\n", node.TopoCounter.seq)
return node, nil
}
func (node *Node) LoadNodeConfig() {
var addr common.Address
addr.PrivateSpendKey = node.custom.Node.Signer
addr.PublicSpendKey = addr.PrivateSpendKey.Public()
addr.PrivateViewKey = addr.PublicSpendKey.DeterministicHashDerive()
addr.PublicViewKey = addr.PrivateViewKey.Public()
node.Signer = addr
node.Listener = node.custom.Network.Listener
}
func (node *Node) buildNodeStateSequences(allNodesSortedWithState []*CNode, acceptedOnly bool) []*NodeStateSequence {
nodeStateSequences := make([]*NodeStateSequence, len(allNodesSortedWithState))
for i, n := range allNodesSortedWithState {
nodes := node.nodeSequeueWithoutState(n.Timestamp+1, acceptedOnly)
seq := &NodeStateSequence{
Timestamp: n.Timestamp,
NodesWithoutState: nodes,
}
nodeStateSequences[i] = seq
}
return nodeStateSequences
}
func (node *Node) NodesListWithoutState(threshold uint64, acceptedOnly bool) []*CNode {
sequences := node.nodeStateSequences
if acceptedOnly {
sequences = node.acceptedNodeStateSequences
}
for i := len(sequences); i > 0; i-- {
seq := sequences[i-1]
if seq.Timestamp < threshold {
return seq.NodesWithoutState
}
}
return nil
}
func (node *Node) nodeSequeueWithoutState(threshold uint64, acceptedOnly bool) []*CNode {
filter := make(map[crypto.Hash]*CNode)
for _, n := range node.allNodesSortedWithState {
if n.Timestamp >= threshold {
break
}
filter[n.IdForNetwork] = n
}
nodes := make([]*CNode, 0)
for _, n := range filter {
if !acceptedOnly || n.State == common.NodeStateAccepted {
nodes = append(nodes, &CNode{
IdForNetwork: n.IdForNetwork,
Signer: n.Signer,
Payee: n.Payee,
Transaction: n.Transaction,
Timestamp: n.Timestamp,
State: n.State,
})
}
}
sort.Slice(nodes, func(i, j int) bool {
if nodes[i].Timestamp < nodes[j].Timestamp {
return true
}
if nodes[i].Timestamp > nodes[j].Timestamp {
return false
}
a := nodes[i].IdForNetwork
b := nodes[j].IdForNetwork
return a.String() < b.String()
})
for index, i := 0, 0; i < len(nodes); i++ {
cn := nodes[i]
cn.ConsensusIndex = index
switch cn.State {
case common.NodeStateAccepted, common.NodeStatePledging:
index++
}
}
return nodes
}
func (node *Node) PledgingNode(timestamp uint64) *CNode {
nodes := node.NodesListWithoutState(timestamp, false)
if len(nodes) == 0 {
return nil
}
cn := nodes[len(nodes)-1]
if cn.State == common.NodeStatePledging {
return cn
}
return nil
}
func (node *Node) GetAcceptedOrPledgingNode(id crypto.Hash) *CNode {
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
for _, cn := range nodes {
if cn.IdForNetwork == id && (cn.State == common.NodeStateAccepted || cn.State == common.NodeStatePledging) {
return cn
}
}
return nil
}
// An accepted node can sign transactions only when it satisfies either:
// 1. It is a genesis node.
// 2. It has been accepted more than 12 hours.
func (node *Node) ConsensusReady(cn *CNode, timestamp uint64) bool {
if cn.State != common.NodeStateAccepted {
return false
}
if node.genesisNodesMap[cn.IdForNetwork] {
return true
}
if cn.Timestamp+uint64(config.KernelNodeAcceptPeriodMinimum) < timestamp {
return true
}
return false
}
func (node *Node) ConsensusThreshold(timestamp uint64) int {
consensusBase := 0
nodes := node.NodesListWithoutState(timestamp, false)
for _, cn := range nodes {
threshold := config.SnapshotReferenceThreshold * config.SnapshotRoundGap
if threshold > uint64(3*time.Minute) {
panic("should never be here")
}
switch cn.State {
case common.NodeStatePledging:
// FIXME the pledge transaction may be broadcasted very late
// at this situation, the node should be treated as evil
if config.KernelNodeAcceptPeriodMinimum < time.Hour {
panic("should never be here")
}
t := uint64(config.KernelNodeAcceptPeriodMinimum) - threshold*3
if cn.Timestamp+t < timestamp {
consensusBase++
}
case common.NodeStateAccepted:
if node.genesisNodesMap[cn.IdForNetwork] || cn.Timestamp+threshold < timestamp {
consensusBase++
}
}
}
if consensusBase < config.KernelMinimumNodesCount {
logger.Debugf("invalid consensus base %d %d %d\n", timestamp, consensusBase, config.KernelMinimumNodesCount)
return 1000
}
return consensusBase*2/3 + 1
}
func (node *Node) LoadConsensusNodes() error {
threshold := uint64(clock.Now().UnixNano()) * 2
nodes := node.persistStore.ReadAllNodes(threshold, true)
sort.Slice(nodes, func(i, j int) bool {
if nodes[i].Timestamp < nodes[j].Timestamp {
return true
}
if nodes[i].Timestamp > nodes[j].Timestamp {
return false
}
a := nodes[i].IdForNetwork(node.networkId)
b := nodes[j].IdForNetwork(node.networkId)
return a.String() < b.String()
})
cnodes := make([]*CNode, len(nodes))
for i, n := range nodes {
cnodes[i] = &CNode{
IdForNetwork: n.IdForNetwork(node.networkId),
Signer: n.Signer,
Payee: n.Payee,
Transaction: n.Transaction,
Timestamp: n.Timestamp,
State: n.State,
}
logger.Printf("LoadConsensusNode %v\n", cnodes[i])
}
node.allNodesSortedWithState = cnodes
node.nodeStateSequences = node.buildNodeStateSequences(cnodes, false)
node.acceptedNodeStateSequences = node.buildNodeStateSequences(cnodes, true)
node.chain = node.GetOrCreateChain(node.IdForNetwork)
return nil
}
func (node *Node) PingNeighborsFromConfig() error {
node.Peer = network.NewPeer(node, node.IdForNetwork, node.addr, node.custom.Network.GossipNeighbors)
f, err := os.ReadFile(node.configDir + "/nodes.json")
if err != nil {
return err
}
var inputs []struct {
Host string `json:"host"`
}
err = json.Unmarshal(f, &inputs)
if err != nil {
return err
}
for _, in := range inputs {
if in.Host == node.Listener {
continue
}
node.Peer.PingNeighbor(in.Host)
}
return nil
}
func (node *Node) UpdateNeighbors(neighbors []string) error {
for _, in := range neighbors {
if in == node.Listener {
continue
}
node.Peer.PingNeighbor(in)
}
return nil
}
func (node *Node) ListenNeighbors() error {
return node.Peer.ListenNeighbors()
}
func (node *Node) NetworkId() crypto.Hash {
return node.networkId
}
func (node *Node) Uptime() time.Duration {
return clock.Now().Sub(node.startAt)
}
func (node *Node) GetCacheStore() *fastcache.Cache {
return node.cacheStore
}
func (node *Node) BuildGraph() []*network.SyncPoint {
node.chains.RLock()
defer node.chains.RUnlock()
points := make([]*network.SyncPoint, 0)
for _, chain := range node.chains.m {
if chain.State == nil {
continue
}
f := chain.State.FinalRound
points = append(points, &network.SyncPoint{
NodeId: chain.ChainId,
Hash: f.Hash,
Number: f.Number,
Pool: map[string]int{
"index": chain.FinalIndex,
"count": chain.FinalCount,
},
})
}
return points
}
func (node *Node) BuildAuthenticationMessage() []byte {
data := make([]byte, 8)
binary.BigEndian.PutUint64(data, uint64(clock.Now().Unix()))
data = append(data, node.Signer.PublicSpendKey[:]...)
sig := node.Signer.PrivateSpendKey.Sign(data)
data = append(data, sig[:]...)
return append(data, []byte(node.Listener)...)
}
func (node *Node) Authenticate(msg []byte) (crypto.Hash, string, error) {
if len(msg) < 8+len(crypto.Hash{})+len(crypto.Signature{}) {
return crypto.Hash{}, "", fmt.Errorf("peer authentication message malformated %d", len(msg))
}
ts := binary.BigEndian.Uint64(msg[:8])
if clock.Now().Unix()-int64(ts) > 3 {
return crypto.Hash{}, "", fmt.Errorf("peer authentication message timeout %d %d", ts, clock.Now().Unix())
}
var signer common.Address
copy(signer.PublicSpendKey[:], msg[8:40])
signer.PublicViewKey = signer.PublicSpendKey.DeterministicHashDerive().Public()
peerId := signer.Hash().ForNetwork(node.networkId)
if peerId == node.IdForNetwork {
return crypto.Hash{}, "", fmt.Errorf("peer authentication invalid consensus peer %s", peerId)
}
peer := node.GetAcceptedOrPledgingNode(peerId)
if node.custom.Node.ConsensusOnly && peer == nil {
return crypto.Hash{}, "", fmt.Errorf("peer authentication invalid consensus peer %s", peerId)
}
if peer != nil && peer.Signer.Hash() != signer.Hash() {
return crypto.Hash{}, "", fmt.Errorf("peer authentication invalid consensus peer %s", peerId)
}
var sig crypto.Signature
copy(sig[:], msg[40:40+len(sig)])
if !signer.PublicSpendKey.Verify(msg[:40], sig) {
return crypto.Hash{}, "", fmt.Errorf("peer authentication message signature invalid %s", peerId)
}
listener := string(msg[40+len(sig):])
return peerId, listener, nil
}
func (node *Node) SendTransactionToPeer(peerId, hash crypto.Hash) error {
tx, err := node.checkTxInStorage(hash)
if err != nil || tx == nil {
return err
}
return node.Peer.SendTransactionMessage(peerId, tx)
}
func (node *Node) CachePutTransaction(peerId crypto.Hash, tx *common.VersionedTransaction) error {
return node.persistStore.CachePutTransaction(tx)
}
func (node *Node) ReadAllNodesWithoutState() []crypto.Hash {
var all []crypto.Hash
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), false)
for _, cn := range nodes {
all = append(all, cn.IdForNetwork)
}
return all
}
func (node *Node) ReadSnapshotsSinceTopology(offset, count uint64) ([]*common.SnapshotWithTopologicalOrder, error) {
return node.persistStore.ReadSnapshotsSinceTopology(offset, count)
}
func (node *Node) ReadSnapshotsForNodeRound(nodeIdWithNetwork crypto.Hash, round uint64) ([]*common.SnapshotWithTopologicalOrder, error) {
return node.persistStore.ReadSnapshotsForNodeRound(nodeIdWithNetwork, round)
}
func (node *Node) UpdateSyncPoint(peerId crypto.Hash, points []*network.SyncPoint) {
for _, p := range points {
if p.NodeId == node.IdForNetwork {
node.SyncPoints.Set(peerId, p)
}
}
node.SyncPointsMap = node.SyncPoints.Map()
}
func (node *Node) CheckBroadcastedToPeers() bool {
spm := node.SyncPointsMap
if len(spm) == 0 || node.chain.State == nil {
return false
}
final, count := node.chain.State.FinalRound.Number, 1
threshold := node.ConsensusThreshold(uint64(clock.Now().UnixNano()))
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
for _, cn := range nodes {
remote := spm[cn.IdForNetwork]
if remote == nil {
continue
}
if remote.Number+1 >= final {
count += 1
}
}
return count >= threshold
}
func (node *Node) CheckCatchUpWithPeers() bool {
spm := node.SyncPointsMap
if len(spm) == 0 || node.chain.State == nil {
return false
}
threshold := node.ConsensusThreshold(uint64(clock.Now().UnixNano()))
cache, updated := node.chain.State.CacheRound, 1
final := node.chain.State.FinalRound.Number
nodes := node.NodesListWithoutState(uint64(clock.Now().UnixNano()), true)
for _, cn := range nodes {
remote := spm[cn.IdForNetwork]
if remote == nil {
continue
}
updated = updated + 1
if remote.Number <= final {
continue
}
if remote.Number > final+1 {
logger.Verbosef("CheckCatchUpWithPeers local(%d)+1 < remote(%s:%d)\n", final, cn.IdForNetwork, remote.Number)
return false
}
if cache == nil {
logger.Verbosef("CheckCatchUpWithPeers local cache nil\n")
return false
}
cf := cache.asFinal()
if cf == nil {
logger.Verbosef("CheckCatchUpWithPeers local cache empty\n")
return false
}
if cf.Hash != remote.Hash {
logger.Verbosef("CheckCatchUpWithPeers local(%s) != remote(%s)\n", cf.Hash, remote.Hash)
return false
}
if now := uint64(clock.Now().UnixNano()); cf.Start+config.SnapshotRoundGap*100 > now {
logger.Verbosef("CheckCatchUpWithPeers local start(%d)+%d > now(%d)\n", cf.Start, config.SnapshotRoundGap*100, now)
return false
}
}
if updated < threshold {
logger.Verbosef("CheckCatchUpWithPeers updated(%d) < threshold(%d)\n", updated, threshold)
}
return updated >= threshold
}
type syncMap struct {
mutex *sync.RWMutex
m map[crypto.Hash]*network.SyncPoint
}
func (s *syncMap) Set(k crypto.Hash, p *network.SyncPoint) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.m[k] = p
}
func (s *syncMap) Map() map[crypto.Hash]*network.SyncPoint {
s.mutex.RLock()
defer s.mutex.RUnlock()
m := make(map[crypto.Hash]*network.SyncPoint)
for k, p := range s.m {
m[k] = p
}
return m
}