Skip to content

Commit 9324aae

Browse files
committed
remove bugs
1 parent 191d6ef commit 9324aae

File tree

5 files changed

+73
-51
lines changed

5 files changed

+73
-51
lines changed

nimbus_verified_proxy/rpc/blocks.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ proc resolveBlockTag*(
3030
of "latest":
3131
let hLatest = vp.headerStore.latest.valueOr:
3232
return err("Couldn't get the latest block number from header store")
33-
ok(BlockTag(kind: bidNumber, number: hLatest.number))
33+
ok(BlockTag(kind: bidNumber, number: Quantity(hLatest.number)))
3434
of "finalized":
3535
let hFinalized = vp.headerStore.finalized.valueOr:
3636
return err("Couldn't get the latest block number from header store")
37-
ok(BlockTag(kind: bidNumber, number: hFinalized.number))
37+
ok(BlockTag(kind: bidNumber, number: Quantity(hFinalized.number)))
3838
of "earliest":
3939
let hEarliest = vp.headerStore.earliest.valueOr:
4040
return err("Couldn't get the latest block number from header store")
41-
ok(BlockTag(kind: bidNumber, number: hEarliest.number))
41+
ok(BlockTag(kind: bidNumber, number: Quantity(hEarliest.number)))
4242
else:
4343
err("No support for block tag " & $blockTag)
4444
else:

nimbus_verified_proxy/rpc/receipts.nim

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,15 @@ proc getReceipts*(
8484

8585
await vp.getReceipts(header, numberTag)
8686

87-
proc resolveFilterTags*(filter: FilterOptions): Result[FilterOptions, string] =
87+
proc resolveFilterTags*(vp: VerifiedRpcProxy, filter: FilterOptions): Result[FilterOptions, string] =
88+
if filter.blockHash.isSome():
89+
return ok(filter)
8890
let
8991
fromBlock = filter.fromBlock.get(types.BlockTag(kind: bidAlias, alias: "latest"))
9092
toBlock = filter.toBlock.get(types.BlockTag(kind: bidAlias, alias: "latest"))
91-
fromBlockNumberTag = resolveBlockTag(fromBlock).valueOr:
93+
fromBlockNumberTag = vp.resolveBlockTag(fromBlock).valueOr:
9294
return err(error)
93-
toBlockNumberTag = resolveBlockTag(toBlock).valueOr:
95+
toBlockNumberTag = vp.resolveBlockTag(toBlock).valueOr:
9496
return err(error)
9597

9698
return ok(
@@ -104,9 +106,8 @@ proc resolveFilterTags*(filter: FilterOptions): Result[FilterOptions, string] =
104106
)
105107

106108
proc verifyLogs*(
107-
vp: VerifiedRpcProxy, filterOptions: FilterOptions, logObjs: seq[LogObject]
108-
): Future[Result[bool, string]] {.async.} =
109-
var res = newSeq[LogObject]()
109+
vp: VerifiedRpcProxy, filter: FilterOptions, logObjs: seq[LogObject]
110+
): Future[Result[void, string]] {.async.} =
110111

111112
# store block hashes contains the logs so that we can batch receipt requests
112113
var
@@ -120,7 +121,7 @@ proc verifyLogs*(
120121
if prevBlockHash != lg.blockHash.get():
121122
# TODO: a cache will solve downloading the same block receipts for multiple logs
122123
rxs = (await vp.getReceipts(lg.blockHash.get())).valueOr:
123-
return err(error)
124+
return err("Couldn't get block receipt to verify logs")
124125
prevBlockHash = lg.blockHash.get()
125126
let
126127
txIdx = distinctBase(lg.transactionIndex.get())
@@ -133,7 +134,21 @@ proc verifyLogs*(
133134
rxLog.topics != lg.topics or
134135
lg.blockNumber.get() < filter.fromBlock.get().number or
135136
lg.blockNumber.get() > filter.toBlock.get().number or
136-
(not match(toLog(lg), filterOptions.address, filterOptions.topics)):
137+
(not match(toLog(lg), filter.address, filter.topics)):
137138
return err("one of the returned logs is invalid")
138139

139-
return ok()
140+
ok()
141+
142+
proc getLogs*(vp: VerifiedRpcProxy, filter: FilterOptions): Future[Result[seq[LogObject], string]] {.async.} =
143+
let
144+
resolvedFilter = vp.resolveFilterTags(filter).valueOr:
145+
return err(error)
146+
logObjs =
147+
try:
148+
await vp.rpcClient.eth_getLogs(resolvedFilter)
149+
except CatchableError as e:
150+
return err(e.msg)
151+
152+
?(await vp.verifyLogs(resolvedFilter, logObjs))
153+
154+
return ok(logObjs)

nimbus_verified_proxy/rpc/rpc_eth_api.nim

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -272,36 +272,25 @@ proc installEthApiHandlers*(vp: VerifiedRpcProxy) =
272272
raise newException(ValueError, "receipt couldn't be verified")
273273

274274
vp.proxy.rpc("eth_getLogs") do(filterOptions: FilterOptions) -> seq[LogObject]:
275-
let
276-
filter = resolveFilterTags(filterOptions).valueOr:
277-
raise newException(ValueError, error)
278-
logObjs =
279-
try:
280-
await vp.rpcClient.eth_getLogs(filter)
281-
except CatchableError as e:
282-
raise newException(ValueError, e.msg)
283-
284-
?(await vp.verifyLogs(filter, logObjs))
285-
286-
return logObjs
275+
(await vp.getLogs(filterOptions)).valueOr:
276+
raise newException(ValueError, error)
287277

288-
vp.proxy.rpc("eth_newFilter") do(filterOptions: FilterOptions) -> int:
278+
vp.proxy.rpc("eth_newFilter") do(filterOptions: FilterOptions) -> string:
289279
let
290-
hexId =
280+
id =
291281
try:
292282
# filter is not resolved when storing only while fetching
293283
await vp.rpcClient.eth_newFilter(filterOptions)
294284
except CatchableError as e:
295285
raise newException(ValueError, e.msg)
296-
id = fromHex[int](hexId)
297286

298287
vp.filterStore[id] = filterOptions
299288
return id
300289

301-
vp.proxy.rpc("eth_uninstallFilter") do(filterId: int) -> bool:
290+
vp.proxy.rpc("eth_uninstallFilter") do(filterId: string) -> bool:
302291
let status =
303292
try:
304-
await vp.rpcClient.eth_uninstallFilter("0x" & toHex(filterId))
293+
await vp.rpcClient.eth_uninstallFilter(filterId)
305294
except CatchableError as e:
306295
raise newException(ValueError, e.msg)
307296

@@ -310,40 +299,33 @@ proc installEthApiHandlers*(vp: VerifiedRpcProxy) =
310299

311300
return status
312301

313-
vp.proxy.rpc("eth_getFilterLogs") do(filterId: int) -> seq[LogObject]:
302+
vp.proxy.rpc("eth_getFilterLogs") do(filterId: string) -> seq[LogObject]:
314303
if filterId notin vp.filterStore:
315304
raise newException(ValueError, "Filter doesn't exist")
316305

317-
let
318-
filter = resolveFilterTags(vp.filterStore[filterId]).valueOr:
319-
raise newException(ValueError, error)
320-
logObjs =
321-
try:
322-
# use locally stored filter and get logs
323-
await vp.rpcClient.eth_getLogs(filter)
324-
except CatchableError as e:
325-
raise newException(ValueError, e.msg)
326-
327-
?(await vp.verifyLogs(filter, logObjs))
328-
329-
return logObjs
306+
(await vp.getLogs(vp.filterStore[filterId])).valueOr:
307+
raise newException(ValueError, error)
330308

331-
vp.proxy.rpc("eth_getFilterChanges") do(filterId: int) -> seq[LogObject]:
309+
vp.proxy.rpc("eth_getFilterChanges") do(filterId: string) -> seq[LogObject]:
332310
if filterId notin vp.filterStore:
333311
raise newException(ValueError, "Filter doesn't exist")
334312

335313
let
336-
filter = resolveFilterTags(vp.filterStore[filterId]).valueOr:
314+
filter = vp.resolveFilterTags(vp.filterStore[filterId]).valueOr:
337315
raise newException(ValueError, error)
338316
logObjs =
339317
try:
340-
await vp.rpcClient.eth_getFilterChanges("0x" & toHex(filterId))
318+
await vp.rpcClient.eth_getFilterChanges(filterId)
341319
except CatchableError as e:
342320
raise newException(ValueError, e.msg)
343321

344-
?(await vp.verifyLogs(filter, logObjs))
322+
unmarshalledLogs = logObjs.to(seq[LogObject])
323+
verified = (await vp.verifyLogs(filter, unmarshalledLogs))
324+
325+
if verified.isErr():
326+
raise newException(ValueError, verified.error)
345327

346-
return logObjs
328+
return unmarshalledLogs
347329

348330
# Following methods are forwarded directly to the web3 provider and therefore
349331
# are not validated in any way.

nimbus_verified_proxy/rpc_api_backend.nim

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
{.push raises: [], gcsafe.}
99

10-
import json_rpc/[rpcproxy, rpcclient], web3/[eth_api, eth_api_types], stint, ./types
10+
import json_rpc/[rpcproxy, rpcclient], web3/[eth_api, eth_api_types], stint, std/json, ./types
1111

1212
proc initNetworkApiBackend*(vp: VerifiedRpcProxy): EthApiBackend =
1313
let
@@ -59,6 +59,21 @@ proc initNetworkApiBackend*(vp: VerifiedRpcProxy): EthApiBackend =
5959
): Future[seq[LogObject]] {.async: (raw: true).} =
6060
vp.proxy.getClient.eth_getLogs(filterOptions)
6161

62+
newFilterProc = proc(
63+
filterOptions: FilterOptions
64+
): Future[string] {.async: (raw: true).} =
65+
vp.proxy.getClient.eth_newFilter(filterOptions)
66+
67+
uninstallFilterProc = proc(
68+
filterId: string
69+
): Future[bool] {.async: (raw: true).} =
70+
vp.proxy.getClient.eth_uninstallFilter(filterId)
71+
72+
getFilterChangesProc = proc(
73+
filterId: string
74+
): Future[JsonNode] {.async: (raw: true).} =
75+
vp.proxy.getClient.eth_getFilterChanges(filterId)
76+
6277
EthApiBackend(
6378
eth_chainId: ethChainIdProc,
6479
eth_getBlockByHash: getBlockByHashProc,
@@ -70,4 +85,7 @@ proc initNetworkApiBackend*(vp: VerifiedRpcProxy): EthApiBackend =
7085
eth_getLogs: getLogsProc,
7186
eth_getTransactionByHash: getTransactionByHashProc,
7287
eth_getTransactionReceipt: getTransactionReceiptProc,
88+
eth_newFilter: newFilterProc,
89+
eth_uninstallFilter: uninstallFilterProc,
90+
eth_getFilterChanges: getFilterChangesProc
7391
)

nimbus_verified_proxy/types.nim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import
1212
json_rpc/[rpcproxy, rpcclient],
1313
web3/[eth_api, eth_api_types],
1414
stint,
15+
std/json,
1516
minilru,
1617
./header_store,
1718
../execution_chain/evm/async_evm
@@ -51,6 +52,10 @@ type
5152
GetTransactionReceiptProc = proc(txHash: Hash32): Future[ReceiptObject] {.async.}
5253
GetTransactionByHashProc = proc(txHash: Hash32): Future[TransactionObject] {.async.}
5354
GetLogsProc = proc(filterOptions: FilterOptions): Future[seq[LogObject]] {.async.}
55+
NewFilterProc = proc(filterOptions: FilterOptions): Future[string] {.async.}
56+
UninstallFilterProc = proc(filterId: string): Future[bool] {.async.}
57+
GetFilterChangesProc = proc(filterid: string): Future[JsonNode] {.async.}
58+
5459

5560
EthApiBackend* = object
5661
eth_chainId*: ChainIdProc
@@ -63,6 +68,9 @@ type
6368
eth_getTransactionReceipt*: GetTransactionReceiptProc
6469
eth_getTransactionByHash*: GetTransactionByHashProc
6570
eth_getLogs*: GetLogsProc
71+
eth_newFilter*: NewFilterProc
72+
eth_uninstallFilter*: UninstallFilterProc
73+
eth_getFilterChanges*: GetFilterChangesProc
6674

6775
VerifiedRpcProxy* = ref object
6876
evm*: AsyncEvm
@@ -75,7 +83,7 @@ type
7583

7684
# TODO: when the list grows big add a config object instead
7785
# config parameters
78-
filterStore*: Table[int, FilterOptions]
86+
filterStore*: Table[string, FilterOptions]
7987
chainId*: UInt256
8088
maxBlockWalk*: uint64
8189

@@ -92,7 +100,6 @@ proc init*(
92100
accountsCache: AccountsCache.init(ACCOUNTS_CACHE_SIZE),
93101
codeCache: CodeCache.init(CODE_CACHE_SIZE),
94102
storageCache: StorageCache.init(STORAGE_CACHE_SIZE),
95-
filterStore: initTable[int, FilterOptions](),
96103
chainId: chainId,
97104
maxBlockWalk: maxBlockWalk,
98105
)

0 commit comments

Comments
 (0)