Skip to content

Commit

Permalink
Remove usage of aliases for Hash32 such as BlockHash (#2707)
Browse files Browse the repository at this point in the history
These create only confusion as if they are actual different types
and it is within their usage already clear what they are about
because of the name of the variable or the function.

They are also nowhere aliased like this in any of the Portal
specification.
  • Loading branch information
kdeme authored Oct 7, 2024
1 parent e1c942c commit 833719a
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 95 deletions.
1 change: 0 additions & 1 deletion fluffy/common/common_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type

ContentId* = UInt256
ContentKeyByteList* = ByteList[2048] # The encoded content key
BlockHash* = Hash32

func fromSszBytes*(T: type Hash32, data: openArray[byte]): T {.raises: [SszError].} =
if data.len != sizeof(result):
Expand Down
14 changes: 7 additions & 7 deletions fluffy/eth_data/history_data_json_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type

BlockDataTable* = Table[string, BlockData]

iterator blockHashes*(blockData: BlockDataTable): BlockHash =
iterator blockHashes*(blockData: BlockDataTable): Hash32 =
for k, v in blockData:
var blockHash: BlockHash
var blockHash: Hash32
try:
blockHash.data = hexToByteArray[sizeof(BlockHash)](k)
blockHash.data = hexToByteArray[sizeof(Hash32)](k)
except ValueError as e:
error "Invalid hex for block hash", error = e.msg, number = v.number
continue
Expand All @@ -54,9 +54,9 @@ func readBlockData*(
): Result[seq[(ContentKey, seq[byte])], string] =
var res: seq[(ContentKey, seq[byte])]

var blockHash: BlockHash
var blockHash: Hash32
try:
blockHash.data = hexToByteArray[sizeof(BlockHash)](hash)
blockHash.data = hexToByteArray[sizeof(Hash32)](hash)
except ValueError as e:
return err("Invalid hex for blockhash, number " & $blockData.number & ": " & e.msg)

Expand Down Expand Up @@ -127,9 +127,9 @@ func readBlockHeader*(blockData: BlockData): Result[Header, string] =
func readHeaderData*(
hash: string, blockData: BlockData, verify = false
): Result[(ContentKey, seq[byte]), string] =
var blockHash: BlockHash
var blockHash: Hash32
try:
blockHash.data = hexToByteArray[sizeof(BlockHash)](hash)
blockHash.data = hexToByteArray[sizeof(Hash32)](hash)
except ValueError as e:
return err("Invalid hex for blockhash, number " & $blockData.number & ": " & e.msg)

Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/history/content/content_deprecated.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type
epochRecordDeprecated = 0x03

BlockKey = object
blockHash: BlockHash
blockHash: Hash32

EpochRecordKeyDeprecated = object
epochHash: Digest
Expand Down
14 changes: 7 additions & 7 deletions fluffy/network/history/content/content_keys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type
blockNumber = 0x03

BlockKey* = object
blockHash*: BlockHash
blockHash*: Hash32

BlockNumberKey* = object
blockNumber*: uint64
Expand All @@ -49,19 +49,19 @@ type
of blockNumber:
blockNumberKey*: BlockNumberKey

func blockHeaderContentKey*(id: BlockHash | uint64): ContentKey =
when id is BlockHash:
func blockHeaderContentKey*(id: Hash32 | uint64): ContentKey =
when id is Hash32:
ContentKey(contentType: blockHeader, blockHeaderKey: BlockKey(blockHash: id))
else:
ContentKey(
contentType: blockNumber, blockNumberKey: BlockNumberKey(blockNumber: id)
)

func blockBodyContentKey*(hash: BlockHash): ContentKey =
ContentKey(contentType: blockBody, blockBodyKey: BlockKey(blockHash: hash))
func blockBodyContentKey*(blockHash: Hash32): ContentKey =
ContentKey(contentType: blockBody, blockBodyKey: BlockKey(blockHash: blockHash))

func receiptsContentKey*(hash: BlockHash): ContentKey =
ContentKey(contentType: receipts, receiptsKey: BlockKey(blockHash: hash))
func receiptsContentKey*(blockHash: Hash32): ContentKey =
ContentKey(contentType: receipts, receiptsKey: BlockKey(blockHash: blockHash))

func encode*(contentKey: ContentKey): ContentKeyByteList =
ContentKeyByteList.init(SSZ.encode(contentKey))
Expand Down
26 changes: 13 additions & 13 deletions fluffy/network/history/history_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ template calcReceiptsRoot*(receipts: PortalReceipts): Hash32 =
template calcWithdrawalsRoot*(receipts: Withdrawals): Hash32 =
calcRootHash(receipts)

func validateBlockHeader*(header: Header, hash: BlockHash): Result[void, string] =
if not (header.rlpHash() == hash):
func validateBlockHeader*(header: Header, blockHash: Hash32): Result[void, string] =
if not (header.rlpHash() == blockHash):
err("Block header hash does not match")
else:
ok()
Expand All @@ -197,7 +197,7 @@ func validateBlockHeader*(header: Header, number: uint64): Result[void, string]
ok()

func validateBlockHeaderBytes*(
bytes: openArray[byte], id: uint64 | BlockHash
bytes: openArray[byte], id: uint64 | Hash32
): Result[Header, string] =
let header = ?decodeRlp(bytes, Header)

Expand Down Expand Up @@ -411,7 +411,7 @@ func verifyHeader(
verifyHeader(n.accumulator, header, proof)

proc getVerifiedBlockHeader*(
n: HistoryNetwork, id: BlockHash | uint64
n: HistoryNetwork, id: Hash32 | uint64
): Future[Opt[Header]] {.async: (raises: [CancelledError]).} =
let
contentKey = blockHeaderContentKey(id).encode()
Expand Down Expand Up @@ -460,18 +460,18 @@ proc getVerifiedBlockHeader*(
return Opt.none(Header)

proc getBlockBody*(
n: HistoryNetwork, hash: BlockHash, header: Header
n: HistoryNetwork, blockHash: Hash32, header: Header
): Future[Opt[BlockBody]] {.async: (raises: [CancelledError]).} =
if header.txRoot == EMPTY_ROOT_HASH and header.ommersHash == EMPTY_UNCLE_HASH:
# Short path for empty body indicated by txRoot and ommersHash
return Opt.some(BlockBody(transactions: @[], uncles: @[]))

let
contentKey = blockBodyContentKey(hash).encode()
contentKey = blockBodyContentKey(blockHash).encode()
contentId = contentKey.toContentId()

logScope:
hash
blockHash
contentKey

let bodyFromDb = n.contentDB.get(BlockBody, contentId, header)
Expand Down Expand Up @@ -502,7 +502,7 @@ proc getBlockBody*(
return Opt.none(BlockBody)

proc getBlock*(
n: HistoryNetwork, id: BlockHash | uint64
n: HistoryNetwork, id: Hash32 | uint64
): Future[Opt[Block]] {.async: (raises: [CancelledError]).} =
debug "Trying to retrieve block", id

Expand All @@ -514,7 +514,7 @@ proc getBlock*(
warn "Failed to get header when getting block", id
return Opt.none(Block)
hash =
when id is BlockHash:
when id is Hash32:
id
else:
header.rlpHash()
Expand All @@ -526,25 +526,25 @@ proc getBlock*(

proc getBlockHashByNumber*(
n: HistoryNetwork, blockNumber: uint64
): Future[Result[BlockHash, string]] {.async: (raises: [CancelledError]).} =
): Future[Result[Hash32, string]] {.async: (raises: [CancelledError]).} =
let header = (await n.getVerifiedBlockHeader(blockNumber)).valueOr:
return err("Cannot retrieve block header for given block number")

ok(header.rlpHash())

proc getReceipts*(
n: HistoryNetwork, hash: BlockHash, header: Header
n: HistoryNetwork, blockHash: Hash32, header: Header
): Future[Opt[seq[Receipt]]] {.async: (raises: [CancelledError]).} =
if header.receiptsRoot == EMPTY_ROOT_HASH:
# Short path for empty receipts indicated by receipts root
return Opt.some(newSeq[Receipt]())

let
contentKey = receiptsContentKey(hash).encode()
contentKey = receiptsContentKey(blockHash).encode()
contentId = contentKey.toContentId()

logScope:
hash
blockHash
contentKey

let receiptsFromDb = n.getContentFromDb(seq[Receipt], contentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const

type
HeaderRecord* = object
blockHash*: BlockHash
blockHash*: Hash32
totalDifficulty*: UInt256

EpochRecord* = List[HeaderRecord, EPOCH_SIZE]
Expand Down
25 changes: 8 additions & 17 deletions fluffy/network/state/content/content_keys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import
export ssz_serialization, common_types, hash, results

type
NodeHash* = Hash32
CodeHash* = Hash32
AddressHash* = Hash32

ContentType* = enum
# Note: Need to add this unused value as a case object with an enum without
# a 0 valueis not allowed: "low(contentType) must be 0 for discriminant".
Expand All @@ -40,16 +36,16 @@ type

AccountTrieNodeKey* = object
path*: Nibbles
nodeHash*: NodeHash
nodeHash*: Hash32

ContractTrieNodeKey* = object
addressHash*: AddressHash
addressHash*: Hash32
path*: Nibbles
nodeHash*: NodeHash
nodeHash*: Hash32

ContractCodeKey* = object
addressHash*: AddressHash
codeHash*: CodeHash
addressHash*: Hash32
codeHash*: Hash32

ContentKey* = object
case contentType*: ContentType
Expand All @@ -64,21 +60,16 @@ type

ContentKeyType* = AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey

func init*(
T: type AccountTrieNodeKey, path: Nibbles, nodeHash: NodeHash
): T {.inline.} =
func init*(T: type AccountTrieNodeKey, path: Nibbles, nodeHash: Hash32): T {.inline.} =
T(path: path, nodeHash: nodeHash)

func init*(
T: type ContractTrieNodeKey,
addressHash: AddressHash,
path: Nibbles,
nodeHash: NodeHash,
T: type ContractTrieNodeKey, addressHash: Hash32, path: Nibbles, nodeHash: Hash32
): T {.inline.} =
T(addressHash: addressHash, path: path, nodeHash: nodeHash)

func init*(
T: type ContractCodeKey, addressHash: AddressHash, codeHash: CodeHash
T: type ContractCodeKey, addressHash: Hash32, codeHash: Hash32
): T {.inline.} =
T(addressHash: addressHash, codeHash: codeHash)

Expand Down
12 changes: 6 additions & 6 deletions fluffy/network/state/content/content_values.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ type

AccountTrieNodeOffer* = object
proof*: TrieProof
blockHash*: BlockHash
blockHash*: Hash32

AccountTrieNodeRetrieval* = object
node*: TrieNode

ContractTrieNodeOffer* = object
storageProof*: TrieProof
accountProof*: TrieProof
blockHash*: BlockHash
blockHash*: Hash32

ContractTrieNodeRetrieval* = object
node*: TrieNode

ContractCodeOffer* = object
code*: Bytecode
accountProof*: TrieProof
blockHash*: BlockHash
blockHash*: Hash32

ContractCodeRetrieval* = object
code*: Bytecode
Expand All @@ -53,7 +53,7 @@ type
ContentValueType* = ContentOfferType | ContentRetrievalType

func init*(
T: type AccountTrieNodeOffer, proof: TrieProof, blockHash: BlockHash
T: type AccountTrieNodeOffer, proof: TrieProof, blockHash: Hash32
): T {.inline.} =
T(proof: proof, blockHash: blockHash)

Expand All @@ -64,7 +64,7 @@ func init*(
T: type ContractTrieNodeOffer,
storageProof: TrieProof,
accountProof: TrieProof,
blockHash: BlockHash,
blockHash: Hash32,
): T {.inline.} =
T(storageProof: storageProof, accountProof: accountProof, blockHash: blockHash)

Expand All @@ -75,7 +75,7 @@ func init*(
T: type ContractCodeOffer,
code: Bytecode,
accountProof: TrieProof,
blockHash: BlockHash,
blockHash: Hash32,
): T {.inline.} =
T(code: code, accountProof: accountProof, blockHash: blockHash)

Expand Down
23 changes: 10 additions & 13 deletions fluffy/network/state/state_endpoints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ logScope:

proc getNextNodeHash(
trieNode: TrieNode, nibbles: UnpackedNibbles, nibbleIdx: var int
): Opt[(Nibbles, NodeHash)] =
): Opt[(Nibbles, Hash32)] =
# the trie node should have already been validated against the lookup hash
# so we expect that no rlp errors should be possible
try:
Expand All @@ -41,7 +41,7 @@ proc getNextNodeHash(

let nextHashBytes = trieNodeRlp.listElem(nextNibble.int).toBytes()
if nextHashBytes.len() == 0:
return Opt.none((Nibbles, NodeHash))
return Opt.none((Nibbles, Hash32))

nibbleIdx += 1
return Opt.some(
Expand All @@ -56,16 +56,16 @@ proc getNextNodeHash(
if unpackedPrefix != nibbles[nibbleIdx ..< nibbleIdx + unpackedPrefix.len()]:
# The nibbles don't match so we stop the search and don't increment
# the nibble index to indicate how many nibbles were consumed
return Opt.none((Nibbles, NodeHash))
return Opt.none((Nibbles, Hash32))

nibbleIdx += prefix.unpackNibbles().len()
if isLeaf:
return Opt.none((Nibbles, NodeHash))
return Opt.none((Nibbles, Hash32))

# extension node
let nextHashBytes = trieNodeRlp.listElem(1).toBytes()
if nextHashBytes.len() == 0:
return Opt.none((Nibbles, NodeHash))
return Opt.none((Nibbles, Hash32))

Opt.some((nibbles[0 ..< nibbleIdx].packNibbles(), Hash32.fromBytes(nextHashBytes)))
except RlpError as e:
Expand Down Expand Up @@ -263,7 +263,7 @@ proc getProofsByStateRoot*(

# Used by: eth_getBalance,
proc getBalance*(
n: StateNetwork, blockNumOrHash: uint64 | BlockHash, address: Address
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
let stateRoot = (await n.getStateRootByBlockNumOrHash(blockNumOrHash)).valueOr:
warn "Failed to get state root by block number or hash", blockNumOrHash
Expand All @@ -273,7 +273,7 @@ proc getBalance*(

# Used by: eth_getTransactionCount
proc getTransactionCount*(
n: StateNetwork, blockNumOrHash: uint64 | BlockHash, address: Address
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address
): Future[Opt[AccountNonce]] {.async: (raises: [CancelledError]).} =
let stateRoot = (await n.getStateRootByBlockNumOrHash(blockNumOrHash)).valueOr:
warn "Failed to get state root by block number or hash", blockNumOrHash
Expand All @@ -283,10 +283,7 @@ proc getTransactionCount*(

# Used by: eth_getStorageAt
proc getStorageAt*(
n: StateNetwork,
blockNumOrHash: uint64 | BlockHash,
address: Address,
slotKey: UInt256,
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address, slotKey: UInt256
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
let stateRoot = (await n.getStateRootByBlockNumOrHash(blockNumOrHash)).valueOr:
warn "Failed to get state root by block number or hash", blockNumOrHash
Expand All @@ -296,7 +293,7 @@ proc getStorageAt*(

# Used by: eth_getCode
proc getCode*(
n: StateNetwork, blockNumOrHash: uint64 | BlockHash, address: Address
n: StateNetwork, blockNumOrHash: uint64 | Hash32, address: Address
): Future[Opt[Bytecode]] {.async: (raises: [CancelledError]).} =
let stateRoot = (await n.getStateRootByBlockNumOrHash(blockNumOrHash)).valueOr:
warn "Failed to get state root by block number or hash", blockNumOrHash
Expand All @@ -307,7 +304,7 @@ proc getCode*(
# Used by: eth_getProof
proc getProofs*(
n: StateNetwork,
blockNumOrHash: uint64 | BlockHash,
blockNumOrHash: uint64 | Hash32,
address: Address,
slotKeys: seq[UInt256],
): Future[Opt[Proofs]] {.async: (raises: [CancelledError]).} =
Expand Down
Loading

0 comments on commit 833719a

Please sign in to comment.