-
Notifications
You must be signed in to change notification settings - Fork 127
/
final.go
142 lines (127 loc) · 3.36 KB
/
final.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
package kernel
import (
"github.com/MixinNetwork/mixin/common"
)
func (node *Node) checkFinalSnapshotTransaction(s *common.Snapshot) (*common.VersionedTransaction, error) {
inNode, err := node.persistStore.CheckTransactionInNode(s.NodeId, s.Transaction)
if err != nil || inNode {
return nil, err
}
tx, err := node.persistStore.ReadTransaction(s.Transaction)
if err != nil || tx != nil {
return tx, err
}
tx, err = node.persistStore.CacheGetTransaction(s.Transaction)
if err != nil || tx == nil {
return nil, err
}
err = tx.LockInputs(node.persistStore, true)
if err != nil {
return nil, err
}
if d := tx.DepositData(); d != nil {
err = node.persistStore.WriteAsset(d.Asset())
if err != nil {
return nil, err
}
}
return tx, node.persistStore.WriteTransaction(tx)
}
func (node *Node) tryToStartNewRound(s *common.Snapshot) error {
if s.RoundNumber == 0 {
return nil
}
cache := node.Graph.CacheRound[s.NodeId].Copy()
final := node.Graph.FinalRound[s.NodeId].Copy()
if s.RoundNumber != cache.Number+1 {
return nil
}
if round, err := node.startNewRound(s, cache); err != nil {
return err
} else if round == nil {
return nil
} else {
final = round
}
cache = &CacheRound{
NodeId: s.NodeId,
Number: s.RoundNumber,
Timestamp: s.Timestamp,
References: s.References,
}
err := node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, final.Start)
if err != nil {
panic(err)
}
node.assignNewGraphRound(final, cache)
return nil
}
func (node *Node) handleSyncFinalSnapshot(s *common.Snapshot, tx *common.VersionedTransaction) error {
if node.checkInitialAcceptSnapshot(s, tx) {
err := node.finalizeNodeAcceptSnapshot(s)
if err != nil {
return err
}
return node.reloadConsensusNodesList(s, tx)
}
cache := node.Graph.CacheRound[s.NodeId].Copy()
final := node.Graph.FinalRound[s.NodeId].Copy()
if s.RoundNumber < cache.Number {
return nil
}
if s.RoundNumber > cache.Number+1 {
return node.queueSnapshotOrPanic(s, true)
}
if s.RoundNumber == cache.Number && !s.References.Equal(cache.References) {
if s.NodeId == node.IdForNetwork {
return nil
}
if len(cache.Snapshots) != 0 {
return nil
}
err := node.persistStore.UpdateEmptyHeadRound(cache.NodeId, cache.Number, s.References)
if err != nil {
panic(err)
}
cache.References = s.References
node.assignNewGraphRound(final, cache)
return node.handleSnapshotInput(s)
}
if s.RoundNumber == cache.Number+1 {
if round, err := node.startNewRound(s, cache); err != nil {
return node.queueSnapshotOrPanic(s, true)
} else if round == nil {
return nil
} else {
final = round
}
cache = &CacheRound{
NodeId: s.NodeId,
Number: s.RoundNumber,
Timestamp: s.Timestamp,
References: s.References,
}
err := node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, final.Start)
if err != nil {
panic(err)
}
}
node.assignNewGraphRound(final, cache)
if !cache.ValidateSnapshot(s, false) {
return nil
}
topo := &common.SnapshotWithTopologicalOrder{
Snapshot: *s,
TopologicalOrder: node.TopoCounter.Next(),
}
err := node.persistStore.WriteSnapshot(topo)
if err != nil {
panic(err)
}
if !cache.ValidateSnapshot(s, true) {
panic("should never be here")
}
node.assignNewGraphRound(final, cache)
node.removeFromCache(s)
return node.reloadConsensusNodesList(s, tx)
}