Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evm: Set inbound limit when registering manager peer #260

Merged
merged 1 commit into from
Mar 5, 2024
Merged
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
9 changes: 7 additions & 2 deletions evm/src/NttManager/NttManagerState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,12 @@ abstract contract NttManagerState is
}

/// @inheritdoc INttManagerState
function setPeer(uint16 peerChainId, bytes32 peerContract, uint8 decimals) public onlyOwner {
function setPeer(
uint16 peerChainId,
bytes32 peerContract,
uint8 decimals,
uint256 inboundLimit
) public onlyOwner {
if (peerChainId == 0) {
revert InvalidPeerChainIdZero();
}
Expand All @@ -273,7 +278,7 @@ abstract contract NttManagerState is
_getPeersStorage()[peerChainId].peerAddress = peerContract;
_getPeersStorage()[peerChainId].tokenDecimals = decimals;

_setInboundLimit(TrimmedAmountLib.max(tokenDecimals_), peerChainId);
_setInboundLimit(inboundLimit.trim(tokenDecimals_, tokenDecimals_), peerChainId);

emit PeerUpdated(
peerChainId, oldPeer.peerAddress, oldPeer.tokenDecimals, peerContract, decimals
Expand Down
8 changes: 7 additions & 1 deletion evm/src/interfaces/INttManagerState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ interface INttManagerState {
/// @param peerChainId The chain ID of the peer.
/// @param peerContract The address of the peer nttManager contract.
/// @param decimals The number of decimals of the token on the peer chain.
function setPeer(uint16 peerChainId, bytes32 peerContract, uint8 decimals) external;
/// @param inboundLimit The inbound rate limit for the peer chain id
function setPeer(
uint16 peerChainId,
bytes32 peerContract,
uint8 decimals,
uint256 inboundLimit
) external;

/// @notice Checks if a message has been approved. The message should have at least
/// the minimum threshold of attestations from distinct endpoints.
Expand Down
32 changes: 24 additions & 8 deletions evm/test/IntegrationRelayer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ contract TestEndToEndRelayer is
wormholeTransceiverChain2.setWormholePeer(
chainId1, bytes32(uint256(uint160(address(wormholeTransceiverChain1))))
);
nttManagerChain2.setPeer(chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 9);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 9, type(uint64).max
);
DummyToken token2 = DummyTokenMintAndBurn(nttManagerChain2.token());
wormholeTransceiverChain2.setIsWormholeRelayingEnabled(chainId1, true);
wormholeTransceiverChain2.setIsWormholeEvmChain(chainId1);
Expand All @@ -180,7 +182,9 @@ contract TestEndToEndRelayer is
wormholeTransceiverChain1.setWormholePeer(
chainId2, bytes32(uint256(uint160((address(wormholeTransceiverChain2)))))
);
nttManagerChain1.setPeer(chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 7);
nttManagerChain1.setPeer(
chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 7, type(uint64).max
);

// Enable general relaying on the chain to transfer for the funds.
wormholeTransceiverChain1.setIsWormholeRelayingEnabled(chainId2, true);
Expand Down Expand Up @@ -267,14 +271,18 @@ contract TestEndToEndRelayer is
wormholeTransceiverChain2.setWormholePeer(
chainId1, bytes32(uint256(uint160(address(wormholeTransceiverChain1))))
);
nttManagerChain2.setPeer(chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 9);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 9, type(uint64).max
);
DummyToken token2 = DummyTokenMintAndBurn(nttManagerChain2.token());
wormholeTransceiverChain2.setIsWormholeRelayingEnabled(chainId1, true);
wormholeTransceiverChain2.setIsWormholeEvmChain(chainId1);

// Register peer contracts for the nttManager and transceiver. Transceivers and nttManager each have the concept of peers here.
vm.selectFork(sourceFork);
nttManagerChain1.setPeer(chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 7);
nttManagerChain1.setPeer(
chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 7, type(uint64).max
);
wormholeTransceiverChain1.setWormholePeer(
chainId2, bytes32(uint256(uint160((address(wormholeTransceiverChain2)))))
);
Expand Down Expand Up @@ -499,8 +507,12 @@ contract TestRelayerEndToEndManual is
nttManagerChain2.setInboundLimit(type(uint64).max, chainId1);

// Register peer contracts for the nttManager and transceiver. Transceivers and nttManager each have the concept of peers here.
nttManagerChain1.setPeer(chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 9);
nttManagerChain2.setPeer(chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 7);
nttManagerChain1.setPeer(
chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 9, type(uint64).max
);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 7, type(uint64).max
);
}

