Skip to content

Commit

Permalink
test(fork): add SpokePortalFork
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Nov 15, 2024
1 parent a676b05 commit 217c87e
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 58 deletions.
2 changes: 1 addition & 1 deletion script/helpers/Utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ contract Utils {
address internal constant _OPTIMISM_SEPOLIA_WORMHOLE_RELAYER = 0x93BAD53DDfB6132b0aC8E37f6029163E63372cEE;

uint256 internal constant _MIN_WORMHOLE_GAS_LIMIT = 150_000;
uint256 internal constant _WORMHOLE_GAS_LIMIT = 200_000;
uint256 internal constant _WORMHOLE_GAS_LIMIT = 250_000;

// Instant confirmation. Guardians signs the VAA once the transaction has been included in a block.
uint8 internal constant _INSTANT_CONSISTENCY_LEVEL = 200;
Expand Down
3 changes: 3 additions & 0 deletions test/fork/ForkTestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ contract ForkTestBase is CastBase, ConfigureBase, DeployBase, Test {

deal(_DEPLOYER, 10 ether);
deal(_alice, 10 ether);
deal(_mHolder, 10 ether);

vm.startPrank(_DEPLOYER);

Expand Down Expand Up @@ -123,6 +124,7 @@ contract ForkTestBase is CastBase, ConfigureBase, DeployBase, Test {

deal(_DEPLOYER, 10 ether);
deal(_alice, 10 ether);
deal(_mHolder, 10 ether);

vm.startPrank(_DEPLOYER);

Expand Down Expand Up @@ -168,6 +170,7 @@ contract ForkTestBase is CastBase, ConfigureBase, DeployBase, Test {

deal(_DEPLOYER, 10 ether);
deal(_alice, 10 ether);
deal(_mHolder, 10 ether);

vm.startPrank(_DEPLOYER);

Expand Down
4 changes: 2 additions & 2 deletions test/fork/HubPortalFork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ contract HubPortalForkTests is ForkTestBase {

vm.stopPrank();

assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_hubPortal), amount_ - 1);
assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_hubPortal), amount_ = amount_ - 1);

bytes memory signedMessage_ = _signMessage(_hubGuardian, _MAINNET_WORMHOLE_CHAIN_ID);

vm.selectFork(_baseForkId);

_deliverMessage(_BASE_WORMHOLE_RELAYER, signedMessage_);

assertEq(IERC20(_baseSpokeMToken).balanceOf(_mHolder), amount_ - 1);
assertEq(IERC20(_baseSpokeMToken).balanceOf(_mHolder), amount_);
assertEq(IContinuousIndexing(_baseSpokeMToken).currentIndex(), mainnetIndex_);
}

