Skip to content

Commit

Permalink
Merge pull request #112 from XLabs/tbrv3-audit-fixes-4
Browse files Browse the repository at this point in the history
Fix setCanonicalPeer not setting the peer as canonical if it didn't exist
  • Loading branch information
scnale authored Jan 9, 2025
2 parents 7576f2d + 8ef8442 commit 83da6a6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 64 deletions.
2 changes: 1 addition & 1 deletion evm/src/assets/TbrBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ abstract contract TbrBase is PriceOracleIntegration {
bool isAlreadyPeer = state.peers[peer];
if (!isAlreadyPeer)
_addPeer(targetChain, peer);
else if (state.canonicalPeer != peer)
if (state.canonicalPeer != peer)
state.canonicalPeer = peer;
}

Expand Down
144 changes: 81 additions & 63 deletions evm/test/ConfigTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import "tbr/assets/TbrIds.sol";
contract ConfigTest is TbrTestBase {
using BytesParsing for bytes;

function addCanonicalPeer(uint16 peerChain) internal {
function addCanonicalPeer(uint16 peerChain, bytes32 peer) internal {
uint8 commandCount = 1;
vm.prank(owner);
invokeTbr(
abi.encodePacked(
tbr.exec768.selector,
Expand All @@ -27,7 +26,7 @@ contract ConfigTest is TbrTestBase {
commandCount,
UPDATE_CANONICAL_PEER_ID,
peerChain,
makeBytes32('peer')
peer
)
);
}
Expand Down Expand Up @@ -540,6 +539,75 @@ contract ConfigTest is TbrTestBase {
assertEq(isPeer, true);
}

function testAddCanonicalPeer() public {
uint16 peerChain = 1;
bytes32 newPeer = makeBytes32("peer");
uint8 commandCount = 1;

vm.expectRevert(NotAuthorized.selector);
addCanonicalPeer(peerChain, newPeer);

vm.startPrank(owner);
addCanonicalPeer(peerChain, newPeer);

(bool isPeer, ) = invokeStaticTbr(
abi.encodePacked(
tbr.get1959.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_QUERIES_ID,
commandCount,
IS_PEER_ID,
peerChain,
newPeer
)
).asBoolUnchecked(0);

assertEq(isPeer, true);

(bytes32 canonicalPeer, ) = invokeStaticTbr(
abi.encodePacked(
tbr.get1959.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_QUERIES_ID,
commandCount,
CANONICAL_PEER_ID,
peerChain
)
).asBytes32Unchecked(0);

assertEq(canonicalPeer, newPeer);

bytes32 anotherPeer = makeBytes32("anotherPeer");
addCanonicalPeer(peerChain, anotherPeer);

(isPeer, ) = invokeStaticTbr(
abi.encodePacked(
tbr.get1959.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_QUERIES_ID,
commandCount,
IS_PEER_ID,
peerChain,
anotherPeer
)
).asBoolUnchecked(0);

assertEq(isPeer, true);

(canonicalPeer, ) = invokeStaticTbr(
abi.encodePacked(
tbr.get1959.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_QUERIES_ID,
commandCount,
CANONICAL_PEER_ID,
peerChain
)
).asBytes32Unchecked(0);

assertEq(canonicalPeer, anotherPeer);
}

function testUpdateMaxGasDropoff() public {
uint16 chainId = 1;
uint32 maxGasDropoff = 1e6;
Expand Down Expand Up @@ -574,7 +642,9 @@ contract ConfigTest is TbrTestBase {
)
);

addCanonicalPeer(chainId);
vm.prank(owner);
addCanonicalPeer(chainId, makeBytes32("peer"));

vm.prank(admin);
invokeTbr(
abi.encodePacked(
Expand Down Expand Up @@ -677,7 +747,9 @@ contract ConfigTest is TbrTestBase {
)
);

addCanonicalPeer(chainId);
vm.prank(owner);
addCanonicalPeer(chainId, makeBytes32("peer"));

vm.prank(admin);
invokeTbr(
abi.encodePacked(
Expand Down Expand Up @@ -739,7 +811,9 @@ contract ConfigTest is TbrTestBase {
)
);

addCanonicalPeer(chainId);
vm.prank(owner);
addCanonicalPeer(chainId, makeBytes32("peer"));

vm.prank(admin);
invokeTbr(
abi.encodePacked(
Expand Down Expand Up @@ -767,51 +841,6 @@ contract ConfigTest is TbrTestBase {
assertEq(paused_, paused);
}

function testUpdateCanonicalPeer() public {
bytes32 newCanonicalPeer = makeBytes32("canonicalPeer");
uint16 peerChain = 1;
uint8 commandCount = 1;

vm.expectRevert(NotAuthorized.selector);
invokeTbr(
abi.encodePacked(
tbr.exec768.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_ID,
commandCount,
UPDATE_CANONICAL_PEER_ID,
peerChain,
newCanonicalPeer
)
);

vm.prank(owner);
invokeTbr(
abi.encodePacked(
tbr.exec768.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_ID,
commandCount,
UPDATE_CANONICAL_PEER_ID,
peerChain,
newCanonicalPeer
)
);

(bytes32 newCanonicalPeer_, ) = invokeStaticTbr(
abi.encodePacked(
tbr.get1959.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_QUERIES_ID,
commandCount,
CANONICAL_PEER_ID,
peerChain
)
).asBytes32Unchecked(0);

assertEq(newCanonicalPeer_, newCanonicalPeer);
}

function testSweepTokens() public {
uint usdtAmount = 1e6;
uint ethAmount = 1 ether;
Expand Down Expand Up @@ -839,7 +868,6 @@ contract ConfigTest is TbrTestBase {
function testIsChainSupported() public {
uint16 chainId = 1;
uint8 commandCount = 1;
bytes32 fakePeer = makeBytes32("peer");

(bool isSupported, ) = invokeStaticTbr(
abi.encodePacked(
Expand All @@ -854,17 +882,7 @@ contract ConfigTest is TbrTestBase {
assertEq(isSupported, false);

vm.prank(owner);
invokeTbr(
abi.encodePacked(
tbr.exec768.selector,
DISPATCHER_PROTOCOL_VERSION0,
CONFIG_ID,
commandCount,
UPDATE_CANONICAL_PEER_ID,
chainId,
fakePeer
)
);
addCanonicalPeer(chainId, makeBytes32("peer"));

(isSupported, ) = invokeStaticTbr(
abi.encodePacked(
Expand Down

0 comments on commit 83da6a6

Please sign in to comment.