Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add helper function for verifier tests #12771

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 verify_validate_and_charge_transaction(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn verify_validate_and_charge_transaction(
pub fn validate_verify_and_charge_transaction(

As the functions are correspondingly validate_transaction and 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 = verify_validate_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 = verify_validate_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 = verify_validate_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 = verify_validate_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 = verify_validate_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 = verify_validate_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 = verify_validate_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 = verify_validate_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(
verify_validate_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(
verify_validate_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(
verify_validate_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 = verify_validate_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 = verify_validate_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 = verify_validate_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(
verify_validate_and_charge_transaction(
&config,
&mut state_update,
&transaction,
&cost,
gas_price,
None,
PROTOCOL_VERSION,
)
Expand Down
Loading