Skip to content

Commit

Permalink
Introduce eth_implicit_accounts flag to runtime config
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Nov 24, 2023
1 parent 6c2ad38 commit b7c3142
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 55 deletions.
2 changes: 1 addition & 1 deletion chain/chain/src/tests/simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn build_chain() {

let hash = chain.head().unwrap().last_block_hash;
if cfg!(feature = "nightly") {
insta::assert_display_snapshot!(hash, @"3iXi6BshQaPx9TsbDt5itAXUjnTQz9AR9pg2w349TFNj");
insta::assert_display_snapshot!(hash, @"Dn18HUFm149fojXpwV1dYCfjdPh56S1k233kp7vmnFeE");
} else {
insta::assert_display_snapshot!(hash, @"HbQVGVZ3WGxsNqeM3GfSwDoxwYZ2RBP1SinAze9SYR3C");
}
Expand Down
1 change: 1 addition & 0 deletions core/primitives-core/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub enum Parameter {
AltBn128,
FunctionCallWeight,
VmKind,
EthImplicitAccounts,
}

#[derive(
Expand Down
45 changes: 21 additions & 24 deletions core/primitives-core/src/runtime/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
//! * 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 crate::checked_feature;
use crate::config::ActionCosts;
use crate::num_rational::Rational32;
use crate::types::{Balance, Gas, ProtocolVersion};
use crate::types::{Balance, Gas};
use enum_map::EnumMap;
use near_account_id::AccountType;

Expand Down Expand Up @@ -210,25 +209,24 @@ impl StorageUsageConfig {
pub fn transfer_exec_fee(
cfg: &RuntimeFeesConfig,
implicit_account_creation_allowed: bool,
eth_implicit_accounts_enabled: bool,
receiver_account_type: AccountType,
protocol_version: ProtocolVersion,
) -> Gas {
let transfer_fee = cfg.fee(ActionCosts::transfer).exec_fee();
match (implicit_account_creation_allowed, receiver_account_type) {
match (implicit_account_creation_allowed, eth_implicit_accounts_enabled, receiver_account_type)
{
// Regular transfer to a named account.
(_, AccountType::NamedAccount) => transfer_fee,
(_, _, AccountType::NamedAccount) => transfer_fee,
// No account will be created, just a regular transfer.
(false, _) => transfer_fee,
(false, _, _) => transfer_fee,
// No account will be created, just a regular transfer.
(true, false, AccountType::EthImplicitAccount) => transfer_fee,
// Extra fee for the CreateAccount.
(true, AccountType::EthImplicitAccount) => {
if checked_feature!("stable", EthImplicit, protocol_version) {
transfer_fee + cfg.fee(ActionCosts::create_account).exec_fee()
} else {
transfer_fee
}
(true, true, AccountType::EthImplicitAccount) => {
transfer_fee + cfg.fee(ActionCosts::create_account).exec_fee()
}
// Extra fees for the CreateAccount and AddFullAccessKey.
(true, AccountType::NearImplicitAccount) => {
(true, _, AccountType::NearImplicitAccount) => {
transfer_fee
+ cfg.fee(ActionCosts::create_account).exec_fee()
+ cfg.fee(ActionCosts::add_full_access_key).exec_fee()
Expand All @@ -240,25 +238,24 @@ pub fn transfer_send_fee(
cfg: &RuntimeFeesConfig,
sender_is_receiver: bool,
implicit_account_creation_allowed: bool,
eth_implicit_accounts_enabled: bool,
receiver_account_type: AccountType,
protocol_version: ProtocolVersion,
) -> Gas {
let transfer_fee = cfg.fee(ActionCosts::transfer).send_fee(sender_is_receiver);
match (implicit_account_creation_allowed, receiver_account_type) {
match (implicit_account_creation_allowed, eth_implicit_accounts_enabled, receiver_account_type)
{
// Regular transfer to a named account.
(_, AccountType::NamedAccount) => transfer_fee,
(_, _, AccountType::NamedAccount) => transfer_fee,
// No account will be created, just a regular transfer.
(false, _, _) => transfer_fee,
// No account will be created, just a regular transfer.
(false, _) => transfer_fee,
(true, false, AccountType::EthImplicitAccount) => transfer_fee,
// Extra fee for the CreateAccount.
(true, AccountType::EthImplicitAccount) => {
if checked_feature!("stable", EthImplicit, protocol_version) {
transfer_fee + cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver)
} else {
transfer_fee
}
(true, true, AccountType::EthImplicitAccount) => {
transfer_fee + cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver)
}
// Extra fees for the CreateAccount and AddFullAccessKey.
(true, AccountType::NearImplicitAccount) => {
(true, _, AccountType::NearImplicitAccount) => {
transfer_fee
+ cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver)
+ cfg.fee(ActionCosts::add_full_access_key).send_fee(sender_is_receiver)
Expand Down
4 changes: 2 additions & 2 deletions core/primitives-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub enum ProtocolFeature {
/// NEP: https://github.com/near/NEPs/pull/509
#[cfg(feature = "protocol_feature_chunk_validation")]
ChunkValidation,
EthImplicit,
EthImplicitAccounts,
}

impl ProtocolFeature {
Expand Down Expand Up @@ -184,7 +184,7 @@ impl ProtocolFeature {
ProtocolFeature::SimpleNightshadeV2 => 135,
#[cfg(feature = "protocol_feature_chunk_validation")]
ProtocolFeature::ChunkValidation => 137,
ProtocolFeature::EthImplicit => 138,
ProtocolFeature::EthImplicitAccounts => 138,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/138.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eth_implicit_accounts: { old: false, new: true }
1 change: 1 addition & 0 deletions core/primitives/res/runtime_configs/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,4 @@ ed25519_verify: false
alt_bn128: false
function_call_weight: false
vm_kind: Wasmer0
eth_implicit_accounts: false
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,4 @@ ed25519_verify: false
alt_bn128: false
function_call_weight: false
vm_kind: Wasmer0
eth_implicit_accounts: false
2 changes: 2 additions & 0 deletions core/primitives/src/runtime/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ static CONFIG_DIFFS: &[(ProtocolVersion, &str)] = &[
(63, include_config!("63.yaml")),
(64, include_config!("64.yaml")),
(129, include_config!("129.yaml")),
// Introduce ETH-implicit accounts.
(138, include_config!("138.yaml")),
];

/// Testnet parameters for versions <= 29, which (incorrectly) differed from mainnet parameters
Expand Down
1 change: 1 addition & 0 deletions core/primitives/src/runtime/parameter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ impl TryFrom<&ParameterTable> for RuntimeConfig {
ed25519_verify: params.get(Parameter::Ed25519Verify)?,
alt_bn128: params.get(Parameter::AltBn128)?,
function_call_weight: params.get(Parameter::FunctionCallWeight)?,
eth_implicit_accounts: params.get(Parameter::EthImplicitAccounts)?,
},
account_creation_config: AccountCreationConfig {
min_allowed_top_level_account_length: params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ expression: "&view"
"ed25519_verify": true,
"alt_bn128": true,
"function_call_weight": true,
"eth_implicit_accounts": true,
"limit_config": {
"max_gas_burnt": 300000000000000,
"max_stack_height": 262144,
Expand Down
9 changes: 4 additions & 5 deletions core/primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use serde;

use crate::checked_feature;
use crate::hash::{hash, CryptoHash};
use crate::receipt::Receipt;
use crate::transaction::SignedTransaction;
Expand Down Expand Up @@ -472,17 +471,17 @@ where
Serializable(object)
}

// TODO(eth-implicit) Replace this function (and wat dependency) with real Wallet Contract implementation.
// TODO(eth-implicit) Replace this function (and wat dependency) with a real Wallet Contract implementation.
pub fn wallet_contract_placeholder() -> ContractCode {
let code = wat::parse_str(r#"(module (func (export "main")))"#);
ContractCode::new(code.unwrap().to_vec(), None)
}

/// From `near-account-id` version `1.0.0-alpha.2`, `is_implicit` returns true for ETH-implicit accounts.
/// This function is a wrapper for `is_implicit` method so that we can easily differentiate its behavior
/// based on the protocol version.
pub fn account_is_implicit(account_id: &AccountId, protocol_version: ProtocolVersion) -> bool {
if checked_feature!("stable", EthImplicit, protocol_version) {
/// based on whether ETH-implicit accounts are enabled.
pub fn account_is_implicit(account_id: &AccountId, eth_implicit_accounts_enabled: bool) -> bool {
if eth_implicit_accounts_enabled {
account_id.get_account_type().is_implicit()
} else {
account_id.get_account_type() == AccountType::NearImplicitAccount
Expand Down
4 changes: 4 additions & 0 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,8 @@ pub struct VMConfigView {
pub alt_bn128: bool,
/// See [`VMConfig::function_call_weight`].
pub function_call_weight: bool,
/// See [`VMConfig::eth_implicit_accounts`].
pub eth_implicit_accounts: bool,

/// Describes limits for VM and Runtime.
///
Expand All @@ -2585,6 +2587,7 @@ impl From<near_vm_runner::logic::Config> for VMConfigView {
alt_bn128: config.alt_bn128,
function_call_weight: config.function_call_weight,
vm_kind: config.vm_kind,
eth_implicit_accounts: config.eth_implicit_accounts,
}
}
}
Expand All @@ -2605,6 +2608,7 @@ impl From<VMConfigView> for near_vm_runner::logic::Config {
alt_bn128: view.alt_bn128,
function_call_weight: view.function_call_weight,
vm_kind: view.vm_kind,
eth_implicit_accounts: view.eth_implicit_accounts,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn test_transaction_hash_collision_for_near_implicit_account_ok() {
/// Test that transactions from ETH-implicit accounts are rejected.
#[test]
fn test_transaction_from_eth_implicit_account_fail() {
if !checked_feature!("stable", EthImplicit, PROTOCOL_VERSION) {
if !checked_feature!("stable", EthImplicitAccounts, PROTOCOL_VERSION) {
return;
}
let genesis = Genesis::test(vec!["test0".parse().unwrap(), "test1".parse().unwrap()], 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn check_meta_tx_execution(
let user_pubk = match sender.get_account_type() {
AccountType::NearImplicitAccount => PublicKey::from_near_implicit_account(&sender).unwrap(),
AccountType::EthImplicitAccount => {
if checked_feature!("stable", EthImplicit, protocol_version) {
if checked_feature!("stable", EthImplicitAccounts, protocol_version) {
// ETH-implicit accounts must not have access key.
panic!("No access keys");
} else {
Expand Down Expand Up @@ -816,7 +816,7 @@ fn meta_tx_create_near_implicit_account_fails() {

#[test]
fn meta_tx_create_eth_implicit_account_fails() {
if !checked_feature!("stable", EthImplicit, PROTOCOL_VERSION) {
if !checked_feature!("stable", EthImplicitAccounts, PROTOCOL_VERSION) {
return;
}
meta_tx_create_implicit_account_fails(eth_implicit_test_account());
Expand Down Expand Up @@ -863,7 +863,7 @@ fn meta_tx_create_and_use_near_implicit_account() {

#[test]
fn meta_tx_create_and_use_eth_implicit_account() {
if !checked_feature!("stable", EthImplicit, PROTOCOL_VERSION) {
if !checked_feature!("stable", EthImplicitAccounts, PROTOCOL_VERSION) {
return;
}
meta_tx_create_and_use_implicit_account(eth_implicit_test_account());
Expand Down Expand Up @@ -941,7 +941,7 @@ fn meta_tx_create_near_implicit_account() {

#[test]
fn meta_tx_create_eth_implicit_account() {
if !checked_feature!("stable", EthImplicit, PROTOCOL_VERSION) {
if !checked_feature!("stable", EthImplicitAccounts, PROTOCOL_VERSION) {
return;
}
meta_tx_create_implicit_account(eth_implicit_test_account());
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/standard_cases/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn test_transfer_tokens_near_implicit_account_runtime() {

#[test]
fn test_transfer_tokens_eth_implicit_account_runtime() {
if !checked_feature!("stable", EthImplicit, PROTOCOL_VERSION) {
if !checked_feature!("stable", EthImplicitAccounts, PROTOCOL_VERSION) {
return;
}
let node = create_runtime_node();
Expand All @@ -142,7 +142,7 @@ fn test_trying_to_create_near_implicit_account_runtime() {

#[test]
fn test_trying_to_create_eth_implicit_account_runtime() {
if !checked_feature!("stable", EthImplicit, PROTOCOL_VERSION) {
if !checked_feature!("stable", EthImplicitAccounts, PROTOCOL_VERSION) {
return;
}
let node = create_runtime_node();
Expand Down
3 changes: 3 additions & 0 deletions runtime/near-vm-runner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub struct Config {
/// Enable the `FunctionCallWeight` protocol feature.
pub function_call_weight: bool,

/// Enable the `EthImplicitAccounts` protocol feature.
pub eth_implicit_accounts: bool,

/// Describes limits for VM and Runtime.
pub limit_config: LimitConfig,
}
Expand Down
7 changes: 2 additions & 5 deletions runtime/near-vm-runner/src/logic/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use near_primitives_core::runtime::fees::{transfer_exec_fee, transfer_send_fee};
use near_primitives_core::types::{
AccountId, Balance, Compute, EpochHeight, Gas, GasWeight, StorageUsage,
};
use near_primitives_core::version::PROTOCOL_VERSION;
use std::mem::size_of;

pub type Result<T, E = VMLogicError> = ::std::result::Result<T, E>;
Expand Down Expand Up @@ -1781,16 +1780,14 @@ impl<'a> VMLogic<'a> {
self.fees_config,
sir,
self.config.implicit_account_creation,
self.config.eth_implicit_accounts,
receiver_id.get_account_type(),
// TODO(eth-implicit) What protocol version to use?
PROTOCOL_VERSION,
);
let exec_fee = transfer_exec_fee(
self.fees_config,
self.config.implicit_account_creation,
self.config.eth_implicit_accounts,
receiver_id.get_account_type(),
// TODO(eth-implicit) What protocol version to use?
PROTOCOL_VERSION,
);
let burn_gas = send_fee;
let use_gas = burn_gas.checked_add(exec_fee).ok_or(HostError::IntegerOverflow)?;
Expand Down
1 change: 1 addition & 0 deletions runtime/near-vm-runner/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl crate::logic::Config {
ed25519_verify: config.ed25519_verify,
alt_bn128: config.alt_bn128,
function_call_weight: config.function_call_weight,
eth_implicit_accounts: config.eth_implicit_accounts,
limit_config: crate::config::LimitConfig {
max_gas_burnt: config.limit_config.max_gas_burnt,
max_stack_height: config.limit_config.max_stack_height,
Expand Down
8 changes: 3 additions & 5 deletions runtime/runtime/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ pub(crate) fn action_implicit_account_creation_transfer(
// Invariant: The `account_id` is implicit.
// It holds because in the only calling site, we've checked the permissions before.
AccountType::EthImplicitAccount => {
if checked_feature!("stable", EthImplicit, current_protocol_version) {
if checked_feature!("stable", EthImplicitAccounts, current_protocol_version) {
// TODO(eth-implicit) Use real Wallet Contract.
let wallet_contract = wallet_contract_placeholder();
let storage_usage = fee_config.storage_usage_config.num_bytes_account
Expand Down Expand Up @@ -918,7 +918,6 @@ pub(crate) fn check_account_existence(
config: &RuntimeConfig,
is_the_only_action: bool,
is_refund: bool,
current_protocol_version: ProtocolVersion,
) -> Result<(), ActionError> {
match action {
Action::CreateAccount(_) => {
Expand All @@ -930,7 +929,7 @@ pub(crate) fn check_account_existence(
} else {
// TODO: this should be `config.implicit_account_creation`.
if config.wasm_config.implicit_account_creation
&& account_is_implicit(account_id, current_protocol_version)
&& account_is_implicit(account_id, config.wasm_config.eth_implicit_accounts)
{
// If the account doesn't exist and it's implicit, then you
// should only be able to create it using single transfer action.
Expand All @@ -952,7 +951,7 @@ pub(crate) fn check_account_existence(
if account.is_none() {
return if config.wasm_config.implicit_account_creation
&& is_the_only_action
&& account_is_implicit(account_id, current_protocol_version)
&& account_is_implicit(account_id, config.wasm_config.eth_implicit_accounts)
&& !is_refund
{
// OK. It's implicit account creation.
Expand Down Expand Up @@ -1407,7 +1406,6 @@ mod tests {
&RuntimeConfig::test(),
false,
false,
apply_state.current_protocol_version,
),
Err(ActionErrorKind::AccountDoesNotExist { account_id: sender_id.clone() }.into())
);
Expand Down
7 changes: 2 additions & 5 deletions runtime/runtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use near_primitives::account::AccessKeyPermission;
use near_primitives::errors::IntegerOverflowError;
use near_primitives_core::config::ActionCosts;
use near_primitives_core::version::PROTOCOL_VERSION;
use num_bigint::BigUint;
use num_traits::cast::ToPrimitive;
use num_traits::pow::Pow;
Expand Down Expand Up @@ -101,9 +100,8 @@ pub fn total_send_fees(
fees,
sender_is_receiver,
config.wasm_config.implicit_account_creation,
config.wasm_config.eth_implicit_accounts,
receiver_id.get_account_type(),
// TODO(eth-implicit) What protocol version to use?
PROTOCOL_VERSION,
)
}
Stake(_) => fees.fee(ActionCosts::stake).send_fee(sender_is_receiver),
Expand Down Expand Up @@ -197,9 +195,8 @@ pub fn exec_fee(config: &RuntimeConfig, action: &Action, receiver_id: &AccountId
transfer_exec_fee(
fees,
config.wasm_config.implicit_account_creation,
config.wasm_config.eth_implicit_accounts,
receiver_id.get_account_type(),
// TODO(eth-implicit) What protocol version to use?
PROTOCOL_VERSION,
)
}
Stake(_) => fees.fee(ActionCosts::stake).exec_fee(),
Expand Down
1 change: 0 additions & 1 deletion runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ impl Runtime {
&apply_state.config,
is_the_only_action,
is_refund,
apply_state.current_protocol_version,
) {
result.result = Err(e);
return Ok(result);
Expand Down

0 comments on commit b7c3142

Please sign in to comment.