Skip to content

Commit

Permalink
Merge pull request #3211 from dusk-network/min_deploy_points
Browse files Browse the repository at this point in the history
Add minimum deployment gas points setting
  • Loading branch information
miloszm authored Dec 20, 2024
2 parents 9c0c8b0 + 36ea68e commit 49f9d63
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ kadcast = "=0.7.0"
phoenix-circuits = { version = "=0.4.0", default-features = false }
phoenix-core = { version = "=0.32.0", default-features = false }
# we leave piecrust open until a stable release is out
piecrust = "=0.27.0-rc.0"
piecrust-uplink = "0.17.2-rc.0"
piecrust = "=0.27.0"
piecrust-uplink = "0.17.3"
poseidon-merkle = "=0.7.0"

# External dependencies
Expand Down
7 changes: 5 additions & 2 deletions node/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ impl MempoolSrv {
}

let gas_per_deploy_byte = vm.gas_per_deploy_byte();
let min_gas_limit =
vm::bytecode_charge(&deploy.bytecode, gas_per_deploy_byte);
let min_gas_limit = vm::bytecode_charge(
&deploy.bytecode,
gas_per_deploy_byte,
vm.min_deploy_points(),
);
if tx.inner.gas_limit() < min_gas_limit {
return Err(TxAcceptanceError::GasLimitTooLow(min_gas_limit));
}
Expand Down
8 changes: 7 additions & 1 deletion node/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use dusk_core::transfer::data::ContractBytecode;
use dusk_core::transfer::moonlight::AccountData;
use node_data::events::contract::ContractEvent;
use node_data::ledger::{Block, SpentTransaction, Transaction};
use std::cmp::max;

#[derive(Default)]
pub struct Config {}
Expand Down Expand Up @@ -88,6 +89,7 @@ pub trait VMExecution: Send + Sync + 'static {
fn gas_per_deploy_byte(&self) -> u64;
fn min_deployment_gas_price(&self) -> u64;
fn min_gas_limit(&self) -> u64;
fn min_deploy_points(&self) -> u64;
}

