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

Modify message verification inside ft_transfer_call for external receiver_id #326

Merged
merged 4 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
94 changes: 94 additions & 0 deletions engine-tests/src/tests/eth_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,100 @@ fn test_ft_transfer_call_eth() {
assert_eq!(balance, transfer_amount);
}

#[test]
fn test_ft_transfer_call_without_message() {
let (master_account, contract) = init(CUSTODIAN_ADDRESS);
call_deposit_eth_to_near(&contract, CONTRACT_ACC);

let balance = get_eth_on_near_balance(&master_account, DEPOSITED_RECIPIENT, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_AMOUNT - DEPOSITED_FEE);

let balance = get_eth_on_near_balance(&master_account, CONTRACT_ACC, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_FEE);

let res = contract.call(
CONTRACT_ACC.parse().unwrap(),
"register_relayer",
&RegisterRelayerCallArgs {
address: validate_eth_address(CUSTODIAN_ADDRESS),
}
.try_to_vec()
.unwrap(),
DEFAULT_GAS,
0,
);
res.assert_success();

let transfer_amount = 50;
// Send to Aurora contract with wrong message should failed
let res = contract.call(
CONTRACT_ACC.parse().unwrap(),
"ft_transfer_call",
json!({
"receiver_id": CONTRACT_ACC,
"amount": transfer_amount.to_string(),
"msg": "",
})
.to_string()
.as_bytes(),
DEFAULT_GAS,
1,
);
match res.outcome().status {
ExecutionStatus::Failure(_) => {}
_ => panic!("should panice"),
}

birchmd marked this conversation as resolved.
Show resolved Hide resolved
// Sending to excternal receiver with empty message should be success
let transfer_amount = 22;
let res = contract.call(
CONTRACT_ACC.parse().unwrap(),
"ft_transfer_call",
json!({
"receiver_id": "some-test-acc",
birchmd marked this conversation as resolved.
Show resolved Hide resolved
"amount": transfer_amount.to_string(),
"msg": "",
})
.to_string()
.as_bytes(),
DEFAULT_GAS,
1,
);
res.assert_success();

let balance = get_eth_on_near_balance(&master_account, DEPOSITED_RECIPIENT, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_AMOUNT - DEPOSITED_FEE);

let balance = get_eth_on_near_balance(&master_account, "some-test-acc", CONTRACT_ACC);
assert_eq!(balance, 2 * transfer_amount);

let balance = get_eth_on_near_balance(&master_account, CONTRACT_ACC, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_FEE - 2 * transfer_amount);

let balance = get_eth_balance(
&master_account,
validate_eth_address(RECIPIENT_ETH_ADDRESS),
CONTRACT_ACC,
);
assert_eq!(balance, 0);

let balance = get_eth_balance(
&master_account,
validate_eth_address(CUSTODIAN_ADDRESS),
CONTRACT_ACC,
);
assert_eq!(balance, 0);

let balance = total_supply(&master_account, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_AMOUNT);

let balance = total_eth_supply_on_near(&master_account, CONTRACT_ACC);
assert_eq!(balance, DEPOSITED_AMOUNT);

let balance = total_eth_supply_on_aurora(&master_account, CONTRACT_ACC);
assert_eq!(balance, 0);
}

#[test]
fn test_deposit_with_same_proof() {
let (_master_account, contract) = init(CUSTODIAN_ADDRESS);
Expand Down
42 changes: 23 additions & 19 deletions engine/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,26 +492,30 @@ impl EthConnectorContract {
args.receiver_id, args.amount,
));
// Verify message data before `ft_on_transfer` call to avoid verification panics
let message_data = self.parse_on_transfer_message(&args.msg);
// Check is transfer amount > fee
assert!(
args.amount > message_data.fee.as_u128(),
"{}",
ERR_NOT_ENOUGH_BALANCE_FOR_FEE,
);
if args.receiver_id.as_bytes() == &sdk::current_account_id()[..] {
let message_data = self.parse_on_transfer_message(&args.msg);
// Check is transfer amount > fee
assert!(
args.amount > message_data.fee.as_u128(),
"{}",
ERR_NOT_ENOUGH_BALANCE_FOR_FEE,
);

// Additional check overflow before process `ft_on_transfer`
// But don't check overflow for relayer
// Note: It can't overflow because the total supply doesn't change during transfer.
let amount_for_check = self
.ft
.internal_unwrap_balance_of_eth_on_aurora(message_data.recipient);
assert!(amount_for_check.checked_add(args.amount).is_some());
assert!(self
.ft
.total_eth_supply_on_aurora
.checked_add(args.amount)
.is_some());
// Additional check overflow before process `ft_on_transfer`
// But don't check overflow for relayer
// Note: It can't overflow because the total supply doesn't change during transfer.
let amount_for_check = self
.ft
.internal_unwrap_balance_of_eth_on_aurora(message_data.recipient);
assert!(amount_for_check.checked_add(args.amount).is_some());

// It'll call only on Aurora contract
assert!(self
.ft
.total_eth_supply_on_aurora
.checked_add(args.amount)
.is_some());
}

self.ft
.ft_transfer_call(&args.receiver_id, args.amount, &args.memo, args.msg);
Expand Down