Skip to content

Commit

Permalink
Regenesis should also store da block height (#1747)
Browse files Browse the repository at this point in the history
Closes #1667.

It seems that state config already is a good place to store
`block_height` and `da_block_height`.

---------

Co-authored-by: Ahmed Sagdati <37515857+segfault-magnet@users.noreply.github.com>
  • Loading branch information
xgreenx and segfault-magnet committed Mar 11, 2024
1 parent b5be029 commit 3bffdd1
Show file tree
Hide file tree
Showing 27 changed files with 268 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- [#1747](https://github.com/FuelLabs/fuel-core/pull/1747): The DA block height is now included in the genesis state.
- [#1740](https://github.com/FuelLabs/fuel-core/pull/1740): Remove optional fields from genesis configs
- [#1737](https://github.com/FuelLabs/fuel-core/pull/1737): Remove temporary tables for calculating roots during genesis.
- [#1731](https://github.com/FuelLabs/fuel-core/pull/1731): Expose `schema.sdl` from `fuel-core-client`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147686,5 +147686,6 @@
"amount": 1126914900
}
],
"block_height": 0
"block_height": 0,
"da_block_height": 0
}
1 change: 1 addition & 0 deletions bin/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tracing-subscriber = { workspace = true, features = [
url = { version = "2.2", optional = true }

[dev-dependencies]
fuel-core-types = { workspace = true, features = ["test-helpers"] }
rand = { workspace = true }
tempfile = { workspace = true }
test-case = { workspace = true }
Expand Down
57 changes: 25 additions & 32 deletions bin/fuel-core/src/cli/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ fn contract_snapshot(
std::fs::create_dir_all(output_dir)?;

let (contract, state, balance) = db.get_contract_by_id(contract_id)?;
let block_height = db.get_block_height()?;
let block = db.get_last_block()?;

let metadata = write_metadata(output_dir, Encoding::Json)?;
let mut writer = StateWriter::for_snapshot(&metadata)?;

writer.write_contracts(vec![contract])?;
writer.write_contract_state(state)?;
writer.write_contract_balance(balance)?;
writer.write_block_height(block_height)?;
writer.write_block_data(*block.header().height(), block.header().da_height)?;
writer.close()?;
Ok(())
}
Expand Down Expand Up @@ -221,7 +221,8 @@ fn write_chain_state(
writer.write_contract_balance(chunk)
})?;

writer.write_block_height(db.get_block_height()?)?;
let block = db.get_last_block()?;
writer.write_block_data(*block.header().height(), block.header().da_height)?;

writer.close()?;

Expand Down Expand Up @@ -250,14 +251,6 @@ mod tests {

use std::iter::repeat_with;

use fuel_core::database::{
database_description::{
on_chain::OnChain,
DatabaseDescription,
DatabaseMetadata,
},
metadata::MetadataTable,
};
use fuel_core_chain_config::{
CoinConfig,
ContractBalanceConfig,
Expand All @@ -274,14 +267,18 @@ mod tests {
ContractsLatestUtxo,
ContractsRawCode,
ContractsState,
FuelBlocks,
Messages,
},
ContractsAssetKey,
ContractsStateKey,
StorageAsMut,
};
use fuel_core_types::{
blockchain::primitives::DaBlockHeight,
blockchain::{
block::CompressedBlock,
primitives::DaBlockHeight,
},
entities::{
coins::coin::{
CompressedCoin,
Expand All @@ -300,7 +297,6 @@ mod tests {
TxPointer,
UtxoId,
},
fuel_types::BlockHeight,
fuel_vm::Salt,
};
use rand::{
Expand Down Expand Up @@ -357,32 +353,27 @@ mod tests {
.collect()
};

let block_height = self.given_block_height();
let block = self.given_block();

StateConfig {
coins,
messages,
contracts,
contract_state,
contract_balance,
block_height,
block_height: *block.header().height(),
da_block_height: block.header().da_height,
}
}

fn given_block_height(&mut self) -> BlockHeight {
let height = BlockHeight::from(10);
self.db
.storage::<MetadataTable<OnChain>>()
.insert(
&(),
&DatabaseMetadata::V1 {
version: OnChain::version(),
height,
},
)
.unwrap();
fn given_block(&mut self) -> CompressedBlock {
let mut block = CompressedBlock::default();
let height = 10u32.into();
block.header_mut().application_mut().da_height = 14u64.into();
block.header_mut().set_block_height(height);
let _ = self.db.storage::<FuelBlocks>().insert(&height, &block);

height
block
}

fn given_coin(&mut self) -> CoinConfig {
Expand Down Expand Up @@ -528,7 +519,7 @@ mod tests {
let db_path = temp_dir.path().join("db");
let mut db = DbPopulator::new(open_db(&db_path)?, StdRng::seed_from_u64(2));

let height = db.given_block_height();
let block = db.given_block();
let state = db.given_persisted_state(10, 10, 10, 10, 10);
drop(db);

Expand All @@ -547,7 +538,8 @@ mod tests {

let snapshot = StateConfig::from_snapshot_metadata(snapshot)?;

assert_eq!(snapshot.block_height, height);
assert_eq!(snapshot.block_height, *block.header().height());
assert_eq!(snapshot.da_block_height, block.header().da_height);

assert_ne!(snapshot, state);
assert_eq!(snapshot, sorted_state(state));
Expand All @@ -571,7 +563,7 @@ mod tests {
let db_path = temp_dir.path().join("db");
let mut db = DbPopulator::new(open_db(&db_path)?, StdRng::seed_from_u64(2));

db.given_block_height();
db.given_block();
let state = db.given_persisted_state(10, 10, 10, 10, 10);
drop(db);

Expand Down Expand Up @@ -665,7 +657,8 @@ mod tests {
contract_state: expected_contract_state,
contract_balance: expected_contract_balance,
contracts: vec![random_contract],
block_height: state.block_height
block_height: state.block_height,
da_block_height: state.da_block_height,
}
);

Expand Down
9 changes: 9 additions & 0 deletions crates/chain-config/src/config/snapshot_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct ParquetFiles {
pub contract_state: PathBuf,
pub contract_balance: PathBuf,
pub block_height: PathBuf,
pub da_block_height: PathBuf,
}

#[cfg(feature = "parquet")]
Expand All @@ -30,6 +31,7 @@ impl ParquetFiles {
prepend(&mut self.contract_state);
prepend(&mut self.contract_balance);
prepend(&mut self.block_height);
prepend(&mut self.da_block_height);
}
}

Expand All @@ -45,6 +47,7 @@ impl ParquetFiles {
contract_state: parquet_file("contract_state"),
contract_balance: parquet_file("contract_balance"),
block_height: parquet_file("block_height"),
da_block_height: parquet_file("da_block_height"),
}
}
}
Expand Down Expand Up @@ -317,6 +320,7 @@ mod tests {
.path()
.join("contract_balance.parquet"),
block_height: temp_dir.path().join("block_height.parquet"),
da_block_height: temp_dir.path().join("da_block_height.parquet"),
},
compression: crate::ZstdCompressionLevel::Max,
group_size: 10,
Expand Down Expand Up @@ -350,6 +354,7 @@ mod tests {
contract_state: PathBuf::from("./contract_state.parquet"),
contract_balance: PathBuf::from("./contract_balance.parquet"),
block_height: PathBuf::from("./block_height.parquet"),
da_block_height: PathBuf::from("./da_block_height.parquet"),
},
compression: crate::ZstdCompressionLevel::Max,
group_size: 10,
Expand Down Expand Up @@ -388,6 +393,7 @@ mod tests {
.path()
.join("contract_balance.parquet"),
block_height: temp_dir.path().join("block_height.parquet"),
da_block_height: temp_dir.path().join("da_block_height.parquet"),
},
compression: crate::ZstdCompressionLevel::Max,
group_size: 10,
Expand All @@ -413,6 +419,7 @@ mod tests {
contract_state: "../other/contract_state.parquet".into(),
contract_balance: "../other/contract_balance.parquet".into(),
block_height: "../other/block_height.parquet".into(),
da_block_height: "../other/da_block_height.parquet".into(),
},
compression: crate::ZstdCompressionLevel::Max,
group_size: 10,
Expand Down Expand Up @@ -440,6 +447,8 @@ mod tests {
contract_balance: snapshot_path
.join("../other/contract_balance.parquet"),
block_height: snapshot_path.join("../other/block_height.parquet"),
da_block_height: snapshot_path
.join("../other/da_block_height.parquet"),
},
compression: crate::ZstdCompressionLevel::Max,
group_size: 10,
Expand Down
Loading

0 comments on commit 3bffdd1

Please sign in to comment.