Skip to content

Commit

Permalink
feat: stabilize meta transactions NEP-366
Browse files Browse the repository at this point in the history
Tracking issue: near#8075
  • Loading branch information
jakmeier committed Feb 20, 2023
1 parent 34ca6d8 commit 1b3c02f
Show file tree
Hide file tree
Showing 29 changed files with 38 additions and 233 deletions.
2 changes: 0 additions & 2 deletions chain/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ nightly = [
"nightly_protocol",
"protocol_feature_flat_state",
"near-chain/nightly",
"protocol_feature_nep366_delegate_action"
]
sandbox = [
"near-client-primitives/sandbox",
"near-chain/sandbox",
]
protocol_feature_flat_state = ["near-store/protocol_feature_flat_state", "near-chain/protocol_feature_flat_state"]
protocol_feature_nep366_delegate_action = []
6 changes: 1 addition & 5 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ use near_chunks::adapter::ShardsManagerRequestFromClient;
use near_chunks::client::ShardsManagerResponse;
use near_chunks::test_utils::{MockClientAdapterForShardsManager, SynchronousShardsManagerAdapter};
use near_client_primitives::types::Error;
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use near_crypto::Signer;
use near_crypto::{InMemorySigner, KeyType, PublicKey};
use near_crypto::{InMemorySigner, KeyType, PublicKey, Signer};
use near_network::test_utils::MockPeerManagerAdapter;
use near_network::types::{
AccountOrPeerIdOrHash, HighestHeightPeerInfo, PartialEncodedChunkRequestMsg,
Expand All @@ -54,7 +52,6 @@ use near_network::types::{
use near_o11y::testonly::TracingCapture;
use near_o11y::WithSpanContextExt;
use near_primitives::block::{ApprovalInner, Block, GenesisId};
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use near_primitives::delegate_action::{DelegateAction, NonDelegateAction, SignedDelegateAction};
use near_primitives::epoch_manager::RngSeed;
use near_primitives::hash::{hash, CryptoHash};
Expand Down Expand Up @@ -1828,7 +1825,6 @@ impl TestEnv {
}

/// Wrap actions in a delegate action, put it in a transaction, sign.
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
pub fn meta_tx_from_actions(
&mut self,
actions: Vec<Action>,
Expand Down
7 changes: 1 addition & 6 deletions chain/rosetta-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,4 @@ insta = "1"
near-actix-test-utils = { path = "../../test-utils/actix-test-utils" }

[features]
protocol_feature_nep366_delegate_action = [
"near-primitives/protocol_feature_nep366_delegate_action"
]
nightly = [
"protocol_feature_nep366_delegate_action"
]
nightly = []
4 changes: 2 additions & 2 deletions chain/rosetta-rpc/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ impl From<NearActions> for Vec<crate::models::Operation> {
);
operations.push(deploy_contract_operation);
}
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
near_primitives::transaction::Action::Delegate(_) => todo!(),
// TODO(#8469): Implement delegate action support, for now they are ignored.
near_primitives::transaction::Action::Delegate(_) => (),
}
}
operations
Expand Down
3 changes: 1 addition & 2 deletions core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ insta.workspace = true

[features]
default = []
protocol_feature_nep366_delegate_action = []
nightly = ["protocol_feature_nep366_delegate_action"]
nightly = []
1 change: 0 additions & 1 deletion core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ pub enum ActionCosts {
new_action_receipt = 12,
new_data_receipt_base = 13,
new_data_receipt_byte = 14,
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
delegate = 15,
}

