2019-07-27 00:45:27 +00:00
|
|
|
package sectorbuilder
|
|
|
|
|
|
|
|
import (
|
2019-08-07 06:35:57 +00:00
|
|
|
"encoding/binary"
|
2019-07-27 00:45:27 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
|
2019-08-07 23:22:35 +00:00
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log"
|
2019-08-06 22:04:21 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-27 00:45:27 +00:00
|
|
|
)
|
|
|
|
|
2019-08-07 23:22:35 +00:00
|
|
|
var log = logging.Logger("sectorbuilder")
|
|
|
|
|
2019-07-27 21:08:10 +00:00
|
|
|
type SectorSealingStatus = sectorbuilder.SectorSealingStatus
|
|
|
|
|
|
|
|
type StagedSectorMetadata = sectorbuilder.StagedSectorMetadata
|
|
|
|
|
2019-07-27 00:45:27 +00:00
|
|
|
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) {
|
2019-08-07 06:35:57 +00:00
|
|
|
proverId := addressToProverID(cfg.Miner)
|
2019-07-27 00:45:27 +00:00
|
|
|
|
2019-08-28 20:20:03 +00:00
|
|
|
sbp, err := sectorbuilder.InitSectorBuilder(cfg.SectorSize, 2, 1, 1, cfg.MetadataDir, proverId, cfg.SealedDir, cfg.StagedDir, 16)
|
2019-07-27 00:45:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SectorBuilder{
|
|
|
|
handle: sbp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:35:57 +00:00
|
|
|
func addressToProverID(a address.Address) [31]byte {
|
|
|
|
var proverId [31]byte
|
|
|
|
copy(proverId[:], a.Payload())
|
|
|
|
return proverId
|
|
|
|
}
|
|
|
|
|
|
|
|
func sectorIDtoBytes(sid uint64) [31]byte {
|
|
|
|
var out [31]byte
|
|
|
|
binary.LittleEndian.PutUint64(out[:], sid)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-27 00:45:27 +00:00
|
|
|
func (sb *SectorBuilder) Destroy() {
|
|
|
|
sectorbuilder.DestroySectorBuilder(sb.handle)
|
|
|
|
}
|
|
|
|
|
2019-08-07 12:41:27 +00:00
|
|
|
func (sb *SectorBuilder) AddPiece(pieceKey string, pieceSize uint64, piecePath string) (uint64, error) {
|
|
|
|
return sectorbuilder.AddPiece(sb.handle, pieceKey, pieceSize, piecePath)
|
2019-07-27 00:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-07-27 21:08:10 +00:00
|
|
|
func (sb *SectorBuilder) SealStatus(sector uint64) (SectorSealingStatus, error) {
|
2019-07-27 00:45:27 +00:00
|
|
|
return sectorbuilder.GetSectorSealingStatusByID(sb.handle, sector)
|
|
|
|
}
|
|
|
|
|
2019-07-27 21:08:10 +00:00
|
|
|
func (sb *SectorBuilder) GetAllStagedSectors() ([]StagedSectorMetadata, error) {
|
|
|
|
return sectorbuilder.GetAllStagedSectors(sb.handle)
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:47:55 +00:00
|
|
|
func (sb *SectorBuilder) GeneratePoSt(sectorInfo sectorbuilder.SortedSectorInfo, challengeSeed [CommLen]byte, faults []uint64) ([]byte, error) {
|
2019-07-27 00:45:27 +00:00
|
|
|
// Wait, this is a blocking method with no way of interrupting it?
|
|
|
|
// does it checkpoint itself?
|
2019-09-16 12:47:55 +00:00
|
|
|
return sectorbuilder.GeneratePoSt(sb.handle, sectorInfo, challengeSeed, faults)
|
2019-07-27 00:45:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 21:08:10 +00:00
|
|
|
var UserBytesForSectorSize = sectorbuilder.GetMaxUserBytesPerStagedSector
|
|
|
|
|
2019-08-07 06:35:57 +00:00
|
|
|
func VerifySeal(sectorSize uint64, commR, commD, commRStar []byte, proverID address.Address, sectorID uint64, proof []byte) (bool, error) {
|
|
|
|
var commRa, commDa, commRStara [32]byte
|
|
|
|
copy(commRa[:], commR)
|
|
|
|
copy(commDa[:], commD)
|
|
|
|
copy(commRStara[:], commRStar)
|
|
|
|
proverIDa := addressToProverID(proverID)
|
|
|
|
|
2019-09-16 12:47:55 +00:00
|
|
|
return sectorbuilder.VerifySeal(sectorSize, commRa, commDa, commRStara, proverIDa, sectorID, proof)
|
2019-07-27 00:45:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 13:52:14 +00:00
|
|
|
func VerifyPieceInclusionProof(sectorSize uint64, pieceSize uint64, commP []byte, commD []byte, proof []byte) (bool, error) {
|
|
|
|
var commPa, commDa [32]byte
|
|
|
|
copy(commPa[:], commP)
|
|
|
|
copy(commDa[:], commD)
|
|
|
|
|
|
|
|
return sectorbuilder.VerifyPieceInclusionProof(sectorSize, pieceSize, commPa, commDa, proof)
|
|
|
|
}
|
|
|
|
|
2019-09-18 15:10:03 +00:00
|
|
|
type SortedSectorInfo = sectorbuilder.SortedSectorInfo
|
|
|
|
type SectorInfo = sectorbuilder.SectorInfo
|
|
|
|
|
|
|
|
func NewSortedSectorInfo(sectors []SectorInfo) SortedSectorInfo {
|
|
|
|
return sectorbuilder.NewSortedSectorInfo(sectors...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyPost(sectorSize uint64, sectorInfo SortedSectorInfo, challengeSeed [CommLen]byte, proof []byte, faults []uint64) (bool, error) {
|
|
|
|
return sectorbuilder.VerifyPoSt(sectorSize, sectorInfo, challengeSeed, proof, faults)
|
2019-07-27 00:45:27 +00:00
|
|
|
}
|