2024-07-30 14:56:21 +00:00
|
|
|
# Fluffy
|
|
|
|
# Copyright (c) 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
|
|
|
|
std/[sequtils, sugar],
|
|
|
|
eth/common,
|
|
|
|
../../../network/state/[state_content, state_utils, state_gossip],
|
|
|
|
./world_state
|
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
type OffersBuilder* = object
|
2024-07-30 14:56:21 +00:00
|
|
|
worldState: WorldStateRef
|
|
|
|
blockHash: BlockHash
|
|
|
|
accountTrieOffers: seq[AccountTrieOfferWithKey]
|
|
|
|
contractTrieOffers: seq[ContractTrieOfferWithKey]
|
|
|
|
contractCodeOffers: seq[ContractCodeOfferWithKey]
|
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
proc init*(T: type OffersBuilder, worldState: WorldStateRef, blockHash: BlockHash): T =
|
2024-07-30 14:56:21 +00:00
|
|
|
T(worldState: worldState, blockHash: blockHash)
|
|
|
|
|
|
|
|
proc toTrieProof(proof: seq[seq[byte]]): TrieProof =
|
|
|
|
TrieProof.init(proof.map((node) => TrieNode.init(node)))
|
|
|
|
|
|
|
|
proc buildAccountTrieNodeOffer(
|
2024-08-07 16:01:30 +00:00
|
|
|
builder: var OffersBuilder, addressHash: content_keys.AddressHash, proof: TrieProof
|
2024-07-30 14:56:21 +00:00
|
|
|
) =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
path = removeLeafKeyEndNibbles(
|
2024-08-07 16:01:30 +00:00
|
|
|
Nibbles.init(addressHash.data, isEven = true), proof[^1]
|
2024-07-30 14:56:21 +00:00
|
|
|
)
|
|
|
|
offerKey = AccountTrieNodeKey.init(path, keccakHash(proof[^1].asSeq()))
|
|
|
|
offerValue = AccountTrieNodeOffer.init(proof, builder.blockHash)
|
|
|
|
|
|
|
|
builder.accountTrieOffers.add(offerValue.withKey(offerKey))
|
|
|
|
except RlpError as e:
|
|
|
|
raiseAssert(e.msg) # Should never happen
|
|
|
|
|
|
|
|
proc buildContractTrieNodeOffer(
|
2024-08-06 07:38:38 +00:00
|
|
|
builder: var OffersBuilder,
|
2024-08-07 16:01:30 +00:00
|
|
|
addressHash: content_keys.AddressHash,
|
2024-07-30 14:56:21 +00:00
|
|
|
slotHash: SlotKeyHash,
|
|
|
|
storageProof: TrieProof,
|
|
|
|
accountProof: TrieProof,
|
|
|
|
) =
|
2024-08-06 07:38:38 +00:00
|
|
|
try:
|
|
|
|
let
|
|
|
|
path = removeLeafKeyEndNibbles(
|
|
|
|
Nibbles.init(slotHash.data, isEven = true), storageProof[^1]
|
|
|
|
)
|
2024-08-07 16:01:30 +00:00
|
|
|
offerKey = ContractTrieNodeKey.init(
|
|
|
|
addressHash, path, keccakHash(storageProof[^1].asSeq())
|
|
|
|
)
|
2024-08-06 07:38:38 +00:00
|
|
|
offerValue =
|
|
|
|
ContractTrieNodeOffer.init(storageProof, accountProof, builder.blockHash)
|
2024-07-30 14:56:21 +00:00
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
builder.contractTrieOffers.add(offerValue.withKey(offerKey))
|
|
|
|
except RlpError as e:
|
|
|
|
raiseAssert(e.msg) # Should never happen
|
2024-07-30 14:56:21 +00:00
|
|
|
|
|
|
|
proc buildContractCodeOffer(
|
2024-08-06 07:38:38 +00:00
|
|
|
builder: var OffersBuilder,
|
2024-08-07 16:01:30 +00:00
|
|
|
addressHash: content_keys.AddressHash,
|
2024-07-30 14:56:21 +00:00
|
|
|
code: seq[byte],
|
|
|
|
accountProof: TrieProof,
|
|
|
|
) =
|
|
|
|
let
|
|
|
|
#bytecode = Bytelist.init(code) # This fails to compile for some reason
|
|
|
|
bytecode = List[byte, MAX_BYTECODE_LEN](code)
|
2024-08-07 16:01:30 +00:00
|
|
|
offerKey = ContractCodeKey.init(addressHash, keccakHash(code))
|
2024-07-30 14:56:21 +00:00
|
|
|
offerValue = ContractCodeOffer.init(bytecode, accountProof, builder.blockHash)
|
|
|
|
|
|
|
|
builder.contractCodeOffers.add(offerValue.withKey(offerKey))
|
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
proc buildBlockOffers*(builder: var OffersBuilder) =
|
2024-08-07 16:01:30 +00:00
|
|
|
for addressHash, proof in builder.worldState.updatedAccountProofs():
|
2024-07-30 14:56:21 +00:00
|
|
|
let accountProof = toTrieProof(proof)
|
2024-08-07 16:01:30 +00:00
|
|
|
builder.buildAccountTrieNodeOffer(addressHash, accountProof)
|
2024-07-30 14:56:21 +00:00
|
|
|
|
2024-08-07 16:01:30 +00:00
|
|
|
for slotHash, sProof in builder.worldState.updatedStorageProofs(addressHash):
|
2024-07-30 14:56:21 +00:00
|
|
|
let storageProof = toTrieProof(sProof)
|
2024-08-07 16:01:30 +00:00
|
|
|
builder.buildContractTrieNodeOffer(
|
|
|
|
addressHash, slotHash, storageProof, accountProof
|
|
|
|
)
|
2024-07-30 14:56:21 +00:00
|
|
|
|
2024-08-07 16:01:30 +00:00
|
|
|
let code = builder.worldState.getUpdatedBytecode(addressHash)
|
2024-07-30 14:56:21 +00:00
|
|
|
if code.len() > 0:
|
2024-08-07 16:01:30 +00:00
|
|
|
builder.buildContractCodeOffer(addressHash, code, accountProof)
|
2024-07-30 14:56:21 +00:00
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
proc getAccountTrieOffers*(builder: OffersBuilder): lent seq[AccountTrieOfferWithKey] =
|
2024-07-30 14:56:21 +00:00
|
|
|
builder.accountTrieOffers
|
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
proc getContractTrieOffers*(
|
|
|
|
builder: OffersBuilder
|
|
|
|
): lent seq[ContractTrieOfferWithKey] =
|
2024-07-30 14:56:21 +00:00
|
|
|
builder.contractTrieOffers
|
|
|
|
|
2024-08-06 07:38:38 +00:00
|
|
|
proc getContractCodeOffers*(
|
|
|
|
builder: OffersBuilder
|
|
|
|
): lent seq[ContractCodeOfferWithKey] =
|
2024-07-30 14:56:21 +00:00
|
|
|
builder.contractCodeOffers
|