diff --git a/go.mod b/go.mod index e806e5c6d..d4ea9604b 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/BurntSushi/toml v0.3.1 github.com/filecoin-project/go-bls-sigs v0.0.0-20190718224239-4bc4b8a7bbf8 github.com/filecoin-project/go-leb128 v0.0.0-20190212224330-8d79a5489543 + github.com/filecoin-project/go-sectorbuilder v0.0.0-00010101000000-000000000000 github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1 github.com/gorilla/websocket v1.4.0 github.com/ipfs/go-bitswap v0.1.5 diff --git a/lib/sectorbuilder/sectorbuilder.go b/lib/sectorbuilder/sectorbuilder.go new file mode 100644 index 000000000..bc50ff841 --- /dev/null +++ b/lib/sectorbuilder/sectorbuilder.go @@ -0,0 +1,73 @@ +package sectorbuilder + +import ( + "unsafe" + + "github.com/filecoin-project/go-lotus/chain/address" + sectorbuilder "github.com/filecoin-project/go-sectorbuilder" +) + +const CommLen = sectorbuilder.CommitmentBytesLen + +type SectorBuilder struct { + handle unsafe.Pointer +} + +type SectorBuilderConfig struct { + SectorSize uint64 + Miner address.Address + SealedDir string + StagedDir string + MetadataDir string +} + +func New(cfg *SectorBuilderConfig) (*SectorBuilder, error) { + var proverId [31]byte + copy(proverId[:], cfg.Miner.Payload()) + + sbp, err := sectorbuilder.InitSectorBuilder(cfg.SectorSize, 2, 2, 1, cfg.MetadataDir, [31]byte{}, cfg.SealedDir, cfg.StagedDir, 16) + if err != nil { + return nil, err + } + + return &SectorBuilder{ + handle: sbp, + }, nil +} + +func (sb *SectorBuilder) Destroy() { + sectorbuilder.DestroySectorBuilder(sb.handle) +} + +func (sb *SectorBuilder) AddPiece(pieceKey string, pieceSize uint64, piecePath string) (uint64, error) { + return sectorbuilder.AddPiece(sb.handle, pieceKey, pieceSize, piecePath) +} + +// TODO: should *really really* return an io.ReadCloser +func (sb *SectorBuilder) ReadPieceFromSealedSector(pieceKey string) ([]byte, error) { + return sectorbuilder.ReadPieceFromSealedSector(sb.handle, pieceKey) +} + +func (sb *SectorBuilder) SealAllStagedSectors() error { + return sectorbuilder.SealAllStagedSectors(sb.handle) +} + +func (sb *SectorBuilder) SealStatus(sector uint64) (sectorbuilder.SectorSealingStatus, error) { + return sectorbuilder.GetSectorSealingStatusByID(sb.handle, sector) +} + +func (sb *SectorBuilder) GeneratePoSt(sortedCommRs [][CommLen]byte, challengeSeed [CommLen]byte) ([][]byte, []uint64, error) { + // Wait, this is a blocking method with no way of interrupting it? + // does it checkpoint itself? + return sectorbuilder.GeneratePoSt(sb.handle, sortedCommRs, challengeSeed) +} + +func VerifySeal(sectorSize uint64, commR, commD, commRStar [CommLen]byte, proverID address.Address, sectorID uint64, proof []byte) (bool, error) { + panic("TODO") + // return sectorbuilder.VerifySeal(sectorSize, commR, commD, commRStar, providerID, sectorID, proof) +} + +func VerifyPost(sectorSize uint64, sortedCommRs [][CommLen]byte, challengeSeed [CommLen]byte, proofs [][]byte, faults []uint64) (bool, error) { + // sectorbuilder.VerifyPost() + panic("no") +} diff --git a/node/builder.go b/node/builder.go index da79b06a8..c8c61dc3b 100644 --- a/node/builder.go +++ b/node/builder.go @@ -28,6 +28,7 @@ import ( "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/wallet" + "github.com/filecoin-project/go-lotus/lib/sectorbuilder" "github.com/filecoin-project/go-lotus/node/config" "github.com/filecoin-project/go-lotus/node/hello" "github.com/filecoin-project/go-lotus/node/impl" @@ -246,6 +247,11 @@ func Config(cfg *config.Root) Option { ApplyIf(func(s *Settings) bool { return s.Online }, Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)), ), + + ApplyIf(func(s *Settings) bool { return s.nodeType == nodeStorageMiner }, + Override(new(*sectorbuilder.SectorBuilderConfig), modules.SectorBuilderConfig), + Override(new(*sectorbuilder.SectorBuilder), sectorbuilder.New), + ), ) } diff --git a/node/impl/storminer.go b/node/impl/storminer.go index 197ca710a..dfaad454c 100644 --- a/node/impl/storminer.go +++ b/node/impl/storminer.go @@ -2,10 +2,13 @@ package impl import ( "github.com/filecoin-project/go-lotus/api" + "github.com/filecoin-project/go-lotus/lib/sectorbuilder" ) type StorageMinerAPI struct { CommonAPI + + SectorBuilder *sectorbuilder.SectorBuilder } var _ api.StorageMiner = &StorageMinerAPI{} diff --git a/node/modules/core.go b/node/modules/core.go index cf7600ffd..8892e9b7d 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -30,8 +30,10 @@ import ( "golang.org/x/xerrors" "github.com/filecoin-project/go-lotus/api" + "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" + "github.com/filecoin-project/go-lotus/lib/sectorbuilder" "github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/filecoin-project/go-lotus/node/repo" ) @@ -216,3 +218,23 @@ func LoadGenesis(genBytes []byte) func(blockstore.Blockstore) Genesis { } } } + +func SectorBuilderConfig(storagePath string) (*sectorbuilder.SectorBuilderConfig, error) { + metadata := filepath.Join(storagePath, "meta") + sealed := filepath.Join(storagePath, "sealed") + staging := filepath.Join(storagePath, "staging") + + // TODO: get the address of the miner actor + minerAddr, err := address.NewIDAddress(42) + if err != nil { + return nil, err + } + + return §orbuilder.SectorBuilderConfig{ + Miner: minerAddr, + SectorSize: 1024, + MetadataDir: metadata, + SealedDir: sealed, + StagedDir: staging, + }, nil +}