Update nim-eth types (#6583)

Minimal changes needed for compatiblity with
https://github.com/status-im/nim-eth/pull/733 which aligns core types
with execution spec.
This commit is contained in:
Jacek Sieka 2024-09-29 13:32:55 +02:00 committed by GitHub
parent 2e64fdfa67
commit b20f1ba8b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 82 additions and 75 deletions

View File

@ -1133,6 +1133,8 @@ type
AnyConf* = BeaconNodeConf | ValidatorClientConf | SigningNodeConf
Address = primitives.Address
proc defaultDataDir*[Conf](config: Conf): string =
let dataDir = when defined(windows):
"AppData" / "Roaming" / "Nimbus"

View File

@ -35,11 +35,13 @@ logScope:
topics = "elman"
type
FixedBytes[N: static int] = web3.FixedBytes[N]
PubKeyBytes = DynamicBytes[48, 48]
WithdrawalCredentialsBytes = DynamicBytes[32, 32]
SignatureBytes = DynamicBytes[96, 96]
Int64LeBytes = DynamicBytes[8, 8]
WithoutTimeout* = distinct int
Address = web3.Address
SomeEnginePayloadWithValue =
BellatrixExecutionPayloadWithValue |

View File

@ -1282,25 +1282,25 @@ proc ETHExecutionBlockHeaderCreateFromJson(
if data.nonce.isNone:
return nil
let blockHeader = ExecutionBlockHeader(
parentHash: data.parentHash.asEth2Digest,
ommersHash: data.sha3Uncles.asEth2Digest,
coinbase: distinctBase(data.miner),
stateRoot: data.stateRoot.asEth2Digest,
txRoot: data.transactionsRoot.asEth2Digest,
receiptsRoot: data.receiptsRoot.asEth2Digest,
logsBloom: distinctBase(data.logsBloom),
parentHash: data.parentHash.asEth2Digest.to(Hash32),
ommersHash: data.sha3Uncles.asEth2Digest.to(Hash32),
coinbase: distinctBase(data.miner).to(EthAddress),
stateRoot: data.stateRoot.asEth2Digest.to(Hash32),
transactionsRoot: data.transactionsRoot.asEth2Digest.to(Hash32),
receiptsRoot: data.receiptsRoot.asEth2Digest.to(Hash32),
logsBloom: distinctBase(data.logsBloom).to(Bloom),
difficulty: data.difficulty,
number: distinctBase(data.number),
gasLimit: distinctBase(data.gasLimit),
gasUsed: distinctBase(data.gasUsed),
timestamp: EthTime(distinctBase(data.timestamp)),
extraData: distinctBase(data.extraData),
mixHash: data.mixHash.asEth2Digest,
nonce: distinctBase(data.nonce.get),
mixHash: data.mixHash.asEth2Digest.to(Hash32),
nonce: distinctBase(data.nonce.get).to(Bytes8),
baseFeePerGas: data.baseFeePerGas,
withdrawalsRoot:
if data.withdrawalsRoot.isSome:
Opt.some(data.withdrawalsRoot.get.asEth2Digest)
Opt.some(data.withdrawalsRoot.get.asEth2Digest.to(Hash32))
else:
Opt.none(ExecutionHash256),
blobGasUsed:
@ -1315,12 +1315,12 @@ proc ETHExecutionBlockHeaderCreateFromJson(
Opt.none(uint64),
parentBeaconBlockRoot:
if data.parentBeaconBlockRoot.isSome:
Opt.some distinctBase(data.parentBeaconBlockRoot.get.asEth2Digest)
Opt.some data.parentBeaconBlockRoot.get.asEth2Digest.to(Hash32)
else:
Opt.none(ExecutionHash256),
requestsRoot:
if data.requestsRoot.isSome:
Opt.some(data.requestsRoot.get.asEth2Digest)
Opt.some(data.requestsRoot.get.asEth2Digest.to(Hash32))
else:
Opt.none(ExecutionHash256))
if rlpHash(blockHeader) != executionHash[]:
@ -1342,7 +1342,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
wd = ExecutionWithdrawal(
index: distinctBase(data.index),
validatorIndex: distinctBase(data.validatorIndex),
address: distinctBase(data.address),
address: distinctBase(data.address).to(EthAddress),
amount: distinctBase(data.amount))
rlpBytes =
try:
@ -1353,7 +1353,7 @@ proc ETHExecutionBlockHeaderCreateFromJson(
wds.add ETHWithdrawal(
index: wd.index,
validatorIndex: wd.validatorIndex,
address: ExecutionAddress(data: wd.address),
address: ExecutionAddress(data: wd.address.data),
amount: wd.amount,
bytes: rlpBytes)
@ -1379,10 +1379,10 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct deposit request
let
req = ExecutionDepositRequest(
pubkey: distinctBase(data.pubkey),
withdrawalCredentials: distinctBase(data.withdrawalCredentials),
pubkey: distinctBase(data.pubkey).to(Bytes48),
withdrawalCredentials: distinctBase(data.withdrawalCredentials).to(Bytes32),
amount: distinctBase(data.amount),
signature: distinctBase(data.signature),
signature: distinctBase(data.signature).to(Bytes96),
index: distinctBase(data.index))
rlpBytes =
try:
@ -1391,10 +1391,10 @@ proc ETHExecutionBlockHeaderCreateFromJson(
raiseAssert "Unreachable"
depositRequests.add ETHDepositRequest(
pubkey: ValidatorPubKey(blob: req.pubkey),
withdrawalCredentials: req.withdrawalCredentials,
pubkey: ValidatorPubKey(blob: req.pubkey.data),
withdrawalCredentials: req.withdrawalCredentials.data,
amount: req.amount,
signature: ValidatorSig(blob: req.signature),
signature: ValidatorSig(blob: req.signature.data),
index: req.index,
bytes: rlpBytes)
@ -1411,8 +1411,8 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct withdrawal request
let
req = ExecutionWithdrawalRequest(
sourceAddress: distinctBase(data.sourceAddress),
validatorPubkey: distinctBase(data.validatorPubkey),
sourceAddress: distinctBase(data.sourceAddress).to(EthAddress),
validatorPubkey: distinctBase(data.validatorPubkey).to(Bytes48),
amount: distinctBase(data.amount))
rlpBytes =
try:
@ -1421,8 +1421,8 @@ proc ETHExecutionBlockHeaderCreateFromJson(
raiseAssert "Unreachable"
withdrawalRequests.add ETHWithdrawalRequest(
sourceAddress: ExecutionAddress(data: req.sourceAddress),
validatorPubkey: ValidatorPubKey(blob: req.validatorPubkey),
sourceAddress: ExecutionAddress(data: req.sourceAddress.data),
validatorPubkey: ValidatorPubKey(blob: req.validatorPubkey.data),
amount: req.amount,
bytes: rlpBytes)
@ -1439,9 +1439,9 @@ proc ETHExecutionBlockHeaderCreateFromJson(
# Construct consolidation request
let
req = ExecutionConsolidationRequest(
sourceAddress: distinctBase(data.sourceAddress),
sourcePubkey: distinctBase(data.sourcePubkey),
targetPubkey: distinctBase(data.targetPubkey))
sourceAddress: distinctBase(data.sourceAddress).to(EthAddress),
sourcePubkey: distinctBase(data.sourcePubkey).to(Bytes48),
targetPubkey: distinctBase(data.targetPubkey).to(Bytes48))
rlpBytes =
try:
rlp.encode(req)
@ -1449,9 +1449,9 @@ proc ETHExecutionBlockHeaderCreateFromJson(
raiseAssert "Unreachable"
consolidationRequests.add ETHConsolidationRequest(
sourceAddress: ExecutionAddress(data: req.sourceAddress),
sourcePubkey: ValidatorPubKey(blob: req.sourcePubkey),
targetPubkey: ValidatorPubKey(blob: req.targetPubkey),
sourceAddress: ExecutionAddress(data: req.sourceAddress.data),
sourcePubkey: ValidatorPubKey(blob: req.sourcePubkey.data),
targetPubkey: ValidatorPubKey(blob: req.targetPubkey.data),
bytes: rlpBytes)
# Verify requests root
@ -1487,9 +1487,9 @@ proc ETHExecutionBlockHeaderCreateFromJson(
let executionBlockHeader = ETHExecutionBlockHeader.new()
executionBlockHeader[] = ETHExecutionBlockHeader(
transactionsRoot: blockHeader.txRoot,
withdrawalsRoot: blockHeader.withdrawalsRoot.get(ZERO_HASH),
withdrawalsRoot: blockHeader.withdrawalsRoot.get(zeroHash32),
withdrawals: wds,
requestsRoot: blockHeader.requestsRoot.get(ZERO_HASH),
requestsRoot: blockHeader.requestsRoot.get(zeroHash32),
depositRequests: depositRequests,
withdrawalRequests: withdrawalRequests,
consolidationRequests: consolidationRequests)
@ -1776,7 +1776,7 @@ proc ETHTransactionsCreateFromJson(
gasLimit: distinctBase(data.gas).GasInt,
to:
if data.to.isSome:
Opt.some(distinctBase(data.to.get))
Opt.some(distinctBase(data.to.get).to(EthAddress))
else:
Opt.none(EthAddress),
value: data.value,
@ -1784,8 +1784,8 @@ proc ETHTransactionsCreateFromJson(
accessList:
if data.accessList.isSome:
data.accessList.get.mapIt(AccessPair(
address: distinctBase(it.address),
storageKeys: it.storageKeys.mapIt(distinctBase(it))))
address: distinctBase(it.address).to(EthAddress),
storageKeys: it.storageKeys.mapIt(distinctBase(it).to(Bytes32))))
else:
@[],
maxFeePerBlobGas:
@ -1793,14 +1793,14 @@ proc ETHTransactionsCreateFromJson(
versionedHashes:
if data.blobVersionedHashes.isSome:
data.blobVersionedHashes.get.mapIt(
ExecutionHash256(data: distinctBase(it)))
Bytes32(distinctBase(it)))
else:
@[],
authorizationList:
if data.authorizationList.isSome:
data.authorizationList.get.mapIt(Authorization(
chainId: it.chainId.ChainId,
address: distinctBase(it.address),
address: distinctBase(it.address).to(EthAddress),
nonce: distinctBase(it.nonce),
yParity: distinctBase(it.yParity),
R: it.R,
@ -1834,7 +1834,7 @@ proc ETHTransactionsCreateFromJson(
return Opt.none(array[20, byte])
pubkey = sig.recover(sigHash).valueOr:
return Opt.none(array[20, byte])
Opt.some keys.PublicKey(pubkey).toCanonicalAddress()
Opt.some keys.PublicKey(pubkey).toCanonicalAddress().data
# Compute from execution address
let
@ -1861,10 +1861,8 @@ proc ETHTransactionsCreateFromJson(
of DestinationType.Regular:
tx.to.get
of DestinationType.Create:
var res {.noinit.}: array[20, byte]
res[0 ..< 20] = keccakHash(rlp.encodeList(fromAddress, tx.nonce))
.data.toOpenArray(12, 31)
res
let hash = keccakHash(rlp.encodeList(fromAddress, tx.nonce))
hash.to(EthAddress)
# Compute authorizations
var authorizationList = newSeqOfCap[ETHAuthorizationTuple](
@ -1876,7 +1874,7 @@ proc ETHTransactionsCreateFromJson(
return nil
authorizationList.add ETHAuthorizationTuple(
chainId: distinctBase(auth.chainId).u256,
address: ExecutionAddress(data: auth.address),
address: ExecutionAddress(data: auth.address.data),
nonce: auth.nonce,
authority: ExecutionAddress(data: authority),
signature: @signature)
@ -1890,14 +1888,14 @@ proc ETHTransactionsCreateFromJson(
maxFeePerGas: tx.maxFeePerGas.uint64,
gas: tx.gasLimit.uint64,
destinationType: destinationType,
to: ExecutionAddress(data: toAddress),
to: ExecutionAddress(data: toAddress.data),
value: tx.value,
input: tx.payload,
accessList: tx.accessList.mapIt(ETHAccessTuple(
address: ExecutionAddress(data: it.address),
storageKeys: it.storageKeys.mapIt(Eth2Digest(data: it)))),
address: ExecutionAddress(data: it.address.data),
storageKeys: it.storageKeys.mapIt(Eth2Digest(data: it.data)))),
maxFeePerBlobGas: tx.maxFeePerBlobGas,
blobVersionedHashes: tx.versionedHashes,
blobVersionedHashes: tx.versionedHashes.mapIt(Eth2Digest(data: it.data)),
authorizationList: authorizationList,
signature: @rawSig,
bytes: rlpBytes.TypedTransaction)
@ -2581,14 +2579,14 @@ proc ETHReceiptsCreateFromJson(
status: distinctBase(data.status.get(1.Quantity)) != 0'u64,
hash:
if data.root.isSome:
ExecutionHash256(data: distinctBase(data.root.get))
ExecutionHash256(distinctBase(data.root.get))
else:
default(ExecutionHash256),
cumulativeGasUsed: distinctBase(data.cumulativeGasUsed).GasInt,
logsBloom: distinctBase(data.logsBloom),
logsBloom: distinctBase(data.logsBloom).to(Bloom),
logs: data.logs.mapIt(Log(
address: distinctBase(it.address),
topics: it.topics.mapIt(distinctBase(it)),
address: distinctBase(it.address).to(EthAddress),
topics: it.topics.mapIt(distinctBase(it).to(Bytes32)),
data: it.data)))
rlpBytes =
try:
@ -2605,10 +2603,10 @@ proc ETHReceiptsCreateFromJson(
root: rec.hash,
status: rec.status,
gasUsed: distinctBase(data.gasUsed), # Validated during sanity checks.
logsBloom: BloomLogs(data: rec.logsBloom),
logsBloom: BloomLogs(data: rec.logsBloom.data),
logs: rec.logs.mapIt(ETHLog(
address: ExecutionAddress(data: it.address),
topics: it.topics.mapIt(Eth2Digest(data: it)),
address: ExecutionAddress(data: it.address.data),
topics: it.topics.mapIt(Eth2Digest(data: it.data)),
data: it.data)),
bytes: rlpBytes)

View File

@ -454,7 +454,7 @@ proc computeTransactionsTrieRoot*(
raiseAssert "HexaryTrie.put failed: " & $exc.msg
tr.rootHash()
func toExecutionWithdrawal*(
func toExecutionWithdrawal(
withdrawal: capella.Withdrawal): ExecutionWithdrawal =
ExecutionWithdrawal(
index: withdrawal.index,
@ -462,6 +462,11 @@ func toExecutionWithdrawal*(
address: EthAddress withdrawal.address.data,
amount: distinctBase(withdrawal.amount))
proc rlpEncode(withdrawal: capella.Withdrawal): seq[byte] =
# TODO if this encode call is in a generic function, nim doesn't find the
# right `append` to use with `Address` (!)
rlp.encode(toExecutionWithdrawal(withdrawal))
# https://eips.ethereum.org/EIPS/eip-4895
proc computeWithdrawalsTrieRoot*(
payload: capella.ExecutionPayload | deneb.ExecutionPayload |
@ -472,7 +477,7 @@ proc computeWithdrawalsTrieRoot*(
var tr = initHexaryTrie(newMemoryDB())
for i, withdrawal in payload.withdrawals:
try:
tr.put(rlp.encode(i.uint), rlp.encode(toExecutionWithdrawal(withdrawal)))
tr.put(rlp.encode(i.uint), rlpEncode(withdrawal))
except RlpError as exc:
raiseAssert "HexaryTrie.put failed: " & $exc.msg
tr.rootHash()
@ -480,25 +485,25 @@ proc computeWithdrawalsTrieRoot*(
func toExecutionDepositRequest*(
request: electra.DepositRequest): ExecutionDepositRequest =
ExecutionDepositRequest(
pubkey: request.pubkey.blob,
withdrawalCredentials: request.withdrawal_credentials.data,
pubkey: Bytes48 request.pubkey.blob,
withdrawalCredentials: Bytes32 request.withdrawal_credentials.data,
amount: distinctBase(request.amount),
signature: request.signature.blob,
signature: Bytes96 request.signature.blob,
index: request.index)
func toExecutionWithdrawalRequest*(
request: electra.WithdrawalRequest): ExecutionWithdrawalRequest =
ExecutionWithdrawalRequest(
sourceAddress: request.source_address.data,
validatorPubkey: request.validator_pubkey.blob,
sourceAddress: Address request.source_address.data,
validatorPubkey: Bytes48 request.validator_pubkey.blob,
amount: distinctBase(request.amount))
func toExecutionConsolidationRequest*(
request: electra.ConsolidationRequest): ExecutionConsolidationRequest =
ExecutionConsolidationRequest(
sourceAddress: request.source_address.data,
sourcePubkey: request.source_pubkey.blob,
targetPubkey: request.target_pubkey.blob)
sourceAddress: Address request.source_address.data,
sourcePubkey: Bytes48 request.source_pubkey.blob,
targetPubkey: Bytes48 request.target_pubkey.blob)
# https://eips.ethereum.org/EIPS/eip-7685
proc computeRequestsTrieRoot(
@ -571,7 +576,7 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
Opt.none(uint64)
parentBeaconBlockRoot =
when typeof(payload).kind >= ConsensusFork.Deneb:
Opt.some ExecutionHash256(data: blck.parent_root.data)
Opt.some ExecutionHash256(blck.parent_root.data)
else:
Opt.none(ExecutionHash256)
requestsRoot =
@ -581,20 +586,20 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
Opt.none(ExecutionHash256)
ExecutionBlockHeader(
parentHash : payload.parent_hash,
parentHash : payload.parent_hash.to(Hash32),
ommersHash : EMPTY_UNCLE_HASH,
coinbase : EthAddress payload.fee_recipient.data,
stateRoot : payload.state_root,
txRoot : txRoot,
receiptsRoot : payload.receipts_root,
logsBloom : payload.logs_bloom.data,
stateRoot : payload.state_root.to(Root),
transactionsRoot : txRoot,
receiptsRoot : payload.receipts_root.to(Root),
logsBloom : BloomFilter payload.logs_bloom.data.to(Bloom),
difficulty : default(DifficultyInt),
number : payload.block_number,
gasLimit : payload.gas_limit,
gasUsed : payload.gas_used,
timestamp : EthTime(payload.timestamp),
extraData : payload.extra_data.asSeq,
mixHash : payload.prev_randao, # EIP-4399 `mixHash` -> `prevRandao`
mixHash : payload.prev_randao.to(Hash32), # EIP-4399 `mixHash` -> `prevRandao`
nonce : default(BlockNonce),
baseFeePerGas : Opt.some payload.base_fee_per_gas,
withdrawalsRoot : withdrawalsRoot,
@ -604,7 +609,7 @@ proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
requestsRoot : requestsRoot) # EIP-7685
proc compute_execution_block_hash*(blck: ForkyBeaconBlock): Eth2Digest =
rlpHash blockToBlockHeader(blck)
rlpHash(blockToBlockHeader(blck)).to(Eth2Digest)
from std/math import exp, ln
from std/sequtils import foldl

View File

@ -276,7 +276,7 @@ contract(DepositContract):
proc deposit(pubkey: PubKeyBytes,
withdrawalCredentials: WithdrawalCredentialsBytes,
signature: SignatureBytes,
deposit_data_root: FixedBytes[32])
deposit_data_root: web3.FixedBytes[32])
template `as`(address: Eth1Address, T: type bellatrix.ExecutionAddress): T =
T(data: distinctBase(address))

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 4eecab27efb6a743098632024505474d34a65e9b
Subproject commit 6bd6bae86c437a61bcd6426571204dd7cdb88123

2
vendor/nim-stew vendored

@ -1 +1 @@
Subproject commit 90a9bfd84389ad9ff1639a54404bed20c861ded8
Subproject commit 41f48efee97ac688fc48d40280c8d45d340cc46d