Skip to content

Commit

Permalink
rusk: added minimum deploy points
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Dec 17, 2024
1 parent 26d22e8 commit a090479
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
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 @@ -169,6 +171,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 @@ -188,6 +191,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 @@ -279,6 +283,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 @@ -317,6 +322,7 @@ impl Rusk {
slashing,
voters,
self.gas_per_deploy_byte,
self.min_deploy_points,
self.min_deployment_gas_price,
)?;

Expand Down Expand Up @@ -572,6 +578,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 @@ -596,6 +603,7 @@ fn accept(
&mut session,
tx,
gas_per_deploy_byte,
min_deploy_points,
min_deployment_gas_price,
)?;

Expand Down Expand Up @@ -675,9 +683,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 @@ -747,13 +760,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 @@ -785,6 +802,7 @@ fn execute(
deploy,
gas_left,
gas_per_deploy_byte,
min_deploy_points,
&mut receipt,
);
}
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 a090479

Please sign in to comment.