Skip to content

Commit

Permalink
feat: draft implementation of NEP-366 (fork) (#8385)
Browse files Browse the repository at this point in the history
A fork of #7497 

Nearcore code owners currently don't have permissions to change the
original PR, so we will add the necessary changes here before merging.

This is also merged with the current master.
  • Loading branch information
jakmeier authored Jan 19, 2023
1 parent 0033c73 commit f4744fd
Show file tree
Hide file tree
Showing 42 changed files with 1,482 additions and 35 deletions.
4 changes: 2 additions & 2 deletions chain/chain/src/tests/simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn build_chain() {
// cargo insta test --accept -p near-chain --features nightly -- tests::simple_chain::build_chain
let hash = chain.head().unwrap().last_block_hash;
if cfg!(feature = "nightly") {
insta::assert_display_snapshot!(hash, @"HTpETHnBkxcX1h3eD87uC5YP5nV66E6UYPrJGnQHuRqt");
insta::assert_display_snapshot!(hash, @"96KiRJdbMN8A9cFPXarZdaRQ8U2HvYcrGTGC8a4EgFzM");
} else {
insta::assert_display_snapshot!(hash, @"7r5VSLXhkxHHEeiAAPQbKPGv3rr877obehGYwPbKZMA7");
}
Expand Down Expand Up @@ -73,7 +73,7 @@ fn build_chain() {

let hash = chain.head().unwrap().last_block_hash;
if cfg!(feature = "nightly") {
insta::assert_display_snapshot!(hash, @"HyDYbjs5tgeEDf1N1XB4m312VdCeKjHqeGQ7dc7Lqwv8");
insta::assert_display_snapshot!(hash, @"4eW4jvyu1Ek6WmY3EuUoFFkrascC7svRww5UcZbNMkUf");
} else {
insta::assert_display_snapshot!(hash, @"9772sSKzm1eGPV3pRi17YaZkotrcN6dAkJUn226CopTm");
}
Expand Down
56 changes: 54 additions & 2 deletions chain/jsonrpc/res/rpc_errors_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,13 @@
"FunctionCallError",
"NewReceiptValidationError",
"OnlyImplicitAccountCreationAllowed",
"DeleteAccountWithLargeState"
"DeleteAccountWithLargeState",
"DelegateActionInvalidSignature",
"DelegateActionSenderDoesNotMatchTxReceiver",
"DelegateActionExpired",
"DelegateActionAccessKeyError",
"DelegateActionInvalidNonce",
"DelegateActionNonceTooLarge"
],
"props": {
"index": ""
Expand All @@ -480,7 +486,9 @@
"FunctionCallMethodNameLengthExceeded",
"FunctionCallArgumentsLengthExceeded",
"UnsuitableStakingKey",
"FunctionCallZeroAttachedGas"
"FunctionCallZeroAttachedGas",
"DelegateActionCantContainNestedOne",
"DelegateActionMustBeOnlyOne"
],
"props": {}
},
Expand Down Expand Up @@ -556,6 +564,50 @@
"registrar_account_id": ""
}
},
"DelegateActionCantContainNestedOne": {
"name": "DelegateActionCantContainNestedOne",
"subtypes": [],
"props": {}
},
"DelegateActionExpired": {
"name": "DelegateActionExpired",
"subtypes": [],
"props": {}
},
"DelegateActionInvalidNonce": {
"name": "DelegateActionInvalidNonce",
"subtypes": [],
"props": {
"ak_nonce": "",
"delegate_nonce": ""
}
},
"DelegateActionInvalidSignature": {
"name": "DelegateActionInvalidSignature",
"subtypes": [],
"props": {}
},
"DelegateActionMustBeOnlyOne": {
"name": "DelegateActionMustBeOnlyOne",
"subtypes": [],
"props": {}
},
"DelegateActionNonceTooLarge": {
"name": "DelegateActionNonceTooLarge",
"subtypes": [],
"props": {
"delegate_nonce": "",
"upper_bound": ""
}
},
"DelegateActionSenderDoesNotMatchTxReceiver": {
"name": "DelegateActionSenderDoesNotMatchTxReceiver",
"subtypes": [],
"props": {
"receiver_id": "",
"sender_id": ""
}
},
"DeleteAccountStaking": {
"name": "DeleteAccountStaking",
"subtypes": [],
Expand Down
8 changes: 8 additions & 0 deletions chain/rosetta-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ near-o11y = { path = "../../core/o11y" }
[dev-dependencies]
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"
]
2 changes: 2 additions & 0 deletions chain/rosetta-rpc/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +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!(),
}
}
operations
Expand Down
3 changes: 2 additions & 1 deletion core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ insta.workspace = true
[features]
default = []
protocol_feature_ed25519_verify = []
nightly = ["protocol_feature_ed25519_verify"]
protocol_feature_nep366_delegate_action = []
nightly = ["protocol_feature_ed25519_verify", "protocol_feature_nep366_delegate_action"]
2 changes: 2 additions & 0 deletions core/primitives-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ 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,
}