#[allow(clippy::large_enum_variant)]
Expand All @@ -105,6 +107,10 @@ pub enum PreverificationResult {
pub fn bytecode_charge(
bytecode: &ContractBytecode,
gas_per_deploy_byte: u64,
min_deploy_points: u64,
) -> u64 {
bytecode.bytes.len() as u64 * gas_per_deploy_byte
max(
bytecode.bytes.len() as u64 * gas_per_deploy_byte,
min_deploy_points,
)
}
1 change: 1 addition & 0 deletions rusk/default.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#gas_per_deploy_byte = 100
#min_deployment_gas_price = 2000
#min_gas_limit = 75000
#min_deploy_points = 5000000

[databroker]
max_inv_entries = 100
Expand Down
5 changes: 5 additions & 0 deletions rusk/src/bin/config/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub(crate) struct ChainConfig {
// forking the chain.
gas_per_deploy_byte: Option<u64>,
min_deployment_gas_price: Option<u64>,
min_deploy_points: Option<u64>,
min_gas_limit: Option<u64>,
block_gas_limit: Option<u64>,

Expand Down Expand Up @@ -93,6 +94,10 @@ impl ChainConfig {
self.min_deployment_gas_price
}

pub(crate) fn min_deploy_points(&self) -> Option<u64> {
self.min_deploy_points
}

pub(crate) fn min_gas_limit(&self) -> Option<u64> {
self.min_gas_limit
}
Expand Down
1 change: 1 addition & 0 deletions rusk/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_min_deployment_gas_price(
config.chain.min_deployment_gas_price(),
)
.with_min_deploy_points(config.chain.min_deploy_points())
.with_min_gas_limit(config.chain.min_gas_limit())
.with_block_gas_limit(config.chain.block_gas_limit());
};
Expand Down
13 changes: 13 additions & 0 deletions rusk/src/lib/builder/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct RuskNodeBuilder {
gas_per_deploy_byte: Option<u64>,
min_deployment_gas_price: Option<u64>,
min_gas_limit: Option<u64>,
min_deploy_points: Option<u64>,
block_gas_limit: u64,
feeder_call_gas: u64,
state_dir: PathBuf,
Expand All @@ -56,6 +57,7 @@ pub struct RuskNodeBuilder {
const DEFAULT_GAS_PER_DEPLOY_BYTE: u64 = 100;
const DEFAULT_MIN_DEPLOYMENT_GAS_PRICE: u64 = 2000;
const DEFAULT_MIN_GAS_LIMIT: u64 = 75000;
const DEFAULT_MIN_DEPLOY_POINTS: u64 = 5_000_000;

impl RuskNodeBuilder {
pub fn with_consensus_keys(mut self, consensus_keys_path: String) -> Self {
Expand Down Expand Up @@ -142,6 +144,14 @@ impl RuskNodeBuilder {
self
}

pub fn with_min_deploy_points(
mut self,
min_deploy_points: Option<u64>,
) -> Self {
self.min_deploy_points = min_deploy_points;
self
}

pub fn with_block_gas_limit(mut self, block_gas_limit: u64) -> Self {
self.block_gas_limit = block_gas_limit;
self
Expand Down Expand Up @@ -187,6 +197,8 @@ impl RuskNodeBuilder {
.min_deployment_gas_price
.unwrap_or(DEFAULT_MIN_DEPLOYMENT_GAS_PRICE);
let min_gas_limit = self.min_gas_limit.unwrap_or(DEFAULT_MIN_GAS_LIMIT);
let min_deploy_points =
self.min_deploy_points.unwrap_or(DEFAULT_MIN_DEPLOY_POINTS);

let rusk = Rusk::new(
self.state_dir,
Expand All @@ -195,6 +207,7 @@ impl RuskNodeBuilder {
gas_per_deploy_byte,
min_deployment_gas_price,
min_gas_limit,
min_deploy_points,
self.block_gas_limit,
self.feeder_call_gas,
rues_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 @@ -44,6 +44,7 @@ pub struct Rusk {
pub(crate) gas_per_deploy_byte: u64,
pub(crate) min_deployment_gas_price: u64,
pub(crate) min_gas_limit: u64,
pub(crate) min_deploy_points: u64,
pub(crate) feeder_gas_limit: u64,
pub(crate) block_gas_limit: u64,
pub(crate) event_sender: broadcast::Sender<RuesEvent>,
Expand Down
24 changes: 21 additions & 3 deletions rusk/src/lib/node/rusk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Rusk {
gas_per_deploy_byte: u64,
min_deployment_gas_price: u64,
min_gas_limit: u64,
min_deploy_points: u64,
block_gas_limit: u64,
feeder_gas_limit: u64,
event_sender: broadcast::Sender<RuesEvent>,
Expand Down Expand Up @@ -101,6 +102,7 @@ impl Rusk {
gas_per_deploy_byte,
min_deployment_gas_price,
min_gas_limit,
min_deploy_points,
feeder_gas_limit,
event_sender,
#[cfg(feature = "archive")]
Expand Down Expand Up @@ -171,6 +173,7 @@ impl Rusk {
&mut session,
&unspent_tx.inner,
self.gas_per_deploy_byte,
self.min_deploy_points,
self.min_deployment_gas_price,
) {
Ok(receipt) => {
Expand All @@ -191,6 +194,7 @@ impl Rusk {
&mut session,
&spent_tx.inner.inner,
self.gas_per_deploy_byte,
self.min_deploy_points,
self.min_deployment_gas_price,
);
}
Expand Down Expand Up @@ -283,6 +287,7 @@ impl Rusk {
slashing,
voters,
self.gas_per_deploy_byte,
self.min_deploy_points,
self.min_deployment_gas_price,
)
.map(|(a, b, _, _)| (a, b))
Expand Down Expand Up @@ -322,6 +327,7 @@ impl Rusk {
slashing,
voters,
self.gas_per_deploy_byte,
self.min_deploy_points,
self.min_deployment_gas_price,
)?;

Expand Down Expand Up @@ -580,6 +586,7 @@ fn accept(
slashing: Vec<Slash>,
voters: &[Voter],
gas_per_deploy_byte: u64,
min_deploy_points: u64,
min_deployment_gas_price: u64,
) -> Result<(
Vec<SpentTransaction>,
Expand All @@ -604,6 +611,7 @@ fn accept(
&mut session,
tx,
gas_per_deploy_byte,
min_deploy_points,
min_deployment_gas_price,
)?;

Expand Down Expand Up @@ -683,9 +691,14 @@ fn contract_deploy(
deploy: &ContractDeploy,
gas_limit: u64,
gas_per_deploy_byte: u64,
min_deploy_points: u64,
receipt: &mut CallReceipt<Result<Vec<u8>, ContractError>>,
) {
let deploy_charge = bytecode_charge(&deploy.bytecode, gas_per_deploy_byte);
let deploy_charge = bytecode_charge(
&deploy.bytecode,
gas_per_deploy_byte,
min_deploy_points,
);
let min_gas_limit = receipt.gas_spent + deploy_charge;
let hash = blake3::hash(deploy.bytecode.bytes.as_slice());
if gas_limit < min_gas_limit {
Expand Down Expand Up @@ -755,13 +768,17 @@ fn execute(
session: &mut Session,
tx: &ProtocolTransaction,
gas_per_deploy_byte: u64,
min_deploy_points: u64,
min_deployment_gas_price: u64,
) -> Result<CallReceipt<Result<Vec<u8>, ContractError>>, PiecrustError> {
// Transaction will be discarded if it is a deployment transaction
// with gas limit smaller than deploy charge.
if let Some(deploy) = tx.deploy() {
let deploy_charge =
bytecode_charge(&deploy.bytecode, gas_per_deploy_byte);
let deploy_charge = bytecode_charge(
&deploy.bytecode,
gas_per_deploy_byte,
min_deploy_points,
);
if tx.gas_price() < min_deployment_gas_price {
return Err(PiecrustError::Panic(
"gas price too low to deploy".into(),
Expand Down Expand Up @@ -793,6 +810,7 @@ fn execute(
deploy,
gas_left,
gas_per_deploy_byte,
min_deploy_points,
&mut receipt,
);
}
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 @@ -279,6 +279,10 @@ impl VMExecution for Rusk {
fn min_gas_limit(&self) -> u64 {
self.min_gas_limit
}

fn min_deploy_points(&self) -> u64 {
self.min_deploy_points
}
}

fn has_unique_elements<T>(iter: T) -> bool
Expand Down
2 changes: 2 additions & 0 deletions rusk/tests/common/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const CHAIN_ID: u8 = 0xFA;
pub const DEFAULT_GAS_PER_DEPLOY_BYTE: u64 = 100;
pub const DEFAULT_MIN_DEPLOYMENT_GAS_PRICE: u64 = 2000;
pub const DEFAULT_MIN_GAS_LIMIT: u64 = 75000;
pub const DEFAULT_MIN_DEPLOY_POINTS: u64 = 5000000;

// Creates a Rusk initial state in the given directory
pub fn new_state<P: AsRef<Path>>(
Expand Down Expand Up @@ -64,6 +65,7 @@ pub fn new_state_with_chainid<P: AsRef<Path>>(
DEFAULT_GAS_PER_DEPLOY_BYTE,
DEFAULT_MIN_DEPLOYMENT_GAS_PRICE,
DEFAULT_MIN_GAS_LIMIT,
DEFAULT_MIN_DEPLOY_POINTS,
block_gas_limit,
u64::MAX,
sender,
Expand Down
2 changes: 2 additions & 0 deletions rusk/tests/services/contract_deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tracing::info;

use crate::common::logger;
use crate::common::state::DEFAULT_MIN_DEPLOYMENT_GAS_PRICE;
use crate::common::state::DEFAULT_MIN_DEPLOY_POINTS;
use crate::common::state::{generator_procedure, ExecuteResult};
use crate::common::state::{
DEFAULT_GAS_PER_DEPLOY_BYTE, DEFAULT_MIN_GAS_LIMIT,
Expand Down Expand Up @@ -106,6 +107,7 @@ fn initial_state<P: AsRef<Path>>(dir: P, deploy_bob: bool) -> Result<Rusk> {
DEFAULT_GAS_PER_DEPLOY_BYTE,
DEFAULT_MIN_DEPLOYMENT_GAS_PRICE,
DEFAULT_MIN_GAS_LIMIT,
DEFAULT_MIN_DEPLOY_POINTS,
BLOCK_GAS_LIMIT,
u64::MAX,
sender,
Expand Down
2 changes: 2 additions & 0 deletions rusk/tests/services/owner_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use tracing::info;

use crate::common::logger;
use crate::common::state::DEFAULT_MIN_DEPLOYMENT_GAS_PRICE;
use crate::common::state::DEFAULT_MIN_DEPLOY_POINTS;
use crate::common::state::{
DEFAULT_GAS_PER_DEPLOY_BYTE, DEFAULT_MIN_GAS_LIMIT,
};
Expand Down Expand Up @@ -85,6 +86,7 @@ fn initial_state<P: AsRef<Path>>(
DEFAULT_GAS_PER_DEPLOY_BYTE,
DEFAULT_MIN_DEPLOYMENT_GAS_PRICE,
DEFAULT_MIN_GAS_LIMIT,
DEFAULT_MIN_DEPLOY_POINTS,
BLOCK_GAS_LIMIT,
u64::MAX,
sender,
Expand Down

0 comments on commit 49f9d63

Please sign in to comment.