Skip to content

Commit

Permalink
Merge pull request MystenLabs#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 authored 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

0 comments on commit 6286592

Please sign in to comment.