Skip to content

Commit 684be82

Browse files
committed
FFI-free implementtion of GenerateUnsealedCID
1 parent 8557028 commit 684be82

File tree

4 files changed

+433
-0
lines changed

4 files changed

+433
-0
lines changed

nonffi/commd.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package nonffi
2+
3+
import (
4+
"fmt"
5+
"math/bits"
6+
7+
"github.com/filecoin-project/go-commp-utils/zerocomm"
8+
commcid "github.com/filecoin-project/go-fil-commcid"
9+
"github.com/filecoin-project/go-state-types/abi"
10+
"github.com/ipfs/go-cid"
11+
sha256simd "github.com/minio/sha256-simd"
12+
)
13+
14+
func GenerateUnsealedCID(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) {
15+
spi, found := abi.SealProofInfos[proofType]
16+
if !found {
17+
return cid.Undef, fmt.Errorf("unknown seal proof type %d", proofType)
18+
}
19+
20+
target := abi.PaddedPieceSize(spi.SectorSize)
21+
if len(pieces) == 0 {
22+
return zerocomm.ZeroPieceCommitment(target.Unpadded()), nil
23+
}
24+
25+
// sancheck everything
26+
for i, p := range pieces {
27+
if p.Size < 128 {
28+
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is too small", i, p.Size)
29+
}
30+
if pieces[i].Size > target {
31+
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is larger than sector size of SealProofType %d", i, p.Size, proofType)
32+
}
33+
if bits.OnesCount64(uint64(p.Size)) != 1 {
34+
return cid.Undef, fmt.Errorf("invalid Size of PieceInfo %d: value %d is not a power of 2", i, p.Size)
35+
}
36+
if _, err := commcid.CIDToPieceCommitmentV1(p.PieceCID); err != nil {
37+
return cid.Undef, fmt.Errorf("invalid PieceCid for PieceInfo %d: %w", i, err)
38+
}
39+
}
40+
41+
// reimplement https://github.com/filecoin-project/rust-fil-proofs/blob/380d6437c2/filecoin-proofs/src/pieces.rs#L85-L145
42+
stack := append(
43+
make([]abi.PieceInfo, 0, 32),
44+
pieces[0],
45+
)
46+
47+
for i := 1; i < len(pieces); i++ {
48+
49+
for stack[len(stack)-1].Size < pieces[i].Size {
50+
lastSize := stack[len(stack)-1].Size
51+
52+
stack = reduceStack(
53+
append(
54+
stack,
55+
abi.PieceInfo{
56+
Size: lastSize,
57+
PieceCID: zerocomm.ZeroPieceCommitment(lastSize.Unpadded()),
58+
},
59+
),
60+
)
61+
}
62+
63+
stack = reduceStack(
64+
append(
65+
stack,
66+
pieces[i],
67+
),
68+
)
69+
}
70+
71+
for len(stack) > 1 || stack[0].Size < target {
72+
lastSize := stack[len(stack)-1].Size
73+
stack = reduceStack(
74+
append(
75+
stack,
76+
abi.PieceInfo{
77+
Size: lastSize,
78+
PieceCID: zerocomm.ZeroPieceCommitment(lastSize.Unpadded()),
79+
},
80+
),
81+
)
82+
}
83+
84+
if stack[0].Size > target {
85+
return cid.Undef, fmt.Errorf("provided pieces sum up to %d bytes, which is larger than sector size of SealProofType %d", stack[0].Size, proofType)
86+
}
87+
88+
return stack[0].PieceCID, nil
89+
}
90+
91+
var s256 = sha256simd.New()
92+
93+
func reduceStack(s []abi.PieceInfo) []abi.PieceInfo {
94+
for {
95+
if len(s) < 2 || s[len(s)-2].Size != s[len(s)-1].Size {
96+
break
97+
}
98+
99+
l, _ := commcid.CIDToPieceCommitmentV1(s[len(s)-2].PieceCID)
100+
r, _ := commcid.CIDToPieceCommitmentV1(s[len(s)-1].PieceCID)
101+
s256.Reset()
102+
s256.Write(l)
103+
s256.Write(r)
104+
d := s256.Sum(make([]byte, 0, 32))
105+
d[31] &= 0b00111111
106+
newPiece, _ := commcid.PieceCommitmentV1ToCID(d)
107+
108+
s[len(s)-2] = abi.PieceInfo{
109+
Size: 2 * s[len(s)-2].Size,
110+
PieceCID: newPiece,
111+
}
112+
113+
s = s[:len(s)-1]
114+
}
115+
116+
return s
117+
}

nonffi/commd_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package nonffi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/filecoin-project/go-state-types/abi"
7+
"github.com/ipfs/go-cid"
8+
)
9+
10+
func TestGenerateUnsealedCID(t *testing.T) {
11+
12+
/*
13+
Testing live sector data with the help of a fellow SP
14+
15+
~$ lotus-miner sectors status --log 139074
16+
SectorID: 139074
17+
Status: Proving
18+
CIDcommD: baga6ea4seaqiw3gbmstmexb7sqwkc5r23o3i7zcyx5kr76pfobpykes3af62kca
19+
...
20+
Precommit: bafy2bzacec3dyxgqfbjekvnbin6uhcel7adis576346bi3tahp64bhijeiymy
21+
Commit: bafy2bzacecafq4ksrjzlhjagxkrrpycmfpjo5ch62s3tbq7gr5rop75fuqhwk
22+
Deals: [3755444 0 0 3755443 3755442 3755608 3755679 3755680 0 3755754 3755803 3755883 0 3755882 0 0 0]
23+
*/
24+
25+
expCommD := cidMustParse("baga6ea4seaqiw3gbmstmexb7sqwkc5r23o3i7zcyx5kr76pfobpykes3af62kca")
26+
27+
commD, _ := GenerateUnsealedCID(
28+
abi.RegisteredSealProof_StackedDrg32GiBV1_1, // 32G sector SP
29+
[]abi.PieceInfo{
30+
{PieceCID: cidMustParse("baga6ea4seaqknzm22isnhsxt2s4dnw45kfywmhenngqq3nc7jvecakoca6ksyhy"), Size: 256 << 20}, // https://filfox.info/en/deal/3755444
31+
{PieceCID: cidMustParse("baga6ea4seaqnq6o5wuewdpviyoafno4rdpqnokz6ghvg2iyeyfbqxgcwdlj2egi"), Size: 1024 << 20}, // https://filfox.info/en/deal/3755443
32+
{PieceCID: cidMustParse("baga6ea4seaqpixk4ifbkzato3huzycj6ty6gllqwanhdpsvxikawyl5bg2h44mq"), Size: 512 << 20}, // https://filfox.info/en/deal/3755442
33+
{PieceCID: cidMustParse("baga6ea4seaqaxwe5dy6nt3ko5tngtmzvpqxqikw5mdwfjqgaxfwtzenc6bgzajq"), Size: 512 << 20}, // https://filfox.info/en/deal/3755608
34+
{PieceCID: cidMustParse("baga6ea4seaqpy33nbesa4d6ot2ygeuy43y4t7amc4izt52mlotqenwcmn2kyaai"), Size: 1024 << 20}, // https://filfox.info/en/deal/3755679
35+
{PieceCID: cidMustParse("baga6ea4seaqphvv4x2s2v7ykgc3ugs2kkltbdeg7icxstklkrgqvv72m2v3i2aa"), Size: 256 << 20}, // https://filfox.info/en/deal/3755680
36+
{PieceCID: cidMustParse("baga6ea4seaqf5u55znk6jwhdsrhe37emzhmehiyvjxpsww274f6fiy3h4yctady"), Size: 512 << 20}, // https://filfox.info/en/deal/3755754
37+
{PieceCID: cidMustParse("baga6ea4seaqa3qbabsbmvk5er6rhsjzt74beplzgulthamm22jue4zgqcuszofi"), Size: 1024 << 20}, // https://filfox.info/en/deal/3755803
38+
{PieceCID: cidMustParse("baga6ea4seaqiekvf623muj6jpxg6vsqaikyw3r4ob5u7363z7zcaixqvfqsc2ji"), Size: 256 << 20}, // https://filfox.info/en/deal/3755883
39+
{PieceCID: cidMustParse("baga6ea4seaqhsewv65z2d4m5o4vo65vl5o6z4bcegdvgnusvlt7rao44gro36pi"), Size: 512 << 20}, // https://filfox.info/en/deal/3755882
40+
},
41+
)
42+
43+
if commD != expCommD {
44+
t.Fatalf("calculated commd for sector f01392893:139074 as %s, expected %s", commD, expCommD)
45+
}
46+
}
47+
48+
// Delete below when this ships in a stable release https://github.com/ipfs/go-cid/pull/139
49+
func cidMustParse(v interface{}) cid.Cid {
50+
c, err := cid.Parse(v)
51+
if err != nil {
52+
panic(err)
53+
}
54+
return c
55+
}

nonffi/go.mod

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module github.com/filecoin-project/go-commp-utils/nonffi
2+
3+
go 1.17
4+
5+
require (
6+
github.com/filecoin-project/go-commp-utils v0.1.3
7+
github.com/filecoin-project/go-fil-commcid v0.1.0
8+
github.com/filecoin-project/go-state-types v0.1.10
9+
github.com/ipfs/go-cid v0.2.0
10+
github.com/minio/sha256-simd v1.0.0
11+
)
12+
13+
require (
14+
github.com/filecoin-project/go-address v0.0.5 // indirect
15+
github.com/ipfs/go-block-format v0.0.3 // indirect
16+
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
17+
github.com/ipfs/go-ipld-cbor v0.0.6-0.20211211231443-5d9b9e1f6fa8 // indirect
18+
github.com/ipfs/go-ipld-format v0.0.2 // indirect
19+
github.com/klauspost/cpuid/v2 v2.0.4 // indirect
20+
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
21+
github.com/mr-tron/base58 v1.2.0 // indirect
22+
github.com/multiformats/go-base32 v0.0.3 // indirect
23+
github.com/multiformats/go-base36 v0.1.0 // indirect
24+
github.com/multiformats/go-multibase v0.0.3 // indirect
25+
github.com/multiformats/go-multihash v0.0.15 // indirect
26+
github.com/multiformats/go-varint v0.0.6 // indirect
27+
github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a // indirect
28+
github.com/whyrusleeping/cbor-gen v0.0.0-20210118024343-169e9d70c0c2 // indirect
29+
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect
30+
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
31+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
32+
)

0 commit comments

Comments
 (0)