mirror of
https://github.com/status-im/nim-ethers.git
synced 2025-02-20 02:58:09 +00:00
fix: block filters can be also recreated
This commit is contained in:
parent
765379a662
commit
1c2610a583
@ -164,8 +164,14 @@ proc new*(_: type JsonRpcSubscriptions,
|
||||
raise e
|
||||
except CatchableError as e:
|
||||
if "filter not found" in e.msg:
|
||||
let filter = subscriptions.filters[originalId]
|
||||
let newId = await subscriptions.client.eth_newFilter(filter)
|
||||
var newId: JsonNode
|
||||
# If there exists filter for given ID, then the filter was a log filter
|
||||
# otherwise it was a block filter
|
||||
if subscriptions.filters.hasKey(originalId):
|
||||
let filter = subscriptions.filters[originalId]
|
||||
newId = await subscriptions.client.eth_newFilter(filter)
|
||||
else:
|
||||
newId = await subscriptions.client.eth_newBlockFilter()
|
||||
subscriptions.subscriptionMapping[originalId] = newId
|
||||
return await getChanges(originalId)
|
||||
else:
|
||||
|
@ -25,6 +25,11 @@ proc start*(server: MockRpcHttpServer) =
|
||||
server.filters.add filterId
|
||||
return filterId
|
||||
|
||||
server.srv.router.rpc("eth_newBlockFilter") do() -> string:
|
||||
let filterId = "0x" & (array[16, byte].example).toHex
|
||||
server.filters.add filterId
|
||||
return filterId
|
||||
|
||||
server.srv.router.rpc("eth_getFilterChanges") do(id: string) -> seq[string]:
|
||||
if id notin server.filters:
|
||||
raise (ref ApplicationError)(code: -32000, msg: "filter not found")
|
||||
|
@ -124,7 +124,7 @@ suite "HTTP polling subscriptions - filter not found":
|
||||
await client.close()
|
||||
await mockServer.stop()
|
||||
|
||||
test "filter not found error recreates filter":
|
||||
test "filter not found error recreates log filter":
|
||||
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
|
||||
let emptyHandler = proc(log: Log) = discard
|
||||
|
||||
@ -142,7 +142,7 @@ suite "HTTP polling subscriptions - filter not found":
|
||||
|
||||
check eventually subscriptions.subscriptionMapping[id] != id
|
||||
|
||||
test "recreated filter can be still unsubscribed using the original id":
|
||||
test "recreated log filter can be still unsubscribed using the original id":
|
||||
let filter = EventFilter(address: Address.example, topics: @[array[32, byte].example])
|
||||
let emptyHandler = proc(log: Log) = discard
|
||||
let id = await subscriptions.subscribeLogs(filter, emptyHandler)
|
||||
@ -153,3 +153,24 @@ suite "HTTP polling subscriptions - filter not found":
|
||||
|
||||
check not subscriptions.filters.hasKey id
|
||||
check not subscriptions.subscriptionMapping.hasKey id
|
||||
|
||||
test "filter not found error recreates block filter":
|
||||
let emptyHandler = proc(blck: Block) = discard
|
||||
|
||||
check subscriptions.subscriptionMapping.len == 0
|
||||
let id = await subscriptions.subscribeBlocks(emptyHandler)
|
||||
check subscriptions.subscriptionMapping[id] == id
|
||||
|
||||
mockServer.invalidateFilter(id)
|
||||
|
||||
check eventually subscriptions.subscriptionMapping[id] != id
|
||||
|
||||
test "recreated block filter can be still unsubscribed using the original id":
|
||||
let emptyHandler = proc(blck: Block) = discard
|
||||
let id = await subscriptions.subscribeBlocks(emptyHandler)
|
||||
mockServer.invalidateFilter(id)
|
||||
check eventually subscriptions.subscriptionMapping[id] != id
|
||||
|
||||
await subscriptions.unsubscribe(id)
|
||||
|
||||
check not subscriptions.subscriptionMapping.hasKey id
|
||||
|
Loading…
x
Reference in New Issue
Block a user