Skip to content

Commit

Permalink
Block production trigger (FuelLabs#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal authored Oct 13, 2022
1 parent 0074018 commit efa498c
Show file tree
Hide file tree
Showing 36 changed files with 1,615 additions and 80 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"fuel-client",
"fuel-core",
"fuel-core-bft",
"fuel-poa-coordinator",
"fuel-core-interfaces",
"fuel-metrics",
"fuel-p2p",
Expand Down
12 changes: 7 additions & 5 deletions fuel-block-producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use fuel_core_interfaces::{
common::{
crypto::ephemeral_merkle_root,
fuel_types::Bytes32,
prelude::Word,
},
executor::{
Error,
Expand Down Expand Up @@ -60,7 +61,11 @@ pub struct Producer<'a> {
#[async_trait::async_trait]
impl<'a> Trait for Producer<'a> {
/// Produces a block for the specified height
async fn produce_block(&self, height: BlockHeight) -> Result<FuelBlock> {
async fn produce_block(
&self,
height: BlockHeight,
max_gas: Word,
) -> Result<FuelBlock> {
// - get previous block info (hash, root, etc)
// - select best da_height from relayer
// - get available txs from txpool
Expand All @@ -75,10 +80,7 @@ impl<'a> Trait for Producer<'a> {
let previous_block_info = self.previous_block_info(height)?;
let new_da_height = self.select_new_da_height(previous_block_info.da_height)?;

let best_transactions = self
.txpool
.get_includable_txs(height, self.config.max_gas_per_block)
.await?;
let best_transactions = self.txpool.get_includable_txs(height, max_gas).await?;

let header = FuelBlockHeader {
height,
Expand Down
14 changes: 8 additions & 6 deletions fuel-block-producer/src/block_producer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn cant_produce_at_genesis_height() {
let producer = ctx.producer();

let err = producer
.produce_block(0u32.into())
.produce_block(0u32.into(), 1_000_000_000)
.await
.expect_err("expected failure");

Expand All @@ -53,7 +53,7 @@ async fn can_produce_initial_block() {
let ctx = TestContext::default();
let producer = ctx.producer();

let result = producer.produce_block(1u32.into()).await;
let result = producer.produce_block(1u32.into(), 1_000_000_000).await;

assert!(result.is_ok());
}
Expand Down Expand Up @@ -82,7 +82,9 @@ async fn can_produce_next_block() {

let ctx = TestContext::default_from_db(db);
let producer = ctx.producer();
let result = producer.produce_block(prev_height + 1u32.into()).await;
let result = producer
.produce_block(prev_height + 1u32.into(), 1_000_000_000)
.await;

assert!(result.is_ok());
}
Expand All @@ -94,7 +96,7 @@ async fn cant_produce_if_no_previous_block() {
let producer = ctx.producer();

let err = producer
.produce_block(100u32.into())
.produce_block(100u32.into(), 1_000_000_000)
.await
.expect_err("expected failure");

Expand Down Expand Up @@ -139,7 +141,7 @@ async fn cant_produce_if_previous_block_da_height_too_high() {
let producer = ctx.producer();

let err = producer
.produce_block(prev_height + 1u32.into())
.produce_block(prev_height + 1u32.into(), 1_000_000_000)
.await
.expect_err("expected failure");

Expand Down Expand Up @@ -170,7 +172,7 @@ async fn production_fails_on_execution_error() {
let producer = ctx.producer();

let err = producer
.produce_block(1u32.into())
.produce_block(1u32.into(), 1_000_000_000)
.await
.expect_err("expected failure");

Expand Down
18 changes: 2 additions & 16 deletions fuel-block-producer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
use fuel_core_interfaces::common::{
fuel_tx::ConsensusParameters,
fuel_types::Word,
};
use fuel_core_interfaces::common::fuel_tx::ConsensusParameters;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct Config {
pub max_gas_per_block: Word,
pub consensus_params: ConsensusParameters,
}

impl Default for Config {
fn default() -> Self {
Self {
// TODO: pick a reasonable default value based on gas scheduling analysis
max_gas_per_block: 10 * ConsensusParameters::DEFAULT.max_gas_per_tx,
consensus_params: Default::default(),
}
}
}
11 changes: 4 additions & 7 deletions fuel-block-producer/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ async fn block_producer() -> Result<()> {
let mock_db = MockDb::default();

let block_producer = Producer {
config: fuel_block_producer::config::Config {
max_gas_per_block,
consensus_params,
},
config: fuel_block_producer::config::Config { consensus_params },
db: &mock_db,
txpool: &TxPoolAdapter {
sender: txpool.sender().clone(),
Expand Down Expand Up @@ -193,7 +190,7 @@ async fn block_producer() -> Result<()> {

// Trigger block production
let generated_block = block_producer
.produce_block(1u32.into())
.produce_block(1u32.into(), max_gas_per_block)
.await
.expect("Failed to generate block");

Expand All @@ -212,7 +209,7 @@ async fn block_producer() -> Result<()> {

// Trigger block production again
let generated_block = block_producer
.produce_block(2u32.into())
.produce_block(2u32.into(), max_gas_per_block)
.await
.expect("Failed to generate block");

Expand All @@ -229,7 +226,7 @@ async fn block_producer() -> Result<()> {

// Trigger block production once more, now the block should be empty
let generated_block = block_producer
.produce_block(3u32.into())
.produce_block(3u32.into(), max_gas_per_block)
.await
.expect("Failed to generate block");

Expand Down
2 changes: 2 additions & 0 deletions fuel-chain-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ anyhow = "1.0"
fuel-core-interfaces = { path = "../fuel-core-interfaces", version = "0.10.1", features = [
"serde",
] }
fuel-poa-coordinator = { path = "../fuel-poa-coordinator", version = "0.10.1" }
hex = { version = "0.4", features = ["serde"] }
itertools = "0.10"
rand = "0.8"
ron = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["raw_value"] }
serde_with = "1.11"
Expand Down
32 changes: 20 additions & 12 deletions fuel-chain-config/src/config/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,21 @@ pub const TESTNET_INITIAL_BALANCE: u64 = 10_000_000;
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct ChainConfig {
pub chain_name: String,
pub block_production: ProductionStrategy,
pub block_production: BlockProduction,
pub block_gas_limit: u64,
#[serde(default)]
pub initial_state: Option<StateConfig>,
pub transaction_parameters: ConsensusParameters,
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub enum ProductionStrategy {
Instant,
Manual,
RoundRobin,
ProofOfStake,
}

impl Default for ChainConfig {
fn default() -> Self {
Self {
chain_name: "local".into(),
block_production: ProductionStrategy::Instant,
block_production: BlockProduction::ProofOfAuthority {
trigger: fuel_poa_coordinator::Trigger::Instant,
},
block_gas_limit: 1_000_000, // TODO: Pick a sensible default
transaction_parameters: ConsensusParameters::DEFAULT,
initial_state: None,
}
Expand Down Expand Up @@ -92,12 +88,11 @@ impl ChainConfig {

Self {
chain_name: LOCAL_TESTNET.to_string(),
block_production: ProductionStrategy::Instant,
initial_state: Some(StateConfig {
coins: Some(initial_coins),
..StateConfig::default()
}),
transaction_parameters: ConsensusParameters::DEFAULT,
..Default::default()
}
}
}
Expand Down Expand Up @@ -125,3 +120,16 @@ impl FromStr for ChainConfig {
}
}
}

/// Block production mode and settings
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BlockProduction {
/// Proof-of-authority modes
ProofOfAuthority {
#[serde(flatten)]
trigger: fuel_poa_coordinator::Trigger,
},
// TODO:
// RoundRobin,
// ProofOfStake,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ expression: json
---
{
"chain_name": "local_testnet",
"block_production": "Instant",
"block_production": {
"ProofOfAuthority": {
"trigger": "instant"
}
},
"block_gas_limit": 1000000,
"initial_state": {
"height": "0x0000000014c8be1f"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ expression: json
---
{
"chain_name": "local_testnet",
"block_production": "Instant",
"block_production": {
"ProofOfAuthority": {
"trigger": "instant"
}
},
"block_gas_limit": 1000000,
"initial_state": {
"contracts": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ expression: json
---
{
"chain_name": "local_testnet",
"block_production": "Instant",
"block_production": {
"ProofOfAuthority": {
"trigger": "instant"
}
},
"block_gas_limit": 1000000,
"initial_state": {
"contracts": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ expression: json
---
{
"chain_name": "local_testnet",
"block_production": "Instant",
"block_production": {
"ProofOfAuthority": {
"trigger": "instant"
}
},
"block_gas_limit": 1000000,
"initial_state": {
"coins": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ expression: json
---
{
"chain_name": "local_testnet",
"block_production": "Instant",
"block_production": {
"ProofOfAuthority": {
"trigger": "instant"
}
},
"block_gas_limit": 1000000,
"initial_state": {
"coins": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ expression: json
---
{
"chain_name": "local_testnet",
"block_production": "Instant",
"block_production": {
"ProofOfAuthority": {
"trigger": "instant"
}
},
"block_gas_limit": 1000000,
"initial_state": {
"contracts": [
{
Expand Down
Loading

0 comments on commit efa498c

Please sign in to comment.