Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Preserve message proofs post-regenesis #1891

Merged
merged 36 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7245764
Populate prev root from last block before regenesis
bvrooman May 7, 2024
d6fa53c
Update state.rs
bvrooman May 8, 2024
4ce3175
Add `OldFuelBlockMerkleData` and `OldFuelBlockMerkleMetadata`
bvrooman May 8, 2024
7e13d03
Retrieve merkle metadata from archived data
bvrooman May 8, 2024
80a06bd
Merge branch 'master' into bvrooman/feat/preserve-messages-post-regen…
May 8, 2024
9d38271
Update CHANGELOG.md
bvrooman May 8, 2024
aaa061c
Revert "Populate prev root from last block before regenesis"
bvrooman May 9, 2024
e387da6
Update state.rs
bvrooman May 9, 2024
689c267
Update worker_service.rs
bvrooman May 9, 2024
161aa7c
Fix OldFuelBlockMerkleMetadata basic storage test
bvrooman May 9, 2024
8ba540a
Fix merkle data and metadata export
bvrooman May 9, 2024
7d42421
Refactor DatabaseMessageProof impls
bvrooman May 10, 2024
e5415c7
Revet macro export
bvrooman May 10, 2024
210c86b
Fix
bvrooman May 10, 2024
301e32b
clippy
bvrooman May 10, 2024
0a6068b
Test message proofs are preserverd
bvrooman May 10, 2024
932fa6b
clipheehee
bvrooman May 10, 2024
5888063
Naming
bvrooman May 10, 2024
c49e299
Merge branch 'master' into bvrooman/feat/preserve-messages-post-regen…
bvrooman May 13, 2024
9cdebb6
Update test
bvrooman May 13, 2024
16e6d0d
Update test
bvrooman May 13, 2024
7fad460
Fix checks
bvrooman May 13, 2024
726aad8
block hash
bvrooman May 13, 2024
de5e03c
Clippy
bvrooman May 13, 2024
3f9f73c
Update state.rs
bvrooman May 13, 2024
470abde
Update snapshot
bvrooman May 13, 2024
f89fd50
SOmething
xgreenx May 13, 2024
6174eef
Update test
bvrooman May 14, 2024
7482083
Migrate FuelBlockMerkleData/Metadata to on-chain
bvrooman May 14, 2024
d66600f
Fixed regenesis
xgreenx May 15, 2024
a503f29
Minimized number of changes
xgreenx May 15, 2024
dd79379
More blocks between regenesis
xgreenx May 15, 2024
2dffd31
Make compilation happy
xgreenx May 15, 2024
8b8eece
Fixed test
xgreenx May 15, 2024
bc25f4d
Fixed test
xgreenx May 15, 2024
0bef672
Fixed test
xgreenx May 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

