Skip to content

Commit 15948f0

Browse files
authored
Merge pull request #103 from thirdweb-dev/10-16-add_lower_range_for_finding_staged_block_number
Improve poller block range handling and staging data retrieval
2 parents 241a8a7 + ce2266d commit 15948f0

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

internal/orchestrator/poller.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ func NewPoller(rpc rpc.IRPCClient, storage storage.IStorage) *Poller {
4343
}
4444
untilBlock := big.NewInt(int64(config.Cfg.Poller.UntilBlock))
4545
pollFromBlock := big.NewInt(int64(config.Cfg.Poller.FromBlock))
46-
lastPolledBlock, err := storage.StagingStorage.GetLastStagedBlockNumber(rpc.GetChainID(), untilBlock)
47-
if err != nil || lastPolledBlock == nil || lastPolledBlock.Sign() <= 0 {
48-
lastPolledBlock = new(big.Int).Sub(pollFromBlock, big.NewInt(1)) // needs to include the first block
49-
log.Warn().Err(err).Msgf("No last polled block found, setting to %s", lastPolledBlock.String())
46+
lastPolledBlock := new(big.Int).Sub(pollFromBlock, big.NewInt(1)) // needs to include the first block
47+
if config.Cfg.Poller.ForceFromBlock {
48+
log.Debug().Msgf("ForceFromBlock is enabled, setting last polled block to %s", lastPolledBlock.String())
5049
} else {
51-
// In the case where the start block in staging introduces a gap with main storage,
52-
// This hack allows us to re-poll from the start block without having to delete the staging data
53-
if config.Cfg.Poller.ForceFromBlock {
54-
lastPolledBlock = new(big.Int).Sub(pollFromBlock, big.NewInt(1)) // needs to include the first block
50+
highestBlockFromStaging, err := storage.StagingStorage.GetLastStagedBlockNumber(rpc.GetChainID(), pollFromBlock, untilBlock)
51+
if err != nil || highestBlockFromStaging == nil || highestBlockFromStaging.Sign() <= 0 {
52+
log.Warn().Err(err).Msgf("No last polled block found, setting to %s", lastPolledBlock.String())
53+
} else {
54+
lastPolledBlock = highestBlockFromStaging
55+
log.Debug().Msgf("Last polled block found in staging: %s", lastPolledBlock.String())
5556
}
56-
log.Info().Msgf("Last polled block found: %s", lastPolledBlock.String())
5757
}
5858
return &Poller{
5959
rpc: rpc,

internal/storage/clickhouse.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,14 @@ func (c *ClickHouseConnector) GetMaxBlockNumber(chainId *big.Int) (maxBlockNumbe
516516
return maxBlockNumber, nil
517517
}
518518

519-
func (c *ClickHouseConnector) GetLastStagedBlockNumber(chainId *big.Int, rangeEnd *big.Int) (maxBlockNumber *big.Int, err error) {
519+
func (c *ClickHouseConnector) GetLastStagedBlockNumber(chainId *big.Int, rangeStart *big.Int, rangeEnd *big.Int) (maxBlockNumber *big.Int, err error) {
520520
query := fmt.Sprintf("SELECT block_number FROM %s.block_data WHERE is_deleted = 0", c.cfg.Database)
521521
if chainId.Sign() > 0 {
522522
query += fmt.Sprintf(" AND chain_id = %s", chainId.String())
523523
}
524+
if rangeStart.Sign() > 0 {
525+
query += fmt.Sprintf(" AND block_number >= %s", rangeStart.String())
526+
}
524527
if rangeEnd.Sign() > 0 {
525528
query += fmt.Sprintf(" AND block_number <= %s", rangeEnd.String())
526529
}

internal/storage/connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type IStagingStorage interface {
4646
InsertStagingData(data []common.BlockData) error
4747
GetStagingData(qf QueryFilter) (data *[]common.BlockData, err error)
4848
DeleteStagingData(data *[]common.BlockData) error
49-
GetLastStagedBlockNumber(chainId *big.Int, rangeEnd *big.Int) (maxBlockNumber *big.Int, err error)
49+
GetLastStagedBlockNumber(chainId *big.Int, rangeStart *big.Int, rangeEnd *big.Int) (maxBlockNumber *big.Int, err error)
5050
}
5151

5252
type IMainStorage interface {

internal/storage/memory.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ func (m *MemoryConnector) GetMaxBlockNumber(chainId *big.Int) (*big.Int, error)
194194
return maxBlockNumber, nil
195195
}
196196

197-
func IsInRange(num *big.Int, rangeEnd *big.Int) bool {
197+
func IsInRange(num *big.Int, rangeStart *big.Int, rangeEnd *big.Int) bool {
198198
if rangeEnd.Sign() == 0 {
199199
return true
200200
}
201-
return num.Cmp(rangeEnd) <= 0
201+
return num.Cmp(rangeStart) >= 0 && num.Cmp(rangeEnd) <= 0
202202
}
203203

204-
func (m *MemoryConnector) GetLastStagedBlockNumber(chainId *big.Int, rangeEnd *big.Int) (*big.Int, error) {
204+
func (m *MemoryConnector) GetLastStagedBlockNumber(chainId *big.Int, rangeStart *big.Int, rangeEnd *big.Int) (*big.Int, error) {
205205
maxBlockNumber := new(big.Int)
206206
for _, key := range m.cache.Keys() {
207207
if strings.HasPrefix(key, fmt.Sprintf("blockData:%s:", chainId.String())) {
@@ -210,7 +210,7 @@ func (m *MemoryConnector) GetLastStagedBlockNumber(chainId *big.Int, rangeEnd *b
210210
if !ok {
211211
return nil, fmt.Errorf("failed to parse block number: %s", blockNumberStr)
212212
}
213-
if blockNumber.Cmp(maxBlockNumber) > 0 && IsInRange(blockNumber, rangeEnd) {
213+
if blockNumber.Cmp(maxBlockNumber) > 0 && IsInRange(blockNumber, rangeStart, rangeEnd) {
214214
maxBlockNumber = blockNumber
215215
}
216216
}

test/mocks/MockIStagingStorage.go

Lines changed: 15 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)