Skip to content

Commit

Permalink
Cargo fmt check style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
staffik committed Oct 30, 2023
1 parent 068b0f6 commit 92cdc36
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 39 deletions.
7 changes: 3 additions & 4 deletions core/primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,7 @@ where
/// If the key type is ED25519, returns hex-encoded copy of the key.
pub fn derive_account_id_from_public_key(public_key: &PublicKey) -> AccountId {
match public_key.key_type() {
KeyType::ED25519 => {
hex::encode(public_key.key_data()).parse().unwrap()
},
KeyType::ED25519 => hex::encode(public_key.key_data()).parse().unwrap(),
_ => unimplemented!(),
}
}
Expand All @@ -487,7 +485,8 @@ mod tests {
#[test]
fn test_derive_account_id_from_ed25519_public_key() {
let public_key = PublicKey::from_seed(KeyType::ED25519, "test");
let expected: AccountId = "bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0".parse().unwrap();
let expected: AccountId =
"bb4dc639b212e075a751685b26bdcea5920a504181ff2910e8549742127092a0".parse().unwrap();
assert_eq!(derive_account_id_from_public_key(&public_key), expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,27 @@ fn test_transaction_hash_collision_for_implicit_account_fail() {
let implicit_account_id = derive_account_id_from_public_key(&secret_key.public_key());
let implicit_account_signer = InMemorySigner::from_secret_key(implicit_account_id, secret_key);
assert_matches!(
get_status_of_tx_hash_collision_for_implicit_account(protocol_version, implicit_account_signer),
get_status_of_tx_hash_collision_for_implicit_account(
protocol_version,
implicit_account_signer
),
ProcessTxResponse::InvalidTx(InvalidTxError::InvalidNonce { .. })
);
}

/// Test that duplicate transactions from NEAR-implicit accounts are not rejected until protocol upgrade.
#[test]
fn test_transaction_hash_collision_for_implicit_account_ok() {
let protocol_version = ProtocolFeature::AccessKeyNonceForImplicitAccounts.protocol_version() - 1;
let protocol_version =
ProtocolFeature::AccessKeyNonceForImplicitAccounts.protocol_version() - 1;
let secret_key = SecretKey::from_seed(KeyType::ED25519, "test");
let implicit_account_id = derive_account_id_from_public_key(&secret_key.public_key());
let implicit_account_signer = InMemorySigner::from_secret_key(implicit_account_id, secret_key);
assert_matches!(
get_status_of_tx_hash_collision_for_implicit_account(protocol_version, implicit_account_signer),
get_status_of_tx_hash_collision_for_implicit_account(
protocol_version,
implicit_account_signer
),
ProcessTxResponse::ValidTx
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use near_chain::ChainGenesis;
use near_chain_configs::Genesis;
use near_client::test_utils::TestEnv;
use near_crypto::{KeyType, PublicKey, Signer};
use near_primitives::account::{AccessKey, AccessKeyPermission, FunctionCallPermission, id::AccountType};
use near_primitives::account::{
id::AccountType, AccessKey, AccessKeyPermission, FunctionCallPermission,
};
use near_primitives::config::ActionCosts;
use near_primitives::errors::{
ActionError, ActionErrorKind, ActionsValidationError, InvalidAccessKeyError, InvalidTxError,
Expand Down
23 changes: 13 additions & 10 deletions integration-tests/src/tests/standard_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ mod runtime;
use assert_matches::assert_matches;
use near_crypto::{InMemorySigner, KeyType, PublicKey};
use near_jsonrpc_primitives::errors::ServerError;
use near_primitives::account::{AccessKey, AccessKeyPermission, FunctionCallPermission, id::AccountType};
use near_primitives::account::{
id::AccountType, AccessKey, AccessKeyPermission, FunctionCallPermission,
};
use near_primitives::config::{ActionCosts, ExtCosts};
use near_primitives::errors::{
ActionError, ActionErrorKind, FunctionCallError, InvalidAccessKeyError, InvalidTxError,
Expand Down Expand Up @@ -366,7 +368,7 @@ pub fn transfer_tokens_implicit_account(node: impl Node, public_key: PublicKey)
match receiver_id.get_account_type() {
AccountType::NearImplicitAccount => {
assert_eq!(view_access_key.unwrap(), AccessKey::full_access().into());
},
}
AccountType::EthImplicitAccount => std::panic!("must be implicit"),
AccountType::NamedAccount => std::panic!("must be implicit"),
}
Expand Down Expand Up @@ -411,15 +413,16 @@ pub fn trying_to_create_implicit_account(node: impl Node, public_key: PublicKey)
.unwrap();

let cost = match receiver_id.get_account_type() {
AccountType::NearImplicitAccount =>
AccountType::NearImplicitAccount => {
fee_helper.create_account_transfer_full_key_cost_fail_on_create_account()
+ fee_helper.gas_to_balance(
fee_helper.cfg().fee(ActionCosts::create_account).send_fee(false)
+ fee_helper
.cfg()
.fee(near_primitives::config::ActionCosts::add_full_access_key)
.send_fee(false),
),
+ fee_helper.gas_to_balance(
fee_helper.cfg().fee(ActionCosts::create_account).send_fee(false)
+ fee_helper
.cfg()
.fee(near_primitives::config::ActionCosts::add_full_access_key)
.send_fee(false),
)
}
AccountType::EthImplicitAccount => std::panic!("must be implicit"),
AccountType::NamedAccount => std::panic!("must be implicit"),
};
Expand Down
18 changes: 13 additions & 5 deletions runtime/near-vm-runner/src/logic/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1773,11 +1773,19 @@ impl<'a> VMLogic<'a> {

let (receipt_idx, sir) = self.promise_idx_to_receipt_idx_with_sir(promise_idx)?;
let receiver_id = self.ext.get_receipt_receiver(receipt_idx);
let is_receiver_implicit = self.config.implicit_account_creation && receiver_id.get_account_type().is_implicit();
let send_fee =
transfer_send_fee(self.fees_config, sir, is_receiver_implicit, receiver_id.get_account_type());
let exec_fee =
transfer_exec_fee(self.fees_config, is_receiver_implicit, receiver_id.get_account_type());
let is_receiver_implicit =
self.config.implicit_account_creation && receiver_id.get_account_type().is_implicit();
let send_fee = transfer_send_fee(
self.fees_config,
sir,
is_receiver_implicit,
receiver_id.get_account_type(),
);
let exec_fee = transfer_exec_fee(
self.fees_config,
is_receiver_implicit,
receiver_id.get_account_type(),
);
let burn_gas = send_fee;
let use_gas = burn_gas.checked_add(exec_fee).ok_or(HostError::IntegerOverflow)?;
self.gas_counter.pay_action_accumulated(burn_gas, use_gas, ActionCosts::transfer)?;
Expand Down
12 changes: 9 additions & 3 deletions runtime/runtime/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,11 @@ pub(crate) fn action_implicit_account_creation_transfer(
let mut access_key = AccessKey::full_access();
// Set default nonce for newly created access key to avoid transaction hash collision.
// See <https://github.com/near/nearcore/issues/3779>.
if checked_feature!("stable", AccessKeyNonceForImplicitAccounts, current_protocol_version) {
if checked_feature!(
"stable",
AccessKeyNonceForImplicitAccounts,
current_protocol_version
) {
access_key.nonce = (block_height - 1)
* near_primitives::account::AccessKey::ACCESS_KEY_NONCE_RANGE_MULTIPLIER;
}
Expand All @@ -467,7 +471,7 @@ pub(crate) fn action_implicit_account_creation_transfer(
));

set_access_key(state_update, account_id.clone(), public_key, &access_key);
},
}
AccountType::EthImplicitAccount => panic!("must be implicit"),
AccountType::NamedAccount => panic!("must be implicit"),
}
Expand Down Expand Up @@ -890,7 +894,9 @@ pub(crate) fn check_account_existence(
.into());
} else {
// TODO: this should be `config.implicit_account_creation`.
if config.wasm_config.implicit_account_creation && account_id.get_account_type().is_implicit() {
if config.wasm_config.implicit_account_creation
&& account_id.get_account_type().is_implicit()
{
// If the account doesn't exist and it's implicit, then you
// should only be able to create it using single transfer action.
// Because you should not be able to add another access key to the account in
Expand Down
11 changes: 8 additions & 3 deletions runtime/runtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ pub fn total_send_fees(
Transfer(_) => {
// Account for implicit account creation
let receiver_account_type = receiver_id.get_account_type();
let is_receiver_implicit =
config.wasm_config.implicit_account_creation && receiver_account_type.is_implicit();
transfer_send_fee(fees, sender_is_receiver, is_receiver_implicit, receiver_account_type)
let is_receiver_implicit = config.wasm_config.implicit_account_creation
&& receiver_account_type.is_implicit();
transfer_send_fee(
fees,
sender_is_receiver,
is_receiver_implicit,
receiver_account_type,
)
}
Stake(_) => fees.fee(ActionCosts::stake).send_fee(sender_is_receiver),
AddKey(add_key_action) => match &add_key_action.access_key.permission {
Expand Down
6 changes: 4 additions & 2 deletions tools/fork-network/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ impl ForkNetworkCommand {
}
}
StateRecord::PostponedReceipt(receipt) => {
if receipt.predecessor_id.get_account_type().is_implicit() || receipt.receiver_id.get_account_type().is_implicit()
if receipt.predecessor_id.get_account_type().is_implicit()
|| receipt.receiver_id.get_account_type().is_implicit()
{
let new_receipt = Receipt {
predecessor_id: map_account(&receipt.predecessor_id, None),
Expand All @@ -549,7 +550,8 @@ impl ForkNetworkCommand {
}
}
StateRecord::DelayedReceipt(receipt) => {
if receipt.predecessor_id.get_account_type().is_implicit() || receipt.receiver_id.get_account_type().is_implicit()
if receipt.predecessor_id.get_account_type().is_implicit()
|| receipt.receiver_id.get_account_type().is_implicit()
{
let new_receipt = Receipt {
predecessor_id: map_account(&receipt.predecessor_id, None),
Expand Down
8 changes: 6 additions & 2 deletions tools/mirror/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ pub fn map_records<P: AsRef<Path>>(
records_seq.serialize_element(&r).unwrap();
}
StateRecord::PostponedReceipt(receipt) => {
if receipt.predecessor_id.get_account_type().is_implicit() || receipt.receiver_id.get_account_type().is_implicit() {
if receipt.predecessor_id.get_account_type().is_implicit()
|| receipt.receiver_id.get_account_type().is_implicit()
{
receipt.predecessor_id =
crate::key_mapping::map_account(&receipt.predecessor_id, secret.as_ref());
receipt.receiver_id =
Expand All @@ -83,7 +85,9 @@ pub fn map_records<P: AsRef<Path>>(
records_seq.serialize_element(&r).unwrap();
}
StateRecord::DelayedReceipt(receipt) => {
if receipt.predecessor_id.get_account_type().is_implicit() || receipt.receiver_id.get_account_type().is_implicit() {
if receipt.predecessor_id.get_account_type().is_implicit()
|| receipt.receiver_id.get_account_type().is_implicit()
{
receipt.predecessor_id =
crate::key_mapping::map_account(&receipt.predecessor_id, secret.as_ref());
receipt.receiver_id =
Expand Down
6 changes: 3 additions & 3 deletions tools/mirror/src/key_mapping.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use hkdf::Hkdf;
use near_crypto::{ED25519PublicKey, ED25519SecretKey, PublicKey, Secp256K1PublicKey, SecretKey};
use near_primitives_core::account::id::AccountType;
use near_primitives::types::AccountId;
use near_primitives::utils::derive_account_id_from_public_key;
use near_primitives_core::account::id::AccountType;
use sha2::Sha256;

// there is nothing special about this key, it's just some randomly generated one.
Expand Down Expand Up @@ -106,8 +106,8 @@ pub fn map_account(
PublicKey::from_near_implicit_account(account_id).expect("must be implicit");
let mapped_key = map_key(&public_key, secret);
derive_account_id_from_public_key(&mapped_key.public_key())
},
}
AccountType::EthImplicitAccount => account_id.clone(),
AccountType::NamedAccount => account_id.clone()
AccountType::NamedAccount => account_id.clone(),
}
}
11 changes: 8 additions & 3 deletions tools/mirror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,9 @@ impl<T: ChainAccess> TxMirror<T> {
actions.push(Action::DeleteKey(Box::new(DeleteKeyAction { public_key })));
}
Action::Transfer(_) => {
if tx.receiver_id().get_account_type().is_implicit() && source_actions.len() == 1 {
if tx.receiver_id().get_account_type().is_implicit()
&& source_actions.len() == 1
{
let target_account =
crate::key_mapping::map_account(tx.receiver_id(), self.secret.as_ref());
if !account_exists(&self.target_view_client, &target_account)
Expand All @@ -1002,8 +1004,11 @@ impl<T: ChainAccess> TxMirror<T> {
)
})?
{
if target_account.get_account_type() == AccountType::NearImplicitAccount {
let public_key = PublicKey::from_near_implicit_account(&target_account).expect("must be implicit");
if target_account.get_account_type() == AccountType::NearImplicitAccount
{
let public_key =
PublicKey::from_near_implicit_account(&target_account)
.expect("must be implicit");
nonce_updates.insert((target_account, public_key));
}
}
Expand Down

0 comments on commit 92cdc36

Please sign in to comment.