lotus/extern/storage-sealing/commit_batch.go

536 lines
14 KiB
Go
Raw Normal View History

2021-03-10 15:16:44 +00:00
package sealing
import (
"bytes"
"context"
"sort"
"sync"
"time"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/chain/actors"
2021-03-10 15:16:44 +00:00
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"
proof5 "github.com/filecoin-project/specs-actors/v5/actors/runtime/proof"
"github.com/filecoin-project/lotus/api"
2021-05-18 11:30:47 +00:00
"github.com/filecoin-project/lotus/build"
2021-03-10 15:16:44 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
2021-05-18 17:47:30 +00:00
"github.com/filecoin-project/lotus/chain/actors/policy"
2021-03-10 15:16:44 +00:00
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
2021-06-01 09:56:19 +00:00
"github.com/filecoin-project/lotus/extern/storage-sealing/sealiface"
2021-06-08 13:43:43 +00:00
"github.com/filecoin-project/lotus/node/config"
2021-03-10 15:16:44 +00:00
)
2021-05-17 18:47:41 +00:00
const arp = abi.RegisteredAggregationProof_SnarkPackV1
2021-03-10 15:16:44 +00:00
type CommitBatcherApi interface {
SendMsg(ctx context.Context, from, to address.Address, method abi.MethodNum, value, maxFee abi.TokenAmount, params []byte) (cid.Cid, error)
StateMinerInfo(context.Context, address.Address, TipSetToken) (miner.MinerInfo, error)
2021-05-18 11:30:47 +00:00
ChainHead(ctx context.Context) (TipSetToken, abi.ChainEpoch, error)
ChainBaseFee(context.Context, TipSetToken) (abi.TokenAmount, error)
2021-06-01 09:56:19 +00:00
StateSectorPreCommitInfo(ctx context.Context, maddr address.Address, sectorNumber abi.SectorNumber, tok TipSetToken) (*miner.SectorPreCommitOnChainInfo, error)
StateMinerInitialPledgeCollateral(context.Context, address.Address, miner.SectorPreCommitInfo, TipSetToken) (big.Int, error)
StateNetworkVersion(ctx context.Context, tok TipSetToken) (network.Version, error)
2021-03-10 15:16:44 +00:00
}
type AggregateInput struct {
2021-05-26 07:58:57 +00:00
spt abi.RegisteredSealProof
2021-03-10 15:16:44 +00:00
info proof5.AggregateSealVerifyInfo
proof []byte
}
type CommitBatcher struct {
api CommitBatcherApi
maddr address.Address
mctx context.Context
addrSel AddrSel
2021-06-08 13:43:43 +00:00
feeCfg config.MinerFeeConfig
2021-03-10 15:16:44 +00:00
getConfig GetSealingConfigFunc
2021-05-19 13:20:23 +00:00
prover ffiwrapper.Prover
2021-03-10 15:16:44 +00:00
cutoffs map[abi.SectorNumber]time.Time
todo map[abi.SectorNumber]AggregateInput
waiting map[abi.SectorNumber][]chan sealiface.CommitBatchRes
2021-03-10 15:16:44 +00:00
notify, stop, stopped chan struct{}
2021-06-01 09:56:19 +00:00
force chan chan []sealiface.CommitBatchRes
2021-03-10 15:16:44 +00:00
lk sync.Mutex
}
2021-06-08 13:43:43 +00:00
func NewCommitBatcher(mctx context.Context, maddr address.Address, api CommitBatcherApi, addrSel AddrSel, feeCfg config.MinerFeeConfig, getConfig GetSealingConfigFunc, prov ffiwrapper.Prover) *CommitBatcher {
2021-03-10 15:16:44 +00:00
b := &CommitBatcher{
api: api,
maddr: maddr,
mctx: mctx,
addrSel: addrSel,
feeCfg: feeCfg,
getConfig: getConfig,
2021-05-19 13:20:23 +00:00
prover: prov,
2021-03-10 15:16:44 +00:00
cutoffs: map[abi.SectorNumber]time.Time{},
todo: map[abi.SectorNumber]AggregateInput{},
waiting: map[abi.SectorNumber][]chan sealiface.CommitBatchRes{},
2021-03-10 15:16:44 +00:00
notify: make(chan struct{}, 1),
2021-06-01 09:56:19 +00:00
force: make(chan chan []sealiface.CommitBatchRes),
2021-03-10 15:16:44 +00:00
stop: make(chan struct{}),
stopped: make(chan struct{}),
}
go b.run()
return b
}
func (b *CommitBatcher) run() {
2021-06-01 09:56:19 +00:00
var forceRes chan []sealiface.CommitBatchRes
var lastMsg []sealiface.CommitBatchRes
2021-03-10 15:16:44 +00:00
cfg, err := b.getConfig()
if err != nil {
panic(err)
}
2021-03-10 15:16:44 +00:00
for {
if forceRes != nil {
forceRes <- lastMsg
forceRes = nil
}
lastMsg = nil
var sendAboveMax, sendAboveMin bool
select {
case <-b.stop:
close(b.stopped)
return
case <-b.notify:
sendAboveMax = true
case <-b.batchWait(cfg.CommitBatchWait, cfg.CommitBatchSlack):
2021-03-10 15:16:44 +00:00
sendAboveMin = true
case fr := <-b.force: // user triggered
forceRes = fr
}
var err error
2021-06-01 09:56:19 +00:00
lastMsg, err = b.maybeStartBatch(sendAboveMax, sendAboveMin)
2021-03-10 15:16:44 +00:00
if err != nil {
2021-05-18 15:37:52 +00:00
log.Warnw("CommitBatcher processBatch error", "error", err)
2021-03-10 15:16:44 +00:00
}
}
}
func (b *CommitBatcher) batchWait(maxWait, slack time.Duration) <-chan time.Time {
2021-05-18 11:30:47 +00:00
now := time.Now()
b.lk.Lock()
defer b.lk.Unlock()
if len(b.todo) == 0 {
return nil
}
var cutoff time.Time
2021-05-18 11:30:47 +00:00
for sn := range b.todo {
sectorCutoff := b.cutoffs[sn]
if cutoff.IsZero() || (!sectorCutoff.IsZero() && sectorCutoff.Before(cutoff)) {
cutoff = sectorCutoff
2021-05-18 11:30:47 +00:00
}
}
for sn := range b.waiting {
sectorCutoff := b.cutoffs[sn]
if cutoff.IsZero() || (!sectorCutoff.IsZero() && sectorCutoff.Before(cutoff)) {
cutoff = sectorCutoff
2021-05-18 11:30:47 +00:00
}
}
if cutoff.IsZero() {
return time.After(maxWait)
2021-05-18 11:30:47 +00:00
}
cutoff = cutoff.Add(-slack)
if cutoff.Before(now) {
return time.After(time.Nanosecond) // can't return 0
2021-05-18 11:30:47 +00:00
}
wait := cutoff.Sub(now)
2021-05-18 11:30:47 +00:00
if wait > maxWait {
wait = maxWait
}
return time.After(wait)
2021-05-18 11:30:47 +00:00
}
2021-06-01 09:56:19 +00:00
func (b *CommitBatcher) maybeStartBatch(notif, after bool) ([]sealiface.CommitBatchRes, error) {
2021-03-10 15:16:44 +00:00
b.lk.Lock()
defer b.lk.Unlock()
total := len(b.todo)
if total == 0 {
return nil, nil // nothing to do
}
cfg, err := b.getConfig()
if err != nil {
return nil, xerrors.Errorf("getting config: %w", err)
}
if notif && total < cfg.MaxCommitBatch {
return nil, nil
}
if after && total < cfg.MinCommitBatch {
return nil, nil
}
2021-06-01 09:56:19 +00:00
var res []sealiface.CommitBatchRes
if total < cfg.MinCommitBatch || total < miner5.MinAggregatedSectors {
2021-06-01 09:56:19 +00:00
res, err = b.processIndividually()
} else {
res, err = b.processBatch(cfg)
}
if err != nil && len(res) == 0 {
return nil, err
}
for _, r := range res {
if err != nil {
r.Error = err.Error()
}
for _, sn := range r.Sectors {
for _, ch := range b.waiting[sn] {
ch <- r // buffered
}
delete(b.waiting, sn)
delete(b.todo, sn)
delete(b.cutoffs, sn)
2021-06-01 09:56:19 +00:00
}
}
return res, nil
}
func (b *CommitBatcher) processBatch(cfg sealiface.Config) ([]sealiface.CommitBatchRes, error) {
tok, _, err := b.api.ChainHead(b.mctx)
if err != nil {
return nil, err
}
2021-06-01 09:56:19 +00:00
total := len(b.todo)
var res sealiface.CommitBatchRes
params := miner5.ProveCommitAggregateParams{
SectorNumbers: bitfield.New(),
}
2021-05-17 20:51:29 +00:00
proofs := make([][]byte, 0, total)
2021-05-18 14:51:06 +00:00
infos := make([]proof5.AggregateSealVerifyInfo, 0, total)
collateral := big.Zero()
2021-03-10 15:16:44 +00:00
for id, p := range b.todo {
if len(infos) >= cfg.MaxCommitBatch {
log.Infow("commit batch full")
break
}
sc, err := b.getSectorCollateral(id, tok)
if err != nil {
res.FailedSectors[id] = err.Error()
continue
}
collateral = big.Add(collateral, sc)
res.Sectors = append(res.Sectors, id)
2021-03-10 15:16:44 +00:00
params.SectorNumbers.Set(uint64(id))
2021-05-18 14:51:06 +00:00
infos = append(infos, p.info)
2021-03-10 15:16:44 +00:00
}
2021-05-18 16:58:41 +00:00
sort.Slice(infos, func(i, j int) bool {
return infos[i].Number < infos[j].Number
})
for _, info := range infos {
proofs = append(proofs, b.todo[info.Number].proof)
}
2021-05-18 18:34:23 +00:00
mid, err := address.IDFromAddress(b.maddr)
if err != nil {
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("getting miner id: %w", err)
2021-05-18 18:34:23 +00:00
}
2021-05-19 13:20:23 +00:00
params.AggregateProof, err = b.prover.AggregateSealProofs(proof5.AggregateSealVerifyProofAndInfos{
2021-05-18 18:34:23 +00:00
Miner: abi.ActorID(mid),
SealProof: b.todo[infos[0].Number].spt,
2021-05-18 14:51:06 +00:00
AggregateProof: arp,
Infos: infos,
}, proofs)
2021-03-10 15:16:44 +00:00
if err != nil {
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("aggregating proofs: %w", err)
2021-03-10 15:16:44 +00:00
}
enc := new(bytes.Buffer)
if err := params.MarshalCBOR(enc); err != nil {
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("couldn't serialize ProveCommitAggregateParams: %w", err)
2021-03-10 15:16:44 +00:00
}
mi, err := b.api.StateMinerInfo(b.mctx, b.maddr, nil)
if err != nil {
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("couldn't get miner info: %w", err)
2021-03-10 15:16:44 +00:00
}
2021-06-08 13:43:43 +00:00
maxFee := b.feeCfg.MaxCommitBatchGasFee.FeeForSectors(len(infos))
bf, err := b.api.ChainBaseFee(b.mctx, tok)
if err != nil {
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("couldn't get base fee: %w", err)
}
nv, err := b.api.StateNetworkVersion(b.mctx, tok)
if err != nil {
log.Errorf("getting network version: %s", err)
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("getting network version: %s", err)
}
aggFee := policy.AggregateNetworkFee(nv, len(infos), bf)
2021-06-08 13:43:43 +00:00
goodFunds := big.Add(maxFee, big.Add(collateral, aggFee))
from, _, err := b.addrSel(b.mctx, mi, api.CommitAddr, goodFunds, collateral)
2021-03-10 15:16:44 +00:00
if err != nil {
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("no good address found: %w", err)
2021-03-10 15:16:44 +00:00
}
2021-06-08 13:43:43 +00:00
mcid, err := b.api.SendMsg(b.mctx, from, b.maddr, miner.Methods.ProveCommitAggregate, collateral, maxFee, enc.Bytes())
2021-03-10 15:16:44 +00:00
if err != nil {
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, xerrors.Errorf("sending message failed: %w", err)
2021-03-10 15:16:44 +00:00
}
2021-06-01 09:56:19 +00:00
res.Msg = &mcid
2021-05-19 18:34:50 +00:00
log.Infow("Sent ProveCommitAggregate message", "cid", mcid, "from", from, "todo", total, "sectors", len(infos))
2021-03-10 15:16:44 +00:00
2021-06-01 09:56:19 +00:00
return []sealiface.CommitBatchRes{res}, nil
}
2021-03-10 15:16:44 +00:00
2021-06-01 09:56:19 +00:00
func (b *CommitBatcher) processIndividually() ([]sealiface.CommitBatchRes, error) {
mi, err := b.api.StateMinerInfo(b.mctx, b.maddr, nil)
if err != nil {
return nil, xerrors.Errorf("couldn't get miner info: %w", err)
}
tok, _, err := b.api.ChainHead(b.mctx)
if err != nil {
return nil, err
}
var res []sealiface.CommitBatchRes
for sn, info := range b.todo {
r := sealiface.CommitBatchRes{
Sectors: []abi.SectorNumber{sn},
2021-03-10 15:16:44 +00:00
}
2021-06-01 09:56:19 +00:00
mcid, err := b.processSingle(mi, sn, info, tok)
if err != nil {
log.Errorf("process single error: %+v", err) // todo: return to user
r.FailedSectors[sn] = err.Error()
} else {
r.Msg = &mcid
}
res = append(res, r)
}
return res, nil
}
func (b *CommitBatcher) processSingle(mi miner.MinerInfo, sn abi.SectorNumber, info AggregateInput, tok TipSetToken) (cid.Cid, error) {
enc := new(bytes.Buffer)
params := &miner.ProveCommitSectorParams{
SectorNumber: sn,
Proof: info.proof,
}
if err := params.MarshalCBOR(enc); err != nil {
return cid.Undef, xerrors.Errorf("marshaling commit params: %w", err)
}
collateral, err := b.getSectorCollateral(sn, tok)
2021-03-10 15:16:44 +00:00
if err != nil {
2021-06-01 09:56:19 +00:00
return cid.Undef, err
2021-03-10 15:16:44 +00:00
}
2021-06-08 13:43:43 +00:00
goodFunds := big.Add(collateral, big.Int(b.feeCfg.MaxCommitGasFee))
2021-06-01 09:56:19 +00:00
from, _, err := b.addrSel(b.mctx, mi, api.CommitAddr, goodFunds, collateral)
if err != nil {
return cid.Undef, xerrors.Errorf("no good address to send commit message from: %w", err)
}
2021-06-08 13:43:43 +00:00
mcid, err := b.api.SendMsg(b.mctx, from, b.maddr, miner.Methods.ProveCommitSector, collateral, big.Int(b.feeCfg.MaxCommitGasFee), enc.Bytes())
2021-06-01 09:56:19 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("pushing message to mpool: %w", err)
}
return mcid, nil
2021-03-10 15:16:44 +00:00
}
// register commit, wait for batch message, return message CID
2021-06-01 09:56:19 +00:00
func (b *CommitBatcher) AddCommit(ctx context.Context, s SectorInfo, in AggregateInput) (res sealiface.CommitBatchRes, err error) {
sn := s.SectorNumber
cu, err := b.getCommitCutoff(s)
2021-05-18 14:51:06 +00:00
if err != nil {
return sealiface.CommitBatchRes{}, err
2021-05-18 14:51:06 +00:00
}
2021-03-10 15:16:44 +00:00
b.lk.Lock()
b.cutoffs[sn] = cu
2021-05-18 11:30:47 +00:00
b.todo[sn] = in
2021-03-10 15:16:44 +00:00
2021-06-01 09:56:19 +00:00
sent := make(chan sealiface.CommitBatchRes, 1)
2021-05-18 11:30:47 +00:00
b.waiting[sn] = append(b.waiting[sn], sent)
2021-03-10 15:16:44 +00:00
select {
case b.notify <- struct{}{}:
default: // already have a pending notification, don't need more
}
b.lk.Unlock()
select {
2021-06-01 09:56:19 +00:00
case r := <-sent:
return r, nil
2021-03-10 15:16:44 +00:00
case <-ctx.Done():
2021-06-01 09:56:19 +00:00
return sealiface.CommitBatchRes{}, ctx.Err()
2021-03-10 15:16:44 +00:00
}
}
2021-06-01 09:56:19 +00:00
func (b *CommitBatcher) Flush(ctx context.Context) ([]sealiface.CommitBatchRes, error) {
resCh := make(chan []sealiface.CommitBatchRes, 1)
2021-03-10 15:16:44 +00:00
select {
case b.force <- resCh:
select {
case res := <-resCh:
return res, nil
case <-ctx.Done():
return nil, ctx.Err()
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (b *CommitBatcher) Pending(ctx context.Context) ([]abi.SectorID, error) {
b.lk.Lock()
defer b.lk.Unlock()
mid, err := address.IDFromAddress(b.maddr)
if err != nil {
return nil, err
}
res := make([]abi.SectorID, 0)
for _, s := range b.todo {
res = append(res, abi.SectorID{
Miner: abi.ActorID(mid),
Number: s.info.Number,
})
}
sort.Slice(res, func(i, j int) bool {
if res[i].Miner != res[j].Miner {
return res[i].Miner < res[j].Miner
}
return res[i].Number < res[j].Number
})
return res, nil
}
func (b *CommitBatcher) Stop(ctx context.Context) error {
close(b.stop)
select {
case <-b.stopped:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
2021-05-18 14:51:06 +00:00
2021-06-09 16:26:20 +00:00
// TODO: If this returned epochs, it would make testing much easier
func (b *CommitBatcher) getCommitCutoff(si SectorInfo) (time.Time, error) {
tok, curEpoch, err := b.api.ChainHead(b.mctx)
if err != nil {
2021-06-09 16:26:20 +00:00
return time.Now(), xerrors.Errorf("getting chain head: %s", err)
}
nv, err := b.api.StateNetworkVersion(b.mctx, tok)
if err != nil {
log.Errorf("getting network version: %s", err)
2021-06-09 16:26:20 +00:00
return time.Now(), xerrors.Errorf("getting network version: %s", err)
}
pci, err := b.api.StateSectorPreCommitInfo(b.mctx, b.maddr, si.SectorNumber, tok)
if err != nil {
log.Errorf("getting precommit info: %s", err)
return time.Now(), err
}
cutoffEpoch := pci.PreCommitEpoch + policy.GetMaxProveCommitDuration(actors.VersionForNetwork(nv), si.SectorType)
2021-05-18 14:51:06 +00:00
for _, p := range si.Pieces {
if p.DealInfo == nil {
continue
}
startEpoch := p.DealInfo.DealSchedule.StartEpoch
if startEpoch < cutoffEpoch {
cutoffEpoch = startEpoch
2021-05-18 14:51:06 +00:00
}
}
if cutoffEpoch <= curEpoch {
return time.Now(), nil
2021-05-18 14:51:06 +00:00
}
return time.Now().Add(time.Duration(cutoffEpoch-curEpoch) * time.Duration(build.BlockDelaySecs) * time.Second), nil
2021-05-18 14:51:06 +00:00
}
2021-06-01 09:56:19 +00:00
func (b *CommitBatcher) getSectorCollateral(sn abi.SectorNumber, tok TipSetToken) (abi.TokenAmount, error) {
pci, err := b.api.StateSectorPreCommitInfo(b.mctx, b.maddr, sn, tok)
if err != nil {
return big.Zero(), xerrors.Errorf("getting precommit info: %w", err)
}
if pci == nil {
return big.Zero(), xerrors.Errorf("precommit info not found on chain")
}
collateral, err := b.api.StateMinerInitialPledgeCollateral(b.mctx, b.maddr, pci.Info, tok)
if err != nil {
return big.Zero(), xerrors.Errorf("getting initial pledge collateral: %w", err)
}
collateral = big.Sub(collateral, pci.PreCommitDeposit)
if collateral.LessThan(big.Zero()) {
collateral = big.Zero()
}
return collateral, nil
}