Skip to content

Commit

Permalink
runtime: pass around the RuntimeConfig rather than just fees
Browse files Browse the repository at this point in the history
This is a no-op change that's been split out from #9364. In principle
this is not a strict improvement in isolation since the functions now
get access to more information than they strictly need to work with, but
in practice this should make our lives easier as now we won’t need to
redo this work if we want to make some fees dependent on some
configuration option :)
  • Loading branch information
nagisa committed Aug 16, 2023
1 parent 9e5794d commit 09adb4e
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 184 deletions.
2 changes: 1 addition & 1 deletion chain/indexer/src/streamer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) async fn convert_transactions_sir_into_local_receipts(
txs.into_iter()
.map(|tx| {
let cost = tx_cost(
&runtime_config.fees,
&runtime_config,
&near_primitives::transaction::Transaction {
signer_id: tx.transaction.signer_id.clone(),
public_key: tx.transaction.public_key.clone(),
Expand Down
5 changes: 1 addition & 4 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,10 +1252,7 @@ impl TryFrom<ActionView> for Action {
Action::DeleteAccount(DeleteAccountAction { beneficiary_id })
}
ActionView::Delegate { delegate_action, signature } => {
Action::Delegate(SignedDelegateAction {
delegate_action: delegate_action,
signature,
})
Action::Delegate(SignedDelegateAction { delegate_action, signature })
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/node/runtime_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ mod tests {
let (alice1, bob1) = (node.view_balance(&alice).unwrap(), node.view_balance(&bob).unwrap());
node_user.send_money(alice.clone(), bob.clone(), 1).unwrap();
let runtime_config = node.client.as_ref().read().unwrap().runtime_config.clone();
let fee_helper = FeeHelper::new(runtime_config.fees, node.genesis().config.min_gas_price);
let fee_helper = FeeHelper::new(runtime_config, node.genesis().config.min_gas_price);
let transfer_cost = fee_helper.transfer_cost();
let (alice2, bob2) = (node.view_balance(&alice).unwrap(), node.view_balance(&bob).unwrap());
assert_eq!(alice2, alice1 - 1 - transfer_cost);
Expand Down
13 changes: 7 additions & 6 deletions integration-tests/src/tests/client/features/delegate_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,15 @@ fn check_meta_tx_fn_call(
// dynamic cost. The contract reward can be inferred from that.

// static send gas is paid and burnt upfront
let static_send_gas = fee_helper.cfg.fee(ActionCosts::new_action_receipt).send_fee(false)
+ num_fn_calls as u64 * fee_helper.cfg.fee(ActionCosts::function_call_base).send_fee(false)
+ msg_len * fee_helper.cfg.fee(ActionCosts::function_call_byte).send_fee(false);
let static_send_gas = fee_helper.cfg().fee(ActionCosts::new_action_receipt).send_fee(false)
+ num_fn_calls as u64
* fee_helper.cfg().fee(ActionCosts::function_call_base).send_fee(false)
+ msg_len * fee_helper.cfg().fee(ActionCosts::function_call_byte).send_fee(false);
// static execution gas burnt in the same receipt as the function calls but
// it doesn't contribute to the contract reward
let static_exec_gas = fee_helper.cfg.fee(ActionCosts::new_action_receipt).exec_fee()
+ num_fn_calls as u64 * fee_helper.cfg.fee(ActionCosts::function_call_base).exec_fee()
+ msg_len * fee_helper.cfg.fee(ActionCosts::function_call_byte).exec_fee();
let static_exec_gas = fee_helper.cfg().fee(ActionCosts::new_action_receipt).exec_fee()
+ num_fn_calls as u64 * fee_helper.cfg().fee(ActionCosts::function_call_base).exec_fee()
+ msg_len * fee_helper.cfg().fee(ActionCosts::function_call_byte).exec_fee();

// calculate contract rewards as reward("gas burnt in fn call receipt" - "static exec costs")
let gas_burnt_for_function_call =
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/src/tests/standard_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use testlib::runtime_utils::{
const FUNCTION_CALL_AMOUNT: Balance = TESTING_INIT_BALANCE / 10;

pub(crate) fn fee_helper(node: &impl Node) -> FeeHelper {
FeeHelper::new(RuntimeConfig::test().fees, node.genesis().config.min_gas_price)
FeeHelper::new(RuntimeConfig::test(), node.genesis().config.min_gas_price)
}

/// Adds given access key to the given account_id using signer2.
Expand Down Expand Up @@ -404,9 +404,9 @@ pub fn trying_to_create_implicit_account(node: impl Node) {

let cost = fee_helper.create_account_transfer_full_key_cost_fail_on_create_account()
+ fee_helper.gas_to_balance(
fee_helper.cfg.fee(ActionCosts::create_account).send_fee(false)
fee_helper.cfg().fee(ActionCosts::create_account).send_fee(false)
+ fee_helper
.cfg
.cfg()
.fee(near_primitives::config::ActionCosts::add_full_access_key)
.send_fee(false),
);
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/src/tests/test_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use near_primitives::transaction::{
use near_primitives::version::PROTOCOL_VERSION;
use nearcore::config::{GenesisExt, TESTING_INIT_BALANCE, TESTING_INIT_STAKE};
use nearcore::load_test_config;
use node_runtime::config::RuntimeConfig;
use testlib::runtime_utils::{alice_account, bob_account};

fn start_node() -> ThreadNode {
Expand Down Expand Up @@ -68,7 +69,7 @@ fn test_deliver_tx_error_log() {
let runtime_config_store = RuntimeConfigStore::new(None);
let runtime_config = runtime_config_store.get_config(PROTOCOL_VERSION);
let fee_helper = testlib::fees_utils::FeeHelper::new(
runtime_config.fees.clone(),
RuntimeConfig::clone(&runtime_config),
node.genesis().config.min_gas_price,
);
let signer =
Expand Down
7 changes: 3 additions & 4 deletions nearcore/tests/economics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ fn calc_total_supply(env: &mut TestEnv) -> u128 {
fn test_burn_mint() {
let genesis = build_genesis();
let mut env = setup_env(&genesis);
let transaction_costs = env.clients[0]
let config = env.clients[0]
.runtime_adapter
.get_protocol_config(&EpochId::default())
.unwrap()
.runtime_config
.fees;
let fee_helper = FeeHelper::new(transaction_costs, genesis.config.min_gas_price);
.runtime_config;
let fee_helper = FeeHelper::new(config, genesis.config.min_gas_price);
let signer = InMemorySigner::from_seed("test0".parse().unwrap(), KeyType::ED25519, "test0");
let initial_total_supply = env.chain_genesis.total_supply;
let genesis_hash = *env.clients[0].chain.genesis().hash();
Expand Down
4 changes: 2 additions & 2 deletions runtime/runtime/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ pub(crate) fn apply_delegate_action(
// Therefore Relayer should verify DelegateAction before submitting it because it spends the attached deposit.

let prepaid_send_fees = total_prepaid_send_fees(
&apply_state.config.fees,
&apply_state.config,
&action_receipt.actions,
apply_state.current_protocol_version,
)?;
Expand All @@ -709,7 +709,7 @@ fn receipt_required_gas(apply_state: &ApplyState, receipt: &Receipt) -> Result<G
ReceiptEnum::Action(action_receipt) => {
let mut required_gas = safe_add_gas(
total_prepaid_exec_fees(
&apply_state.config.fees,
&apply_state.config,
&action_receipt.actions,
&receipt.receiver_id,
apply_state.current_protocol_version,
Expand Down
54 changes: 25 additions & 29 deletions runtime/runtime/src/balance_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use near_primitives::errors::{
BalanceMismatchError, IntegerOverflowError, RuntimeError, StorageError,
};
use near_primitives::receipt::{Receipt, ReceiptEnum};
use near_primitives::runtime::fees::RuntimeFeesConfig;
use near_primitives::runtime::config::RuntimeConfig;
use near_primitives::transaction::SignedTransaction;
use near_primitives::trie_key::TrieKey;
use near_primitives::types::{AccountId, Balance};
Expand Down Expand Up @@ -37,7 +37,7 @@ fn get_delayed_receipts(

/// Calculates and returns cost of a receipt.
fn receipt_cost(
transaction_costs: &RuntimeFeesConfig,
config: &RuntimeConfig,
current_protocol_version: ProtocolVersion,
receipt: &Receipt,
) -> Result<Balance, IntegerOverflowError> {
Expand All @@ -46,9 +46,9 @@ fn receipt_cost(
let mut total_cost = total_deposit(&action_receipt.actions)?;
if !AccountId::is_system(&receipt.predecessor_id) {
let mut total_gas = safe_add_gas(
transaction_costs.fee(ActionCosts::new_action_receipt).exec_fee(),
config.fees.fee(ActionCosts::new_action_receipt).exec_fee(),
total_prepaid_exec_fees(
transaction_costs,
config,
&action_receipt.actions,
&receipt.receiver_id,
current_protocol_version,
Expand All @@ -58,7 +58,7 @@ fn receipt_cost(
total_gas = safe_add_gas(
total_gas,
total_prepaid_send_fees(
transaction_costs,
config,
&action_receipt.actions,
current_protocol_version,
)?,
Expand All @@ -74,12 +74,12 @@ fn receipt_cost(

/// Calculates and returns total cost of all the receipts.
fn total_receipts_cost(
transaction_costs: &RuntimeFeesConfig,
config: &RuntimeConfig,
current_protocol_version: ProtocolVersion,
receipts: &[Receipt],
) -> Result<Balance, IntegerOverflowError> {
receipts.iter().try_fold(0, |accumulator, receipt| {
let cost = receipt_cost(transaction_costs, current_protocol_version, receipt)?;
let cost = receipt_cost(config, current_protocol_version, receipt)?;
safe_add_balance(accumulator, cost)
})
}
Expand All @@ -101,22 +101,22 @@ fn total_accounts_balance(
/// Calculates and returns total costs of all the postponed receipts.
fn total_postponed_receipts_cost(
state: &dyn TrieAccess,
transaction_costs: &RuntimeFeesConfig,
config: &RuntimeConfig,
current_protocol_version: ProtocolVersion,
receipt_ids: &HashSet<(AccountId, crate::CryptoHash)>,
) -> Result<Balance, RuntimeError> {
receipt_ids.iter().try_fold(0, |total, item| {
let (account_id, receipt_id) = item;
let cost = match get_postponed_receipt(state, account_id, *receipt_id)? {
None => return Ok(total),
Some(receipt) => receipt_cost(transaction_costs, current_protocol_version, &receipt)?,
Some(receipt) => receipt_cost(config, current_protocol_version, &receipt)?,
};
safe_add_balance(total, cost).map_err(|_| RuntimeError::UnexpectedIntegerOverflow)
})
}

pub(crate) fn check_balance(
transaction_costs: &RuntimeFeesConfig,
config: &RuntimeConfig,
final_state: &TrieUpdate,
validator_accounts_update: &Option<ValidatorAccountsUpdate>,
incoming_receipts: &[Receipt],
Expand Down Expand Up @@ -173,7 +173,7 @@ pub(crate) fn check_balance(
let final_accounts_balance = total_accounts_balance(final_state, &all_accounts_ids)?;
// Receipts
let receipts_cost = |receipts: &[Receipt]| -> Result<Balance, IntegerOverflowError> {
total_receipts_cost(transaction_costs, current_protocol_version, receipts)
total_receipts_cost(config, current_protocol_version, receipts)
};
let incoming_receipts_balance = receipts_cost(incoming_receipts)?;
let outgoing_receipts_balance = receipts_cost(outgoing_receipts)?;
Expand Down Expand Up @@ -210,13 +210,13 @@ pub(crate) fn check_balance(

let initial_postponed_receipts_balance = total_postponed_receipts_cost(
initial_state,
transaction_costs,
config,
current_protocol_version,
&all_potential_postponed_receipt_ids,
)?;
let final_postponed_receipts_balance = total_postponed_receipts_cost(
final_state,
transaction_costs,
config,
current_protocol_version,
&all_potential_postponed_receipt_ids,
)?;
Expand Down Expand Up @@ -268,7 +268,6 @@ mod tests {
use near_crypto::{InMemorySigner, KeyType};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::receipt::ActionReceipt;
use near_primitives::runtime::fees::RuntimeFeesConfig;
use near_primitives::test_utils::account_new;
use near_primitives::transaction::{Action, TransferAction};
use near_primitives::types::{MerkleHash, StateChangeCause};
Expand All @@ -291,9 +290,8 @@ mod tests {
let tries = create_tries();
let root = MerkleHash::default();
let final_state = tries.new_trie_update(ShardUId::single_shard(), root);
let transaction_costs = RuntimeFeesConfig::test();
check_balance(
&transaction_costs,
&RuntimeConfig::test(),
&final_state,
&None,
&[],
Expand All @@ -310,9 +308,8 @@ mod tests {
let tries = create_tries();
let root = MerkleHash::default();
let final_state = tries.new_trie_update(ShardUId::single_shard(), root);
let transaction_costs = RuntimeFeesConfig::test();
let err = check_balance(
&transaction_costs,
&RuntimeConfig::test(),
&final_state,
&None,
&[Receipt::new_balance_refund(&alice_account(), 1000)],
Expand Down Expand Up @@ -371,9 +368,8 @@ mod tests {
},
);

let transaction_costs = RuntimeFeesConfig::test();
check_balance(
&transaction_costs,
&RuntimeConfig::test(),
&final_state,
&None,
&[Receipt::new_balance_refund(&account_id, refund_balance)],
Expand All @@ -392,13 +388,14 @@ mod tests {
let initial_balance = TESTING_INIT_BALANCE / 2;
let deposit = 500_000_000;
let gas_price = 100;
let cfg = RuntimeFeesConfig::test();
let exec_gas = cfg.fee(ActionCosts::new_action_receipt).exec_fee()
+ cfg.fee(ActionCosts::transfer).exec_fee();
let send_gas = cfg.fee(ActionCosts::new_action_receipt).send_fee(false)
+ cfg.fee(ActionCosts::transfer).send_fee(false);
let contract_reward = send_gas as u128 * *cfg.burnt_gas_reward.numer() as u128 * gas_price
/ (*cfg.burnt_gas_reward.denom() as u128);
let cfg = RuntimeConfig::test();
let fees = &cfg.fees;
let exec_gas = fees.fee(ActionCosts::new_action_receipt).exec_fee()
+ fees.fee(ActionCosts::transfer).exec_fee();
let send_gas = fees.fee(ActionCosts::new_action_receipt).send_fee(false)
+ fees.fee(ActionCosts::transfer).send_fee(false);
let contract_reward = send_gas as u128 * *fees.burnt_gas_reward.numer() as u128 * gas_price
/ (*fees.burnt_gas_reward.denom() as u128);
let total_validator_reward = send_gas as Balance * gas_price - contract_reward;

let final_state = prepare_state_change(
Expand Down Expand Up @@ -495,10 +492,9 @@ mod tests {
}),
};

let transaction_costs = RuntimeFeesConfig::test();
assert_eq!(
check_balance(
&transaction_costs,
&RuntimeConfig::test(),
&initial_state,
&None,
&[receipt],
Expand Down
Loading

0 comments on commit 09adb4e

Please sign in to comment.