-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.go
237 lines (202 loc) · 6.16 KB
/
main.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
package main
import (
"context"
"time"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"github.com/coinbase/chainstorage/protos/coinbase/c3/common"
api "github.com/coinbase/chainstorage/protos/coinbase/chainstorage"
"github.com/coinbase/chainstorage/sdk"
)
const (
blockchain = common.Blockchain_BLOCKCHAIN_POLYGON
network = common.Network_NETWORK_POLYGON_MAINNET
batchSize = 20
startDistance = 100
)
type (
Worker struct {
manager sdk.SystemManager
logger *zap.Logger
session sdk.Session
irreversibleDistance uint64 // N
backoffInterval time.Duration
checkpoint int64
numTransactions int
numBatches int
blocks []*api.NativeBlock // Last N blocks in the canonical chain.
}
)
func main() {
manager := sdk.NewManager()
defer manager.Shutdown()
worker, err := NewWorker(manager)
if err != nil {
panic(err)
}
if err := worker.Run(); err != nil {
panic(err)
}
}
func NewWorker(manager sdk.SystemManager) (*Worker, error) {
logger := manager.Logger()
session, err := sdk.New(manager, &sdk.Config{
Blockchain: blockchain,
Network: network,
Env: sdk.EnvProduction,
})
if err != nil {
return nil, xerrors.Errorf("failed to create session: %w", err)
}
ctx := manager.ServiceContext()
client := session.Client()
chainMetadata, err := client.GetChainMetadata(ctx, &api.GetChainMetadataRequest{})
if err != nil {
return nil, xerrors.Errorf("failed to get chain metadata: %w", err)
}
irreversibleDistance := chainMetadata.IrreversibleDistance
backoffInterval, err := time.ParseDuration(chainMetadata.BlockTime)
if err != nil {
return nil, xerrors.Errorf("failed to parse block time: %w", err)
}
events, err := client.GetChainEvents(ctx, &api.GetChainEventsRequest{
InitialPositionInStream: api.InitialPosition_LATEST.String(),
MaxNumEvents: 1,
})
if err != nil {
return nil, xerrors.Errorf("failed to get chain events: %w", err)
}
if len(events) != 1 {
return nil, xerrors.Errorf("got unexpected number of events: %v", len(events))
}
checkpoint := events[0].SequenceNum - startDistance
blocks := make([]*api.NativeBlock, 0, irreversibleDistance)
return &Worker{
manager: manager,
logger: logger,
session: session,
irreversibleDistance: irreversibleDistance,
backoffInterval: backoffInterval,
checkpoint: checkpoint,
numTransactions: 0,
numBatches: 0,
blocks: blocks,
}, nil
}
func (w *Worker) Run() error {
ctx := w.manager.ServiceContext()
w.logger.Info(
"running worker",
zap.Int64("checkpoint", w.checkpoint),
)
for {
sequence := w.checkpoint
nextSequence, err := w.processBatch(ctx, sequence, batchSize)
if err != nil {
return xerrors.Errorf("failed to process batch [%v, %v): %w", sequence, sequence+batchSize, err)
}
// Checkpoint is typically persisted in the database.
// In this example, we simply store the checkpoint in memory.
w.checkpoint = nextSequence
}
}
func (w *Worker) processBatch(ctx context.Context, sequence int64, batchSize uint64) (int64, error) {
events, err := w.session.Client().GetChainEvents(ctx, &api.GetChainEventsRequest{
SequenceNum: sequence,
MaxNumEvents: batchSize,
})
if err != nil {
return 0, xerrors.Errorf("failed to get chain events: %w", err)
}
if len(events) == 0 {
// Note that the above API is non-blocking, and it returns an empty slice when no event is available.
w.logger.Info(
"waiting for new events",
zap.Int64("sequence", sequence),
zap.String("backoffInterval", w.backoffInterval.String()),
)
time.Sleep(w.backoffInterval)
return sequence, nil
}
w.logger.Info(
"processing batch",
zap.Int64("sequence", sequence),
zap.Uint64("batchSize", batchSize),
)
blocks := make([]*api.NativeBlock, len(events))
group, ctx := errgroup.WithContext(ctx)
for i := range events {
i := i
group.Go(func() error {
event := events[i]
blockID := event.Block
if blockID.Skipped {
return nil
}
block, err := w.session.Client().GetBlockWithTag(ctx, blockID.Tag, blockID.Height, blockID.Hash)
if err != nil {
return xerrors.Errorf("failed to get block {%+v}: %w", blockID, err)
}
nativeBlock, err := w.session.Parser().ParseNativeBlock(ctx, block)
if err != nil {
return xerrors.Errorf("failed to parse block {%+v}: %w", blockID, err)
}
blocks[i] = nativeBlock
return nil
})
}
if err := group.Wait(); err != nil {
return 0, xerrors.Errorf("failed to get blocks: %w", err)
}
w.numBatches += 1
for i, event := range events {
if event.Block.Skipped {
continue
}
block := blocks[i]
if event.Type == api.BlockchainEvent_BLOCK_ADDED {
w.blocks = append(w.blocks, block)
w.numTransactions += len(block.GetEthereum().Transactions)
} else if event.Type == api.BlockchainEvent_BLOCK_REMOVED {
if len(w.blocks) > 0 {
// ChainStorage guarantees the ordering of the events.
// +1, +2, +3, -3, -2, +2', +3'
lastBlock := w.blocks[len(w.blocks)-1]
if block.Height != lastBlock.Height {
return 0, xerrors.Errorf("invalid chain: block=%v, lastBlock=%v", block.Height, lastBlock.Height)
}
// Remove the orphaned block.
w.blocks = w.blocks[:len(w.blocks)-1]
}
w.logger.Info(
"removing orphaned block",
zap.Reflect("height", block.Height),
zap.Reflect("hash", block.Hash),
)
w.numTransactions -= len(block.GetEthereum().Transactions)
} else {
return 0, xerrors.Errorf("unknown event type: %v", event.Type)
}
}
lastEvent := events[len(events)-1]
nextSequence := lastEvent.SequenceNum
if n := int(w.irreversibleDistance); len(w.blocks) > n {
// Keep last n blocks only.
w.blocks = w.blocks[len(w.blocks)-n:]
}
var lastBlock *api.NativeBlock
if len(w.blocks) > 0 {
lastBlock = w.blocks[len(w.blocks)-1]
}
w.logger.Info(
"finished batch",
zap.Int64("sequence", sequence),
zap.Int64("nextSequence", nextSequence),
zap.Int("numBatches", w.numBatches),
zap.Int("numTransactions", w.numTransactions),
zap.Int("numBlocks", len(w.blocks)),
zap.Uint64("lastBlock", lastBlock.Height),
)
return nextSequence, nil
}