/
sync.go
229 lines (206 loc) · 4.9 KB
/
sync.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
// Copyright (c) 2018 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package comm
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/rlp"
"github.com/miniBamboo/workshare/block"
"github.com/miniBamboo/workshare/chain"
"github.com/miniBamboo/workshare/co"
"github.com/miniBamboo/workshare/comm/proto"
"github.com/pkg/errors"
)
func download(_ctx context.Context, repo *chain.Repository, peer *Peer, headNum uint32, handler HandleBlockStream) error {
ancestor, err := findCommonAncestor(_ctx, repo, peer, headNum)
if err != nil {
return errors.WithMessage(err, "find common ancestor")
}
var (
ctx, cancel = context.WithCancel(_ctx)
fetched = make(chan []*block.Block, 1)
warmedUp = make(chan *block.Block, 2048)
goes co.Goes
fetchErr error
)
defer goes.Wait()
goes.Go(func() {
defer close(fetched)
fetchErr = fetchBlocks(ctx, peer, ancestor+1, fetched)
})
goes.Go(func() {
defer close(warmedUp)
warmupBlocks(ctx, fetched, warmedUp)
})
defer cancel()
if err := handler(ctx, warmedUp); err != nil {
return err
}
return fetchErr
}
func fetchBlocks(ctx context.Context, peer *Peer, fromBlockNum uint32, fetched chan<- []*block.Block) error {
for {
result, err := proto.GetBlocksFromNumber(ctx, peer, fromBlockNum)
if err != nil {
return err
}
if len(result) == 0 {
return nil
}
blocks := make([]*block.Block, 0, len(result))
for _, raw := range result {
var blk block.Block
if err := rlp.DecodeBytes(raw, &blk); err != nil {
return errors.Wrap(err, "invalid block")
}
if blk.Header().Number() != fromBlockNum {
return errors.New("broken sequence")
}
fromBlockNum++
blocks = append(blocks, &blk)
}
select {
case fetched <- blocks:
case <-ctx.Done():
return ctx.Err()
}
}
}
func warmupBlocks(ctx context.Context, fetched <-chan []*block.Block, warmedUp chan<- *block.Block) {
<-co.Parallel(func(queue chan<- func()) {
for blocks := range fetched {
for _, blk := range blocks {
h := blk.Header()
queue <- func() {
h.ID()
h.Beta()
}
for _, tx := range blk.Transactions() {
tx := tx
queue <- func() {
tx.ID()
tx.UnprovedWork()
_, _ = tx.IntrinsicGas()
_, _ = tx.Delegator()
}
}
}
for _, blk := range blocks {
select {
case <-ctx.Done():
return
case warmedUp <- blk:
}
// when queued blocks count > 10% warmed up channel cap,
// send nil block to throttle to reduce mem pressure.
if len(warmedUp)*10 > cap(warmedUp) {
const targetSize = 2048
for i := 0; i < int(blk.Size())/targetSize-1; i++ {
select {
case warmedUp <- nil:
default:
}
}
}
}
}
})
}
func findCommonAncestor(ctx context.Context, repo *chain.Repository, peer *Peer, headNum uint32) (uint32, error) {
if headNum == 0 {
return headNum, nil
}
isOverlapped := func(num uint32) (bool, error) {
result, err := proto.GetBlockIDByNumber(ctx, peer, num)
if err != nil {
return false, err
}
id, err := repo.NewBestChain().GetBlockID(num)
if err != nil {
return false, err
}
return id == result, nil
}
var find func(start uint32, end uint32, ancestor uint32) (uint32, error)
find = func(start uint32, end uint32, ancestor uint32) (uint32, error) {
if start == end {
overlapped, err := isOverlapped(start)
if err != nil {
return 0, err
}
if overlapped {
return start, nil
}
} else {
mid := (start + end) / 2
overlapped, err := isOverlapped(mid)
if err != nil {
return 0, err
}
if overlapped {
return find(mid+1, end, mid)
}
if mid > start {
return find(start, mid-1, ancestor)
}
}
return ancestor, nil
}
fastSeek := func() (uint32, error) {
var backward uint32
for {
if backward >= headNum {
return 0, nil
}
overlapped, err := isOverlapped(headNum - backward)
if err != nil {
return 0, err
}
if overlapped {
return headNum - backward, nil
}
if backward == 0 {
backward = 1
} else {
backward <<= 1
}
}
}
seekNum, err := fastSeek()
if err != nil {
return 0, err
}
if seekNum == headNum {
return headNum, nil
}
return find(seekNum, headNum, 0)
}
func (c *Communicator) syncTxs(peer *Peer) {
for i := 0; ; i++ {
peer.logger.Debug(fmt.Sprintf("sync txs loop %v", i))
result, err := proto.GetTxs(c.ctx, peer)
if err != nil {
peer.logger.Debug("failed to request txs", "err", err)
return
}
// no more txs
if len(result) == 0 {
break
}
for _, tx := range result {
peer.MarkTransaction(tx.Hash())
_ = c.txPool.StrictlyAdd(tx)
select {
case <-c.ctx.Done():
return
default:
}
}
if i >= 100 {
peer.logger.Debug("too many loops to sync txs, break")
return
}
}
peer.logger.Debug("sync txs done")
}