-
Notifications
You must be signed in to change notification settings - Fork 198
/
chain_tracker.go
350 lines (318 loc) · 15.8 KB
/
chain_tracker.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
package chaintracker
import (
"context"
"errors"
fmt "fmt"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/lavanet/lava/utils"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
grpc "google.golang.org/grpc"
)
type ChainFetcher interface {
FetchLatestBlockNum(ctx context.Context) (int64, error)
FetchBlockHashByNum(ctx context.Context, blockNum int64) (string, error)
}
type ChainTracker struct {
chainFetcher ChainFetcher // used to communicate with the node
blocksToSave uint64 // how many finalized blocks to keep
latestBlockNum int64
blockQueueMu sync.RWMutex
blocksQueue []BlockStore // holds all past hashes up until latest block
forkCallback func(int64) // a function to be called when a fork is detected
newLatestCallback func(int64) // a function to be called when a new block is detected
serverBlockMemory uint64
quit chan bool
}
// this function returns block hashes of the blocks: [from block - to block) non inclusive. an additional specific block hash can be provided. order is sorted ascending
// it supports requests for [spectypes.LATEST_BLOCK-distance1, spectypes.LATEST_BLOCK-distance2)
// spectypes.NOT_APPLICABLE in fromBlock or toBlock results in only returning specific block.
// if specific block is spectypes.NOT_APPLICABLE it is ignored
func (cs *ChainTracker) GetLatestBlockData(fromBlock int64, toBlock int64, specificBlock int64) (latestBlock int64, requestedHashes []*BlockStore, err error) {
cs.blockQueueMu.RLock()
defer cs.blockQueueMu.RUnlock()
latestBlock = cs.GetLatestBlockNum()
if len(cs.blocksQueue) == 0 {
return latestBlock, nil, utils.LavaFormatError("ChainTracker GetLatestBlockData had no blocks", nil, &map[string]string{"latestBlock": strconv.FormatInt(latestBlock, 10)})
}
earliestBlockSaved := cs.getEarliestBlockUnsafe().Block
wantedBlocksData := WantedBlocksData{}
err = wantedBlocksData.New(fromBlock, toBlock, specificBlock, latestBlock, earliestBlockSaved)
if err != nil {
return latestBlock, nil, utils.LavaFormatError("invalid input for GetLatestBlockData", err, &map[string]string{
"fromBlock": strconv.FormatInt(fromBlock, 10), "toBlock": strconv.FormatInt(toBlock, 10), "specificBlock": strconv.FormatInt(specificBlock, 10),
"latestBlock": strconv.FormatInt(latestBlock, 10), "earliestBlockSaved": strconv.FormatInt(earliestBlockSaved, 10),
})
}
for _, blocksQueueIdx := range wantedBlocksData.IterationIndexes() {
blockStore := cs.blocksQueue[blocksQueueIdx]
if !wantedBlocksData.IsWanted(blockStore.Block) {
return latestBlock, nil, utils.LavaFormatError("invalid wantedBlocksData Iteration", err, &map[string]string{
"blocksQueueIdx": strconv.FormatInt(int64(blocksQueueIdx), 10), "blockStore": fmt.Sprintf("%+v", blockStore), "wantedBlocksData": wantedBlocksData.String(),
})
}
requestedHashes = append(requestedHashes, &blockStore)
}
return
}
// blockQueueMu must be locked
func (cs *ChainTracker) getEarliestBlockUnsafe() BlockStore {
return cs.blocksQueue[0]
}
// blockQueueMu must be locked
func (cs *ChainTracker) getLatestBlockUnsafe() BlockStore {
if len(cs.blocksQueue) == 0 {
return BlockStore{Hash: "BAD-HASH"}
}
return cs.blocksQueue[len(cs.blocksQueue)-1]
}
func (cs *ChainTracker) GetLatestBlockNum() int64 {
return atomic.LoadInt64(&cs.latestBlockNum)
}
func (cs *ChainTracker) setLatestBlockNum(value int64) {
atomic.StoreInt64(&cs.latestBlockNum, value)
}
func (cs *ChainTracker) fetchLatestBlockNum(ctx context.Context) (int64, error) {
return cs.chainFetcher.FetchLatestBlockNum(ctx)
}
func (cs *ChainTracker) fetchBlockHashByNum(ctx context.Context, blockNum int64) (string, error) {
if blockNum < cs.GetLatestBlockNum()-int64(cs.serverBlockMemory) {
return "", ErrorFailedToFetchTooEarlyBlock.Wrapf("requested Block: %d, latest block: %d, server memory %d", blockNum, cs.GetLatestBlockNum(), cs.serverBlockMemory)
}
return cs.chainFetcher.FetchBlockHashByNum(ctx, blockNum)
}
// this function fetches all previous blocks from the node starting at the latest provided going backwards blocksToSave blocks
// if it reaches a hash that it already has it stops reading
func (cs *ChainTracker) fetchAllPreviousBlocks(ctx context.Context, latestBlock int64) (err error) {
newBlocksQueue := make([]BlockStore, int64(cs.blocksToSave))
currentLatestBlock := cs.GetLatestBlockNum()
if latestBlock < currentLatestBlock {
return utils.LavaFormatError("invalid latestBlock provided to fetch, it is older than the current state latest block", err, &map[string]string{"latestBlock": strconv.FormatInt(latestBlock, 10), "currentLatestBlock": strconv.FormatInt(currentLatestBlock, 10)})
}
readIndexDiff := latestBlock - currentLatestBlock
blocksQueueStartIndex, blocksQueueEndIndex, newQueueStartIndex := int64(0), int64(0), int64(0)
cs.blockQueueMu.RLock()
// loop through our block queue and compare new hashes to previous ones to find when to stop reading
for idx := int64(0); idx < int64(cs.blocksToSave); idx++ {
blockNumToFetch := latestBlock - idx // reading the blocks from the newest to oldest
newHashForBlock, err := cs.fetchBlockHashByNum(ctx, blockNumToFetch)
if err != nil {
return utils.LavaFormatError("could not get block data in Chain Tracker", err, &map[string]string{"block": strconv.FormatInt(blockNumToFetch, 10)})
}
var foundOverlap bool
foundOverlap, blocksQueueStartIndex, blocksQueueEndIndex, newQueueStartIndex = cs.hashesOverlapIndexes(readIndexDiff, idx, blockNumToFetch, newHashForBlock)
if foundOverlap {
utils.LavaFormatDebug("Chain Tracker read a block Hash, and it existed, stopping fetch", &map[string]string{"block": strconv.FormatInt(blockNumToFetch, 10), "hash": newHashForBlock, "KeptBlocks": strconv.FormatInt(blocksQueueEndIndex-blocksQueueStartIndex, 10)})
break
}
// there is no existing hash for this block
utils.LavaFormatDebug("Chain Tracker read a new block hash", &map[string]string{"block": strconv.FormatInt(blockNumToFetch, 10), "newHash": newHashForBlock})
newBlocksQueue[int64(cs.blocksToSave)-1-idx] = BlockStore{Block: blockNumToFetch, Hash: newHashForBlock}
}
cs.blockQueueMu.RUnlock()
blocksCopied := int64(cs.blocksToSave)
cs.blockQueueMu.Lock()
cs.setLatestBlockNum(latestBlock)
if newQueueStartIndex > 0 {
// means we copy previous blocks
cs.blocksQueue = append(cs.blocksQueue[blocksQueueStartIndex:blocksQueueEndIndex], newBlocksQueue[newQueueStartIndex:]...)
blocksCopied = blocksQueueEndIndex - blocksQueueStartIndex
} else {
// this should only happens if we lost connection for a really long time and readIndexDiff is big, or there was a bigger fork than memory
cs.blocksQueue = newBlocksQueue
}
blocksQueueLen := uint64(len(cs.blocksQueue))
latestHash := cs.getLatestBlockUnsafe().Hash
cs.blockQueueMu.Unlock()
if blocksQueueLen < cs.blocksToSave {
return utils.LavaFormatError("fetchAllPreviousBlocks didn't save enough blocks in Chain Tracker", nil, &map[string]string{"blocksQueueLen": strconv.FormatUint(blocksQueueLen, 10)})
} else {
utils.LavaFormatInfo("Chain Tracker Updated latest block", &map[string]string{"block": strconv.FormatInt(latestBlock, 10), "latestHash": latestHash, "blocksQueueLen": strconv.FormatUint(blocksQueueLen, 10), "blocksQueried": strconv.FormatInt(blocksCopied, 10)})
}
return nil
}
// this function finds if there is an existing block data by hash at the existing data, this allows us to stop querying for further data backwards since when there is a match all former blocks are the same
// it goes over the list backwards looking for a match. when one is found it returns how many blocks are needed from the memory in order to get the required length of queue
func (cs *ChainTracker) hashesOverlapIndexes(readIndexDiff int64, newQueueIdx int64, fetchedBlockNum int64, newHashForBlock string) (foundOverlap bool, blocksQueueStartIndex int64, blocksQueueEndIndex int64, newQueueStartIndex int64) {
savedBlocks := int64(len(cs.blocksQueue))
if readIndexDiff >= savedBlocks {
// we are too far ahead, there is no overlap for sure
return false, 0, 0, 0
}
blocksQueueEnd := savedBlocks - 1 + readIndexDiff // this is not the real end of the queue, its incremented by readIndexDiff so we traverse it together with newBlockQueue
blocksQueueIdx := blocksQueueEnd - newQueueIdx
if blocksQueueIdx > 0 && blocksQueueIdx <= savedBlocks-1 {
existingBlockStore := cs.blocksQueue[blocksQueueIdx]
if existingBlockStore.Block != fetchedBlockNum { // sanity
utils.LavaFormatError("mismatching blocksQueue Index and fetch index, blockStore isn't the right block", nil, &map[string]string{
"block": strconv.FormatInt(fetchedBlockNum, 10), "existingBlockStore": fmt.Sprintf("%+v", existingBlockStore),
"blocksQueueIdx": strconv.FormatInt(blocksQueueEnd, 10), "newQueueIdx": strconv.FormatInt(newQueueIdx, 10), "readIndexDiff": strconv.FormatInt(readIndexDiff, 10),
})
return false, 0, 0, 0
}
if existingBlockStore.Hash == newHashForBlock { // means we already have that hash, since its a blockchain, this means all previous hashes are the same too
overwriteElements := blocksQueueIdx + 1
if overwriteElements < int64(cs.blocksToSave)-1-newQueueIdx || readIndexDiff > overwriteElements { // make sure that in the tail we updated and the existing block we have at least cs.blocksToSave
utils.LavaFormatError("mismatching blocksQueue Index and fetch index, there aren't enough blocks", nil, &map[string]string{
"block": strconv.FormatInt(fetchedBlockNum, 10), "existingBlockStore": fmt.Sprintf("%+v", existingBlockStore),
"overwriteElements": strconv.FormatInt(overwriteElements, 10), "newQueueIdx": strconv.FormatInt(newQueueIdx, 10), "readIndexDiff": strconv.FormatInt(readIndexDiff, 10),
})
return false, 0, 0, 0
} else {
return true, readIndexDiff, overwriteElements, overwriteElements - readIndexDiff
}
}
}
return false, 0, 0, 0
}
// this function reads the hash of the latest block and finds wether there was a fork, if it identifies a newer block arrived it goes backwards to the block in memory and reads again
func (cs *ChainTracker) forkChanged(ctx context.Context, newLatestBlock int64) (forked bool, err error) {
if newLatestBlock == cs.GetLatestBlockNum() {
// no new block arrived, compare the last hash
hash, err := cs.fetchBlockHashByNum(ctx, newLatestBlock)
if err != nil {
return false, err
}
cs.blockQueueMu.RLock()
defer cs.blockQueueMu.RUnlock()
latestBlockSaved := cs.getLatestBlockUnsafe()
return latestBlockSaved.Hash != hash, nil
}
// a new block was received, we need to compare a previous hash
cs.blockQueueMu.RLock()
latestBlockSaved := cs.getLatestBlockUnsafe()
cs.blockQueueMu.RUnlock() // not with defer because we are going to call an external function here
prevHash, err := cs.fetchBlockHashByNum(ctx, latestBlockSaved.Block)
if err != nil {
return false, err
}
return latestBlockSaved.Hash != prevHash, nil
}
func (cs *ChainTracker) gotNewBlock(ctx context.Context, newLatestBlock int64) (gotNewBlock bool) {
return newLatestBlock > cs.GetLatestBlockNum()
}
// this function is periodically called, it checks if there is a new block or a fork and fetches all necessary previous data in order to fill gaps if any
func (cs *ChainTracker) fetchAllPreviousBlocksIfNecessary(ctx context.Context) (err error) {
newLatestBlock, err := cs.fetchLatestBlockNum(ctx)
if err != nil {
return utils.LavaFormatError("could not fetchLatestBlockNum in ChainTracker", err, nil)
}
gotNewBlock := cs.gotNewBlock(ctx, newLatestBlock)
forked, err := cs.forkChanged(ctx, newLatestBlock)
if err != nil {
return utils.LavaFormatError("could not fetchLatestBlock Hash in ChainTracker", err, &map[string]string{"block": strconv.FormatInt(newLatestBlock, 10)})
}
if gotNewBlock || forked {
utils.LavaFormatDebug("ChainTracker should update state", &map[string]string{"gotNewBlock": fmt.Sprintf("%t", gotNewBlock), "forked": fmt.Sprintf("%t", forked), "newLatestBlock": strconv.FormatInt(newLatestBlock, 10), "currentBlock": strconv.FormatInt(cs.GetLatestBlockNum(), 10)})
// TODO: if we didn't fork theres really no need to refetch
cs.fetchAllPreviousBlocks(ctx, newLatestBlock)
if gotNewBlock {
if cs.newLatestCallback != nil {
cs.newLatestCallback(newLatestBlock)
}
}
if forked {
if cs.forkCallback != nil {
cs.forkCallback(newLatestBlock)
}
}
}
return
}
// this function starts the fetching timer periodically checking by polling if updates are necessary
func (cs *ChainTracker) start(ctx context.Context, pollingBlockTime time.Duration) error {
// how often to query latest block.
// TODO: subscribe instead of repeatedly fetching
// TODO: improve the polling time, we don't need to poll the first half of every block change
ticker := time.NewTicker(pollingBlockTime / 10) // divide here so we don't miss new blocks by all that much
newLatestBlock, err := cs.fetchLatestBlockNum(ctx)
if err != nil {
utils.LavaFormatFatal("could not fetchLatestBlockNum in ChainTracker", err, nil)
}
cs.fetchAllPreviousBlocks(ctx, newLatestBlock)
// Polls blocks and keeps a queue of them
go func() {
for {
select {
case <-ticker.C:
err := cs.fetchAllPreviousBlocksIfNecessary(ctx)
if err != nil {
utils.LavaFormatError("failed to fetch all previous blocks and was necessary", err, nil)
}
case <-cs.quit:
ticker.Stop()
return
}
}
}()
return nil
}
// this function serves a grpc server if configuration for it was provided, the goal is to enable stateTracker to serve several processes and minimize node queries
func (ct *ChainTracker) serve(ctx context.Context, listenAddr string) error {
if listenAddr == "" {
return nil
}
ctx, cancel := context.WithCancel(ctx)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
defer func() {
signal.Stop(signalChan)
cancel()
}()
lis, err := net.Listen("tcp", listenAddr)
if err != nil {
utils.LavaFormatFatal("Chain Tracker failure setting up listener", err, &map[string]string{"listenAddr": listenAddr})
}
s := grpc.NewServer()
wrappedServer := grpcweb.WrapServer(s)
handler := func(resp http.ResponseWriter, req *http.Request) {
// Set CORS headers
resp.Header().Set("Access-Control-Allow-Origin", "*")
resp.Header().Set("Access-Control-Allow-Headers", "Content-Type,x-grpc-web")
wrappedServer.ServeHTTP(resp, req)
}
httpServer := http.Server{
Handler: h2c.NewHandler(http.HandlerFunc(handler), &http2.Server{}),
}
go func() {
select {
case <-ctx.Done():
utils.LavaFormatInfo("Chain Tracker Server ctx.Done", nil)
case <-signalChan:
utils.LavaFormatInfo("Chain Tracker Server signalChan", nil)
}
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
utils.LavaFormatFatal("chainTracker failed to shutdown", err, &map[string]string{})
}
}()
server := &ChainTrackerService{ChainTracker: ct}
RegisterChainTrackerServiceServer(s, server)
utils.LavaFormatInfo("Chain Tracker Listening", &map[string]string{"Address": lis.Addr().String()})
if err := httpServer.Serve(lis); !errors.Is(err, http.ErrServerClosed) {
utils.LavaFormatFatal("Chain Tracker failed to serve", err, &map[string]string{"Address": lis.Addr().String()})
}
return nil
}
func New(ctx context.Context, chainFetcher ChainFetcher, config ChainTrackerConfig) (chainTracker *ChainTracker, err error) {
err = config.validate()
if err != nil {
return nil, err
}
chainTracker = &ChainTracker{forkCallback: config.ForkCallback, newLatestCallback: config.NewLatestCallback, blocksToSave: config.BlocksToSave, chainFetcher: chainFetcher, latestBlockNum: 0, serverBlockMemory: config.ServerBlockMemory}
err = chainTracker.start(ctx, config.AverageBlockTime)
if err != nil {
return nil, err
}
err = chainTracker.serve(ctx, config.ServerAddress)
return
}