Skip to content

Commit

Permalink
Adds gas unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Mar 22, 2023
1 parent 315c614 commit 265b25b
Show file tree
Hide file tree
Showing 6 changed files with 683 additions and 130 deletions.
4 changes: 2 additions & 2 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ pub fn genesis() -> Genesis {
},
max_expected_time_per_block: namada::types::time::DurationSecs(30),
max_proposal_bytes: Default::default(),
max_block_gas: u64::MAX, //FIXME: adjust this value
max_block_gas: 100_000_000, //FIXME: adjust this value
vp_whitelist: vec![],
tx_whitelist: vec![],
implicit_vp_code_path: vp_implicit_path.into(),
Expand All @@ -936,7 +936,7 @@ pub fn genesis() -> Genesis {
pos_gain_d: dec!(0.1),
staked_ratio: dec!(0.0),
pos_inflation_amount: 0,
wrapper_tx_fees: Some(token::Amount::whole(0)),
wrapper_tx_fees: Some(token::Amount::whole(100)),
gas_table: BTreeMap::default(),
};
let albert = EstablishedAccount {
Expand Down
112 changes: 90 additions & 22 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ where
let fee_payer = if wrapper.pk != address::masp_tx_key().ref_to() {
wrapper.fee_payer()
} else {
address::masp()
address::masp() //FIXME: here?
};

let balance_key = token::balance_key(&wrapper.fee.token, &fee_payer);
Expand Down Expand Up @@ -593,6 +593,7 @@ mod test_finalize_block {
InitProposalData, VoteProposalData,
};
use namada::types::transaction::{EncryptionKey, Fee, WrapperTx, MIN_FEE};
use tendermint_proto::abci::RequestInitChain;

use super::*;
use crate::node::ledger::shell::test_utils::*;
Expand Down Expand Up @@ -1096,16 +1097,12 @@ mod test_finalize_block {
},
&keypair,
Epoch(0),
GAS_LIMIT_MULTIPLIER.into(),
0.into(),
raw_tx.clone(),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);
let signed_wrapper = wrapper_tx
.sign(&keypair, shell.chain_id.clone(), None)
.unwrap()
.to_bytes();

// Write inner hash in storage
let inner_hash_key =
Expand All @@ -1128,30 +1125,101 @@ mod test_finalize_block {
info: "".into(),
},
};
let gas_limit =
u64::from(&wrapper_tx.gas_limit) - signed_wrapper.len() as u64;
let gas_limit = u64::from(&wrapper_tx.gas_limit);
shell.enqueue_tx(wrapper_tx, gas_limit);

let _event = &shell
let event = &shell
.finalize_block(FinalizeBlock {
txs: vec![processed_tx],
..Default::default()
})
.expect("Test failed")[0];

// FIXME: uncomment when proper gas metering is in place
// // Check inner tx hash has been removed from storage
// assert_eq!(event.event_type.to_string(), String::from("applied"));
// let code = event.attributes.get("code").expect("Test
// failed").as_str(); assert_eq!(code,
// String::from(ErrorCodes::WasmRuntimeError).as_str());

// assert!(
// !shell
// .storage
// .has_key(&inner_hash_key)
// .expect("Test failed")
// .0
// Check inner tx hash has been removed from storage
assert_eq!(event.event_type.to_string(), String::from("applied"));
let code = event.attributes.get("code").expect("Testfailed").as_str();
assert_eq!(code, String::from(ErrorCodes::WasmRuntimeError).as_str());

assert!(!shell
.shell
.wl_storage
.has_key(&inner_hash_key)
.expect("Test failed"))
}

/// Test that a wrapper transaction rejected by [`process_proposal`] because of gas,
/// still pays the fee
#[test]
fn test_rejected_wrapper_for_gas_pays_fee() {
let (mut shell, _) = setup();

let keypair = gen_keypair();
let address = Address::from(&keypair.to_public());

let mut wasm_path = top_level_directory();
wasm_path.push("wasm_for_tests/tx_no_op.wasm");
let tx_code = std::fs::read(wasm_path)
.expect("Expected a file at given code path");
let raw_tx = Tx::new(
tx_code,
Some("Encrypted transaction data".as_bytes().to_owned()),
shell.chain_id.clone(),
None,
);
let wrapper_tx = WrapperTx::new(
Fee {
amount: 1.into(),
token: shell.wl_storage.storage.native_token.clone(),
},
&keypair,
Epoch(0),
1.into(),
raw_tx.clone(),
Default::default(),
#[cfg(not(feature = "mainnet"))]
None,
);

let wrapper = wrapper_tx
.sign(&keypair, shell.chain_id.clone(), None)
.expect("Test failed");

let processed_tx = ProcessedTx {
tx: wrapper.to_bytes(),
result: TxResult {
code: ErrorCodes::TxGasLimit.into(),
info: "".into(),
},
};

let initial_balance = Amount::whole(1_000_000);
let balance_key = token::balance_key(&wrapper_tx.fee.token, &address);
shell
.wl_storage
.storage
.write(&balance_key, initial_balance.try_to_vec().unwrap())
.unwrap();

//FIXME: uncomment when variable fees
// let event = &shell
// .finalize_block(FinalizeBlock {
// txs: vec![processed_tx],
// ..Default::default()
// })
// .expect("Test failed")[0];

// assert_eq!(event.event_type.to_string(), String::from("accepted"));
// let code = event.attributes.get("code").expect("Testfailed").as_str();
// assert_eq!(code, String::from(ErrorCodes::TxGasLimit).as_str());

// assert_eq!(
// storage_api::token::read_balance(
// &shell.wl_storage,
// &wrapper_tx.fee.token,
// &address
// )
// .unwrap(),
// Amount::whole(1000)
// )
}
}
Loading

0 comments on commit 265b25b

Please sign in to comment.