- [#1891](https://github.com/FuelLabs/fuel-core/pull/1891): Regenesis now preserves `FuelBlockMerkleData` and `FuelBlockMerkleMetadata` in the off-chain table. These tables are checked when querying message proofs.
- [#1886](https://github.com/FuelLabs/fuel-core/pull/1886): Use ref to `Block` in validation code
- [#1876](https://github.com/FuelLabs/fuel-core/pull/1876): Updated benchmark to include the worst scenario for `CROO` opcode. Also include consensus parameters in bench output.

### Changed

- [#1879](https://github.com/FuelLabs/fuel-core/pull/1879): Return the old behaviour for the `discovery_works` test.
- [#1848](https://github.com/FuelLabs/fuel-core/pull/1848): Added `version` field to the `Block` and `BlockHeader` GraphQL entities. Added corresponding `version` field to the `Block` and `BlockHeader` client types in `fuel-core-client`.

### Changed

- [#1873](https://github.com/FuelLabs/fuel-core/pull/1873/): Separate dry runs from block production in executor code, remove `ExecutionKind` and `ExecutionType`, remove `thread_block_transaction` concept, remove `PartialBlockComponent` type, refactor away `inner` functions.

## [Version 0.26.0]
Expand Down
6 changes: 5 additions & 1 deletion bin/fuel-core/src/cli/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ mod tests {
use fuel_core_chain_config::{
AddTable,
AsTable,
LastBlockConfig,
SnapshotMetadata,
SnapshotReader,
StateConfig,
Expand Down Expand Up @@ -316,7 +317,10 @@ mod tests {
builder.add(self.common.contract_balance);

builder
.build(Some(self.common.block.value.header().into()))
.build(Some(LastBlockConfig::from_header(
self.common.block.value.header(),
Default::default(),
)))
.unwrap()
}

Expand Down
46 changes: 36 additions & 10 deletions crates/chain-config/src/config/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use fuel_core_storage::{
use fuel_core_types::{
blockchain::primitives::DaBlockHeight,
entities::contract::ContractUtxoInfo,
fuel_types::BlockHeight,
fuel_types::{
BlockHeight,
Bytes32,
},
};
use itertools::Itertools;
use serde::{
Expand All @@ -49,6 +52,10 @@ use bech32::{
};
#[cfg(feature = "test-helpers")]
use core::str::FromStr;
use fuel_core_storage::tables::merkle::{
FuelBlockMerkleData,
FuelBlockMerkleMetadata,
};
use fuel_core_types::blockchain::header::{
BlockHeader,
ConsensusParametersVersion,
Expand All @@ -57,7 +64,6 @@ use fuel_core_types::blockchain::header::{
#[cfg(feature = "test-helpers")]
use fuel_core_types::{
fuel_types::Address,
fuel_types::Bytes32,
fuel_vm::SecretKey,
};

Expand Down Expand Up @@ -89,16 +95,12 @@ pub struct LastBlockConfig {
pub consensus_parameters_version: ConsensusParametersVersion,
/// The version of state transition function used to produce last block.
pub state_transition_version: StateTransitionBytecodeVersion,
/// The Merkle root of all blocks before regenesis.
pub blocks_root: Bytes32,
}

impl From<BlockHeader> for LastBlockConfig {
fn from(header: BlockHeader) -> Self {
Self::from(&header)
}
}

impl From<&BlockHeader> for LastBlockConfig {
fn from(header: &BlockHeader) -> Self {
impl LastBlockConfig {
pub fn from_header(header: &BlockHeader, blocks_root: Bytes32) -> Self {
Self {
block_height: *header.height(),
da_block_height: header.application().da_height,
Expand All @@ -108,6 +110,7 @@ impl From<&BlockHeader> for LastBlockConfig {
state_transition_version: header
.application()
.state_transition_bytecode_version,
blocks_root,
}
}
}
Expand Down Expand Up @@ -288,6 +291,7 @@ impl crate::Randomize for StateConfig {
da_block_height: rng.gen(),
consensus_parameters_version: rng.gen(),
state_transition_version: rng.gen(),
blocks_root: rng.gen(),
}),
}
}
Expand Down Expand Up @@ -434,6 +438,26 @@ impl AddTable<SealedBlockConsensus> for StateConfigBuilder {
fn add(&mut self, _entries: Vec<TableEntry<SealedBlockConsensus>>) {}
}

impl AsTable<FuelBlockMerkleData> for StateConfig {
fn as_table(&self) -> Vec<TableEntry<FuelBlockMerkleData>> {
Vec::new() // Do not include these for now
}
}

impl AddTable<FuelBlockMerkleData> for StateConfigBuilder {
fn add(&mut self, _entries: Vec<TableEntry<FuelBlockMerkleData>>) {}
}

impl AsTable<FuelBlockMerkleMetadata> for StateConfig {
fn as_table(&self) -> Vec<TableEntry<FuelBlockMerkleMetadata>> {
Vec::new() // Do not include these for now
}
}

impl AddTable<FuelBlockMerkleMetadata> for StateConfigBuilder {
fn add(&mut self, _entries: Vec<TableEntry<FuelBlockMerkleMetadata>>) {}
}

impl AddTable<ProcessedTransactions> for StateConfigBuilder {
fn add(&mut self, _: Vec<TableEntry<ProcessedTransactions>>) {}
}
Expand Down Expand Up @@ -759,11 +783,13 @@ mod tests {
let da_block_height = 14u64.into();
let consensus_parameters_version = 321u32;
let state_transition_version = 123u32;
let blocks_root = Bytes32::from([123; 32]);
let block_config = LastBlockConfig {
block_height,
da_block_height,
consensus_parameters_version,
state_transition_version,
blocks_root,
};
let writer = writer(temp_dir.path());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ expression: encoded_json
"block_height": 2346951900,
"da_block_height": 14074420184374977699,
"consensus_parameters_version": 2019076799,
"state_transition_version": 1670454246
"state_transition_version": 1670454246,
"blocks_root": "dfa3ac8a57ea08860870fd515cf7ca6afb887dd1e09be8e451cd3c364076623c"
}
}
10 changes: 9 additions & 1 deletion crates/fuel-core/src/combined_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ use crate::{
},
service::DbType,
};
use fuel_core_chain_config::LastBlockConfig;
#[cfg(feature = "test-helpers")]
use fuel_core_chain_config::{
StateConfig,
StateConfigBuilder,
};
use fuel_core_producer::ports::BlockProducerDatabase;
#[cfg(feature = "test-helpers")]
use fuel_core_storage::tables::{
Coins,
Expand Down Expand Up @@ -180,7 +182,13 @@ impl CombinedDatabase {
);

let latest_block = self.on_chain().latest_block()?;
let state_config = builder.build(Some(latest_block.header().into()))?;
let blocks_root = self
.on_chain()
.block_header_merkle_root(latest_block.header().height())?;
let state_config = builder.build(Some(LastBlockConfig::from_header(
latest_block.header(),
blocks_root,
)))?;

Ok(state_config)
}
Expand Down
10 changes: 6 additions & 4 deletions crates/fuel-core/src/service/adapters/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::{
StaticGasPrice,
};
use crate::{
database::Database,
fuel_core_graphql_api::ports::{
worker,
BlockProducerPort,
Expand All @@ -15,9 +14,12 @@ use crate::{
P2pPort,
TxPoolPort,
},
service::adapters::{
P2PAdapter,
TxPoolAdapter,
service::{
adapters::{
P2PAdapter,
TxPoolAdapter,
},
Database,
},
};
use async_trait::async_trait;
Expand Down
8 changes: 5 additions & 3 deletions crates/fuel-core/src/service/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub fn create_genesis_block(config: &Config) -> Block {
let da_height;
let consensus_parameters_version;
let state_transition_bytecode_version;
let prev_root;

// If the rollup continues the old rollup, the height of the new block should
// be higher than that of the old chain by one to make it continuous.
Expand All @@ -196,8 +197,8 @@ pub fn create_genesis_block(config: &Config) -> Block {
.state_transition_version
.checked_add(1)
.expect("State transition bytecode version overflow");

da_height = latest_block.da_block_height;
prev_root = latest_block.blocks_root;
} else {
height = 0u32.into();
#[cfg(feature = "relayer")]
Expand All @@ -214,6 +215,7 @@ pub fn create_genesis_block(config: &Config) -> Block {
}
consensus_parameters_version = ConsensusParametersVersion::MIN;
state_transition_bytecode_version = StateTransitionBytecodeVersion::MIN;
prev_root = Bytes32::zeroed();
}

let transactions_ids = vec![];
Expand All @@ -228,7 +230,7 @@ pub fn create_genesis_block(config: &Config) -> Block {
generated: Empty,
},
consensus: ConsensusHeader::<Empty> {
prev_root: Bytes32::zeroed(),
prev_root,
height,
time: fuel_core_types::tai64::Tai64::UNIX_EPOCH,
generated: Empty,
Expand Down Expand Up @@ -291,7 +293,7 @@ mod tests {
use std::vec;

#[tokio::test]
async fn config_initializes_block_height_of_genesic_block() {
async fn config_initializes_block_height_of_genesis_block() {
let block_height = BlockHeight::from(99u32);
let service_config = Config::local_node_with_state_config(StateConfig {
last_block: Some(LastBlockConfig {
Expand Down
16 changes: 15 additions & 1 deletion crates/fuel-core/src/service/genesis/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,24 @@ use crate::{
use fuel_core_chain_config::{
AddTable,
ChainConfig,
LastBlockConfig,
SnapshotFragment,
SnapshotMetadata,
SnapshotWriter,
StateConfigBuilder,
TableEntry,
};
use fuel_core_poa::ports::Database as DatabaseTrait;
use fuel_core_storage::{
blueprint::BlueprintInspect,
iter::IterDirection,
kv_store::StorageColumn,
structured_storage::TableWithBlueprint,
tables::{
merkle::{
FuelBlockMerkleData,
FuelBlockMerkleMetadata,
},
Coins,
ContractsAssets,
ContractsLatestUtxo,
Expand Down Expand Up @@ -101,6 +107,8 @@ where
ContractsState,
ContractsAssets,
FuelBlocks,
FuelBlockMerkleData,
FuelBlockMerkleMetadata,
Transactions,
SealedBlockConsensus,
ProcessedTransactions
Expand Down Expand Up @@ -145,6 +153,12 @@ where
async fn finalize(self) -> anyhow::Result<SnapshotMetadata> {
let writer = self.create_writer()?;
let latest_block = self.db.on_chain().latest_block()?;
let blocks_root = self
.db
.on_chain()
.block_header_merkle_root(latest_block.header().height())?;
let latest_block =
LastBlockConfig::from_header(latest_block.header(), blocks_root);

let writer_fragment = writer.partial_close()?;
self.task_manager
Expand All @@ -154,7 +168,7 @@ where
.try_fold(writer_fragment, |fragment, next_fragment| {
fragment.merge(next_fragment)
})?
.finalize(Some(latest_block.header().into()), &self.prev_chain_config)
.finalize(Some(latest_block), &self.prev_chain_config)
}

fn create_writer(&self) -> anyhow::Result<SnapshotWriter> {
Expand Down
6 changes: 6 additions & 0 deletions crates/fuel-core/src/service/genesis/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ use fuel_core_storage::{
kv_store::StorageColumn,
structured_storage::TableWithBlueprint,
tables::{
merkle::{
FuelBlockMerkleData,
FuelBlockMerkleMetadata,
},
Coins,
ContractsAssets,
ContractsLatestUtxo,
Expand Down Expand Up @@ -113,6 +117,8 @@ impl SnapshotImporter {
self.spawn_worker_on_chain::<ContractsState>()?;
self.spawn_worker_on_chain::<ContractsAssets>()?;
self.spawn_worker_on_chain::<ProcessedTransactions>()?;
self.spawn_worker_on_chain::<FuelBlockMerkleData>()?;
self.spawn_worker_on_chain::<FuelBlockMerkleMetadata>()?;

self.spawn_worker_off_chain::<TransactionStatuses, TransactionStatuses>()?;
self.spawn_worker_off_chain::<OwnedTransactions, OwnedTransactions>()?;
Expand Down
45 changes: 45 additions & 0 deletions crates/fuel-core/src/service/genesis/importer/on_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use anyhow::anyhow;
use fuel_core_chain_config::TableEntry;
use fuel_core_storage::{
tables::{
merkle::{
FuelBlockMerkleData,
FuelBlockMerkleMetadata,
},
Coins,
ContractsAssets,
ContractsLatestUtxo,
Expand Down Expand Up @@ -149,6 +153,47 @@ impl ImportTable for Handler<ContractsAssets, ContractsAssets> {
}
}

impl ImportTable for Handler<FuelBlockMerkleData, FuelBlockMerkleData> {
type TableInSnapshot = FuelBlockMerkleData;
type TableBeingWritten = FuelBlockMerkleData;
type DbDesc = OnChain;

fn process(
&mut self,
group: Vec<TableEntry<Self::TableInSnapshot>>,
tx: &mut StorageTransaction<&mut GenesisDatabase<Self::DbDesc>>,
) -> anyhow::Result<()> {
let blocks = group
.iter()
.map(|TableEntry { key, value, .. }| (key, value));
for (height, block) in blocks {
tx.storage::<FuelBlockMerkleData>().insert(height, block)?;
}
Ok(())
}
}

impl ImportTable for Handler<FuelBlockMerkleMetadata, FuelBlockMerkleMetadata> {
type TableInSnapshot = FuelBlockMerkleMetadata;
type TableBeingWritten = FuelBlockMerkleMetadata;
type DbDesc = OnChain;

fn process(
&mut self,
group: Vec<TableEntry<Self::TableInSnapshot>>,
tx: &mut StorageTransaction<&mut GenesisDatabase<Self::DbDesc>>,
) -> anyhow::Result<()> {
let blocks = group
.iter()
.map(|TableEntry { key, value, .. }| (key, value));
for (height, metadata) in blocks {
tx.storage::<FuelBlockMerkleMetadata>()
.insert(height, metadata)?;
}
Ok(())
}
}

fn init_coin(
transaction: &mut StorageTransaction<&mut GenesisDatabase>,
coin: &TableEntry<Coins>,
Expand Down
Loading
Loading