Skip to content

Commit

Permalink
Merge pull request #25 from exp-table/wormhole-vaa
Browse files Browse the repository at this point in the history
feat: add support for wormhole specialized relayers
  • Loading branch information
sujithsomraaj authored Aug 29, 2023
2 parents e2f450a + dd357e7 commit ca65e47
Show file tree
Hide file tree
Showing 22 changed files with 1,977 additions and 2,347 deletions.
12 changes: 8 additions & 4 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ StargateHelperTest:testCustomOrderingSG() (gas: 797976)
StargateHelperTest:testFancySG() (gas: 589535)
StargateHelperTest:testSimpleSG() (gas: 537570)
StargateHelperTest:testSimpleSGWithEstimates() (gas: 538082)
WormholeHelperTest:testCustomOrderingWormhole() (gas: 287323)
WormholeHelperTest:testFancyWormhole() (gas: 226022)
WormholeHelperTest:testMultiDstWormhole() (gas: 349780)
WormholeHelperTest:testSimpleWormhole() (gas: 179638)
WormholeAutomaticRelayerHelperTest:testCustomOrderingWormhole() (gas: 287323)
WormholeAutomaticRelayerHelperTest:testFancyWormhole() (gas: 226022)
WormholeAutomaticRelayerHelperTest:testMultiDstWormhole() (gas: 349780)
WormholeAutomaticRelayerHelperTest:testSimpleWormhole() (gas: 179638)
WormholeSpecializedRelayerHelperTest:testCustomOrderingWormhole() (gas: 591017)
WormholeSpecializedRelayerHelperTest:testFancyWormhole() (gas: 327851)
WormholeSpecializedRelayerHelperTest:testMultiDstWormhole() (gas: 626734)
WormholeSpecializedRelayerHelperTest:testSimpleWormhole() (gas: 330643)
80 changes: 20 additions & 60 deletions src/axelar/AxelarHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,16 @@ interface IAxelarExecutable {
}

interface IAxelarGateway {
function approveContractCall(
bytes calldata params,
bytes32 commandId
) external;
function approveContractCall(bytes calldata params, bytes32 commandId) external;

function callContract(
string calldata destinationChain,
string calldata contractAddress,
bytes calldata payload
) external;
function callContract(string calldata destinationChain, string calldata contractAddress, bytes calldata payload)
external;
}

