-
Notifications
You must be signed in to change notification settings - Fork 127
/
election.go
561 lines (512 loc) · 19.4 KB
/
election.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
554
555
556
557
558
559
560
561
package kernel
import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"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"
)
const (
MainnetAcceptPeriodForkSnapshotHash = "b8855c19a38999f283d9be6daa45147aef47cc6d35007673f62390c2e137e4e1"
)
func (node *Node) ElectionLoop() {
defer close(node.elc)
ticker := time.NewTicker(time.Duration(node.custom.Node.KernelOprationPeriod) * time.Second)
defer ticker.Stop()
for node.Graph.MyCacheRound == nil {
select {
case <-node.done:
return
case <-ticker.C:
now := uint64(clock.Now().UnixNano())
if now < node.Epoch {
logger.Printf("LOCAL TIME INVALID %d %d\n", now, node.Epoch)
continue
}
hours := int((now-node.Epoch)/3600000000000) % 24
if hours < config.KernelNodeAcceptTimeBegin || hours > config.KernelNodeAcceptTimeEnd {
continue
}
err := node.tryToSendAcceptTransaction()
if err != nil {
logger.Println("tryToSendAcceptTransaction", err)
}
}
}
for {
select {
case <-node.done:
return
case <-ticker.C:
candi, err := node.checkRemovePossibility(node.IdForNetwork, node.Graph.GraphTimestamp)
if err != nil {
logger.Printf("checkRemovePossibility %s", err.Error())
continue
}
err = node.tryToSendRemoveTransaction(candi)
if err != nil {
logger.Println("tryToSendRemoveTransaction", err)
}
}
}
}
func (node *Node) checkRemovePossibility(nodeId crypto.Hash, now uint64) (*common.Node, error) {
if p := node.ConsensusPledging; p != nil {
return nil, fmt.Errorf("still pledging now %s", p.Signer.String())
}
if now < node.Epoch {
return nil, fmt.Errorf("local time invalid %d %d", now, node.Epoch)
}
hours := (now - node.Epoch) / 3600000000000
if hours%24 < config.KernelNodeAcceptTimeBegin || hours%24 > config.KernelNodeAcceptTimeEnd {
return nil, fmt.Errorf("invalid node remove hour %d", hours%24)
}
nodes := node.SortAllNodesByTimestampAndId()
candi := nodes[0]
for _, cn := range nodes {
if cn.Timestamp == 0 {
cn.Timestamp = node.Epoch
}
if now < cn.Timestamp {
return nil, fmt.Errorf("invalid timestamp %d %d", cn.Timestamp, now)
}
elapse := time.Duration(now - cn.Timestamp)
if elapse < config.KernelNodePledgePeriodMinimum {
return nil, fmt.Errorf("invalid period %d %d %d %d", config.KernelNodePledgePeriodMinimum, elapse, now, cn.Timestamp)
}
if cn.State != common.NodeStateAccepted && cn.State != common.NodeStateCancelled && cn.State != common.NodeStateRemoved {
return nil, fmt.Errorf("invalid node pending state %s %s", cn.Signer, cn.State)
}
if cn.State == common.NodeStateAccepted && candi.State != common.NodeStateAccepted {
candi = cn
}
}
if candi.State != common.NodeStateAccepted {
return nil, fmt.Errorf("invalid node state %s %s", candi.IdForNetwork(node.networkId), candi.State)
}
days := int((now - node.Epoch) / 3600000000000 / 24)
threshold := time.Duration(days/MintYearBatches*MintYearBatches) * 24 * time.Hour
if t := node.Epoch + uint64(threshold); candi.Timestamp >= t {
return nil, fmt.Errorf("all old nodes removed %d %d %d %d", candi.Timestamp, t, now, days)
}
if candi.IdForNetwork(node.networkId) == nodeId {
return nil, fmt.Errorf("never handle the node remove transaction by the node self")
}
return candi, nil
}
func (node *Node) tryToSendRemoveTransaction(candi *common.Node) error {
tx, err := node.buildRemoveTransaction(candi)
if err != nil {
return err
}
err = tx.Validate(node.persistStore)
if err != nil {
return err
}
err = node.persistStore.CachePutTransaction(tx)
if err != nil {
return err
}
return node.QueueAppendSnapshot(node.IdForNetwork, &common.Snapshot{
Version: common.SnapshotVersion,
NodeId: node.IdForNetwork,
Transaction: tx.PayloadHash(),
}, false)
}
func (node *Node) buildRemoveTransaction(candi *common.Node) (*common.VersionedTransaction, error) {
accept, _, err := node.persistStore.ReadTransaction(candi.Transaction)
if err != nil {
return nil, err
}
if accept == nil {
return nil, fmt.Errorf("accept transaction not available yet %s", candi.Transaction)
}
if accept.PayloadHash() != candi.Transaction {
return nil, fmt.Errorf("accept transaction malformed %s %s", candi.Transaction, accept.PayloadHash())
}
signer := candi.Signer.PublicSpendKey
payee := candi.Payee.PublicSpendKey[:]
if len(accept.Extra) != len(signer)*2 {
return nil, fmt.Errorf("invalid accept transaction extra %s", hex.EncodeToString(accept.Extra))
}
if bytes.Compare(append(signer[:], payee...), accept.Extra) != 0 {
return nil, fmt.Errorf("invalid accept transaction extra %s %s %s", hex.EncodeToString(accept.Extra), signer, hex.EncodeToString(payee))
}
tx := common.NewTransaction(common.XINAssetId)
tx.AddInput(candi.Transaction, 0)
tx.Extra = accept.Extra
script := common.NewThresholdScript(1)
in := fmt.Sprintf("NODEREMOVE%s", candi.Signer.String())
si := crypto.NewHash([]byte(candi.Payee.String() + in))
seed := append(si[:], si[:]...)
tx.AddOutputWithType(common.OutputTypeNodeRemove, []common.Address{candi.Payee}, script, accept.Outputs[0].Amount, seed)
return tx.AsLatestVersion(), nil
}
func (node *Node) tryToSendAcceptTransaction() error {
pledging := node.ConsensusPledging
if pledging == nil {
return fmt.Errorf("no consensus pledging node")
}
if pledging.Signer.String() != node.Signer.String() {
return fmt.Errorf("invalid consensus pledging node %s %s", pledging.Signer, node.Signer)
}
pledge, _, err := node.persistStore.ReadTransaction(pledging.Transaction)
if err != nil {
return err
}
if pledge == nil {
return fmt.Errorf("pledge transaction not available yet %s", pledging.Transaction)
}
if pledge.PayloadHash() != pledging.Transaction {
return fmt.Errorf("pledge transaction malformed %s %s", pledging.Transaction, pledge.PayloadHash())
}
signer := node.Signer.PublicSpendKey
if len(pledge.Extra) != len(signer)*2 {
return fmt.Errorf("invalid pledge transaction extra %s", hex.EncodeToString(pledge.Extra))
}
if bytes.Compare(signer[:], pledge.Extra[:len(signer)]) != 0 {
return fmt.Errorf("invalid pledge transaction extra %s %s", hex.EncodeToString(pledge.Extra[:len(signer)]), signer)
}
tx := common.NewTransaction(common.XINAssetId)
tx.AddInput(pledging.Transaction, 0)
tx.AddOutputWithType(common.OutputTypeNodeAccept, nil, common.Script{}, pledge.Outputs[0].Amount, []byte{})
tx.Extra = pledge.Extra
ver := tx.AsLatestVersion()
err = ver.Validate(node.persistStore)
if err != nil {
return err
}
err = node.persistStore.CachePutTransaction(ver)
if err != nil {
return err
}
err = node.persistStore.QueueAppendSnapshot(node.IdForNetwork, &common.Snapshot{
Version: common.SnapshotVersion,
NodeId: node.IdForNetwork,
Transaction: ver.PayloadHash(),
}, false)
logger.Println("tryToSendAcceptTransaction", ver.PayloadHash(), hex.EncodeToString(ver.Marshal()))
return nil
}
func (node *Node) reloadConsensusNodesList(s *common.Snapshot, tx *common.VersionedTransaction) error {
switch tx.TransactionType() {
case common.TransactionTypeNodePledge,
common.TransactionTypeNodeCancel,
common.TransactionTypeNodeAccept,
common.TransactionTypeNodeResign,
common.TransactionTypeNodeRemove:
err := node.LoadConsensusNodes()
if err != nil {
return err
}
graph, err := LoadRoundGraph(node.persistStore, node.networkId, node.IdForNetwork)
if err != nil {
return err
}
node.Graph = graph
}
return nil
}
func (node *Node) finalizeNodeAcceptSnapshot(s *common.Snapshot) error {
cache := &CacheRound{
NodeId: s.NodeId,
Number: s.RoundNumber,
Timestamp: s.Timestamp,
}
if err := cache.ValidateSnapshot(s, true); err != nil {
panic("should never be here")
}
err := node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, cache.Timestamp)
if err != nil {
panic(err)
}
topo := &common.SnapshotWithTopologicalOrder{
Snapshot: *s,
TopologicalOrder: node.TopoCounter.Next(),
}
err = node.persistStore.WriteSnapshot(topo)
if err != nil {
panic(err)
}
final := cache.asFinal()
external, err := node.getInitialExternalReference(s)
if err != nil {
panic(err)
}
cache = &CacheRound{
NodeId: s.NodeId,
Number: 1,
Timestamp: s.Timestamp + config.SnapshotRoundGap + 1,
References: &common.RoundLink{
Self: final.Hash,
External: external.Hash,
},
}
err = node.persistStore.StartNewRound(cache.NodeId, cache.Number, cache.References, cache.Timestamp)
if err != nil {
panic(err)
}
node.assignNewGraphRound(final, cache)
return nil
}
func (node *Node) getInitialExternalReference(s *common.Snapshot) (*FinalRound, error) {
nodeDistance := func(a, b crypto.Hash) int {
ai := new(big.Int).SetBytes(a[:])
bi := new(big.Int).SetBytes(b[:])
si := new(big.Int).Sub(ai, bi)
ai = new(big.Int).Abs(si)
mi := new(big.Int).Mod(ai, big.NewInt(100))
return int(mi.Int64())
}
externalId := node.genesisNodes[0]
distance := nodeDistance(s.NodeId, externalId)
for _, id := range node.genesisNodes {
nd := nodeDistance(s.NodeId, id)
if nd < distance {
distance = nd
externalId = id
}
}
return loadFinalRoundForNode(node.persistStore, externalId, 0)
}
func (node *Node) validateNodePledgeSnapshot(s *common.Snapshot, tx *common.VersionedTransaction) error {
timestamp := s.Timestamp
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
}
if timestamp < node.Epoch {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.Epoch, timestamp)
}
since := timestamp - node.Epoch
days := int(since / 3600000000000 / 24)
elp := time.Duration((days%MintYearBatches)*24) * time.Hour
eta := time.Duration((MintYearBatches-days%MintYearBatches)*24) * time.Hour
if eta < config.KernelNodeAcceptPeriodMaximum*2 || elp < config.KernelNodeAcceptPeriodMinimum*2 {
return fmt.Errorf("invalid pledge timestamp %d %d", eta, elp)
}
var signerSpend crypto.Key
copy(signerSpend[:], tx.Extra)
for _, cn := range node.persistStore.ReadAllNodes() {
if cn.Timestamp == 0 {
cn.Timestamp = node.Epoch
}
if timestamp < cn.Timestamp {
return fmt.Errorf("invalid snapshot timestamp %d %d", cn.Timestamp, timestamp)
}
elapse := time.Duration(timestamp - cn.Timestamp)
if elapse < config.KernelNodePledgePeriodMinimum {
return fmt.Errorf("invalid pledge period %d %d", config.KernelNodePledgePeriodMinimum, elapse)
}
if cn.State != common.NodeStateAccepted && cn.State != common.NodeStateCancelled && cn.State != common.NodeStateRemoved {
return fmt.Errorf("invalid node pending state %s %s", cn.Signer, cn.State)
}
if cn.Signer.PublicSpendKey.String() == signerSpend.String() {
return fmt.Errorf("invalid node signer key %s %s", hex.EncodeToString(tx.Extra), cn.Signer)
}
if cn.Payee.PublicSpendKey.String() == signerSpend.String() {
return fmt.Errorf("invalid node signer key %s %s", hex.EncodeToString(tx.Extra), cn.Payee)
}
}
if cn := node.ConsensusPledging; cn != nil {
return fmt.Errorf("invalid node state %s %s", cn.Signer, cn.State)
}
if tx.Asset != common.XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 1 {
return fmt.Errorf("invalid outputs count %d for pledge transaction", len(tx.Outputs))
}
if len(tx.Extra) != 2*len(crypto.Key{}) {
return fmt.Errorf("invalid extra length %d for pledge transaction", len(tx.Extra))
}
if tx.Outputs[0].Amount.Cmp(pledgeAmount(time.Duration(since))) != 0 {
return fmt.Errorf("invalid pledge amount %s", tx.Outputs[0].Amount.String())
}
// FIXME the node operation lock threshold should be optimized on pledging period
return node.persistStore.AddNodeOperation(tx, timestamp, uint64(config.KernelNodePledgePeriodMinimum)*2)
}
func (node *Node) validateNodeCancelSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
if tx.Asset != common.XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 2 {
return fmt.Errorf("invalid outputs count %d for cancel transaction", len(tx.Outputs))
}
if len(tx.Inputs) != 1 {
return fmt.Errorf("invalid inputs count %d for cancel transaction", len(tx.Inputs))
}
if len(tx.Extra) != len(crypto.Key{})*3 {
return fmt.Errorf("invalid extra %s for cancel transaction", hex.EncodeToString(tx.Extra))
}
cancel, script := tx.Outputs[0], tx.Outputs[1]
if cancel.Type != common.OutputTypeNodeCancel || script.Type != common.OutputTypeScript {
return fmt.Errorf("invalid outputs type %d %d for cancel transaction", cancel.Type, script.Type)
}
if len(script.Keys) != 1 {
return fmt.Errorf("invalid script output keys %d for cancel transaction", len(script.Keys))
}
if node.ConsensusPledging == nil {
return fmt.Errorf("invalid consensus status")
}
if node.ConsensusPledging.Transaction != tx.Inputs[0].Hash {
return fmt.Errorf("invalid plede utxo source %s %s", node.ConsensusPledging.Transaction, tx.Inputs[0].Hash)
}
pledge, _, err := node.persistStore.ReadTransaction(tx.Inputs[0].Hash)
if err != nil {
return err
}
if len(pledge.Outputs) != 1 {
return fmt.Errorf("invalid pledge utxo count %d", len(pledge.Outputs))
}
if pledge.Outputs[0].Type != common.OutputTypeNodePledge {
return fmt.Errorf("invalid pledge utxo type %d", pledge.Outputs[0].Type)
}
if cancel.Amount.Cmp(pledge.Outputs[0].Amount.Div(100)) != 0 {
return fmt.Errorf("invalid script output amount %s for cancel transaction", cancel.Amount)
}
pit, _, err := node.persistStore.ReadTransaction(pledge.Inputs[0].Hash)
if err != nil {
return err
}
if pit == nil {
return fmt.Errorf("invalid pledge input source %s:%d", pledge.Inputs[0].Hash, pledge.Inputs[0].Index)
}
pi := pit.Outputs[pledge.Inputs[0].Index]
if len(pi.Keys) != 1 {
return fmt.Errorf("invalid pledge input source keys %d", len(pi.Keys))
}
var a crypto.Key
copy(a[:], tx.Extra[len(crypto.Key{})*2:])
pledgeSpend := crypto.ViewGhostOutputKey(&pi.Keys[0], &a, &pi.Mask, uint64(pledge.Inputs[0].Index))
targetSpend := crypto.ViewGhostOutputKey(&script.Keys[0], &a, &script.Mask, 1)
if bytes.Compare(pledge.Extra, tx.Extra[:len(crypto.Key{})*2]) != 0 {
return fmt.Errorf("invalid pledge and accpet key %s %s", hex.EncodeToString(pledge.Extra), hex.EncodeToString(tx.Extra))
}
if bytes.Compare(pledgeSpend[:], targetSpend[:]) != 0 {
return fmt.Errorf("invalid pledge and cancel target %s %s", pledgeSpend, targetSpend)
}
timestamp := s.Timestamp
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
}
if timestamp < node.Epoch {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.Epoch, timestamp)
}
since := timestamp - node.Epoch
hours := int(since / 3600000000000)
if hours%24 < config.KernelNodeAcceptTimeBegin || hours%24 > config.KernelNodeAcceptTimeEnd {
return fmt.Errorf("invalid node cancel hour %d", hours%24)
}
threshold := config.SnapshotRoundGap * config.SnapshotReferenceThreshold
if !finalized && timestamp+threshold*2 < node.Graph.GraphTimestamp {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.Graph.GraphTimestamp, timestamp)
}
if timestamp < node.ConsensusPledging.Timestamp {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.ConsensusPledging.Timestamp, timestamp)
}
elapse := time.Duration(timestamp - node.ConsensusPledging.Timestamp)
if elapse < config.KernelNodeAcceptPeriodMinimum {
return fmt.Errorf("invalid cancel period %d %d", config.KernelNodeAcceptPeriodMinimum, elapse)
}
if elapse > config.KernelNodeAcceptPeriodMaximum {
return fmt.Errorf("invalid cancel period %d %d", config.KernelNodeAcceptPeriodMaximum, elapse)
}
// FIXME the node operation lock threshold should be optimized on pledging period
return node.persistStore.AddNodeOperation(tx, timestamp, uint64(config.KernelNodePledgePeriodMinimum)*2)
}
func (node *Node) validateNodeRemoveSnapshot(s *common.Snapshot, tx *common.VersionedTransaction) error {
timestamp := s.Timestamp
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
}
candi, err := node.checkRemovePossibility(s.NodeId, timestamp)
if err != nil {
return err
}
cantx, err := node.buildRemoveTransaction(candi)
if err != nil {
return err
}
if cantx.PayloadHash() != tx.PayloadHash() {
return fmt.Errorf("invalid node remove transaction %s %s", cantx.PayloadHash(), tx.PayloadHash())
}
return nil
}
func (node *Node) validateNodeAcceptSnapshot(s *common.Snapshot, tx *common.VersionedTransaction, finalized bool) error {
if tx.Asset != common.XINAssetId {
return fmt.Errorf("invalid node asset %s", tx.Asset.String())
}
if len(tx.Outputs) != 1 {
return fmt.Errorf("invalid outputs count %d for accept transaction", len(tx.Outputs))
}
if len(tx.Inputs) != 1 {
return fmt.Errorf("invalid inputs count %d for accept transaction", len(tx.Inputs))
}
if node.ConsensusPledging == nil {
return fmt.Errorf("invalid consensus status")
}
if id := node.ConsensusPledging.IdForNetwork(node.networkId); id != s.NodeId {
return fmt.Errorf("invalid pledging node %s %s", id, s.NodeId)
}
if node.ConsensusPledging.Transaction != tx.Inputs[0].Hash {
return fmt.Errorf("invalid plede utxo source %s %s", node.ConsensusPledging.Transaction, tx.Inputs[0].Hash)
}
pledge, _, err := node.persistStore.ReadTransaction(tx.Inputs[0].Hash)
if err != nil {
return err
}
if len(pledge.Outputs) != 1 {
return fmt.Errorf("invalid pledge utxo count %d", len(pledge.Outputs))
}
if pledge.Outputs[0].Type != common.OutputTypeNodePledge {
return fmt.Errorf("invalid pledge utxo type %d", pledge.Outputs[0].Type)
}
if bytes.Compare(pledge.Extra, tx.Extra) != 0 {
return fmt.Errorf("invalid pledge and accpet key %s %s", hex.EncodeToString(pledge.Extra), hex.EncodeToString(tx.Extra))
}
timestamp := s.Timestamp
if s.RoundNumber != 0 {
return fmt.Errorf("invalid snapshot round %d", s.RoundNumber)
}
if s.Timestamp == 0 && s.NodeId == node.IdForNetwork {
timestamp = uint64(clock.Now().UnixNano())
}
if timestamp < node.Epoch {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.Epoch, timestamp)
}
if r := node.Graph.CacheRound[s.NodeId]; r != nil {
return fmt.Errorf("invalid graph round %s %d", s.NodeId, r.Number)
}
if r := node.Graph.FinalRound[s.NodeId]; r != nil {
return fmt.Errorf("invalid graph round %s %d", s.NodeId, r.Number)
}
since := timestamp - node.Epoch
hours := int(since / 3600000000000)
if hours%24 < config.KernelNodeAcceptTimeBegin || hours%24 > config.KernelNodeAcceptTimeEnd {
return fmt.Errorf("invalid node accept hour %d", hours%24)
}
threshold := config.SnapshotRoundGap * config.SnapshotReferenceThreshold
if !finalized && timestamp+threshold*2 < node.Graph.GraphTimestamp {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.Graph.GraphTimestamp, timestamp)
}
if timestamp < node.ConsensusPledging.Timestamp {
return fmt.Errorf("invalid snapshot timestamp %d %d", node.ConsensusPledging.Timestamp, timestamp)
}
elapse := time.Duration(timestamp - node.ConsensusPledging.Timestamp)
if elapse < config.KernelNodeAcceptPeriodMinimum {
if s.PayloadHash().String() == MainnetAcceptPeriodForkSnapshotHash {
logger.Printf("FORK invalid accept period %d %d\n", config.KernelNodeAcceptPeriodMinimum, elapse)
} else {
return fmt.Errorf("invalid accept period %d %d", config.KernelNodeAcceptPeriodMinimum, elapse)
}
}
if elapse > config.KernelNodeAcceptPeriodMaximum {
return fmt.Errorf("invalid accept period %d %d", config.KernelNodeAcceptPeriodMaximum, elapse)
}
return nil
}