-
Notifications
You must be signed in to change notification settings - Fork 127
/
self.go
109 lines (100 loc) · 3.43 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
package kernel
import (
"encoding/hex"
"fmt"
"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"
)
func (node *Node) checkCacheSnapshotTransaction(s *common.Snapshot) (*common.VersionedTransaction, bool, error) {
tx, finalized, err := node.persistStore.ReadTransaction(s.Transaction)
if err == nil && tx != nil {
err = node.validateKernelSnapshot(s, tx, false)
}
if err != nil || 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, false)
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, finalized bool) error {
switch tx.TransactionType() {
case common.TransactionTypeMint:
err := node.validateMintSnapshot(s, tx)
if err != nil {
logger.Verbosef("validateMintSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodePledge:
err := node.validateNodePledgeSnapshot(s, tx)
if err != nil {
logger.Verbosef("validateNodePledgeSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeCancel:
err := node.validateNodeCancelSnapshot(s, tx, finalized)
if err != nil {
logger.Verbosef("validateNodeCancelSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeAccept:
err := node.validateNodeAcceptSnapshot(s, tx, finalized)
if err != nil {
logger.Verbosef("validateNodeAcceptSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
return err
}
case common.TransactionTypeNodeRemove:
err := node.validateNodeRemoveSnapshot(s, tx)
if err != nil {
logger.Verbosef("validateNodeRemoveSnapshot ERROR %v %s %s\n", s, hex.EncodeToString(tx.PayloadMarshal()), err.Error())
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(nodeId crypto.Hash, roundTime uint64) *FinalRound {
var best *FinalRound
var start, height uint64
for id, rounds := range node.Graph.RoundHistory {
r, rts, rh := rounds[0], rounds[0].Start, uint64(len(rounds))
if id == nodeId || rh < height || rts > roundTime {
continue
}
if !node.genesisNodesMap[id] && r.Number < 7+config.SnapshotReferenceThreshold*2 {
continue
}
if rl := node.Graph.ReverseRoundLinks[id]; rl > 0 && rl+1 >= node.Graph.FinalRound[nodeId].Number {
continue
}
if rts+config.SnapshotRoundGap*rh > uint64(clock.Now().UnixNano()) {
continue
}
if cr := node.Graph.CacheRound[id]; len(cr.Snapshots) == 0 && cr.Number == r.Number+1 && r.Number > 0 {
continue
}
if rh > height || rts > start {
best, start, height = r, rts, rh
}
}
return best
}