Skip to content

Commit 353f736

Browse files
committed
fix: block filters can be also recreated
1 parent 64ad39e commit 353f736

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

ethers/providers/jsonrpc/subscriptions.nim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,14 @@ proc new*(_: type JsonRpcSubscriptions,
164164
raise e
165165
except CatchableError as e:
166166
if "filter not found" in e.msg:
167-
let filter = subscriptions.filters[originalId]
168-
let newId = await subscriptions.client.eth_newFilter(filter)
167+
var newId: JsonNode
168+
# If there exists filter for given ID, then the filter was a log filter
169+
# otherwise it was a block filter
170+
if subscriptions.filters.hasKey(originalId):
171+
let filter = subscriptions.filters[originalId]
172+
newId = await subscriptions.client.eth_newFilter(filter)
173+
else:
174+
newId = await subscriptions.client.eth_newBlockFilter()
169175
subscriptions.subscriptionMapping[originalId] = newId
170176
return await getChanges(originalId)
171177
else:

testmodule/providers/jsonrpc/rpc_mock.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ proc start*(server: MockRpcHttpServer) =
2525
server.filters.add filterId
2626
return filterId
2727

28+
server.srv.router.rpc("eth_newBlockFilter") do() -> string:
29+
let filterId = "0x" & (array[16, byte].example).toHex
30+
server.filters.add filterId
31+
return filterId
32+
2833
server.srv.router.rpc("eth_getFilterChanges") do(id: string) -> seq[string]:
2934
if id notin server.filters:
3035
raise (ref ApplicationError)(code: -32000, msg: "filter not found")

testmodule/providers/jsonrpc/testJsonRpcSubscriptions.nim

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ suite "HTTP polling subscriptions - filter not found":
124124
await client.close()
125125
await mockServer.stop()
126126

127-
test "filter not found error recreates filter":
127+
test "filter not found error recreates log filter":
128128
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
129129
let emptyHandler = proc(log: Log) = discard
130130

@@ -140,7 +140,7 @@ suite "HTTP polling subscriptions - filter not found":
140140

141141
check eventually subscriptions.subscriptionMapping[id] != id
142142

143-
test "recreated filter can be still unsubscribed using the original id":
143+
test "recreated log filter can be still unsubscribed using the original id":
144144
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
145145
let emptyHandler = proc(log: Log) = discard
146146
let id = await subscriptions.subscribeLogs(filter, emptyHandler)
@@ -151,3 +151,24 @@ suite "HTTP polling subscriptions - filter not found":
151151

152152
check not subscriptions.filters.hasKey id
153153
check not subscriptions.subscriptionMapping.hasKey id
154+
155+
test "filter not found error recreates block filter":
156+
let emptyHandler = proc(blck: Block) = discard
157+
158+
check subscriptions.subscriptionMapping.len == 0
159+
let id = await subscriptions.subscribeBlocks(emptyHandler)
160+
check subscriptions.subscriptionMapping[id] == id
161+
162+
mockServer.invalidateFilter(id)
163+
164+
check eventually subscriptions.subscriptionMapping[id] != id
165+
166+
test "recreated block filter can be still unsubscribed using the original id":
167+
let emptyHandler = proc(blck: Block) = discard
168+
let id = await subscriptions.subscribeBlocks(emptyHandler)
169+
mockServer.invalidateFilter(id)
170+
check eventually subscriptions.subscriptionMapping[id] != id
171+
172+
await subscriptions.unsubscribe(id)
173+
174+
check not subscriptions.subscriptionMapping.hasKey id

0 commit comments

Comments
 (0)