Bump nim-web3 to 285d97c2b05bbe2a13dab4b52ea878157fb1a1a1 (#2088)

* Bump nim-web3 to 285d97c2b05bbe2a13dab4b52ea878157fb1a1a1

Unify EthCall/EthSend into TransactionArgs (#138)

* bump ssz-serialization

* Fix BlockNumber conversion

* Bump ssz-serialization: Restrict toSszType usage to non SszType in readSszBytes (#81)
This commit is contained in:
andri lim 2024-03-21 08:05:22 +07:00 committed by GitHub
parent 99238ce0e4
commit 30277be1f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 70 additions and 53 deletions

View File

@ -46,7 +46,7 @@ func init*(
): T {.raises: [ValidationError].} =
TransactionObject(
blockHash: some(w3Hash header.blockHash),
blockNumber: some(Quantity(header.blockNumber.truncate(uint64))),
blockNumber: some(rpc_types.BlockNumber(header.blockNumber.truncate(uint64))),
`from`: w3Addr tx.getSender(),
gas: Quantity(tx.gasLimit),
gasPrice: Quantity(tx.gasPrice),
@ -76,7 +76,7 @@ func init*(
let blockHash = header.blockHash
var blockObject = BlockObject(
number: Quantity(header.blockNumber.truncate(uint64)),
number: rpc_types.BlockNumber(header.blockNumber.truncate(uint64)),
hash: w3Hash blockHash,
parentHash: w3Hash header.parentHash,
nonce: some(FixedBytes[8](header.nonce)),
@ -272,7 +272,7 @@ proc installEthApiHandlers*(
raise newException(ValueError, "Unsupported block tag " & tag)
else:
let
blockNumber = quantityTag.number.toBlockNumber
blockNumber = quantityTag.number.uint64.toBlockNumber
maybeBlock = (await historyNetwork.getBlock(blockNumber)).valueOr:
raise newException(ValueError, error)

View File

@ -56,10 +56,10 @@ proc completeCmdArg*(T: type BlockHash, val: string): seq[string] =
proc walkBlocks(client: RpcClient, startHash: BlockHash) {.async: (raises: []).} =
var parentHash = startHash
var blockNumber: Quantity
var blockNumber: BlockNumber
# Should be 0x0, but block 0 does not exist in the json data file
while blockNumber != Quantity(0x1):
while blockNumber != BlockNumber(0x1):
let parentBlockOpt =
try:
await client.eth_getBlockByHash(parentHash, false)

View File

@ -221,6 +221,11 @@ proc maybeU64(n: Option[Quantity]): Option[uint64] =
return none(uint64)
some(n.get.uint64)
proc maybeU64(n: Option[Web3BlockNumber]): Option[uint64] =
if n.isNone:
return none(uint64)
some(n.get.uint64)
proc maybeBool(n: Option[Quantity]): Option[bool] =
if n.isNone:
return none(bool)
@ -238,7 +243,7 @@ proc maybeInt(n: Option[Quantity]): Option[int] =
proc toBlockHeader*(bc: BlockObject): common.BlockHeader =
common.BlockHeader(
blockNumber : toBlockNumber(bc.number),
blockNumber : bc.number.u256,
parentHash : ethHash bc.parentHash,
nonce : toBlockNonce(bc.nonce),
ommersHash : ethHash bc.sha3Uncles,
@ -538,7 +543,7 @@ proc verifyPoWProgress*(client: RpcClient, lastBlockHash: Hash256): Future[Resul
return err("cannot get block by hash " & lastBlockHash.data.toHex)
let header = res
let number = toBlockNumber(header.number)
let number = header.number.u256
let period = chronos.seconds(3)
var loop = 0
@ -554,7 +559,7 @@ proc verifyPoWProgress*(client: RpcClient, lastBlockHash: Hash256): Future[Resul
if diff.isZero:
return err("Expected PoW chain to progress in PoW mode, but following block difficulty: " & $diff)
if toBlockNumber(bc.number) > number:
if bc.number.u256 > number:
return ok()
await sleepAsync(period)

View File

@ -22,16 +22,17 @@ export
primitives
type
Web3Hash* = web3types.Hash256
Web3Address* = web3types.Address
Web3Bloom* = web3types.FixedBytes[256]
Web3Quantity* = web3types.Quantity
Web3PrevRandao* = web3types.FixedBytes[32]
Web3ExtraData* = web3types.DynamicBytes[0, 32]
Web3Topic* = eth_api_types.Topic
Web3Tx* = engine_api_types.TypedTransaction
Web3Blob* = engine_api_types.Blob
Web3KZGProof* = engine_api_types.KZGProof
Web3Hash* = web3types.Hash256
Web3Address* = web3types.Address
Web3Bloom* = web3types.FixedBytes[256]
Web3Quantity* = web3types.Quantity
Web3PrevRandao* = web3types.FixedBytes[32]
Web3ExtraData* = web3types.DynamicBytes[0, 32]
Web3BlockNumber* = web3types.BlockNumber
Web3Topic* = eth_api_types.Topic
Web3Tx* = engine_api_types.TypedTransaction
Web3Blob* = engine_api_types.Blob
Web3KZGProof* = engine_api_types.KZGProof
Web3KZGCommitment* = engine_api_types.KZGCommitment
{.push gcsafe, raises:[].}
@ -86,6 +87,9 @@ func u64*(x: Option[Web3Quantity]): Option[uint64] =
func u256*(x: Web3Quantity): UInt256 =
u256(x.uint64)
func u256*(x: Web3BlockNumber): UInt256 =
u256(x.uint64)
func u256*(x: FixedBytes[32]): UInt256 =
UInt256.fromBytesBE(x.bytes)
@ -221,6 +225,16 @@ func w3Qty*(x: Option[uint64]): Option[Web3Quantity] =
func w3Qty*(x: uint64): Web3Quantity =
Web3Quantity(x)
func w3BlockNumber*(x: Option[uint64]): Option[Web3BlockNumber] =
if x.isNone: none(Web3BlockNumber)
else: some(Web3BlockNumber x.get)
func w3BlockNumber*(x: uint64): Web3BlockNumber =
Web3BlockNumber(x)
func w3BlockNumber*(x: UInt256): Web3BlockNumber =
Web3BlockNumber x.truncate(uint64)
func w3FixedBytes*(x: UInt256): FixedBytes[32] =
FixedBytes[32](x.toBytesBE)

View File

@ -79,7 +79,7 @@ proc fetchBlockHeaderWithHash*(rpcClient: RpcClient, h: common.Hash256): Future[
proc fetchBlockHeaderWithNumber*(rpcClient: RpcClient, n: common.BlockNumber): Future[common.BlockHeader] {.async.} =
let t0 = now()
let bid = blockId(n.truncate(uint64))
let bid = blockId(w3BlockNumber n)
let blockObject: BlockObject = await rpcClient.eth_getBlockByNumber(bid, false)
durationSpentDoingFetches += now() - t0
fetchCounter += 1
@ -141,7 +141,7 @@ proc fetchAccountAndSlots*(rpcClient: RpcClient, address: EthAddress, slots: seq
debug "Got to fetchAccountAndSlots", address=address, slots=slots, blockNumber=blockNumber
#let blockNumberUint64 = blockNumber.truncate(uint64)
let a = web3.Address(address)
let bid = blockId(blockNumber.truncate(uint64))
let bid = blockId(w3BlockNumber blockNumber)
debug "About to call eth_getProof", address=address, slots=slots, blockNumber=blockNumber
let proofResponse: ProofResponse = await rpcClient.eth_getProof(a, slots, bid)
debug "Received response to eth_getProof", proofResponse=proofResponse
@ -161,7 +161,7 @@ proc fetchAccountAndSlots*(rpcClient: RpcClient, address: EthAddress, slots: seq
proc fetchCode*(client: RpcClient, blockNumber: common.BlockNumber, address: EthAddress): Future[seq[byte]] {.async.} =
let t0 = now()
let a = web3.Address(address)
let bid = blockId(blockNumber.truncate(uint64))
let bid = blockId(w3BlockNumber blockNumber)
let fetchedCode: seq[byte] = await client.eth_getCode(a, bid)
durationSpentDoingFetches += now() - t0
fetchCounter += 1

View File

@ -47,7 +47,7 @@ proc deriveLogs*(header: BlockHeader, transactions: seq[Transaction], receipts:
transactionIndex: some(w3Qty(i)),
transactionHash: some(w3Hash transactions[i].rlpHash),
blockHash: some(w3Hash header.blockHash),
blockNumber: some(w3Qty(header.blockNumber.truncate(uint64))),
blockNumber: some(w3BlockNumber(header.blockNumber)),
address: w3Addr log.address,
data: log.data,
# TODO topics should probably be kept as Hash256 in receipts

View File

@ -246,11 +246,11 @@ proc setupEthRpc*(
raise newException(ValueError, "Account locked, please unlock it first")
result = sign(acc.privateKey, cast[string](message))
server.rpc("eth_signTransaction") do(data: EthSend) -> seq[byte]:
server.rpc("eth_signTransaction") do(data: TransactionArgs) -> seq[byte]:
## Signs a transaction that can be submitted to the network at a later time using with
## eth_sendRawTransaction
let
address = data.`from`.ethAddr
address = data.`from`.get(w3Address()).ethAddr
acc = ctx.am.getAccount(address).tryGet()
if not acc.unlocked:
@ -263,14 +263,14 @@ proc setupEthRpc*(
signedTx = signTransaction(tx, acc.privateKey, com.chainId, eip155)
result = rlp.encode(signedTx)
server.rpc("eth_sendTransaction") do(data: EthSend) -> Web3Hash:
server.rpc("eth_sendTransaction") do(data: TransactionArgs) -> Web3Hash:
## Creates new message call transaction or a contract creation, if the data field contains code.
##
## obj: the transaction object.
## Returns the transaction hash, or the zero hash if the transaction is not yet available.
## Note: Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
let
address = data.`from`.ethAddr
address = data.`from`.get(w3Address()).ethAddr
acc = ctx.am.getAccount(address).tryGet()
if not acc.unlocked:
@ -301,7 +301,7 @@ proc setupEthRpc*(
raise newException(ValueError, res.error)
result = txHash.w3Hash
server.rpc("eth_call") do(call: EthCall, quantityTag: BlockTag) -> seq[byte]:
server.rpc("eth_call") do(call: TransactionArgs, quantityTag: BlockTag) -> seq[byte]:
## Executes a new message call immediately without creating a transaction on the block chain.
##
## call: the transaction call object.
@ -313,7 +313,7 @@ proc setupEthRpc*(
res = rpcCallEvm(callData, header, com)
result = res.output
server.rpc("eth_estimateGas") do(call: EthCall) -> Web3Quantity:
server.rpc("eth_estimateGas") do(call: TransactionArgs) -> Web3Quantity:
## Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
## The transaction will not be added to the blockchain. Note that the estimate may be significantly more than
## the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
@ -322,7 +322,6 @@ proc setupEthRpc*(
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
## Returns the amount of gas used.
let
# TODO: use latest spec EthCall
header = chainDB.headerFromTag(blockId("latest"))
callData = callData(call)
# TODO: DEFAULT_RPC_GAS_CAP should configurable

View File

@ -43,7 +43,7 @@ proc headerFromTag*(chain: CoreDbRef, blockId: BlockTag): BlockHeader
else:
raise newException(ValueError, "Unsupported block tag " & tag)
else:
let blockNum = blockId.number.toBlockNumber
let blockNum = blockId.number.uint64.toBlockNumber
result = chain.getBlockHeader(blockNum)
proc headerFromTag*(chain: CoreDbRef, blockTag: Option[BlockTag]): BlockHeader
@ -69,7 +69,7 @@ proc calculateMedianGasPrice*(chain: CoreDbRef): GasInt
else:
result = prices[middle]
proc unsignedTx*(tx: EthSend, chain: CoreDbRef, defaultNonce: AccountNonce): Transaction
proc unsignedTx*(tx: TransactionArgs, chain: CoreDbRef, defaultNonce: AccountNonce): Transaction
{.gcsafe, raises: [CatchableError].} =
if tx.to.isSome:
result.to = some(ethAddr(tx.to.get))
@ -94,7 +94,7 @@ proc unsignedTx*(tx: EthSend, chain: CoreDbRef, defaultNonce: AccountNonce): Tra
else:
result.nonce = defaultNonce
result.payload = tx.data
result.payload = tx.payload
template optionalAddress(src, dst: untyped) =
if src.isSome:
@ -112,7 +112,7 @@ template optionalBytes(src, dst: untyped) =
if src.isSome:
dst = src.get
proc callData*(call: EthCall): RpcCallData {.gcsafe, raises: [].} =
proc callData*(call: TransactionArgs): RpcCallData {.gcsafe, raises: [].} =
optionalAddress(call.source, result.source)
optionalAddress(call.to, result.to)
optionalGas(call.gas, result.gasLimit)
@ -160,7 +160,7 @@ proc populateTransactionObject*(tx: Transaction,
if optionalHeader.isSome:
let header = optionalHeader.get
result.blockHash = some(w3Hash header.hash)
result.blockNumber = some(w3Qty(header.blockNumber.truncate(uint64)))
result.blockNumber = some(w3BlockNumber(header.blockNumber))
result.`from` = w3Addr tx.getSender()
result.gas = w3Qty(tx.gasLimit)
@ -191,7 +191,7 @@ proc populateBlockObject*(header: BlockHeader, chain: CoreDbRef, fullTx: bool, i
let blockHash = header.blockHash
result = BlockObject()
result.number = w3Qty(header.blockNumber)
result.number = w3BlockNumber(header.blockNumber)
result.hash = w3Hash blockHash
result.parentHash = w3Hash header.parentHash
result.nonce = some(FixedBytes[8] header.nonce)
@ -206,7 +206,7 @@ proc populateBlockObject*(header: BlockHeader, chain: CoreDbRef, fullTx: bool, i
result.mixHash = w3Hash header.mixDigest
# discard sizeof(seq[byte]) of extraData and use actual length
let size = sizeof(BlockHeader) - sizeof(Blob) + header.extraData.len
let size = sizeof(BlockHeader) - sizeof(common.Blob) + header.extraData.len
result.size = w3Qty(size)
result.gasLimit = w3Qty(header.gasLimit)
@ -249,7 +249,7 @@ proc populateReceipt*(receipt: Receipt, gasUsed: GasInt, tx: Transaction,
result.transactionHash = w3Hash tx.rlpHash
result.transactionIndex = w3Qty(txIndex)
result.blockHash = w3Hash header.hash
result.blockNumber = w3Qty(header.blockNumber)
result.blockNumber = w3BlockNumber(header.blockNumber)
result.`from` = w3Addr tx.getSender()
result.to = some(w3Addr tx.destination)
result.cumulativeGasUsed = w3Qty(receipt.cumulativeGasUsed)

View File

@ -86,7 +86,7 @@ proc dumpMemoryDB*(node: JsonNode, db: CoreDbRef) =
n[k.toHex(false)] = %v
node["state"] = n
proc dumpMemoryDB*(node: JsonNode, kvt: TableRef[Blob,Blob]) =
proc dumpMemoryDB*(node: JsonNode, kvt: TableRef[common.Blob, common.Blob]) =
var n = newJObject()
for k, v in kvt:
n[k.toHex(false)] = %v

View File

@ -130,7 +130,7 @@ proc asBlockObject*(p: ExecutionData): BlockObject {.raises: [ValueError].} =
let headerSize = blockHeaderSize(p, txRoot)
let blockSize = txSize + headerSize
BlockObject(
number: p.blockNumber,
number: web3.BlockNumber p.blockNumber,
hash: p.blockHash,
parentHash: p.parentHash,
sha3Uncles: FixedBytes(etypes.EMPTY_UNCLE_HASH.data),

View File

@ -383,13 +383,12 @@ proc rpcMain*() =
check recoveredAddr == signer # verified
test "eth_signTransaction, eth_sendTransaction, eth_sendRawTransaction":
var unsignedTx = EthSend(
`from`: w3Addr(signer),
var unsignedTx = TransactionArgs(
`from`: w3Addr(signer).some,
to: w3Addr(ks2).some,
gas: w3Qty(100000'u).some,
gasPrice: none(Quantity),
value: some 100.u256,
data: @[],
nonce: none(Quantity)
)
@ -402,7 +401,7 @@ proc rpcMain*() =
check hashAhex == hashBhex
test "eth_call":
var ec = EthCall(
var ec = TransactionArgs(
`from`: w3Addr(signer).some,
to: w3Addr(ks2).some,
gas: w3Qty(100000'u).some,
@ -414,7 +413,7 @@ proc rpcMain*() =
check res == hexToSeqByte("deadbeef")
test "eth_estimateGas":
var ec = EthCall(
var ec = TransactionArgs(
`from`: w3Addr(signer).some,
to: w3Addr(ks3).some,
gas: w3Qty(42000'u).some,
@ -442,14 +441,14 @@ proc rpcMain*() =
test "eth_getTransactionByHash":
let res = await client.eth_getTransactionByHash(w3Hash env.txHash)
check res.isNil.not
check res.blockNumber.get() == w3Qty(1'u64)
check res.blockNumber.get() == w3BlockNumber(1'u64)
let res2 = await client.eth_getTransactionByHash(w3Hash env.blockHash)
check res2.isNil
test "eth_getTransactionByBlockHashAndIndex":
let res = await client.eth_getTransactionByBlockHashAndIndex(w3Hash env.blockHash, w3Qty(0'u64))
check res.isNil.not
check res.blockNumber.get() == w3Qty(1'u64)
check res.blockNumber.get() == w3BlockNumber(1'u64)
let res2 = await client.eth_getTransactionByBlockHashAndIndex(w3Hash env.blockHash, w3Qty(3'u64))
check res2.isNil
@ -460,7 +459,7 @@ proc rpcMain*() =
test "eth_getTransactionByBlockNumberAndIndex":
let res = await client.eth_getTransactionByBlockNumberAndIndex("latest", w3Qty(1'u64))
check res.isNil.not
check res.blockNumber.get() == w3Qty(1'u64)
check res.blockNumber.get() == w3BlockNumber(1'u64)
let res2 = await client.eth_getTransactionByBlockNumberAndIndex("latest", w3Qty(3'u64))
check res2.isNil
@ -468,7 +467,7 @@ proc rpcMain*() =
test "eth_getTransactionReceipt":
let res = await client.eth_getTransactionReceipt(w3Hash env.txHash)
check res.isNil.not
check res.blockNumber == w3Qty(1'u64)
check res.blockNumber == w3BlockNumber(1'u64)
let res2 = await client.eth_getTransactionReceipt(w3Hash env.blockHash)
check res2.isNil
@ -476,7 +475,7 @@ proc rpcMain*() =
test "eth_getUncleByBlockHashAndIndex":
let res = await client.eth_getUncleByBlockHashAndIndex(w3Hash env.blockHash, w3Qty(0'u64))
check res.isNil.not
check res.number == w3Qty(1'u64)
check res.number == w3BlockNumber(1'u64)
let res2 = await client.eth_getUncleByBlockHashAndIndex(w3Hash env.blockHash, w3Qty(1'u64))
check res2.isNil
@ -487,7 +486,7 @@ proc rpcMain*() =
test "eth_getUncleByBlockNumberAndIndex":
let res = await client.eth_getUncleByBlockNumberAndIndex("latest", w3Qty(0'u64))
check res.isNil.not
check res.number == w3Qty(1'u64)
check res.number == w3BlockNumber(1'u64)
let res2 = await client.eth_getUncleByBlockNumberAndIndex("latest", w3Qty(1'u64))
check res2.isNil

@ -1 +1 @@
Subproject commit aa8f487debbe5c0b7c49951d2b668beb394d9c01
Subproject commit fe033e257af5ff1188f92e295eabd33da6459fc5

2
vendor/nim-web3 vendored

@ -1 +1 @@
Subproject commit 428c46c94f58cf19d052b25cd3ae6f4c8c722e56
Subproject commit 285d97c2b05bbe2a13dab4b52ea878157fb1a1a1

2
vendor/nimbus-eth2 vendored

@ -1 +1 @@
Subproject commit 3863373575c89e172a734ba71cf516c572288a54
Subproject commit 1fe6efcf53d419b56f6fbf1e21289171017cad15