Skip to content

Commit

Permalink
fix: don't misinterprent alt_bn128_g1 costs as action costs (#4182)
Browse files Browse the repository at this point in the history
Before this PR, we store costs for `alt_bn128_g1` and costs of certain actions in the same array index, becaues, when adding alt_bn128_g1, we forgot to update the number of costs. The fix is to make this mistake impossible, by including an explicit sentinel node at the end.


Test plan
---------

Add sanity check to existing ext tests that they don't increase action costs.
  • Loading branch information
matklad authored Apr 2, 2021
1 parent f00da46 commit b95eedf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 10 additions & 2 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@ pub enum ExtCosts {
alt_bn128_g1_sum_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_byte,

// NOTE: this should be the last element of the enum.
__count,
}

// Type of an action, used in fees logic.
Expand All @@ -552,6 +555,9 @@ pub enum ActionCosts {
delete_key,
value_return,
new_receipt,

// NOTE: this should be the last element of the enum.
__count,
}

impl fmt::Display for ActionCosts {
Expand All @@ -562,7 +568,7 @@ impl fmt::Display for ActionCosts {

impl ActionCosts {
pub const fn count() -> usize {
ActionCosts::new_receipt as usize + 1
ActionCosts::__count as usize
}

pub fn name_of(index: usize) -> &'static str {
Expand Down Expand Up @@ -654,11 +660,13 @@ impl ExtCosts {
alt_bn128_g1_sum_base => config.alt_bn128_g1_sum_base,
#[cfg(feature = "protocol_feature_alt_bn128")]
alt_bn128_g1_sum_byte => config.alt_bn128_g1_sum_byte,

__count => unreachable!(),
}
}

pub const fn count() -> usize {
ExtCosts::validator_total_stake_base as usize + 1
ExtCosts::__count as usize
}

pub fn name_of(index: usize) -> &'static str {
Expand Down
5 changes: 4 additions & 1 deletion runtime/near-vm-runner/src/tests/rs_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ fn run_test_ext(
let fees = RuntimeFeesConfig::default();
let context = create_context(input.to_vec());

let profile = ProfileData::new_enabled();
let (outcome, err) = run_vm(
&code,
&method,
Expand All @@ -145,9 +146,11 @@ fn run_test_ext(
vm_kind,
LATEST_PROTOCOL_VERSION,
None,
ProfileData::new_disabled(),
profile.clone(),
);

assert_eq!(profile.action_gas(), 0);

if let Some(_) = err {
panic!("Failed execution: {:?}", err);
}
Expand Down

0 comments on commit b95eedf

Please sign in to comment.