Skip to content

Commit

Permalink
Merge pull request #2127 from dusk-network/fix-2121
Browse files Browse the repository at this point in the history
rusk: Configurable block gas limit
  • Loading branch information
goshawk-3 authored Aug 21, 2024
2 parents 359f5f5 + 1c9b6a2 commit 266d9c9
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 24 deletions.
2 changes: 0 additions & 2 deletions consensus/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub const PROPOSAL_COMMITTEE_CREDITS: usize = 1;
pub const VALIDATION_COMMITTEE_CREDITS: usize = 64;
pub const RATIFICATION_COMMITTEE_CREDITS: usize = 64;

pub const DEFAULT_BLOCK_GAS_LIMIT: u64 = 5 * 1_000_000_000;

pub const RELAX_ITERATION_THRESHOLD: u8 = 8;

/// Emergency mode is enabled after 16 iterations
Expand Down
3 changes: 2 additions & 1 deletion consensus/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl From<InvalidFault> for Error {
#[derive(Default, Clone, Debug)]
pub struct CallParams {
pub round: u64,
pub block_gas_limit: u64,
pub generator_pubkey: node_data::bls::PublicKey,
pub to_slash: Vec<Slash>,
pub voters_pubkey: Option<Vec<Voter>>,
Expand Down Expand Up @@ -111,4 +110,6 @@ pub trait Operations: Send + Sync {
step_name: StepName,
elapsed: Duration,
) -> Result<(), Error>;

async fn get_block_gas_limit(&self) -> u64;
}
7 changes: 4 additions & 3 deletions consensus/src/proposal/block_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use node_data::ledger::{
};
use std::cmp::max;

use crate::config;
use crate::merkle::merkle_root;

use crate::config::MINIMUM_BLOCK_TIME;
Expand Down Expand Up @@ -60,6 +59,7 @@ impl<T: Operations> Generator<T> {
info!(
event = "gen_candidate",
hash = &to_str(&candidate.header().hash),
gas_limit = candidate.header().gas_limit,
state_hash = &to_str(&candidate.header().state_hash),
dur = format!("{:?}ms", start.elapsed().as_millis()),
);
Expand Down Expand Up @@ -97,7 +97,6 @@ impl<T: Operations> Generator<T> {

let call_params = CallParams {
round: ru.round,
block_gas_limit: config::DEFAULT_BLOCK_GAS_LIMIT,
generator_pubkey: ru.pubkey_bls.clone(),
to_slash,
voters_pubkey: Some(voters.to_owned()),
Expand All @@ -106,6 +105,8 @@ impl<T: Operations> Generator<T> {
let result =
self.executor.execute_state_transition(call_params).await?;

let block_gas_limit = self.executor.get_block_gas_limit().await;

let tx_hashes: Vec<_> =
result.txs.iter().map(|t| t.inner.hash()).collect();
let txs: Vec<_> = result.txs.into_iter().map(|t| t.inner).collect();
Expand All @@ -122,7 +123,7 @@ impl<T: Operations> Generator<T> {
version: 0,
height: ru.round,
timestamp,
gas_limit: config::DEFAULT_BLOCK_GAS_LIMIT,
gas_limit: block_gas_limit,
prev_block_hash,
seed,
generator_bls_pubkey: *ru.pubkey_bls.bytes(),
Expand Down
1 change: 1 addition & 0 deletions node/default.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ db_path = '/tmp/rusk-harness/'
consensus_keys_path = '/tmp/consensus_bls.keys'
generation_timeout = '3s'
max_queue_size = 5000
block_gas_limit = 5000000000

[chain.db_options]
enable_debug = false
Expand Down
4 changes: 4 additions & 0 deletions node/src/chain/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,8 @@ impl<DB: database::DB, VM: vm::VMExecution> Operations for Executor<DB, VM> {

Ok(())
}

async fn get_block_gas_limit(&self) -> u64 {
self.vm.read().await.get_block_gas_limit()
}
}
3 changes: 3 additions & 0 deletions node/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub trait VMExecution: Send + Sync + 'static {
/// Returns last finalized state root
fn get_finalized_state_root(&self) -> anyhow::Result<[u8; 32]>;

/// Returns block gas limit
fn get_block_gas_limit(&self) -> u64;

fn revert(&self, state_hash: [u8; 32]) -> anyhow::Result<[u8; 32]>;
fn revert_to_finalized(&self) -> anyhow::Result<[u8; 32]>;
}
5 changes: 4 additions & 1 deletion rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use tempfile::tempdir;

use common::state::new_state;

const BLOCK_GAS_LIMIT: u64 = 1_000_000_000_000;

fn load_phoenix_txs() -> Vec<Transaction> {
// The file "phoenix-txs" can be generated using
// `generate_phoenix_txs()` in "tests/rusk-state.rs".
Expand Down Expand Up @@ -164,7 +166,8 @@ pub fn accept_benchmark(c: &mut Criterion) {
let snapshot = toml::from_str(include_str!("../tests/config/bench.toml"))
.expect("Cannot deserialize config");

let rusk = new_state(&tmp, &snapshot).expect("Creating state should work");
let rusk = new_state(&tmp, &snapshot, BLOCK_GAS_LIMIT)
.expect("Creating state should work");

let phoenix_txs = load_phoenix_txs();
let moonlight_txs = load_moonlight_txs();
Expand Down
14 changes: 11 additions & 3 deletions rusk/src/bin/config/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{path::PathBuf, time::Duration};
use node::database::DatabaseOptions;
use serde::{Deserialize, Serialize};

pub const DEFAULT_BLOCK_GAS_LIMIT: u64 = 5 * 1_000_000_000;

use crate::args::Args;

#[derive(Serialize, Deserialize, Clone, Default)]
Expand All @@ -19,11 +21,13 @@ pub(crate) struct ChainConfig {
consensus_keys_path: Option<PathBuf>,
#[serde(with = "humantime_serde")]
generation_timeout: Option<Duration>,
// Note: changing the gas per deploy byte parameter is equivalent to
// forking the chain.
gas_per_deploy_byte: Option<u64>,

max_queue_size: Option<usize>,

// NB: changing the gas_per_deploy_byte/block_gas_limit is equivalent to
// forking the chain.
gas_per_deploy_byte: Option<u64>,
block_gas_limit: Option<u64>,
}

impl ChainConfig {
Expand Down Expand Up @@ -78,4 +82,8 @@ impl ChainConfig {
pub(crate) fn max_queue_size(&self) -> usize {
self.max_queue_size.unwrap_or(10_000)
}

pub(crate) fn block_gas_limit(&self) -> u64 {
self.block_gas_limit.unwrap_or(DEFAULT_BLOCK_GAS_LIMIT)
}
}
1 change: 1 addition & 0 deletions rusk/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
state_dir,
config.chain.generation_timeout(),
config.chain.gas_per_deploy_byte(),
config.chain.block_gas_limit(),
config.http.feeder_call_gas,
_event_sender.clone(),
)?;
Expand Down
1 change: 1 addition & 0 deletions rusk/src/lib/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Rusk {
pub(crate) generation_timeout: Option<Duration>,
pub(crate) gas_per_deploy_byte: Option<u64>,
pub(crate) feeder_gas_limit: u64,
pub(crate) block_gas_limit: u64,
pub(crate) event_sender: broadcast::Sender<RuesEvent>,
}

Expand Down
8 changes: 7 additions & 1 deletion rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl Rusk {
dir: P,
generation_timeout: Option<Duration>,
gas_per_deploy_byte: Option<u64>,
block_gas_limit: u64,
feeder_gas_limit: u64,
event_sender: broadcast::Sender<RuesEvent>,
) -> Result<Self> {
Expand Down Expand Up @@ -90,6 +91,7 @@ impl Rusk {
gas_per_deploy_byte,
feeder_gas_limit,
event_sender,
block_gas_limit,
})
}

Expand All @@ -102,7 +104,7 @@ impl Rusk {
let started = Instant::now();

let block_height = params.round;
let block_gas_limit = params.block_gas_limit;
let block_gas_limit = self.block_gas_limit;
let generator = params.generator_pubkey.inner();
let to_slash = params.to_slash.clone();

Expand Down Expand Up @@ -418,6 +420,10 @@ impl Rusk {
// finalization to wait, we spawn a new task to delete the commits.
task::spawn(delete_commits(self.vm.clone(), to_delete));
}

pub(crate) fn block_gas_limit(&self) -> u64 {
self.block_gas_limit
}
}

async fn delete_commits(vm: Arc<VM>, commits: Vec<[u8; 32]>) {
Expand Down
4 changes: 4 additions & 0 deletions rusk/src/lib/node/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ impl VMExecution for Rusk {

Ok(state_hash)
}

fn get_block_gas_limit(&self) -> u64 {
self.block_gas_limit()
}
}

impl Rusk {
Expand Down
9 changes: 6 additions & 3 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ use tracing::info;
use crate::common::keys::STAKE_SK;

// Creates a Rusk initial state in the given directory
pub fn new_state<P: AsRef<Path>>(dir: P, snapshot: &Snapshot) -> Result<Rusk> {
pub fn new_state<P: AsRef<Path>>(
dir: P,
snapshot: &Snapshot,
block_gas_limit: u64,
) -> Result<Rusk> {
let dir = dir.as_ref();

let (_, commit_id) = state::deploy(dir, snapshot, |_| {})
.expect("Deploying initial state should succeed");

let (sender, _) = broadcast::channel(10);

let rusk = Rusk::new(dir, None, None, u64::MAX, sender)
let rusk = Rusk::new(dir, None, None, block_gas_limit, u64::MAX, sender)
.expect("Instantiating rusk should succeed");

assert_eq!(
Expand Down Expand Up @@ -109,7 +113,6 @@ pub fn generator_procedure(

let call_params = CallParams {
round,
block_gas_limit,
generator_pubkey,
to_slash,
voters_pubkey: None,
Expand Down
7 changes: 4 additions & 3 deletions rusk/tests/rusk-state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ use tracing::info;
use crate::common::state::new_state;

const BLOCK_HEIGHT: u64 = 1;
const BLOCK_GAS_LIMIT: u64 = 100_000_000_000;
const INITIAL_BALANCE: u64 = 10_000_000_000;

// Creates the Rusk initial state for the tests below
fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
let snapshot = toml::from_str(include_str!("./config/rusk-state.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

fn leaves_from_height(rusk: &Rusk, height: u64) -> Result<Vec<TreeLeaf>> {
Expand Down Expand Up @@ -183,7 +184,7 @@ async fn generate_phoenix_txs() -> Result<(), Box<dyn std::error::Error>> {
let snapshot = toml::from_str(include_str!("./config/bench.toml"))
.expect("Cannot deserialize config");

let rusk = new_state(&tmp, &snapshot)?;
let rusk = new_state(&tmp, &snapshot, 100_000_000_000)?;

let cache =
Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));
Expand Down Expand Up @@ -248,7 +249,7 @@ async fn generate_moonlight_txs() -> Result<(), Box<dyn std::error::Error>> {
let snapshot = toml::from_str(include_str!("./config/bench.toml"))
.expect("Cannot deserialize config");

let rusk = new_state(&tmp, &snapshot)?;
let rusk = new_state(&tmp, &snapshot, 100_000_000_000)?;

let cache =
Arc::new(std::sync::RwLock::new(std::collections::HashMap::new()));
Expand Down
2 changes: 1 addition & 1 deletion rusk/tests/services/contract_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn initial_state<P: AsRef<Path>>(dir: P, deploy_bob: bool) -> Result<Rusk> {

let (sender, _) = broadcast::channel(10);

let rusk = Rusk::new(dir, None, None, u64::MAX, sender)
let rusk = Rusk::new(dir, None, None, BLOCK_GAS_LIMIT, u64::MAX, sender)
.expect("Instantiating rusk should succeed");
Ok(rusk)
}
Expand Down
2 changes: 1 addition & 1 deletion rusk/tests/services/gas_behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
let snapshot = toml::from_str(include_str!("../config/gas-behavior.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

const SENDER_INDEX_0: u64 = 0;
Expand Down
2 changes: 1 addition & 1 deletion rusk/tests/services/multi_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
toml::from_str(include_str!("../config/multi_transfer.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

/// Executes three different transactions in the same block, expecting only two
Expand Down
4 changes: 2 additions & 2 deletions rusk/tests/services/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ fn stake_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
let snapshot = toml::from_str(include_str!("../config/stake.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

// Creates the Rusk initial state for the tests below
fn slash_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
let snapshot = toml::from_str(include_str!("../config/slash.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

/// Stakes an amount Dusk and produces a block with this single transaction,
Expand Down
2 changes: 1 addition & 1 deletion rusk/tests/services/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
let snapshot = toml::from_str(include_str!("../config/transfer.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

/// Transacts between two accounts on the in the same wallet and produces a
Expand Down
2 changes: 1 addition & 1 deletion rusk/tests/services/unspendable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn initial_state<P: AsRef<Path>>(dir: P) -> Result<Rusk> {
let snapshot = toml::from_str(include_str!("../config/unspendable.toml"))
.expect("Cannot deserialize config");

new_state(dir, &snapshot)
new_state(dir, &snapshot, BLOCK_GAS_LIMIT)
}

const SENDER_INDEX_0: u64 = 0;
Expand Down

0 comments on commit 266d9c9

Please sign in to comment.