Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in block polling logic and serialization #84

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions internal/orchestrator/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p *Poller) Start() {
return
}
}
log.Debug().Msgf("Polling blocks %s to %s", blockNumbers[0], endBlock)
log.Debug().Msgf("Polling %d blocks starting from %s to %s", len(blockNumbers), blockNumbers[0], endBlock)

worker := worker.NewWorker(p.rpc)
results := worker.Run(blockNumbers)
Expand Down Expand Up @@ -131,11 +131,15 @@ func (p *Poller) getBlockRange() ([]*big.Int, error) {
startBlock := new(big.Int).Add(p.lastPolledBlock, big.NewInt(1))
endBlock := new(big.Int).Add(startBlock, big.NewInt(p.blocksPerPoll-1))

if startBlock.Cmp(latestBlock) > 0 {
log.Debug().Msgf("Start block %s is greater than latest block %s, skipping", startBlock, latestBlock)
return nil, nil
}
if endBlock.Cmp(latestBlock) > 0 {
endBlock = latestBlock
}

blockCount := endBlock.Sub(endBlock, startBlock).Int64() + 1
blockCount := new(big.Int).Sub(endBlock, startBlock).Int64() + 1
blockNumbers := make([]*big.Int, blockCount)
for i := int64(0); i < blockCount; i++ {
blockNumbers[i] = new(big.Int).Add(startBlock, big.NewInt(i))
Expand Down
15 changes: 11 additions & 4 deletions internal/worker/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package worker

import (
"encoding/json"
"fmt"
"math/big"
"strconv"

Expand All @@ -10,7 +11,7 @@ import (
)

func SerializeWorkerResults(chainId *big.Int, blocks []BatchFetchResult[RawBlock], logs []BatchFetchResult[RawLogs], traces []BatchFetchResult[RawTraces]) []WorkerResult {
results := make([]WorkerResult, len(blocks))
results := make([]WorkerResult, 0, len(blocks))

rawLogsMap := make(map[string]BatchFetchResult[RawLogs])
for _, rawLogs := range logs {
Expand All @@ -22,14 +23,20 @@ func SerializeWorkerResults(chainId *big.Int, blocks []BatchFetchResult[RawBlock
rawTracesMap[rawTraces.BlockNumber.String()] = rawTraces
}

for i, rawBlock := range blocks {
for _, rawBlock := range blocks {
result := WorkerResult{
BlockNumber: rawBlock.BlockNumber,
}
if rawBlock.Result == nil {
log.Warn().Msgf("Received a nil block result for block %s.", rawBlock.BlockNumber.String())
result.Error = fmt.Errorf("received a nil block result from RPC")
results = append(results, result)
continue
}

if rawBlock.Error != nil {
result.Error = rawBlock.Error
results[i] = result
results = append(results, result)
continue
}

Expand All @@ -55,7 +62,7 @@ func SerializeWorkerResults(chainId *big.Int, blocks []BatchFetchResult[RawBlock
}
}

results[i] = result
results = append(results, result)
}

return results
Expand Down
Loading