Skip to content

Commit

Permalink
Wrap changes in protocol_feature
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Nov 8, 2023
1 parent 32cbe4a commit 2c3f3a3
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 120 deletions.
4 changes: 4 additions & 0 deletions chain/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ protocol_feature_simple_nightshade_v2 = [
protocol_feature_chunk_validation = [
"near-primitives/protocol_feature_chunk_validation",
]
protocol_feature_eth_implicit = [
"near-primitives/protocol_feature_eth_implicit",
]

nightly = [
"nightly_protocol",
"protocol_feature_chunk_validation",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_simple_nightshade_v2",
"near-async/nightly",
"protocol_feature_eth_implicit",
"near-chain-configs/nightly",
"near-client-primitives/nightly",
"near-epoch-manager/nightly",
Expand Down
2 changes: 2 additions & 0 deletions core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protocol_feature_fix_contract_loading_cost = []
protocol_feature_reject_blocks_with_outdated_protocol_version = []
protocol_feature_simple_nightshade_v2 = []
protocol_feature_chunk_validation = []
protocol_feature_eth_implicit = []

nightly = [
"nightly_protocol",
Expand All @@ -45,6 +46,7 @@ nightly = [
"protocol_feature_fix_staking_threshold",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_simple_nightshade_v2",
"protocol_feature_eth_implicit",
]

nightly_protocol = [
Expand Down
11 changes: 9 additions & 2 deletions core/primitives-core/src/runtime/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ pub fn transfer_exec_fee(
(false, _) => transfer_fee,
// Extra fee for the CreateAccount.
(true, AccountType::EthImplicitAccount) => {
transfer_fee + cfg.fee(ActionCosts::create_account).exec_fee()
#[cfg(feature = "protocol_feature_eth_implicit")]
return transfer_fee + cfg.fee(ActionCosts::create_account).exec_fee();
#[cfg(not(feature = "protocol_feature_eth_implicit"))]
transfer_fee
}
// Extra fees for the CreateAccount and AddFullAccessKey.
(true, AccountType::NearImplicitAccount) => {
Expand All @@ -244,7 +247,11 @@ pub fn transfer_send_fee(
(false, _) => transfer_fee,
// Extra fee for the CreateAccount.
(true, AccountType::EthImplicitAccount) => {
transfer_fee + cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver)
#[cfg(feature = "protocol_feature_eth_implicit")]
return transfer_fee
+ cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver);
#[cfg(not(feature = "protocol_feature_eth_implicit"))]
transfer_fee
}
// Extra fees for the CreateAccount and AddFullAccessKey.
(true, AccountType::NearImplicitAccount) => {
Expand Down
6 changes: 5 additions & 1 deletion core/primitives-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub enum ProtocolFeature {
/// NEP: https://github.com/near/NEPs/pull/509
#[cfg(feature = "protocol_feature_chunk_validation")]
ChunkValidation,
#[cfg(feature = "protocol_feature_eth_implicit")]
EthImplicit,
}

impl ProtocolFeature {
Expand Down Expand Up @@ -189,6 +191,8 @@ impl ProtocolFeature {
ProtocolFeature::PostStateRoot => 136,
#[cfg(feature = "protocol_feature_chunk_validation")]
ProtocolFeature::ChunkValidation => 137,
#[cfg(feature = "protocol_feature_eth_implicit")]
ProtocolFeature::EthImplicit => 138,
}
}
}
Expand All @@ -201,7 +205,7 @@ const STABLE_PROTOCOL_VERSION: ProtocolVersion = 64;
/// Largest protocol version supported by the current binary.
pub const PROTOCOL_VERSION: ProtocolVersion = if cfg!(feature = "nightly_protocol") {
// On nightly, pick big enough version to support all features.
139
140
} else {
// Enable all stable features.
STABLE_PROTOCOL_VERSION
Expand Down
2 changes: 2 additions & 0 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protocol_feature_fix_staking_threshold = ["near-primitives-core/protocol_feature
protocol_feature_fix_contract_loading_cost = ["near-primitives-core/protocol_feature_fix_contract_loading_cost"]
protocol_feature_reject_blocks_with_outdated_protocol_version = ["near-primitives-core/protocol_feature_reject_blocks_with_outdated_protocol_version"]
protocol_feature_simple_nightshade_v2 = ["near-primitives-core/protocol_feature_simple_nightshade_v2"]
protocol_feature_eth_implicit = ["near-primitives-core/protocol_feature_eth_implicit"]
protocol_feature_chunk_validation = []
nightly = [
"nightly_protocol",
Expand All @@ -59,6 +60,7 @@ nightly = [
"protocol_feature_fix_staking_threshold",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_simple_nightshade_v2",
"protocol_feature_eth_implicit",
"near-fmt/nightly",
"near-primitives-core/nightly",
"near-vm-runner/nightly",
Expand Down
12 changes: 12 additions & 0 deletions core/primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::version::{

use near_crypto::{KeyType, PublicKey};
use near_primitives_core::account::id::AccountId;
#[cfg(not(feature = "protocol_feature_eth_implicit"))]
use near_primitives_core::account::id::AccountType;

use std::mem::size_of;
use std::ops::Deref;
Expand Down Expand Up @@ -469,6 +471,16 @@ where
Serializable(object)
}

/// 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) -> bool {
#[cfg(feature = "protocol_feature_eth_implicit")]
return account_id.get_account_type().is_implicit();
#[cfg(not(feature = "protocol_feature_eth_implicit"))]
return account_id.get_account_type() == AccountType::NearImplicitAccount;
}

/// Derives `AccountId` from `PublicKey`.
/// If the key type is ED25519, returns hex-encoded copy of the key.
/// If the key type is SECP256K1, returns '0x' + keccak256(public_key)[12:32].hex().
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ protocol_feature_reject_blocks_with_outdated_protocol_version = [
protocol_feature_simple_nightshade_v2 = [
"near-primitives/protocol_feature_simple_nightshade_v2",
]
protocol_feature_eth_implicit = [
"near-primitives/protocol_feature_eth_implicit",
]

nightly = [
"nightly_protocol",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_simple_nightshade_v2",
"protocol_feature_eth_implicit",
"near-actix-test-utils/nightly",
"near-async/nightly",
"near-chain-configs/nightly",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ fn get_status_of_tx_hash_collision_for_implicit_account(
/// Test that duplicate transactions from ETH-implicit account can be properly rejected
/// if we set nonce to `(block_height - 1) * 1e6` for transactions that results in
/// access key being added to the ETH-implicit account.
#[cfg(feature = "protocol_feature_eth_implicit")]
#[test]
fn test_transaction_eth_implicit_account() {
let epoch_length = 10;
Expand Down Expand Up @@ -337,6 +338,7 @@ fn test_transaction_eth_implicit_account() {
}

/// Test that the signer is correctly verified for transactions done from an ETH-implicit account.
#[cfg(feature = "protocol_feature_eth_implicit")]
#[test]
fn test_transaction_eth_implicit_account_invalid_pk() {
let epoch_length = 10;
Expand Down Expand Up @@ -465,6 +467,7 @@ fn test_transaction_hash_collision_for_near_implicit_account_ok() {
/// Test that duplicate transactions from ETH-implicit accounts are not rejected before and after protocol upgrade.
/// It is responsibility of the transaction signer to choose nonce equal to `(block_height - 1) * 1e6` in case the transaction
/// results in adding a new key to an ETH-implicit account (see https://github.com/near/NEPs/issues/498#issuecomment-1782881395).
#[cfg(feature = "protocol_feature_eth_implicit")]
#[test]
fn test_transaction_hash_collision_for_eth_implicit_account_ok() {
let protocol_version =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ fn meta_tx_create_near_implicit_account_fails() {
meta_tx_create_implicit_account_fails(near_implicit_test_account());
}

#[cfg(feature = "protocol_feature_eth_implicit")]
#[test]
fn meta_tx_create_eth_implicit_account_fails() {
meta_tx_create_implicit_account_fails(eth_implicit_test_account());
Expand Down Expand Up @@ -871,6 +872,7 @@ fn meta_tx_create_and_use_near_implicit_account() {
meta_tx_create_and_use_implicit_account(near_implicit_test_account());
}

#[cfg(feature = "protocol_feature_eth_implicit")]
#[test]
fn meta_tx_create_and_use_eth_implicit_account() {
meta_tx_create_and_use_implicit_account(eth_implicit_test_account());
Expand Down Expand Up @@ -952,6 +954,7 @@ fn meta_tx_create_near_implicit_account() {
meta_tx_create_implicit_account(near_implicit_test_account(), None);
}

#[cfg(feature = "protocol_feature_eth_implicit")]
#[test]
fn meta_tx_create_eth_implicit_account() {
let secret_key = eth_implicit_test_account_secret();
Expand Down
4 changes: 4 additions & 0 deletions nearcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ protocol_feature_fix_contract_loading_cost = [
protocol_feature_simple_nightshade_v2 = [
"near-primitives/protocol_feature_simple_nightshade_v2",
]
protocol_feature_eth_implicit = [
"near-primitives/protocol_feature_eth_implicit",
]
new_epoch_sync = [
"near-client/new_epoch_sync"
]
Expand All @@ -125,6 +128,7 @@ nightly = [
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_fix_staking_threshold",
"protocol_feature_simple_nightshade_v2",
"protocol_feature_eth_implicit",
"serialize_all_state_changes",
"near-async/nightly",
"near-chain-configs/nightly",
Expand Down
2 changes: 2 additions & 0 deletions neard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ rosetta_rpc = ["nearcore/rosetta_rpc"]
json_rpc = ["nearcore/json_rpc"]
protocol_feature_fix_staking_threshold = ["nearcore/protocol_feature_fix_staking_threshold"]
protocol_feature_simple_nightshade_v2 = ["nearcore/protocol_feature_simple_nightshade_v2"]
protocol_feature_eth_implicit = ["nearcore/protocol_feature_eth_implicit"]
serialize_all_state_changes = ["nearcore/serialize_all_state_changes"]
new_epoch_sync = ["nearcore/new_epoch_sync"]

nightly = [
"nightly_protocol",
"protocol_feature_fix_staking_threshold",
"protocol_feature_simple_nightshade_v2",
"protocol_feature_eth_implicit",
"serialize_all_state_changes",
"near-chain-configs/nightly",
"near-client/nightly",
Expand Down
5 changes: 5 additions & 0 deletions runtime/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ near-store.workspace = true
near-vm-runner.workspace = true

[features]
protocol_feature_eth_implicit = [
"near-primitives/protocol_feature_eth_implicit",
]

nightly = [
"nightly_protocol",
"protocol_feature_eth_implicit",
"near-chain-configs/nightly",
"near-o11y/nightly",
"near-primitives-core/nightly",
Expand Down
Loading

0 comments on commit 2c3f3a3

Please sign in to comment.