Skip to content

Commit

Permalink
fix: eth_subscribe with geth open ended range (#2027)
Browse files Browse the repository at this point in the history
* fix: eth_subscribe with open ended range

* fix

* test

* set FromBlock to latest

* fix unit test

* fix

---------

Co-authored-by: cordt-sei <[email protected]>
  • Loading branch information
blindchaser and cordt-sei authored Jan 23, 2025
1 parent 59d8e48 commit 18b7597
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
15 changes: 14 additions & 1 deletion evmrpc/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ func (a *SubscriptionAPI) Logs(ctx context.Context, filter *filters.FilterCriter
if filter == nil {
filter = &filters.FilterCriteria{}
}
// when fromBlock is 0 and toBlock is latest, adjust the filter
// to unbounded filter
if filter.FromBlock != nil && filter.FromBlock.Int64() == 0 &&
filter.ToBlock != nil && filter.ToBlock.Int64() < 0 {
latest := big.NewInt(a.logFetcher.ctxProvider(LatestCtxHeight).BlockHeight())
unboundedFilter := &filters.FilterCriteria{
FromBlock: latest, // set to latest block height
ToBlock: nil, // set to nil to continue listening
Addresses: filter.Addresses,
Topics: filter.Topics,
}
filter = unboundedFilter
}

rpcSub := notifier.CreateSubscription()

if filter.BlockHash != nil {
Expand Down Expand Up @@ -183,7 +197,6 @@ func (a *SubscriptionAPI) Logs(ctx context.Context, filter *filters.FilterCriter
}
begin = lastToHeight
filter.FromBlock = big.NewInt(lastToHeight + 1)

time.Sleep(SleepInterval)
}
}()
Expand Down
11 changes: 5 additions & 6 deletions evmrpc/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,16 @@ func TestSubscribeEmptyLogs(t *testing.T) {
}

func TestSubscribeNewLogs(t *testing.T) {
t.Parallel()
data := map[string]interface{}{
"fromBlock": "0x0",
"toBlock": "latest",
"address": []common.Address{
common.HexToAddress("0x1111111111111111111111111111111111111112"),
common.HexToAddress("0xc0ffee254729296a45a3885639AC7E10F9d54979"),
common.HexToAddress("0x1111111111111111111111111111111111111111"),
},
"topics": [][]common.Hash{
{
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000123"),
common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111"),
common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111112"),
},
},
}
Expand Down Expand Up @@ -148,11 +147,11 @@ func TestSubscribeNewLogs(t *testing.T) {
t.Fatal("Subscription ID does not match")
}
resultMap := paramMap["result"].(map[string]interface{})
if resultMap["address"] != "0x1111111111111111111111111111111111111112" && resultMap["address"] != "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" {
if resultMap["address"] != "0x1111111111111111111111111111111111111111" && resultMap["address"] != "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" {
t.Fatalf("Unexpected address, got %v", resultMap["address"])
}
firstTopic := resultMap["topics"].([]interface{})[0].(string)
if firstTopic != "0x0000000000000000000000000000000000000000000000000000000000000123" {
if firstTopic != "0x1111111111111111111111111111111111111111111111111111111111111111" {
t.Fatalf("Unexpected topic, got %v", firstTopic)
}
case <-timer.C:
Expand Down

0 comments on commit 18b7597

Please sign in to comment.