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.
|
|
|
|
|
|
|
|
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 =
|
|
|
|
doAssert(p.path.len() > 0, "nibbles too short")
|
|
|
|
doAssert(p.proof.len() > 1, "proof too short")
|
2024-05-24 07:49:51 +00:00
|
|
|
|
|
|
|
let
|
2024-05-28 00:45:30 +00:00
|
|
|
parentProof = TrieProof.init(p.proof[0 ..^ 2])
|
2024-05-24 07:49:51 +00:00
|
|
|
parentEndNode = rlpFromBytes(parentProof[^1].asSeq())
|
|
|
|
|
|
|
|
# the trie proof should have already been validated when receiving the offer content
|
|
|
|
doAssert(parentEndNode.listLen() == 2 or parentEndNode.listLen() == 17)
|
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
var unpackedNibbles = p.path.unpackNibbles()
|
2024-05-24 07:49:51 +00:00
|
|
|
|
|
|
|
if parentEndNode.listLen() == 17:
|
|
|
|
# branch node so only need to remove a single nibble
|
2024-05-28 00:45:30 +00:00
|
|
|
return parentProof.withPath(unpackedNibbles.dropN(1).packNibbles())
|
2024-05-24 07:49:51 +00:00
|
|
|
|
|
|
|
# leaf or extension node so we need to remove one or more nibbles
|
2024-05-28 00:45:30 +00:00
|
|
|
let prefixNibbles = decodePrefix(parentEndNode.listElem(0))[2]
|
2024-05-24 07:49:51 +00:00
|
|
|
|
2024-05-28 00:45:30 +00:00
|
|
|
parentProof.withPath(unpackedNibbles.dropN(prefixNibbles.len()).packNibbles())
|
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
|
|
|
|
(parentPath, parentProof) = offer.proof.withPath(key.path).getParent()
|
|
|
|
|
|
|
|
parentKey = AccountTrieNodeKey.init(parentPath, keccakHash(parentProof[^1].asSeq()))
|
2024-05-24 07:49:51 +00:00
|
|
|
parentOffer = AccountTrieNodeOffer.init(parentProof, offer.blockHash)
|
|
|
|
|
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
|
|
|
|
(parentPath, parentProof) = offer.storageProof.withPath(key.path).getParent()
|
|
|
|
|
2024-05-24 07:49:51 +00:00
|
|
|
parentKey = ContractTrieNodeKey.init(
|
2024-05-28 00:45:30 +00:00
|
|
|
key.address, parentPath, keccakHash(parentProof[^1].asSeq())
|
2024-05-24 07:49:51 +00:00
|
|
|
)
|
|
|
|
parentOffer =
|
|
|
|
ContractTrieNodeOffer.init(parentProof, offer.accountProof, offer.blockHash)
|
|
|
|
|
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,
|
|
|
|
) {.async.} =
|
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
|
|
|
|
|
|
|
# root node, recursive gossip is finished
|
|
|
|
if key.path.unpackNibbles().len() == 0:
|
2024-05-22 01:22:07 +00:00
|
|
|
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()
|
|
|
|
req2Peers = await p.neighborhoodGossip(
|
|
|
|
srcNodeId, ContentKeysList.init(@[parentKeyBytes]), @[parentOffer.encode()]
|
|
|
|
)
|
|
|
|
|
|
|
|
info "Offered content parent gossipped successfully with peers",
|
|
|
|
parentKeyBytes, peers = req2Peers
|
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,
|
|
|
|
) {.async.} =
|
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
|
|
|
|
|
|
|
# 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()
|
|
|
|
req2Peers = await p.neighborhoodGossip(
|
|
|
|
srcNodeId, ContentKeysList.init(@[parentKeyBytes]), @[parentOffer.encode()]
|
|
|
|
)
|
|
|
|
|
|
|
|
info "Offered content parent gossipped successfully with peers",
|
|
|
|
parentKeyBytes, peers = req2Peers
|
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: ContractCodeKey,
|
|
|
|
offer: ContractCodeOffer,
|
|
|
|
) {.async.} =
|
2024-05-28 00:45:30 +00:00
|
|
|
let peers = 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
|