Add blockByNumber to engine_client of simulator (#2902)

This commit is contained in:
andri lim 2024-12-04 10:57:21 +07:00 committed by GitHub
parent 359eb6d974
commit 5a3bfe486f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -25,7 +25,7 @@ const
defaultProtectedHeader = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" defaultProtectedHeader = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"
createRpcSigsFromNim(RpcClient): createRpcSigsFromNim(RpcClient):
proc engine_getClientVersionV1(version: ClientVersionV1): ClientVersionV1 proc engine_getClientVersionV1(version: ClientVersionV1): seq[ClientVersionV1]
func base64urlEncode(x: auto): string = func base64urlEncode(x: auto): string =
base64.encode(x, safe = true).replace("=", "") base64.encode(x, safe = true).replace("=", "")

View File

@ -40,6 +40,8 @@ template wrapTry(body: untyped) =
return err(e.msg) return err(e.msg)
except JsonRpcError as ex: except JsonRpcError as ex:
return err(ex.msg) return err(ex.msg)
except JsonReaderError as ex:
return err(ex.formatMsg("rpc"))
except CatchableError as ex: except CatchableError as ex:
return err(ex.msg) return err(ex.msg)
@ -455,6 +457,18 @@ proc latestBlock*(client: RpcClient): Result[Block, string] =
) )
return ok(output) return ok(output)
proc blockByNumber*(client: RpcClient, number: uint64): Result[Block, string] =
wrapTry:
let res = waitFor client.eth_getBlockByNumber(blockId(number), true)
if res.isNil:
return err("failed to get block " & $number)
let output = Block(
header: toBlockHeader(res),
transactions: toTransactions(res.transactions),
withdrawals: res.withdrawals,
)
return ok(output)
proc namedHeader*(client: RpcClient, name: string): Result[Header, string] = proc namedHeader*(client: RpcClient, name: string): Result[Header, string] =
wrapTry: wrapTry:
let res = waitFor client.eth_getBlockByNumber(name, false) let res = waitFor client.eth_getBlockByNumber(name, false)