Skip to content

Commit

Permalink
Allow producer to produce predefined blocks (#2081)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
Part of #1902

## Description
Provide mechanism for taking predefined blocks and including them in
block production.

Does not provide the mechanism for introducing those predefined blocks
into the running system, e.g. parsing from file or something. That will
come after.

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [ ] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?

---------

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

### Added
- [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.

### Changed
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions crates/fuel-core/src/service/adapters/consensus_module/poa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use fuel_core_poa::{
use fuel_core_services::stream::BoxStream;
use fuel_core_storage::transactional::Changes;
use fuel_core_types::{
blockchain::block::Block,
fuel_tx::TxId,
fuel_types::BlockHeight,
services::{
Expand Down Expand Up @@ -120,6 +121,15 @@ impl fuel_core_poa::ports::BlockProducer for BlockProducerAdapter {
}
}
}

async fn produce_predefined_block(
&self,
block: &Block,
) -> anyhow::Result<UncommittedResult<Changes>> {
self.block_producer
.produce_and_execute_predefined(block)
.await
}
}

#[async_trait::async_trait]
Expand Down
5 changes: 5 additions & 0 deletions crates/fuel-core/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ impl From<&Config> for fuel_core_poa::Config {
metrics: false,
min_connected_reserved_peers: config.min_connected_reserved_peers,
time_until_synced: config.time_until_synced,
chain_id: config
.snapshot_reader
.chain_config()
.consensus_parameters
.chain_id(),
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions crates/fuel-core/src/service/sub_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ use fuel_core_gas_price_service::fuel_gas_price_updater::{
UpdaterMetadata,
V0Metadata,
};
use fuel_core_poa::Trigger;
use fuel_core_poa::{
ports::InMemoryPredefinedBlocks,
Trigger,
};
use fuel_core_services::{
RunnableService,
ServiceRunner,
Expand All @@ -53,13 +56,20 @@ use fuel_core_storage::{
};
#[cfg(feature = "relayer")]
use fuel_core_types::blockchain::primitives::DaBlockHeight;
use std::sync::Arc;
use std::{
collections::HashMap,
sync::Arc,
};
use tokio::sync::Mutex;

mod algorithm_updater;

pub type PoAService =
fuel_core_poa::Service<TxPoolAdapter, BlockProducerAdapter, BlockImporterAdapter>;
pub type PoAService = fuel_core_poa::Service<
TxPoolAdapter,
BlockProducerAdapter,
BlockImporterAdapter,
InMemoryPredefinedBlocks,
>;
#[cfg(feature = "p2p")]
pub type P2PService = fuel_core_p2p::service::Service<Database>;
pub type TxPoolSharedState = fuel_core_txpool::service::SharedState<
Expand Down Expand Up @@ -235,6 +245,7 @@ pub fn init_sub_services(
tracing::info!("Enabled manual block production because of `debug` flag");
}

let predefined_blocks: InMemoryPredefinedBlocks = HashMap::new().into();
let poa = (production_enabled).then(|| {
fuel_core_poa::new_service(
&last_block_header,
Expand All @@ -243,6 +254,7 @@ pub fn init_sub_services(
producer_adapter.clone(),
importer_adapter.clone(),
p2p_adapter.clone(),
predefined_blocks,
)
});
let poa_adapter = PoAAdapter::new(poa.as_ref().map(|service| service.shared.clone()));
Expand Down
3 changes: 3 additions & 0 deletions crates/services/consensus_module/poa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ fuel-core-chain-config = { workspace = true }
fuel-core-services = { workspace = true }
fuel-core-storage = { workspace = true }
fuel-core-types = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tokio-stream = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
fuel-core-poa = { path = ".", features = ["test-helpers"] }
fuel-core-services = { workspace = true, features = ["test-helpers"] }
fuel-core-storage = { path = "./../../../storage", features = ["test-helpers"] }
fuel-core-types = { path = "./../../../types", features = ["test-helpers"] }
mockall = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/services/consensus_module/poa/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use fuel_core_types::{
blockchain::primitives::SecretKeyWrapper,
fuel_types::ChainId,
secrecy::Secret,
};
use tokio::time::Duration;
Expand All @@ -11,6 +12,7 @@ pub struct Config {
pub metrics: bool,
pub min_connected_reserved_peers: usize,
pub time_until_synced: Duration,
pub chain_id: ChainId,
}

#[cfg(feature = "test-helpers")]
Expand All @@ -22,6 +24,7 @@ impl Default for Config {
metrics: false,
min_connected_reserved_peers: 0,
time_until_synced: Duration::ZERO,
chain_id: ChainId::default(),
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions crates/services/consensus_module/poa/src/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use fuel_core_storage::{
};
use fuel_core_types::{
blockchain::{
block::Block,
header::BlockHeader,
primitives::DaBlockHeight,
},
Expand All @@ -29,6 +30,7 @@ use fuel_core_types::{
},
tai64::Tai64,
};
use std::collections::HashMap;

#[cfg_attr(test, mockall::automock)]
pub trait TransactionPool: Send + Sync {
Expand Down Expand Up @@ -59,6 +61,11 @@ pub trait BlockProducer: Send + Sync {
block_time: Tai64,
source: TransactionsSource,
) -> anyhow::Result<UncommittedExecutionResult<Changes>>;

async fn produce_predefined_block(
&self,
block: &Block,
) -> anyhow::Result<UncommittedExecutionResult<Changes>>;
}

#[cfg_attr(test, mockall::automock)]
Expand Down Expand Up @@ -108,3 +115,29 @@ pub trait SyncPort: Send + Sync {
/// await synchronization with the peers
async fn sync_with_peers(&mut self) -> anyhow::Result<()>;
}

pub trait PredefinedBlocks: Send + Sync {
fn get_block(&self, height: &BlockHeight) -> Option<Block>;
}

pub struct InMemoryPredefinedBlocks {
blocks: HashMap<BlockHeight, Block>,
}

impl From<HashMap<BlockHeight, Block>> for InMemoryPredefinedBlocks {
fn from(blocks: HashMap<BlockHeight, Block>) -> Self {
Self::new(blocks)
}
}

impl InMemoryPredefinedBlocks {
pub fn new(blocks: HashMap<BlockHeight, Block>) -> Self {
Self { blocks }
}
}

impl PredefinedBlocks for InMemoryPredefinedBlocks {
fn get_block(&self, height: &BlockHeight) -> Option<Block> {
self.blocks.get(height).cloned()
}
}
Loading

0 comments on commit 9de2c08

Please sign in to comment.