Expand Down
1 change: 0 additions & 1 deletion core/primitives-core/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ impl From<ActionCosts> for FeeParameter {
match other {
ActionCosts::create_account => Self::ActionCreateAccount,
ActionCosts::delete_account => Self::ActionDeleteAccount,
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
ActionCosts::delegate => Self::ActionDelegate,
ActionCosts::deploy_contract_base => Self::ActionDeployContract,
ActionCosts::deploy_contract_byte => Self::ActionDeployContractPerByte,
Expand Down
7 changes: 3 additions & 4 deletions core/primitives-core/src/runtime/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ impl RuntimeFeesConfig {
send_not_sir: 59357464,
execution: 59357464,
},
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
ActionCosts::delegate => Fee {
send_sir: 2319861500000,
send_not_sir: 2319861500000,
execution: 2319861500000,
send_sir: 200_000_000_000,
send_not_sir: 200_000_000_000,
execution: 200_000_000_000,
},
},
}
Expand Down
4 changes: 0 additions & 4 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ dump_errors_schema = ["near-rpc-error-macro/dump_errors_schema"]
protocol_feature_fix_staking_threshold = []
protocol_feature_fix_contract_loading_cost = []
protocol_feature_reject_blocks_with_outdated_protocol_version = []
protocol_feature_nep366_delegate_action = [
"near-primitives-core/protocol_feature_nep366_delegate_action"
]
nightly = [
"nightly_protocol",
"protocol_feature_fix_staking_threshold",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_nep366_delegate_action",
]

nightly_protocol = []
Expand Down
42 changes: 1 addition & 41 deletions core/primitives/src/delegate_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,12 @@ pub struct DelegateAction {
pub public_key: PublicKey,
}

#[cfg_attr(feature = "protocol_feature_nep366_delegate_action", derive(BorshDeserialize))]
#[derive(BorshSerialize, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
pub struct SignedDelegateAction {
pub delegate_action: DelegateAction,
pub signature: Signature,
}

#[cfg(not(feature = "protocol_feature_nep366_delegate_action"))]
impl borsh::de::BorshDeserialize for SignedDelegateAction {
fn deserialize(_buf: &mut &[u8]) -> ::core::result::Result<Self, borsh::maybestd::io::Error> {
return Err(Error::new(ErrorKind::InvalidInput, "Delegate action isn't supported"));
}
}

