forked from filedrive-team/go-graphsplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support piececid calculation for single car file filedrive-team#11
- Loading branch information
Showing
8 changed files
with
507 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/graphsplit | ||
/commp | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "extern/filecoin-ffi"] | ||
path = extern/filecoin-ffi | ||
url = https://github.com/filecoin-project/filecoin-ffi.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
.PHONY: build | ||
build: | ||
go build -ldflags "-s -w" -o graphsplit ./cmd/graphsplit/main.go | ||
|
||
|
||
## FFI | ||
|
||
ffi: | ||
./extern/filecoin-ffi/install-filcrypto | ||
.PHONY: ffi | ||
|
||
|
||
commp: | ||
go build -ldflags "-s -w" -o commp ./cmd/commp/main.go | ||
.PHONY: commp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/filedrive-team/go-graphsplit/piece" | ||
logging "github.com/ipfs/go-log/v2" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var log = logging.Logger("commp") | ||
|
||
func main() { | ||
logging.SetLogLevel("*", "INFO") | ||
local := []*cli.Command{ | ||
singleCmd, | ||
} | ||
|
||
app := &cli.App{ | ||
Name: "commp", | ||
Flags: []cli.Flag{}, | ||
Commands: local, | ||
} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
fmt.Println("Error: ", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var singleCmd = &cli.Command{ | ||
Name: "single", | ||
Usage: "PieceCID and PieceSize calculation", | ||
Flags: []cli.Flag{}, | ||
Action: func(c *cli.Context) error { | ||
ctx := context.Background() | ||
targetPath := c.Args().First() | ||
|
||
res, err := piece.CalcCommP(ctx, targetPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("PieceCID: %s, PieceSize: %d\n", res.Root, res.Size) | ||
return nil | ||
}, | ||
} |
Submodule filecoin-ffi
added at
dc4e4e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package piece | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"io" | ||
"os" | ||
|
||
"github.com/filecoin-project/go-commp-utils/ffiwrapper" | ||
"github.com/filecoin-project/go-padreader" | ||
"github.com/filecoin-project/go-state-types/abi" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-car" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
type CommPRet struct { | ||
Root cid.Cid | ||
Size abi.UnpaddedPieceSize | ||
} | ||
|
||
// almost copy paste from https://github.com/filecoin-project/lotus/node/impl/client/client.go#L749-L770 | ||
func CalcCommP(ctx context.Context, inpath string) (*CommPRet, error) { | ||
|
||
// Hard-code the sector type to 32GiBV1_1, because: | ||
// - ffiwrapper.GeneratePieceCIDFromFile requires a RegisteredSealProof | ||
// - commP itself is sector-size independent, with rather low probability of that changing | ||
// ( note how the final rust call is identical for every RegSP type ) | ||
// https://github.com/filecoin-project/rust-filecoin-proofs-api/blob/v5.0.0/src/seal.rs#L1040-L1050 | ||
// | ||
// IF/WHEN this changes in the future we will have to be able to calculate | ||
// "old style" commP, and thus will need to introduce a version switch or similar | ||
arbitraryProofType := abi.RegisteredSealProof_StackedDrg32GiBV1_1 | ||
|
||
rdr, err := os.Open(inpath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rdr.Close() //nolint:errcheck | ||
|
||
stat, err := rdr.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// check that the data is a car file; if it's not, retrieval won't work | ||
_, _, err = car.ReadHeader(bufio.NewReader(rdr)) | ||
if err != nil { | ||
return nil, xerrors.Errorf("not a car file: %w", err) | ||
} | ||
|
||
if _, err := rdr.Seek(0, io.SeekStart); err != nil { | ||
return nil, xerrors.Errorf("seek to start: %w", err) | ||
} | ||
|
||
pieceReader, pieceSize := padreader.New(rdr, uint64(stat.Size())) | ||
commP, err := ffiwrapper.GeneratePieceCIDFromFile(arbitraryProofType, pieceReader, pieceSize) | ||
|
||
if err != nil { | ||
return nil, xerrors.Errorf("computing commP failed: %w", err) | ||
} | ||
|
||
return &CommPRet{ | ||
Root: commP, | ||
Size: pieceSize, | ||
}, nil | ||
} |