impl ExtCosts {
Expand Down
6 changes: 6 additions & 0 deletions core/primitives-core/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub enum Parameter {
ActionDeleteKeySendSir,
ActionDeleteKeySendNotSir,
ActionDeleteKeyExecution,
ActionDelegateSendSir,
ActionDelegateSendNotSir,
ActionDelegateExecution,

// Smart contract dynamic gas costs
WasmRegularOpCost,
Expand Down Expand Up @@ -205,6 +208,7 @@ pub enum FeeParameter {
ActionAddFunctionCallKey,
ActionAddFunctionCallKeyPerByte,
ActionDeleteKey,
ActionDelegate,
}

impl Parameter {
Expand Down Expand Up @@ -250,6 +254,8 @@ 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,
ActionCosts::function_call_base => Self::ActionFunctionCall,
Expand Down
9 changes: 9 additions & 0 deletions core/primitives-core/src/runtime/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub struct ActionCreationConfig {

/// Base cost of deleting an account.
pub delete_account_cost: Fee,

/// Base cost of a delegate action
pub delegate_cost: Fee,
}

/// Describes the cost of creating an access key.
Expand Down Expand Up @@ -220,6 +223,12 @@ 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,
},
},
}
}
Expand Down
4 changes: 4 additions & 0 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ protocol_feature_reject_blocks_with_outdated_protocol_version = []
protocol_feature_ed25519_verify = [
"near-primitives-core/protocol_feature_ed25519_verify"
]
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_ed25519_verify",
"protocol_feature_nep366_delegate_action",
]

nightly_protocol = []
Expand Down
4 changes: 4 additions & 0 deletions core/primitives/res/runtime_configs/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ action_add_function_call_key_per_byte_execution: 1_925_331
action_delete_key_send_sir: 94_946_625_000
action_delete_key_send_not_sir: 94_946_625_000
action_delete_key_execution: 94_946_625_000
# TODO: place-holder values, needs estimation, tracked in #8114
action_delegate_send_sir: 2_319_861_500_000
action_delegate_send_not_sir: 2_319_861_500_000
action_delegate_execution: 2_319_861_500_000

# Smart contract dynamic gas costs
wasm_regular_op_cost: 3_856_371
Expand Down
4 changes: 4 additions & 0 deletions core/primitives/res/runtime_configs/parameters_testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ action_add_function_call_key_per_byte_execution: 1_925_331
action_delete_key_send_sir: 94_946_625_000
action_delete_key_send_not_sir: 94_946_625_000
action_delete_key_execution: 94_946_625_000
# TODO: place-holder values, needs estimation, tracked in #8114
action_delegate_send_sir: 2_319_861_500_000
action_delegate_send_not_sir: 2_319_861_500_000
action_delegate_execution: 2_319_861_500_000

