Skip to content

Commit

Permalink
Pass predefined blocks via the CLI (#2094)
Browse files Browse the repository at this point in the history
Follow-up PR for #2081.

Closes #1902

Add a CLI argument to process predefined blocks.

## Checklist
- [x] New behavior is reflected in tests

### Before requesting review
- [x] I have reviewed the code myself

---------

Co-authored-by: Mitch Turner <[email protected]>
  • Loading branch information
xgreenx and MitchTurner authored Aug 18, 2024
1 parent 9de2c08 commit f999c83
Show file tree
Hide file tree
Showing 33 changed files with 364 additions and 145 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- [2094](https://github.com/FuelLabs/fuel-core/pull/2094): Added support for predefined blocks provided via the filesystem.
- [2094](https://github.com/FuelLabs/fuel-core/pull/2094): Added `--predefined-blocks-path` CLI argument to pass the path to the predefined blocks.
- [2081](https://github.com/FuelLabs/fuel-core/pull/2081): Enable producer to include predefined blocks.
- [2079](https://github.com/FuelLabs/fuel-core/pull/2079): Open unknown columns in the RocksDB for forward compatibility.

Expand All @@ -19,6 +21,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [2086](https://github.com/FuelLabs/fuel-core/pull/2086): Added support for PoA key rotation.
- [2086](https://github.com/FuelLabs/fuel-core/pull/2086): Support overriding of the non consensus parameters in the chan config.

### Fixed

- [2094](https://github.com/FuelLabs/fuel-core/pull/2094): Fixed bug in rollback logic because of wrong ordering of modifications.

## [Version 0.32.1]

### Added
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fuel-core-sync = { path = "./../crates/services/sync", features = [
"benchmarking",
] }
fuel-core-types = { path = "./../crates/types", features = ["test-helpers"] }
futures = "0.3"
futures = { workspace = true }
p256 = { version = "0.13", default-features = false, features = [
"digest",
"ecdsa",
Expand Down
2 changes: 1 addition & 1 deletion bin/e2e-test-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fuel-core = { workspace = true, default-features = false, optional = true }
fuel-core-chain-config = { workspace = true, features = ["default"] }
fuel-core-client = { workspace = true }
fuel-core-types = { workspace = true, features = ["test-helpers"] }
futures = "0.3"
futures = { workspace = true }
hex = "0.4"
humantime-serde = "1.1"
itertools = { workspace = true }
Expand Down
6 changes: 6 additions & 0 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ pub struct Command {
#[clap(flatten)]
pub poa_trigger: PoATriggerArgs,

/// The path to the directory containing JSON encoded predefined blocks.
#[arg(long = "predefined-blocks-path", env)]
pub predefined_blocks_path: Option<PathBuf>,

/// The block's fee recipient public key.
///
/// If not set, `consensus_key` is used as the provider of the `Address`.
Expand Down Expand Up @@ -262,6 +266,7 @@ impl Command {
gas_price_threshold_percent,
consensus_key,
poa_trigger,
predefined_blocks_path,
coinbase_recipient,
#[cfg(feature = "relayer")]
relayer_args,
Expand Down Expand Up @@ -406,6 +411,7 @@ impl Command {
continue_on_error,
utxo_validation,
block_production: trigger,
predefined_blocks_path,
vm: VMConfig {
backtrace: vm_backtrace,
},
Expand Down
32 changes: 12 additions & 20 deletions crates/fuel-core/src/combined_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,18 @@ impl CombinedDatabase {
while !shutdown_listener.is_cancelled() {
let on_chain_height = self
.on_chain()
.latest_height()?
.latest_height_from_metadata()?
.ok_or(anyhow::anyhow!("on-chain database doesn't have height"))?;

let off_chain_height = self
.off_chain()
.latest_height()?
.latest_height_from_metadata()?
.ok_or(anyhow::anyhow!("off-chain database doesn't have height"))?;

let gas_price_chain_height = self.off_chain().latest_height()?.ok_or(
anyhow::anyhow!("gas-price-chain database doesn't have height"),
)?;
let gas_price_chain_height =
self.gas_price().latest_height_from_metadata()?.ok_or(
anyhow::anyhow!("gas-price-chain database doesn't have height"),
)?;

if on_chain_height == target_block_height
&& off_chain_height == target_block_height
Expand All @@ -264,44 +265,35 @@ impl CombinedDatabase {

if on_chain_height < target_block_height {
return Err(anyhow::anyhow!(
"on-chain database height is less than target height"
"on-chain database height({on_chain_height}) \
is less than target height({target_block_height})"
));
}

if off_chain_height < target_block_height {
return Err(anyhow::anyhow!(
"off-chain database height is less than target height"
"off-chain database height({off_chain_height}) \
is less than target height({target_block_height})"
));
}

if gas_price_chain_height < target_block_height {
return Err(anyhow::anyhow!(
"gas-price-chain database height is less than target height"
"gas-price-chain database height({gas_price_chain_height}) \
is less than target height({target_block_height})"
));
}

if on_chain_height > target_block_height {
self.on_chain().rollback_last_block()?;
tracing::info!(
"Rolled back on-chain database to height {:?}",
on_chain_height.pred()
);
}

if off_chain_height > target_block_height {
self.off_chain().rollback_last_block()?;
tracing::info!(
"Rolled back off-chain database to height {:?}",
on_chain_height.pred()
);
}

if gas_price_chain_height > target_block_height {
self.gas_price().rollback_last_block()?;
tracing::info!(
"Rolled back gas-price-chain database to height {:?}",
gas_price_chain_height.pred()
);
}
}

Expand Down
40 changes: 22 additions & 18 deletions crates/fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ where
},
));
let height = database
.latest_height()
.latest_height_from_metadata()
.expect("Failed to get latest height during creation of the database");

database.stage.height = SharedMutex::new(height);
Expand Down Expand Up @@ -278,10 +278,14 @@ where
anyhow::anyhow!("Database doesn't have a height to rollback").into(),
);
};
self.inner_storage().data.rollback_last_block()?;
self.inner_storage().data.rollback_block_to(&height)?;
let new_height = height.rollback_height();
*lock = new_height;
tracing::info!("Rollback to the height {:?} was successful", new_height);
tracing::info!(
"Rollback of the {} to the height {:?} was successful",
Description::name(),
new_height
);

Ok(())
}
Expand Down Expand Up @@ -547,7 +551,7 @@ mod tests {
fn database_advances_with_a_new_block() {
// Given
let mut database = Database::<OnChain>::default();
assert_eq!(database.latest_height().unwrap(), None);
assert_eq!(database.latest_height(), None);

// When
let advanced_height = 1.into();
Expand All @@ -557,14 +561,14 @@ mod tests {
.unwrap();

// Then
assert_eq!(database.latest_height().unwrap(), Some(advanced_height));
assert_eq!(database.latest_height(), Some(advanced_height));
}

#[test]
fn database_not_advances_without_block() {
// Given
let mut database = Database::<OnChain>::default();
assert_eq!(database.latest_height().unwrap(), None);
assert_eq!(database.latest_height(), None);

// When
database
Expand All @@ -585,7 +589,7 @@ mod tests {
.storage_as_mut::<FuelBlocks>()
.insert(&starting_height, &CompressedBlock::default())
.unwrap();
assert_eq!(database.latest_height().unwrap(), Some(starting_height));
assert_eq!(database.latest_height(), Some(starting_height));

// When
let next_height = starting_height.advance_height().unwrap();
Expand All @@ -595,7 +599,7 @@ mod tests {
.unwrap();

// Then
assert_eq!(database.latest_height().unwrap(), Some(next_height));
assert_eq!(database.latest_height(), Some(next_height));
}

#[test]
Expand Down Expand Up @@ -707,7 +711,7 @@ mod tests {
fn database_advances_with_a_new_block() {
// Given
let mut database = Database::<OffChain>::default();
assert_eq!(database.latest_height().unwrap(), None);
assert_eq!(database.latest_height(), None);

// When
let advanced_height = 1.into();
Expand All @@ -717,14 +721,14 @@ mod tests {
.unwrap();

// Then
assert_eq!(database.latest_height().unwrap(), Some(advanced_height));
assert_eq!(database.latest_height(), Some(advanced_height));
}

#[test]
fn database_not_advances_without_block() {
// Given
let mut database = Database::<OffChain>::default();
assert_eq!(database.latest_height().unwrap(), None);
assert_eq!(database.latest_height(), None);

// When
database
Expand All @@ -745,7 +749,7 @@ mod tests {
.storage_as_mut::<FuelBlockIdsToHeights>()
.insert(&Default::default(), &starting_height)
.unwrap();
assert_eq!(database.latest_height().unwrap(), Some(starting_height));
assert_eq!(database.latest_height(), Some(starting_height));

// When
let next_height = starting_height.advance_height().unwrap();
Expand All @@ -755,7 +759,7 @@ mod tests {
.unwrap();

// Then
assert_eq!(database.latest_height().unwrap(), Some(next_height));
assert_eq!(database.latest_height(), Some(next_height));
}

#[test]
Expand Down Expand Up @@ -867,7 +871,7 @@ mod tests {
fn database_advances_with_a_new_block() {
// Given
let mut database = Database::<Relayer>::default();
assert_eq!(database.latest_height().unwrap(), None);
assert_eq!(database.latest_height(), None);

// When
let advanced_height = 1u64.into();
Expand All @@ -877,14 +881,14 @@ mod tests {
.unwrap();

// Then
assert_eq!(database.latest_height().unwrap(), Some(advanced_height));
assert_eq!(database.latest_height(), Some(advanced_height));
}

#[test]
fn database_not_advances_without_block() {
// Given
let mut database = Database::<Relayer>::default();
assert_eq!(database.latest_height().unwrap(), None);
assert_eq!(database.latest_height(), None);

// When
database
Expand All @@ -911,7 +915,7 @@ mod tests {
.storage_as_mut::<EventsHistory>()
.insert(&starting_height, &[])
.unwrap();
assert_eq!(database.latest_height().unwrap(), Some(starting_height));
assert_eq!(database.latest_height(), Some(starting_height));

// When
let next_height = starting_height.advance_height().unwrap();
Expand All @@ -921,7 +925,7 @@ mod tests {
.unwrap();

// Then
assert_eq!(database.latest_height().unwrap(), Some(next_height));
assert_eq!(database.latest_height(), Some(next_height));
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion crates/fuel-core/src/database/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ where
Ok(())
}

pub fn latest_height(&self) -> StorageResult<Option<Description::Height>> {
pub fn latest_height_from_metadata(
&self,
) -> StorageResult<Option<Description::Height>> {
let metadata = self.storage::<MetadataTable<Description>>().get(&())?;

let metadata = metadata.map(|metadata| *metadata.height());
Expand Down
12 changes: 8 additions & 4 deletions crates/fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl FuelService {
{
let start_up_consensus_config = &config.snapshot_reader.chain_config().consensus;

let mut rollback_height = None;
let mut found_override_height = None;
match start_up_consensus_config {
ConsensusConfig::PoA { .. } => {
// We don't support overriding of the heights for PoA version 1.
Expand Down Expand Up @@ -247,7 +247,7 @@ impl FuelService {
);

if !block_valid {
rollback_height = override_height.pred();
found_override_height = Some(override_height);
}
} else {
return Err(anyhow::anyhow!(
Expand All @@ -258,9 +258,13 @@ impl FuelService {
}
}

if let Some(rollback_height) = rollback_height {
if let Some(override_height) = found_override_height {
let rollback_height = override_height.pred().ok_or(anyhow::anyhow!(
"The override height is zero. \
The override height should be greater than zero."
))?;
tracing::warn!(
"The consensus at override height {rollback_height} \
"The consensus at override height {override_height} \
does not match with the database. \
Rollbacking the database to the height {rollback_height}"
);
Expand Down
Loading

0 comments on commit f999c83

Please sign in to comment.