forked from MixinNetwork/mixin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
108 lines (95 loc) · 3.04 KB
/
space.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
package kernel
import (
"fmt"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/logger"
)
func (chain *Chain) AggregateRoundSpace() {
logger.Printf("AggregateRoundSpace(%s)\n", chain.ChainId)
defer close(chain.slc)
batch, round, err := chain.persistStore.ReadRoundSpaceCheckpoint(chain.ChainId)
if err != nil {
panic(err)
}
logger.Printf("AggregateRoundSpace(%s) begin with %d:%d\n", chain.ChainId, batch, round)
wait := time.Duration(chain.node.custom.Node.KernelOprationPeriod/2) * time.Second
for chain.running {
if cs := chain.State; cs == nil {
logger.Printf("AggregateRoundSpace(%s) no state yet\n", chain.ChainId)
chain.waitOrDone(wait)
continue
}
frn := chain.State.FinalRound.Number
if frn < round {
panic(fmt.Errorf("AggregateRoundSpace(%s) waiting %d %d", chain.ChainId, frn, round))
}
if frn < round+1 {
chain.waitOrDone(wait)
continue
}
nextTime, err := chain.readFinalRoundTimestamp(round + 1)
if err != nil {
logger.Verbosef("AggregateRoundSpace(%s) ERROR readFinalRoundTimestamp %d %v\n",
chain.ChainId, round+1, err)
continue
}
checkTime, err := chain.readFinalRoundTimestamp(round)
if err != nil {
logger.Verbosef("AggregateRoundSpace(%s) ERROR readFinalRoundTimestamp %d %v\n",
chain.ChainId, round, err)
continue
}
since := checkTime - chain.node.Epoch
batch := uint64(since/3600000000000) / 24
space := &common.RoundSpace{
NodeId: chain.ChainId,
Batch: batch,
Round: round,
Duration: nextTime - checkTime,
}
if space.Round == 0 {
space.Duration = 0
}
if space.Duration > uint64(config.CheckpointDuration) {
logger.Printf("AggregateRoundSpace(%s) => large gap %d:%d %d\n", chain.ChainId, batch, round, space.Duration)
} else {
space.Duration = 0
}
err = chain.persistStore.WriteRoundSpaceAndState(space)
if err != nil {
logger.Verbosef("AggregateRoundSpace(%s) ERROR WriteRoundSpaceAndState %d %v\n", chain.ChainId, round, err)
continue
}
round = round + 1
}
batch, round, err = chain.persistStore.ReadRoundSpaceCheckpoint(chain.ChainId)
if err != nil {
panic(err)
}
logger.Printf("AggregateRoundSpace(%s) end with %d:%d\n", chain.ChainId, batch, round)
}
func (chain *Chain) readFinalRoundTimestamp(round uint64) (uint64, error) {
snapshots, err := chain.persistStore.ReadSnapshotsForNodeRound(chain.ChainId, round)
if err != nil {
return 0, err
}
if len(snapshots) == 0 {
panic(fmt.Errorf("readFinalRoundTimestamp(%s, %d) empty round", chain.ChainId, round))
}
rawSnapshots := make([]*common.Snapshot, len(snapshots))
for i, s := range snapshots {
rawSnapshots[i] = s.Snapshot
}
start, _, hash := common.ComputeRoundHash(chain.ChainId, round, rawSnapshots)
r, err := chain.persistStore.ReadRound(hash)
if err != nil {
return 0, err
}
if r.Timestamp != start || r.Number != round {
panic(fmt.Errorf("readFinalRoundTimestamp(%s, %d) => malformed round attributes %d %d",
chain.ChainId, round, start, r.Timestamp))
}
return r.Timestamp, nil
}