Skip to content

Commit

Permalink
Add debug logging and enhance block polling logic (#19)
Browse files Browse the repository at this point in the history
### TL;DR

Added ability to set block limit for polling. Added additional logging for RPC method support checks and added safeguards for block data serialization.

### What changed?

- Added logging for RPC method support checks in `rpc.go`
- Introduced `POLL_UNTIL_BLOCK` environment variable to limit polling
- Modified block range calculation to prevent skipping the genesis block
- Updated `GetLatestPolledBlockNumber` to return `math.MaxUint64` when no block is found so next block polled will overflow to genesis
- Added a null check for `WithdrawalsHash` in block serialization

### How to test?

1. Set the `POLL_UNTIL_BLOCK` environment variable to test polling limits
2. Verify logging output for RPC method support checks
3. Ensure polling starts from genesis when no last polled block is found
4. Test block serialization with and without `WithdrawalsHash` data
  • Loading branch information
iuwqyir authored Sep 19, 2024
1 parent 6a4bbca commit a09c36f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
5 changes: 4 additions & 1 deletion internal/common/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,21 @@ func (rpc *RPC) checkSupportedMethods() error {
if err != nil {
return fmt.Errorf("eth_getBlockByNumber method not supported: %v", err)
}
fmt.Println("eth_getBlockByNumber method supported")

var getLogsResult interface{}
logsErr := rpc.RPCClient.Call(&getLogsResult, "eth_getLogs", map[string]string{"fromBlock": "0x0", "toBlock": "0x0"})
if logsErr != nil {
return fmt.Errorf("eth_getBlockByNumber method not supported: %v", logsErr)
}
fmt.Println("eth_getLogs method supported")

var traceBlockResult interface{}
if traceBlockErr := rpc.RPCClient.Call(&traceBlockResult, "trace_block", "latest"); traceBlockErr != nil {
log.Printf("Optional method trace_block not supported")
log.Printf("Optional method trace_block not supported: %v", traceBlockErr)
}
rpc.SupportsTraceBlock = traceBlockResult != nil
fmt.Printf("trace_block method supported: %v\n", rpc.SupportsTraceBlock)
return nil
}

Expand Down
21 changes: 15 additions & 6 deletions internal/orchestrator/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"math"
"os"
"strconv"
"time"
Expand All @@ -22,6 +23,7 @@ type Poller struct {
triggerIntervalMs int
storage storage.IStorage
lastPolledBlock uint64
pollUntilBlock uint64
}

type BlockNumberWithError struct {
Expand All @@ -38,11 +40,16 @@ func NewPoller(rpc common.RPC, storage storage.IStorage) *Poller {
if err != nil || triggerInterval == 0 {
triggerInterval = DEFAULT_TRIGGER_INTERVAL
}
pollUntilBlock, err := strconv.ParseUint(os.Getenv("POLL_UNTIL_BLOCK"), 10, 64)
if err != nil {
pollUntilBlock = 0
}
return &Poller{
rpc: rpc,
triggerIntervalMs: triggerInterval,
blocksPerPoll: blocksPerPoll,
storage: storage,
pollUntilBlock: pollUntilBlock,
}
}

Expand Down Expand Up @@ -70,6 +77,11 @@ func (p *Poller) Start() {
} else {
p.lastPolledBlock = endBlock
}

if p.pollUntilBlock != 0 && endBlock >= p.pollUntilBlock {
fmt.Println("Reached poll limit, exiting poller")
break
}
}
}()

Expand All @@ -86,14 +98,11 @@ func (p *Poller) getBlockRange() ([]uint64, uint64, error) {
lastPolledBlock, err := p.storage.OrchestratorStorage.GetLatestPolledBlockNumber()
if err != nil {
log.Printf("No last polled block found, starting from genesis %s", err)
lastPolledBlock = 0
lastPolledBlock = math.MaxUint64 // adding 1 will overflow to 0, so it starts from genesis
}

startBlock := lastPolledBlock
if startBlock != 0 {
startBlock = startBlock + 1 // do not skip genesis
}
endBlock := startBlock + uint64(p.blocksPerPoll)
startBlock := lastPolledBlock + 1
endBlock := startBlock + uint64(p.blocksPerPoll) - 1
if endBlock > latestBlock {
endBlock = latestBlock
}
Expand Down
3 changes: 2 additions & 1 deletion internal/storage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage
import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"

Expand Down Expand Up @@ -41,7 +42,7 @@ func NewMemoryConnector(cfg *MemoryConnectorConfig) (*MemoryConnector, error) {
func (m *MemoryConnector) GetLatestPolledBlockNumber() (uint64, error) {
blockNumber, ok := m.cache.Get("latest_polled_block_number")
if !ok {
return 0, nil
return math.MaxUint64, nil // this will overflow to genesis
}
return strconv.ParseUint(blockNumber, 10, 64)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/worker/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func serializeBlock(rpc common.RPC, block *types.Block) common.Block {
GasUsed: big.NewInt(int64(block.GasUsed())),
TransactionCount: uint64(len(block.Transactions())),
BaseFeePerGas: block.BaseFee(),
WithdrawalsRoot: block.Header().WithdrawalsHash.Big().String(),
WithdrawalsRoot: func() string {
if block.Header().WithdrawalsHash == nil {
return ""
}
return block.Header().WithdrawalsHash.Big().String()
}(),
}
}

Expand Down

0 comments on commit a09c36f

Please sign in to comment.