Skip to content

Commit

Permalink
Reduce size of the Action enum using Box (#9451)
Browse files Browse the repository at this point in the history
* use Box to reduce size of Action from 224B to 32B
  • Loading branch information
saketh-are authored and nikurt committed Aug 28, 2023
1 parent e105763 commit 9c3b28c
Show file tree
Hide file tree
Showing 37 changed files with 397 additions and 366 deletions.
6 changes: 3 additions & 3 deletions chain/client/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ impl TestEnv {
relayer,
sender,
&relayer_signer,
vec![Action::Delegate(signed_delegate_action)],
vec![Action::Delegate(Box::new(signed_delegate_action))],
tip.last_block_hash,
)
}
Expand Down Expand Up @@ -2246,12 +2246,12 @@ impl TestEnv {
/// deployed already.
pub fn call_main(&mut self, account: &AccountId) -> FinalExecutionOutcomeView {
let signer = InMemorySigner::from_seed(account.clone(), KeyType::ED25519, account.as_str());
let actions = vec![Action::FunctionCall(FunctionCallAction {
let actions = vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "main".to_string(),
args: vec![],
gas: 3 * 10u64.pow(14),
deposit: 0,
})];
}))];
let tx = self.tx_from_actions(actions, &signer, signer.account_id.clone());
self.execute_tx(tx).unwrap()
}
Expand Down
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 @@ -1096,7 +1096,7 @@ mod tests {
let original_near_actions = NearActions {
sender_account_id: "proxy.near".parse().unwrap(),
receiver_account_id: "account.near".parse().unwrap(),
actions: vec![Action::Delegate(SignedDelegateAction {
actions: vec![Action::Delegate(Box::new(SignedDelegateAction {
delegate_action: DelegateAction {
sender_id: "account.near".parse().unwrap(),
receiver_id: "receiver.near".parse().unwrap(),
Expand All @@ -1108,7 +1108,7 @@ mod tests {
public_key: sk.public_key(),
},
signature: sk.sign(&[0]),
})],
}))],
};

let operations: Vec<crate::models::Operation> =
Expand Down
6 changes: 3 additions & 3 deletions core/primitives/src/action/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl SignedDelegateAction {

impl From<SignedDelegateAction> for Action {
fn from(delegate_action: SignedDelegateAction) -> Self {
Self::Delegate(delegate_action)
Self::Delegate(Box::new(delegate_action))
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ mod tests {
);

fn create_delegate_action(actions: Vec<Action>) -> Action {
Action::Delegate(SignedDelegateAction {
Action::Delegate(Box::new(SignedDelegateAction {
delegate_action: DelegateAction {
sender_id: "aaa".parse().unwrap(),
receiver_id: "bbb".parse().unwrap(),
Expand All @@ -163,7 +163,7 @@ mod tests {
public_key: PublicKey::empty(KeyType::ED25519),
},
signature: Signature::empty(KeyType::ED25519),
})
}))
}

#[test]
Expand Down
22 changes: 13 additions & 9 deletions core/primitives/src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,18 @@ pub enum Action {
CreateAccount(CreateAccountAction),
/// Sets a Wasm code to a receiver_id
DeployContract(DeployContractAction),
FunctionCall(FunctionCallAction),
FunctionCall(Box<FunctionCallAction>),
Transfer(TransferAction),
Stake(StakeAction),
AddKey(AddKeyAction),
DeleteKey(DeleteKeyAction),
Stake(Box<StakeAction>),
AddKey(Box<AddKeyAction>),
DeleteKey(Box<DeleteKeyAction>),
DeleteAccount(DeleteAccountAction),
Delegate(delegate::SignedDelegateAction),
Delegate(Box<delegate::SignedDelegateAction>),
}
const _: () = assert!(
cfg!(not(target_pointer_width = "64")) || std::mem::size_of::<Action>() == 32,
"Action is less than 32 bytes for performance reasons, see #9451"
);

impl Action {
pub fn get_prepaid_gas(&self) -> Gas {
Expand Down Expand Up @@ -209,7 +213,7 @@ impl From<DeployContractAction> for Action {

impl From<FunctionCallAction> for Action {
fn from(function_call_action: FunctionCallAction) -> Self {
Self::FunctionCall(function_call_action)
Self::FunctionCall(Box::new(function_call_action))
}
}

Expand All @@ -221,19 +225,19 @@ impl From<TransferAction> for Action {

impl From<StakeAction> for Action {
fn from(stake_action: StakeAction) -> Self {
Self::Stake(stake_action)
Self::Stake(Box::new(stake_action))
}
}

impl From<AddKeyAction> for Action {
fn from(add_key_action: AddKeyAction) -> Self {
Self::AddKey(add_key_action)
Self::AddKey(Box::new(add_key_action))
}
}

impl From<DeleteKeyAction> for Action {
fn from(delete_key_action: DeleteKeyAction) -> Self {
Self::DeleteKey(delete_key_action)
Self::DeleteKey(Box::new(delete_key_action))
}
}

Expand Down
27 changes: 16 additions & 11 deletions core/primitives/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ impl Transaction {
gas: Gas,
deposit: Balance,
) -> Self {
self.actions.push(Action::FunctionCall(FunctionCallAction {
self.actions.push(Action::FunctionCall(Box::new(FunctionCallAction {
method_name,
args,
gas,
deposit,
}));
})));
self
}

Expand All @@ -75,16 +75,16 @@ impl Transaction {
}

pub fn stake(mut self, stake: Balance, public_key: PublicKey) -> Self {
self.actions.push(Action::Stake(StakeAction { stake, public_key }));
self.actions.push(Action::Stake(Box::new(StakeAction { stake, public_key })));
self
}
pub fn add_key(mut self, public_key: PublicKey, access_key: AccessKey) -> Self {
self.actions.push(Action::AddKey(AddKeyAction { public_key, access_key }));
self.actions.push(Action::AddKey(Box::new(AddKeyAction { public_key, access_key })));
self
}

pub fn delete_key(mut self, public_key: PublicKey) -> Self {
self.actions.push(Action::DeleteKey(DeleteKeyAction { public_key }));
self.actions.push(Action::DeleteKey(Box::new(DeleteKeyAction { public_key })));
self
}

Expand Down Expand Up @@ -145,7 +145,7 @@ impl SignedTransaction {
signer_id.clone(),
signer_id,
signer,
vec![Action::Stake(StakeAction { stake, public_key })],
vec![Action::Stake(Box::new(StakeAction { stake, public_key }))],
block_hash,
)
}
Expand All @@ -166,10 +166,10 @@ impl SignedTransaction {
signer,
vec![
Action::CreateAccount(CreateAccountAction {}),
Action::AddKey(AddKeyAction {
Action::AddKey(Box::new(AddKeyAction {
public_key,
access_key: AccessKey { nonce: 0, permission: AccessKeyPermission::FullAccess },
}),
})),
Action::Transfer(TransferAction { deposit: amount }),
],
block_hash,
Expand All @@ -193,10 +193,10 @@ impl SignedTransaction {
signer,
vec![
Action::CreateAccount(CreateAccountAction {}),
Action::AddKey(AddKeyAction {
Action::AddKey(Box::new(AddKeyAction {
public_key,
access_key: AccessKey { nonce: 0, permission: AccessKeyPermission::FullAccess },
}),
})),
Action::Transfer(TransferAction { deposit: amount }),
Action::DeployContract(DeployContractAction { code }),
],
Expand All @@ -220,7 +220,12 @@ impl SignedTransaction {
signer_id,
receiver_id,
signer,
vec![Action::FunctionCall(FunctionCallAction { args, method_name, gas, deposit })],
vec![Action::FunctionCall(Box::new(FunctionCallAction {
args,
method_name,
gas,
deposit,
}))],
block_hash,
)
}
Expand Down
15 changes: 9 additions & 6 deletions core/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,18 @@ mod tests {
actions: vec![
Action::CreateAccount(CreateAccountAction {}),
Action::DeployContract(DeployContractAction { code: vec![1, 2, 3] }),
Action::FunctionCall(FunctionCallAction {
Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "qqq".to_string(),
args: vec![1, 2, 3],
gas: 1_000,
deposit: 1_000_000,
}),
})),
Action::Transfer(TransferAction { deposit: 123 }),
Action::Stake(StakeAction { public_key: public_key.clone(), stake: 1_000_000 }),
Action::AddKey(AddKeyAction {
Action::Stake(Box::new(StakeAction {
public_key: public_key.clone(),
stake: 1_000_000,
})),
Action::AddKey(Box::new(AddKeyAction {
public_key: public_key.clone(),
access_key: AccessKey {
nonce: 0,
Expand All @@ -350,8 +353,8 @@ mod tests {
method_names: vec!["www".to_string()],
}),
},
}),
Action::DeleteKey(DeleteKeyAction { public_key }),
})),
Action::DeleteKey(Box::new(DeleteKeyAction { public_key })),
Action::DeleteAccount(DeleteAccountAction {
beneficiary_id: "123".parse().unwrap(),
}),
Expand Down
12 changes: 6 additions & 6 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,28 +1231,28 @@ impl TryFrom<ActionView> for Action {
Action::DeployContract(DeployContractAction { code })
}
ActionView::FunctionCall { method_name, args, gas, deposit } => {
Action::FunctionCall(FunctionCallAction {
Action::FunctionCall(Box::new(FunctionCallAction {
method_name,
args: args.into(),
gas,
deposit,
})
}))
}
ActionView::Transfer { deposit } => Action::Transfer(TransferAction { deposit }),
ActionView::Stake { stake, public_key } => {
Action::Stake(StakeAction { stake, public_key })
Action::Stake(Box::new(StakeAction { stake, public_key }))
}
ActionView::AddKey { public_key, access_key } => {
Action::AddKey(AddKeyAction { public_key, access_key: access_key.into() })
Action::AddKey(Box::new(AddKeyAction { public_key, access_key: access_key.into() }))
}
ActionView::DeleteKey { public_key } => {
Action::DeleteKey(DeleteKeyAction { public_key })
Action::DeleteKey(Box::new(DeleteKeyAction { public_key }))
}
ActionView::DeleteAccount { beneficiary_id } => {
Action::DeleteAccount(DeleteAccountAction { beneficiary_id })
}
ActionView::Delegate { delegate_action, signature } => {
Action::Delegate(SignedDelegateAction { delegate_action, signature })
Action::Delegate(Box::new(SignedDelegateAction { delegate_action, signature }))
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions genesis-tools/genesis-csv-to-json/src/csv_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ fn account_records(row: &Row, gas_price: Balance) -> Vec<StateRecord> {
gas_price,
output_data_receivers: vec![],
input_data_ids: vec![],
actions: vec![Action::FunctionCall(FunctionCallAction {
actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "init".to_string(),
args,
gas: INIT_GAS,
deposit: 0,
})],
}))],
}),
};
res.push(StateRecord::PostponedReceipt(Box::new(receipt)));
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/client/cold_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ fn test_storage_after_commit_of_cold_update() {
"test0".parse().unwrap(),
"test0".parse().unwrap(),
&signer,
vec![Action::FunctionCall(FunctionCallAction {
vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "write_random_value".to_string(),
args: vec![],
gas: 100_000_000_000_000,
deposit: 0,
})],
}))],
last_hash,
);
assert_eq!(env.clients[0].process_tx(tx, false, false), ProcessTxResponse::ValidTx);
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/client/epoch_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ fn generate_transactions(last_hash: &CryptoHash, h: BlockHeight) -> Vec<SignedTr
"test0".parse().unwrap(),
"test0".parse().unwrap(),
&signer,
vec![Action::FunctionCall(FunctionCallAction {
vec![Action::FunctionCall(Box::new(FunctionCallAction {
method_name: "write_random_value".to_string(),
args: vec![],
gas: 100_000_000_000_000,
deposit: 0,
})],
}))],
last_hash.clone(),
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn test_account_id_in_function_call_permission_upgrade() {
signer_id: "test0".parse().unwrap(),
receiver_id: "test0".parse().unwrap(),
public_key: signer.public_key(),
actions: vec![Action::AddKey(AddKeyAction {
actions: vec![Action::AddKey(Box::new(AddKeyAction {
public_key: signer.public_key(),
access_key: AccessKey {
nonce: 1,
Expand All @@ -56,7 +56,7 @@ fn test_account_id_in_function_call_permission_upgrade() {
method_names: vec![],
}),
},
})],
}))],
nonce: 0,
block_hash: CryptoHash::default(),
};
Expand Down Expand Up @@ -111,7 +111,7 @@ fn test_very_long_account_id() {
signer_id: "test0".parse().unwrap(),
receiver_id: "test0".parse().unwrap(),
public_key: signer.public_key(),
actions: vec![Action::AddKey(AddKeyAction {
actions: vec![Action::AddKey(Box::new(AddKeyAction {
public_key: signer.public_key(),
access_key: AccessKey {
nonce: 1,
Expand All @@ -121,7 +121,7 @@ fn test_very_long_account_id() {
method_names: vec![],
}),
},
})],
}))],
nonce: 0,
block_hash: tip.last_block_hash,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ fn process_transaction(
"test0".parse().unwrap(),
signer,
vec![
Action::FunctionCall(FunctionCallAction {
Action::FunctionCall(Box::new(FunctionCallAction {
args: encode(&[0u64, 10u64]),
method_name: "write_key_value".to_string(),
gas,
deposit: 0,
}),
Action::FunctionCall(FunctionCallAction {
})),
Action::FunctionCall(Box::new(FunctionCallAction {
args: encode(&[1u64, 20u64]),
method_name: "write_key_value".to_string(),
gas,
deposit: 0,
}),
})),
],
last_block_hash,
);
Expand Down
Loading

0 comments on commit 9c3b28c

Please sign in to comment.