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: eth_subscribe with geth open ended range #2027

Merged
merged 8 commits into from
Jan 23, 2025
Merged
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
15 changes: 14 additions & 1 deletion evmrpc/subscribe.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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)
}
}()
11 changes: 5 additions & 6 deletions evmrpc/subscribe_test.go
Original file line number Diff line number Diff line change
@@ -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"),
},
},
}
@@ -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: