Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,27 @@ jobs:
with:
fetch-depth: 0

- name: configure
run: ./configure

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
version: v1.7.1

- name: Install Dependencies
run: |
forge install

- 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;
2 changes: 1 addition & 1 deletion .github/workflows/publish-abis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions src/Cids.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/PDPVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions test/Cids.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 11 additions & 1 deletion test/PDPVerifier.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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++) {
Expand Down
33 changes: 20 additions & 13 deletions test/PDPVerifierMetadata.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -272,15 +274,15 @@ 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");
}

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);
Expand All @@ -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);
Expand All @@ -307,14 +309,19 @@ 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);

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);
}
Expand Down
3 changes: 3 additions & 0 deletions test/PieceHelper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading