Skip to content

Commit

Permalink
Cleanup state network recursive gossip which was removed from the por…
Browse files Browse the repository at this point in the history
…tal specs. (#2345)
  • Loading branch information
web3-developer committed Jun 13, 2024
1 parent 060c759 commit f326ae2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 50 deletions.
87 changes: 53 additions & 34 deletions fluffy/network/state/state_gossip.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,22 @@ func getParent(p: ProofWithPath): ProofWithPath =
func getParent*(offerWithKey: AccountTrieOfferWithKey): AccountTrieOfferWithKey =
let
(key, offer) = offerWithKey
(parentPath, parentProof) = offer.proof.withPath(key.path).getParent()

parentKey = AccountTrieNodeKey.init(parentPath, keccakHash(parentProof[^1].asSeq()))
parentOffer = AccountTrieNodeOffer.init(parentProof, offer.blockHash)
parent = offer.proof.withPath(key.path).getParent()
parentKey =
AccountTrieNodeKey.init(parent.path, keccakHash(parent.proof[^1].asSeq()))
parentOffer = AccountTrieNodeOffer.init(parent.proof, offer.blockHash)

parentOffer.withKey(parentKey)

func getParent*(offerWithKey: ContractTrieOfferWithKey): ContractTrieOfferWithKey =
let
(key, offer) = offerWithKey
(parentPath, parentProof) = offer.storageProof.withPath(key.path).getParent()

parent = offer.storageProof.withPath(key.path).getParent()
parentKey = ContractTrieNodeKey.init(
key.address, parentPath, keccakHash(parentProof[^1].asSeq())
key.address, parent.path, keccakHash(parent.proof[^1].asSeq())
)
parentOffer =
ContractTrieNodeOffer.init(parentProof, offer.accountProof, offer.blockHash)
ContractTrieNodeOffer.init(parent.proof, offer.accountProof, offer.blockHash)

parentOffer.withKey(parentKey)

Expand All @@ -98,21 +97,6 @@ proc gossipOffer*(
)
info "Offered content gossipped successfully with peers", keyBytes, peers = req1Peers

# root node, recursive gossip is finished
if key.path.unpackNibbles().len() == 0:
return

# continue the recursive gossip by sharing the parent offer with peers
let
(parentKey, parentOffer) = offer.withKey(key).getParent()
parentKeyBytes = parentKey.toContentKey().encode()
req2Peers = await p.neighborhoodGossip(
srcNodeId, ContentKeysList.init(@[parentKeyBytes]), @[parentOffer.encode()]
)

info "Offered content parent gossipped successfully with peers",
parentKeyBytes, peers = req2Peers

proc gossipOffer*(
p: PortalProtocol,
srcNodeId: Opt[NodeId],
Expand All @@ -126,6 +110,31 @@ proc gossipOffer*(
)
info "Offered content gossipped successfully with peers", keyBytes, peers = req1Peers

proc gossipOffer*(
p: PortalProtocol,
srcNodeId: Opt[NodeId],
keyBytes: ByteList,
offerBytes: seq[byte],
key: ContractCodeKey,
offer: ContractCodeOffer,
) {.async.} =
let peers = await p.neighborhoodGossip(
srcNodeId, ContentKeysList.init(@[keyBytes]), @[offerBytes]
)
info "Offered content gossipped successfully with peers", keyBytes, peers

# Currently only used for testing to gossip an entire account trie proof
# This may also be useful for the state network bridge
proc recursiveGossipOffer*(
p: PortalProtocol,
srcNodeId: Opt[NodeId],
keyBytes: ByteList,
offerBytes: seq[byte],
key: AccountTrieNodeKey,
offer: AccountTrieNodeOffer,
) {.async.} =
asyncSpawn gossipOffer(p, srcNodeId, keyBytes, offerBytes, key, offer)

# root node, recursive gossip is finished
if key.path.unpackNibbles().len() == 0:
return
Expand All @@ -134,22 +143,32 @@ proc gossipOffer*(
let
(parentKey, parentOffer) = offer.withKey(key).getParent()
parentKeyBytes = parentKey.toContentKey().encode()
req2Peers = await p.neighborhoodGossip(
srcNodeId, ContentKeysList.init(@[parentKeyBytes]), @[parentOffer.encode()]
)

info "Offered content parent gossipped successfully with peers",
parentKeyBytes, peers = req2Peers
asyncSpawn recursiveGossipOffer(
p, srcNodeId, parentKeyBytes, parentOffer.encode(), parentKey, parentOffer
)

proc gossipOffer*(
# Currently only used for testing to gossip an entire contract trie proof
# This may also be useful for the state network bridge
proc recursiveGossipOffer*(
p: PortalProtocol,
srcNodeId: Opt[NodeId],
keyBytes: ByteList,
offerBytes: seq[byte],
key: ContractCodeKey,
offer: ContractCodeOffer,
key: ContractTrieNodeKey,
offer: ContractTrieNodeOffer,
) {.async.} =
let peers = await p.neighborhoodGossip(
srcNodeId, ContentKeysList.init(@[keyBytes]), @[offerBytes]
asyncSpawn gossipOffer(p, srcNodeId, keyBytes, offerBytes, key, offer)

# root node, recursive gossip is finished
if key.path.unpackNibbles().len() == 0:
return

# continue the recursive gossip by sharing the parent offer with peers
let
(parentKey, parentOffer) = offer.withKey(key).getParent()
parentKeyBytes = parentKey.toContentKey().encode()

asyncSpawn recursiveGossipOffer(
p, srcNodeId, parentKeyBytes, parentOffer.encode(), parentKey, parentOffer
)
info "Offered content gossipped successfully with peers", keyBytes, peers
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ procSuite "State Endpoints":
stateNode2.mockBlockHashToStateRoot(contentValue.blockHash, stateRoot)

# offer the leaf node
await stateNode1.portalProtocol.gossipOffer(
await stateNode1.portalProtocol.recursiveGossipOffer(
Opt.none(NodeId),
contentKeyBytes,
contentValueBytes,
Expand Down Expand Up @@ -167,7 +167,7 @@ procSuite "State Endpoints":
stateNode2.mockBlockHashToStateRoot(contentValue.blockHash, stateRoot)

# offer the leaf node
await stateNode1.portalProtocol.gossipOffer(
await stateNode1.portalProtocol.recursiveGossipOffer(
Opt.none(NodeId),
contentKeyBytes,
contentValueBytes,
Expand All @@ -192,7 +192,7 @@ procSuite "State Endpoints":
stateNode2.mockBlockHashToStateRoot(contentValue.blockHash, stateRoot)

# offer the leaf node
await stateNode1.portalProtocol.gossipOffer(
await stateNode1.portalProtocol.recursiveGossipOffer(
Opt.none(NodeId),
contentKeyBytes,
contentValueBytes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ procSuite "State Gossip - Gossip Offer":
await sleepAsync(1.milliseconds)
await sleepAsync(100.milliseconds)

# check that both the offer and parent were received by the second state instance
# check that the offer was received by the second state instance
let res1 =
await stateNode2.stateNetwork.getAccountTrieNode(contentKey.accountTrieNodeKey)
check:
Expand All @@ -89,14 +89,13 @@ procSuite "State Gossip - Gossip Offer":
res1.get() == contentValue.toRetrievalValue()
res1.get().node == contentValue.toRetrievalValue().node

# check that the parent offer was not received by the second state instance
let res2 = await stateNode2.stateNetwork.getAccountTrieNode(
parentContentKey.accountTrieNodeKey
)
check:
stateNode2.containsId(parentContentId)
res2.isOk()
res2.get() == parentContentValue.toRetrievalValue()
res2.get().node == parentContentValue.toRetrievalValue().node
not stateNode2.containsId(parentContentId)
res2.isNone()

await stateNode1.stop()
await stateNode2.stop()
Expand Down Expand Up @@ -156,7 +155,7 @@ procSuite "State Gossip - Gossip Offer":
await sleepAsync(1.milliseconds)
await sleepAsync(100.milliseconds)

# check that both the offer and parent were received by the second state instance
# check that the offer was received by the second state instance
let res1 = await stateNode2.stateNetwork.getContractTrieNode(
contentKey.contractTrieNodeKey
)
Expand All @@ -166,14 +165,13 @@ procSuite "State Gossip - Gossip Offer":
res1.get() == contentValue.toRetrievalValue()
res1.get().node == contentValue.toRetrievalValue().node

# check that the offer parent was not received by the second state instance
let res2 = await stateNode2.stateNetwork.getContractTrieNode(
parentContentKey.contractTrieNodeKey
)
check:
stateNode2.containsId(parentContentId)
res2.isOk()
res2.get() == parentContentValue.toRetrievalValue()
res2.get().node == parentContentValue.toRetrievalValue().node
not stateNode2.containsId(parentContentId)
res2.isNone()

await stateNode1.stop()
await stateNode2.stop()
Expand Down Expand Up @@ -222,7 +220,7 @@ procSuite "State Gossip - Gossip Offer":
await sleepAsync(1.milliseconds)
await sleepAsync(100.milliseconds)

# check that both the offer and parent were received by the second state instance
# check that the offer was received by the second state instance
let res1 =
await stateNode2.stateNetwork.getContractCode(contentKey.contractCodeKey)
check:
Expand Down Expand Up @@ -273,7 +271,7 @@ procSuite "State Gossip - Gossip Offer":
check not stateNode2.containsId(contentId)

# offer the leaf node
await stateNode1.portalProtocol.gossipOffer(
await stateNode1.portalProtocol.recursiveGossipOffer(
Opt.none(NodeId),
contentKeyBytes,
contentValueBytes,
Expand Down Expand Up @@ -350,7 +348,7 @@ procSuite "State Gossip - Gossip Offer":
check not stateNode2.containsId(contentId)

# offer the leaf node
await stateNode1.portalProtocol.gossipOffer(
await stateNode1.portalProtocol.recursiveGossipOffer(
Opt.none(NodeId),
contentKeyBytes,
contentValueBytes,
Expand Down

0 comments on commit f326ae2

Please sign in to comment.