2020-03-23 11:40:02 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
commcid "github.com/filecoin-project/go-fil-commcid"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
"github.com/filecoin-project/specs-storage/storage"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-03-27 23:21:36 +00:00
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
2020-03-23 11:40:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("sbmock")
|
|
|
|
|
|
|
|
type SectorMgr struct {
|
|
|
|
sectors map[abi.SectorID]*sectorState
|
|
|
|
sectorSize abi.SectorSize
|
|
|
|
nextSectorID abi.SectorNumber
|
|
|
|
proofType abi.RegisteredProof
|
|
|
|
|
|
|
|
lk sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockVerif struct{}
|
|
|
|
|
2020-05-08 11:36:08 +00:00
|
|
|
func NewMockSectorMgr(ssize abi.SectorSize) *SectorMgr {
|
2020-04-10 21:01:35 +00:00
|
|
|
rt, err := ffiwrapper.SealProofTypeFromSectorSize(ssize)
|
2020-03-23 11:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SectorMgr{
|
|
|
|
sectors: make(map[abi.SectorID]*sectorState),
|
|
|
|
sectorSize: ssize,
|
|
|
|
nextSectorID: 5,
|
|
|
|
proofType: rt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
statePacking = iota
|
|
|
|
statePreCommit
|
2020-04-01 23:18:20 +00:00
|
|
|
stateCommit // nolint
|
2020-03-23 11:40:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sectorState struct {
|
|
|
|
pieces []cid.Cid
|
|
|
|
failed bool
|
|
|
|
|
|
|
|
state int
|
|
|
|
|
|
|
|
lk sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) NewSector(ctx context.Context, sector abi.SectorID) error {
|
2020-03-23 11:40:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) AddPiece(ctx context.Context, sectorId abi.SectorID, existingPieces []abi.UnpaddedPieceSize, size abi.UnpaddedPieceSize, r io.Reader) (abi.PieceInfo, error) {
|
|
|
|
log.Warn("Add piece: ", sectorId, size, mgr.proofType)
|
|
|
|
mgr.lk.Lock()
|
|
|
|
ss, ok := mgr.sectors[sectorId]
|
2020-03-23 11:40:02 +00:00
|
|
|
if !ok {
|
|
|
|
ss = §orState{
|
|
|
|
state: statePacking,
|
|
|
|
}
|
2020-04-10 19:12:23 +00:00
|
|
|
mgr.sectors[sectorId] = ss
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
2020-04-10 19:12:23 +00:00
|
|
|
mgr.lk.Unlock()
|
2020-03-23 11:40:02 +00:00
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
c, err := ffiwrapper.GeneratePieceCIDFromFile(mgr.proofType, r, size)
|
2020-03-23 11:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return abi.PieceInfo{}, xerrors.Errorf("failed to generate piece cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("Generated Piece CID: ", c)
|
|
|
|
|
|
|
|
ss.pieces = append(ss.pieces, c)
|
|
|
|
return abi.PieceInfo{
|
|
|
|
Size: size.Padded(),
|
|
|
|
PieceCID: c,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) SectorSize() abi.SectorSize {
|
|
|
|
return mgr.sectorSize
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) AcquireSectorNumber() (abi.SectorNumber, error) {
|
|
|
|
mgr.lk.Lock()
|
|
|
|
defer mgr.lk.Unlock()
|
|
|
|
id := mgr.nextSectorID
|
|
|
|
mgr.nextSectorID++
|
2020-03-23 11:40:02 +00:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) SealPreCommit1(ctx context.Context, sid abi.SectorID, ticket abi.SealRandomness, pieces []abi.PieceInfo) (out storage.PreCommit1Out, err error) {
|
|
|
|
mgr.lk.Lock()
|
|
|
|
ss, ok := mgr.sectors[sid]
|
|
|
|
mgr.lk.Unlock()
|
2020-03-23 11:40:02 +00:00
|
|
|
if !ok {
|
2020-03-26 19:34:38 +00:00
|
|
|
return nil, xerrors.Errorf("no sector with id %d in storage", sid)
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
ussize := abi.PaddedPieceSize(mgr.sectorSize).Unpadded()
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
// TODO: verify pieces in sinfo.pieces match passed in pieces
|
|
|
|
|
|
|
|
var sum abi.UnpaddedPieceSize
|
|
|
|
for _, p := range pieces {
|
|
|
|
sum += p.Size.Unpadded()
|
|
|
|
}
|
|
|
|
|
|
|
|
if sum != ussize {
|
|
|
|
return nil, xerrors.Errorf("aggregated piece sizes don't match up: %d != %d", sum, ussize)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ss.state != statePacking {
|
|
|
|
return nil, xerrors.Errorf("cannot call pre-seal on sector not in 'packing' state")
|
|
|
|
}
|
|
|
|
|
|
|
|
opFinishWait(ctx)
|
|
|
|
|
|
|
|
ss.state = statePreCommit
|
|
|
|
|
|
|
|
pis := make([]abi.PieceInfo, len(ss.pieces))
|
|
|
|
for i, piece := range ss.pieces {
|
|
|
|
pis[i] = abi.PieceInfo{
|
|
|
|
Size: pieces[i].Size,
|
|
|
|
PieceCID: piece,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
commd, err := MockVerifier.GenerateDataCommitment(mgr.proofType, pis)
|
2020-03-23 11:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, _, err := commcid.CIDToCommitment(commd)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cc[0] ^= 'd'
|
|
|
|
|
|
|
|
return cc, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) SealPreCommit2(ctx context.Context, sid abi.SectorID, phase1Out storage.PreCommit1Out) (cids storage.SectorCids, err error) {
|
2020-03-23 11:40:02 +00:00
|
|
|
db := []byte(string(phase1Out))
|
|
|
|
db[0] ^= 'd'
|
|
|
|
|
|
|
|
d := commcid.DataCommitmentV1ToCID(db)
|
|
|
|
|
|
|
|
commr := make([]byte, 32)
|
|
|
|
for i := range db {
|
|
|
|
commr[32-(i+1)] = db[i]
|
|
|
|
}
|
|
|
|
|
2020-04-17 22:52:42 +00:00
|
|
|
commR := commcid.ReplicaCommitmentV1ToCID(commr)
|
2020-03-23 11:40:02 +00:00
|
|
|
|
|
|
|
return storage.SectorCids{
|
|
|
|
Unsealed: d,
|
|
|
|
Sealed: commR,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) SealCommit1(ctx context.Context, sid abi.SectorID, ticket abi.SealRandomness, seed abi.InteractiveSealRandomness, pieces []abi.PieceInfo, cids storage.SectorCids) (output storage.Commit1Out, err error) {
|
|
|
|
mgr.lk.Lock()
|
|
|
|
ss, ok := mgr.sectors[sid]
|
|
|
|
mgr.lk.Unlock()
|
2020-03-23 11:40:02 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, xerrors.Errorf("no such sector %d", sid)
|
|
|
|
}
|
|
|
|
ss.lk.Lock()
|
|
|
|
defer ss.lk.Unlock()
|
|
|
|
|
|
|
|
if ss.failed {
|
|
|
|
return nil, xerrors.Errorf("[mock] cannot commit failed sector %d", sid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ss.state != statePreCommit {
|
|
|
|
return nil, xerrors.Errorf("cannot commit sector that has not been precommitted")
|
|
|
|
}
|
|
|
|
|
|
|
|
opFinishWait(ctx)
|
|
|
|
|
|
|
|
var out [32]byte
|
|
|
|
for i := range out {
|
|
|
|
out[i] = cids.Unsealed.Bytes()[i] + cids.Sealed.Bytes()[31-i] - ticket[i]*seed[i] ^ byte(sid.Number&0xff)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out[:], nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) SealCommit2(ctx context.Context, sid abi.SectorID, phase1Out storage.Commit1Out) (proof storage.Proof, err error) {
|
2020-03-23 11:40:02 +00:00
|
|
|
var out [32]byte
|
|
|
|
for i := range out {
|
|
|
|
out[i] = phase1Out[i] ^ byte(sid.Number&0xff)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out[:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Instrumentation Methods
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) FailSector(sid abi.SectorID) error {
|
|
|
|
mgr.lk.Lock()
|
|
|
|
defer mgr.lk.Unlock()
|
|
|
|
ss, ok := mgr.sectors[sid]
|
2020-03-23 11:40:02 +00:00
|
|
|
if !ok {
|
2020-03-26 19:34:38 +00:00
|
|
|
return fmt.Errorf("no such sector in storage")
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss.failed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func opFinishWait(ctx context.Context) {
|
|
|
|
val, ok := ctx.Value("opfinish").(chan struct{})
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
<-val
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddOpFinish(ctx context.Context) (context.Context, func()) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
return context.WithValue(ctx, "opfinish", done), func() {
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) GenerateWinningPoSt(ctx context.Context, minerID abi.ActorID, sectorInfo []abi.SectorInfo, randomness abi.PoStRandomness) ([]abi.PoStProof, error) {
|
2020-03-23 11:40:02 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) GenerateWindowPoSt(ctx context.Context, minerID abi.ActorID, sectorInfo []abi.SectorInfo, randomness abi.PoStRandomness) ([]abi.PoStProof, error) {
|
|
|
|
panic("implement me")
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) ReadPieceFromSealedSector(ctx context.Context, sectorID abi.SectorID, offset ffiwrapper.UnpaddedByteIndex, size abi.UnpaddedPieceSize, ticket abi.SealRandomness, commD cid.Cid) (io.ReadCloser, error) {
|
|
|
|
if len(mgr.sectors[sectorID].pieces) > 1 {
|
2020-03-23 11:40:02 +00:00
|
|
|
panic("implme")
|
|
|
|
}
|
2020-04-10 19:12:23 +00:00
|
|
|
return ioutil.NopCloser(io.LimitReader(bytes.NewReader(mgr.sectors[sectorID].pieces[0].Bytes()[offset:]), int64(size))), nil
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) StageFakeData(mid abi.ActorID) (abi.SectorID, []abi.PieceInfo, error) {
|
|
|
|
usize := abi.PaddedPieceSize(mgr.sectorSize).Unpadded()
|
|
|
|
sid, err := mgr.AcquireSectorNumber()
|
2020-03-23 11:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return abi.SectorID{}, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, usize)
|
|
|
|
rand.Read(buf)
|
|
|
|
|
|
|
|
id := abi.SectorID{
|
|
|
|
Miner: mid,
|
|
|
|
Number: sid,
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
pi, err := mgr.AddPiece(context.TODO(), id, nil, usize, bytes.NewReader(buf))
|
2020-03-23 11:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return abi.SectorID{}, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, []abi.PieceInfo{pi}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (mgr *SectorMgr) FinalizeSector(context.Context, abi.SectorID) error {
|
2020-03-23 11:40:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockVerif) VerifySeal(svi abi.SealVerifyInfo) (bool, error) {
|
|
|
|
if len(svi.OnChain.Proof) != 32 { // Real ones are longer, but this should be fine
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, b := range svi.OnChain.Proof {
|
|
|
|
if b != svi.UnsealedCID.Bytes()[i]+svi.OnChain.SealedCID.Bytes()[31-i]-svi.InteractiveRandomness[i]*svi.Randomness[i] {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-04-10 19:12:23 +00:00
|
|
|
func (m mockVerif) VerifyWinningPoSt(ctx context.Context, info abi.WinningPoStVerifyInfo) (bool, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockVerif) VerifyWindowPoSt(ctx context.Context, info abi.WindowPoStVerifyInfo) (bool, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:04:40 +00:00
|
|
|
func (m mockVerif) GenerateDataCommitment(pt abi.RegisteredProof, pieces []abi.PieceInfo) (cid.Cid, error) {
|
2020-03-26 02:50:56 +00:00
|
|
|
return ffiwrapper.GenerateUnsealedCID(pt, pieces)
|
2020-03-23 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 14:00:21 +00:00
|
|
|
func (m mockVerif) GenerateWinningPoStSectorChallenge(ctx context.Context, proofType abi.RegisteredProof, minerID abi.ActorID, randomness abi.PoStRandomness, eligibleSectorCount uint64) ([]uint64, error) {
|
2020-04-17 22:52:42 +00:00
|
|
|
return []uint64{0}, nil
|
2020-04-17 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 11:40:02 +00:00
|
|
|
var MockVerifier = mockVerif{}
|
|
|
|
|
2020-03-26 02:50:56 +00:00
|
|
|
var _ ffiwrapper.Verifier = MockVerifier
|