Skip to content

Commit

Permalink
eth: bump to new types
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 committed Sep 26, 2024
1 parent 5e3f3db commit a9347c7
Show file tree
Hide file tree
Showing 114 changed files with 464 additions and 463 deletions.
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
4 changes: 2 additions & 2 deletions fluffy/eth_data/history_data_json_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func readBlockData*(
try:
# If wanted the hash for the corresponding header can be verified
if verify:
if keccakHash(blockData.header.hexToSeqByte()) != blockHash:
if keccak256(blockData.header.hexToSeqByte()) != blockHash:
return err("Data is not matching hash, number " & $blockData.number)

block:
Expand Down Expand Up @@ -135,7 +135,7 @@ func readHeaderData*(
try:
# If wanted the hash for the corresponding header can be verified
if verify:
if keccakHash(blockData.header.hexToSeqByte()) != blockHash:
if keccak256(blockData.header.hexToSeqByte()) != blockHash:
return err("Data is not matching hash, number " & $blockData.number)

let contentKey =
Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/history/history_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ proc validateBlockBody*(
body: PortalBlockBodyLegacy, header: BlockHeader
): Result[void, string] =
## Validate the block body against the txRoot and ommersHash from the header.
let calculatedOmmersHash = keccakHash(body.uncles.asSeq())
let calculatedOmmersHash = keccak256(body.uncles.asSeq())
if calculatedOmmersHash != header.ommersHash:
return err("Invalid ommers hash")

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

# Header Accumulator, as per specification:
# https://github.com/ethereum/portal-network-specs/blob/master/history-network.md#the-header-accumulator
Expand Down Expand Up @@ -62,12 +62,12 @@ type

# HistoricalHashesAccumulator
Accumulator* = object
historicalEpochs*: List[Bytes32, int(MAX_HISTORICAL_EPOCHS)]
historicalEpochs*: List[common_types.Bytes32, int(MAX_HISTORICAL_EPOCHS)]
currentEpoch*: EpochRecord

# HistoricalHashesAccumulator in its final state
FinishedAccumulator* = object
historicalEpochs*: List[Bytes32, int(MAX_HISTORICAL_EPOCHS)]
historicalEpochs*: List[common_types.Bytes32, int(MAX_HISTORICAL_EPOCHS)]
currentEpoch*: EpochRecord

func init*(T: type Accumulator): T =
Expand Down Expand Up @@ -103,7 +103,7 @@ func updateAccumulator*(a: var Accumulator, 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 @@ -150,7 +150,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)

# TODO: Implement more generalized `get_generalized_index`
Expand Down
4 changes: 2 additions & 2 deletions fluffy/network/state/state_endpoints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ proc getStorageProof(
let nibbles = slotKey.toPath().unpackNibbles()

var
addressHash = keccakHash(address)
addressHash = keccak256(address)
nibblesIdx = 0
key = ContractTrieNodeKey.init(addressHash, Nibbles.empty(), storageRoot)
proof = TrieProof.empty()
Expand Down Expand Up @@ -203,7 +203,7 @@ proc getCodeByStateRoot*(
return Opt.some(Bytecode.empty())

let
contractCodeKey = ContractCodeKey.init(keccakHash(address), account.codeHash)
contractCodeKey = ContractCodeKey.init(keccak256(address), account.codeHash)
contractCodeRetrieval = (await n.getContractCode(contractCodeKey)).valueOr:
warn "Failed to get contract code"
return Opt.none(Bytecode)
Expand Down
4 changes: 2 additions & 2 deletions fluffy/network/state/state_gossip.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func getParent*(offerWithKey: AccountTrieOfferWithKey): AccountTrieOfferWithKey
(key, offer) = offerWithKey
parent = offer.proof.withPath(key.path).getParent()
parentKey =
AccountTrieNodeKey.init(parent.path, keccakHash(parent.proof[^1].asSeq()))
AccountTrieNodeKey.init(parent.path, keccak256(parent.proof[^1].asSeq()))
parentOffer = AccountTrieNodeOffer.init(parent.proof, offer.blockHash)

parentOffer.withKey(parentKey)
Expand All @@ -81,7 +81,7 @@ func getParent*(offerWithKey: ContractTrieOfferWithKey): ContractTrieOfferWithKe
(key, offer) = offerWithKey
parent = offer.storageProof.withPath(key.path).getParent()
parentKey = ContractTrieNodeKey.init(
key.addressHash, parent.path, keccakHash(parent.proof[^1].asSeq())
key.addressHash, parent.path, keccak256(parent.proof[^1].asSeq())
)
parentOffer =
ContractTrieNodeOffer.init(parent.proof, offer.accountProof, offer.blockHash)
Expand Down
4 changes: 2 additions & 2 deletions fluffy/network/state/state_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func toPath*(hash: KeccakHash): Nibbles {.inline.} =
Nibbles.init(hash.data, isEven = true)

func toPath*(address: EthAddress): Nibbles {.inline.} =
keccakHash(address).toPath()
keccak256(address).toPath()

func toPath*(slotKey: UInt256): Nibbles {.inline.} =
keccakHash(toBytesBE(slotKey)).toPath()
keccak256(toBytesBE(slotKey)).toPath()
2 changes: 1 addition & 1 deletion fluffy/network/state/state_validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import results, eth/common, ../../common/common_utils, ./state_content, ./state_
export results, state_content

proc hashEquals(value: TrieNode | Bytecode, expectedHash: KeccakHash): bool {.inline.} =
keccakHash(value.asSeq()) == expectedHash
keccak256(value.asSeq()) == expectedHash

proc isValidNextNode(
thisNodeRlp: Rlp, rlpIdx: int, nextNode: TrieNode
Expand Down
10 changes: 5 additions & 5 deletions fluffy/tests/state_network_tests/state_test_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ proc getTrieProof*(
proc generateAccountProof*(
state: HexaryTrie, address: EthAddress
): TrieProof {.raises: [RlpError].} =
let key = keccakHash(address).data
let key = keccak256(address).data
state.getTrieProof(key)

proc generateStorageProof*(
state: HexaryTrie, slotKey: UInt256
): TrieProof {.raises: [RlpError].} =
let key = keccakHash(toBytesBE(slotKey)).data
let key = keccak256(toBytesBE(slotKey)).data
state.getTrieProof(key)

proc getGenesisAlloc*(filePath: string): GenesisAlloc =
Expand All @@ -89,20 +89,20 @@ proc toState*(
if genAccount.code.len() > 0:
var storageTrie = initHexaryTrie(newMemoryDB())
for slotKey, slotValue in genAccount.storage:
let key = keccakHash(toBytesBE(slotKey)).data
let key = keccak256(toBytesBE(slotKey)).data
storageTrie.put(key, rlp.encode(slotValue))

storageStates[address] = storageTrie
storageRoot = storageTrie.rootHash()
codeHash = keccakHash(genAccount.code)
codeHash = keccak256(genAccount.code)

let account = Account(
nonce: genAccount.nonce,
balance: genAccount.balance,
storageRoot: storageRoot,
codeHash: codeHash,
)
accountTrie.put(keccakHash(address).data, rlp.encode(account))
accountTrie.put(keccak256(address).data, rlp.encode(account))

(accountTrie, storageStates)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ suite "State Content Keys":
raiseAssert "Cannot read test vector: " & error

packedNibbles = packNibbles(testCase.path)
addressHash = EthAddress.fromHex(testCase.address).keccakHash()
addressHash = EthAddress.fromHex(testCase.address).keccak256()
nodeHash = NodeHash.fromHex(testCase.node_hash)
contentKey =
ContractTrieNodeKey.init(addressHash, packedNibbles, nodeHash).toContentKey()
Expand Down Expand Up @@ -91,7 +91,7 @@ suite "State Content Keys":
testCase = YamlContractBytecodeKey.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

addressHash = EthAddress.fromHex(testCase.address).keccakHash()
addressHash = EthAddress.fromHex(testCase.address).keccak256()
codeHash = CodeHash.fromHex(testCase.code_hash)
contentKey = ContractCodeKey.init(addressHash, codeHash).toContentKey()
encoded = contentKey.encode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ suite "State Endpoints - Genesis JSON Files":
let
proof = accountState.generateAccountProof(address)
leafNode = proof[^1]
addressHash = keccakHash(address)
addressHash = keccak256(address)
path = removeLeafKeyEndNibbles(Nibbles.init(addressHash.data, true), leafNode)
key = AccountTrieNodeKey.init(path, keccakHash(leafNode.asSeq()))
key = AccountTrieNodeKey.init(path, keccak256(leafNode.asSeq()))
offer = AccountTrieNodeOffer(proof: proof)

# store the account leaf node
Expand Down Expand Up @@ -68,7 +68,7 @@ suite "State Endpoints - Genesis JSON Files":
proc setupCodeInDb(stateNode: StateNode, address: EthAddress, code: seq[byte]) =
let
key =
ContractCodeKey(addressHash: keccakHash(address), codeHash: keccakHash(code))
ContractCodeKey(addressHash: keccak256(address), codeHash: keccak256(code))
value = ContractCodeRetrieval(code: Bytecode.init(code))

let contentKey = key.toContentKey().encode()
Expand All @@ -84,15 +84,15 @@ suite "State Endpoints - Genesis JSON Files":
slot: UInt256,
) =
let
addressHash = keccakHash(address)
addressHash = keccak256(address)
proof = accountState.generateAccountProof(address)
storageProof = storageState.generateStorageProof(slot)
leafNode = storageProof[^1]
path = removeLeafKeyEndNibbles(
Nibbles.init(keccakHash(toBytesBE(slot)).data, true), leafNode
Nibbles.init(keccak256(toBytesBE(slot)).data, true), leafNode
)
key = ContractTrieNodeKey(
addressHash: addressHash, path: path, nodeHash: keccakHash(leafNode.asSeq())
addressHash: addressHash, path: path, nodeHash: keccak256(leafNode.asSeq())
)
offer = ContractTrieNodeOffer(storageProof: storageProof, accountProof: proof)

Expand Down Expand Up @@ -233,7 +233,7 @@ suite "State Endpoints - Genesis JSON Files":
proofs.account.balance == account.balance
proofs.account.nonce == account.nonce
proofs.account.storageRoot == storageState.rootHash()
proofs.account.codeHash == keccakHash(account.code)
proofs.account.codeHash == keccak256(account.code)
proofs.accountProof.len() > 0
proofs.accountProof == accountState.generateAccountProof(address)
proofs.slots.len() == 0
Expand All @@ -253,7 +253,7 @@ suite "State Endpoints - Genesis JSON Files":
proofs.account.balance == account.balance
proofs.account.nonce == account.nonce
proofs.account.storageRoot == storageState.rootHash()
proofs.account.codeHash == keccakHash(account.code)
proofs.account.codeHash == keccak256(account.code)
proofs.accountProof.len() > 0
proofs.accountProof == accountState.generateAccountProof(address)
proofs.slots == @[(slotKey, slotValue)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ suite "State Gossip getParent - Genesis JSON Files":
let
proof = accountState.generateAccountProof(address)
leafNode = proof[^1]
addressHash = keccakHash(address).data
addressHash = keccak256(address).data
path = removeLeafKeyEndNibbles(Nibbles.init(addressHash, true), leafNode)
key = AccountTrieNodeKey.init(path, keccakHash(leafNode.asSeq()))
key = AccountTrieNodeKey.init(path, keccak256(leafNode.asSeq()))
offer = AccountTrieNodeOffer(proof: proof)

var db = newMemoryDB()
Expand Down Expand Up @@ -69,7 +69,7 @@ suite "State Gossip getParent - Genesis JSON Files":

for address, account in accounts:
let
addressHash = address.keccakHash()
addressHash = address.keccak256()
accountProof = accountState.generateAccountProof(address)

if account.code.len() > 0:
Expand All @@ -79,14 +79,14 @@ suite "State Gossip getParent - Genesis JSON Files":
let
storageProof = storageState.generateStorageProof(slotKey)
leafNode = storageProof[^1]
slotKeyHash = keccakHash(toBytesBE(slotKey)).data
slotKeyHash = keccak256(toBytesBE(slotKey)).data
path = removeLeafKeyEndNibbles(
Nibbles.init(keccakHash(toBytesBE(slotKey)).data, true), leafNode
Nibbles.init(keccak256(toBytesBE(slotKey)).data, true), leafNode
)
key = ContractTrieNodeKey(
addressHash: addressHash,
path: path,
nodeHash: keccakHash(leafNode.asSeq()),
nodeHash: keccak256(leafNode.asSeq()),
)
offer = ContractTrieNodeOffer(
storageProof: storageProof, accountProof: accountProof
Expand Down
20 changes: 10 additions & 10 deletions fluffy/tests/state_network_tests/test_state_validation_genesis.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ template checkValidProofsForExistingLeafs(
) =
for address, account in genAccounts:
var acc = newAccount(account.nonce, account.balance)
acc.codeHash = keccakHash(account.code)
acc.codeHash = keccak256(account.code)

let
addressHash = address.keccakHash()
addressHash = address.keccak256()
accountProof = accountState.generateAccountProof(address)
accountPath =
removeLeafKeyEndNibbles(Nibbles.init(addressHash.data, true), accountProof[^1])
accountTrieNodeKey = AccountTrieNodeKey(
path: accountPath, nodeHash: keccakHash(accountProof[^1].asSeq())
path: accountPath, nodeHash: keccak256(accountProof[^1].asSeq())
)
accountTrieOffer = AccountTrieNodeOffer(proof: accountProof)
proofResult = validateOffer(
Expand All @@ -56,12 +56,12 @@ template checkValidProofsForExistingLeafs(
let
storageProof = storageState.generateStorageProof(slotKey)
slotPath = removeLeafKeyEndNibbles(
Nibbles.init(keccakHash(toBytesBE(slotKey)).data, true), storageProof[^1]
Nibbles.init(keccak256(toBytesBE(slotKey)).data, true), storageProof[^1]
)
contractTrieNodeKey = ContractTrieNodeKey(
addressHash: addressHash,
path: slotPath,
nodeHash: keccakHash(storageProof[^1].asSeq()),
nodeHash: keccak256(storageProof[^1].asSeq()),
)
contractTrieOffer = ContractTrieNodeOffer(
storageProof: storageProof, accountProof: accountProof
Expand All @@ -78,15 +78,15 @@ template checkInvalidProofsWithBadValue(
) =
for address, account in genAccounts:
var acc = newAccount(account.nonce, account.balance)
acc.codeHash = keccakHash(account.code)
acc.codeHash = keccak256(account.code)

var
addressHash = address.keccakHash()
addressHash = address.keccak256()
accountProof = accountState.generateAccountProof(address)
accountPath =
removeLeafKeyEndNibbles(Nibbles.init(addressHash.data, true), accountProof[^1])
accountTrieNodeKey = AccountTrieNodeKey(
path: accountPath, nodeHash: keccakHash(accountProof[^1].asSeq())
path: accountPath, nodeHash: keccak256(accountProof[^1].asSeq())
)
accountProof[^1][^1] += 1 # bad account leaf value
let
Expand Down Expand Up @@ -115,12 +115,12 @@ template checkInvalidProofsWithBadValue(
var
storageProof = storageState.generateStorageProof(slotKey)
slotPath = removeLeafKeyEndNibbles(
Nibbles.init(keccakHash(toBytesBE(slotKey)).data, true), storageProof[^1]
Nibbles.init(keccak256(toBytesBE(slotKey)).data, true), storageProof[^1]
)
contractTrieNodeKey = ContractTrieNodeKey(
addressHash: addressHash,
path: slotPath,
nodeHash: keccakHash(storageProof[^1].asSeq()),
nodeHash: keccak256(storageProof[^1].asSeq()),
)
storageProof[^1][^1] += 1 # bad storage leaf value
let
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import
./state_test_helpers

proc getKeyBytes(i: int): seq[byte] =
let hash = keccakHash(u256(i).toBytesBE())
let hash = keccak256(u256(i).toBytesBE())
return toSeq(hash.data)

suite "State Validation - validateTrieProof":
Expand Down
2 changes: 1 addition & 1 deletion fluffy/tools/beacon_lc_bridge/beacon_lc_bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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 Down
2 changes: 1 addition & 1 deletion fluffy/tools/portal_bridge/portal_bridge_history.nim
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ proc runBackfillLoopAuditMode(
error "Failed to decode block header content", error
break headerBlock

if keccakHash(headerWithProof.header.asSeq()) != blockHash:
if keccak256(headerWithProof.header.asSeq()) != blockHash:
error "Block hash mismatch", blockNumber
break headerBlock

Expand Down
Loading

0 comments on commit a9347c7

Please sign in to comment.