Skip to content

Commit

Permalink
evm: Use chain list from AdapterRegistry (#17)
Browse files Browse the repository at this point in the history
* evm: Use chain list from AdapterRegistry

* Tilt: Update endpoint commit
  • Loading branch information
bruce-riley authored Jan 13, 2025
1 parent b0954f4 commit c02fb27
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 164 deletions.
59 changes: 5 additions & 54 deletions evm/src/NttManager/ManagerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity >=0.8.8 <0.9.0;
import "wormhole-solidity-sdk/Utils.sol";
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";

import "example-messaging-endpoint/evm/src/interfaces/IAdapterRegistry.sol";
import "example-messaging-endpoint/evm/src/interfaces/IEndpointAdmin.sol";
import "example-messaging-endpoint/evm/src/interfaces/IEndpointIntegrator.sol";
import "example-messaging-executor/evm/src/interfaces/IExecutor.sol";
Expand Down Expand Up @@ -70,9 +71,6 @@ abstract contract ManagerBase is

bytes32 private constant THRESHOLD_SLOT = bytes32(uint256(keccak256("ntt.threshold")) - 1);

bytes32 private constant RECV_ENABLED_CHAINS_SLOT =
bytes32(uint256(keccak256("ntt.recvEnabledChains")) - 1);

// =============== Storage Getters/Setters ==============================================

function _getThresholdStorage()
Expand All @@ -93,13 +91,6 @@ abstract contract ManagerBase is
}
}

function _getChainsEnabledForReceiveStorage() internal pure returns (uint16[] storage $) {
uint256 slot = uint256(RECV_ENABLED_CHAINS_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}

// =============== External Logic =============================================================

/// @inheritdoc IManagerBase
Expand Down Expand Up @@ -257,7 +248,6 @@ abstract contract ManagerBase is
// However if the threshold is 0 (the initial case) we do increment to 1.
if (_threshold.num == 0) {
_threshold.num = 1;
_addChainEnabledForReceive(chain);
}

_checkThresholdInvariantsForChain(chain);
Expand All @@ -273,7 +263,7 @@ abstract contract ManagerBase is
IEndpointAdmin(address(endpoint)).disableRecvAdapter(address(this), chain, transceiver);

_Threshold storage _threshold = _getThresholdStorage()[chain];
uint8 numEnabled = IEndpointAdmin(address(endpoint)).getNumEnabledRecvAdaptersForChain(
uint8 numEnabled = IAdapterRegistry(address(endpoint)).getNumEnabledRecvAdaptersForChain(
address(this), chain
);

Expand All @@ -284,10 +274,6 @@ abstract contract ManagerBase is
emit ThresholdChanged(chainId, oldThreshold, numEnabled);
}

if (numEnabled == 0) {
_removeChainEnabledForReceive(chain);
}

_checkThresholdInvariantsForChain(chain);
}

Expand All @@ -301,7 +287,6 @@ abstract contract ManagerBase is
uint8 oldThreshold = _threshold.num;

_threshold.num = threshold;
_addChainEnabledForReceive(chain);
_checkThresholdInvariantsForChain(chain);
emit ThresholdChanged(chainId, oldThreshold, threshold);
}
Expand All @@ -313,41 +298,6 @@ abstract contract ManagerBase is
_getMessageSequenceStorage().num++;
}

/// @dev It's not an error if the chain is not in the list.
function _removeChainEnabledForReceive(
uint16 chain
) internal {
uint16[] storage chains = _getChainsEnabledForReceiveStorage();
uint256 len = chains.length;
for (uint256 idx = 0; (idx < len);) {
if (chains[idx] == chain) {
chains[idx] = chains[len - 1];
chains.pop();
return;
}
unchecked {
++idx;
}
}
}

/// @dev It's not an error if the chain is already in the list.
function _addChainEnabledForReceive(
uint16 chain
) internal {
uint16[] storage chains = _getChainsEnabledForReceiveStorage();
uint256 len = chains.length;
for (uint256 idx = 0; (idx < len);) {
if (chains[idx] == chain) {
return;
}
unchecked {
++idx;
}
}
chains.push(chain);
}

/// ============== Invariants =============================================

/// @dev When we add new immutables, this function should be updated
Expand All @@ -360,7 +310,8 @@ abstract contract ManagerBase is
}

