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

Add tests for transfer gas logic #142

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
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
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"));
Copy link
Contributor

Choose a reason for hiding this comment

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

So the confirmation handling has a cost!?
Note: the design is such that anybody can send a (valid) confirmation. For instance, I can emit a signed transfer to Bob, collect a confirmation, and give him the certificate instead of broadcasting it myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmation handling is where we actually execute the order (e.g. invoke Move bytecode). And that's where we deduct gas (in this case, we are deducting the gas for Transfer order). So if the gas is insufficient to execute the order, it will error out during confirmation handling step.
Are you suggesting to do order execution during the order handling step instead of order confirmation step?

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you suggesting to do order execution during the order handling step instead of order confirmation step?

Not at all, I was trying to figure out why you got to a cost of 12 gas, rather than the minimum of 8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok, that's because of this: https://github.com/MystenLabs/fastnft/blob/main/fastx_types/src/gas.rs#L100
The current gas cost for transferring an object is based on the size of the object, which is obviously arbitrarily made up by me at this point.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, well, that will be a finnicky test to maintain. But that's certainly an acceptable state for now.

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