Skip to content

Commit

Permalink
refactor: add helper function for verifier tests
Browse files Browse the repository at this point in the history
Add `validate_verify_and_charge_transaction` function for verifier
tests which combines `validate_transaction` and
`verify_and_charge_transaction` functions. This reduces complexity of
current and future tests.
  • Loading branch information
miloserdow committed Jan 24, 2025
1 parent 6f11ae3 commit 1e5bcf5
Showing 1 changed file with 56 additions and 65 deletions.
121 changes: 56 additions & 65 deletions runtime/runtime/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,31 @@ mod tests {
}
}

pub fn validate_verify_and_charge_transaction(
config: &RuntimeConfig,
state_update: &mut TrieUpdate,
signed_transaction: &SignedTransaction,
gas_price: Balance,
block_height: Option<BlockHeight>,
current_protocol_version: ProtocolVersion,
) -> Result<VerificationResult, InvalidTxError> {
let transaction_cost = validate_transaction(
config,
gas_price,
signed_transaction,
true,
current_protocol_version,
)?;
verify_and_charge_transaction(
config,
state_update,
signed_transaction,
&transaction_cost,
block_height,
current_protocol_version,
)
}

mod zero_balance_account_tests {
use crate::near_primitives::account::id::AccountId;
use crate::near_primitives::account::{
Expand Down Expand Up @@ -911,13 +936,11 @@ mod tests {
CryptoHash::default(),
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("valid transaction");
let verification_result = verify_and_charge_transaction(
let verification_result = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -985,17 +1008,11 @@ mod tests {
CryptoHash::default(),
);

let maybe_cost =
validate_transaction(&config, gas_price, &transaction, false, PROTOCOL_VERSION);
// Validation might pass if it doesn't require the access key check at this stage
let cost =
maybe_cost.expect("expected validation to succeed (no access key check at this stage)");

let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1058,13 +1075,11 @@ mod tests {
CryptoHash::default(),
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand All @@ -1090,13 +1105,11 @@ mod tests {
CryptoHash::default(),
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1164,13 +1177,11 @@ mod tests {
CryptoHash::default(),
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected cost from validation");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1215,13 +1226,11 @@ mod tests {
0,
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected cost from validation");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1261,13 +1270,11 @@ mod tests {
CryptoHash::default(),
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected cost from validation");
let verification_result = verify_and_charge_transaction(
let verification_result = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1304,21 +1311,19 @@ mod tests {
CryptoHash::default(),
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected cost from validation");
let res = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
.expect_err("expected an error");
let account = get_account(&state_update, &account_id).unwrap().unwrap();

assert_eq!(
res,
err,
InvalidTxError::LackBalanceForState {
signer_id: account_id,
amount: Balance::from(account.storage_usage()) * config.storage_amount_per_byte()
Expand Down Expand Up @@ -1361,13 +1366,11 @@ mod tests {
CryptoHash::default(),
0,
);
let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
verify_and_charge_transaction(
validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand All @@ -1383,13 +1386,11 @@ mod tests {
CryptoHash::default(),
0,
);
let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
verify_and_charge_transaction(
validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand All @@ -1405,13 +1406,11 @@ mod tests {
CryptoHash::default(),
0,
);
let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
verify_and_charge_transaction(
validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1449,13 +1448,11 @@ mod tests {
0,
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1500,13 +1497,11 @@ mod tests {
0,
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1550,13 +1545,11 @@ mod tests {
0,
);

let cost = validate_transaction(&config, gas_price, &transaction, true, PROTOCOL_VERSION)
.expect("expected no validation error");
let err = verify_and_charge_transaction(
let err = validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down Expand Up @@ -1605,13 +1598,11 @@ mod tests {
wasm_config.limit_config.max_transaction_size = transaction_size + 1;
}

let cost = validate_transaction(&config, gas_price, &transaction, false, PROTOCOL_VERSION)
.expect("expected no validation error");
verify_and_charge_transaction(
validate_verify_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down

0 comments on commit 1e5bcf5

Please sign in to comment.