Skip to content
Open
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
11 changes: 9 additions & 2 deletions contracts/src/Functions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ import {TokenInfo} from "./types/Common.sol";
import {ChannelID, Channel} from "./v1/Types.sol";
import {IGatewayBase} from "./interfaces/IGatewayBase.sol";
import {IGatewayV1} from "./v1/IGateway.sol";
import {Constants} from "./Constants.sol";

// Common functionality that is shared between V1 and V2
library Functions {
using Address for address;
using SafeNativeTransfer for address payable;
using SafeTokenTransferFrom for IERC20;

error AgentDoesNotExist();
error InvalidToken();
error InvalidAmount();
error ChannelDoesNotExist();

/// Looks up an agent contract address, failing if no such mapping exists
function ensureAgent(bytes32 agentID) internal view returns (address agent) {
Expand All @@ -35,6 +34,14 @@ library Functions {
}
}

/// Looks up an agent contract address, failing if no such mapping exists or if the agent is the primary asset hub agent
function ensureNonPrivilegedAgent(bytes32 agentID) internal view returns (address agent) {
if(agentID == Constants.ASSET_HUB_AGENT_ID) {
revert IGatewayBase.UnauthorizedPrivilegedAgent();
}
agent = ensureAgent(agentID);
}

/// @dev Ensure that the specified parachain has a channel allocated
function ensureChannel(ChannelID channelID) internal view returns (Channel storage ch) {
ch = CoreStorage.layout().channels[channelID];
Expand Down
1 change: 1 addition & 0 deletions contracts/src/interfaces/IGatewayBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface IGatewayBase {
error Unsupported();
error InvalidDestinationFee();
error AgentDoesNotExist();
error UnauthorizedPrivilegedAgent();
error TokenAlreadyRegistered();
error TokenMintFailed();
error TokenTransferFailed();
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/v2/Handlers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ library HandlersV2 {

function callContract(bytes32 origin, address executor, bytes calldata data) external {
CallContractParams memory params = abi.decode(data, (CallContractParams));
address agent = Functions.ensureAgent(origin);
address agent = Functions.ensureNonPrivilegedAgent(origin);
bytes memory call =
abi.encodeCall(AgentExecutor.callContract, (params.target, params.data, params.value));
Functions.invokeOnAgent(agent, executor, call);
Expand Down
26 changes: 26 additions & 0 deletions contracts/test/GatewayV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,32 @@ contract GatewayV2Test is Test {
vm.expectEmit(true, false, false, true);
emit IGatewayV2.InboundMessageDispatched(1, topic, true, relayerRewardAddress);

address bridgeHubAgent = IGatewayV2(address(gateway)).agentOf(Constants.BRIDGE_HUB_AGENT_ID);
vm.deal(bridgeHubAgent, 1 ether);
hoax(relayer, 1 ether);
IGatewayV2(address(gateway))
.v2_submit(
InboundMessageV2({
origin: Constants.BRIDGE_HUB_AGENT_ID,
nonce: 1,
topic: topic,
commands: makeCallContractCommand(0.1 ether)
}),
proof,
makeMockProof(),
relayerRewardAddress
);
}

function testAgentCallContractFailsForAssetHub() public {
bytes32 topic = keccak256("topic");

// The command fails, then the message is dispatched with success: false
vm.expectEmit(true, false, false, false);
emit IGatewayV2.CommandFailed(1, 0);
vm.expectEmit(true, false, false, true);
emit IGatewayV2.InboundMessageDispatched(1, topic, false, relayerRewardAddress);

vm.deal(assetHubAgent, 1 ether);
hoax(relayer, 1 ether);
IGatewayV2(address(gateway))
Expand Down
Loading