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

Ignore AgentTransferFromNative bridge command #1359

Merged
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
23 changes: 2 additions & 21 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
CreateChannelParams,
UpdateChannelParams,
SetOperatingModeParams,
TransferNativeFromAgentParams,
SetTokenTransferFeesParams,
SetPricingParametersParams,
RegisterForeignTokenParams,
Expand Down Expand Up @@ -197,10 +196,8 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
success = false;
}
} else if (message.command == Command.TransferNativeFromAgent) {
try Gateway(this).transferNativeFromAgent{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
// Disable this bridge command because now native Ether can be locked in an agent.
success = true;
} else if (message.command == Command.Upgrade) {
try Gateway(this).upgrade{gas: maxDispatchGas}(message.params) {}
catch {
Expand Down Expand Up @@ -382,16 +379,6 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
emit OperatingModeChanged(params.mode);
}

// @dev Transfer funds from an agent to a recipient account
function transferNativeFromAgent(bytes calldata data) external onlySelf {
TransferNativeFromAgentParams memory params = abi.decode(data, (TransferNativeFromAgentParams));

address agent = _ensureAgent(params.agentID);

_transferNativeFromAgent(agent, payable(params.recipient), params.amount);
emit AgentFundsWithdrawn(params.agentID, params.recipient, params.amount);
}

// @dev Set token fees of the gateway
function setTokenTransferFees(bytes calldata data) external onlySelf {
AssetsStorage.Layout storage $ = AssetsStorage.layout();
Expand Down Expand Up @@ -591,12 +578,6 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
return Call.verifyResult(success, returndata);
}

/// @dev Transfer ether from an agent
function _transferNativeFromAgent(address agent, address payable recipient, uint256 amount) internal {
bytes memory call = abi.encodeCall(AgentExecutor.transferNative, (recipient, amount));
_invokeOnAgent(agent, call);
}

/// @dev Define the dust threshold as the minimum cost to transfer ether between accounts
function _dustThreshold() internal view returns (uint256) {
return 21000 * tx.gasprice;
Expand Down
3 changes: 0 additions & 3 deletions contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ interface IGateway {
// Emitted when pricing params updated
event PricingParametersChanged();

// Emitted when funds are withdrawn from an agent
event AgentFundsWithdrawn(bytes32 indexed agentID, address indexed recipient, uint256 amount);

// Emitted when foreign token from polkadot registed
event ForeignTokenRegistered(bytes32 indexed tokenID, address token);

Expand Down
49 changes: 30 additions & 19 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ contract GatewayTest is Test {
return (Command.TransferNativeToken, abi.encode(params));
}

function makeTransferNativeFromAgentCommand(bytes32 agentID, address recipient, uint128 amount)
public
pure
returns (Command, bytes memory)
{
TransferNativeFromAgentParams memory params =
TransferNativeFromAgentParams({agentID: agentID, recipient: recipient, amount: amount});
return (Command.TransferNativeFromAgent, abi.encode(params));
}

function makeMockProof() public pure returns (Verification.Proof memory) {
return Verification.Proof({
header: Verification.ParachainHeader({
Expand Down Expand Up @@ -589,14 +599,6 @@ contract GatewayTest is Test {
MockGateway(address(gateway)).transferNativeTokenPublic(encodedParams);
}

function testAgentExecutionBadOrigin() public {
TransferNativeFromAgentParams memory params =
TransferNativeFromAgentParams({agentID: bytes32(0), recipient: address(this), amount: 1});

vm.expectRevert(Gateway.AgentDoesNotExist.selector);
MockGateway(address(gateway)).transferNativeFromAgentPublic(abi.encode(params));
}

function testAgentExecutionBadPayload() public {
AgentExecuteParams memory params = AgentExecuteParams({agentID: assetHubAgentID, payload: ""});

Expand Down Expand Up @@ -758,18 +760,30 @@ contract GatewayTest is Test {
assertEq(uint256(mode), 1);
}

function testWithdrawAgentFunds() public {
deal(assetHubAgent, 50 ether);
function testWithdrawAgentFundIsIgnored() public {
address recipient = makeAddr("test_recipeint");
uint128 amount = 1;

deal(assetHubAgent, amount);

address recipient = makeAddr("recipient");
(Command command, bytes memory params) = makeTransferNativeFromAgentCommand(assetHubAgentID, recipient, amount);

bytes memory params =
abi.encode(TransferNativeFromAgentParams({agentID: assetHubAgentID, recipient: recipient, amount: 3 ether}));
assertEq(address(assetHubAgent).balance, amount);
assertEq(recipient.balance, 0);

MockGateway(address(gateway)).transferNativeFromAgentPublic(params);
// Expect the gateway to emit `InboundMessageDispatched`
vm.expectEmit();
emit IGateway.InboundMessageDispatched(assetHubParaID.into(), 1, messageID, true);

assertEq(assetHubAgent.balance, 47 ether);
assertEq(recipient.balance, 3 ether);
hoax(relayer, 1 ether);
IGateway(address(gateway)).submitV1(
InboundMessage(assetHubParaID.into(), 1, command, params, maxDispatchGas, maxRefund, reward, messageID),
proof,
makeMockProof()
);

assertEq(address(assetHubAgent).balance, amount);
assertEq(recipient.balance, 0);
}

/**
Expand Down Expand Up @@ -961,9 +975,6 @@ contract GatewayTest is Test {

vm.expectRevert(Gateway.Unauthorized.selector);
Gateway(address(gateway)).upgrade("");

vm.expectRevert(Gateway.Unauthorized.selector);
Gateway(address(gateway)).transferNativeFromAgent("");
}

function testGetters() public {
Expand Down
4 changes: 0 additions & 4 deletions contracts/test/mocks/MockGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ contract MockGateway is Gateway {
this.setOperatingMode(params);
}

function transferNativeFromAgentPublic(bytes calldata params) external {
this.transferNativeFromAgent(params);
}

function setCommitmentsAreVerified(bool value) external {
commitmentsAreVerified = value;
}
Expand Down
Loading