2019-11-01 13:58:48 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-07 07:57:10 +00:00
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-11-08 20:11:56 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/lib/sectorbuilder"
|
2019-11-01 13:58:48 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
type providerHandlerFunc func(ctx context.Context, deal SectorInfo) *sectorUpdate
|
2019-11-01 13:58:48 +00:00
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (m *Miner) handleSectorUpdate(ctx context.Context, sector SectorInfo, cb providerHandlerFunc) {
|
2019-11-01 13:58:48 +00:00
|
|
|
go func() {
|
2019-12-03 23:42:22 +00:00
|
|
|
update := cb(ctx, sector)
|
2019-11-01 13:58:48 +00:00
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
if update == nil {
|
|
|
|
return // async
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2019-12-03 23:42:22 +00:00
|
|
|
case m.sectorUpdated <- *update:
|
2019-11-01 13:58:48 +00:00
|
|
|
case <-m.stop:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (m *Miner) handlePacking(ctx context.Context, sector SectorInfo) *sectorUpdate {
|
2019-11-06 23:09:48 +00:00
|
|
|
log.Infow("performing filling up rest of the sector...", "sector", sector.SectorID)
|
|
|
|
|
2019-11-07 18:22:59 +00:00
|
|
|
var allocated uint64
|
|
|
|
for _, piece := range sector.Pieces {
|
|
|
|
allocated += piece.Size
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:43:15 +00:00
|
|
|
ubytes := sectorbuilder.UserBytesForSectorSize(m.sb.SectorSize())
|
|
|
|
|
|
|
|
if allocated > ubytes {
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().fatal(xerrors.Errorf("too much data in sector: %d > %d", allocated, ubytes))
|
2019-11-07 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 18:43:15 +00:00
|
|
|
fillerSizes, err := fillersFromRem(ubytes - allocated)
|
2019-11-06 23:09:48 +00:00
|
|
|
if err != nil {
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().fatal(err)
|
2019-11-06 23:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(fillerSizes) > 0 {
|
|
|
|
log.Warnf("Creating %d filler pieces for sector %d", len(fillerSizes), sector.SectorID)
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:40:46 +00:00
|
|
|
pieces, err := m.storeGarbage(ctx, sector.SectorID, sector.existingPieces(), fillerSizes...)
|
2019-11-06 23:09:48 +00:00
|
|
|
if err != nil {
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().fatal(xerrors.Errorf("filling up the sector (%v): %w", fillerSizes, err))
|
2019-11-06 23:09:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().to(api.Unsealed).state(func(info *SectorInfo) {
|
2019-11-07 18:22:59 +00:00
|
|
|
info.Pieces = append(info.Pieces, pieces...)
|
2019-12-03 23:42:22 +00:00
|
|
|
})
|
2019-11-06 23:09:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (m *Miner) handleUnsealed(ctx context.Context, sector SectorInfo) *sectorUpdate {
|
2019-11-01 13:58:48 +00:00
|
|
|
log.Infow("performing sector replication...", "sector", sector.SectorID)
|
2019-11-07 18:22:59 +00:00
|
|
|
ticket, err := m.tktFn(ctx)
|
|
|
|
if err != nil {
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().fatal(err)
|
2019-11-07 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rspco, err := m.sb.SealPreCommit(sector.SectorID, *ticket, sector.pieceInfos())
|
2019-11-01 13:58:48 +00:00
|
|
|
if err != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.SealFailed).error(xerrors.Errorf("seal pre commit failed: %w", err))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().to(api.PreCommitting).state(func(info *SectorInfo) {
|
2019-11-07 18:22:59 +00:00
|
|
|
info.CommC = rspco.CommC[:]
|
|
|
|
info.CommD = rspco.CommD[:]
|
|
|
|
info.CommR = rspco.CommR[:]
|
|
|
|
info.CommRLast = rspco.CommRLast[:]
|
2019-11-01 13:58:48 +00:00
|
|
|
info.Ticket = SealTicket{
|
2019-11-07 18:22:59 +00:00
|
|
|
BlockHeight: ticket.BlockHeight,
|
|
|
|
TicketBytes: ticket.TicketBytes[:],
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
2019-12-03 23:42:22 +00:00
|
|
|
})
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (m *Miner) handlePreCommitting(ctx context.Context, sector SectorInfo) *sectorUpdate {
|
2019-11-07 12:03:18 +00:00
|
|
|
params := &actors.SectorPreCommitInfo{
|
2019-11-01 13:58:48 +00:00
|
|
|
SectorNumber: sector.SectorID,
|
2019-11-07 12:03:18 +00:00
|
|
|
|
|
|
|
CommR: sector.CommR,
|
|
|
|
SealEpoch: sector.Ticket.BlockHeight,
|
2019-11-07 18:22:59 +00:00
|
|
|
DealIDs: sector.deals(),
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
enc, aerr := actors.SerializeParams(params)
|
|
|
|
if aerr != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.PreCommitFailed).error(xerrors.Errorf("could not serialize commit sector parameters: %w", aerr))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: m.maddr,
|
|
|
|
From: m.worker,
|
|
|
|
Method: actors.MAMethods.PreCommitSector,
|
|
|
|
Params: enc,
|
|
|
|
Value: types.NewInt(0), // TODO: need to ensure sufficient collateral
|
|
|
|
GasLimit: types.NewInt(1000000 /* i dont know help */),
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("submitting precommit for sector: ", sector.SectorID)
|
|
|
|
smsg, err := m.api.MpoolPushMessage(ctx, msg)
|
|
|
|
if err != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.PreCommitFailed).error(xerrors.Errorf("pushing message to mpool: %w", err))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().to(api.PreCommitted).state(func(info *SectorInfo) {
|
2019-11-01 13:58:48 +00:00
|
|
|
mcid := smsg.Cid()
|
|
|
|
info.PreCommitMessage = &mcid
|
2019-12-03 23:42:22 +00:00
|
|
|
})
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (m *Miner) handlePreCommitted(ctx context.Context, sector SectorInfo) *sectorUpdate {
|
2019-11-01 13:58:48 +00:00
|
|
|
// would be ideal to just use the events.Called handler, but it wouldnt be able to handle individual message timeouts
|
2019-11-07 07:57:10 +00:00
|
|
|
log.Info("Sector precommitted: ", sector.SectorID)
|
2019-11-01 13:58:48 +00:00
|
|
|
mw, err := m.api.StateWaitMsg(ctx, *sector.PreCommitMessage)
|
|
|
|
if err != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.PreCommitFailed).error(err)
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mw.Receipt.ExitCode != 0 {
|
|
|
|
log.Error("sector precommit failed: ", mw.Receipt.ExitCode)
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.PreCommitFailed).error(err)
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
2019-11-07 07:57:10 +00:00
|
|
|
log.Info("precommit message landed on chain: ", sector.SectorID)
|
2019-11-01 13:58:48 +00:00
|
|
|
|
2019-11-02 15:07:18 +00:00
|
|
|
randHeight := mw.TipSet.Height() + build.InteractivePoRepDelay - 1 // -1 because of how the messages are applied
|
2019-11-05 14:03:59 +00:00
|
|
|
log.Infof("precommit for sector %d made it on chain, will start proof computation at height %d", sector.SectorID, randHeight)
|
2019-11-01 13:58:48 +00:00
|
|
|
|
2019-11-05 14:03:59 +00:00
|
|
|
err = m.events.ChainAt(func(ctx context.Context, ts *types.TipSet, curH uint64) error {
|
2019-11-19 15:53:00 +00:00
|
|
|
rand, err := m.api.ChainGetRandomness(ctx, ts.Key(), int64(randHeight))
|
2019-11-08 18:15:13 +00:00
|
|
|
if err != nil {
|
2019-12-03 23:42:22 +00:00
|
|
|
err = xerrors.Errorf("failed to get randomness for computing seal proof: %w", err)
|
2019-11-08 18:15:13 +00:00
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
m.sectorUpdated <- *sector.upd().fatal(err)
|
|
|
|
return err
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
m.sectorUpdated <- *sector.upd().to(api.Committing).state(func(info *SectorInfo) {
|
|
|
|
info.Seed = SealSeed{
|
|
|
|
BlockHeight: randHeight,
|
|
|
|
TicketBytes: rand,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
return nil
|
2019-11-05 14:03:59 +00:00
|
|
|
}, func(ctx context.Context, ts *types.TipSet) error {
|
2019-11-01 13:58:48 +00:00
|
|
|
log.Warn("revert in interactive commit sector step")
|
|
|
|
return nil
|
|
|
|
}, 3, mw.TipSet.Height()+build.InteractivePoRepDelay)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("waitForPreCommitMessage ChainAt errored: ", err)
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
return nil
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
func (m *Miner) handleCommitting(ctx context.Context, sector SectorInfo) *sectorUpdate {
|
2019-11-01 13:58:48 +00:00
|
|
|
log.Info("scheduling seal proof computation...")
|
|
|
|
|
2019-11-26 15:15:32 +00:00
|
|
|
proof, err := m.sb.SealCommit(sector.SectorID, sector.Ticket.SB(), sector.Seed.SB(), sector.pieceInfos(), sector.rspco())
|
2019-11-01 23:43:54 +00:00
|
|
|
if err != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.SealCommitFailed).error(xerrors.Errorf("computing seal proof failed: %w", err))
|
2019-11-01 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 18:15:13 +00:00
|
|
|
// TODO: Consider splitting states and persist proof for faster recovery
|
|
|
|
|
2019-11-01 13:58:48 +00:00
|
|
|
params := &actors.SectorProveCommitInfo{
|
|
|
|
Proof: proof,
|
|
|
|
SectorID: sector.SectorID,
|
2019-11-07 18:22:59 +00:00
|
|
|
DealIDs: sector.deals(),
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 23:43:54 +00:00
|
|
|
enc, aerr := actors.SerializeParams(params)
|
2019-11-01 13:58:48 +00:00
|
|
|
if aerr != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.CommitFailed).error(xerrors.Errorf("could not serialize commit sector parameters: %w", aerr))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: m.maddr,
|
|
|
|
From: m.worker,
|
|
|
|
Method: actors.MAMethods.ProveCommitSector,
|
|
|
|
Params: enc,
|
|
|
|
Value: types.NewInt(0), // TODO: need to ensure sufficient collateral
|
|
|
|
GasLimit: types.NewInt(1000000 /* i dont know help */),
|
|
|
|
GasPrice: types.NewInt(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
smsg, err := m.api.MpoolPushMessage(ctx, msg)
|
|
|
|
if err != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.CommitFailed).error(xerrors.Errorf("pushing message to mpool: %w", err))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Separate state before this wait, so we persist message cid?
|
|
|
|
|
|
|
|
mw, err := m.api.StateWaitMsg(ctx, smsg.Cid())
|
|
|
|
if err != nil {
|
2019-12-03 23:59:40 +00:00
|
|
|
return sector.upd().to(api.CommitFailed).error(xerrors.Errorf("failed to wait for porep inclusion: %w", err))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mw.Receipt.ExitCode != 0 {
|
2019-11-21 04:18:04 +00:00
|
|
|
log.Errorf("UNHANDLED: submitting sector proof failed (exit=%d, msg=%s) (t:%x; s:%x(%d); p:%x)", mw.Receipt.ExitCode, smsg.Cid(), sector.Ticket.TicketBytes, sector.Seed.TicketBytes, sector.Seed.BlockHeight, params.Proof)
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().fatal(xerrors.New("UNHANDLED: submitting sector proof failed"))
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 23:42:22 +00:00
|
|
|
return sector.upd().to(api.Proving).state(func(info *SectorInfo) {
|
2019-11-01 13:58:48 +00:00
|
|
|
mcid := smsg.Cid()
|
|
|
|
info.CommitMessage = &mcid
|
2019-11-08 18:15:13 +00:00
|
|
|
info.Proof = proof
|
2019-12-03 23:42:22 +00:00
|
|
|
})
|
2019-11-01 13:58:48 +00:00
|
|
|
}
|