Skip to content

Commit

Permalink
feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Apr 22, 2024
1 parent 3614148 commit 59eddd9
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions core/parameters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ assert_matches.workspace = true
insta.workspace = true

[features]
protocol_feature_nonrefundable_transfer_nep491 = []
nightly = [
"near-primitives-core/nightly",
"nightly_protocol",
"protocol_feature_nonrefundable_transfer_nep491",
]
nightly_protocol = [
"near-primitives-core/nightly_protocol",
Expand Down
12 changes: 10 additions & 2 deletions core/parameters/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub enum ActionCosts {
new_data_receipt_base = 13,
new_data_receipt_byte = 14,
delegate = 15,
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
nonrefundable_transfer = 16,
}

Expand Down Expand Up @@ -421,6 +422,7 @@ impl RuntimeFeesConfig {
send_not_sir: 115123062500,
execution: 115123062500,
},
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
ActionCosts::nonrefundable_transfer => Fee {
send_sir: 115123062500,
send_not_sir: 115123062500,
Expand Down Expand Up @@ -519,13 +521,16 @@ pub fn transfer_exec_fee(
implicit_account_creation_allowed: bool,
eth_implicit_accounts_enabled: bool,
receiver_account_type: AccountType,
is_nonrefundable: bool,
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")] is_nonrefundable: bool,
) -> Gas {
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
let transfer_fee = if is_nonrefundable {
cfg.fee(ActionCosts::nonrefundable_transfer).exec_fee()
} else {
cfg.fee(ActionCosts::transfer).exec_fee()
};
#[cfg(not(feature = "protocol_feature_nonrefundable_transfer_nep491"))]
let transfer_fee = cfg.fee(ActionCosts::transfer).exec_fee();
match (implicit_account_creation_allowed, eth_implicit_accounts_enabled, receiver_account_type)
{
// Regular transfer to a named account.
Expand Down Expand Up @@ -553,13 +558,16 @@ pub fn transfer_send_fee(
implicit_account_creation_allowed: bool,
eth_implicit_accounts_enabled: bool,
receiver_account_type: AccountType,
is_nonrefundable: bool,
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")] is_nonrefundable: bool,
) -> Gas {
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
let transfer_fee = if is_nonrefundable {
cfg.fee(ActionCosts::nonrefundable_transfer).send_fee(sender_is_receiver)
} else {
cfg.fee(ActionCosts::transfer).send_fee(sender_is_receiver)
};
#[cfg(not(feature = "protocol_feature_nonrefundable_transfer_nep491"))]
let transfer_fee = cfg.fee(ActionCosts::transfer).send_fee(sender_is_receiver);
match (implicit_account_creation_allowed, eth_implicit_accounts_enabled, receiver_account_type)
{
// Regular transfer to a named account.
Expand Down
5 changes: 4 additions & 1 deletion core/parameters/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ pub enum FeeParameter {
ActionAddFunctionCallKeyPerByte,
ActionDeleteKey,
ActionDelegate,
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
ActionNonrefundableStorageTransfer,
}

impl Parameter {
Expand Down Expand Up @@ -252,7 +254,8 @@ impl From<ActionCosts> for FeeParameter {
ActionCosts::function_call_base => Self::ActionFunctionCall,
ActionCosts::function_call_byte => Self::ActionFunctionCallPerByte,
ActionCosts::transfer => Self::ActionTransfer,
ActionCosts::nonrefundable_transfer => Self::ActionTransfer, // TODO
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
ActionCosts::nonrefundable_transfer => Self::ActionNonrefundableStorageTransfer,
ActionCosts::stake => Self::ActionStake,
ActionCosts::add_full_access_key => Self::ActionAddFullAccessKey,
ActionCosts::add_function_call_key_base => Self::ActionAddFunctionCallKey,
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,16 @@ protocol_feature_fix_contract_loading_cost = [
"near-primitives-core/protocol_feature_fix_contract_loading_cost",
]

protocol_feature_nonrefundable_transfer_nep491 = [
"near-primitives-core/protocol_feature_nonrefundable_transfer_nep491",
]

nightly = [
"near-parameters/nightly",
"near-primitives-core/nightly",
"nightly_protocol",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_nonrefundable_transfer_nep491",
]
sandbox = []
io_trace = []
Expand Down
2 changes: 2 additions & 0 deletions runtime/near-vm-runner/src/logic/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,13 +1783,15 @@ impl<'a> VMLogic<'a> {
self.config.implicit_account_creation,
self.config.eth_implicit_accounts,
receiver_id.get_account_type(),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
false,
);
let exec_fee = transfer_exec_fee(
self.fees_config,
self.config.implicit_account_creation,
self.config.eth_implicit_accounts,
receiver_id.get_account_type(),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
false,
);
let burn_gas = send_fee;
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime-params-estimator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ no_cache = [
"node-runtime/no_cache",
"near-store/no_cache",
]
protocol_feature_nonrefundable_transfer_nep491 = [
"near-parameters/protocol_feature_nonrefundable_transfer_nep491",
]
nightly = [
"genesis-populate/nightly",
"near-chain-configs/nightly",
Expand All @@ -80,6 +83,7 @@ nightly = [
"nearcore/nightly",
"nightly_protocol",
"node-runtime/nightly",
"protocol_feature_nonrefundable_transfer_nep491",
]
nightly_protocol = [
"genesis-populate/nightly_protocol",
Expand Down
8 changes: 8 additions & 0 deletions runtime/runtime-params-estimator/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,14 @@ pub enum Cost {
/// `promise_yield_resume` host function.
YieldResumeByte,

/// Estimates `action_creation_config.transfer_cost` which is charged for
/// every `Action::Transfer`, the same value for sending and executing.
///
/// Estimation: Measure a transaction with only a transfer and subtract the
/// base cost of creating a receipt.
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
ActionNonrefundableStorageTransfer,

__Count,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fn runtime_fees_config(cost_table: &CostTable) -> anyhow::Result<RuntimeFeesConf
ActionCosts::function_call_base => fee(Cost::ActionFunctionCallBase)?,
ActionCosts::function_call_byte => fee(Cost::ActionFunctionCallPerByte)?,
ActionCosts::transfer => fee(Cost::ActionTransfer)?,
ActionCosts::nonrefundable_transfer => fee(Cost::ActionTransfer)?, // TODO
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
ActionCosts::nonrefundable_transfer => fee(Cost::ActionNonrefundableStorageTransfer)?,
ActionCosts::stake => fee(Cost::ActionStake)?,
ActionCosts::add_full_access_key => fee(Cost::ActionAddFullAccessKey)?,
ActionCosts::add_function_call_key_base => fee(Cost::ActionAddFunctionAccessKeyBase)?,
Expand Down
27 changes: 27 additions & 0 deletions runtime/runtime-params-estimator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ use gas_metering::gas_metering_cost;
use near_crypto::{KeyType, SecretKey};
use near_parameters::{ExtCosts, RuntimeConfigStore, RuntimeFeesConfig};
use near_primitives::account::{AccessKey, AccessKeyPermission, FunctionCallPermission};
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
use near_primitives::action::NonrefundableStorageTransferAction;
use near_primitives::transaction::{
Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction,
DeployContractAction, SignedTransaction, StakeAction, TransferAction,
Expand Down Expand Up @@ -132,6 +134,8 @@ static ALL_COSTS: &[(Cost, fn(&mut EstimatorContext) -> GasCost)] = &[
(Cost::ActionTransferSendSir, action_costs::transfer_send_sir),
(Cost::ActionTransferSendNotSir, action_costs::transfer_send_not_sir),
(Cost::ActionTransferExec, action_costs::transfer_exec),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
(Cost::ActionNonrefundableStorageTransfer, action_nonrefundable_storage_transfer),
(Cost::ActionCreateAccount, action_create_account),
(Cost::ActionCreateAccountSendSir, action_costs::create_account_send_sir),
(Cost::ActionCreateAccountSendNotSir, action_costs::create_account_send_not_sir),
Expand Down Expand Up @@ -377,6 +381,29 @@ fn action_transfer(ctx: &mut EstimatorContext) -> GasCost {
total_cost.saturating_sub(&base_cost, &NonNegativeTolerance::PER_MILLE)
}

#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
fn action_nonrefundable_storage_transfer(ctx: &mut EstimatorContext) -> GasCost {
let total_cost = {
let mut make_transaction = |tb: &mut TransactionBuilder| -> SignedTransaction {
let (sender, receiver) = tb.random_account_pair();

let actions =
vec![Action::NonrefundableStorageTransfer(NonrefundableStorageTransferAction {
deposit: 1,
})];
tb.transaction_from_actions(sender, receiver, actions)
};
let block_size = 100;
// Transferring from one account to another may touch two shards, thus executes over two blocks.
let block_latency = 1;
transaction_cost_ext(ctx, block_size, &mut make_transaction, block_latency).0
};

let base_cost = action_receipt_creation(ctx);

total_cost.saturating_sub(&base_cost, &NonNegativeTolerance::PER_MILLE)
}

fn action_create_account(ctx: &mut EstimatorContext) -> GasCost {
let total_cost = {
let mut make_transaction = |tb: &mut TransactionBuilder| -> SignedTransaction {
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn total_send_fees(
config.wasm_config.implicit_account_creation,
config.wasm_config.eth_implicit_accounts,
receiver_id.get_account_type(),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
false,
)
}
Expand All @@ -116,6 +117,7 @@ pub fn total_send_fees(
config.wasm_config.implicit_account_creation,
config.wasm_config.eth_implicit_accounts,
receiver_id.get_account_type(),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
true,
)
}
Expand Down Expand Up @@ -212,6 +214,7 @@ pub fn exec_fee(config: &RuntimeConfig, action: &Action, receiver_id: &AccountId
config.wasm_config.implicit_account_creation,
config.wasm_config.eth_implicit_accounts,
receiver_id.get_account_type(),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
false,
)
}
Expand All @@ -223,6 +226,7 @@ pub fn exec_fee(config: &RuntimeConfig, action: &Action, receiver_id: &AccountId
config.wasm_config.implicit_account_creation,
config.wasm_config.eth_implicit_accounts,
receiver_id.get_account_type(),
#[cfg(feature = "protocol_feature_nonrefundable_transfer_nep491")]
true,
)
}
Expand Down

0 comments on commit 59eddd9

Please sign in to comment.