Skip to content

Commit

Permalink
eth: bump to new types (#2660)
Browse files Browse the repository at this point in the history
This is a minimal set of changes to make things work with the new types
in nim-eth - this is the minimal PR that merely resolves
incompatibilities while the full change set would include more cleanup
and migration.
  • Loading branch information
arnetheduck authored Sep 29, 2024
1 parent 5f1b945 commit c210885
Show file tree
Hide file tree
Showing 130 changed files with 706 additions and 684 deletions.
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,4 @@
[submodule "vendor/nim-minilru"]
path = vendor/nim-minilru
url = https://github.com/status-im/nim-minilru.git
branch = master
2 changes: 1 addition & 1 deletion fluffy/common/common_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import

func fromBytes*(T: type KeccakHash, hash: openArray[byte]): T =
doAssert(hash.len() == 32)
KeccakHash(data: array[32, byte].initCopyFrom(hash))
KeccakHash(array[32, byte].initCopyFrom(hash))

iterator strippedLines(filename: string): string {.raises: [ref IOError].} =
for line in lines(filename):
Expand Down
10 changes: 8 additions & 2 deletions fluffy/eth_data/era1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ proc buildAccumulator*(f: Era1File): Result[EpochRecordCached, string] =
totalDifficulty = ?f.getTotalDifficulty(blockNumber)

headerRecords.add(
HeaderRecord(blockHash: blockHeader.blockHash(), totalDifficulty: totalDifficulty)
HeaderRecord(
blockHash: BlockHash(data: blockHeader.blockHash().data),
totalDifficulty: totalDifficulty,
)
)

ok(EpochRecordCached.init(headerRecords))
Expand Down Expand Up @@ -479,7 +482,10 @@ proc verify*(f: Era1File): Result[Digest, string] =
return err("Invalid receipts root")

headerRecords.add(
HeaderRecord(blockHash: blockHeader.blockHash(), totalDifficulty: totalDifficulty)
HeaderRecord(
blockHash: BlockHash(data: blockHeader.blockHash().data),
totalDifficulty: totalDifficulty,
)
)

let expectedRoot = ?f.getAccumulatorRoot()
Expand Down
1 change: 1 addition & 0 deletions fluffy/eth_data/history_data_json_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ proc getGenesisHeader*(id: NetworkId = MainNet): BlockHeader =
try:
networkParams(id)
except ValueError, RlpError:
debugEcho getCurrentException()[]
raise (ref Defect)(msg: "Network parameters should be valid")

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import
../../../common/common_types,
../history_content

export ssz_serialization, merkleization, proofs, eth_types_rlp
export ssz_serialization, merkleization, proofs, eth_types_rlp, BlockHash

# HistoricalHashesAccumulator, as per specification:
# https://github.com/ethereum/portal-network-specs/blob/master/history/history-network.md#the-historical-hashes-accumulator
Expand Down Expand Up @@ -68,6 +68,8 @@ type
historicalEpochs*: List[Bytes32, int(MAX_HISTORICAL_EPOCHS)]
currentEpoch*: EpochRecord

Bytes32 = common_types.Bytes32

func init*(T: type HistoricalHashesAccumulator): T =
HistoricalHashesAccumulator(
historicalEpochs: List[Bytes32, int(MAX_HISTORICAL_EPOCHS)].init(@[]),
Expand Down Expand Up @@ -101,7 +103,7 @@ func updateAccumulator*(a: var HistoricalHashesAccumulator, header: BlockHeader)
a.currentEpoch = EpochRecord.init(@[])

let headerRecord = HeaderRecord(
blockHash: header.blockHash(),
blockHash: BlockHash(data: header.blockHash().data),
totalDifficulty: lastTotalDifficulty + header.difficulty,
)

Expand Down Expand Up @@ -152,7 +154,7 @@ func verifyProof(
epochIndex = getEpochIndex(header)
epochRecordHash = Digest(data: a.historicalEpochs[epochIndex])

leave = hash_tree_root(header.blockHash())
leave = hash_tree_root(BlockHash(data: header.blockHash().data))
headerRecordIndex = getHeaderRecordIndex(header, epochIndex)

gIndex = GeneralizedIndex(EPOCH_SIZE * 2 * 2 + (headerRecordIndex * 2))
Expand Down
11 changes: 11 additions & 0 deletions fluffy/network/state/content/content_keys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ type

ContentKeyType* = AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey

func fromSszBytes*(
T: type KeccakHash, data: openArray[byte]
): T {.raises: [SszError].} =
if data.len != sizeof(result):
raiseIncorrectSize T

T.copyFrom(data)

template toSszType*(v: KeccakHash): array[32, byte] =
v.data

func init*(
T: type AccountTrieNodeKey, path: Nibbles, nodeHash: NodeHash
): T {.inline.} =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ proc createEmptyHeaders(fromNum: int, toNum: int): seq[BlockHeader] =
bh.difficulty = u256(i)
# empty so that we won't care about creating fake block bodies
bh.ommersHash = EMPTY_UNCLE_HASH
bh.txRoot = EMPTY_ROOT_HASH
bh.transactionsRoot = EMPTY_ROOT_HASH
headers.add(bh)
return headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import
stew/byteutils,
unittest2,
stint,
eth/common/eth_hash,
nimcrypto/hash,
eth/trie/[hexary, db, trie_defs],
../../network/state/[state_content, state_validation],
Expand Down
2 changes: 1 addition & 1 deletion fluffy/tests/test_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import
std/net,
eth/[common, keys],
eth/common/keys,
eth/p2p/discoveryv5/[enr, node, routing_table],
eth/p2p/discoveryv5/protocol as discv5_protocol

Expand Down
21 changes: 10 additions & 11 deletions fluffy/tools/beacon_lc_bridge/beacon_lc_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{.push raises: [].}

import
std/[os, strutils, options],
std/[os, strutils],
chronicles,
chronos,
confutils,
Expand All @@ -46,9 +46,9 @@ import
# for `BlockHeader`.
eth/common/eth_types as etypes,
eth/common/eth_types_rlp,
beacon_chain/el/el_manager,
beacon_chain/el/[el_manager, engine_api_conversions],
beacon_chain/gossip_processing/optimistic_processor,
beacon_chain/networking/topic_params,
beacon_chain/networking/[eth2_network, topic_params],
beacon_chain/spec/beaconstate,
beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
beacon_chain/[light_client, nimbus_binary_common],
Expand All @@ -61,7 +61,6 @@ import
../../common/common_types,
./beacon_lc_bridge_conf

from stew/objects import checkedEnumAssign
from web3/primitives as web3types import BlockHash

from beacon_chain/gossip_processing/block_processor import newExecutionPayload
Expand All @@ -70,7 +69,7 @@ from beacon_chain/gossip_processing/eth2_processor import toValidationResult
type Hash256 = etypes.Hash256

template asEthHash(hash: web3types.BlockHash): Hash256 =
Hash256(data: distinctBase(hash))
Hash32(distinctBase(hash))

proc calculateTransactionData(
items: openArray[TypedTransaction]
Expand All @@ -95,7 +94,7 @@ proc calculateWithdrawalsRoot(items: openArray[WithdrawalV1]): Hash256 {.raises:
let withdrawal = etypes.Withdrawal(
index: distinctBase(w.index),
validatorIndex: distinctBase(w.validatorIndex),
address: distinctBase(w.address),
address: distinctBase(w.address).to(EthAddress),
amount: distinctBase(w.amount),
)
tr.put(rlp.encode(i), rlp.encode(withdrawal))
Expand All @@ -115,9 +114,9 @@ proc asPortalBlockData*(
ommersHash: EMPTY_UNCLE_HASH,
coinbase: EthAddress payload.feeRecipient,
stateRoot: payload.stateRoot.asEthHash,
txRoot: txRoot,
transactionsRoot: txRoot,
receiptsRoot: payload.receiptsRoot.asEthHash,
logsBloom: distinctBase(payload.logsBloom),
logsBloom: distinctBase(payload.logsBloom).to(Bloom),
difficulty: default(DifficultyInt),
number: payload.blockNumber.distinctBase,
gasLimit: distinctBase(payload.gasLimit),
Expand Down Expand Up @@ -160,9 +159,9 @@ proc asPortalBlockData*(
ommersHash: EMPTY_UNCLE_HASH,
coinbase: EthAddress payload.feeRecipient,
stateRoot: payload.stateRoot.asEthHash,
txRoot: txRoot,
transactionsRoot: txRoot,
receiptsRoot: payload.receiptsRoot.asEthHash,
logsBloom: distinctBase(payload.logsBloom),
logsBloom: distinctBase(payload.logsBloom).to(Bloom),
difficulty: default(DifficultyInt),
number: payload.blockNumber.distinctBase,
gasLimit: distinctBase(payload.gasLimit),
Expand Down Expand Up @@ -271,7 +270,7 @@ proc run(config: BeaconBridgeConf) {.raises: [CatchableError].} =
when consensusFork >= ConsensusFork.Bellatrix:
if forkyBlck.message.is_execution_block:
template payload(): auto =
forkyBlck.message.body.execution_payload
forkyBlck.message.body

# TODO: Get rid of the asEngineExecutionPayload step?
let executionPayload = payload.asEngineExecutionPayload()
Expand Down
2 changes: 1 addition & 1 deletion fluffy/tools/eth_data_exporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ proc downloadHeader(client: RpcClient, i: uint64): BlockHeader =
fatal "Error while requesting BlockHeader", error = e.msg, number = i
quit 1

proc downloadBlock(i: uint64, client: RpcClient): Block =
proc downloadBlock(i: uint64, client: RpcClient): downloader.Block =
try:
return requestBlock(i, flags = {DownloadReceipts}, client = some(client))
except CatchableError as e:
Expand Down
6 changes: 3 additions & 3 deletions fluffy/tools/portal_bridge/portal_bridge_state.nim
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ proc runBackfillBuildBlockOffersLoop(
ws.applyGenesisAccounts(genesisAccounts)

if gossipGenesis:
let genesisBlockHash = KeccakHash.fromHex(
"d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
)
let genesisBlockHash =
hash32"d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"

var builder = OffersBuilder.init(ws, genesisBlockHash)
builder.buildBlockOffers()

Expand Down
Loading

0 comments on commit c210885

Please sign in to comment.