From 02384ca673bfcfeea8839edbf790eedc4455af9d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 19 Nov 2026 17:07:00 +1100 Subject: [PATCH 1/3] fix: enforce FRC-0069 PieceCIDv2 sizing in addPieces Reject pieces below height 2 and pieces that aren't in the smallest tree holding their data, so each piece has one valid CID on chain. Limit the padding uvarint to 9 bytes in validateCommPv2. Pieces already added are unaffected. Closes: #314 Ref: https://github.com/filecoin-project/FIPs#1311 --- CHANGELOG.md | 1 + src/Cids.sol | 15 +++++++++++++++ src/PDPVerifier.sol | 12 ++++++++++-- test/Cids.t.sol | 29 +++++++++++++++++++++++++++++ test/PDPVerifier.t.sol | 12 +++++++++++- test/PDPVerifierMetadata.t.sol | 33 ++++++++++++++++++++------------- test/PieceHelper.t.sol | 3 +++ 7 files changed, 89 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff8b873..f1d63ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Changed - Removed the 2,000-piece pending-removal limit from `schedulePieceDeletions()`. Storage providers can queue larger batches or accumulate removals across calls, then drain the queue with `processPieceDeletions()` before starting the next proving period ([#310](https://github.com/FilOzone/pdp/issues/310)). The obsolete `MAX_ENQUEUED_REMOVALS()` getter has been removed from the ABI; contract storage layout is unchanged. +- `addPieces()` now applies the FRC-0069 PieceCIDv2 sizing rules: a piece must be at least height 2 (a 128 byte piece) and use the smallest tree that holds its data, and its padding uvarint is limited to 9 bytes. Each piece now has a single valid CID on chain. Curio and the FOC SDKs already produce only these forms, and pieces already added are unaffected ([#314](https://github.com/FilOzone/pdp/issues/314)). ### Fixed - Data-set cleanup now drains pending removals incrementally alongside piece cleanup, so an unbounded removal queue does not accumulate for the final cleanup transaction. diff --git a/src/Cids.sol b/src/Cids.sol index 8e9e78f..25d39e4 100644 --- a/src/Cids.sol +++ b/src/Cids.sol @@ -5,6 +5,10 @@ library Cids { // 0x01 0x55 0x9120 // (cidv1) (raw) (fr32-sha2-256-trunc254-padded-binary-tree) bytes4 public constant COMMP_V2_PREFIX = hex"01559120"; + // The smallest PieceCIDv2 tree: 4 leaves, a 128 byte piece holding up to 127 bytes of data + uint8 internal constant MIN_HEIGHT = 2; + // The unsigned-varint limit FRC-0069 applies to padding, holding values below 2^63 + uint256 internal constant MAX_PADDING_UVARINT_LENGTH = 9; error CidTooShort(); error InvalidCommPv2Prefix(); @@ -53,6 +57,7 @@ library Cids { uint256 paddingOffset; (padding, paddingOffset) = _readUvarint(cid.data, offset); require(paddingOffset - offset == _uvarintLength(padding), NonMinimalUvarint()); + require(paddingOffset - offset <= MAX_PADDING_UVARINT_LENGTH, UvarintOverflow()); offset = paddingOffset; require(offset < cid.data.length, InvalidCommPv2DigestLength()); @@ -72,6 +77,16 @@ library Cids { return (128 * padding) / 127 >= 1 << (height + 5); } + // isMinimalHeight checks that the tree is the smallest that holds the data, as FRC-0069 requires: the + // smallest tree holds 0 to 127 bytes, and any larger tree must be more than half full. + // Expects MIN_HEIGHT <= height <= 249, above which the shift truncates. + function isMinimalHeight(uint256 padding, uint8 height) internal pure returns (bool) { + if (height == MIN_HEIGHT) { + return padding <= 127; + } + return padding < 127 << (height - 3); + } + // pieceSize returns the size of the data defined by amount of padding and height of the tree // this is after the Fr32 expansion, if 1 bit of actual data spills into padding byte, the whole byte is counted as data // as the padding is specified as before expansion diff --git a/src/PDPVerifier.sol b/src/PDPVerifier.sol index 37c3730..7c608af 100644 --- a/src/PDPVerifier.sol +++ b/src/PDPVerifier.sol @@ -982,11 +982,19 @@ contract PDPVerifier is Initializable, UUPSUpgradeable, OwnableUpgradeable { returns (uint256 leafCount) { (uint256 padding, uint8 height, bytes32 root) = Cids.validateCommPv2(piece); + if (height > MAX_PIECE_SIZE_LOG2) { + revert IndexedError(callIdx, "Piece size must be less than 2^50"); + } + if (height < Cids.MIN_HEIGHT) { + revert IndexedError(callIdx, "Piece size must be at least 128 bytes"); + } + // A piece needs at least one data leaf to be challenged, so the empty piece FRC-0069 allows (padding 127 + // at height 2) is rejected along with padding beyond the tree if (Cids.isPaddingExcessive(padding, height)) { revert IndexedError(callIdx, "Padding is too large"); } - if (height > MAX_PIECE_SIZE_LOG2) { - revert IndexedError(callIdx, "Piece size must be less than 2^50"); + if (!Cids.isMinimalHeight(padding, height)) { + revert IndexedError(callIdx, "Piece size must be the smallest that holds its data"); } leafCount = Cids.leafCount(padding, height); diff --git a/test/Cids.t.sol b/test/Cids.t.sol index 72f28e8..ee976e6 100644 --- a/test/Cids.t.sol +++ b/test/Cids.t.sol @@ -101,6 +101,35 @@ contract CidsTest is Test { assertEq(Cids.leafCount(1024 * 127 / 128 - 1, 5), 1); } + function testIsMinimalHeight() public pure { + // The smallest tree holds 0 to 127 bytes + assertEq(Cids.isMinimalHeight(0, 2), true); + assertEq(Cids.isMinimalHeight(127, 2), true); + assertEq(Cids.isMinimalHeight(128, 2), false); + + // Larger trees must be more than half full: 128 to 254 bytes at height 3 + assertEq(Cids.isMinimalHeight(126, 3), true); + assertEq(Cids.isMinimalHeight(127, 3), false); + + // FRC-0069 vectors: 512 and 513 bytes at height 5, and 508 bytes at height 5, which fits height 4 + assertEq(Cids.isMinimalHeight(504, 5), true); + assertEq(Cids.isMinimalHeight(503, 5), true); + assertEq(Cids.isMinimalHeight(508, 5), false); + + assertEq(Cids.isMinimalHeight(0, 50), true); + assertEq(Cids.isMinimalHeight((uint256(127) << 47) - 1, 50), true); + assertEq(Cids.isMinimalHeight(uint256(127) << 47, 50), false); + } + + function testValidateCommPv2RejectsPaddingUvarintOverNineBytes() public { + bytes32 root = hex"beadcafefacedeedfeedbabedeadbeefbeadcafefacedeedfeedbabedeadbeef"; + (uint256 padding,,) = this.validateCommPv2(Cids.CommPv2FromDigest(type(uint64).max >> 1, 10, root)); + assertEq(padding, type(uint64).max >> 1, "nine byte padding"); + + vm.expectRevert(Cids.UvarintOverflow.selector); + this.validateCommPv2(Cids.CommPv2FromDigest(uint256(1) << 63, 10, root)); + } + function testValidateCommPv2RejectsShortCid() public { _expectInvalid(hex""); _expectInvalid(hex"0155"); diff --git a/test/PDPVerifier.t.sol b/test/PDPVerifier.t.sol index 32694f6..7c6c25c 100644 --- a/test/PDPVerifier.t.sol +++ b/test/PDPVerifier.t.sol @@ -524,6 +524,16 @@ contract PDPVerifierDataSetMutateTest is MockFVMTest, PieceHelper { expectIndexedError(0, "Piece size must be less than 2^50"); pdpVerifier.addPieces(setId, address(0), pieces, empty); + // Fail below the smallest piece, 4 leaves at height 2 + pieces[0] = Cids.CommPv2FromDigest(0, 1, bytes32(uint256(1))); + expectIndexedError(0, "Piece size must be at least 128 bytes"); + pdpVerifier.addPieces(setId, address(0), pieces, empty); + + // Fail when a smaller piece holds the data: 127 bytes at height 3 fits height 2 + pieces[0] = Cids.CommPv2FromDigest(127, 3, bytes32(uint256(1))); + expectIndexedError(0, "Piece size must be the smallest that holds its data"); + pdpVerifier.addPieces(setId, address(0), pieces, empty); + // Fail when not adding any pieces; Cids.Cid[] memory emptyPieces = new Cids.Cid[](0); vm.expectRevert("Must add at least one piece"); @@ -2406,7 +2416,7 @@ contract PDPVerifierE2ETest is MockFVMTest, ProofBuilderHelper, PieceHelper { // Step 2: Add data `A` in scope for the first proving period // Note that the data in the first addPieces call is added to the first proving period uint256[] memory leafCountsA = new uint256[](2); - leafCountsA[0] = 2; + leafCountsA[0] = 6; leafCountsA[1] = 3; bytes32[][][] memory treesA = new bytes32[][][](2); for (uint256 i = 0; i < leafCountsA.length; i++) { diff --git a/test/PDPVerifierMetadata.t.sol b/test/PDPVerifierMetadata.t.sol index ca28850..589e14d 100644 --- a/test/PDPVerifierMetadata.t.sol +++ b/test/PDPVerifierMetadata.t.sol @@ -190,8 +190,8 @@ contract PDPVerifierMetadataTest is Test { function testCompactAdditionAppendsRootsMetadataAndSumTreeCounts() public { uint256 setId = 9; Cids.Cid[] memory firstBatch = new Cids.Cid[](3); - firstBatch[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(1))); - firstBatch[1] = Cids.CommPv2FromDigest(0, 1, bytes32(uint256(2))); + firstBatch[0] = _smallPiece(1, bytes32(uint256(1))); + firstBatch[1] = _smallPiece(2, bytes32(uint256(2))); firstBatch[2] = Cids.CommPv2FromDigest(0, 2, bytes32(uint256(3))); assertEq(harness.addPiecesForTest(setId, firstBatch), 0, "first batch starts at zero"); @@ -205,13 +205,15 @@ contract PDPVerifierMetadataTest is Test { assertEq(harness.compactPieceCount(setId), 5, "array length grows monotonically"); assertEq(harness.dataSetLeafCountForTest(setId), 31, "batch leaf counts accumulate once"); + uint256[5] memory expectedPaddings = [uint256(96), 64, 0, 0, 0]; + uint256[5] memory expectedHeights = [uint256(2), 2, 2, 3, 4]; uint256[5] memory expectedLeafCounts = [uint256(1), 2, 4, 8, 16]; uint256[5] memory expectedSums = [uint256(1), 3, 4, 15, 16]; for (uint256 pieceId; pieceId < 5; ++pieceId) { (bytes32 root, uint256 metadata) = harness.compactPiece(setId, pieceId); assertEq(root, bytes32(pieceId + 1), "root"); - assertEq(harness.piecePadding(metadata), 0, "padding"); - assertEq(harness.pieceHeight(metadata), pieceId, "height"); + assertEq(harness.piecePadding(metadata), expectedPaddings[pieceId], "padding"); + assertEq(harness.pieceHeight(metadata), expectedHeights[pieceId], "height"); assertEq(harness.pieceLeafCount(metadata), expectedLeafCounts[pieceId], "leaf count"); assertEq(harness.pieceSum(metadata), expectedSums[pieceId], "sum tree count"); } @@ -220,8 +222,8 @@ contract PDPVerifierMetadataTest is Test { function testCompactRemovalOfLeafNodePreservesAncestorSumAndLookup() public { uint256 setId = 9; Cids.Cid[] memory pieces = new Cids.Cid[](3); - pieces[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(1))); - pieces[1] = Cids.CommPv2FromDigest(0, 1, bytes32(uint256(2))); + pieces[0] = _smallPiece(1, bytes32(uint256(1))); + pieces[1] = _smallPiece(2, bytes32(uint256(2))); pieces[2] = Cids.CommPv2FromDigest(0, 2, bytes32(uint256(3))); harness.addPiecesForTest(setId, pieces); @@ -233,7 +235,7 @@ contract PDPVerifierMetadataTest is Test { assertEq(root, bytes32(0), "removed root cleared"); _assertMetadata(metadata, 0, 0, 0, 0); (, uint256 ancestorMetadata) = harness.compactPiece(setId, 1); - _assertMetadata(ancestorMetadata, 0, 1, 2, 2); + _assertMetadata(ancestorMetadata, 64, 2, 2, 2); assertEq(harness.compactPieceCount(setId), 3, "ordinary removal keeps stable IDs"); uint256[] memory leaves = new uint256[](2); @@ -249,8 +251,8 @@ contract PDPVerifierMetadataTest is Test { function testCompactRemovalOfInternalNodeRetainsPartialSumAndAppendsAtTail() public { uint256 setId = 10; Cids.Cid[] memory pieces = new Cids.Cid[](3); - pieces[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(1))); - pieces[1] = Cids.CommPv2FromDigest(0, 1, bytes32(uint256(2))); + pieces[0] = _smallPiece(1, bytes32(uint256(1))); + pieces[1] = _smallPiece(2, bytes32(uint256(2))); pieces[2] = Cids.CommPv2FromDigest(0, 2, bytes32(uint256(3))); harness.addPiecesForTest(setId, pieces); @@ -272,7 +274,7 @@ contract PDPVerifierMetadataTest is Test { assertEq(found[1].pieceId, 2, "lookup crosses removed internal boundary"); Cids.Cid[] memory append = new Cids.Cid[](1); - append[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(4))); + append[0] = _smallPiece(1, bytes32(uint256(4))); assertEq(harness.addPiecesForTest(setId, append), 3, "append does not fill removed hole"); assertEq(harness.compactPieceCount(setId), 4, "append grows compact array tail"); } @@ -280,7 +282,7 @@ contract PDPVerifierMetadataTest is Test { function testCompactAdditionRevertsEntireInvalidBatch() public { uint256 setId = 10; Cids.Cid[] memory pieces = new Cids.Cid[](2); - pieces[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(1))); + pieces[0] = _smallPiece(1, bytes32(uint256(1))); pieces[1] = Cids.Cid(bytes("")); vm.expectRevert(Cids.CidTooShort.selector); @@ -294,7 +296,7 @@ contract PDPVerifierMetadataTest is Test { uint256 setId = 11; harness.setDataSetLeafCount(setId, SUM_TREE_MAX); Cids.Cid[] memory pieces = new Cids.Cid[](1); - pieces[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(1))); + pieces[0] = _smallPiece(1, bytes32(uint256(1))); vm.expectRevert(PieceMetadataOverflow.selector); harness.addPiecesForTest(setId, pieces); @@ -307,7 +309,7 @@ contract PDPVerifierMetadataTest is Test { uint256 setId = 12; harness.appendCompactPiece(setId, bytes32(uint256(1)), harness.packPieceMetadata(0, 0, 1, SUM_TREE_MAX)); Cids.Cid[] memory pieces = new Cids.Cid[](1); - pieces[0] = Cids.CommPv2FromDigest(0, 0, bytes32(uint256(2))); + pieces[0] = _smallPiece(1, bytes32(uint256(2))); vm.expectRevert(PieceMetadataOverflow.selector); harness.addPiecesForTest(setId, pieces); @@ -315,6 +317,11 @@ contract PDPVerifierMetadataTest is Test { assertEq(harness.compactPieceCount(setId), 1, "sum overflow adds no compact piece"); } + // A piece at the smallest height whose data fills `leafCount` of its 4 leaves + function _smallPiece(uint256 leafCount, bytes32 root) private pure returns (Cids.Cid memory) { + return Cids.CommPv2FromDigest((4 - leafCount) * 32, Cids.MIN_HEIGHT, root); + } + function _assertRoundTrip(uint256 padding, uint256 height, uint256 leafCount, uint256 sum) private view { _assertMetadata(harness.packPieceMetadata(padding, height, leafCount, sum), padding, height, leafCount, sum); } diff --git a/test/PieceHelper.t.sol b/test/PieceHelper.t.sol index 6fdc9e5..735dba7 100644 --- a/test/PieceHelper.t.sol +++ b/test/PieceHelper.t.sol @@ -59,6 +59,9 @@ contract PieceHelper is Test { return Cids.CommPv2FromDigest(127, 2, tree[0][0]); } uint8 height = uint8(256 - BitOps.clz(leafCount - 1)); + if (height < Cids.MIN_HEIGHT) { + height = Cids.MIN_HEIGHT; + } require(1 << height >= leafCount, "makePiece: height not enough to hold leaf count"); uint256 paddingLeaves = (1 << height) - leafCount; uint256 padding = (paddingLeaves * 32 * 127 + 127) / 128; From d1d0516c2b1abd229b3431a9f7da7e81252471fc Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 19 Nov 2026 17:19:53 +1100 Subject: [PATCH 2/3] ci: pin foundry versions --- .github/workflows/makefile.yml | 2 +- .github/workflows/publish-abis.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml index 97cf5c1..fdac33e 100644 --- a/.github/workflows/makefile.yml +++ b/.github/workflows/makefile.yml @@ -21,7 +21,7 @@ jobs: - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 with: - version: nightly + version: v1.7.1 - name: Install Dependencies run: | diff --git a/.github/workflows/publish-abis.yml b/.github/workflows/publish-abis.yml index 00b06d7..89479d1 100644 --- a/.github/workflows/publish-abis.yml +++ b/.github/workflows/publish-abis.yml @@ -20,7 +20,7 @@ jobs: - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 with: - version: nightly + version: v1.7.1 - name: Install jq run: sudo apt-get update && sudo apt-get install -y jq From 06375a78a60f5ba2759490c71d4cd28fdbf7474d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 19 Nov 2026 17:50:11 +1100 Subject: [PATCH 3/3] fixup! ci: pin foundry versions --- .github/workflows/makefile.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml index fdac33e..de60756 100644 --- a/.github/workflows/makefile.yml +++ b/.github/workflows/makefile.yml @@ -15,9 +15,6 @@ jobs: with: fetch-depth: 0 - - name: configure - run: ./configure - - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 with: @@ -29,20 +26,16 @@ jobs: - name: Run build run: | - export PATH="/home/runner/.config/.foundry/bin:$PATH"; make build; - name: Run tests run: | - export PATH="/home/runner/.config/.foundry/bin:$PATH"; make test; - name: Run fast profile tests run: | - export PATH="/home/runner/.config/.foundry/bin:$PATH"; make test-fast; - name: Check storage layout run: | - export PATH="/home/runner/.config/.foundry/bin:$PATH"; make check-layout;