2024-05-22 01:22:07 +00:00
|
|
|
# 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.
|
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
import
|
|
|
|
results,
|
|
|
|
chronos,
|
|
|
|
chronicles,
|
|
|
|
eth/common,
|
2024-05-24 07:49:51 +00:00
|
|
|
../wire/portal_protocol,
|
|
|
|
./state_content,
|
|
|
|
./state_utils
|
2024-05-22 01:22:07 +00:00
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
export results, state_content
|
2024-05-22 01:22:07 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "portal_state"
|
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
type ProofWithPath = tuple[path: Nibbles, proof: TrieProof]
|
|
|
|
|
|
|
|
type AccountTrieOfferWithKey* =
|
|
|
|
tuple[key: AccountTrieNodeKey, offer: AccountTrieNodeOffer]
|
|
|
|
|
|
|
|
type ContractTrieOfferWithKey* =
|
|
|
|
tuple[key: ContractTrieNodeKey, offer: ContractTrieNodeOffer]
|
|
|
|
|
|
|
|
func withPath(proof: TrieProof, path: Nibbles): ProofWithPath =
|
|
|
|
(path: path, proof: proof)
|
|
|
|
|
|
|
|
func withKey*(
|
|
|
|
offer: AccountTrieNodeOffer, key: AccountTrieNodeKey
|
|
|
|
): AccountTrieOfferWithKey =
|
|
|
|
(key: key, offer: offer)
|
|
|
|
|
|
|
|
func withKey*(
|
|
|
|
offer: ContractTrieNodeOffer, key: ContractTrieNodeKey
|
|
|
|
): ContractTrieOfferWithKey =
|
|
|
|
(key: key, offer: offer)
|
|
|
|
|
|
|
|
func getParent(p: ProofWithPath): ProofWithPath =
|
2024-06-14 05:38:24 +00:00
|
|
|
# this function assumes that the proof contains valid rlp therefore
|
|
|
|
# if required these proofs should be validated beforehand
|
|
|
|
try:
|
|
|
|
doAssert(p.path.len() > 0, "nibbles too short")
|
|
|
|
doAssert(p.proof.len() > 1, "proof too short")
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
let
|
|
|
|
parentProof = TrieProof.init(p.proof[0 ..^ 2])
|
|
|
|
parentEndNode = rlpFromBytes(parentProof[^1].asSeq())
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
# the trie proof should have already been validated when receiving the offer content
|
|
|
|
doAssert(parentEndNode.listLen() == 2 or parentEndNode.listLen() == 17)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
var unpackedNibbles = p.path.unpackNibbles()
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
if parentEndNode.listLen() == 17:
|
|
|
|
# branch node so only need to remove a single nibble
|
|
|
|
return parentProof.withPath(unpackedNibbles.dropN(1).packNibbles())
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
# leaf or extension node so we need to remove one or more nibbles
|
|
|
|
let (_, _, prefixNibbles) = decodePrefix(parentEndNode.listElem(0))
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-14 05:38:24 +00:00
|
|
|
parentProof.withPath(unpackedNibbles.dropN(prefixNibbles.len()).packNibbles())
|
|
|
|
except RlpError as e:
|
|
|
|
raiseAssert(e.msg)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
func getParent*(offerWithKey: AccountTrieOfferWithKey): AccountTrieOfferWithKey =
|
2024-05-24 07:49:51 +00:00
|
|
|
let
|
2024-05-28 00:45:30 +00:00
|
|
|
(key, offer) = offerWithKey
|
2024-06-13 05:53:49 +00:00
|
|
|
parent = offer.proof.withPath(key.path).getParent()
|
|
|
|
parentKey =
|
|
|
|
AccountTrieNodeKey.init(parent.path, keccakHash(parent.proof[^1].asSeq()))
|
|
|
|
parentOffer = AccountTrieNodeOffer.init(parent.proof, offer.blockHash)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
parentOffer.withKey(parentKey)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
func getParent*(offerWithKey: ContractTrieOfferWithKey): ContractTrieOfferWithKey =
|
2024-05-24 07:49:51 +00:00
|
|
|
let
|
2024-05-28 00:45:30 +00:00
|
|
|
(key, offer) = offerWithKey
|
2024-06-13 05:53:49 +00:00
|
|
|
parent = offer.storageProof.withPath(key.path).getParent()
|
2024-05-24 07:49:51 +00:00
|
|
|
parentKey = ContractTrieNodeKey.init(
|
2024-06-13 05:53:49 +00:00
|
|
|
key.address, parent.path, keccakHash(parent.proof[^1].asSeq())
|
2024-05-24 07:49:51 +00:00
|
|
|
)
|
|
|
|
parentOffer =
|
2024-06-13 05:53:49 +00:00
|
|
|
ContractTrieNodeOffer.init(parent.proof, offer.accountProof, offer.blockHash)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
parentOffer.withKey(parentKey)
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
proc gossipOffer*(
|
|
|
|
p: PortalProtocol,
|
2024-05-24 07:49:51 +00:00
|
|
|
srcNodeId: Opt[NodeId],
|
|
|
|
keyBytes: ByteList,
|
|
|
|
offerBytes: seq[byte],
|
|
|
|
key: AccountTrieNodeKey,
|
|
|
|
offer: AccountTrieNodeOffer,
|
2024-06-14 12:21:30 +00:00
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2024-05-28 00:45:30 +00:00
|
|
|
let req1Peers = await p.neighborhoodGossip(
|
2024-05-24 07:49:51 +00:00
|
|
|
srcNodeId, ContentKeysList.init(@[keyBytes]), @[offerBytes]
|
|
|
|
)
|
2024-05-28 00:45:30 +00:00
|
|
|
info "Offered content gossipped successfully with peers", keyBytes, peers = req1Peers
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-22 01:22:07 +00:00
|
|
|
proc gossipOffer*(
|
|
|
|
p: PortalProtocol,
|
2024-05-24 07:49:51 +00:00
|
|
|
srcNodeId: Opt[NodeId],
|
|
|
|
keyBytes: ByteList,
|
|
|
|
offerBytes: seq[byte],
|
|
|
|
key: ContractTrieNodeKey,
|
|
|
|
offer: ContractTrieNodeOffer,
|
2024-06-14 12:21:30 +00:00
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2024-05-28 00:45:30 +00:00
|
|
|
let req1Peers = await p.neighborhoodGossip(
|
2024-05-24 07:49:51 +00:00
|
|
|
srcNodeId, ContentKeysList.init(@[keyBytes]), @[offerBytes]
|
|
|
|
)
|
2024-05-28 00:45:30 +00:00
|
|
|
info "Offered content gossipped successfully with peers", keyBytes, peers = req1Peers
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-06-13 05:53:49 +00:00
|
|
|
proc gossipOffer*(
|
|
|
|
p: PortalProtocol,
|
|
|
|
srcNodeId: Opt[NodeId],
|
|
|
|
keyBytes: ByteList,
|
|
|
|
offerBytes: seq[byte],
|
|
|
|
key: ContractCodeKey,
|
|
|
|
offer: ContractCodeOffer,
|
2024-06-14 12:21:30 +00:00
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2024-06-13 05:53:49 +00:00
|
|
|
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,
|
2024-06-14 12:21:30 +00:00
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2024-06-20 15:41:34 +00:00
|
|
|
await gossipOffer(p, srcNodeId, keyBytes, offerBytes, key, offer)
|
2024-06-13 05:53:49 +00:00
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
# root node, recursive gossip is finished
|
|
|
|
if key.path.unpackNibbles().len() == 0:
|
|
|
|
return
|
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
# continue the recursive gossip by sharing the parent offer with peers
|
|
|
|
let
|
|
|
|
(parentKey, parentOffer) = offer.withKey(key).getParent()
|
|
|
|
parentKeyBytes = parentKey.toContentKey().encode()
|
|
|
|
|
2024-06-20 15:41:34 +00:00
|
|
|
await recursiveGossipOffer(
|
2024-06-13 05:53:49 +00:00
|
|
|
p, srcNodeId, parentKeyBytes, parentOffer.encode(), parentKey, parentOffer
|
|
|
|
)
|
2024-05-22 01:22:07 +00:00
|
|
|
|
2024-06-13 05:53:49 +00:00
|
|
|
# Currently only used for testing to gossip an entire contract trie proof
|
|
|
|
# This may also be useful for the state network bridge
|
|
|
|
proc recursiveGossipOffer*(
|
2024-05-22 01:22:07 +00:00
|
|
|
p: PortalProtocol,
|
2024-05-24 07:49:51 +00:00
|
|
|
srcNodeId: Opt[NodeId],
|
|
|
|
keyBytes: ByteList,
|
|
|
|
offerBytes: seq[byte],
|
2024-06-13 05:53:49 +00:00
|
|
|
key: ContractTrieNodeKey,
|
|
|
|
offer: ContractTrieNodeOffer,
|
2024-06-14 12:21:30 +00:00
|
|
|
) {.async: (raises: [CancelledError]).} =
|
2024-06-20 15:41:34 +00:00
|
|
|
await gossipOffer(p, srcNodeId, keyBytes, offerBytes, key, offer)
|
2024-06-13 05:53:49 +00:00
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
2024-06-20 15:41:34 +00:00
|
|
|
await recursiveGossipOffer(
|
2024-06-13 05:53:49 +00:00
|
|
|
p, srcNodeId, parentKeyBytes, parentOffer.encode(), parentKey, parentOffer
|
2024-05-24 07:49:51 +00:00
|
|
|
)
|