Skip to content

Commit

Permalink
Ignore AgentTransferFromNative bridge command (#1359)
Browse files Browse the repository at this point in the history
* do not dispatch transfer from native command

* test to make sure the behavior is off

* fmt

* naming
  • Loading branch information
alistair-singh authored Jan 7, 2025
1 parent bb130a4 commit c778b96
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
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

0 comments on commit c778b96

Please sign in to comment.