/// @title Axelar Helper
/// @notice helps mock the message transfer using axelar bridge
contract AxelarHelper is Test {
bytes32 constant MESSAGE_EVENT_SELECTOR =
0x30ae6cc78c27e651745bf2ad08a11de83910ac1e347a52f7ac898c0fbef94dae;
bytes32 constant MESSAGE_EVENT_SELECTOR = 0x30ae6cc78c27e651745bf2ad08a11de83910ac1e347a52f7ac898c0fbef94dae;

function help(
string memory fromChain,
Expand All @@ -40,14 +33,7 @@ contract AxelarHelper is Test {
Vm.Log[] calldata logs
) external {
for (uint256 i; i < toGateway.length; i++) {
_help(
fromChain,
toGateway[i],
expDstChain[i],
forkId[i],
MESSAGE_EVENT_SELECTOR,
logs
);
_help(fromChain, toGateway[i], expDstChain[i], forkId[i], MESSAGE_EVENT_SELECTOR, logs);
}
}

Expand All @@ -69,20 +55,10 @@ contract AxelarHelper is Test {
uint256 forkId,
Vm.Log[] calldata logs
) external {
_help(
fromChain,
toGateway,
expDstChain,
forkId,
MESSAGE_EVENT_SELECTOR,
logs
);
_help(fromChain, toGateway, expDstChain, forkId, MESSAGE_EVENT_SELECTOR, logs);
}

function findLogs(
Vm.Log[] calldata logs,
uint256 length
) external pure returns (Vm.Log[] memory HLLogs) {
function findLogs(Vm.Log[] calldata logs, uint256 length) external pure returns (Vm.Log[] memory HLLogs) {
return _findLogs(logs, MESSAGE_EVENT_SELECTOR, length);
}

Expand Down Expand Up @@ -112,32 +88,20 @@ contract AxelarHelper is Test {
v.log = logs[i];

if (v.log.topics[0] == eventSelector) {
(v.destinationChain, v.destinationContract, v.payload) = abi
.decode(v.log.data, (string, string, bytes));
(v.destinationChain, v.destinationContract, v.payload) = abi.decode(v.log.data, (string, string, bytes));

/// FIXME: length based checks aren't sufficient
if (isStringsEqual(expDstChain, v.destinationChain)) {
string memory srcAddress = AddressHelper.toString(
address(uint160(uint256(v.log.topics[1])))
);
address dstContract = AddressHelper.fromString(
v.destinationContract
);
string memory srcAddress = AddressHelper.toString(address(uint160(uint256(v.log.topics[1]))));
address dstContract = AddressHelper.fromString(v.destinationContract);

IAxelarGateway(toGateway).approveContractCall(
abi.encode(
fromChain,
srcAddress,
dstContract,
keccak256(v.payload),
bytes32(0),
i
),
abi.encode(fromChain, srcAddress, dstContract, keccak256(v.payload), bytes32(0), i),
v.log.topics[2]
);

IAxelarExecutable(dstContract).execute(
v.log.topics[2], /// payloadHash
v.log.topics[2],
/// payloadHash
fromChain,
srcAddress,
v.payload
Expand All @@ -150,19 +114,15 @@ contract AxelarHelper is Test {
vm.selectFork(v.prevForkId);
}

function isStringsEqual(
string memory a,
string memory b
) public pure returns (bool) {
return (keccak256(abi.encodePacked((a))) ==
keccak256(abi.encodePacked((b))));
function isStringsEqual(string memory a, string memory b) public pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}

function _findLogs(
Vm.Log[] memory logs,
bytes32 dispatchSelector,
uint256 length
) internal pure returns (Vm.Log[] memory AxelarLogs) {
function _findLogs(Vm.Log[] memory logs, bytes32 dispatchSelector, uint256 length)
internal
pure
returns (Vm.Log[] memory AxelarLogs)
{
AxelarLogs = new Vm.Log[](length);

uint256 currentIndex = 0;
Expand Down
19 changes: 5 additions & 14 deletions src/axelar/lib/AddressHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ library AddressHelper {
return address(_addressBytes);
}

function remove0xPrefix(
string memory _hexString
) internal pure returns (string memory) {
function remove0xPrefix(string memory _hexString) internal pure returns (string memory) {
if (
bytes(_hexString).length >= 2 &&
bytes(_hexString)[0] == "0" &&
(bytes(_hexString)[1] == "x" || bytes(_hexString)[1] == "X")
bytes(_hexString).length >= 2 && bytes(_hexString)[0] == "0"
&& (bytes(_hexString)[1] == "x" || bytes(_hexString)[1] == "X")
) {
return substring(_hexString, 2, bytes(_hexString).length);
}
return _hexString;
}

function parseHexStringToBytes20(
string memory _hexString
) internal pure returns (bytes20) {
function parseHexStringToBytes20(string memory _hexString) internal pure returns (bytes20) {
bytes memory _bytesString = bytes(_hexString);
uint160 _parsedBytes = 0;
for (uint256 i = 0; i < _bytesString.length; i += 2) {
Expand All @@ -48,11 +43,7 @@ library AddressHelper {
}
}

function substring(
string memory _str,
uint256 _start,
uint256 _end
) internal pure returns (string memory) {
function substring(string memory _str, uint256 _start, uint256 _end) internal pure returns (string memory) {
bytes memory _strBytes = bytes(_str);
bytes memory _result = new bytes(_end - _start);
for (uint256 i = _start; i < _end; i++) {
Expand Down
Loading

0 comments on commit ca65e47

Please sign in to comment.