impl SignedDelegateAction {
pub fn verify(&self) -> bool {
let delegate_action = &self.delegate_action;
Expand All @@ -62,7 +54,6 @@ impl SignedDelegateAction {
}
}

#[cfg(feature = "protocol_feature_nep366_delegate_action")]
impl From<SignedDelegateAction> for Action {
fn from(delegate_action: SignedDelegateAction) -> Self {
Self::Delegate(delegate_action)
Expand Down Expand Up @@ -112,7 +103,6 @@ mod private_non_delegate_action {
#[error("attempted to construct NonDelegateAction from Action::Delegate")]
pub struct IsDelegateAction;

#[cfg(feature = "protocol_feature_nep366_delegate_action")]
impl TryFrom<Action> for NonDelegateAction {
type Error = IsDelegateAction;

Expand Down Expand Up @@ -149,9 +139,7 @@ mod private_non_delegate_action {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use crate::transaction::CreateAccountAction;
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use near_crypto::KeyType;

/// A serialized `Action::Delegate(SignedDelegateAction)` for testing.
Expand All @@ -167,7 +155,6 @@ mod tests {
"0000000000000000000000000000000000000000000000000000000000"
);

#[cfg(feature = "protocol_feature_nep366_delegate_action")]
fn create_delegate_action(actions: Vec<Action>) -> Action {
Action::Delegate(SignedDelegateAction {
delegate_action: DelegateAction {
Expand All @@ -186,7 +173,6 @@ mod tests {
}

#[test]
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
fn test_delegate_action_deserialization() {
// Expected an error. Buffer is empty
assert_eq!(
Expand Down Expand Up @@ -218,35 +204,9 @@ mod tests {
);
}

/// Check that we will not accept delegate actions with the feature
/// disabled.
///
/// This test is to ensure that while working on meta transactions, we don't
/// accientally start accepting delegate actions in receipts. Otherwise, a
/// malicious validator could create receipts that include delegate actions
/// and other nodes will accept such a receipt.
///
/// TODO: Before stabilizing "protocol_feature_nep366_delegate_action" we
/// have to replace this rest with a test that checks that we discard
/// delegate actions for earlier versions somewhere in validation.
#[test]
#[cfg(not(feature = "protocol_feature_nep366_delegate_action"))]
fn test_delegate_action_deserialization() {
let serialized_delegate_action = hex::decode(DELEGATE_ACTION_HEX).expect("invalid hex");

// DelegateAction isn't supported
assert_eq!(
Action::try_from_slice(&serialized_delegate_action).map_err(|e| e.kind()),
Err(ErrorKind::InvalidInput)
);
}

/// Check that the hard-coded delegate action is valid.
#[test]
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
fn test_delegate_action_deserialization_hard_coded() {
use crate::transaction::CreateAccountAction;

let serialized_delegate_action = hex::decode(DELEGATE_ACTION_HEX).expect("invalid hex");
// The hex data is the same as the one we create below.
let delegate_action =
Expand Down
5 changes: 1 addition & 4 deletions core/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::account::AccessKey;
use crate::delegate_action::SignedDelegateAction;
use crate::errors::TxExecutionError;
use crate::hash::{hash, CryptoHash};
use crate::merkle::MerklePath;
Expand All @@ -12,9 +13,6 @@ use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};

#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use crate::delegate_action::SignedDelegateAction;

pub type LogEntry = String;

#[derive(
Expand Down Expand Up @@ -76,7 +74,6 @@ pub enum Action {
AddKey(AddKeyAction),
DeleteKey(DeleteKeyAction),
DeleteAccount(DeleteAccountAction),
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
Delegate(SignedDelegateAction),
}

Expand Down
12 changes: 7 additions & 5 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ pub enum ProtocolFeature {
AccountIdInFunctionCallPermission,
/// Zero Balance Account NEP 448: https://github.com/near/NEPs/pull/448
ZeroBalanceAccount,
/// Execute a set of actions on behalf of another account.
///
/// Meta Transaction NEP-366: https://github.com/near/NEPs/blob/master/neps/nep-0366.md
DelegateAction,

/// In case not all validator seats are occupied our algorithm provide incorrect minimal seat
/// price - it reports as alpha * sum_stake instead of alpha * sum_stake / (1 - alpha), where
Expand All @@ -146,8 +150,6 @@ pub enum ProtocolFeature {
Ed25519Verify,
#[cfg(feature = "protocol_feature_reject_blocks_with_outdated_protocol_version")]
RejectBlocksWithOutdatedProtocolVersions,
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
DelegateAction,
}

/// Both, outgoing and incoming tcp connections to peers, will be rejected if `peer's`
Expand Down Expand Up @@ -225,7 +227,9 @@ impl ProtocolFeature {
ProtocolFeature::AltBn128 => 55,
ProtocolFeature::ChunkOnlyProducers | ProtocolFeature::MaxKickoutStake => 56,
ProtocolFeature::AccountIdInFunctionCallPermission => 57,
ProtocolFeature::Ed25519Verify | ProtocolFeature::ZeroBalanceAccount => 59,
ProtocolFeature::Ed25519Verify
| ProtocolFeature::ZeroBalanceAccount
| ProtocolFeature::DelegateAction => 59,

// Nightly features
#[cfg(feature = "protocol_feature_fix_staking_threshold")]
Expand All @@ -234,8 +238,6 @@ impl ProtocolFeature {
ProtocolFeature::FixContractLoadingCost => 129,
#[cfg(feature = "protocol_feature_reject_blocks_with_outdated_protocol_version")]
ProtocolFeature::RejectBlocksWithOutdatedProtocolVersions => 132,
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
ProtocolFeature::DelegateAction => 133,
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::block_header::{
};
use crate::challenge::{Challenge, ChallengesResult};
use crate::contract::ContractCode;
use crate::delegate_action::{DelegateAction, SignedDelegateAction};
use crate::errors::TxExecutionError;
use crate::hash::{hash, CryptoHash};
use crate::merkle::{combine_hash, MerklePath};
Expand Down Expand Up @@ -49,8 +50,6 @@ use std::sync::Arc;
use strum::IntoEnumIterator;
use validator_stake_view::ValidatorStakeView;

#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use crate::delegate_action::{DelegateAction, SignedDelegateAction};
/// A view of the account
#[derive(serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct AccountView {
Expand Down Expand Up @@ -1137,7 +1136,6 @@ pub enum ActionView {
DeleteAccount {
beneficiary_id: AccountId,
},
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
Delegate {
delegate_action: DelegateAction,
signature: Signature,
Expand Down Expand Up @@ -1170,7 +1168,6 @@ impl From<Action> for ActionView {
Action::DeleteAccount(action) => {
ActionView::DeleteAccount { beneficiary_id: action.beneficiary_id }
}
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
Action::Delegate(action) => ActionView::Delegate {
delegate_action: action.delegate_action,
signature: action.signature,
Expand Down Expand Up @@ -1204,7 +1201,6 @@ impl TryFrom<ActionView> for Action {
ActionView::DeleteAccount { beneficiary_id } => {
Action::DeleteAccount(DeleteAccountAction { beneficiary_id })
}
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
ActionView::Delegate { delegate_action, signature } => {
Action::Delegate(SignedDelegateAction {
delegate_action: delegate_action,
Expand Down Expand Up @@ -2326,7 +2322,6 @@ pub struct ActionCreationConfigView {
/// Base cost for processing a delegate action.
///
/// This is on top of the costs for the actions inside the delegate action.
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
pub delegate_cost: Fee,
}

Expand Down Expand Up @@ -2403,7 +2398,6 @@ impl From<RuntimeConfig> for RuntimeConfigView {
},
delete_key_cost: config.fees.fee(ActionCosts::delete_key).clone(),
delete_account_cost: config.fees.fee(ActionCosts::delete_account).clone(),
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
delegate_cost: config.fees.fee(ActionCosts::delegate).clone(),
},
storage_usage_config: StorageUsageConfigView {
Expand Down Expand Up @@ -2449,7 +2443,6 @@ impl From<RuntimeConfigView> for RuntimeConfig {
action_fees: enum_map::enum_map! {
ActionCosts::create_account => config.transaction_costs.action_creation_config.create_account_cost.clone(),
ActionCosts::delete_account => config.transaction_costs.action_creation_config.delete_account_cost.clone(),
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
ActionCosts::delegate => config.transaction_costs.action_creation_config.delegate_cost.clone(),
ActionCosts::deploy_contract_base => config.transaction_costs.action_creation_config.deploy_contract_cost.clone(),
ActionCosts::deploy_contract_byte => config.transaction_costs.action_creation_config.deploy_contract_cost_per_byte.clone(),
Expand Down
6 changes: 0 additions & 6 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,13 @@ protocol_feature_reject_blocks_with_outdated_protocol_version = [
"near-chain/protocol_feature_reject_blocks_with_outdated_protocol_version"
]
protocol_feature_flat_state = ["nearcore/protocol_feature_flat_state"]
protocol_feature_nep366_delegate_action = [
"nearcore/protocol_feature_nep366_delegate_action",
"testlib/protocol_feature_nep366_delegate_action",
]


nightly = [
"nightly_protocol",
"nearcore/nightly",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_flat_state",
"protocol_feature_nep366_delegate_action",
]
nightly_protocol = ["nearcore/nightly_protocol"]
sandbox = [
Expand Down
1 change: 0 additions & 1 deletion integration-tests/src/tests/client/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod account_id_in_function_call_permission;
mod adversarial_behaviors;
mod cap_max_gas_price;
mod chunk_nodes_cache;
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
mod delegate_action;
#[cfg(feature = "protocol_feature_fix_contract_loading_cost")]
mod fix_contract_loading_cost;
Expand Down
3 changes: 0 additions & 3 deletions integration-tests/src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use futures::{future::LocalBoxFuture, FutureExt};
use near_crypto::{PublicKey, Signer};
use near_jsonrpc_primitives::errors::ServerError;
use near_primitives::account::AccessKey;
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use near_primitives::delegate_action::{DelegateAction, NonDelegateAction, SignedDelegateAction};
use near_primitives::hash::CryptoHash;
use near_primitives::receipt::Receipt;
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
use near_primitives::test_utils::create_user_test_signer;
use near_primitives::transaction::{
Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction,
Expand Down Expand Up @@ -249,7 +247,6 @@ pub trait User {
///
/// The signer signs the delegate action to be sent to the receiver. The
/// relayer packs that in a transaction and signs it .
#[cfg(feature = "protocol_feature_nep366_delegate_action")]
fn meta_tx(
&self,
signer_id: AccountId,
Expand Down
Loading

0 comments on commit 1b3c02f

Please sign in to comment.