Skip to content

Commit

Permalink
Merge branch 'master' into refcount_map
Browse files Browse the repository at this point in the history
  • Loading branch information
Kouprin authored Jul 4, 2020
2 parents 478da5c + 0b65f75 commit a48596a
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 118 deletions.
4 changes: 2 additions & 2 deletions chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2733,8 +2733,6 @@ mod tests {

use near_crypto::KeyType;
use near_primitives::block::{Block, Tip};
#[cfg(feature = "expensive_tests")]
use near_primitives::epoch_manager::BlockInfo;
use near_primitives::errors::InvalidTxError;
use near_primitives::hash::hash;
use near_primitives::types::{BlockHeight, EpochId, GCCount, NumBlocks};
Expand All @@ -2746,6 +2744,8 @@ mod tests {
use crate::test_utils::KeyValueRuntime;
use crate::{Chain, ChainGenesis, DoomslugThresholdMode};

#[cfg(feature = "expensive_tests")]
use near_primitives::epoch_manager::BlockInfo;
use near_store::DBCol;
#[cfg(feature = "expensive_tests")]
use {crate::store_validator::StoreValidator, near_chain_configs::GenesisConfig};
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ pub const DB_VERSION: DbVersion = 4;
pub type ProtocolVersion = u32;

/// Current latest version of the protocol.
pub const PROTOCOL_VERSION: ProtocolVersion = 28;
pub const PROTOCOL_VERSION: ProtocolVersion = 29;

pub const FIRST_BACKWARD_COMPATIBLE_PROTOCOL_VERSION: ProtocolVersion = PROTOCOL_VERSION;
82 changes: 42 additions & 40 deletions neard/res/genesis_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"protocol_version": 28,
"protocol_version": 29,
"genesis_time": "1970-01-01T00:00:00.000000000Z",
"chain_id": "sample",
"genesis_height": 0,
Expand Down Expand Up @@ -133,39 +133,41 @@
},
"wasm_config": {
"ext_costs": {
"base": 265261758,
"read_memory_base": 2584050225,
"read_memory_byte": 3801396,
"write_memory_base": 2780731725,
"write_memory_byte": 2723859,
"read_register_base": 2493624561,
"read_register_byte": 98622,
"write_register_base": 2840975211,
"write_register_byte": 3801645,
"utf8_decoding_base": 3110963061,
"utf8_decoding_byte": 289342653,
"utf16_decoding_base": 3593689800,
"utf16_decoding_byte": 167519322,
"sha256_base": 4530046500,
"sha256_byte": 24116301,
"keccak256_base": 5867223186,
"keccak256_byte": 21469644,
"keccak512_base": 5798128650,
"keccak512_byte": 36651981,
"log_base": 2408221236,
"log_byte": 15863835,
"storage_write_base": 45187219125,
"storage_write_key_byte": 66445653,
"storage_write_value_byte": 29682120,
"storage_write_evicted_byte": 28939782,
"storage_read_base": 32029296375,
"storage_read_key_byte": 28463997,
"storage_read_value_byte": 3289884,
"storage_remove_base": 35876668875,
"storage_remove_key_byte": 35342424,
"storage_remove_ret_value_byte": 7303842,
"storage_has_key_base": 31315025250,
"storage_has_key_byte": 28376217,
"base": 264768111,
"contract_compile_base": 35445963,
"contract_compile_bytes": 216750,
"read_memory_base": 2609863200,
"read_memory_byte": 3801333,
"write_memory_base": 2803794861,
"write_memory_byte": 2723772,
"read_register_base": 2517165186,
"read_register_byte": 98562,
"write_register_base": 2865522486,
"write_register_byte": 3801564,
"utf8_decoding_base": 3111779061,
"utf8_decoding_byte": 291580479,
"utf16_decoding_base": 3543313050,
"utf16_decoding_byte": 163577493,
"sha256_base": 4540970250,
"sha256_byte": 24117351,
"keccak256_base": 5879491275,
"keccak256_byte": 21471105,
"keccak512_base": 5811388236,
"keccak512_byte": 36649701,
"log_base": 3543313050,
"log_byte": 13198791,
"storage_write_base": 64196736000,
"storage_write_key_byte": 70482867,
"storage_write_value_byte": 31018539,
"storage_write_evicted_byte": 32117307,
"storage_read_base": 56356845750,
"storage_read_key_byte": 30952533,
"storage_read_value_byte": 5611005,
"storage_remove_base": 53473030500,
"storage_remove_key_byte": 38220384,
"storage_remove_ret_value_byte": 11531556,
"storage_has_key_base": 54039896625,
"storage_has_key_byte": 30790845,
"storage_iter_create_prefix_base": 0,
"storage_iter_create_prefix_byte": 0,
"storage_iter_create_range_base": 0,
Expand All @@ -174,12 +176,12 @@
"storage_iter_next_base": 0,
"storage_iter_next_key_byte": 0,
"storage_iter_next_value_byte": 0,
"touching_trie_node": 5764118571,
"promise_and_base": 1473816795,
"promise_and_per_promise": 5613432,
"promise_return": 558292404,
"validator_stake_base": 303944908800,
"validator_total_stake_base": 303944908800
"touching_trie_node": 16101955926,
"promise_and_base": 1465013400,
"promise_and_per_promise": 5452176,
"promise_return": 560152386,
"validator_stake_base": 911834726400,
"validator_total_stake_base": 911834726400
},
"grow_mem_cost": 1,
"regular_op_cost": 3856371,
Expand Down
107 changes: 60 additions & 47 deletions runtime/near-vm-logic/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ pub struct ExtCostsConfig {
/// Base cost for calling a host function.
pub base: Gas,

/// Base cost of loading and compiling contract
pub contract_compile_base: Gas,
/// Cost of the execution to load and compile contract
pub contract_compile_bytes: Gas,

/// Base cost for guest memory read
pub read_memory_base: Gas,
/// Cost for guest memory read
Expand Down Expand Up @@ -317,53 +322,55 @@ const SAFETY_MULTIPLIER: u64 = 3;
impl Default for ExtCostsConfig {
fn default() -> ExtCostsConfig {
ExtCostsConfig {
base: SAFETY_MULTIPLIER * 88420586,
read_memory_base: SAFETY_MULTIPLIER * 861350075,
read_memory_byte: SAFETY_MULTIPLIER * 1267132,
write_memory_base: SAFETY_MULTIPLIER * 926910575,
write_memory_byte: SAFETY_MULTIPLIER * 907953,
read_register_base: SAFETY_MULTIPLIER * 831208187,
read_register_byte: SAFETY_MULTIPLIER * 32874,
write_register_base: SAFETY_MULTIPLIER * 946991737,
write_register_byte: SAFETY_MULTIPLIER * 1267215,
utf8_decoding_base: SAFETY_MULTIPLIER * 1036987687,
utf8_decoding_byte: SAFETY_MULTIPLIER * 96447551,
utf16_decoding_base: SAFETY_MULTIPLIER * 1197896600,
utf16_decoding_byte: SAFETY_MULTIPLIER * 55839774,
sha256_base: SAFETY_MULTIPLIER * 1510015500,
sha256_byte: SAFETY_MULTIPLIER * 8038767,
keccak256_base: SAFETY_MULTIPLIER * 1955741062,
keccak256_byte: SAFETY_MULTIPLIER * 7156548,
keccak512_base: SAFETY_MULTIPLIER * 1932709550,
keccak512_byte: SAFETY_MULTIPLIER * 12217327,
log_base: SAFETY_MULTIPLIER * 802740412,
log_byte: SAFETY_MULTIPLIER * 5287945,
storage_write_base: SAFETY_MULTIPLIER * 15062406375,
storage_write_key_byte: SAFETY_MULTIPLIER * 22148551,
storage_write_value_byte: SAFETY_MULTIPLIER * 9894040,
storage_write_evicted_byte: SAFETY_MULTIPLIER * 9646594,
storage_read_base: SAFETY_MULTIPLIER * 10676432125,
storage_read_key_byte: SAFETY_MULTIPLIER * 9487999,
storage_read_value_byte: SAFETY_MULTIPLIER * 1096628,
storage_remove_base: SAFETY_MULTIPLIER * 11958889625,
storage_remove_key_byte: SAFETY_MULTIPLIER * 11780808,
storage_remove_ret_value_byte: SAFETY_MULTIPLIER * 2434614,
storage_has_key_base: SAFETY_MULTIPLIER * 10438341750,
storage_has_key_byte: SAFETY_MULTIPLIER * 9458739,
storage_iter_create_prefix_base: 0,
storage_iter_create_prefix_byte: 0,
storage_iter_create_range_base: 0,
storage_iter_create_from_byte: 0,
storage_iter_create_to_byte: 0,
storage_iter_next_base: 0,
storage_iter_next_key_byte: 0,
storage_iter_next_value_byte: 0,
touching_trie_node: SAFETY_MULTIPLIER * 1921372857,
promise_and_base: SAFETY_MULTIPLIER * 491272265,
promise_and_per_promise: SAFETY_MULTIPLIER * 1871144,
promise_return: SAFETY_MULTIPLIER * 186097468,
validator_stake_base: SAFETY_MULTIPLIER * 9894040 * 1024 * 10,
validator_total_stake_base: SAFETY_MULTIPLIER * 9894040 * 1024 * 10,
base: SAFETY_MULTIPLIER * 88256037,
contract_compile_base: SAFETY_MULTIPLIER * 11815321,
contract_compile_bytes: SAFETY_MULTIPLIER * 72250,
read_memory_base: SAFETY_MULTIPLIER * 869954400,
read_memory_byte: SAFETY_MULTIPLIER * 1267111,
write_memory_base: SAFETY_MULTIPLIER * 934598287,
write_memory_byte: SAFETY_MULTIPLIER * 907924,
read_register_base: SAFETY_MULTIPLIER * 839055062,
read_register_byte: SAFETY_MULTIPLIER * 32854,
write_register_base: SAFETY_MULTIPLIER * 955174162,
write_register_byte: SAFETY_MULTIPLIER * 1267188,
utf8_decoding_base: SAFETY_MULTIPLIER * 1037259687,
utf8_decoding_byte: SAFETY_MULTIPLIER * 97193493,
utf16_decoding_base: SAFETY_MULTIPLIER * 1181104350,
utf16_decoding_byte: SAFETY_MULTIPLIER * 54525831,
sha256_base: SAFETY_MULTIPLIER * 1513656750,
sha256_byte: SAFETY_MULTIPLIER * 8039117,
keccak256_base: SAFETY_MULTIPLIER * 1959830425,
keccak256_byte: SAFETY_MULTIPLIER * 7157035,
keccak512_base: SAFETY_MULTIPLIER * 1937129412,
keccak512_byte: SAFETY_MULTIPLIER * 12216567,
log_base: SAFETY_MULTIPLIER * 1181104350,
log_byte: SAFETY_MULTIPLIER * 4399597,
storage_write_base: SAFETY_MULTIPLIER * 21398912000,
storage_write_key_byte: SAFETY_MULTIPLIER * 23494289,
storage_write_value_byte: SAFETY_MULTIPLIER * 10339513,
storage_write_evicted_byte: SAFETY_MULTIPLIER * 10705769,
storage_read_base: SAFETY_MULTIPLIER * 18785615250,
storage_read_key_byte: SAFETY_MULTIPLIER * 10317511,
storage_read_value_byte: SAFETY_MULTIPLIER * 1870335,
storage_remove_base: SAFETY_MULTIPLIER * 17824343500,
storage_remove_key_byte: SAFETY_MULTIPLIER * 12740128,
storage_remove_ret_value_byte: SAFETY_MULTIPLIER * 3843852,
storage_has_key_base: SAFETY_MULTIPLIER * 18013298875,
storage_has_key_byte: SAFETY_MULTIPLIER * 10263615,
storage_iter_create_prefix_base: SAFETY_MULTIPLIER * 0,
storage_iter_create_prefix_byte: SAFETY_MULTIPLIER * 0,
storage_iter_create_range_base: SAFETY_MULTIPLIER * 0,
storage_iter_create_from_byte: SAFETY_MULTIPLIER * 0,
storage_iter_create_to_byte: SAFETY_MULTIPLIER * 0,
storage_iter_next_base: SAFETY_MULTIPLIER * 0,
storage_iter_next_key_byte: SAFETY_MULTIPLIER * 0,
storage_iter_next_value_byte: SAFETY_MULTIPLIER * 0,
touching_trie_node: SAFETY_MULTIPLIER * 5367318642,
promise_and_base: SAFETY_MULTIPLIER * 488337800,
promise_and_per_promise: SAFETY_MULTIPLIER * 1817392,
promise_return: SAFETY_MULTIPLIER * 186717462,
validator_stake_base: SAFETY_MULTIPLIER * 303944908800,
validator_total_stake_base: SAFETY_MULTIPLIER * 303944908800,
}
}
}
Expand All @@ -372,6 +379,8 @@ impl ExtCostsConfig {
fn free() -> ExtCostsConfig {
ExtCostsConfig {
base: 0,
contract_compile_base: 0,
contract_compile_bytes: 0,
read_memory_base: 0,
read_memory_byte: 0,
write_memory_base: 0,
Expand Down Expand Up @@ -427,6 +436,8 @@ impl ExtCostsConfig {
#[allow(non_camel_case_types)]
pub enum ExtCosts {
base,
contract_compile_base,
contract_compile_bytes,
read_memory_base,
read_memory_byte,
write_memory_base,
Expand Down Expand Up @@ -480,6 +491,8 @@ impl ExtCosts {
use ExtCosts::*;
match self {
base => config.base,
contract_compile_base => config.contract_compile_base,
contract_compile_bytes => config.contract_compile_bytes,
read_memory_base => config.read_memory_base,
read_memory_byte => config.read_memory_byte,
write_memory_base => config.write_memory_base,
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-logic/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,11 @@ impl<'a> VMLogic<'a> {
logs,
}
}

pub fn add_contract_compile_fee(&mut self, code_len: u64) -> Result<()> {
self.gas_counter.pay_per_byte(contract_compile_bytes, code_len)?;
self.gas_counter.pay_base(contract_compile_base)
}
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
Expand Down
12 changes: 6 additions & 6 deletions runtime/near-vm-logic/tests/test_promises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn test_promise_batch_action_create_account() {
logic
.promise_batch_action_create_account(index)
.expect("should add an action to create account");
assert_eq!(logic.used_gas().unwrap(), 5077341969440);
assert_eq!(logic.used_gas().unwrap(), 5077478438564);
let expected = serde_json::json!([
{
"receipt_indices": [],
Expand Down Expand Up @@ -127,7 +127,7 @@ fn test_promise_batch_action_deploy_contract() {
logic
.promise_batch_action_deploy_contract(index, code.len() as u64, code.as_ptr() as _)
.expect("should add an action to deploy contract");
assert_eq!(logic.used_gas().unwrap(), 5255561051231);
assert_eq!(logic.used_gas().unwrap(), 5255774958146);
let expected = serde_json::json!(
[
{
Expand Down Expand Up @@ -183,7 +183,7 @@ fn test_promise_batch_action_transfer() {
logic
.promise_batch_action_transfer(index, 1u128.to_le_bytes().as_ptr() as _)
.expect_err("not enough money");
assert_eq!(logic.used_gas().unwrap(), 5349464221442);
assert_eq!(logic.used_gas().unwrap(), 5349703444787);
let expected = serde_json::json!(
[
{
Expand Down Expand Up @@ -250,7 +250,7 @@ fn test_promise_batch_action_stake() {
key.as_ptr() as _,
)
.expect("should add an action to stake");
assert_eq!(logic.used_gas().unwrap(), 5138340318074);
assert_eq!(logic.used_gas().unwrap(), 5138631652196);
let expected = serde_json::json!([
{
"receipt_indices": [],
Expand Down Expand Up @@ -327,7 +327,7 @@ fn test_promise_batch_action_add_key_with_function_call() {
method_names,
)
.expect("should add allowance");
assert_eq!(logic.used_gas().unwrap(), 5126428377160);
assert_eq!(logic.used_gas().unwrap(), 5126897175676);
let expected = serde_json::json!(
[
{
Expand Down Expand Up @@ -386,7 +386,7 @@ fn test_promise_batch_then() {
logic
.promise_batch_then(index, account_id.len() as u64, account_id.as_ptr() as _)
.expect("promise batch should run ok");
assert_eq!(logic.used_gas().unwrap(), 24124722826121);
assert_eq!(logic.used_gas().unwrap(), 24124999601771);
let expected = serde_json::json!([
{
"receipt_indices": [],
Expand Down
3 changes: 2 additions & 1 deletion runtime/near-vm-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ mod cache;
mod errors;
mod imports;
mod memory;
mod prepare;
pub mod prepare;
mod runner;
mod wasmer_runner;
#[cfg(feature = "wasmtime_vm")]
mod wasmtime_runner;
pub use near_vm_errors::VMError;
pub use runner::compile_module;
pub use runner::run;
pub use runner::run_vm;
pub use runner::with_vm_variants;
Expand Down
20 changes: 20 additions & 0 deletions runtime/near-vm-runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use near_vm_logic::{External, VMConfig, VMContext, VMKind, VMOutcome};
/// `run` does the following:
/// - deserializes and validate the `code` binary (see `prepare::prepare_contract`)
/// - injects gas counting into
/// - adds fee to VMLogic's GasCounter for size of contract
/// - instantiates (links) `VMLogic` externs with the imports of the binary
/// - calls the `method_name` with `context.input`
/// - updates `ext` with new receipts, created during the execution
Expand Down Expand Up @@ -84,3 +85,22 @@ pub fn with_vm_variants(runner: fn(VMKind) -> ()) {
#[cfg(feature = "wasmtime_vm")]
runner(VMKind::Wasmtime);
}

/// Used for testing cost of compiling a module
pub fn compile_module(vm_kind: VMKind, code: &Vec<u8>) {
match vm_kind {
VMKind::Wasmer => {
use crate::wasmer_runner::compile_module;
compile_module(code);
}
#[cfg(feature = "wasmtime_vm")]
VMKind::Wasmtime => {
use crate::wasmtime_runner::compile_module;
compile_module(code);
}
#[cfg(not(feature = "wasmtime_vm"))]
VMKind::Wasmtime => {
panic!("Wasmtime is not supported, compile with '--features wasmtime_vm'")
}
}
}
Loading

0 comments on commit a48596a

Please sign in to comment.