function _checkThresholdInvariants() internal view {
uint16[] storage chains = _getChainsEnabledForReceiveStorage();
uint16[] memory chains =
IAdapterRegistry(address(endpoint)).getChainsEnabledForRecv(address(this));
uint256 len = chains.length;
for (uint256 idx = 0; (idx < len);) {
_checkThresholdInvariantsForChain(chains[idx]);
Expand All @@ -375,7 +326,7 @@ abstract contract ManagerBase is
uint16 chain
) internal view {
uint8 threshold = _getThresholdStorage()[chain].num;
uint8 numEnabled = IEndpointAdmin(address(endpoint)).getNumEnabledRecvAdaptersForChain(
uint8 numEnabled = IAdapterRegistry(address(endpoint)).getNumEnabledRecvAdaptersForChain(
address(this), chain
);

Expand Down
6 changes: 3 additions & 3 deletions evm/test/NttManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "wormhole-solidity-sdk/interfaces/IWormhole.sol";
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
import "wormhole-solidity-sdk/Utils.sol";
import "example-messaging-endpoint/evm/src/AdapterRegistry.sol";
import "example-messaging-endpoint/evm/src/interfaces/IAdapterRegistry.sol";
import "./libraries/TransceiverHelpers.sol";
import "./libraries/NttManagerHelpers.sol";
import "./mocks/DummyTransceiver.sol";
Expand Down Expand Up @@ -412,7 +412,7 @@ contract TestNttManager is Test, IRateLimiterEvents {
nttManager.setTransceiver(address(e));

vm.expectRevert(
abi.encodeWithSelector(AdapterRegistry.AdapterAlreadyRegistered.selector, address(e))
abi.encodeWithSelector(IAdapterRegistry.AdapterAlreadyRegistered.selector, address(e))
);
nttManager.setTransceiver(address(e));
}
Expand Down Expand Up @@ -512,7 +512,7 @@ contract TestNttManager is Test, IRateLimiterEvents {

// Registering a new transceiver should fail as we've hit the cap
DummyTransceiver c = new DummyTransceiver(chainId, address(endpoint));
vm.expectRevert(AdapterRegistry.TooManyAdapters.selector);
vm.expectRevert(IAdapterRegistry.TooManyAdapters.selector);
nttManager.setTransceiver(address(c));
}

Expand Down
4 changes: 2 additions & 2 deletions evm/test/NttManagerNoRateLimiting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "wormhole-solidity-sdk/interfaces/IWormhole.sol";
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
import "wormhole-solidity-sdk/Utils.sol";
import "example-messaging-endpoint/evm/src/AdapterRegistry.sol";
import "example-messaging-endpoint/evm/src/interfaces/IAdapterRegistry.sol";
import "./libraries/TransceiverHelpers.sol";
import "./libraries/NttManagerHelpers.sol";
import "./mocks/DummyTransceiver.sol";
Expand Down Expand Up @@ -264,7 +264,7 @@ contract TestNoRateLimitingNttManager is Test, IRateLimiterEvents {
nttManager.setTransceiver(address(e));

vm.expectRevert(
abi.encodeWithSelector(AdapterRegistry.AdapterAlreadyRegistered.selector, address(e))
abi.encodeWithSelector(IAdapterRegistry.AdapterAlreadyRegistered.selector, address(e))
);
nttManager.setTransceiver(address(e));
}
Expand Down
83 changes: 0 additions & 83 deletions evm/test/Threshold.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,87 +154,4 @@ contract TestThreshold is Test {
assertEq(0, nttManager.getThreshold(chainId2));
assertEq(2, nttManager.getThreshold(chainId3));
}

function test_chainsEnabledForReceive() public {
uint16[] memory chains = nttManager.getChainsEnabledForReceive();
assertEq(0, chains.length);

nttManager.addChainEnabledForReceive(42);
chains = nttManager.getChainsEnabledForReceive();
assertEq(1, chains.length);
assertEq(42, chains[0]);

nttManager.addChainEnabledForReceive(41);
chains = nttManager.getChainsEnabledForReceive();
assertEq(2, chains.length);
assertEq(42, chains[0]);
assertEq(41, chains[1]);

nttManager.addChainEnabledForReceive(43);
chains = nttManager.getChainsEnabledForReceive();
assertEq(3, chains.length);
assertEq(42, chains[0]);
assertEq(41, chains[1]);
assertEq(43, chains[2]);

// Adding the same thing again shouldn't do anything.
nttManager.addChainEnabledForReceive(43);
chains = nttManager.getChainsEnabledForReceive();
assertEq(3, chains.length);
assertEq(42, chains[0]);
assertEq(41, chains[1]);
assertEq(43, chains[2]);

nttManager.addChainEnabledForReceive(41);
chains = nttManager.getChainsEnabledForReceive();
assertEq(3, chains.length);
assertEq(42, chains[0]);
assertEq(41, chains[1]);
assertEq(43, chains[2]);

// Add one more.
nttManager.addChainEnabledForReceive(44);
chains = nttManager.getChainsEnabledForReceive();
assertEq(4, chains.length);
assertEq(42, chains[0]);
assertEq(41, chains[1]);
assertEq(43, chains[2]);
assertEq(44, chains[3]);

// Now test removing.

// Remove one from the middle. The last one should get moved into its slot.
nttManager.removeChainEnabledForReceive(41);
chains = nttManager.getChainsEnabledForReceive();
assertEq(3, chains.length);
assertEq(42, chains[0]);
assertEq(44, chains[1]);
assertEq(43, chains[2]);

// Removing something not in the list shouldn't do anything.
nttManager.removeChainEnabledForReceive(410);
chains = nttManager.getChainsEnabledForReceive();
assertEq(3, chains.length);
assertEq(42, chains[0]);
assertEq(44, chains[1]);
assertEq(43, chains[2]);

// Remove the first one. The last one should get moved into its slot.
nttManager.removeChainEnabledForReceive(42);
chains = nttManager.getChainsEnabledForReceive();
assertEq(2, chains.length);
assertEq(43, chains[0]);
assertEq(44, chains[1]);

// Remove the last one.
nttManager.removeChainEnabledForReceive(44);
chains = nttManager.getChainsEnabledForReceive();
assertEq(1, chains.length);
assertEq(43, chains[0]);

// Remove the only one.
nttManager.removeChainEnabledForReceive(43);
chains = nttManager.getChainsEnabledForReceive();
assertEq(0, chains.length);
}
}
16 changes: 0 additions & 16 deletions evm/test/mocks/MockNttManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@ contract MockNttManagerContract is NttManager {
}
}

function removeChainEnabledForReceive(
uint16 chain
) public {
_removeChainEnabledForReceive(chain);
}

function addChainEnabledForReceive(
uint16 chain
) public {
_addChainEnabledForReceive(chain);
}

function getChainsEnabledForReceive() public pure returns (uint16[] memory result) {
result = _getChainsEnabledForReceiveStorage();
}

function setGasLimitToZero(
uint16 peerChainId
) external onlyOwner {
Expand Down
8 changes: 4 additions & 4 deletions sdk/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ import axios from "axios";

export const NETWORK: "Devnet" = "Devnet";
export const ENDPOINTS: { [chainId in ChainId]?: `0x${string}` } = {
2: "0xF9f67913ba058BD29E699BfcD0eb0ec878555226",
4: "0x86ee2Ad4bca65764928680812A6C5315eDA9EEfd",
2: "0x9259cB76111dD04b06f3a514B4a70Dc7e704d450",
4: "0xee54955A543e1FC45fb8b3BdC89C72881bc1b8C2",
};
export const EXECUTORS: { [chainId in ChainId]?: `0x${string}` } = {
2: "0x0a65677098872f870224F6E9533734F4a4B0eBAB",
4: "0xB67841A38bF16EB9999dC7B6015746506e20F0aA",
};
export const WHG_ADAPTERS: { [chainId in ChainId]?: `0x${string}` } = {
2: "0x47A17F7E84Fb16c752352325F854A5358b4461d0",
4: "0x9683b5Cb8F274510782183CB20E76c3F7C1c884b",
2: "0x9Ee7A4e1DC11226d90db33f326168cf33B2456cC",
4: "0x3Cc81b56192c66968Df4D0E9D23c46Fa6ce42628",
};

type NativeSdkSigner<P extends Platform> = P extends "Evm"
Expand Down
2 changes: 1 addition & 1 deletion tilt/Dockerfile.deploy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WORKDIR /app/example-messaging-endpoint

RUN git init && \
git remote add origin https://github.com/wormholelabs-xyz/example-messaging-endpoint && \
git fetch --depth 1 origin 0f853ea0335937d611217f5048d677a4f46249fd && \
git fetch --depth 1 origin 026d20c6a276a3ee2e02c9f785df9c5279522566 && \
git checkout FETCH_HEAD

WORKDIR /app/example-messaging-endpoint/evm
Expand Down

0 comments on commit c02fb27

Please sign in to comment.