-
Notifications
You must be signed in to change notification settings - Fork 126
/
round.go
323 lines (295 loc) · 8.17 KB
/
round.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
package kernel
import (
"bytes"
"encoding/binary"
"fmt"
"sort"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/network"
"github.com/MixinNetwork/mixin/storage"
)
type CacheRound struct {
NodeId crypto.Hash
Number uint64
Timestamp uint64
References *common.RoundLink
Snapshots []*common.Snapshot `msgpack:"-"`
}
type FinalRound struct {
NodeId crypto.Hash
Number uint64
Start uint64
End uint64
Hash crypto.Hash
}
type RoundGraph struct {
Nodes []*crypto.Hash
CacheRound map[crypto.Hash]*CacheRound
FinalRound map[crypto.Hash]*FinalRound
RoundHistory map[crypto.Hash][]*FinalRound
ReverseRoundLinks map[crypto.Hash]uint64
GraphTimestamp uint64
FinalCache []*network.SyncPoint
MyCacheRound *CacheRound
MyFinalNumber uint64
}
func (g *RoundGraph) UpdateFinalCache(idForNetwork crypto.Hash) {
finals := make([]*network.SyncPoint, 0)
for _, f := range g.FinalRound {
finals = append(finals, &network.SyncPoint{
NodeId: f.NodeId,
Number: f.Number,
Hash: f.Hash,
})
if f.End > g.GraphTimestamp {
g.GraphTimestamp = f.End
}
}
g.FinalCache = finals
if g.FinalRound[idForNetwork] != nil {
g.MyCacheRound = g.CacheRound[idForNetwork]
g.MyFinalNumber = g.FinalRound[idForNetwork].Number
}
for id, rounds := range g.RoundHistory {
best := rounds[len(rounds)-1].Start
threshold := config.SnapshotReferenceThreshold * config.SnapshotRoundGap * 64
if rounds[0].Start+threshold > best && len(rounds) <= config.SnapshotReferenceThreshold {
continue
}
newRounds := make([]*FinalRound, 0)
for _, r := range rounds {
if r.Start+threshold <= best {
continue
}
newRounds = append(newRounds, r)
}
if rc := len(newRounds) - config.SnapshotReferenceThreshold; rc > 0 {
newRounds = append([]*FinalRound{}, newRounds[rc:]...)
}
g.RoundHistory[id] = newRounds
}
}
func (g *RoundGraph) Print() string {
desc := "ROUND GRAPH BEGIN\n"
for _, id := range g.Nodes {
desc = desc + fmt.Sprintf("NODE# %s\n", id)
final := g.FinalRound[*id]
desc = desc + fmt.Sprintf("FINAL %d %d %s\n", final.Number, final.Start, final.Hash)
cache := g.CacheRound[*id]
start, end := cache.Gap()
desc = desc + fmt.Sprintf("CACHE %d %d %d %d\n", cache.Number, cache.Timestamp, start, end)
}
desc = desc + "ROUND GRAPH END"
return desc
}
func LoadRoundGraph(store storage.Store, networkId, idForNetwork crypto.Hash) (*RoundGraph, error) {
graph := &RoundGraph{
CacheRound: make(map[crypto.Hash]*CacheRound),
FinalRound: make(map[crypto.Hash]*FinalRound),
RoundHistory: make(map[crypto.Hash][]*FinalRound),
ReverseRoundLinks: make(map[crypto.Hash]uint64),
}
allNodes := store.ReadAllNodes()
for _, cn := range allNodes {
if cn.State == common.NodeStatePledging || cn.State == common.NodeStateCancelled {
continue
}
id := cn.IdForNetwork(networkId)
graph.Nodes = append(graph.Nodes, &id)
cache, err := loadHeadRoundForNode(store, id)
if err != nil {
return nil, err
}
graph.CacheRound[cache.NodeId] = cache
final, err := loadFinalRoundForNode(store, id, cache.Number-1)
if err != nil {
return nil, err
}
rlink, err := store.ReadLink(final.NodeId, idForNetwork)
if err != nil {
return nil, err
}
graph.ReverseRoundLinks[final.NodeId] = rlink
history, err := loadRoundHistoryForNode(store, idForNetwork, final)
if err != nil {
return nil, err
}
graph.FinalRound[final.NodeId] = final
graph.RoundHistory[final.NodeId] = history
cache.Timestamp = final.Start + config.SnapshotRoundGap
}
graph.UpdateFinalCache(idForNetwork)
return graph, nil
}
func loadRoundHistoryForNode(store storage.Store, from crypto.Hash, to *FinalRound) ([]*FinalRound, error) {
var history []*FinalRound
link, err := store.ReadLink(from, to.NodeId)
if err != nil {
return nil, err
}
if link > to.Number {
panic(to)
}
start := to.Number - config.SnapshotReferenceThreshold
if start < link || to.Number < config.SnapshotReferenceThreshold {
start = link
}
for ; start <= to.Number; start++ {
final, err := loadFinalRoundForNode(store, to.NodeId, start)
if err != nil {
return nil, err
}
if final.Start+config.SnapshotReferenceThreshold*config.SnapshotRoundGap*64 < to.Start {
continue
}
history = append(history, final)
}
return history, nil
}
func loadHeadRoundForNode(store storage.Store, nodeIdWithNetwork crypto.Hash) (*CacheRound, error) {
meta, err := store.ReadRound(nodeIdWithNetwork)
if err != nil || meta == nil {
return nil, err
}
round := &CacheRound{
NodeId: nodeIdWithNetwork,
Number: meta.Number,
Timestamp: meta.Timestamp,
References: meta.References,
}
topos, err := store.ReadSnapshotsForNodeRound(round.NodeId, round.Number)
if err != nil {
return nil, err
}
for _, t := range topos {
s := &t.Snapshot
s.Hash = s.PayloadHash()
round.Snapshots = append(round.Snapshots, s)
}
return round, nil
}
func loadFinalRoundForNode(store storage.Store, nodeIdWithNetwork crypto.Hash, number uint64) (*FinalRound, error) {
topos, err := store.ReadSnapshotsForNodeRound(nodeIdWithNetwork, number)
if err != nil {
return nil, err
}
if len(topos) == 0 {
panic(nodeIdWithNetwork)
}
snapshots := make([]*common.Snapshot, len(topos))
for i, t := range topos {
s := &t.Snapshot
s.Hash = s.PayloadHash()
snapshots[i] = s
}
cache := &CacheRound{
NodeId: nodeIdWithNetwork,
Number: number,
Snapshots: snapshots,
}
return cache.asFinal(), nil
}
func (c *CacheRound) Copy() *CacheRound {
return &CacheRound{
NodeId: c.NodeId,
Number: c.Number,
Timestamp: c.Timestamp,
References: &common.RoundLink{
Self: c.References.Self,
External: c.References.External,
},
Snapshots: append([]*common.Snapshot{}, c.Snapshots...),
}
}
func (f *FinalRound) Copy() *FinalRound {
return &FinalRound{
NodeId: f.NodeId,
Number: f.Number,
Start: f.Start,
End: f.End,
Hash: f.Hash,
}
}
func (c *CacheRound) Gap() (uint64, uint64) {
start, end := (^uint64(0))/2, uint64(0)
count := len(c.Snapshots)
if count == 0 {
return start, end
}
sort.Slice(c.Snapshots, func(i, j int) bool {
return c.Snapshots[i].Timestamp < c.Snapshots[j].Timestamp
})
start = c.Snapshots[0].Timestamp
end = c.Snapshots[count-1].Timestamp
if end >= start+config.SnapshotRoundGap {
err := fmt.Errorf("GAP %s %d %d %d %d", c.NodeId, c.Number, start, end, start+config.SnapshotRoundGap)
panic(err)
}
return start, end
}
func (c *CacheRound) ValidateSnapshot(s *common.Snapshot, add bool) bool {
if !s.Hash.HasValue() {
panic(s)
}
for _, cs := range c.Snapshots {
if cs.Hash == s.Hash || cs.Timestamp == s.Timestamp {
return false
}
}
if start, end := c.Gap(); start <= end {
if s.Timestamp < start && s.Timestamp+config.SnapshotRoundGap <= end {
return false
}
if s.Timestamp > end && start+config.SnapshotRoundGap <= s.Timestamp {
return false
}
}
if add {
c.Snapshots = append(c.Snapshots, s)
}
return true
}
func ComputeRoundHash(nodeId crypto.Hash, number uint64, snapshots []*common.Snapshot) (uint64, uint64, crypto.Hash) {
sort.Slice(snapshots, func(i, j int) bool {
if snapshots[i].Timestamp < snapshots[j].Timestamp {
return true
}
if snapshots[i].Timestamp > snapshots[j].Timestamp {
return false
}
a, b := snapshots[i].Hash, snapshots[j].Hash
return bytes.Compare(a[:], b[:]) < 0
})
start := snapshots[0].Timestamp
end := snapshots[len(snapshots)-1].Timestamp
if end >= start+config.SnapshotRoundGap {
err := fmt.Errorf("ComputeRoundHash(%s, %d) %d %d %d", nodeId, number, start, end, start+config.SnapshotRoundGap)
panic(err)
}
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, number)
hash := crypto.NewHash(append(nodeId[:], buf...))
for _, s := range snapshots {
if s.Timestamp > end {
panic(nodeId)
}
hash = crypto.NewHash(append(hash[:], s.Hash[:]...))
}
return start, end, hash
}
func (c *CacheRound) asFinal() *FinalRound {
if len(c.Snapshots) == 0 {
return nil
}
start, end, hash := ComputeRoundHash(c.NodeId, c.Number, c.Snapshots)
round := &FinalRound{
NodeId: c.NodeId,
Number: c.Number,
Start: start,
End: end,
Hash: hash,
}
return round
}