function test_relayerTransceiverAuth() public {
Expand Down Expand Up @@ -555,7 +567,9 @@ contract TestRelayerEndToEndManual is

bytes[] memory a;

nttManagerChain2.setPeer(chainId1, bytes32(uint256(uint160(address(0x1)))), 9);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(0x1)))), 9, type(uint64).max
);
vm.startPrank(relayer);
vm.expectRevert(); // bad nttManager peer
wormholeTransceiverChain2.receiveWormholeMessages(
Expand All @@ -568,7 +582,9 @@ contract TestRelayerEndToEndManual is
vm.stopPrank();

// Wrong caller - aka not relayer contract
nttManagerChain2.setPeer(chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 9);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 9, type(uint64).max
);
vm.prank(userD);
vm.expectRevert(
abi.encodeWithSelector(IWormholeTransceiverState.CallerNotRelayer.selector, userD)
Expand Down
8 changes: 6 additions & 2 deletions evm/test/IntegrationStandalone.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ contract TestEndToEndBase is Test, INttManagerEvents, IRateLimiterEvents {
nttManagerChain2.setInboundLimit(type(uint64).max, chainId1);

// Register peer contracts for the nttManager and transceiver. Transceivers and nttManager each have the concept of peers here.
nttManagerChain1.setPeer(chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 9);
nttManagerChain2.setPeer(chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 7);
nttManagerChain1.setPeer(
chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 9, type(uint64).max
);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 7, type(uint64).max
);

// Set peers for the transceivers
wormholeTransceiverChain1.setWormholePeer(
Expand Down
37 changes: 29 additions & 8 deletions evm/test/NttManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {

uint8 decimals = t.decimals();

nttManagerZeroRateLimiter.setPeer(chainId, toWormholeFormat(address(0x1)), 9);
nttManagerZeroRateLimiter.setPeer(
chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max
);

t.mintDummy(address(user_A), 5 * 10 ** decimals);

Expand Down Expand Up @@ -118,7 +120,9 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {

// register nttManager peer
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerZeroRateLimiter.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9);
nttManagerZeroRateLimiter.setPeer(
TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max
);

TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
Expand Down Expand Up @@ -283,13 +287,30 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {
nttManager.setThreshold(1);
}

// == threshold

function test_peerRegistrationLimitsCanBeUpdated() public {
bytes32 peer = toWormholeFormat(address(nttManager));
nttManager.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, 0);

IRateLimiter.RateLimitParams memory params =
nttManager.getInboundLimitParams(TransceiverHelpersLib.SENDING_CHAIN_ID);
assertEq(params.limit.getAmount(), 0);
assertEq(params.limit.getDecimals(), 8);

nttManager.setInboundLimit(type(uint64).max, TransceiverHelpersLib.SENDING_CHAIN_ID);
params = nttManager.getInboundLimitParams(TransceiverHelpersLib.SENDING_CHAIN_ID);
assertEq(params.limit.getAmount(), type(uint64).max / 10 ** (18 - 8));
assertEq(params.limit.getDecimals(), 8);
}

// === attestation

function test_onlyEnabledTransceiversCanAttest() public {
(DummyTransceiver e1,) = TransceiverHelpersLib.setup_transceivers(nttManagerOther);
nttManagerOther.removeTransceiver(address(e1));
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9);
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);

bytes memory transceiverMessage;
(, transceiverMessage) = TransceiverHelpersLib.buildTransceiverMessageWithNttManagerPayload(
Expand Down Expand Up @@ -327,7 +348,7 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {

// register nttManager peer
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9);
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);

TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
Expand All @@ -350,7 +371,7 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {

// register nttManager peer
bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9);
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);

TransceiverStructs.NttManagerMessage memory nttManagerMessage;
bytes memory transceiverMessage;
Expand Down Expand Up @@ -378,7 +399,7 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {
nttManagerOther.setThreshold(2);

bytes32 peer = toWormholeFormat(address(nttManager));
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9);
nttManagerOther.setPeer(TransceiverHelpersLib.SENDING_CHAIN_ID, peer, 9, type(uint64).max);

ITransceiverReceiver[] memory transceivers = new ITransceiverReceiver[](1);
transceivers[0] = e1;
Expand Down Expand Up @@ -415,7 +436,7 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {

uint8 decimals = token.decimals();

nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9);
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max);
nttManager.setOutboundLimit(packTrimmedAmount(type(uint64).max, 8).untrim(decimals));

token.mintDummy(address(user_A), 5 * 10 ** decimals);
Expand Down Expand Up @@ -528,7 +549,7 @@ contract TestNttManager is Test, INttManagerEvents, IRateLimiterEvents {

uint256 maxAmount = 5 * 10 ** decimals;
token.mintDummy(from, maxAmount);
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9);
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max);
nttManager.setOutboundLimit(packTrimmedAmount(type(uint64).max, 8).untrim(decimals));
nttManager.setInboundLimit(
packTrimmedAmount(type(uint64).max, 8).untrim(decimals),
Expand Down
2 changes: 1 addition & 1 deletion evm/test/RateLimit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract TestRateLimit is Test, IRateLimiterEvents {
nttManager = MockNttManagerContract(address(new ERC1967Proxy(address(implementation), "")));
nttManager.initialize();

nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9);
nttManager.setPeer(chainId, toWormholeFormat(address(0x1)), 9, type(uint64).max);
}

function test_outboundRateLimit_setLimitSimple() public {
Expand Down
6 changes: 4 additions & 2 deletions evm/test/Upgrades.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ contract TestUpgrades is Test, INttManagerEvents, IRateLimiterEvents {
nttManagerChain1.setPeer(
chainId2,
bytes32(uint256(uint160(address(nttManagerChain2)))),
DummyToken(nttManagerChain2.token()).decimals()
DummyToken(nttManagerChain2.token()).decimals(),
type(uint64).max
);
nttManagerChain2.setPeer(
chainId1,
bytes32(uint256(uint160(address(nttManagerChain1)))),
DummyToken(nttManagerChain1.token()).decimals()
DummyToken(nttManagerChain1.token()).decimals(),
type(uint64).max
);

wormholeTransceiverChain1.setWormholePeer(
Expand Down
2 changes: 1 addition & 1 deletion evm/test/libraries/NttManagerHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ library NttManagerHelpersLib {
address(nttManager.token()).staticcall(abi.encodeWithSignature("decimals()"));
uint8 tokenDecimals = abi.decode(queriedDecimals, (uint8));
recipientNttManager.setPeer(
SENDING_CHAIN_ID, toWormholeFormat(address(nttManager)), tokenDecimals
SENDING_CHAIN_ID, toWormholeFormat(address(nttManager)), tokenDecimals, type(uint64).max
);
recipientNttManager.setInboundLimit(inboundLimit.untrim(decimals), SENDING_CHAIN_ID);
}
Expand Down
Loading