From e94836bb2f7164947fc622c42d406ba7742c0ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C9=A4=20K=CE=AD=CF=9E=CF=9E=CF=88?= Date: Thu, 12 Nov 2026 08:13:24 +0100 Subject: [PATCH] fix(capacitors): correct updateMaxPacketLength packet count (division, not modulo) --- contracts/capacitors/HashChainCapacitor.sol | 13 ++- .../HashChainCapacitorUpdateMaxLength.t.sol | 91 +++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 test/capacitors/HashChainCapacitorUpdateMaxLength.t.sol diff --git a/contracts/capacitors/HashChainCapacitor.sol b/contracts/capacitors/HashChainCapacitor.sol index e06144eba..09d22ee04 100644 --- a/contracts/capacitors/HashChainCapacitor.sol +++ b/contracts/capacitors/HashChainCapacitor.sol @@ -76,8 +76,17 @@ contract HashChainCapacitor is BaseCapacitor { if (maxPacketLength_ < maxPacketLength) { uint64 lastPackedMsgIndex = messagePacked; uint64 packetCount = _nextPacketCount; - uint64 packets = (nextMessageCount - lastPackedMsgIndex) % - uint64(maxPacketLength_); + // FIX: pending messages are those added but not yet packed. + // nextMessageCount is 1-indexed (starts at 1), so added messages + // = nextMessageCount - 1. The number of complete packets of the + // new size is pending / newMax. The old code used + // (nextMessageCount - messagePacked) % maxPacketLength_: modulo + // gives the remainder (not the packet count) and the pending + // count was off by one, so it over-created packets, advanced + // messagePacked past the messages that exist, and permanently + // bricked the capacitor (addPackedMessage underflows). + uint64 pending = nextMessageCount - messagePacked - 1; + uint64 packets = pending / uint64(maxPacketLength_); _nextPacketCount += packets; diff --git a/test/capacitors/HashChainCapacitorUpdateMaxLength.t.sol b/test/capacitors/HashChainCapacitorUpdateMaxLength.t.sol new file mode 100644 index 000000000..c2e8a5d17 --- /dev/null +++ b/test/capacitors/HashChainCapacitorUpdateMaxLength.t.sol @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-3.0-only +pragma solidity 0.8.19; + +import "forge-std/Test.sol"; +import "../../contracts/capacitors/HashChainCapacitor.sol"; + +// Verifies the FIXED updateMaxPacketLength: shrinking the max eagerly +// creates exactly floor(pending / newMax) packets and never bricks the +// capacitor. Run against the fixed HashChainCapacitor.sol in this project. +contract HashChainCapacitorUpdateMaxFixed is Test { + address immutable _socket = address(0x1001); + address immutable _owner = address(this); + + HashChainCapacitor _hc; + + function setUp() external { + _hc = new HashChainCapacitor(_socket, _owner, 5); + } + + function _add(uint256 n) internal { + for (uint256 i = 0; i < n; i++) { + vm.prank(_socket); + _hc.addPackedMessage(bytes32(uint256(0xaaaa + i))); + } + } + + function testFixOnePending() external { + _add(1); // pending = 1 + _hc.updateMaxPacketLength(3); + // 1 / 3 = 0 packets: nothing eagerly created. + assertEq(_hc.messagePacked(), 0, "messagePacked stays 0"); + // Capacitor still works. + _add(2); + assertEq(_hc.nextMessageCount(), 4); + } + + function testFixZeroPending() external { + _hc.updateMaxPacketLength(3); // pending = 0 + assertEq(_hc.messagePacked(), 0, "messagePacked stays 0"); + _add(3); + assertEq(_hc.nextMessageCount(), 4); + } + + function testFixFourPending() external { + _add(4); // pending = 4 + _hc.updateMaxPacketLength(3); + // 4 / 3 = 1 packet of 3: messagePacked advances 0 -> 3. + assertEq(_hc.messagePacked(), 3, "one packet created"); + // The packet root is the hash-chain root after 3 messages. + assertEq(_hc.getRootByCount(0), _hc.messageRoots(3)); + // Capacitor still works; the 4th message is the start of the next packet. + _add(1); + assertEq(_hc.nextMessageCount(), 6); + // Adding 2 more completes a 3-message packet (messages 4,5,6). + _add(2); + assertEq(_hc.nextMessageCount(), 8); + } + + function testFixSixPending() external { + // With initial max 5, the 5th message auto-packs a packet, so only + // 1 message is pending when we shrink. Use a fresh capacitor with + // max 10 so all 6 messages are pending. + HashChainCapacitor hc10 = new HashChainCapacitor(_socket, _owner, 10); + for (uint256 i = 0; i < 6; i++) { + vm.prank(_socket); + hc10.addPackedMessage(bytes32(uint256(0xaaaa + i))); + } + assertEq(hc10.messagePacked(), 0, "nothing packed yet"); + hc10.updateMaxPacketLength(3); + // pending = 6; 6 / 3 = 2 packets: messagePacked advances 0 -> 6. + assertEq(hc10.messagePacked(), 6, "two packets created"); + assertEq(hc10.getRootByCount(0), hc10.messageRoots(3)); + assertEq(hc10.getRootByCount(1), hc10.messageRoots(6)); + // Capacitor still works. + vm.prank(_socket); + hc10.addPackedMessage(bytes32(uint256(0xffff))); + assertEq(hc10.nextMessageCount(), 8); + } + + function testFixShrinkAfterSomePacked() external { + // Pack one full packet of 5 first, then shrink. + _add(5); // auto-packs packet 0 (5 messages), messagePacked = 5 + assertEq(_hc.messagePacked(), 5); + _add(2); // 2 more pending + _hc.updateMaxPacketLength(3); + // pending = (8-1) - 5 = 2; 2 / 3 = 0 packets. messagePacked stays 5. + assertEq(_hc.messagePacked(), 5, "no extra packet"); + _add(1); // 3rd pending message -> completes a 3-message packet + assertEq(_hc.nextMessageCount(), 9); + } +}