-
Notifications
You must be signed in to change notification settings - Fork 126
/
self.go
111 lines (102 loc) · 3.06 KB
/
self.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
package kernel
import (
"fmt"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/kernel/internal/clock"
"github.com/MixinNetwork/mixin/logger"
)
func (node *Node) checkCacheSnapshotTransaction(s *common.Snapshot) (*common.VersionedTransaction, bool, error) {
tx, finalized, err := node.persistStore.ReadTransaction(s.Transaction)
if tx != nil && len(finalized) == 0 {
err = node.validateKernelSnapshot(s, tx)
}
if err != nil || len(finalized) > 0 || tx != nil {
return tx, len(finalized) > 0, err
}
tx, err = node.persistStore.CacheGetTransaction(s.Transaction)
if err != nil || tx == nil {
return nil, false, err
}
err = tx.Validate(node.persistStore)
if err != nil {
return nil, false, err
}
err = node.validateKernelSnapshot(s, tx)
if err != nil {
return nil, false, err
}
err = tx.LockInputs(node.persistStore, false)
if err != nil {
return nil, false, err
}
return tx, false, node.persistStore.WriteTransaction(tx)
}
func (node *Node) validateKernelSnapshot(s *common.Snapshot, tx *common.VersionedTransaction) error {
switch tx.TransactionType() {
case common.TransactionTypeMint:
err := node.validateMintSnapshot(s, tx)
if err != nil {
logger.Println("validateMintSnapshot", s, tx, err)
return err
}
case common.TransactionTypeNodePledge:
err := node.validateNodePledgeSnapshot(s, tx)
if err != nil {
logger.Println("validateNodePledgeSnapshot", s, tx, err)
return err
}
case common.TransactionTypeNodeCancel:
err := node.validateNodeCancelSnapshot(s, tx)
if err != nil {
logger.Println("validateNodeCancelSnapshot", s, tx, err)
return err
}
case common.TransactionTypeNodeAccept:
err := node.validateNodeAcceptSnapshot(s, tx)
if err != nil {
logger.Println("validateNodeAcceptSnapshot", s, tx, err)
return err
}
case common.TransactionTypeNodeRemove:
err := node.validateNodeRemoveSnapshot(s, tx)
if err != nil {
logger.Println("validateNodeRemoveSnapshot", s, tx, err)
return err
}
}
if s.NodeId != node.IdForNetwork && s.RoundNumber == 0 && tx.TransactionType() != common.TransactionTypeNodeAccept {
return fmt.Errorf("invalid initial transaction type %d", tx.TransactionType())
}
return nil
}
func (node *Node) determinBestRound(roundTime uint64) *FinalRound {
var best *FinalRound
var start, height uint64
for id, rounds := range node.Graph.RoundHistory {
if !node.genesisNodesMap[id] && rounds[0].Number < 7+config.SnapshotReferenceThreshold*2 {
continue
}
if rl := node.Graph.ReverseRoundLinks[id]; rl > 0 && rl+1 >= node.Graph.CacheRound[node.IdForNetwork].Number {
continue
}
rts, rh := rounds[0].Start, uint64(len(rounds))
if id == node.IdForNetwork || rh < height {
continue
}
if rts > roundTime {
continue
}
if rts+config.SnapshotRoundGap*rh > uint64(clock.Now().UnixNano()) {
continue
}
if cr := node.Graph.CacheRound[id]; len(cr.Snapshots) == 0 && cr.Number == rounds[0].Number+1 {
continue
}
if rh > height || rts > start {
best = rounds[0]
start, height = rts, rh
}
}
return best
}