@bananapus/core-v6 0.0.16 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ADMINISTRATION.md +1 -1
- package/ARCHITECTURE.md +2 -1
- package/AUDIT_INSTRUCTIONS.md +342 -0
- package/CHANGE_LOG.md +375 -0
- package/README.md +4 -4
- package/RISKS.md +171 -50
- package/SKILLS.md +9 -6
- package/USER_JOURNEYS.md +622 -0
- package/package.json +2 -2
- package/script/DeployPeriphery.s.sol +7 -1
- package/src/JBController.sol +5 -0
- package/src/JBDeadline.sol +3 -0
- package/src/JBDirectory.sol +2 -1
- package/src/JBMultiTerminal.sol +50 -9
- package/src/JBPermissions.sol +2 -0
- package/src/JBPrices.sol +8 -2
- package/src/JBRulesets.sol +3 -0
- package/src/JBSplits.sol +9 -5
- package/src/JBTerminalStore.sol +54 -47
- package/src/JBTokens.sol +3 -0
- package/src/interfaces/IJBTerminalStore.sol +3 -0
- package/src/libraries/JBFees.sol +2 -0
- package/src/libraries/JBMetadataResolver.sol +17 -4
- package/src/structs/JBBeforeCashOutRecordedContext.sol +4 -0
- package/test/TestAuditResponseDesignProofs.sol +434 -0
- package/test/TestDataHookFuzzing.sol +520 -0
- package/test/TestFeeFreeCashOutBypass.sol +617 -0
- package/test/TestL2SequencerPriceFeed.sol +292 -0
- package/test/TestMetadataOffsetOverflow.sol +179 -0
- package/test/TestMultiTerminalSurplus.sol +348 -0
- package/test/TestPermit2DataHook.t.sol +360 -0
- package/test/TestRulesetQueueing.sol +1 -2
- package/test/TestRulesetWeightCaching.sol +122 -124
- package/test/WeirdTokenTests.t.sol +37 -0
- package/test/regression/HoldFeesCashOutReserved.t.sol +415 -0
- package/test/regression/WeightCacheBoundary.t.sol +291 -0
- package/test/units/static/JBMultiTerminal/TestAddToBalanceOf.sol +2 -2
- package/test/units/static/JBMultiTerminal/TestCashOutTokensOf.sol +18 -17
- package/test/units/static/JBMultiTerminal/TestPay.sol +6 -4
- package/test/units/static/JBMultiTerminal/TestProcessHeldFeesOf.sol +206 -18
- package/test/units/static/JBMultiTerminal/TestUseAllowanceOf.sol +280 -0
- package/test/units/static/JBSplits/TestSelfManagedSplitGroups.sol +55 -12
- package/test/units/static/JBTerminalStore/TestRecordCashOutsFor.sol +72 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.6;
|
|
3
|
+
|
|
4
|
+
import {Test} from "forge-std/Test.sol";
|
|
5
|
+
import {AggregatorV2V3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV2V3Interface.sol";
|
|
6
|
+
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
|
|
7
|
+
import {JBChainlinkV3PriceFeed} from "../src/JBChainlinkV3PriceFeed.sol";
|
|
8
|
+
import {JBChainlinkV3SequencerPriceFeed} from "../src/JBChainlinkV3SequencerPriceFeed.sol";
|
|
9
|
+
|
|
10
|
+
/// @notice Tests for JBChainlinkV3PriceFeed and JBChainlinkV3SequencerPriceFeed failure scenarios.
|
|
11
|
+
/// Covers sequencer down, grace period, stale prices, negative/zero prices, and incomplete rounds.
|
|
12
|
+
contract TestL2SequencerPriceFeed_Local is Test {
|
|
13
|
+
// The price feed under test (with sequencer check).
|
|
14
|
+
JBChainlinkV3SequencerPriceFeed private _sequencerFeed;
|
|
15
|
+
|
|
16
|
+
// The base price feed (without sequencer check).
|
|
17
|
+
JBChainlinkV3PriceFeed private _baseFeed;
|
|
18
|
+
|
|
19
|
+
// Mock addresses for Chainlink aggregators.
|
|
20
|
+
address private _mockPriceFeed = makeAddr("priceFeed");
|
|
21
|
+
address private _mockSequencerFeed = makeAddr("sequencerFeed");
|
|
22
|
+
|
|
23
|
+
uint256 private constant _THRESHOLD = 3600; // 1 hour staleness threshold
|
|
24
|
+
uint256 private constant _GRACE_PERIOD = 3600; // 1 hour grace period after restart
|
|
25
|
+
|
|
26
|
+
function setUp() public {
|
|
27
|
+
// Warp to a realistic timestamp so subtraction does not underflow.
|
|
28
|
+
vm.warp(100_000);
|
|
29
|
+
|
|
30
|
+
vm.label(_mockPriceFeed, "MockPriceFeed");
|
|
31
|
+
vm.label(_mockSequencerFeed, "MockSequencerFeed");
|
|
32
|
+
|
|
33
|
+
// Mock the decimals() call on the price feed aggregator.
|
|
34
|
+
vm.mockCall(
|
|
35
|
+
_mockPriceFeed, abi.encodeWithSelector(AggregatorV3Interface.decimals.selector), abi.encode(uint8(8))
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
_baseFeed = new JBChainlinkV3PriceFeed(AggregatorV3Interface(_mockPriceFeed), _THRESHOLD);
|
|
39
|
+
|
|
40
|
+
_sequencerFeed = new JBChainlinkV3SequencerPriceFeed(
|
|
41
|
+
AggregatorV3Interface(_mockPriceFeed),
|
|
42
|
+
_THRESHOLD,
|
|
43
|
+
AggregatorV2V3Interface(_mockSequencerFeed),
|
|
44
|
+
_GRACE_PERIOD
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// -- Helper to mock a healthy price feed response --
|
|
49
|
+
function _mockHealthyPrice(int256 price, uint256 updatedAt) internal {
|
|
50
|
+
vm.mockCall(
|
|
51
|
+
_mockPriceFeed,
|
|
52
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
53
|
+
abi.encode(uint80(1), price, block.timestamp, updatedAt, uint80(1))
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// -- Helper to mock sequencer feed response --
|
|
58
|
+
function _mockSequencer(int256 answer, uint256 startedAt) internal {
|
|
59
|
+
vm.mockCall(
|
|
60
|
+
_mockSequencerFeed,
|
|
61
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
62
|
+
abi.encode(uint80(1), answer, startedAt, block.timestamp, uint80(1))
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// @notice Sequencer is down (answer=1): should revert.
|
|
67
|
+
function test_revertsWhenSequencerDown() public {
|
|
68
|
+
// answer=1 means sequencer is down.
|
|
69
|
+
// startedAt is well in the past (long enough for grace period).
|
|
70
|
+
_mockSequencer(int256(1), block.timestamp - _GRACE_PERIOD - 100);
|
|
71
|
+
_mockHealthyPrice(2000e8, block.timestamp);
|
|
72
|
+
|
|
73
|
+
vm.expectRevert(
|
|
74
|
+
abi.encodeWithSelector(
|
|
75
|
+
JBChainlinkV3SequencerPriceFeed.JBChainlinkV3SequencerPriceFeed_SequencerDownOrRestarting.selector,
|
|
76
|
+
block.timestamp,
|
|
77
|
+
_GRACE_PERIOD,
|
|
78
|
+
block.timestamp - _GRACE_PERIOD - 100
|
|
79
|
+
)
|
|
80
|
+
);
|
|
81
|
+
_sequencerFeed.currentUnitPrice(18);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// @notice Sequencer just restarted and still within grace period: should revert.
|
|
85
|
+
function test_revertsWhenWithinGracePeriod() public {
|
|
86
|
+
// Sequencer is up (answer=0), but restarted very recently.
|
|
87
|
+
uint256 restartedAt = block.timestamp - _GRACE_PERIOD / 2; // half the grace period ago
|
|
88
|
+
_mockSequencer(int256(0), restartedAt);
|
|
89
|
+
_mockHealthyPrice(2000e8, block.timestamp);
|
|
90
|
+
|
|
91
|
+
vm.expectRevert(
|
|
92
|
+
abi.encodeWithSelector(
|
|
93
|
+
JBChainlinkV3SequencerPriceFeed.JBChainlinkV3SequencerPriceFeed_SequencerDownOrRestarting.selector,
|
|
94
|
+
block.timestamp,
|
|
95
|
+
_GRACE_PERIOD,
|
|
96
|
+
restartedAt
|
|
97
|
+
)
|
|
98
|
+
);
|
|
99
|
+
_sequencerFeed.currentUnitPrice(18);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// @notice Sequencer feed returns startedAt=0 (invalid round): should revert.
|
|
103
|
+
function test_revertsWhenSequencerRoundInvalid() public {
|
|
104
|
+
_mockSequencer(int256(0), 0);
|
|
105
|
+
_mockHealthyPrice(2000e8, block.timestamp);
|
|
106
|
+
|
|
107
|
+
vm.expectRevert(
|
|
108
|
+
abi.encodeWithSelector(
|
|
109
|
+
JBChainlinkV3SequencerPriceFeed.JBChainlinkV3SequencerPriceFeed_InvalidRound.selector
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
_sequencerFeed.currentUnitPrice(18);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// @notice After grace period passes, sequencer feed works normally.
|
|
116
|
+
function test_worksAfterGracePeriodPasses() public {
|
|
117
|
+
// Sequencer is up (answer=0), restarted long enough ago.
|
|
118
|
+
uint256 restartedAt = block.timestamp - _GRACE_PERIOD - 1;
|
|
119
|
+
_mockSequencer(int256(0), restartedAt);
|
|
120
|
+
_mockHealthyPrice(2000e8, block.timestamp);
|
|
121
|
+
|
|
122
|
+
uint256 price = _sequencerFeed.currentUnitPrice(18);
|
|
123
|
+
// 2000e8 with 8 decimals -> adjusted to 18 decimals: 2000e18.
|
|
124
|
+
assertEq(price, 2000e18, "price should be 2000 with 18 decimals");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// @notice Stale price beyond threshold: should revert (base feed behavior).
|
|
128
|
+
function test_revertsOnStalePrice() public {
|
|
129
|
+
uint256 staleUpdatedAt = block.timestamp - _THRESHOLD - 1;
|
|
130
|
+
|
|
131
|
+
vm.mockCall(
|
|
132
|
+
_mockPriceFeed,
|
|
133
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
134
|
+
abi.encode(uint80(1), int256(2000e8), block.timestamp, staleUpdatedAt, uint80(1))
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
vm.expectRevert(
|
|
138
|
+
abi.encodeWithSelector(
|
|
139
|
+
JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_StalePrice.selector,
|
|
140
|
+
block.timestamp,
|
|
141
|
+
_THRESHOLD,
|
|
142
|
+
staleUpdatedAt
|
|
143
|
+
)
|
|
144
|
+
);
|
|
145
|
+
_baseFeed.currentUnitPrice(18);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// @notice Stale price on sequencer feed (sequencer healthy, price stale): should revert.
|
|
149
|
+
function test_sequencerFeed_revertsOnStalePrice() public {
|
|
150
|
+
uint256 restartedAt = block.timestamp - _GRACE_PERIOD - 1;
|
|
151
|
+
_mockSequencer(int256(0), restartedAt);
|
|
152
|
+
|
|
153
|
+
uint256 staleUpdatedAt = block.timestamp - _THRESHOLD - 1;
|
|
154
|
+
vm.mockCall(
|
|
155
|
+
_mockPriceFeed,
|
|
156
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
157
|
+
abi.encode(uint80(1), int256(2000e8), block.timestamp, staleUpdatedAt, uint80(1))
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
vm.expectRevert(
|
|
161
|
+
abi.encodeWithSelector(
|
|
162
|
+
JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_StalePrice.selector,
|
|
163
|
+
block.timestamp,
|
|
164
|
+
_THRESHOLD,
|
|
165
|
+
staleUpdatedAt
|
|
166
|
+
)
|
|
167
|
+
);
|
|
168
|
+
_sequencerFeed.currentUnitPrice(18);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// @notice Negative price: should revert.
|
|
172
|
+
function test_revertsOnNegativePrice() public {
|
|
173
|
+
vm.mockCall(
|
|
174
|
+
_mockPriceFeed,
|
|
175
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
176
|
+
abi.encode(uint80(1), int256(-100), block.timestamp, block.timestamp, uint80(1))
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
vm.expectRevert(
|
|
180
|
+
abi.encodeWithSelector(JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_NegativePrice.selector, int256(-100))
|
|
181
|
+
);
|
|
182
|
+
_baseFeed.currentUnitPrice(18);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/// @notice Fuzz: any negative price should revert.
|
|
186
|
+
function testFuzz_revertsOnAnyNegativePrice(int256 _price) public {
|
|
187
|
+
vm.assume(_price < 0);
|
|
188
|
+
|
|
189
|
+
vm.mockCall(
|
|
190
|
+
_mockPriceFeed,
|
|
191
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
192
|
+
abi.encode(uint80(1), _price, block.timestamp, block.timestamp, uint80(1))
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
vm.expectRevert(
|
|
196
|
+
abi.encodeWithSelector(JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_NegativePrice.selector, _price)
|
|
197
|
+
);
|
|
198
|
+
_baseFeed.currentUnitPrice(18);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// @notice Zero price: should revert (price <= 0 check).
|
|
202
|
+
function test_revertsOnZeroPrice() public {
|
|
203
|
+
vm.mockCall(
|
|
204
|
+
_mockPriceFeed,
|
|
205
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
206
|
+
abi.encode(uint80(1), int256(0), block.timestamp, block.timestamp, uint80(1))
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
vm.expectRevert(
|
|
210
|
+
abi.encodeWithSelector(JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_NegativePrice.selector, int256(0))
|
|
211
|
+
);
|
|
212
|
+
_baseFeed.currentUnitPrice(18);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// @notice Incomplete round (updatedAt=0): should revert.
|
|
216
|
+
function test_revertsOnIncompleteRound() public {
|
|
217
|
+
vm.mockCall(
|
|
218
|
+
_mockPriceFeed,
|
|
219
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
220
|
+
abi.encode(uint80(1), int256(2000e8), block.timestamp, uint256(0), uint80(1))
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
vm.expectRevert(abi.encodeWithSelector(JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_IncompleteRound.selector));
|
|
224
|
+
_baseFeed.currentUnitPrice(18);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// @notice Incomplete round on sequencer feed path: should revert.
|
|
228
|
+
function test_sequencerFeed_revertsOnIncompleteRound() public {
|
|
229
|
+
uint256 restartedAt = block.timestamp - _GRACE_PERIOD - 1;
|
|
230
|
+
_mockSequencer(int256(0), restartedAt);
|
|
231
|
+
|
|
232
|
+
// updatedAt = 0 means incomplete round.
|
|
233
|
+
vm.mockCall(
|
|
234
|
+
_mockPriceFeed,
|
|
235
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
236
|
+
abi.encode(uint80(1), int256(2000e8), block.timestamp, uint256(0), uint80(1))
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
vm.expectRevert(abi.encodeWithSelector(JBChainlinkV3PriceFeed.JBChainlinkV3PriceFeed_IncompleteRound.selector));
|
|
240
|
+
_sequencerFeed.currentUnitPrice(18);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/// @notice Price at exactly the threshold boundary: should succeed.
|
|
244
|
+
function test_priceAtExactThresholdBoundary() public {
|
|
245
|
+
// updatedAt = block.timestamp - THRESHOLD, so block.timestamp == THRESHOLD + updatedAt.
|
|
246
|
+
// The condition is block.timestamp > THRESHOLD + updatedAt, so this should NOT revert.
|
|
247
|
+
uint256 updatedAt = block.timestamp - _THRESHOLD;
|
|
248
|
+
|
|
249
|
+
vm.mockCall(
|
|
250
|
+
_mockPriceFeed,
|
|
251
|
+
abi.encodeWithSelector(AggregatorV3Interface.latestRoundData.selector),
|
|
252
|
+
abi.encode(uint80(1), int256(1500e8), block.timestamp, updatedAt, uint80(1))
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
uint256 price = _baseFeed.currentUnitPrice(18);
|
|
256
|
+
assertEq(price, 1500e18, "price at exact threshold boundary should succeed");
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/// @notice Grace period at exact boundary: sequencer restarted exactly grace period ago should still revert.
|
|
260
|
+
function test_gracePeriodExactBoundary() public {
|
|
261
|
+
// startedAt = block.timestamp - GRACE_PERIOD.
|
|
262
|
+
// The condition is block.timestamp <= GRACE_PERIOD_TIME + startedAt,
|
|
263
|
+
// which is block.timestamp <= block.timestamp, which is true. So should revert.
|
|
264
|
+
uint256 startedAt = block.timestamp - _GRACE_PERIOD;
|
|
265
|
+
_mockSequencer(int256(0), startedAt);
|
|
266
|
+
_mockHealthyPrice(2000e8, block.timestamp);
|
|
267
|
+
|
|
268
|
+
vm.expectRevert(
|
|
269
|
+
abi.encodeWithSelector(
|
|
270
|
+
JBChainlinkV3SequencerPriceFeed.JBChainlinkV3SequencerPriceFeed_SequencerDownOrRestarting.selector,
|
|
271
|
+
block.timestamp,
|
|
272
|
+
_GRACE_PERIOD,
|
|
273
|
+
startedAt
|
|
274
|
+
)
|
|
275
|
+
);
|
|
276
|
+
_sequencerFeed.currentUnitPrice(18);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/// @notice Decimal adjustment: price with fewer decimals scaled up correctly.
|
|
280
|
+
function test_decimalScaling() public {
|
|
281
|
+
_mockHealthyPrice(1e8, block.timestamp); // 1.00000000 with 8 decimals
|
|
282
|
+
|
|
283
|
+
uint256 price18 = _baseFeed.currentUnitPrice(18);
|
|
284
|
+
assertEq(price18, 1e18, "1e8 at 8 decimals should scale to 1e18 at 18 decimals");
|
|
285
|
+
|
|
286
|
+
uint256 price8 = _baseFeed.currentUnitPrice(8);
|
|
287
|
+
assertEq(price8, 1e8, "1e8 at 8 decimals should stay 1e8 at 8 decimals");
|
|
288
|
+
|
|
289
|
+
uint256 price6 = _baseFeed.currentUnitPrice(6);
|
|
290
|
+
assertEq(price6, 1e6, "1e8 at 8 decimals should scale to 1e6 at 6 decimals");
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity 0.8.26;
|
|
3
|
+
|
|
4
|
+
import {Test} from "forge-std/Test.sol";
|
|
5
|
+
import {MetadataResolverHelper} from "./helpers/MetadataResolverHelper.sol";
|
|
6
|
+
import {JBMetadataResolver} from "../src/libraries/JBMetadataResolver.sol";
|
|
7
|
+
|
|
8
|
+
/// @notice Regression test for uint8 overflow in addToMetadata offset increment.
|
|
9
|
+
///
|
|
10
|
+
/// @dev When `addToMetadata` grows the lookup table by one word, it increments every existing
|
|
11
|
+
/// offset byte by 1. If any offset byte is already 255, the old code silently wrapped to 0,
|
|
12
|
+
/// corrupting the lookup table. After the fix it must revert with
|
|
13
|
+
/// `JBMetadataResolver_MetadataTooLong`.
|
|
14
|
+
contract TestMetadataOffsetOverflow is Test {
|
|
15
|
+
MetadataResolverHelper parser;
|
|
16
|
+
|
|
17
|
+
function setUp() external {
|
|
18
|
+
parser = new MetadataResolverHelper();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// @notice Build metadata whose existing offset byte is 255, then call `addToMetadata` so that
|
|
22
|
+
/// the table must grow by one word. The offset increment (255 + 1) must revert.
|
|
23
|
+
function test_addToMetadata_offsetOverflow_reverts() external {
|
|
24
|
+
// We construct metadata by hand so that an existing offset byte equals 255.
|
|
25
|
+
//
|
|
26
|
+
// Layout:
|
|
27
|
+
// [0..31] 32 bytes reserved (all zeros)
|
|
28
|
+
// [32..36] id1 (4 bytes) + offset1 (1 byte = 255)
|
|
29
|
+
// [37..63] padding (zeros) to fill the first lookup-table word
|
|
30
|
+
//
|
|
31
|
+
// For offset1 = 255, the "data" region notionally starts at byte 255*32 = 8160.
|
|
32
|
+
// We need originalMetadata.length >= 255*32 + 32 = 8192 so the library can compute
|
|
33
|
+
// `numberOfWordslastData` without underflow and the metadata looks structurally valid.
|
|
34
|
+
//
|
|
35
|
+
// The lookup table occupies word 1 (bytes 32..63), so firstOffset = 2 would be
|
|
36
|
+
// the normal case for a single entry fitting in one word. But we're forcing offset1 = 255
|
|
37
|
+
// to trigger the overflow.
|
|
38
|
+
//
|
|
39
|
+
// However, the code path that increments offsets only fires when the new entry does NOT
|
|
40
|
+
// fit in the current last table word (i + TOTAL_ID_SIZE >= firstOffset * WORD_SIZE).
|
|
41
|
+
// With one existing entry (5 bytes at position 32..36) and firstOffset derived from the
|
|
42
|
+
// first entry's offset byte, we need to ensure the table is full enough to trigger growth.
|
|
43
|
+
//
|
|
44
|
+
// Strategy: fill the lookup table so that it occupies exactly `firstOffset - 1` words
|
|
45
|
+
// (i.e. words 1 through firstOffset-1), and the last entry's position is near the end
|
|
46
|
+
// of that region. Then adding one more entry will overflow the table word, triggering
|
|
47
|
+
// the offset increment loop.
|
|
48
|
+
//
|
|
49
|
+
// Simpler approach: pack 6 entries into the table (6 * 5 = 30 bytes, which fills word 1
|
|
50
|
+
// almost completely at bytes 32..61). firstOffset comes from the first entry's offset byte.
|
|
51
|
+
// Set all 6 offset bytes to 255. The table ends at byte 61, and firstOffset = 255 means
|
|
52
|
+
// data starts at 255*32 = 8160. When we add entry 7, position 62 + 5 = 67 >= 255*32
|
|
53
|
+
// is false... so this won't trigger growth.
|
|
54
|
+
//
|
|
55
|
+
// The growth condition is `i + TOTAL_ID_SIZE >= firstOffset * WORD_SIZE` where i is the
|
|
56
|
+
// byte index of the last non-zero byte scanning backward from firstOffset*WORD_SIZE - 1.
|
|
57
|
+
// With firstOffset = 2, data starts at byte 64. 6 entries take bytes 32..61. The 7th
|
|
58
|
+
// entry would start at byte 62, and 62 + 5 = 67 >= 64, triggering growth. But we need
|
|
59
|
+
// offset = 255 to trigger overflow.
|
|
60
|
+
//
|
|
61
|
+
// The key insight: we need firstOffset to be small (so the table is nearly full and growth
|
|
62
|
+
// is triggered), but one of the EXISTING offset bytes to be 255.
|
|
63
|
+
//
|
|
64
|
+
// We can achieve this by:
|
|
65
|
+
// - Having 6 entries in the table (30 bytes, nearly filling one 32-byte word)
|
|
66
|
+
// - firstOffset = 2 (from the first entry, normal for 1 table word)
|
|
67
|
+
// - The last entry has offset byte = 255
|
|
68
|
+
// - We need enough data padding to make the metadata length consistent.
|
|
69
|
+
//
|
|
70
|
+
// Actually, the simplest approach: use createMetadata to build valid metadata with many
|
|
71
|
+
// entries so offsets are high, then manually patch an offset byte to 255. But that's
|
|
72
|
+
// fragile. Instead, let's hand-craft minimal metadata that triggers the exact code path.
|
|
73
|
+
|
|
74
|
+
// Minimal approach: 6 entries in lookup table, first offset = 2.
|
|
75
|
+
// Entry offsets: [2, 3, 4, 5, 6, 255]
|
|
76
|
+
// Data: 5 entries of 1 word each at offsets 2-6, plus a huge gap to offset 255.
|
|
77
|
+
// Total metadata length must be at least 255*32 + 32 = 8192.
|
|
78
|
+
|
|
79
|
+
bytes memory originalMetadata = new bytes(8192);
|
|
80
|
+
|
|
81
|
+
// Word 0 (bytes 0-31): reserved - leave as zeros.
|
|
82
|
+
|
|
83
|
+
// Word 1 (bytes 32-63): lookup table with 6 entries (30 bytes + 2 bytes padding).
|
|
84
|
+
// Entry 1: id=0x11111111, offset=2
|
|
85
|
+
originalMetadata[32] = 0x11;
|
|
86
|
+
originalMetadata[33] = 0x11;
|
|
87
|
+
originalMetadata[34] = 0x11;
|
|
88
|
+
originalMetadata[35] = 0x11;
|
|
89
|
+
originalMetadata[36] = bytes1(uint8(2)); // offset = 2
|
|
90
|
+
|
|
91
|
+
// Entry 2: id=0x22222222, offset=3
|
|
92
|
+
originalMetadata[37] = 0x22;
|
|
93
|
+
originalMetadata[38] = 0x22;
|
|
94
|
+
originalMetadata[39] = 0x22;
|
|
95
|
+
originalMetadata[40] = 0x22;
|
|
96
|
+
originalMetadata[41] = bytes1(uint8(3)); // offset = 3
|
|
97
|
+
|
|
98
|
+
// Entry 3: id=0x33333333, offset=4
|
|
99
|
+
originalMetadata[42] = 0x33;
|
|
100
|
+
originalMetadata[43] = 0x33;
|
|
101
|
+
originalMetadata[44] = 0x33;
|
|
102
|
+
originalMetadata[45] = 0x33;
|
|
103
|
+
originalMetadata[46] = bytes1(uint8(4)); // offset = 4
|
|
104
|
+
|
|
105
|
+
// Entry 4: id=0x44444444, offset=5
|
|
106
|
+
originalMetadata[47] = 0x44;
|
|
107
|
+
originalMetadata[48] = 0x44;
|
|
108
|
+
originalMetadata[49] = 0x44;
|
|
109
|
+
originalMetadata[50] = 0x44;
|
|
110
|
+
originalMetadata[51] = bytes1(uint8(5)); // offset = 5
|
|
111
|
+
|
|
112
|
+
// Entry 5: id=0x55555555, offset=6
|
|
113
|
+
originalMetadata[52] = 0x55;
|
|
114
|
+
originalMetadata[53] = 0x55;
|
|
115
|
+
originalMetadata[54] = 0x55;
|
|
116
|
+
originalMetadata[55] = 0x55;
|
|
117
|
+
originalMetadata[56] = bytes1(uint8(6)); // offset = 6
|
|
118
|
+
|
|
119
|
+
// Entry 6: id=0x66666666, offset=255 -- this is the one that will overflow
|
|
120
|
+
originalMetadata[57] = 0x66;
|
|
121
|
+
originalMetadata[58] = 0x66;
|
|
122
|
+
originalMetadata[59] = 0x66;
|
|
123
|
+
originalMetadata[60] = 0x66;
|
|
124
|
+
originalMetadata[61] = bytes1(uint8(255)); // offset = 255 -- will overflow on increment
|
|
125
|
+
|
|
126
|
+
// Bytes 62-63: padding (zeros) -- already zero.
|
|
127
|
+
|
|
128
|
+
// Fill some data at offset 2 (byte 64) through offset 6 (byte 224) so the structure
|
|
129
|
+
// is valid. Just leave as zeros (already initialized).
|
|
130
|
+
|
|
131
|
+
// Fill one word of data at offset 255 (byte 8160) to make numberOfWordslastData = 1.
|
|
132
|
+
// Just needs to exist in the allocation, which it does since length = 8192.
|
|
133
|
+
originalMetadata[8160] = 0xAB; // Some non-zero data.
|
|
134
|
+
|
|
135
|
+
// The new data to add (must be >= 32 bytes and padded to 32).
|
|
136
|
+
bytes memory dataToAdd = abi.encode(uint256(42));
|
|
137
|
+
|
|
138
|
+
// Now call addToMetadata. The code will:
|
|
139
|
+
// 1. Find firstOffset = 2 (from byte 36)
|
|
140
|
+
// 2. Scan backward from byte 63 to find the last non-zero byte: byte 61 (offset=255)
|
|
141
|
+
// 3. Check if new entry fits: 61 + 5 = 66 >= 2*32 = 64 => YES, table must grow
|
|
142
|
+
// 4. Loop to increment all offset bytes -- byte 61 has value 255, 255+1 overflows!
|
|
143
|
+
//
|
|
144
|
+
// Before the fix: wraps to 0 silently, corrupting the table.
|
|
145
|
+
// After the fix: reverts with JBMetadataResolver_MetadataTooLong.
|
|
146
|
+
vm.expectRevert(JBMetadataResolver.JBMetadataResolver_MetadataTooLong.selector);
|
|
147
|
+
parser.addDataToMetadata(originalMetadata, bytes4(0x77777777), dataToAdd);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/// @notice Sanity check: adding to metadata with offset < 255 still works after the fix.
|
|
151
|
+
function test_addToMetadata_offsetNoOverflow_succeeds() external view {
|
|
152
|
+
// Use the library normally: create metadata with a few entries, then add one more.
|
|
153
|
+
bytes4[] memory ids = new bytes4[](2);
|
|
154
|
+
bytes[] memory datas = new bytes[](2);
|
|
155
|
+
|
|
156
|
+
ids[0] = bytes4(0x11111111);
|
|
157
|
+
ids[1] = bytes4(0x22222222);
|
|
158
|
+
datas[0] = abi.encode(uint256(100));
|
|
159
|
+
datas[1] = abi.encode(uint256(200));
|
|
160
|
+
|
|
161
|
+
bytes memory metadata = parser.createMetadata(ids, datas);
|
|
162
|
+
|
|
163
|
+
// Add a third entry -- offsets are small, no overflow.
|
|
164
|
+
bytes memory modified = parser.addDataToMetadata(metadata, bytes4(0x33333333), abi.encode(uint256(300)));
|
|
165
|
+
|
|
166
|
+
// Verify all three entries are retrievable.
|
|
167
|
+
(bool found1, bytes memory data1) = parser.getDataFor(bytes4(0x11111111), modified);
|
|
168
|
+
assertTrue(found1);
|
|
169
|
+
assertEq(abi.decode(data1, (uint256)), 100);
|
|
170
|
+
|
|
171
|
+
(bool found2, bytes memory data2) = parser.getDataFor(bytes4(0x22222222), modified);
|
|
172
|
+
assertTrue(found2);
|
|
173
|
+
assertEq(abi.decode(data2, (uint256)), 200);
|
|
174
|
+
|
|
175
|
+
(bool found3, bytes memory data3) = parser.getDataFor(bytes4(0x33333333), modified);
|
|
176
|
+
assertTrue(found3);
|
|
177
|
+
assertEq(abi.decode(data3, (uint256)), 300);
|
|
178
|
+
}
|
|
179
|
+
}
|