Skip to content

Commit fea1825

Browse files
committed
opt: support offline compute commp
1 parent f1dbb57 commit fea1825

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

client/client.go

+1-42
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545

4646
"github.com/filecoin-project/go-address"
4747
cborutil "github.com/filecoin-project/go-cbor-util"
48-
"github.com/filecoin-project/go-commp-utils/ffiwrapper"
4948
"github.com/filecoin-project/go-commp-utils/writer"
5049
datatransfer "github.com/filecoin-project/go-data-transfer/v2"
5150
"github.com/filecoin-project/go-fil-markets/discovery"
@@ -1332,47 +1331,7 @@ func (a *API) ClientQueryAsk(ctx context.Context, p peer.ID, miner address.Addre
13321331
}
13331332

13341333
func (a *API) ClientCalcCommP(ctx context.Context, inpath string) (*types.CommPRet, error) {
1335-
// Hard-code the sector type to 32GiBV1_1, because:
1336-
// - ffiwrapper.GeneratePieceCIDFromFile requires a RegisteredSealProof
1337-
// - commP itself is sector-size independent, with rather low probability of that changing
1338-
// ( note how the final rust call is identical for every RegSP type )
1339-
// https://github.com/filecoin-project/rust-filecoin-proofs-api/blob/v5.0.0/src/seal.rs#L1040-L1050
1340-
//
1341-
// IF/WHEN this changes in the future we will have to be able to calculate
1342-
// "old style" commP, and thus will need to introduce a version switch or similar
1343-
arbitraryProofType := abi.RegisteredSealProof_StackedDrg32GiBV1_1
1344-
1345-
rdr, err := os.Open(inpath)
1346-
if err != nil {
1347-
return nil, err
1348-
}
1349-
defer rdr.Close() //nolint:errcheck
1350-
1351-
stat, err := rdr.Stat()
1352-
if err != nil {
1353-
return nil, err
1354-
}
1355-
1356-
// check that the data is a car file; if it's not, retrieval won't work
1357-
_, err = car.ReadHeader(bufio.NewReader(rdr))
1358-
if err != nil {
1359-
return nil, fmt.Errorf("not a car file: %w", err)
1360-
}
1361-
1362-
if _, err := rdr.Seek(0, io.SeekStart); err != nil {
1363-
return nil, fmt.Errorf("seek to start: %w", err)
1364-
}
1365-
1366-
pieceReader, pieceSize := padreader.New(rdr, uint64(stat.Size()))
1367-
commP, err := ffiwrapper.GeneratePieceCIDFromFile(arbitraryProofType, pieceReader, pieceSize)
1368-
if err != nil {
1369-
return nil, fmt.Errorf("computing commP failed: %w", err)
1370-
}
1371-
1372-
return &types.CommPRet{
1373-
Root: commP,
1374-
Size: pieceSize,
1375-
}, nil
1334+
return utils.CalcCommP(ctx, inpath)
13761335
}
13771336

13781337
type lenWriter int64

cmd/droplet-client/data.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/filecoin-project/venus/venus-shared/types"
1414
"github.com/filecoin-project/venus/venus-shared/types/market/client"
1515
cli2 "github.com/ipfs-force-community/droplet/v2/cli"
16+
"github.com/ipfs-force-community/droplet/v2/utils"
1617
)
1718

1819
var dataCmd = &cli.Command{
@@ -205,18 +206,13 @@ var dataCommPCmd = &cli.Command{
205206
&cli2.CidBaseFlag,
206207
},
207208
Action: func(cctx *cli.Context) error {
208-
api, closer, err := cli2.NewMarketClientNode(cctx)
209-
if err != nil {
210-
return err
211-
}
212-
defer closer()
213209
ctx := cli2.ReqContext(cctx)
214210

215211
if cctx.Args().Len() != 1 {
216212
return fmt.Errorf("usage: commP <inputPath>")
217213
}
218214

219-
ret, err := api.ClientCalcCommP(ctx, cctx.Args().Get(0))
215+
ret, err := utils.CalcCommP(ctx, cctx.Args().Get(0))
220216
if err != nil {
221217
return err
222218
}

utils/commp.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package utils
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"fmt"
7+
"io"
8+
"os"
9+
10+
"github.com/filecoin-project/go-commp-utils/ffiwrapper"
11+
"github.com/filecoin-project/go-padreader"
12+
"github.com/filecoin-project/go-state-types/abi"
13+
types "github.com/filecoin-project/venus/venus-shared/types/market/client"
14+
"github.com/ipld/go-car"
15+
)
16+
17+
func CalcCommP(ctx context.Context, inpath string) (*types.CommPRet, error) {
18+
// Hard-code the sector type to 32GiBV1_1, because:
19+
// - ffiwrapper.GeneratePieceCIDFromFile requires a RegisteredSealProof
20+
// - commP itself is sector-size independent, with rather low probability of that changing
21+
// ( note how the final rust call is identical for every RegSP type )
22+
// https://github.com/filecoin-project/rust-filecoin-proofs-api/blob/v5.0.0/src/seal.rs#L1040-L1050
23+
//
24+
// IF/WHEN this changes in the future we will have to be able to calculate
25+
// "old style" commP, and thus will need to introduce a version switch or similar
26+
arbitraryProofType := abi.RegisteredSealProof_StackedDrg32GiBV1_1
27+
28+
rdr, err := os.Open(inpath)
29+
if err != nil {
30+
return nil, err
31+
}
32+
defer rdr.Close() //nolint:errcheck
33+
34+
stat, err := rdr.Stat()
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
// check that the data is a car file; if it's not, retrieval won't work
40+
_, err = car.ReadHeader(bufio.NewReader(rdr))
41+
if err != nil {
42+
return nil, fmt.Errorf("not a car file: %w", err)
43+
}
44+
45+
if _, err := rdr.Seek(0, io.SeekStart); err != nil {
46+
return nil, fmt.Errorf("seek to start: %w", err)
47+
}
48+
49+
pieceReader, pieceSize := padreader.New(rdr, uint64(stat.Size()))
50+
commP, err := ffiwrapper.GeneratePieceCIDFromFile(arbitraryProofType, pieceReader, pieceSize)
51+
if err != nil {
52+
return nil, fmt.Errorf("computing commP failed: %w", err)
53+
}
54+
55+
return &types.CommPRet{
56+
Root: commP,
57+
Size: pieceSize,
58+
}, nil
59+
}

0 commit comments

Comments
 (0)