From b95eedf81894d87802285cbd331515ee69a0f51f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 2 Apr 2021 14:50:25 +0300 Subject: [PATCH] fix: don't misinterprent alt_bn128_g1 costs as action costs (#4182) 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. --- core/primitives-core/src/config.rs | 12 ++++++++++-- runtime/near-vm-runner/src/tests/rs_contract.rs | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/primitives-core/src/config.rs b/core/primitives-core/src/config.rs index 8d85cf38051..58136b1d244 100644 --- a/core/primitives-core/src/config.rs +++ b/core/primitives-core/src/config.rs @@ -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. @@ -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 { @@ -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 { @@ -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 { diff --git a/runtime/near-vm-runner/src/tests/rs_contract.rs b/runtime/near-vm-runner/src/tests/rs_contract.rs index 31d43b5f5eb..edf9351b9c5 100644 --- a/runtime/near-vm-runner/src/tests/rs_contract.rs +++ b/runtime/near-vm-runner/src/tests/rs_contract.rs @@ -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, @@ -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); }