Skip to content

Commit

Permalink
Fluffy state network tests (#2245)
Browse files Browse the repository at this point in the history
* Add state network get content test.

* Completed state network get content tests.

* Completed state gossip offer content test.

* Improve state network offer content test.

* Completed state gossip offer test.

* Rename tests.
  • Loading branch information
web3-developer committed May 30, 2024
1 parent 4b8219a commit 1eb170e
Show file tree
Hide file tree
Showing 12 changed files with 1,201 additions and 301 deletions.
19 changes: 4 additions & 15 deletions fluffy/network/state/state_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,6 @@ proc getContractCode*(
): Future[Opt[ContractCodeRetrieval]] {.inline.} =
n.getContent(key, ContractCodeRetrieval)

# High level endpoints
# eth_getBalance
# eth_getStorageAt
# eth_getCode

func decodeKey(contentKey: ByteList): Opt[ContentKey] =
let key = ContentKey.decode(contentKey).valueOr:
return Opt.none(ContentKey)

Opt.some(key)

proc getStateRootByBlockHash(
n: StateNetwork, hash: BlockHash
): Future[Opt[KeccakHash]] {.async.} =
Expand All @@ -146,7 +135,7 @@ proc getStateRootByBlockHash(

Opt.some(header.stateRoot)

proc processOffer(
proc processOffer*(
n: StateNetwork,
maybeSrcNodeId: Opt[NodeId],
contentKeyBytes: ByteList,
Expand Down Expand Up @@ -183,10 +172,11 @@ proc processContentLoop(n: StateNetwork) {.async.} =
try:
while true:
let (srcNodeId, contentKeys, contentValues) = await n.contentQueue.popFirst()

for i, contentValueBytes in contentValues:
let
contentKeyBytes = contentKeys[i]
contentKey = decodeKey(contentKeyBytes).valueOr:
contentKey = ContentKey.decode(contentKeyBytes).valueOr:
error "Unable to decode offered content key", contentKeyBytes
continue

Expand Down Expand Up @@ -219,8 +209,7 @@ proc processContentLoop(n: StateNetwork) {.async.} =
trace "processContentLoop canceled"

proc start*(n: StateNetwork) =
info "Starting Portal execution state network",
protocolId = n.portalProtocol.protocolId
info "Starting Portal State Network", protocolId = n.portalProtocol.protocolId
n.portalProtocol.start()

n.processContentLoop = processContentLoop(n)
Expand Down
13 changes: 7 additions & 6 deletions fluffy/tests/state_network_tests/all_state_network_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
{.warning[UnusedImport]: off.}

import
./test_state_content_keys,
./test_state_content_keys_vectors,
./test_state_content_nibbles,
./test_state_content_values,
#./test_state_network_gossip,
./test_state_network,
./test_state_recursivegossip_genesis,
./test_state_recursivegossip_vectors,
./test_state_content_values_vectors,
./test_state_gossip_getparent_genesis,
./test_state_gossip_getparent_vectors,
./test_state_gossip_gossipoffer_vectors,
./test_state_network_getcontent_vectors,
./test_state_network_offercontent_vectors,
./test_state_validation_genesis,
./test_state_validation_trieproof,
./test_state_validation_vectors
61 changes: 59 additions & 2 deletions fluffy/tests/state_network_tests/state_test_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@

import
std/[sugar, sequtils],
chronos,
eth/[common, trie, trie/db],
eth/p2p/discoveryv5/protocol as discv5_protocol,
eth/p2p/discoveryv5/routing_table,
../../network/wire/[portal_protocol, portal_stream, portal_protocol_config],
../../nimbus/common/chain_config,
../../network/state/[state_content, state_utils],
../../eth_data/yaml_utils
../../network/history/[history_content, history_network],
../../network/state/[state_content, state_utils, state_network],
../../eth_data/yaml_utils,
../../database/content_db,
../test_helpers

export yaml_utils

Expand Down Expand Up @@ -129,3 +136,53 @@ proc toState*(
accountTrie.put(key, value)

(accountTrie, storageStates)

type StateNode* = ref object
discoveryProtocol*: discv5_protocol.Protocol
stateNetwork*: StateNetwork

proc newStateNode*(
rng: ref HmacDrbgContext, port: int
): StateNode {.raises: [CatchableError].} =
let
node = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(port))
db = ContentDB.new("", uint32.high, inMemory = true)
sm = StreamManager.new(node)
hn = HistoryNetwork.new(node, db, sm, FinishedAccumulator())
sn = StateNetwork.new(node, db, sm, historyNetwork = Opt.some(hn))

return StateNode(discoveryProtocol: node, stateNetwork: sn)

proc portalProtocol*(sn: StateNode): PortalProtocol =
sn.stateNetwork.portalProtocol

proc localNode*(sn: StateNode): Node =
sn.discoveryProtocol.localNode

proc start*(sn: StateNode) =
sn.stateNetwork.start()

proc stop*(sn: StateNode) {.async.} =
sn.stateNetwork.stop()
await sn.discoveryProtocol.closeWait()

proc containsId*(sn: StateNode, contentId: ContentId): bool =
return sn.stateNetwork.contentDB.get(contentId).isSome()

proc mockBlockHashToStateRoot*(
sn: StateNode, blockHash: BlockHash, stateRoot: KeccakHash
) =
let
blockHeader = BlockHeader(stateRoot: stateRoot)
headerRlp = rlp.encode(blockHeader)
blockHeaderWithProof = BlockHeaderWithProof(
header: ByteList.init(headerRlp), proof: BlockHeaderProof.init()
)
contentKeyBytes = history_content.ContentKey
.init(history_content.ContentType.blockHeader, blockHash)
.encode()
contentId = history_content.toContentId(contentKeyBytes)

sn.portalProtocol().storeContent(
contentKeyBytes, contentId, SSZ.encode(blockHeaderWithProof)
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import
../../network/state/[state_content, state_validation, state_gossip, state_utils],
./state_test_helpers

suite "State Recursive Gossip - Genesis JSON Files":
suite "State Gossip getParent - Genesis JSON Files":
let genesisFiles = [
"berlin2000.json", "calaveras.json", "chainid1.json", "chainid7.json",
"devnet4.json", "devnet5.json", "holesky.json", "mainshadow1.json", "merge.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import
../../network/state/[state_content, state_gossip],
./state_test_helpers

suite "State Recursive Gossip - Test Vectors":
suite "State Gossip getParent - Test Vectors":
test "Check account trie node parent matches expected recursive gossip":
const file = testVectorDir / "account_trie_node.yaml"

Expand Down

0 comments on commit 1eb170e

Please sign in to comment.