/
process.go
348 lines (310 loc) · 11.9 KB
/
process.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
package sync
import (
"errors"
"github.com/fractal-platform/fractal/common"
"github.com/fractal-platform/fractal/core/types"
"github.com/fractal-platform/fractal/ftl/downloader"
"github.com/fractal-platform/fractal/ftl/network"
"github.com/fractal-platform/fractal/ftl/protocol"
"github.com/fractal-platform/fractal/rlp"
"github.com/fractal-platform/fractal/utils/log"
"github.com/deckarep/golang-set"
"github.com/ratelimit"
"time"
)
func (s *Synchronizer) ProcessNodeData(p *network.Peer, data [][]byte) {
if s.stateSync != nil {
s.stateSync.DeliverData(p.GetID(), data)
}
}
func (s *Synchronizer) ProcessTxPackagesReq(peer *network.Peer, stage protocol.SyncStage, pkgHashes []common.Hash, bucket *ratelimit.Bucket, waitTime time.Duration) {
log.Info("Recv fectch pkgs req", "peer", peer.Name(), "stage", stage, "length", len(pkgHashes))
if !s.IsSyncStatusNormal() || s.cp2fp.isRunning() {
err := peer.SendSyncPkgs(protocol.SyncPkgsRsp{
Stage: stage,
Pkgs: types.TxPackages{},
})
if err != nil {
log.Error("Process sync req failed", "peer", peer.Name(), "err", err)
}
return
}
var pkgs []*types.TxPackage
go func() {
for _, pkghash := range pkgHashes {
pkg := s.chain.GetTxPackage(pkghash)
if pkg != nil {
pkgs = append(pkgs, pkg)
}
}
fetchPkgsRsp := protocol.SyncPkgsRsp{
Stage: stage,
Pkgs: pkgs,
}
encoded, _ := rlp.EncodeToBytes(fetchPkgsRsp)
size := len(encoded)
log.Info("Process sync pkgs req", "peer", peer.Name(), "pkgs", len(pkgs), "size", size)
bucket.WaitMaxDuration(int64(size), waitTime)
err := peer.SendSyncPkgs(fetchPkgsRsp)
if err != nil {
log.Error("Process sync req failed", "peer", peer.Name(), "err", err)
}
}()
}
func (s *Synchronizer) ProcessTxPackagesRsp(peer *network.Peer, stage protocol.SyncStage, pkgs []*types.TxPackage) {
if stage == protocol.SyncStageCP2FP {
s.cp2fp.deliverData(peer.GetID(), pkgs, downloader.Pkgs)
} else {
if s.blockSync != nil {
s.blockSync.DeliverData(peer.GetID(), pkgs, downloader.Pkgs)
}
}
}
func (s *Synchronizer) ProcessBlocksReq(peer *network.Peer, stage protocol.SyncStage, roundFrom uint64, roundTo uint64) error {
currentRound := s.chain.CurrentBlock().Header.Round
genesisRound := s.chain.Genesis().Header.Round
log.Info("Recv fetch blocks req", "peer", peer.Name(), "RoundFrom", roundFrom, "RoundTo", roundTo, "currentRound", currentRound, "genesisRound", genesisRound)
if roundFrom > currentRound || roundFrom < genesisRound-1 {
err := errors.New("The requested round is beyond current round or older than genesis round.")
return err
}
if !s.IsSyncStatusNormal() || s.cp2fp.isRunning() {
err := peer.SendSyncBlocks(stage, types.Blocks{}, roundFrom, roundTo)
if err != nil {
log.Error("Process sync req failed", "peer", peer.Name(), "err", err)
}
return nil
}
var blocks types.Blocks
go func() {
if currentRound > roundTo {
blocks = s.chain.GetBlocksFromRoundRange(roundFrom, roundTo)
} else {
blocks = s.chain.GetBlocksFromRoundRange(roundFrom, currentRound)
}
if blocks == nil {
blocks = types.Blocks{}
}
log.Info("Process sync req", "peer", peer.Name(), "RoundFrom", roundFrom, "RoundTo", roundTo, "blocks", len(blocks))
//pm.bucket.WaitMaxDuration(int64(size), waitTime)
err := peer.SendSyncBlocks(stage, blocks, roundFrom, roundTo)
if err != nil {
log.Error("Process sync req failed", "peer", peer.Name(), "err", err)
}
}()
return nil
}
func (s *Synchronizer) ProcessBlocksRsp(peer *network.Peer, stage protocol.SyncStage, blocks types.Blocks) {
if stage == protocol.SyncStageCP2FP {
s.cp2fp.deliverData(peer.GetID(), blocks, downloader.Blocks)
} else {
if s.blockSync != nil {
s.blockSync.DeliverData(peer.GetID(), blocks, downloader.Blocks)
}
}
}
func (s *Synchronizer) ProcessSyncPreBlocksForStateReq(p *network.Peer, hash common.Hash) error {
var blocks []*types.Block
currentBlock := s.chain.GetBlock(hash)
if currentBlock == nil {
log.Error("Process SyncPreBlocksForStateReqMsg failed: can't find block")
return errors.New("can't find Blocks for SyncPreBlocksForStateReqMsg")
}
log.Info("Send sync state specific pre block", "Hash", currentBlock.FullHash(), "Height", currentBlock.Header.Height, "round", currentBlock.Header.Round)
if currentBlock.FullHash().String() == s.chain.Genesis().FullHash().String() {
log.Info("no need to get pre Blocks")
} else {
//get maxHeightDistance+greedy Blocks for state
//consider if height < lengthForStatesSync ,then sync height Blocks
var length uint64
if currentBlock.Header.Height >= uint64(s.lengthForStatesSync())-1 {
length = uint64(s.lengthForStatesSync())
} else {
length = currentBlock.Header.Height + 1
}
blocks = append(blocks, currentBlock)
for i := uint64(0); i < length-1; i++ {
hash := currentBlock.Header.ParentFullHash
currentBlock = s.chain.GetBlock(hash)
if currentBlock == nil {
log.Error("Process SyncPreBlocksForStateReqMsg failed: can't find block", "hash", hash)
return errors.New("can't find Blocks for SyncPreBlocksForStateReqMsg")
}
blocks = append(blocks, currentBlock)
}
//get greedy size Blocks for block process
roundFrom := blocks[int(length-1)].Header.Round
roundTo := blocks[0].Header.Round
blocks = append(blocks, s.chain.GetBlocksFromRoundRange(roundFrom, roundTo)...)
var pkgHashSet = mapset.NewSet()
var pkgs []*types.TxPackage
for _, block := range blocks {
for _, pkgHash := range block.Body.TxPackageHashes {
if !pkgHashSet.Contains(pkgHash) {
pkg := s.chain.GetTxPackage(pkgHash)
if pkg != nil {
pkgs = append(pkgs, pkg)
}
pkgHashSet.Add(pkgHash)
}
}
}
go func() {
err := p.SendSyncPreBlocksForState(blocks, pkgs)
if err != nil {
log.Error("Process sync req failed", "peer", p.Name(), "err", err)
}
}()
}
return nil
}
func (s *Synchronizer) ProcessSyncPreBlocksForStateRsp(blocks types.Blocks) {
s.blocksForPreStateRevCh <- blocks
}
func (s *Synchronizer) ProcessSyncPostBlocksForStateReq(p *network.Peer, hashReq protocol.IntervalHashReq) error {
var toBlock *types.Block
var fromBlock *types.Block
log.Info("process sync post blocks for state request", "peer", p.Name(), "IntervalHashReq", hashReq)
if hashReq.HashEFrom == (protocol.HashElem{}) {
log.Error("SyncPostBlocksForStateReqMsg", "iHash", hashReq)
return errors.New("wrong args")
}
if hashReq.HashETo == (protocol.HashElem{}) {
toBlock = s.chain.CurrentBlock()
} else {
toBlock = s.chain.GetBlock(hashReq.HashETo.Hash)
}
fromBlock = s.chain.GetBlock(hashReq.HashEFrom.Hash)
if toBlock == nil || fromBlock == nil {
return errors.New("can't find fromBlock and toBlock")
}
lastRound := toBlock.Header.Round
roundFrom := fromBlock.Header.Round
go func() {
for {
var pkgHashSet = mapset.NewSet()
var pkgs []*types.TxPackage
roundTo := roundFrom + 30
blocks := s.chain.GetBlocksFromRoundRange(roundFrom, roundTo)
for _, block := range blocks {
for _, pkgHash := range block.Body.TxPackageHashes {
if !pkgHashSet.Contains(pkgHash) {
pkg := s.chain.GetTxPackage(pkgHash)
if pkg != nil {
pkgs = append(pkgs, pkg)
}
pkgHashSet.Add(pkgHash)
}
}
}
if len(blocks) > 0 {
log.Info("Process sync req", "peer", p.Name(), "RoundFrom", roundFrom, "RoundTo", roundTo, "Blocks", len(blocks), "pkgs", len(pkgs), "finish", false)
err := p.SendSyncPostBlocksForState(blocks, pkgs, roundTo, false)
if err != nil {
log.Info("Process sync req failed", "peer", p.Name(), "err", err)
p.SendSyncPostBlocksForState(types.Blocks{}, []*types.TxPackage{}, roundTo, true)
}
} else if roundFrom >= lastRound {
log.Info("Process sync req", "peer", p.Name(), "RoundTo", roundTo, "Blocks", len(blocks), "pkgs", len(pkgs), "finish", true)
err := p.SendSyncPostBlocksForState(blocks, pkgs, roundTo, true)
if err != nil {
log.Info("Process sync req finish failed", "peer", p.Name(), "err", err)
} else {
log.Info("Process sync req finish", "peer", p.Name())
}
break
}
roundFrom = roundFrom + 30
}
}()
return nil
}
func (s *Synchronizer) ProcessSyncPostBlocksForStateRsp(blocks types.Blocks) {
s.blocksForPostStateRevCh <- blocks
}
func (s *Synchronizer) HandleHashesRequest(p *network.Peer, hashesReq protocol.SyncHashListReq) {
if s.GetSyncStatus() == SyncStatusNormal && !s.cp2fp.isRunning() {
switch hashesReq.Type {
case protocol.SyncHashTypeShort:
s.handleShortHashesReq(p, hashesReq)
case protocol.SyncHashTypeLong:
s.handleSkeletonHashesReq(p, hashesReq)
}
} else {
p.SendSyncHashList(hashesReq.Stage, hashesReq.Type, protocol.HashElems{})
}
}
func (s *Synchronizer) getLocalShortHashes() (protocol.HashElems, error) {
block := s.chain.CurrentBlock()
if block.Header.Height < uint64(s.config.ShortHashListLength-1) {
return nil, errors.New("sorry ,don't have enough Blocks yet")
}
var hashList protocol.HashElems
for i := 0; i < s.config.ShortHashListLength; i++ {
hashList = append(hashList, &protocol.HashElem{Height: block.Header.Height, Hash: block.FullHash(), Round: block.Header.Round})
block = s.chain.GetBlock(block.Header.ParentFullHash)
}
return hashList, nil
}
func (s *Synchronizer) handleShortHashesReq(p *network.Peer, hashesReq protocol.SyncHashListReq) error {
block := s.chain.CurrentBlock()
log.Info("handle short hashes req", "currentBlockHeight", block.Header.Height, "req", hashesReq, "peer", p.GetID())
hashList, err := s.getLocalShortHashes()
if err != nil {
return errors.New("get short hash list failed")
}
return p.SendSyncHashList(hashesReq.Stage, hashesReq.Type, hashList)
}
func (s *Synchronizer) longHashes(hashesReq protocol.SyncHashListReq) (protocol.HashElems, error) {
var rep protocol.HashElems
var blockTo, checkpoint *types.Block
if hashesReq.HashTo == (common.Hash{}) {
blockTo = s.chain.CurrentBlock()
hashesReq.HashTo = blockTo.FullHash()
} else {
blockTo = s.chain.GetBlock(hashesReq.HashTo)
}
if hashesReq.HashFrom == (common.Hash{}) {
checkpoint = s.chain.Genesis()
hashesReq.HashFrom = s.chain.Genesis().FullHash()
} else {
checkpoint = s.chain.GetBlock(hashesReq.HashFrom)
}
// if nil or toHeight<fromHeight ,error happens
if checkpoint == nil || blockTo == nil || blockTo.Header.Height <= checkpoint.Header.Height {
log.Error("failed to get block", "checkpoint", checkpoint, "blockTo", blockTo, "req", hashesReq)
return nil, errFailedGetBlock
}
// get long HashList with Interval
for blockTo.FullHash() != checkpoint.FullHash() {
rep = append(rep, &protocol.HashElem{Height: blockTo.Header.Height, Hash: blockTo.FullHash(), Round: blockTo.Header.Round})
lastBlock := blockTo
blockTo = s.chain.GetBlock(blockTo.Header.ParentFullHash)
if blockTo == nil {
log.Error("can't find parent between checkpoint 2 current", "hash", lastBlock.FullHash(), "height", lastBlock.Header.Height, "round", lastBlock.Header.Round)
return nil, errFailedGetBlock
}
}
// append checkpoint block
rep = append(rep, &protocol.HashElem{Height: blockTo.Header.Height, Hash: blockTo.FullHash(), Round: blockTo.Header.Round})
return rep, nil
}
func (s *Synchronizer) handleSkeletonHashesReq(p *network.Peer, hashesReq protocol.SyncHashListReq) error {
s.log.Info("handle skeleton hashes req", "req", hashesReq, "peer", p.GetID(), "genesis.hash", s.chain.Genesis().FullHash())
rep, err := s.longHashes(hashesReq)
if err != nil {
return err
}
return p.SendSyncHashList(hashesReq.Stage, hashesReq.Type, rep)
}
func (s *Synchronizer) HandleHashesResponse(p *network.Peer, hashesRes protocol.SyncHashListRsp) {
switch hashesRes.Stage {
case protocol.SyncStageCP2FP:
s.syncHashListChForCP2FP <- PeerHashElemList{HashType: hashesRes.Type, Peer: p, HashList: hashesRes.Hashes}
case protocol.SyncStageFastSync:
s.syncHashListChForFastSync <- PeerHashElemList{HashType: hashesRes.Type, Peer: p, HashList: hashesRes.Hashes}
case protocol.SyncStagePeerSync:
s.syncHashListChForPeerSync <- PeerHashElemList{HashType: hashesRes.Type, Peer: p, HashList: hashesRes.Hashes}
default:
}
}