lotus/extern/storage-sealing/commit_batch.go

359 lines
8.5 KiB
Go
Raw Normal View History

2021-03-10 15:16:44 +00:00
package sealing
import (
"bytes"
"context"
"sort"
"sync"
"time"
"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-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)
2021-03-10 15:16:44 +00:00
}
type AggregateInput struct {
2021-05-17 18:47:41 +00:00
spt abi.RegisteredSealProof
// TODO: Something changed in actors, I think this now needs to be AggregateSealVerifyProofAndInfos todo ??
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
feeCfg FeeConfig
getConfig GetSealingConfigFunc
verif ffiwrapper.Verifier
2021-05-18 12:28:51 +00:00
deadlines map[abi.SectorNumber]time.Time
todo map[abi.SectorNumber]AggregateInput
waiting map[abi.SectorNumber][]chan cid.Cid
2021-03-10 15:16:44 +00:00
notify, stop, stopped chan struct{}
force chan chan *cid.Cid
lk sync.Mutex
}
func NewCommitBatcher(mctx context.Context, maddr address.Address, api CommitBatcherApi, addrSel AddrSel, feeCfg FeeConfig, getConfig GetSealingConfigFunc, verif ffiwrapper.Verifier) *CommitBatcher {
b := &CommitBatcher{
api: api,
maddr: maddr,
mctx: mctx,
addrSel: addrSel,
feeCfg: feeCfg,
getConfig: getConfig,
verif: verif,
2021-05-18 12:28:51 +00:00
deadlines: map[abi.SectorNumber]time.Time{},
todo: map[abi.SectorNumber]AggregateInput{},
waiting: map[abi.SectorNumber][]chan cid.Cid{},
2021-03-10 15:16:44 +00:00
notify: make(chan struct{}, 1),
force: make(chan chan *cid.Cid),
stop: make(chan struct{}),
stopped: make(chan struct{}),
}
go b.run()
return b
}
func (b *CommitBatcher) run() {
var forceRes chan *cid.Cid
var lastMsg *cid.Cid
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
2021-05-18 11:30:47 +00:00
case <-time.After(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
lastMsg, err = b.processBatch(sendAboveMax, sendAboveMin)
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
}
}
}
2021-05-18 11:30:47 +00:00
func (b *CommitBatcher) batchWait(maxWait, slack time.Duration) time.Duration {
now := time.Now()
b.lk.Lock()
defer b.lk.Unlock()
var deadline time.Time
for sn := range b.todo {
2021-05-18 12:28:51 +00:00
sectorDeadline := b.deadlines[sn]
2021-05-18 11:30:47 +00:00
if deadline.IsZero() || (!sectorDeadline.IsZero() && sectorDeadline.Before(deadline)) {
deadline = sectorDeadline
}
}
for sn := range b.waiting {
2021-05-18 12:28:51 +00:00
sectorDeadline := b.deadlines[sn]
2021-05-18 11:30:47 +00:00
if deadline.IsZero() || (!sectorDeadline.IsZero() && sectorDeadline.Before(deadline)) {
deadline = sectorDeadline
}
}
if deadline.IsZero() {
return maxWait
}
deadline = deadline.Add(-slack)
if deadline.Before(now) {
return time.Nanosecond // can't return 0
}
wait := deadline.Sub(now)
if wait > maxWait {
wait = maxWait
}
return wait
}
2021-03-10 15:16:44 +00:00
func (b *CommitBatcher) processBatch(notif, after bool) (*cid.Cid, error) {
b.lk.Lock()
defer b.lk.Unlock()
params := miner5.ProveCommitAggregateParams{
SectorNumbers: bitfield.New(),
}
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-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)
2021-03-10 15:16:44 +00:00
for id, p := range b.todo {
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 {
return nil, xerrors.Errorf("getting miner id: %w", err)
}
2021-05-18 14:51:06 +00:00
params.AggregateProof, err = b.verif.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 {
return nil, xerrors.Errorf("aggregating proofs: %w", err)
}
enc := new(bytes.Buffer)
if err := params.MarshalCBOR(enc); err != nil {
2021-05-18 14:51:06 +00:00
return nil, 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 {
return nil, xerrors.Errorf("couldn't get miner info: %w", err)
}
from, _, err := b.addrSel(b.mctx, mi, api.CommitAddr, b.feeCfg.MaxCommitGasFee, b.feeCfg.MaxCommitGasFee)
if err != nil {
return nil, xerrors.Errorf("no good address found: %w", err)
}
mcid, err := b.api.SendMsg(b.mctx, from, b.maddr, miner.Methods.ProveCommitAggregate, big.Zero(), b.feeCfg.MaxCommitGasFee, enc.Bytes())
if err != nil {
return nil, xerrors.Errorf("sending message failed: %w", err)
}
log.Infow("Sent ProveCommitAggregate message", "cid", mcid, "from", from, "sectors", total)
err = params.SectorNumbers.ForEach(func(us uint64) error {
sn := abi.SectorNumber(us)
for _, ch := range b.waiting[sn] {
ch <- mcid // buffered
}
delete(b.waiting, sn)
delete(b.todo, sn)
2021-05-18 12:28:51 +00:00
delete(b.deadlines, sn)
2021-03-10 15:16:44 +00:00
return nil
})
if err != nil {
return nil, xerrors.Errorf("done sectors foreach: %w", err)
}
return &mcid, nil
}
// register commit, wait for batch message, return message CID
2021-05-18 11:30:47 +00:00
func (b *CommitBatcher) AddCommit(ctx context.Context, s SectorInfo, in AggregateInput) (mcid cid.Cid, err error) {
2021-05-18 14:51:06 +00:00
_, curEpoch, err := b.api.ChainHead(b.mctx)
if err != nil {
log.Errorf("getting chain head: %s", err)
return cid.Undef, nil
}
2021-05-18 11:30:47 +00:00
sn := s.SectorNumber
2021-05-18 14:51:06 +00:00
2021-03-10 15:16:44 +00:00
b.lk.Lock()
2021-05-18 14:51:06 +00:00
b.deadlines[sn] = getSectorDeadline(curEpoch, s)
2021-05-18 11:30:47 +00:00
b.todo[sn] = in
2021-03-10 15:16:44 +00:00
sent := make(chan cid.Cid, 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 {
case c := <-sent:
return c, nil
case <-ctx.Done():
return cid.Undef, ctx.Err()
}
}
func (b *CommitBatcher) Flush(ctx context.Context) (*cid.Cid, error) {
resCh := make(chan *cid.Cid, 1)
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
func getSectorDeadline(curEpoch abi.ChainEpoch, si SectorInfo) time.Time {
2021-05-18 17:47:30 +00:00
deadlineEpoch := si.TicketEpoch + policy.MaxPreCommitRandomnessLookback
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 < deadlineEpoch {
deadlineEpoch = startEpoch
}
}
if deadlineEpoch <= curEpoch {
return time.Now()
}
return time.Now().Add(time.Duration(deadlineEpoch-curEpoch) * time.Duration(build.BlockDelaySecs) * time.Second)
}