|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2024 Aztec Labs. |
| 3 | +pragma solidity >=0.8.27; |
| 4 | + |
| 5 | +import {Test} from "forge-std/Test.sol"; |
| 6 | +import {TestERC20} from "src/mock/TestERC20.sol"; |
| 7 | +import {IERC20} from "@oz/token/ERC20/IERC20.sol"; |
| 8 | +import {IInbox} from "@aztec/core/interfaces/messagebridge/IInbox.sol"; |
| 9 | +import { |
| 10 | + ProposeLib, |
| 11 | + INBOX_LAG_SECONDS, |
| 12 | + MAX_L1_TO_L2_MSGS_PER_CHECKPOINT |
| 13 | +} from "@aztec/core/libraries/rollup/ProposeLib.sol"; |
| 14 | +import {TimeLib, Slot, Timestamp} from "@aztec/core/libraries/TimeLib.sol"; |
| 15 | +import {Errors} from "@aztec/core/libraries/Errors.sol"; |
| 16 | +import {DataStructures} from "@aztec/core/libraries/DataStructures.sol"; |
| 17 | +import {InboxHarness} from "../harnesses/InboxHarness.sol"; |
| 18 | +import {TestConstants} from "../harnesses/TestConstants.sol"; |
| 19 | + |
| 20 | +contract ProposeLibHarness { |
| 21 | + constructor(uint256 _genesisTime, uint256 _slotDuration, uint256 _epochDuration) { |
| 22 | + TimeLib.initialize(_genesisTime, _slotDuration, _epochDuration, 1); |
| 23 | + } |
| 24 | + |
| 25 | + function validateInboxConsumption( |
| 26 | + IInbox _inbox, |
| 27 | + bytes32 _inboxRollingHash, |
| 28 | + uint256 _bucketHint, |
| 29 | + Slot _slotNumber, |
| 30 | + uint256 _parentTotalMsgCount |
| 31 | + ) external view returns (uint256) { |
| 32 | + return ProposeLib.validateInboxConsumption( |
| 33 | + _inbox, _inboxRollingHash, _bucketHint, _slotNumber, _parentTotalMsgCount |
| 34 | + ); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +contract ProposeInboxConsumptionTest is Test { |
| 39 | + uint256 internal constant GENESIS_TIME = 100_000; |
| 40 | + uint256 internal constant SLOT_DURATION = 36; |
| 41 | + uint256 internal constant EPOCH_DURATION = 32; |
| 42 | + uint256 internal constant HEIGHT = 10; |
| 43 | + uint256 internal constant SMALL_RING_SIZE = 4; |
| 44 | + |
| 45 | + Slot internal constant SLOT = Slot.wrap(10); |
| 46 | + |
| 47 | + ProposeLibHarness internal rollup; |
| 48 | + InboxHarness internal inbox; |
| 49 | + uint256 internal version = 0; |
| 50 | + |
| 51 | + // Start of the build frame for a checkpoint proposed in SLOT: it is built during the previous slot. |
| 52 | + uint256 internal buildFrameStart = GENESIS_TIME + (Slot.unwrap(SLOT) - 1) * SLOT_DURATION; |
| 53 | + // Buckets at or before the cutoff must be consumed by the checkpoint. |
| 54 | + uint256 internal cutoff = buildFrameStart - INBOX_LAG_SECONDS; |
| 55 | + |
| 56 | + function setUp() public { |
| 57 | + vm.warp(GENESIS_TIME); |
| 58 | + rollup = new ProposeLibHarness(GENESIS_TIME, SLOT_DURATION, EPOCH_DURATION); |
| 59 | + inbox = _deployInbox(TestConstants.AZTEC_INBOX_BUCKET_RING_SIZE); |
| 60 | + } |
| 61 | + |
| 62 | + function _deployInbox(uint256 _ringSize) internal returns (InboxHarness) { |
| 63 | + IERC20 feeAsset = new TestERC20("Fee Asset", "FA", address(this)); |
| 64 | + return new InboxHarness(address(rollup), feeAsset, version, HEIGHT, TestConstants.AZTEC_INBOX_LAG, _ringSize); |
| 65 | + } |
| 66 | + |
| 67 | + function _send(uint256 _salt) internal { |
| 68 | + inbox.sendL2Message( |
| 69 | + DataStructures.L2Actor({actor: bytes32(uint256(0x1000 + _salt)), version: version}), |
| 70 | + bytes32(uint256(0x2000 + _salt)), |
| 71 | + bytes32(uint256(0x3000 + _salt)) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + function _sendMany(uint256 _count) internal { |
| 76 | + for (uint256 i = 0; i < _count; i++) { |
| 77 | + _send(i); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + function testEmptyInbox() public { |
| 82 | + uint256 consumed = rollup.validateInboxConsumption(inbox, bytes32(0), 0, SLOT, 0); |
| 83 | + assertEq(consumed, 0, "consumed nothing on empty inbox"); |
| 84 | + } |
| 85 | + |
| 86 | + function testEmptyInboxRejectsNonZeroHash() public { |
| 87 | + vm.expectRevert( |
| 88 | + abi.encodeWithSelector(Errors.Rollup__InvalidInboxRollingHash.selector, bytes32(0), bytes32(uint256(1))) |
| 89 | + ); |
| 90 | + rollup.validateInboxConsumption(inbox, bytes32(uint256(1)), 0, SLOT, 0); |
| 91 | + } |
| 92 | + |
| 93 | + function testConsumeUpToLatestBucket() public { |
| 94 | + vm.warp(cutoff); |
| 95 | + _sendMany(3); |
| 96 | + bytes32 endHash = inbox.getBucket(1).rollingHash; |
| 97 | + |
| 98 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 99 | + uint256 consumed = rollup.validateInboxConsumption(inbox, endHash, 1, SLOT, 0); |
| 100 | + assertEq(consumed, 3, "consumed all three messages"); |
| 101 | + } |
| 102 | + |
| 103 | + function testExactCutoffBucketMustBeConsumed() public { |
| 104 | + // A bucket created exactly at the cutoff is the "latest bucket at/before build-frame start minus lag": |
| 105 | + // consuming nothing is no longer allowed. |
| 106 | + vm.warp(cutoff); |
| 107 | + _send(0); |
| 108 | + |
| 109 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 110 | + vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__UnconsumedInboxMessages.selector, 1)); |
| 111 | + rollup.validateInboxConsumption(inbox, bytes32(0), 0, SLOT, 0); |
| 112 | + } |
| 113 | + |
| 114 | + function testBucketPastCutoffNeedNotBeConsumed() public { |
| 115 | + vm.warp(cutoff + 1); |
| 116 | + _send(0); |
| 117 | + |
| 118 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 119 | + uint256 consumed = rollup.validateInboxConsumption(inbox, bytes32(0), 0, SLOT, 0); |
| 120 | + assertEq(consumed, 0, "nothing consumed"); |
| 121 | + } |
| 122 | + |
| 123 | + function testCapEscape() public { |
| 124 | + // One more message than the checkpoint cap, all before the cutoff. They spill over into buckets |
| 125 | + // 1..4 of 256 (the per-bucket cap) plus bucket 5 with the single excess message. |
| 126 | + vm.warp(cutoff - 100); |
| 127 | + _sendMany(MAX_L1_TO_L2_MSGS_PER_CHECKPOINT + 1); |
| 128 | + assertEq(inbox.getCurrentBucketSeq(), 5, "expected five buckets"); |
| 129 | + |
| 130 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 131 | + |
| 132 | + // Consuming through bucket 4 exhausts the cap exactly, so bucket 5 escapes the censorship assert |
| 133 | + // even though it is old. |
| 134 | + bytes32 endHash = inbox.getBucket(4).rollingHash; |
| 135 | + uint256 consumed = rollup.validateInboxConsumption(inbox, endHash, 4, SLOT, 0); |
| 136 | + assertEq(consumed, MAX_L1_TO_L2_MSGS_PER_CHECKPOINT, "consumed the full cap"); |
| 137 | + } |
| 138 | + |
| 139 | + function testNoCapEscapeAtExactCap() public { |
| 140 | + vm.warp(cutoff - 100); |
| 141 | + _sendMany(MAX_L1_TO_L2_MSGS_PER_CHECKPOINT + 1); |
| 142 | + |
| 143 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 144 | + |
| 145 | + // Stopping at bucket 3 leaves bucket 4 unconsumed; consuming through it would total exactly the cap, |
| 146 | + // which fits in one checkpoint, so there is no escape and the old bucket must be consumed. |
| 147 | + bytes32 endHash = inbox.getBucket(3).rollingHash; |
| 148 | + vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__UnconsumedInboxMessages.selector, 4)); |
| 149 | + rollup.validateInboxConsumption(inbox, endHash, 3, SLOT, 0); |
| 150 | + } |
| 151 | + |
| 152 | + function testCapEscapeAccountsForParentTotal() public { |
| 153 | + // Same layout as testCapEscape, but the parent checkpoint had already consumed one message: |
| 154 | + // buckets 2..5 then hold cap messages total, which fit in one checkpoint, so no escape from bucket 1. |
| 155 | + vm.warp(cutoff - 100); |
| 156 | + _sendMany(MAX_L1_TO_L2_MSGS_PER_CHECKPOINT + 1); |
| 157 | + |
| 158 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 159 | + |
| 160 | + bytes32 endHash = inbox.getBucket(4).rollingHash; |
| 161 | + vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__UnconsumedInboxMessages.selector, 5)); |
| 162 | + rollup.validateInboxConsumption(inbox, endHash, 4, SLOT, 1); |
| 163 | + } |
| 164 | + |
| 165 | + function testStaleHashRejection() public { |
| 166 | + vm.warp(cutoff); |
| 167 | + _send(0); |
| 168 | + vm.roll(block.number + 1); |
| 169 | + vm.warp(cutoff + 10); |
| 170 | + _send(1); |
| 171 | + |
| 172 | + bytes32 staleHash = inbox.getBucket(1).rollingHash; |
| 173 | + bytes32 currentHash = inbox.getBucket(2).rollingHash; |
| 174 | + |
| 175 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 176 | + vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidInboxRollingHash.selector, currentHash, staleHash)); |
| 177 | + rollup.validateInboxConsumption(inbox, staleHash, 2, SLOT, 0); |
| 178 | + } |
| 179 | + |
| 180 | + function testUnknownHashRejection() public { |
| 181 | + vm.warp(cutoff); |
| 182 | + _send(0); |
| 183 | + |
| 184 | + bytes32 unknownHash = bytes32(uint256(0xdead)); |
| 185 | + bytes32 bucketHash = inbox.getBucket(1).rollingHash; |
| 186 | + |
| 187 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 188 | + vm.expectRevert(abi.encodeWithSelector(Errors.Rollup__InvalidInboxRollingHash.selector, bucketHash, unknownHash)); |
| 189 | + rollup.validateInboxConsumption(inbox, unknownHash, 1, SLOT, 0); |
| 190 | + } |
| 191 | + |
| 192 | + function testHintBeyondCurrentBucketRejected() public { |
| 193 | + vm.warp(cutoff); |
| 194 | + _send(0); |
| 195 | + |
| 196 | + vm.expectRevert(abi.encodeWithSelector(Errors.Inbox__BucketOutOfWindow.selector, 2, 1)); |
| 197 | + rollup.validateInboxConsumption(inbox, bytes32(0), 2, SLOT, 0); |
| 198 | + } |
| 199 | + |
| 200 | + function testHintOnOverwrittenBucketRejected() public { |
| 201 | + InboxHarness smallRingInbox = _deployInbox(SMALL_RING_SIZE); |
| 202 | + |
| 203 | + uint256 timestamp = cutoff - 100; |
| 204 | + for (uint256 i = 1; i <= SMALL_RING_SIZE + 1; i++) { |
| 205 | + vm.roll(block.number + 1); |
| 206 | + vm.warp(timestamp + i); |
| 207 | + smallRingInbox.sendL2Message( |
| 208 | + DataStructures.L2Actor({actor: bytes32(uint256(0x1000 + i)), version: version}), |
| 209 | + bytes32(uint256(0x2000 + i)), |
| 210 | + bytes32(uint256(0x3000 + i)) |
| 211 | + ); |
| 212 | + } |
| 213 | + |
| 214 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 215 | + vm.expectRevert(abi.encodeWithSelector(Errors.Inbox__BucketOutOfWindow.selector, 1, SMALL_RING_SIZE + 1)); |
| 216 | + rollup.validateInboxConsumption(smallRingInbox, bytes32(0), 1, SLOT, 0); |
| 217 | + } |
| 218 | + |
| 219 | + function testConsumeBackwardsReverts() public { |
| 220 | + // Buckets 1 and 2 exist with distinct cumulative totals. A proposal that references bucket 1 while its |
| 221 | + // parent already consumed through bucket 2 would move consumption backwards. |
| 222 | + vm.warp(cutoff - 100); |
| 223 | + _send(0); |
| 224 | + vm.roll(block.number + 1); |
| 225 | + vm.warp(cutoff - 50); |
| 226 | + _send(1); |
| 227 | + |
| 228 | + uint256 parentTotal = inbox.getBucket(2).totalMsgCount; |
| 229 | + uint256 bucketTotal = inbox.getBucket(1).totalMsgCount; |
| 230 | + bytes32 bucketHash = inbox.getBucket(1).rollingHash; |
| 231 | + |
| 232 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 233 | + vm.expectRevert( |
| 234 | + abi.encodeWithSelector(Errors.Rollup__InboxConsumptionBehindParent.selector, parentTotal, bucketTotal) |
| 235 | + ); |
| 236 | + rollup.validateInboxConsumption(inbox, bucketHash, 1, SLOT, parentTotal); |
| 237 | + } |
| 238 | + |
| 239 | + function testEqualReferenceConsumesNothing() public { |
| 240 | + // The parent already consumed the current bucket; re-referencing it with an equal parent total consumes |
| 241 | + // nothing and returns the unchanged cumulative total. No newer pre-cutoff bucket exists. |
| 242 | + vm.warp(cutoff); |
| 243 | + _sendMany(2); |
| 244 | + uint256 total = inbox.getBucket(1).totalMsgCount; |
| 245 | + bytes32 endHash = inbox.getBucket(1).rollingHash; |
| 246 | + |
| 247 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 248 | + uint256 consumed = rollup.validateInboxConsumption(inbox, endHash, 1, SLOT, total); |
| 249 | + assertEq(consumed, total, "equal reference returns the unchanged total"); |
| 250 | + } |
| 251 | + |
| 252 | + function testCapUpperBoundReverts() public { |
| 253 | + // One more message than the checkpoint cap, all before the cutoff, referenced in a single proposal from |
| 254 | + // a fresh parent: the consumed delta exceeds what the circuits can insert. |
| 255 | + vm.warp(cutoff - 100); |
| 256 | + _sendMany(MAX_L1_TO_L2_MSGS_PER_CHECKPOINT + 1); |
| 257 | + assertEq(inbox.getCurrentBucketSeq(), 5, "expected five buckets"); |
| 258 | + |
| 259 | + bytes32 endHash = inbox.getBucket(5).rollingHash; |
| 260 | + |
| 261 | + vm.warp(GENESIS_TIME + Slot.unwrap(SLOT) * SLOT_DURATION); |
| 262 | + vm.expectRevert( |
| 263 | + abi.encodeWithSelector(Errors.Rollup__TooManyInboxMessagesConsumed.selector, MAX_L1_TO_L2_MSGS_PER_CHECKPOINT + 1) |
| 264 | + ); |
| 265 | + rollup.validateInboxConsumption(inbox, endHash, 5, SLOT, 0); |
| 266 | + } |
| 267 | +} |
0 commit comments