# Smart contract dynamic gas costs
wasm_regular_op_cost: 3_856_371
Expand Down
30 changes: 30 additions & 0 deletions core/primitives/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ pub enum ActionsValidationError {
UnsuitableStakingKey { public_key: PublicKey },
/// The attached amount of gas in a FunctionCall action has to be a positive number.
FunctionCallZeroAttachedGas,
/// DelegateAction actions contain another DelegateAction. This is not allowed.
DelegateActionCantContainNestedOne,
/// There should be the only one DelegateAction
DelegateActionMustBeOnlyOne,
}

/// Describes the error for validating a receipt.
Expand Down Expand Up @@ -314,6 +318,14 @@ impl Display for ActionsValidationError {
f,
"The attached amount of gas in a FunctionCall action has to be a positive number",
),
ActionsValidationError::DelegateActionCantContainNestedOne => write!(
f,
"DelegateAction must not contain another DelegateAction"
),
ActionsValidationError::DelegateActionMustBeOnlyOne => write!(
f,
"The actions can contain the ony one DelegateAction"
)
}
}
}
Expand Down Expand Up @@ -397,6 +409,18 @@ pub enum ActionErrorKind {
OnlyImplicitAccountCreationAllowed { account_id: AccountId },
/// Delete account whose state is large is temporarily banned.
DeleteAccountWithLargeState { account_id: AccountId },
/// Signature does not match the provided actions and given signer public key.
DelegateActionInvalidSignature,
/// Receiver of the transaction doesn't match Sender of the delegate action
DelegateActionSenderDoesNotMatchTxReceiver { sender_id: AccountId, receiver_id: AccountId },
/// Delegate action has expired. `max_block_height` is less than actual block height.
DelegateActionExpired,
/// The given public key doesn't exist for Sender account
DelegateActionAccessKeyError(InvalidAccessKeyError),
/// DelegateAction nonce must be greater sender[public_key].nonce
DelegateActionInvalidNonce { delegate_nonce: Nonce, ak_nonce: Nonce },
/// DelegateAction nonce is larger than the upper bound given by the block height
DelegateActionNonceTooLarge { delegate_nonce: Nonce, upper_bound: Nonce },
}

impl From<ActionErrorKind> for ActionError {
Expand Down Expand Up @@ -707,6 +731,12 @@ impl Display for ActionErrorKind {
ActionErrorKind::InsufficientStake { account_id, stake, minimum_stake } => write!(f, "Account {} tries to stake {} but minimum required stake is {}", account_id, stake, minimum_stake),
ActionErrorKind::OnlyImplicitAccountCreationAllowed { account_id } => write!(f, "CreateAccount action is called on hex-characters account of length 64 {}", account_id),
ActionErrorKind::DeleteAccountWithLargeState { account_id } => write!(f, "The state of account {} is too large and therefore cannot be deleted", account_id),
ActionErrorKind::DelegateActionInvalidSignature => write!(f, "DelegateAction is not signed with the given public key"),
ActionErrorKind::DelegateActionSenderDoesNotMatchTxReceiver { sender_id, receiver_id } => write!(f, "Transaction receiver {} doesn't match DelegateAction sender {}", receiver_id, sender_id),
ActionErrorKind::DelegateActionExpired => write!(f, "DelegateAction has expired"),
ActionErrorKind::DelegateActionAccessKeyError(access_key_error) => Display::fmt(&access_key_error, f),
ActionErrorKind::DelegateActionInvalidNonce { delegate_nonce, ak_nonce } => write!(f, "DelegateAction nonce {} must be larger than nonce of the used access key {}", delegate_nonce, ak_nonce),
ActionErrorKind::DelegateActionNonceTooLarge { delegate_nonce, upper_bound } => write!(f, "DelegateAction nonce {} must be smaller than the access key nonce upper bound {}", delegate_nonce, upper_bound),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "100000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "100000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/primitives/src/runtime/config_store.rs
expression: store.get_config(*version)
expression: config_view
---
{
"storage_amount_per_byte": "10000000000000000000",
Expand Down
Loading

0 comments on commit f4744fd

Please sign in to comment.