Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fees for transactions, receipts, actions. #1176

Merged
merged 9 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"core/primitives",
"core/store",
"runtime/runtime",
"runtime/near-runtime-fees",
MaksymZavershynskyi marked this conversation as resolved.
Show resolved Hide resolved
"runtime/near-vm-logic",
"runtime/near-vm-runner",
"runtime/near-vm-runner-standalone",
Expand Down
8 changes: 8 additions & 0 deletions runtime/near-runtime-fees/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "near-runtime-fees"
version = "0.1.0"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
125 changes: 125 additions & 0 deletions runtime/near-runtime-fees/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//! Describes the various costs incurred by creating receipts.
//! We use the following abbreviation for readability:
//! * sir -- sender is receiver. Receipts that are directed by an account to itself are guaranteed
//! to not be cross-shard which is cheaper than cross-shard. Conversely, when sender is not a
//! receiver it might or might not be a cross-shard communication.
use serde::{Deserialize, Serialize};
pub type Gas = u64;

/// Costs associated with an object that can only be sent over the network (and executed
/// by the receiver).
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct Fee {
/// Fee for sending an object from the sender to itself, guaranteeing that it does not leave
/// the shard.
send_sir: Gas,
/// Fee for sending an object potentially across the shards.
send_not_sir: Gas,
/// Fee for executing the object.
execution: Gas,
}

impl Fee {
pub fn send_fee(&self, sir: bool) -> Gas {
if sir {
self.send_sir
} else {
self.send_not_sir
}
}

pub fn exec_fee(&self) -> Gas {
self.execution
}
}

#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct RuntimeFeesConfig {
/// Describes the cost of creating an action receipt, `ActionReceipt`, excluding the actual cost
/// of actions.
pub action_receipt_creation_config: Fee,
/// Describes the cost of creating a data receipt, `DataReceipt`.
pub data_receipt_creation_config: DataReceiptCreationConfig,
/// Describes the cost of creating a certain action, `Action`. Includes all variants.
pub action_creation_config: ActionCreationConfig,
}

/// Describes the cost of creating a data receipt, `DataReceipt`.
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct DataReceiptCreationConfig {
/// Base cost of creating a data receipt.
pub base_cost: Fee,
/// Additional cost per byte sent.
pub cost_per_byte: Fee,
}

/// Describes the cost of creating a specific action, `Action`. Includes all variants.
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct ActionCreationConfig {
/// Base cost of creating an account.
pub create_account_cost: Fee,

/// Base cost of deploying a contract.
pub deploy_contract_cost: Fee,
/// Cost per byte of deploying a contract.
pub deploy_contract_cost_per_byte: Fee,

/// Base cost of calling a function.
pub function_call_cost: Fee,
/// Cost per byte of method name and arguments of calling a function.
pub function_call_cost_per_byte: Fee,

/// Base cost of making a transfer.
pub transfer_cost: Fee,

/// Base cost of staking.
pub stake_cost: Fee,

/// Base cost of adding a key.
pub add_key_cost: AccessKeyCreationConfig,

/// Base cost of deleting a key.
pub delete_key_cost: Fee,

/// Base cost of deleting an account.
pub delete_account_cost: Fee,
MaksymZavershynskyi marked this conversation as resolved.
Show resolved Hide resolved
}

/// Describes the cost of creating an access key.
#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
pub struct AccessKeyCreationConfig {
/// Base cost of creating a full access access-key.
pub full_access_cost: Fee,
/// Base cost of creating an access-key restricted to specific functions.
pub function_call_cost: Fee,
/// Cost per byte of method_names of creating a restricted access-key.
pub function_call_cost_per_byte: Fee,
}

impl Default for RuntimeFeesConfig {
fn default() -> Self {
Self {
action_receipt_creation_config: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
data_receipt_creation_config: DataReceiptCreationConfig {
base_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
cost_per_byte: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
},
action_creation_config: ActionCreationConfig {
create_account_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
deploy_contract_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
deploy_contract_cost_per_byte: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
function_call_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
function_call_cost_per_byte: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
transfer_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
stake_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
add_key_cost: AccessKeyCreationConfig {
full_access_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
function_call_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
function_call_cost_per_byte: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
},
delete_key_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
delete_account_cost: Fee { send_sir: 1, send_not_sir: 1, execution: 1 },
},
}
}
}
1 change: 1 addition & 0 deletions runtime/near-vm-logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
bs58 = "0.2.2"
exonum_sodiumoxide = "0.0.20"
serde = { version = "1.0", features = ["derive"] }
near-runtime-fees = { path = "../near-runtime-fees" }

[[test]]
name = "test_registers"
Expand Down
5 changes: 5 additions & 0 deletions runtime/near-vm-logic/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::types::Gas;
use near_runtime_fees::RuntimeFeesConfig;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

#[derive(Clone, Debug, Hash, Serialize, Deserialize)]
pub struct Config {
/// Fees for creating actions on runtime.
pub runtime_fees: RuntimeFeesConfig,

/// Gas cost of a growing memory by single page.
pub grow_mem_cost: u32,
/// Gas cost of a regular operation.
Expand Down Expand Up @@ -53,6 +57,7 @@ impl Default for Config {
max_number_registers: 100,
max_number_logs: 100,
max_log_len: 500,
runtime_fees: Default::default(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/near-vm-logic/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ pub struct VMContext {
pub random_seed: Vec<u8>,
/// Whether the execution should not charge any costs.
pub free_of_charge: bool,
/// How many `DataReceipt`'s should receive this execution result. This should be empty if
/// this function call is a part of a batch and it is not the last action.
pub output_data_receivers: Vec<AccountId>,
}
Loading