Skip to content

Commit

Permalink
Fix transaction reactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darthsiroftardis committed Dec 6, 2024
1 parent 9b97f5e commit 1204760
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
67 changes: 33 additions & 34 deletions node/src/reactor/main_reactor/tests/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static CHARLIE_SECRET_KEY: Lazy<Arc<SecretKey>> = Lazy::new(|| {
static CHARLIE_PUBLIC_KEY: Lazy<PublicKey> =
Lazy::new(|| PublicKey::from(&*CHARLIE_SECRET_KEY.clone()));

const MIN_GAS_PRICE: u8 = 5;
const MIN_GAS_PRICE: u8 = 1;
const CHAIN_NAME: &str = "single-transaction-test-net";

async fn transfer_to_account<A: Into<U512>>(
Expand Down Expand Up @@ -1023,8 +1023,6 @@ impl SingleTransactionTestCase {
ConfigsOverride::default()
.with_minimum_era_height(5) // make the era longer so that the transaction doesn't land in the switch block.
.with_balance_hold_interval(TimeDiff::from_seconds(5))
.with_min_gas_price(MIN_GAS_PRICE)
.with_max_gas_price(MIN_GAS_PRICE)
.with_chain_name("single-transaction-test-net".to_string())
}

Expand Down Expand Up @@ -3045,25 +3043,27 @@ async fn insufficient_funds_transfer_from_account() {

let transfer_amount = U512::max_value();

let mut txn = Transaction::from(
let txn_v1 =
TransactionV1Builder::new_transfer(transfer_amount, None, ALICE_PUBLIC_KEY.clone(), None)
.unwrap()
.with_chain_name(CHAIN_NAME)
.with_initiator_addr(PublicKey::from(&**BOB_SECRET_KEY))
.build()
.unwrap(),
);
.unwrap();
let price = txn_v1
.payment_amount()
.expect("must have payment amount as txns are using classic");
let mut txn = Transaction::from(txn_v1);
txn.sign(&BOB_SECRET_KEY);

let (_txn_hash, _block_height, exec_result) = test.send_transaction(txn).await;
let ExecutionResult::V2(result) = exec_result else {
panic!("Expected ExecutionResult::V2 but got {:?}", exec_result);
};
let transfer_cost: U512 =
U512::from(test.chainspec().system_costs_config.mint_costs().transfer) * MIN_GAS_PRICE;
let expected_cost: U512 = U512::from(price) * MIN_GAS_PRICE;

assert_eq!(result.error_message.as_deref(), Some("Insufficient funds"));
assert_eq!(result.cost, transfer_cost);
assert_eq!(result.cost, expected_cost);
}

#[tokio::test]
Expand All @@ -3089,22 +3089,22 @@ async fn insufficient_funds_add_bid() {
let (_, bob_initial_balance, _) = test.get_balances(None);
let bid_amount = bob_initial_balance.total;

let mut txn = Transaction::from(
let txn =
TransactionV1Builder::new_add_bid(BOB_PUBLIC_KEY.clone(), 0, bid_amount, None, None, None)
.unwrap()
.with_chain_name(CHAIN_NAME)
.with_initiator_addr(PublicKey::from(&**BOB_SECRET_KEY))
.build()
.unwrap(),
);
.unwrap();
let price = txn.payment_amount().expect("must get payment amount");
let mut txn = Transaction::from(txn);
txn.sign(&BOB_SECRET_KEY);

let (_txn_hash, _block_height, exec_result) = test.send_transaction(txn).await;
let ExecutionResult::V2(result) = exec_result else {
panic!("Expected ExecutionResult::V2 but got {:?}", exec_result);
};
let bid_cost: U512 =
U512::from(test.chainspec().system_costs_config.auction_costs().add_bid) * MIN_GAS_PRICE;
let bid_cost: U512 = U512::from(price) * MIN_GAS_PRICE;

assert_eq!(
result.error_message.as_deref(),
Expand Down Expand Up @@ -3175,27 +3175,26 @@ async fn insufficient_funds_transfer_from_purse() {

// now we try to transfer from the purse we just created
let transfer_amount = U512::max_value();
let mut txn = Transaction::from(
TransactionV1Builder::new_transfer(
transfer_amount,
Some(uref),
ALICE_PUBLIC_KEY.clone(),
None,
)
.unwrap()
.with_chain_name(CHAIN_NAME)
.with_initiator_addr(PublicKey::from(&**BOB_SECRET_KEY))
.build()
.unwrap(),
);
let txn = TransactionV1Builder::new_transfer(
transfer_amount,
Some(uref),
ALICE_PUBLIC_KEY.clone(),
None,
)
.unwrap()
.with_chain_name(CHAIN_NAME)
.with_initiator_addr(PublicKey::from(&**BOB_SECRET_KEY))
.build()
.unwrap();
let price = txn.payment_amount().expect("must get payment amount");
let mut txn = Transaction::from(txn);
txn.sign(&BOB_SECRET_KEY);

let (_txn_hash, _block_height, exec_result) = test.send_transaction(txn).await;
let ExecutionResult::V2(result) = exec_result else {
panic!("Expected ExecutionResult::V2 but got {:?}", exec_result);
};
let transfer_cost: U512 =
U512::from(test.chainspec().system_costs_config.mint_costs().transfer) * MIN_GAS_PRICE;
let transfer_cost: U512 = U512::from(price) * MIN_GAS_PRICE;

assert_eq!(result.error_message.as_deref(), Some("Insufficient funds"));
assert_eq!(result.cost, transfer_cost);
Expand Down Expand Up @@ -3223,22 +3222,22 @@ async fn insufficient_funds_when_caller_lacks_minimum_balance() {

let (_, bob_initial_balance, _) = test.get_balances(None);
let transfer_amount = bob_initial_balance.total - U512::one();
let mut txn = Transaction::from(
let txn =
TransactionV1Builder::new_transfer(transfer_amount, None, ALICE_PUBLIC_KEY.clone(), None)
.unwrap()
.with_chain_name(CHAIN_NAME)
.with_initiator_addr(PublicKey::from(&**BOB_SECRET_KEY))
.build()
.unwrap(),
);
.unwrap();
let price = txn.payment_amount().expect("must get payment amount");
let mut txn = Transaction::from(txn);
txn.sign(&BOB_SECRET_KEY);

let (_txn_hash, _block_height, exec_result) = test.send_transaction(txn).await;
let ExecutionResult::V2(result) = exec_result else {
panic!("Expected ExecutionResult::V2 but got {:?}", exec_result);
};
let transfer_cost: U512 =
U512::from(test.chainspec().system_costs_config.mint_costs().transfer) * MIN_GAS_PRICE;
let transfer_cost: U512 = U512::from(price) * MIN_GAS_PRICE;

assert_eq!(result.error_message.as_deref(), Some("Insufficient funds"));
assert_eq!(result.cost, transfer_cost);
Expand Down
2 changes: 1 addition & 1 deletion node/src/types/transaction/transaction_v1_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<'a> TransactionV1Builder<'a> {
pub const DEFAULT_TTL: TimeDiff = TimeDiff::from_millis(30 * 60 * 1_000);
/// The default pricing mode for v1 transactions, ie FIXED cost.
pub const DEFAULT_PRICING_MODE: PricingMode = PricingMode::PaymentLimited {
payment_amount: 2_500_000_000,
payment_amount: 10_000_000_000,
gas_price_tolerance: 3,
standard_payment: true,
};
Expand Down
10 changes: 10 additions & 0 deletions types/src/transaction/transaction_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ impl TransactionV1 {
self.approvals.extend(approvals);
}

/// Returns the payment amount if the txn is using classic mode.
#[cfg(any(all(feature = "std", feature = "testing"), test))]
pub fn payment_amount(&self) -> Option<u64> {
if let PricingMode::PaymentLimited { payment_amount, .. } = self.pricing_mode() {
Some(*payment_amount)
} else {
None
}
}

/// Returns a random, valid but possibly expired transaction.
#[cfg(any(all(feature = "std", feature = "testing"), test))]
pub fn random(rng: &mut TestRng) -> Self {
Expand Down

0 comments on commit 1204760

Please sign in to comment.