Skip to content

Commit

Permalink
Merge pull request #142 from lxfind/add-transfer-tests
Browse files Browse the repository at this point in the history
Add tests for transfer gas logic
  • Loading branch information
lxfind committed Jan 9, 2022
2 parents 9d34503 + d629445 commit 6286592
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
63 changes: 63 additions & 0 deletions fastpay_core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ async fn test_handle_transfer_order_ok() {
);
}

#[tokio::test]
async fn test_handle_transfer_zero_balance() {
let (sender, sender_key) = get_key_pair();
let recipient = Address::FastPay(dbg_addr(2));
let object_id = ObjectID::random();
let authority_state = init_state_with_ids(vec![(sender, object_id)]).await;

// Create a gas object with 0 balance.
let gas_object_id = ObjectID::random();
let gas_object = Object::with_id_owner_gas_for_testing(gas_object_id, sender, 0);
authority_state
.init_order_lock((gas_object_id, 0.into(), gas_object.digest()))
.await;
authority_state.insert_object(gas_object).await;

let transfer_order =
init_transfer_order(sender, &sender_key, recipient, object_id, gas_object_id);

let result = authority_state.handle_order(transfer_order.clone()).await;
assert!(result
.unwrap_err()
.to_string()
.contains("Gas balance is 0, smaller than minimum requirement of 8 for object transfer."));
}

async fn send_and_confirm_order(
authority: &mut AuthorityState,
order: Order,
Expand Down Expand Up @@ -656,6 +681,44 @@ async fn test_handle_confirmation_order_receiver_equal_sender() {
.is_some());
}

#[tokio::test]
async fn test_handle_confirmation_order_gas() {
let run_test_with_gas = |gas: u64| async move {
let (sender, sender_key) = get_key_pair();
let recipient = dbg_addr(2);
let object_id = ObjectID::random();
let authority_state = init_state_with_ids(vec![(sender, object_id)]).await;

// Create a gas object with insufficient balance.
let gas_object_id = ObjectID::random();
let gas_object = Object::with_id_owner_gas_for_testing(gas_object_id, sender, gas);
authority_state
.init_order_lock((gas_object_id, 0.into(), gas_object.digest()))
.await;
authority_state.insert_object(gas_object).await;

let certified_transfer_order = init_certified_transfer_order(
sender,
&sender_key,
Address::FastPay(recipient),
object_id,
gas_object_id,
&authority_state,
);

authority_state
.handle_confirmation_order(ConfirmationOrder::new(certified_transfer_order.clone()))
.await
};
let result = run_test_with_gas(10).await;
assert!(result
.unwrap_err()
.to_string()
.contains("Gas balance is 10, not enough to pay 12"));
let result = run_test_with_gas(20).await;
assert!(result.is_ok());
}