Expand Down
51 changes: 6 additions & 45 deletions test/fork/Migrate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,57 +33,18 @@ contract Migrate is ForkTestBase, UpgradeBase {
address internal _spokeVault = makeAddr("spoke-vault");

function testFork_migrate() external {
vm.createSelectFork(vm.rpcUrl("mainnet"));
vm.selectFork(_mainnetForkId);

deal(_DEPLOYER, 10 ether);

vm.startPrank(_DEPLOYER);

HubPortal hubPortalImplementation_ = new HubPortal(
_MAINNET_M_TOKEN,
_MAINNET_REGISTRAR,
_MAINNET_WORMHOLE_CHAIN_ID
);

HubPortal hubPortal_ = HubPortal(
ICreateXLike(_CREATE_X_FACTORY).deployCreate3(
_computeSalt(_DEPLOYER, "Portal"),
abi.encodePacked(type(ERC1967Proxy).creationCode, abi.encode(address(hubPortalImplementation_), ""))
)
);

hubPortal_.initialize();

WormholeTransceiver wormholeTransceiverImplementation_ = new WormholeTransceiver(
address(hubPortal_),
_MAINNET_WORMHOLE_CORE_BRIDGE,
_MAINNET_WORMHOLE_RELAYER,
address(0),
_FINALIZED_CONSISTENCY_LEVEL,
_WORMHOLE_GAS_LIMIT
);

WormholeTransceiver wormholeTransceiver_ = WormholeTransceiver(
ICreateXLike(_CREATE_X_FACTORY).deployCreate3(
_computeSalt(_DEPLOYER, "WormholeTransceiver"),
abi.encodePacked(
type(ERC1967Proxy).creationCode,
abi.encode(address(wormholeTransceiverImplementation_), "")
)
)
);

wormholeTransceiver_.initialize();

IManagerBase(hubPortal_).setTransceiver(address(wormholeTransceiver_));
INttManager(hubPortal_).setThreshold(1);

assertEq(wormholeTransceiver_.gasLimit(), _WORMHOLE_GAS_LIMIT);
assertEq(WormholeTransceiver(_hubWormholeTransceiver).gasLimit(), 250_000);

string memory config_ = "test/fork/fixtures/upgrade-config.json";
_upgradeWormholeTransceiver(_loadWormholeConfig(config_, block.chainid));

assertEq(wormholeTransceiver_.gasLimit(), 250_000);
assertEq(WormholeTransceiver(_hubWormholeTransceiver).gasLimit(), 300_000);

_upgradeHubPortal(_loadPortalConfig(config_, block.chainid));

Expand Down Expand Up @@ -118,7 +79,7 @@ contract Migrate is ForkTestBase, UpgradeBase {
_MAINNET_WORMHOLE_RELAYER,
address(0),
_FINALIZED_CONSISTENCY_LEVEL,
_MIN_WORMHOLE_GAS_LIMIT
_WORMHOLE_GAS_LIMIT
);

WormholeTransceiver wormholeTransceiver_ = WormholeTransceiver(
Expand All @@ -145,7 +106,7 @@ contract Migrate is ForkTestBase, UpgradeBase {

vm.stopPrank();

assertEq(wormholeTransceiver_.gasLimit(), _MIN_WORMHOLE_GAS_LIMIT);
assertEq(WormholeTransceiver(_hubWormholeTransceiver).gasLimit(), 250_000);

vm.mockCall(
_MAINNET_REGISTRAR,
Expand All @@ -156,6 +117,6 @@ contract Migrate is ForkTestBase, UpgradeBase {
// Anyone can call migrate().
governor_.migrate();

assertEq(wormholeTransceiver_.gasLimit(), _WORMHOLE_GAS_LIMIT);
assertEq(WormholeTransceiver(_hubWormholeTransceiver).gasLimit(), 300_000);
}
}
125 changes: 125 additions & 0 deletions test/fork/SpokePortalFork.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.26;

import { IERC20 } from "../../lib/common/src/interfaces/IERC20.sol";
import { IContinuousIndexing } from "../../lib/protocol/src/interfaces/IContinuousIndexing.sol";

import { ISpokePortal } from "../../src/interfaces/ISpokePortal.sol";

import { ForkTestBase } from "./ForkTestBase.t.sol";

contract SpokePortalForkTests is ForkTestBase {
uint256 internal _amount;
uint128 internal _mainnetIndex;

function setUp() public override {
super.setUp();
_configurePortals();
}

/* ============ transfer ============ */

function testFork_transferToHubPortal() external {
_beforeTest();

vm.startPrank(_mHolder);

IERC20(_baseSpokeMToken).approve(_baseSpokePortal, _amount);

// Then, transfer M tokens back to the Hub chain.
_transfer(
_baseSpokePortal,
_MAINNET_WORMHOLE_CHAIN_ID,
_amount,
_toUniversalAddress(_mHolder),
_toUniversalAddress(_mHolder),
_quoteDeliveryPrice(_baseSpokePortal, _MAINNET_WORMHOLE_CHAIN_ID)
);

vm.stopPrank();

assertEq(IERC20(_baseSpokeMToken).balanceOf(_mHolder), 0);

bytes memory spokeSignedMessage_ = _signMessage(_baseSpokeGuardian, _BASE_WORMHOLE_CHAIN_ID);

vm.selectFork(_mainnetForkId);

uint256 balanceOfBefore_ = IERC20(_MAINNET_M_TOKEN).balanceOf(_mHolder);

_deliverMessage(_MAINNET_WORMHOLE_RELAYER, spokeSignedMessage_);

assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_hubPortal), 0);
assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_mHolder), balanceOfBefore_ + _amount);
}

function testFork_transferBetweenSpokePortals() external {
_beforeTest();

vm.startPrank(_mHolder);

IERC20(_baseSpokeMToken).approve(_baseSpokePortal, _amount);

// Then, transfer M tokens to the other Spoke chain.
_transfer(
_baseSpokePortal,
_OPTIMISM_WORMHOLE_CHAIN_ID,
_amount,
_toUniversalAddress(_mHolder),
_toUniversalAddress(_mHolder),
_quoteDeliveryPrice(_optimismSpokePortal, _OPTIMISM_WORMHOLE_CHAIN_ID)
);

vm.stopPrank();

assertEq(IERC20(_baseSpokeMToken).balanceOf(_mHolder), 0);

bytes memory spokeSignedMessage_ = _signMessage(_baseSpokeGuardian, _BASE_WORMHOLE_CHAIN_ID);

vm.selectFork(_optimismForkId);

_deliverMessage(_OPTIMISM_WORMHOLE_RELAYER, spokeSignedMessage_);

assertEq(IERC20(_optimismSpokeMToken).balanceOf(_mHolder), _amount);
assertEq(IContinuousIndexing(_optimismSpokeMToken).currentIndex(), _mainnetIndex);
}

function _beforeTest() internal {
_amount = 1_000e6;

// First, transfer M tokens to the Spoke chain.
vm.selectFork(_mainnetForkId);

_mainnetIndex = IContinuousIndexing(_MAINNET_M_TOKEN).currentIndex();

vm.startPrank(_mHolder);
vm.recordLogs();

IERC20(_MAINNET_M_TOKEN).approve(_hubPortal, _amount);

_transfer(
_hubPortal,
_BASE_WORMHOLE_CHAIN_ID,
_amount,
_toUniversalAddress(_mHolder),
_toUniversalAddress(_mHolder),
_quoteDeliveryPrice(_hubPortal, _BASE_WORMHOLE_CHAIN_ID)
);

vm.stopPrank();

assertEq(IERC20(_MAINNET_M_TOKEN).balanceOf(_hubPortal), _amount = _amount - 1);

bytes memory hubSignedMessage_ = _signMessage(_hubGuardian, _MAINNET_WORMHOLE_CHAIN_ID);

vm.selectFork(_baseForkId);

_deliverMessage(_BASE_WORMHOLE_RELAYER, hubSignedMessage_);

assertEq(IERC20(_baseSpokeMToken).balanceOf(_mHolder), _amount);
assertEq(IContinuousIndexing(_baseSpokeMToken).currentIndex(), _mainnetIndex);

// TODO: add excess test once underflow has been fixed
// ISpokePortal(_baseSpokePortal).excess();
}
}
6 changes: 3 additions & 3 deletions test/fork/fixtures/deploy-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"relayer": "0x27428DD2d3DD32A4D7f7C497eAaa23130d894911",
"special_relayer": "0x0000000000000000000000000000000000000000",
"consistency_level": "15",
"gas_limit": "200000"
"gas_limit": "250000"
}
}
},
Expand All @@ -25,7 +25,7 @@
"relayer": "0x706F82e9bb5b0813501714Ab5974216704980e31",
"special_relayer": "0x0000000000000000000000000000000000000000",
"consistency_level": "15",
"gas_limit": "200000"
"gas_limit": "250000"
}
},
"10": {
Expand All @@ -39,7 +39,7 @@
"relayer": "0x27428DD2d3DD32A4D7f7C497eAaa23130d894911",
"special_relayer": "0x0000000000000000000000000000000000000000",
"consistency_level": "15",
"gas_limit": "200000"
"gas_limit": "250000"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/fork/fixtures/migrator/MainnetMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract MainnetMigrator is Migrator {
uint8 internal constant _FINALIZED_CONSISTENCY_LEVEL = 15;

/// @dev Gas limit for Wormhole messages.
uint256 internal constant _WORMHOLE_GAS_LIMIT = 200_000;
uint256 internal constant _HIGH_WORMHOLE_GAS_LIMIT = 300_000;

/**
* @dev Constructs the MainnetMigrator contract.
Expand Down Expand Up @@ -61,7 +61,7 @@ contract MainnetMigrator is Migrator {
wormholeRelayerAddr: 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911,
specialRelayerAddr: address(0),
consistencyLevel: _FINALIZED_CONSISTENCY_LEVEL,
gasLimit: _WORMHOLE_GAS_LIMIT
gasLimit: _HIGH_WORMHOLE_GAS_LIMIT
})
);
} else if (block.chainid == 8453) {
Expand All @@ -80,7 +80,7 @@ contract MainnetMigrator is Migrator {
wormholeRelayerAddr: 0x706F82e9bb5b0813501714Ab5974216704980e31,
specialRelayerAddr: address(0),
consistencyLevel: _FINALIZED_CONSISTENCY_LEVEL,
gasLimit: _WORMHOLE_GAS_LIMIT
gasLimit: _HIGH_WORMHOLE_GAS_LIMIT
})
);
} else if (block.chainid == 10) {
Expand All @@ -99,7 +99,7 @@ contract MainnetMigrator is Migrator {
wormholeRelayerAddr: 0x27428DD2d3DD32A4D7f7C497eAaa23130d894911,
specialRelayerAddr: address(0),
consistencyLevel: _FINALIZED_CONSISTENCY_LEVEL,
gasLimit: _WORMHOLE_GAS_LIMIT
gasLimit: _HIGH_WORMHOLE_GAS_LIMIT
})
);
}
Expand Down
6 changes: 3 additions & 3 deletions test/fork/fixtures/upgrade-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"special_relayer": "0x0000000000000000000000000000000000000000",
"transceiver": "0x0763196A091575adF99e2306E5e90E0Be5154841",
"consistency_level": "15",
"gas_limit": "250000"
"gas_limit": "300000"
}
},
"8453": {
Expand All @@ -25,7 +25,7 @@
"special_relayer": "0x0000000000000000000000000000000000000000",
"transceiver": "0x0763196A091575adF99e2306E5e90E0Be5154841",
"consistency_level": "15",
"gas_limit": "250000"
"gas_limit": "300000"
}
},
"10": {
Expand All @@ -39,7 +39,7 @@
"special_relayer": "0x0000000000000000000000000000000000000000",
"transceiver": "0x0763196A091575adF99e2306E5e90E0Be5154841",
"consistency_level": "15",
"gas_limit": "250000"
"gas_limit": "300000"
}
}
}
Expand Down

0 comments on commit 217c87e

Please sign in to comment.