Skip to content

Commit

Permalink
Refactor and apply renaming to post merge block proofs (#2648)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme authored Sep 23, 2024
1 parent 35d4529 commit 0fb9581
Show file tree
Hide file tree
Showing 29 changed files with 131 additions and 103 deletions.
7 changes: 6 additions & 1 deletion fluffy/database/era1_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

{.push raises: [].}

import std/os, stew/io2, results, ../network/history/accumulator, ../eth_data/era1
import
std/os,
stew/io2,
results,
../network/history/validation/historical_hashes_accumulator,
../eth_data/era1

type Era1DB* = ref object
## The Era1 database manages a collection of era files that together make up
Expand Down
2 changes: 1 addition & 1 deletion fluffy/eth_data/era1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import
beacon_chain/spec/beacon_time,
ssz_serialization,
ncli/e2store,
../network/history/accumulator
../network/history/validation/historical_hashes_accumulator

from nimcrypto/hash import fromHex
from ../../nimbus/utils/utils import calcTxRoot, calcReceiptsRoot
Expand Down
2 changes: 1 addition & 1 deletion fluffy/eth_data/history_data_json_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import
chronicles,
eth/[rlp, common/eth_types],
../../nimbus/common/[chain_config, genesis],
../network/history/[history_content, accumulator]
../network/history/[history_content, validation/historical_hashes_accumulator]

export results, tables

Expand Down
3 changes: 2 additions & 1 deletion fluffy/eth_data/history_data_seeding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import
eth/common/eth_types,
eth/rlp,
../network/wire/portal_protocol,
../network/history/[history_content, history_network, accumulator],
../network/history/
[history_content, history_network, validation/historical_hashes_accumulator],
"."/[era1, history_data_json_store, history_data_ssz_e2s]

export results
Expand Down
2 changes: 1 addition & 1 deletion fluffy/eth_data/history_data_ssz_e2s.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import
results,
eth/[rlp, common/eth_types],
ncli/e2store,
../network/history/[history_content, accumulator]
../network/history/[history_content, validation/historical_hashes_accumulator]

export results

Expand Down
2 changes: 1 addition & 1 deletion fluffy/network/history/content/content_deprecated.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import
ssz_serialization,
../../../common/common_types,
../../../database/content_db,
../accumulator
../validation/historical_hashes_accumulator

type
ContentType = enum
Expand Down
8 changes: 6 additions & 2 deletions fluffy/network/history/history_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import
../../database/content_db,
../../network_metadata,
../wire/[portal_protocol, portal_stream, portal_protocol_config],
"."/[history_content, accumulator, beacon_chain_historical_roots],
"."/[
history_content,
beacon_chain_historical_roots,
validation/historical_hashes_accumulator,
],
./content/content_deprecated

logScope:
topics = "portal_hist"

export accumulator
export historical_hashes_accumulator

type
HistoryNetwork* = ref object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import results, beacon_chain/spec/presets, beacon_chain/spec/forks

type BeaconBlockProof* = array[11, Digest]
type ExecutionBlockProof* = array[11, Digest]

func getBlockRootsIndex*(slot: Slot): uint64 =
slot mod SLOTS_PER_HISTORICAL_ROOT
Expand All @@ -19,7 +19,9 @@ func getBlockRootsIndex*(beaconBlock: SomeForkyBeaconBlock): uint64 =

# Builds proof to be able to verify that the EL block hash is part of the
# CL BeaconBlock for given root.
func buildProof*(blockBody: SomeForkyBeaconBlock): Result[BeaconBlockProof, string] =
func buildProof*(
beaconBlock: SomeForkyBeaconBlock
): Result[ExecutionBlockProof, string] =
let
# BeaconBlock level:
# - 8 as there are 5 fields
Expand All @@ -34,12 +36,14 @@ func buildProof*(blockBody: SomeForkyBeaconBlock): Result[BeaconBlockProof, stri
# - 12 as pos of field is 12
gIndex = GeneralizedIndex(gIndexMidLevel * 1 * 16 + 12)

var proof: BeaconBlockProof
?blockBody.build_proof(gIndex, proof)
var proof: ExecutionBlockProof
?beaconBlock.build_proof(gIndex, proof)

ok(proof)

func verifyProof*(blockHash: Digest, proof: BeaconBlockProof, blockRoot: Digest): bool =
func verifyProof*(
blockHash: Digest, proof: ExecutionBlockProof, blockRoot: Digest
): bool =
let
gIndexTopLevel = (1 * 1 * 8 + 4)
gIndexMidLevel = (gIndexTopLevel * 1 * 16 + 9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ import
ssz_serialization/[proofs, merkleization],
beacon_chain/spec/eth2_ssz_serialization,
beacon_chain/spec/datatypes/bellatrix,
./beacon_chain_block_proof_common
./block_proof_common

export beacon_chain_block_proof_common
export block_proof_common

type
HistoricalRootsProof* = array[14, Digest]
BeaconBlockProofHistoricalRoots* = array[14, Digest]

BeaconChainBlockProof* = object
BlockProofHistoricalRoots* = object
# Total size (11 + 1 + 14) * 32 bytes + 4 bytes = 836 bytes
beaconBlockProof*: BeaconBlockProof
beaconBlockProof*: BeaconBlockProofHistoricalRoots
beaconBlockRoot*: Digest
historicalRootsProof*: HistoricalRootsProof
executionBlockProof*: ExecutionBlockProof
slot*: Slot

func getHistoricalRootsIndex*(slot: Slot): uint64 =
Expand All @@ -111,36 +111,36 @@ template `[]`(x: openArray[Eth2Digest], chunk: Limit): Eth2Digest =
# HistoricalBatch for given root.
func buildProof*(
batch: HistoricalBatch, blockRootIndex: uint64
): Result[HistoricalRootsProof, string] =
): Result[BeaconBlockProofHistoricalRoots, string] =
# max list size * 2 is start point of leaves
let gIndex = GeneralizedIndex(2 * SLOTS_PER_HISTORICAL_ROOT + blockRootIndex)

var proof: HistoricalRootsProof
var proof: BeaconBlockProofHistoricalRoots
?batch.build_proof(gIndex, proof)

ok(proof)

func buildProof*(
batch: HistoricalBatch,
beaconBlock: bellatrix.TrustedBeaconBlock | bellatrix.BeaconBlock,
): Result[BeaconChainBlockProof, string] =
): Result[BlockProofHistoricalRoots, string] =
let
blockRootIndex = getBlockRootsIndex(beaconBlock)
beaconBlockProof = ?beaconBlock.buildProof()
historicalRootsProof = ?batch.buildProof(blockRootIndex)
executionBlockProof = ?beaconBlock.buildProof()
beaconBlockProof = ?batch.buildProof(blockRootIndex)

ok(
BeaconChainBlockProof(
BlockProofHistoricalRoots(
beaconBlockProof: beaconBlockProof,
beaconBlockRoot: hash_tree_root(beaconBlock),
historicalRootsProof: historicalRootsProof,
executionBlockProof: executionBlockProof,
slot: beaconBlock.slot,
)
)

func verifyProof*(
blockHeaderRoot: Digest,
proof: HistoricalRootsProof,
proof: BeaconBlockProofHistoricalRoots,
historicalRoot: Digest,
blockRootIndex: uint64,
): bool =
Expand All @@ -150,14 +150,14 @@ func verifyProof*(

func verifyProof*(
historical_roots: HashList[Eth2Digest, Limit HISTORICAL_ROOTS_LIMIT],
proof: BeaconChainBlockProof,
proof: BlockProofHistoricalRoots,
blockHash: Digest,
): bool =
let
historicalRootsIndex = getHistoricalRootsIndex(proof.slot)
blockRootIndex = getBlockRootsIndex(proof.slot)

blockHash.verifyProof(proof.beaconBlockProof, proof.beaconBlockRoot) and
blockHash.verifyProof(proof.executionBlockProof, proof.beaconBlockRoot) and
proof.beaconBlockRoot.verifyProof(
proof.historicalRootsProof, historical_roots[historicalRootsIndex], blockRootIndex
proof.beaconBlockProof, historical_roots[historicalRootsIndex], blockRootIndex
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ import
beacon_chain/spec/eth2_ssz_serialization,
beacon_chain/spec/presets,
beacon_chain/spec/datatypes/capella,
../beacon_chain_block_proof_common
./block_proof_common

export beacon_chain_block_proof_common
export block_proof_common

type
HistoricalSummariesProof* = array[13, Digest]
BeaconBlockProofHistoricalRoots* = array[13, Digest]

BeaconChainBlockProof* = object
BlockProofHistoricalSummaries* = object
# Total size (11 + 1 + 13) * 32 bytes + 4 bytes = 804 bytes
beaconBlockProof*: BeaconBlockProof
beaconBlockProof*: BeaconBlockProofHistoricalRoots
beaconBlockRoot*: Digest
historicalSummariesProof*: HistoricalSummariesProof
executionBlockProof*: ExecutionBlockProof
slot*: Slot

template `[]`(x: openArray[Eth2Digest], chunk: Limit): Eth2Digest =
Expand All @@ -64,11 +64,11 @@ func getHistoricalSummariesIndex*(
# block_roots for given root.
func buildProof*(
blockRoots: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest], blockRootIndex: uint64
): Result[HistoricalSummariesProof, string] =
): Result[BeaconBlockProofHistoricalRoots, string] =
# max list size * 1 is start point of leaves
let gIndex = GeneralizedIndex(SLOTS_PER_HISTORICAL_ROOT + blockRootIndex)

var proof: HistoricalSummariesProof
var proof: BeaconBlockProofHistoricalRoots
?blockRoots.build_proof(gIndex, proof)

ok(proof)
Expand All @@ -78,24 +78,24 @@ func buildProof*(
func buildProof*(
blockRoots: array[SLOTS_PER_HISTORICAL_ROOT, Eth2Digest],
beaconBlock: capella.TrustedBeaconBlock | capella.BeaconBlock,
): Result[BeaconChainBlockProof, string] =
): Result[BlockProofHistoricalSummaries, string] =
let
blockRootIndex = getBlockRootsIndex(beaconBlock)
beaconBlockProof = ?beaconBlock.buildProof()
historicalSummariesProof = ?blockRoots.buildProof(blockRootIndex)
executionBlockProof = ?beaconBlock.buildProof()
beaconBlockProof = ?blockRoots.buildProof(blockRootIndex)

ok(
BeaconChainBlockProof(
beaconBlockProof: beaconBlockProof,
BlockProofHistoricalSummaries(
beaconBlockRoot: hash_tree_root(beaconBlock),
historicalSummariesProof: historicalSummariesProof,
beaconBlockProof: beaconBlockProof,
executionBlockProof: executionBlockProof,
slot: beaconBlock.slot,
)
)

func verifyProof*(
blockHeaderRoot: Digest,
proof: HistoricalSummariesProof,
proof: BeaconBlockProofHistoricalRoots,
historicalRoot: Digest,
blockRootIndex: uint64,
): bool =
Expand All @@ -105,17 +105,17 @@ func verifyProof*(

func verifyProof*(
historical_summaries: HashList[HistoricalSummary, Limit HISTORICAL_ROOTS_LIMIT],
proof: BeaconChainBlockProof,
proof: BlockProofHistoricalSummaries,
blockHash: Digest,
cfg: RuntimeConfig,
): bool =
let
historicalRootsIndex = getHistoricalSummariesIndex(proof.slot, cfg)
blockRootIndex = getBlockRootsIndex(proof.slot)

blockHash.verifyProof(proof.beaconBlockProof, proof.beaconBlockRoot) and
blockHash.verifyProof(proof.executionBlockProof, proof.beaconBlockRoot) and
proof.beaconBlockRoot.verifyProof(
proof.historicalSummariesProof,
proof.beaconBlockProof,
historical_summaries[historicalRootsIndex].block_summary_root,
blockRootIndex,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import
eth/common/eth_types_rlp,
ssz_serialization,
ssz_serialization/[proofs, merkleization],
../../common/common_types,
./history_content
../../../common/common_types,
../history_content

export ssz_serialization, merkleization, proofs, eth_types_rlp

Expand Down
2 changes: 1 addition & 1 deletion fluffy/network_metadata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import
stew/io2,
chronos/timer,
beacon_chain/spec/forks,
./network/history/accumulator
./network/history/validation/historical_hashes_accumulator

proc loadBootstrapNodes(path: string): seq[string] {.raises: [IOError].} =
# Read a list of ENR URIs from a file containing a flat list of entries.
Expand Down
2 changes: 1 addition & 1 deletion fluffy/scripts/test_portal_testnet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import
../rpc/portal_rpc_client,
../rpc/eth_rpc_client,
../eth_data/[history_data_seeding, history_data_json_store, history_data_ssz_e2s],
../network/history/[history_content, accumulator],
../network/history/[history_content, validation/historical_hashes_accumulator],
../tests/test_history_util

type
Expand Down
3 changes: 1 addition & 2 deletions fluffy/tests/all_fluffy_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import
./test_history_network,
./test_content_db,
./test_discovery_rpc,
./test_beacon_chain_block_proof_bellatrix,
./test_beacon_chain_block_proof_capella,
./test_beacon_chain_historical_roots,
./test_beacon_chain_historical_summaries,
./history_network_tests/all_history_network_tests,
./beacon_network_tests/all_beacon_network_tests,
./state_network_tests/all_state_network_tests
14 changes: 14 additions & 0 deletions fluffy/tests/history_network_tests/all_history_network_tests.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Nimbus
# Copyright (c) 2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.

{.warning[UnusedImport]: off.}

import
./test_block_proof_historical_roots,
./test_block_proof_historical_roots_vectors,
./test_block_proof_historical_summaries,
./test_block_proof_historical_summaries_vectors
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import
beacon_chain /../ tests/testblockutil,
# Mock helpers
beacon_chain /../ tests/mocking/mock_genesis,
../network/history/beacon_chain_block_proof_bellatrix
../../network/history/validation/block_proof_historical_roots

# Test suite for the proofs:
# - HistoricalRootsProof
Expand Down Expand Up @@ -67,7 +67,7 @@ suite "Beacon Chain Block Proofs - Bellatrix":
SLOTS_PER_HISTORICAL_ROOT - 2,
]

test "HistoricalRootsProof for BeaconBlock":
test "BeaconBlockProofHistoricalRoots for BeaconBlock":
let
# Historical batch of first historical root
batch = HistoricalBatch(
Expand All @@ -91,7 +91,7 @@ suite "Beacon Chain Block Proofs - Bellatrix":
blocks[i].root, proof, historical_roots[historicalRootsIndex], blockRootIndex
)

test "BeaconBlockProof for BeaconBlock":
test "ExecutionBlockProof for Execution BlockHeader":
# for i in 0..<(SLOTS_PER_HISTORICAL_ROOT - 1): # Test all blocks
for i in blocksToTest:
let beaconBlock = blocks[i].message
Expand All @@ -103,7 +103,7 @@ suite "Beacon Chain Block Proofs - Bellatrix":
let leave = beaconBlock.body.execution_payload.block_hash
check verifyProof(leave, proof, blocks[i].root)

test "BeaconChainBlockProof for Execution BlockHeader":
test "BlockProofHistoricalRoots for Execution BlockHeader":
let
# Historical batch of first historical root
batch = HistoricalBatch(
Expand Down
Loading

0 comments on commit 0fb9581

Please sign in to comment.