Skip to content

Commit

Permalink
Merge branch '4467_gas-for-basic-operations' into fix-sc-gas
Browse files Browse the repository at this point in the history
  • Loading branch information
Eitu33 committed Nov 6, 2023
2 parents 3efbc30 + feaaf70 commit b7b223e
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions massa-execution-exports/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct ExecutionConfig {
pub max_read_only_gas: u64,
/// Gas costs
pub gas_costs: GasCosts,
/// Gas used by a transaction, a roll buy or a roll sell)
pub base_operation_gas_cost: u64,
/// last start period, used to attach to the correct execution slot if the network has restarted
pub last_start_period: u64,
/// Path to the hard drive cache storage
Expand Down
1 change: 1 addition & 0 deletions massa-execution-exports/src/test_exports/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Default for ExecutionConfig {
.into(),
)
.unwrap(),
base_operation_gas_cost: BASE_OPERATION_GAS_COST,
last_start_period: 0,
hd_cache_path: TempDir::new().unwrap().path().to_path_buf(),
lru_cache_size: 1000,
Expand Down
6 changes: 0 additions & 6 deletions massa-execution-worker/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ pub struct ExecutionContext {
/// speculative list of executed denunciations
speculative_executed_denunciations: SpeculativeExecutedDenunciations,

/// max gas for this execution
pub max_gas: u64,

/// minimal balance allowed for the creator of the operation after its execution
pub creator_min_balance: Option<Amount>,

Expand Down Expand Up @@ -220,7 +217,6 @@ impl ExecutionContext {
final_state,
active_history,
),
max_gas: Default::default(),
creator_min_balance: Default::default(),
slot: Slot::new(0, 0),
created_addr_index: Default::default(),
Expand Down Expand Up @@ -313,7 +309,6 @@ impl ExecutionContext {
pub(crate) fn readonly(
config: ExecutionConfig,
slot: Slot,
max_gas: u64,
call_stack: Vec<ExecutionStackElement>,
final_state: Arc<RwLock<FinalState>>,
active_history: Arc<RwLock<ActiveHistory>>,
Expand All @@ -331,7 +326,6 @@ impl ExecutionContext {

// return readonly context
ExecutionContext {
max_gas,
slot,
stack: call_stack,
read_only: true,
Expand Down
7 changes: 1 addition & 6 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ impl ExecutionState {
// save a snapshot of the context to revert any further changes on error
let context_snapshot = context.get_snapshot();

// set the context max gas to match the one defined in the operation
context.max_gas = operation.get_gas_usage();

// set the creator address
context.creator_address = Some(operation.content_creator_address);

Expand Down Expand Up @@ -381,7 +378,7 @@ impl ExecutionState {
}

// check remaining block gas
let op_gas = operation.get_gas_usage();
let op_gas = operation.get_gas_usage(self.config.base_operation_gas_cost);
let new_remaining_block_gas = remaining_block_gas.checked_sub(op_gas).ok_or_else(|| {
ExecutionError::NotEnoughGas(
"not enough remaining block gas to execute operation".to_string(),
Expand Down Expand Up @@ -942,7 +939,6 @@ impl ExecutionState {
let bytecode = {
let mut context = context_guard!(self);
context_snapshot = context.get_snapshot();
context.max_gas = message.max_gas;
context.creator_address = None;
context.creator_min_balance = None;
context.stack = vec![
Expand Down Expand Up @@ -1402,7 +1398,6 @@ impl ExecutionState {
let execution_context = ExecutionContext::readonly(
self.config.clone(),
slot,
req.max_gas,
req.call_stack,
self.final_state.clone(),
self.active_history.clone(),
Expand Down
2 changes: 2 additions & 0 deletions massa-models/src/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ pub const POOL_CONTROLLER_DENUNCIATIONS_CHANNEL_SIZE: usize = 1024;
pub const MAX_GAS_PER_BLOCK: u64 = u32::MAX as u64;
/// Maximum of GAS allowed for asynchronous messages execution on one slot
pub const MAX_ASYNC_GAS: u64 = 1_000_000_000;
/// Gas used by a base operation (transaction, roll buy, roll sell)
pub const BASE_OPERATION_GAS_COST: u64 = 800_000; // approx MAX_GAS_PER_BLOCK / MAX_OPERATIONS_PER_BLOCK
/// Maximum event size in bytes
pub const MAX_EVENT_DATA_SIZE: usize = 50_000;

Expand Down
10 changes: 6 additions & 4 deletions massa-models/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ use std::fmt::Formatter;
use std::{ops::Bound::Included, ops::RangeInclusive, str::FromStr};
use transition::Versioned;

/// Size in bytes of the serialized operation ID
/// Size in bytes of the serialized operation ID prefix
pub const OPERATION_ID_PREFIX_SIZE_BYTES: usize = 17;

Expand Down Expand Up @@ -971,15 +969,19 @@ impl SecureShareOperation {
start..=self.content.expire_period
}

/// Get the max amount of gas used by the operation (`max_gas`)
pub fn get_gas_usage(&self) -> u64 {
/// Get the maximum amount of gas used by the operation.
///
/// base_operation_gas_cost comes from the configuration and
/// is the cost of a basic operation (BASE_OPERATION_GAS_COST)
pub fn get_gas_usage(&self, base_operation_gas_cost: u64) -> u64 {
match &self.content.op {
OperationType::ExecuteSC { max_gas, .. } => *max_gas,
OperationType::CallSC { max_gas, .. } => *max_gas,
OperationType::RollBuy { .. } => 0,
OperationType::RollSell { .. } => 0,
OperationType::Transaction { .. } => 0,
}
.saturating_add(base_operation_gas_cost)
}

/// get the addresses that are involved in this operation from a ledger point of view
Expand Down
10 changes: 6 additions & 4 deletions massa-node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ use massa_models::config::constants::{
VERSION,
};
use massa_models::config::{
KEEP_EXECUTED_HISTORY_EXTRA_PERIODS, MAX_BOOTSTRAP_FINAL_STATE_PARTS_SIZE,
MAX_BOOTSTRAP_VERSIONING_ELEMENTS_SIZE, MAX_EVENT_DATA_SIZE, MAX_MESSAGE_SIZE,
POOL_CONTROLLER_DENUNCIATIONS_CHANNEL_SIZE, POOL_CONTROLLER_ENDORSEMENTS_CHANNEL_SIZE,
POOL_CONTROLLER_OPERATIONS_CHANNEL_SIZE,
BASE_OPERATION_GAS_COST, KEEP_EXECUTED_HISTORY_EXTRA_PERIODS,
MAX_BOOTSTRAP_FINAL_STATE_PARTS_SIZE, MAX_BOOTSTRAP_VERSIONING_ELEMENTS_SIZE,
MAX_EVENT_DATA_SIZE, MAX_MESSAGE_SIZE, POOL_CONTROLLER_DENUNCIATIONS_CHANNEL_SIZE,
POOL_CONTROLLER_ENDORSEMENTS_CHANNEL_SIZE, POOL_CONTROLLER_OPERATIONS_CHANNEL_SIZE,
};
use massa_models::slot::Slot;
use massa_pool_exports::{PoolBroadcasts, PoolChannels, PoolConfig, PoolManager};
Expand Down Expand Up @@ -498,6 +498,7 @@ async fn launch(
SETTINGS.execution.wasm_gas_costs_file.clone(),
)
.expect("Failed to load gas costs"),
base_operation_gas_cost: BASE_OPERATION_GAS_COST,
last_start_period: final_state.read().last_start_period,
hd_cache_path: SETTINGS.execution.hd_cache_path.clone(),
lru_cache_size: SETTINGS.execution.lru_cache_size,
Expand Down Expand Up @@ -536,6 +537,7 @@ async fn launch(
thread_count: THREAD_COUNT,
max_block_size: MAX_BLOCK_SIZE,
max_block_gas: MAX_GAS_PER_BLOCK,
base_operation_gas_cost: BASE_OPERATION_GAS_COST,
roll_price: ROLL_PRICE,
max_block_endorsement_count: ENDORSEMENT_COUNT,
operation_validity_periods: OPERATION_VALIDITY_PERIODS,
Expand Down
2 changes: 2 additions & 0 deletions massa-pool-exports/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct PoolConfig {
pub max_block_size: u32,
/// maximal gas per block
pub max_block_gas: u64,
/// Gas used by any operation
pub base_operation_gas_cost: u64,
/// cost (in coins) of a single roll
pub roll_price: Amount,
/// operation validity periods
Expand Down
3 changes: 2 additions & 1 deletion massa-pool-exports/src/test_exports/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022 MASSA LABS <[email protected]>

use massa_models::config::{
DENUNCIATION_EXPIRE_PERIODS, ENDORSEMENT_COUNT, MAX_BLOCK_SIZE,
BASE_OPERATION_GAS_COST, DENUNCIATION_EXPIRE_PERIODS, ENDORSEMENT_COUNT, MAX_BLOCK_SIZE,
MAX_DENUNCIATIONS_PER_BLOCK_HEADER, MAX_GAS_PER_BLOCK, MAX_OPERATIONS_PER_BLOCK,
OPERATION_VALIDITY_PERIODS, PERIODS_PER_CYCLE, ROLL_PRICE, T0, THREAD_COUNT,
};
Expand All @@ -17,6 +17,7 @@ impl Default for PoolConfig {
max_block_gas: MAX_GAS_PER_BLOCK,
roll_price: ROLL_PRICE,
max_block_size: MAX_BLOCK_SIZE,
base_operation_gas_cost: BASE_OPERATION_GAS_COST,
max_operation_pool_size: 32000,
max_operation_pool_excess_items: 10000,
max_endorsements_pool_size_per_thread: 1000,
Expand Down
10 changes: 6 additions & 4 deletions massa-pool-worker/src/operation_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl OperationPool {
let mut removed = PreHashSet::default();
self.sorted_ops.retain(|op_info| {
// filter out ops that use too much resources
let mut retain = (op_info.max_gas <= self.config.max_block_gas)
let mut retain = (op_info.max_gas_usage <= self.config.max_block_gas)
&& (op_info.size <= self.config.max_block_size as usize);

// filter out ops that are not valid during our PoS draws
Expand Down Expand Up @@ -266,7 +266,8 @@ impl OperationPool {
// gas score:
// 0% of block gas => score 1
// 100% of block gas => score 0
let gas_score = 1.0 - (op_info.max_gas as f32) / (self.config.max_block_gas as f32);
let gas_score =
1.0 - (op_info.max_gas_usage as f32) / (self.config.max_block_gas as f32);

// general resource score (mean of gas and size scores)
let epsilon_resource_factor = 0.0001; // avoids zero score when gas and size are a perfect fit in the block
Expand Down Expand Up @@ -431,6 +432,7 @@ impl OperationPool {
self.config.operation_validity_periods,
self.config.roll_price,
self.config.thread_count,
self.config.base_operation_gas_cost,
));
}
}
Expand Down Expand Up @@ -487,7 +489,7 @@ impl OperationPool {
}

// exclude ops that require too much gas
if op_info.max_gas > remaining_gas {
if op_info.max_gas_usage > remaining_gas {
continue;
}

Expand All @@ -498,7 +500,7 @@ impl OperationPool {
remaining_space -= op_info.size;

// update remaining block gas
remaining_gas -= op_info.max_gas;
remaining_gas -= op_info.max_gas_usage;

// update remaining number of operations
remaining_ops -= 1;
Expand Down
6 changes: 4 additions & 2 deletions massa-pool-worker/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::ops::RangeInclusive;
pub struct OperationInfo {
pub id: OperationId,
pub size: usize,
pub max_gas: u64,
/// The maximum amount of gas that can be used by an operation.
pub max_gas_usage: u64,
pub creator_address: Address,
pub thread: u8,
pub fee: Amount,
Expand All @@ -24,11 +25,12 @@ impl OperationInfo {
operation_validity_periods: u64,
roll_price: Amount,
thread_count: u8,
base_operation_gas_cost: u64,
) -> Self {
OperationInfo {
id: op.id,
size: op.serialized_size(),
max_gas: op.get_gas_usage(),
max_gas_usage: op.get_gas_usage(base_operation_gas_cost),
creator_address: op.content_creator_address,
fee: op.content.fee,
thread: op.content_creator_address.get_thread(thread_count),
Expand Down

0 comments on commit b7b223e

Please sign in to comment.