#[tokio::test]
async fn test_handle_confirmation_order_ok() {
let (sender, sender_key) = get_key_pair();
Expand Down
14 changes: 11 additions & 3 deletions fastx_types/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ macro_rules! ok_or_gas_error {

const MIN_MOVE_CALL_GAS: u64 = 10;
const MIN_MOVE_PUBLISH_GAS: u64 = 10;
const MIN_OBJ_TRANSFER_GAS: u64 = 8;

pub fn check_gas_requirement(order: &Order, gas_object: &Object) -> FastPayResult {
match &order.kind {
OrderKind::Transfer(_) => {
// TODO: Add gas logic for transfer orders.
Ok(())
OrderKind::Transfer(t) => {
debug_assert_eq!(t.gas_payment.0, gas_object.id());
let balance = get_gas_balance(gas_object)?;
ok_or_gas_error!(
balance >= MIN_OBJ_TRANSFER_GAS,
format!(
"Gas balance is {}, smaller than minimum requirement of {} for object transfer.",
balance, MIN_OBJ_TRANSFER_GAS
)
)
}
OrderKind::Publish(publish) => {
debug_assert_eq!(publish.gas_payment.0, gas_object.id());
Expand Down

1 comment on commit 6286592

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Bench results

�[0m�[0m�[1m�[32m Finished�[0m release [optimized + debuginfo] target(s) in 2.38s
�[0m�[0m�[1m�[32m Running�[0m target/release/bench
[2022-01-09T04:10:23Z INFO bench] Starting benchmark: OrdersAndCerts
[2022-01-09T04:10:23Z INFO bench] Preparing accounts.
[2022-01-09T04:10:27Z INFO bench] Preparing transactions.
[2022-01-09T04:10:35Z INFO fastpay::network] Listening to Tcp traffic on 127.0.0.1:9555
[2022-01-09T04:10:36Z INFO bench] Set max_in_flight to 500
[2022-01-09T04:10:36Z INFO bench] Sending requests.
[2022-01-09T04:10:36Z INFO fastpay::network] Sending Tcp requests to 127.0.0.1:9555
[2022-01-09T04:10:36Z WARN fastpay::network] User query failed: Value was not signed by the correct sender
[2022-01-09T04:10:36Z WARN fastpay::network] User query failed: Attempt to set an non-existing order lock.
[2022-01-09T04:10:36Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:37Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:37Z INFO fastpay::network] 127.0.0.1:9555 has processed 5000 packets
[2022-01-09T04:10:37Z INFO fastpay::network] In flight 500 Remaining 35000
[2022-01-09T04:10:37Z INFO fastpay::network] 127.0.0.1:9555 has processed 10000 packets
[2022-01-09T04:10:38Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:38Z INFO fastpay::network] 127.0.0.1:9555 has processed 15000 packets
[2022-01-09T04:10:39Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:39Z INFO fastpay::network] In flight 500 Remaining 30000
[2022-01-09T04:10:39Z INFO fastpay::network] 127.0.0.1:9555 has processed 20000 packets
[2022-01-09T04:10:39Z INFO fastpay::network] 127.0.0.1:9555 has processed 25000 packets
[2022-01-09T04:10:40Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:40Z INFO fastpay::network] In flight 500 Remaining 25000
[2022-01-09T04:10:40Z INFO fastpay::network] 127.0.0.1:9555 has processed 30000 packets
[2022-01-09T04:10:40Z INFO fastpay::network] 127.0.0.1:9555 has processed 35000 packets
[2022-01-09T04:10:41Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:41Z INFO fastpay::network] In flight 500 Remaining 20000
[2022-01-09T04:10:41Z INFO fastpay::network] 127.0.0.1:9555 has processed 40000 packets
[2022-01-09T04:10:41Z INFO fastpay::network] 127.0.0.1:9555 has processed 45000 packets
[2022-01-09T04:10:42Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:42Z INFO fastpay::network] In flight 500 Remaining 15000
[2022-01-09T04:10:42Z INFO fastpay::network] 127.0.0.1:9555 has processed 50000 packets
[2022-01-09T04:10:42Z INFO fastpay::network] 127.0.0.1:9555 has processed 55000 packets
[2022-01-09T04:10:43Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:43Z INFO fastpay::network] In flight 500 Remaining 10000
[2022-01-09T04:10:43Z INFO fastpay::network] 127.0.0.1:9555 has processed 60000 packets
[2022-01-09T04:10:44Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:44Z INFO fastpay::network] 127.0.0.1:9555 has processed 65000 packets
[2022-01-09T04:10:44Z INFO fastpay::network] In flight 500 Remaining 5000
[2022-01-09T04:10:44Z INFO fastpay::network] 127.0.0.1:9555 has processed 70000 packets
[2022-01-09T04:10:45Z WARN fastpay::network] User query failed: The given sequence (SequenceNumber(0)) number must match the next expected sequence (SequenceNumber(1)) number of the account
[2022-01-09T04:10:45Z INFO fastpay::network] 127.0.0.1:9555 has processed 75000 packets
[2022-01-09T04:10:45Z INFO fastpay::network] Done sending Tcp requests to 127.0.0.1:9555
[2022-01-09T04:10:45Z INFO fastpay::network] 127.0.0.1:9555 has processed 80000 packets
[2022-01-09T04:10:45Z INFO bench] Received 80000 responses.
[2022-01-09T04:10:45Z WARN bench] Completed benchmark for OrdersAndCerts
Total time: 9034540us, items: 40000, tx/sec: 4427.452864229944

Please sign in to comment.