Skip to content

Commit

Permalink
Merge branch 'master' into server-api
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Sep 2, 2024
2 parents 9b8e615 + 84a72c8 commit e2aae7f
Show file tree
Hide file tree
Showing 32 changed files with 565 additions and 423 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@ available.)
For these variables, using <variable>=0 is ignored and <variable>=2
has the same effect as <variable>=1 (ditto for other numbers.)

Other settings where the non-zero value matters:

* ENABLE_ETH_VERSION=66<br>
Enable legacy protocol `eth66` (or another available protocol version.)


### <a name="devel-tips"></a>Development tips

Interesting Make variables and targets are documented in the [nimbus-build-system](https://github.com/status-im/nimbus-build-system) repo.
Expand Down
30 changes: 18 additions & 12 deletions fluffy/network/state/content/content_keys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,31 @@ type

ContentKeyType* = AccountTrieNodeKey | ContractTrieNodeKey | ContractCodeKey

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

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

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

func toContentKey*(key: AccountTrieNodeKey): ContentKey =
func toContentKey*(key: AccountTrieNodeKey): ContentKey {.inline.} =
ContentKey(contentType: accountTrieNode, accountTrieNodeKey: key)

func toContentKey*(key: ContractTrieNodeKey): ContentKey =
func toContentKey*(key: ContractTrieNodeKey): ContentKey {.inline.} =
ContentKey(contentType: contractTrieNode, contractTrieNodeKey: key)

func toContentKey*(key: ContractCodeKey): ContentKey =
func toContentKey*(key: ContractCodeKey): ContentKey {.inline.} =
ContentKey(contentType: contractCode, contractCodeKey: key)

proc readSszBytes*(data: openArray[byte], val: var ContentKey) {.raises: [SszError].} =
Expand All @@ -94,14 +98,16 @@ proc readSszBytes*(data: openArray[byte], val: var ContentKey) {.raises: [SszErr

readSszValue(data, val)

func encode*(contentKey: ContentKey): ContentKeyByteList =
func encode*(contentKey: ContentKey): ContentKeyByteList {.inline.} =
doAssert(contentKey.contentType != unused)
ContentKeyByteList.init(SSZ.encode(contentKey))

func decode*(T: type ContentKey, contentKey: ContentKeyByteList): Result[T, string] =
func decode*(
T: type ContentKey, contentKey: ContentKeyByteList
): Result[T, string] {.inline.} =
decodeSsz(contentKey.asSeq(), T)

func toContentId*(contentKey: ContentKeyByteList): ContentId =
func toContentId*(contentKey: ContentKeyByteList): ContentId {.inline.} =
# TODO: Should we try to parse the content key here for invalid ones?
let idHash = sha256.digest(contentKey.asSeq())
readUintBE[256](idHash.data)
51 changes: 30 additions & 21 deletions fluffy/network/state/content/content_values.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,59 @@ type
AccountTrieNodeRetrieval | ContractTrieNodeRetrieval | ContractCodeRetrieval
ContentValueType* = ContentOfferType | ContentRetrievalType

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

func init*(T: type AccountTrieNodeRetrieval, node: TrieNode): T =
AccountTrieNodeRetrieval(node: node)
func init*(T: type AccountTrieNodeRetrieval, node: TrieNode): T {.inline.} =
T(node: node)

func init*(
T: type ContractTrieNodeOffer,
storageProof: TrieProof,
accountProof: TrieProof,
blockHash: BlockHash,
): T =
ContractTrieNodeOffer(
storageProof: storageProof, accountProof: accountProof, blockHash: blockHash
)
): T {.inline.} =
T(storageProof: storageProof, accountProof: accountProof, blockHash: blockHash)

func init*(T: type ContractTrieNodeRetrieval, node: TrieNode): T =
ContractTrieNodeRetrieval(node: node)
func init*(T: type ContractTrieNodeRetrieval, node: TrieNode): T {.inline.} =
T(node: node)

func init*(
T: type ContractCodeOffer,
code: Bytecode,
accountProof: TrieProof,
blockHash: BlockHash,
): T =
ContractCodeOffer(code: code, accountProof: accountProof, blockHash: blockHash)
): T {.inline.} =
T(code: code, accountProof: accountProof, blockHash: blockHash)

func init*(T: type ContractCodeRetrieval, code: Bytecode): T =
ContractCodeRetrieval(code: code)
func init*(T: type ContractCodeRetrieval, code: Bytecode): T {.inline.} =
T(code: code)

func toRetrievalValue*(offer: AccountTrieNodeOffer): AccountTrieNodeRetrieval =
func toRetrievalValue*(
offer: AccountTrieNodeOffer
): AccountTrieNodeRetrieval {.inline.} =
AccountTrieNodeRetrieval.init(offer.proof[^1])

func toRetrievalValue*(offer: ContractTrieNodeOffer): ContractTrieNodeRetrieval =
func toRetrievalValue*(
offer: ContractTrieNodeOffer
): ContractTrieNodeRetrieval {.inline.} =
ContractTrieNodeRetrieval.init(offer.storageProof[^1])

func toRetrievalValue*(offer: ContractCodeOffer): ContractCodeRetrieval =
func toRetrievalValue*(offer: ContractCodeOffer): ContractCodeRetrieval {.inline.} =
ContractCodeRetrieval.init(offer.code)

func empty*(T: type TrieProof): T =
TrieProof.init(@[])
func empty*(T: type TrieProof): T {.inline.} =
T.init(@[])

func empty*(T: type Bytecode): T {.inline.} =
T(@[])

func encode*(value: ContentValueType): seq[byte] =
func encode*(value: ContentValueType): seq[byte] {.inline.} =
SSZ.encode(value)

func decode*(T: type ContentValueType, bytes: openArray[byte]): Result[T, string] =
func decode*(
T: type ContentValueType, bytes: openArray[byte]
): Result[T, string] {.inline.} =
decodeSsz(bytes, T)
142 changes: 98 additions & 44 deletions fluffy/network/state/state_endpoints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ proc getNextNodeHash(

proc getAccountProof(
n: StateNetwork, stateRoot: KeccakHash, address: EthAddress
): Future[Opt[TrieProof]] {.async: (raises: [CancelledError]).} =
): Future[Result[Opt[TrieProof], string]] {.async: (raises: [CancelledError]).} =
let nibbles = address.toPath().unpackNibbles()

var
Expand All @@ -85,28 +85,29 @@ proc getAccountProof(
proof = TrieProof.empty()

while nibblesIdx < nibbles.len():
let accountTrieNode = (await n.getAccountTrieNode(key)).valueOr:
return err("Failed to get account trie node when building account proof")

let
accountTrieNode = (await n.getAccountTrieNode(key)).valueOr:
warn "Failed to get account trie node when building account proof"
return Opt.none(TrieProof)
trieNode = accountTrieNode.node

let added = proof.add(trieNode)
added = proof.add(trieNode)
doAssert(added)

let (nextPath, nextNodeHash) = trieNode.getNextNodeHash(nibbles, nibblesIdx).valueOr:
break

key = AccountTrieNodeKey.init(nextPath, nextNodeHash)

# For now we don't return partial proofs or proofs for non-existing keys
# TODO: implement proofs for non-existing keys when needed for eth_getProof RPC
if nibblesIdx < nibbles.len():
Opt.none(TrieProof)
ok(Opt.none(TrieProof))
else:
Opt.some(proof)
ok(Opt.some(proof))

proc getStorageProof(
n: StateNetwork, storageRoot: KeccakHash, address: EthAddress, storageKey: UInt256
): Future[Opt[TrieProof]] {.async: (raises: [CancelledError]).} =
): Future[Result[Opt[TrieProof], string]] {.async: (raises: [CancelledError]).} =
let nibbles = storageKey.toPath().unpackNibbles()

var
Expand All @@ -116,86 +117,139 @@ proc getStorageProof(
proof = TrieProof.empty()

while nibblesIdx < nibbles.len():
let contractTrieNode = (await n.getContractTrieNode(key)).valueOr:
return err("Failed to get contract trie node when building account proof")

let
contractTrieNode = (await n.getContractTrieNode(key)).valueOr:
warn "Failed to get contract trie node when building account proof"
return Opt.none(TrieProof)
trieNode = contractTrieNode.node

let added = proof.add(trieNode)
added = proof.add(trieNode)
doAssert(added)

let (nextPath, nextNodeHash) = trieNode.getNextNodeHash(nibbles, nibblesIdx).valueOr:
break

key = ContractTrieNodeKey.init(addressHash, nextPath, nextNodeHash)

# For now we don't return partial proofs or proofs for non-existing keys
# TODO: implement proofs for non-existing keys when needed for eth_getProof RPC
if nibblesIdx < nibbles.len():
Opt.none(TrieProof)
ok(Opt.none(TrieProof))
else:
Opt.some(proof)
ok(Opt.some(proof))

proc getAccount(
n: StateNetwork, blockHash: BlockHash, address: EthAddress
n: StateNetwork, stateRoot: KeccakHash, address: EthAddress
): Future[Opt[Account]] {.async: (raises: [CancelledError]).} =
let
stateRoot = (await n.getStateRootByBlockHash(blockHash)).valueOr:
warn "Failed to get state root by block hash"
return Opt.none(Account)
accountProof = (await n.getAccountProof(stateRoot, address)).valueOr:
warn "Failed to get account proof"
maybeAccountProof = (await n.getAccountProof(stateRoot, address)).valueOr:
warn "Failed to get account proof", error = error
return Opt.none(Account)
accountProof = maybeAccountProof.valueOr:
info "Account doesn't exist, returning default account"
# return an empty account if the account doesn't exist
return Opt.some(newAccount())
account = accountProof.toAccount().valueOr:
error "Failed to get account from accountProof"
return Opt.none(Account)

Opt.some(account)

proc getBalanceByStateRoot*(
n: StateNetwork, stateRoot: KeccakHash, address: EthAddress
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
let account = (await n.getAccount(stateRoot, address)).valueOr:
return Opt.none(UInt256)

Opt.some(account.balance)

proc getTransactionCountByStateRoot*(
n: StateNetwork, stateRoot: KeccakHash, address: EthAddress
): Future[Opt[AccountNonce]] {.async: (raises: [CancelledError]).} =
let account = (await n.getAccount(stateRoot, address)).valueOr:
return Opt.none(AccountNonce)

Opt.some(account.nonce)

proc getStorageAtByStateRoot*(
n: StateNetwork, stateRoot: KeccakHash, address: EthAddress, slotKey: UInt256
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
let account = (await n.getAccount(stateRoot, address)).valueOr:
return Opt.none(UInt256)

if account.storageRoot == EMPTY_ROOT_HASH:
info "Storage doesn't exist, returning default storage value"
# return zero if the storage doesn't exist
return Opt.some(0.u256)

let
maybeStorageProof = (await n.getStorageProof(account.storageRoot, address, slotKey)).valueOr:
warn "Failed to get storage proof", error = error
return Opt.none(UInt256)
storageProof = maybeStorageProof.valueOr:
info "Slot doesn't exist, returning default storage value"
# return zero if the slot doesn't exist
return Opt.some(0.u256)
slotValue = storageProof.toSlot().valueOr:
error "Failed to get slot from storageProof"
return Opt.none(UInt256)

Opt.some(slotValue)

proc getCodeByStateRoot*(
n: StateNetwork, stateRoot: KeccakHash, address: EthAddress
): Future[Opt[Bytecode]] {.async: (raises: [CancelledError]).} =
let account = (await n.getAccount(stateRoot, address)).valueOr:
return Opt.none(Bytecode)

if account.codeHash == EMPTY_CODE_HASH:
info "Code doesn't exist, returning default code value"
# return empty bytecode if the code doesn't exist
return Opt.some(Bytecode.empty())

let
contractCodeKey = ContractCodeKey.init(keccakHash(address), account.codeHash)
contractCodeRetrieval = (await n.getContractCode(contractCodeKey)).valueOr:
warn "Failed to get contract code"
return Opt.none(Bytecode)

Opt.some(contractCodeRetrieval.code)

# Used by: eth_getBalance,
proc getBalance*(
n: StateNetwork, blockHash: BlockHash, address: EthAddress
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
let account = (await n.getAccount(blockHash, address)).valueOr:
let stateRoot = (await n.getStateRootByBlockHash(blockHash)).valueOr:
warn "Failed to get state root by block hash"
return Opt.none(UInt256)

Opt.some(account.balance)
await n.getBalanceByStateRoot(stateRoot, address)

# Used by: eth_getTransactionCount
proc getTransactionCount*(
n: StateNetwork, blockHash: BlockHash, address: EthAddress
): Future[Opt[AccountNonce]] {.async: (raises: [CancelledError]).} =
let account = (await n.getAccount(blockHash, address)).valueOr:
let stateRoot = (await n.getStateRootByBlockHash(blockHash)).valueOr:
warn "Failed to get state root by block hash"
return Opt.none(AccountNonce)

Opt.some(account.nonce)
await n.getTransactionCountByStateRoot(stateRoot, address)

# Used by: eth_getStorageAt
proc getStorageAt*(
n: StateNetwork, blockHash: BlockHash, address: EthAddress, slotKey: UInt256
): Future[Opt[UInt256]] {.async: (raises: [CancelledError]).} =
let
account = (await n.getAccount(blockHash, address)).valueOr:
return Opt.none(UInt256)
storageProof = (await n.getStorageProof(account.storageRoot, address, slotKey)).valueOr:
warn "Failed to get storage proof"
return Opt.none(UInt256)
slotValue = storageProof.toSlot().valueOr:
error "Failed to get slot from storageProof"
return Opt.none(UInt256)
let stateRoot = (await n.getStateRootByBlockHash(blockHash)).valueOr:
warn "Failed to get state root by block hash"
return Opt.none(UInt256)

Opt.some(slotValue)
await n.getStorageAtByStateRoot(stateRoot, address, slotKey)

# Used by: eth_getCode
proc getCode*(
n: StateNetwork, blockHash: BlockHash, address: EthAddress
): Future[Opt[Bytecode]] {.async: (raises: [CancelledError]).} =
let
account = (await n.getAccount(blockHash, address)).valueOr:
return Opt.none(Bytecode)
contractCodeKey = ContractCodeKey.init(keccakHash(address), account.codeHash)

let contractCodeRetrieval = (await n.getContractCode(contractCodeKey)).valueOr:
warn "Failed to get contract code"
let stateRoot = (await n.getStateRootByBlockHash(blockHash)).valueOr:
warn "Failed to get state root by block hash"
return Opt.none(Bytecode)

Opt.some(contractCodeRetrieval.code)
await n.getCodeByStateRoot(stateRoot, address)
21 changes: 21 additions & 0 deletions fluffy/rpc/rpc_calls/rpc_debug_calls.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# fluffy
# Copyright (c) 2021-2024 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.

{.push raises: [].}

import stint, json_rpc/[client, jsonmarshal], web3/conversions, web3/eth_api_types

export eth_api_types

createRpcSigsFromNim(RpcClient):
proc debug_getBalanceByStateRoot(data: Address, stateRoot: Hash256): UInt256
proc debug_getTransactionCountByStateRoot(data: Address, stateRoot: Hash256): Quantity
proc debug_getStorageAtByStateRoot(
data: Address, slot: UInt256, stateRoot: Hash256
): FixedBytes[32]

proc debug_getCodeByStateRoot(data: Address, stateRoot: Hash256): seq[byte]
Loading

0 comments on commit e2aae7f

Please sign in to comment.