Skip to content

Commit

Permalink
added changes to avoid watcher attesting all chain packets
Browse files Browse the repository at this point in the history
  • Loading branch information
tHeMaskedMan981 committed May 15, 2023
1 parent b9556b3 commit 03532e8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
2 changes: 0 additions & 2 deletions contracts/socket/SocketBatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ contract SocketBatcher is AccessControl {
*/
struct AttestRequest {
bytes32 packetId;
uint32 srcChainSlug;
bytes signature;
}

Expand Down Expand Up @@ -306,7 +305,6 @@ contract SocketBatcher is AccessControl {
for (uint256 index = 0; index < attestRequestslength; ) {
FastSwitchboard(switchBoardAddress_).attest(
attestRequests_[index].packetId,
attestRequests_[index].srcChainSlug,
attestRequests_[index].signature
);
unchecked {
Expand Down
10 changes: 5 additions & 5 deletions contracts/switchboard/default-switchboards/FastSwitchboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ contract FastSwitchboard is SwitchboardBase {
/**
* @dev Function to attest a packet
* @param packetId_ Packet ID
* @param srcChainSlug_ Chain slug of the source chain
* @param signature_ Signature of the packet
*/
function attest(
bytes32 packetId_,
uint32 srcChainSlug_,
bytes calldata signature_
) external {

uint32 srcChainSlug = uint32(uint256(packetId_) >> 224);
address watcher = SignatureVerifierLib.recoverSignerFromDigest(
keccak256(
abi.encode(address(this), srcChainSlug_, chainSlug, packetId_)
abi.encode(address(this), srcChainSlug, chainSlug, packetId_)
),
signature_
);

if (isAttested[watcher][packetId_]) revert AlreadyAttested();
if (!_hasRoleWithSlug(WATCHER_ROLE, srcChainSlug_, watcher))
if (!_hasRoleWithSlug(WATCHER_ROLE, srcChainSlug, watcher))
revert WatcherNotFound();

isAttested[watcher][packetId_] = true;
attestations[packetId_]++;

if (attestations[packetId_] >= totalWatchers[srcChainSlug_])
if (attestations[packetId_] >= totalWatchers[srcChainSlug])
isPacketValid[packetId_] = true;

emit PacketAttested(packetId_, watcher);
Expand Down
1 change: 0 additions & 1 deletion test/Setup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ contract Setup is Test {
// attest with packetId_, srcSlug and signature
FastSwitchboard(switchboardAddress).attest(
packetId_,
srcSlug,
attestSignature
);
}
Expand Down
36 changes: 28 additions & 8 deletions test/switchboard/default-switchboards/FastSwitchboard.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "../../Setup.t.sol";

contract FastSwitchboardTest is Setup {
bool isFast = true;
uint32 immutable remoteChainSlug = uint32(uint256(2));
uint32 immutable remoteChainSlug = uint32(2);
bytes32 immutable packetId = bytes32(0);
address watcher;
address altWatcher;
Expand All @@ -26,7 +26,7 @@ contract FastSwitchboardTest is Setup {
function setUp() external {
initialise();

_a.chainSlug = uint32(uint256(1));
_a.chainSlug = uint32(1);
vm.startPrank(_socketOwner);

fastSwitchboard = new FastSwitchboard(
Expand Down Expand Up @@ -106,7 +106,7 @@ contract FastSwitchboardTest is Setup {
vm.expectEmit(false, false, false, true);
emit PacketAttested(packetId, watcher);

fastSwitchboard.attest(packetId, remoteChainSlug, sig);
fastSwitchboard.attest(packetId, sig);

assertTrue(fastSwitchboard.isAttested(watcher, packetId));
}
Expand All @@ -125,12 +125,12 @@ contract FastSwitchboardTest is Setup {
vm.expectEmit(false, false, false, true);
emit PacketAttested(packetId, watcher);

fastSwitchboard.attest(packetId, remoteChainSlug, sig);
fastSwitchboard.attest(packetId, sig);

assertTrue(fastSwitchboard.isAttested(watcher, packetId));

vm.expectRevert(AlreadyAttested.selector);
fastSwitchboard.attest(packetId, remoteChainSlug, sig);
fastSwitchboard.attest(packetId, sig);
}

function testIsAllowed() external {
Expand All @@ -144,7 +144,7 @@ contract FastSwitchboardTest is Setup {
);
bytes memory sig = _createSignature(digest, _watcherPrivateKey);

fastSwitchboard.attest(packetId, remoteChainSlug, sig);
fastSwitchboard.attest(packetId, sig);

digest = keccak256(
abi.encode(
Expand All @@ -156,7 +156,7 @@ contract FastSwitchboardTest is Setup {
);
sig = _createSignature(digest, _altWatcherPrivateKey);

fastSwitchboard.attest(packetId, remoteChainSlug, sig);
fastSwitchboard.attest(packetId, sig);

uint256 proposeTime = block.timestamp -
fastSwitchboard.timeoutInSeconds();
Expand Down Expand Up @@ -344,6 +344,26 @@ contract FastSwitchboardTest is Setup {
bytes memory sig = "0x121234323123232323";

vm.expectRevert(InvalidSigLength.selector);
fastSwitchboard.attest(packetId, remoteChainSlug, sig);
fastSwitchboard.attest(packetId, sig);
}


function testAttesterCantAttestAllChains() public {

// Packet is coming from a chain different from remoteChainSlug
bytes32 altPacketId = bytes32(uint256(100)<<224);

bytes32 digest = keccak256(
abi.encode(
address(fastSwitchboard),
remoteChainSlug,
_a.chainSlug,
altPacketId
)
);
bytes memory sig = _createSignature(digest, _watcherPrivateKey);

vm.expectRevert(WatcherNotFound.selector);
fastSwitchboard.attest(altPacketId, sig);
}
}

0 comments on commit 03532e8

Please sign in to comment.