2021-05-18 14:54:55 +00:00
|
|
|
package sealing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-04-20 21:34:28 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
2023-01-11 21:21:14 +00:00
|
|
|
verifregtypes "github.com/filecoin-project/go-state-types/builtin/v9/verifreg"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
|
2021-05-18 14:54:55 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-07-12 16:46:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2023-08-29 13:16:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2021-07-12 16:46:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
2021-09-30 14:53:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-06-08 13:43:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2022-08-09 11:30:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/pipeline/sealiface"
|
2021-05-18 14:54:55 +00:00
|
|
|
)
|
|
|
|
|
2021-06-09 15:18:09 +00:00
|
|
|
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_precommit_batcher.go -package=mocks . PreCommitBatcherApi
|
|
|
|
|
2021-05-18 14:54:55 +00:00
|
|
|
type PreCommitBatcherApi interface {
|
2022-06-16 13:50:41 +00:00
|
|
|
MpoolPushMessage(context.Context, *types.Message, *api.MessageSendSpec) (*types.SignedMessage, error)
|
2023-04-10 18:52:39 +00:00
|
|
|
GasEstimateMessageGas(context.Context, *types.Message, *api.MessageSendSpec, types.TipSetKey) (*types.Message, error)
|
2022-06-16 09:12:33 +00:00
|
|
|
StateMinerInfo(context.Context, address.Address, types.TipSetKey) (api.MinerInfo, error)
|
|
|
|
StateMinerAvailableBalance(context.Context, address.Address, types.TipSetKey) (big.Int, error)
|
2022-06-16 11:15:49 +00:00
|
|
|
ChainHead(ctx context.Context) (*types.TipSet, error)
|
2022-06-17 09:20:33 +00:00
|
|
|
StateNetworkVersion(ctx context.Context, tsk types.TipSetKey) (network.Version, error)
|
2023-01-11 21:21:14 +00:00
|
|
|
StateGetAllocationForPendingDeal(ctx context.Context, dealId abi.DealID, tsk types.TipSetKey) (*verifregtypes.Allocation, error)
|
2024-01-25 14:15:55 +00:00
|
|
|
StateGetAllocation(ctx context.Context, clientAddr address.Address, allocationId verifregtypes.AllocationId, tsk types.TipSetKey) (*verifregtypes.Allocation, error)
|
2022-08-09 10:57:20 +00:00
|
|
|
|
|
|
|
// Address selector
|
|
|
|
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
|
|
|
WalletHas(context.Context, address.Address) (bool, error)
|
|
|
|
StateAccountKey(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
|
|
|
StateLookupID(context.Context, address.Address, types.TipSetKey) (address.Address, error)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:21:10 +00:00
|
|
|
type preCommitEntry struct {
|
|
|
|
deposit abi.TokenAmount
|
2022-04-20 21:34:28 +00:00
|
|
|
pci *miner.SectorPreCommitInfo
|
2021-05-18 15:21:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:54:55 +00:00
|
|
|
type PreCommitBatcher struct {
|
|
|
|
api PreCommitBatcherApi
|
|
|
|
maddr address.Address
|
|
|
|
mctx context.Context
|
2022-08-09 10:57:20 +00:00
|
|
|
addrSel AddressSelector
|
2021-06-08 13:43:43 +00:00
|
|
|
feeCfg config.MinerFeeConfig
|
2022-08-09 11:30:34 +00:00
|
|
|
getConfig dtypes.GetSealingConfigFunc
|
2021-05-18 14:54:55 +00:00
|
|
|
|
2021-06-07 23:42:14 +00:00
|
|
|
cutoffs map[abi.SectorNumber]time.Time
|
|
|
|
todo map[abi.SectorNumber]*preCommitEntry
|
2022-06-14 18:31:17 +00:00
|
|
|
waiting map[abi.SectorNumber][]chan sealiface.PreCommitBatchRes
|
2021-05-18 14:54:55 +00:00
|
|
|
|
|
|
|
notify, stop, stopped chan struct{}
|
2022-06-14 18:31:17 +00:00
|
|
|
force chan chan []sealiface.PreCommitBatchRes
|
2021-05-18 14:54:55 +00:00
|
|
|
lk sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-08-09 11:30:34 +00:00
|
|
|
func NewPreCommitBatcher(mctx context.Context, maddr address.Address, api PreCommitBatcherApi, addrSel AddressSelector, feeCfg config.MinerFeeConfig, getConfig dtypes.GetSealingConfigFunc) *PreCommitBatcher {
|
2021-05-18 14:54:55 +00:00
|
|
|
b := &PreCommitBatcher{
|
|
|
|
api: api,
|
|
|
|
maddr: maddr,
|
|
|
|
mctx: mctx,
|
|
|
|
addrSel: addrSel,
|
|
|
|
feeCfg: feeCfg,
|
|
|
|
getConfig: getConfig,
|
|
|
|
|
2021-06-07 23:42:14 +00:00
|
|
|
cutoffs: map[abi.SectorNumber]time.Time{},
|
|
|
|
todo: map[abi.SectorNumber]*preCommitEntry{},
|
2022-06-14 18:31:17 +00:00
|
|
|
waiting: map[abi.SectorNumber][]chan sealiface.PreCommitBatchRes{},
|
2021-05-18 14:54:55 +00:00
|
|
|
|
|
|
|
notify: make(chan struct{}, 1),
|
2022-06-14 18:31:17 +00:00
|
|
|
force: make(chan chan []sealiface.PreCommitBatchRes),
|
2021-05-18 14:54:55 +00:00
|
|
|
stop: make(chan struct{}),
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
go b.run()
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *PreCommitBatcher) run() {
|
2022-06-14 18:31:17 +00:00
|
|
|
var forceRes chan []sealiface.PreCommitBatchRes
|
|
|
|
var lastRes []sealiface.PreCommitBatchRes
|
2021-05-18 14:54:55 +00:00
|
|
|
|
|
|
|
cfg, err := b.getConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-07-02 07:53:21 +00:00
|
|
|
timer := time.NewTimer(b.batchWait(cfg.PreCommitBatchWait, cfg.PreCommitBatchSlack))
|
2021-05-18 14:54:55 +00:00
|
|
|
for {
|
|
|
|
if forceRes != nil {
|
2021-06-01 12:35:30 +00:00
|
|
|
forceRes <- lastRes
|
2021-05-18 14:54:55 +00:00
|
|
|
forceRes = nil
|
|
|
|
}
|
2021-06-01 12:35:30 +00:00
|
|
|
lastRes = nil
|
2021-05-18 14:54:55 +00:00
|
|
|
|
2021-06-23 16:30:32 +00:00
|
|
|
var sendAboveMax bool
|
2021-05-18 14:54:55 +00:00
|
|
|
select {
|
|
|
|
case <-b.stop:
|
|
|
|
close(b.stopped)
|
|
|
|
return
|
|
|
|
case <-b.notify:
|
|
|
|
sendAboveMax = true
|
2021-06-30 08:56:40 +00:00
|
|
|
case <-timer.C:
|
2021-06-23 16:30:32 +00:00
|
|
|
// do nothing
|
2021-05-18 14:54:55 +00:00
|
|
|
case fr := <-b.force: // user triggered
|
|
|
|
forceRes = fr
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-06-23 16:30:32 +00:00
|
|
|
lastRes, err = b.maybeStartBatch(sendAboveMax)
|
2021-05-18 14:54:55 +00:00
|
|
|
if err != nil {
|
2021-05-18 15:37:52 +00:00
|
|
|
log.Warnw("PreCommitBatcher processBatch error", "error", err)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
2021-06-30 08:56:40 +00:00
|
|
|
|
|
|
|
if !timer.Stop() {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 07:53:21 +00:00
|
|
|
timer.Reset(b.batchWait(cfg.PreCommitBatchWait, cfg.PreCommitBatchSlack))
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 08:56:40 +00:00
|
|
|
func (b *PreCommitBatcher) batchWait(maxWait, slack time.Duration) time.Duration {
|
2021-05-18 14:54:55 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
b.lk.Lock()
|
|
|
|
defer b.lk.Unlock()
|
|
|
|
|
2021-05-25 14:47:42 +00:00
|
|
|
if len(b.todo) == 0 {
|
2021-06-30 08:56:40 +00:00
|
|
|
return maxWait
|
2021-05-25 14:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 23:42:14 +00:00
|
|
|
var cutoff time.Time
|
2021-05-18 14:54:55 +00:00
|
|
|
for sn := range b.todo {
|
2021-06-07 23:42:14 +00:00
|
|
|
sectorCutoff := b.cutoffs[sn]
|
|
|
|
if cutoff.IsZero() || (!sectorCutoff.IsZero() && sectorCutoff.Before(cutoff)) {
|
|
|
|
cutoff = sectorCutoff
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for sn := range b.waiting {
|
2021-06-07 23:42:14 +00:00
|
|
|
sectorCutoff := b.cutoffs[sn]
|
|
|
|
if cutoff.IsZero() || (!sectorCutoff.IsZero() && sectorCutoff.Before(cutoff)) {
|
|
|
|
cutoff = sectorCutoff
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 23:42:14 +00:00
|
|
|
if cutoff.IsZero() {
|
2021-06-30 08:56:40 +00:00
|
|
|
return maxWait
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 23:42:14 +00:00
|
|
|
cutoff = cutoff.Add(-slack)
|
|
|
|
if cutoff.Before(now) {
|
2021-06-30 08:56:40 +00:00
|
|
|
return time.Nanosecond // can't return 0
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 23:42:14 +00:00
|
|
|
wait := cutoff.Sub(now)
|
2021-05-18 14:54:55 +00:00
|
|
|
if wait > maxWait {
|
|
|
|
wait = maxWait
|
|
|
|
}
|
|
|
|
|
2021-06-30 08:56:40 +00:00
|
|
|
return wait
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
func (b *PreCommitBatcher) maybeStartBatch(notif bool) ([]sealiface.PreCommitBatchRes, error) {
|
2021-05-18 14:54:55 +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)
|
|
|
|
}
|
|
|
|
|
2022-06-16 11:15:49 +00:00
|
|
|
ts, err := b.api.ChainHead(b.mctx)
|
2021-10-01 14:23:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-30 14:53:12 +00:00
|
|
|
|
2023-08-08 11:04:21 +00:00
|
|
|
curBasefeeLow := false
|
|
|
|
if !cfg.BatchPreCommitAboveBaseFee.Equals(big.Zero()) && ts.MinTicketBlock().ParentBaseFee.LessThan(cfg.BatchPreCommitAboveBaseFee) {
|
|
|
|
curBasefeeLow = true
|
2021-10-01 14:23:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:04:21 +00:00
|
|
|
// if this wasn't an user-forced batch, and we're not at/above the max batch size,
|
|
|
|
// and we're not above the basefee threshold, don't batch yet
|
|
|
|
if notif && total < cfg.MaxPreCommitBatch && !curBasefeeLow {
|
|
|
|
return nil, nil
|
2021-09-30 14:53:12 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:04:21 +00:00
|
|
|
nv, err := b.api.StateNetworkVersion(b.mctx, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("couldn't get network version: %w", err)
|
2021-09-30 14:53:12 +00:00
|
|
|
}
|
2023-08-08 11:04:21 +00:00
|
|
|
|
|
|
|
// For precommits the only method to precommit sectors after nv21(22?) is to use the new precommit_batch2 method
|
|
|
|
// So we always batch
|
|
|
|
res, err := b.processBatch(cfg, ts.Key(), ts.MinTicketBlock().ParentBaseFee, nv)
|
2021-06-01 12:35:30 +00:00
|
|
|
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)
|
2021-06-07 23:42:14 +00:00
|
|
|
delete(b.cutoffs, sn)
|
2021-06-01 12:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:52:39 +00:00
|
|
|
func (b *PreCommitBatcher) processPreCommitBatch(cfg sealiface.Config, bf abi.TokenAmount, entries []*preCommitEntry, nv network.Version) ([]sealiface.PreCommitBatchRes, error) {
|
2023-08-08 10:40:08 +00:00
|
|
|
params := miner.PreCommitSectorBatchParams2{}
|
2021-05-18 15:21:10 +00:00
|
|
|
deposit := big.Zero()
|
2022-06-14 18:31:17 +00:00
|
|
|
var res sealiface.PreCommitBatchRes
|
2021-05-18 15:21:10 +00:00
|
|
|
|
2023-04-10 18:52:39 +00:00
|
|
|
for _, p := range entries {
|
2021-06-01 12:35:30 +00:00
|
|
|
res.Sectors = append(res.Sectors, p.pci.SectorNumber)
|
2023-08-08 10:40:08 +00:00
|
|
|
params.Sectors = append(params.Sectors, *p.pci)
|
2021-05-18 15:21:10 +00:00
|
|
|
deposit = big.Add(deposit, p.deposit)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
2021-07-12 16:46:05 +00:00
|
|
|
|
2021-05-18 14:54:55 +00:00
|
|
|
enc := new(bytes.Buffer)
|
|
|
|
if err := params.MarshalCBOR(enc); err != nil {
|
2023-04-10 20:24:13 +00:00
|
|
|
res.Error = err.Error()
|
2022-06-14 18:31:17 +00:00
|
|
|
return []sealiface.PreCommitBatchRes{res}, xerrors.Errorf("couldn't serialize PreCommitSectorBatchParams: %w", err)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:12:33 +00:00
|
|
|
mi, err := b.api.StateMinerInfo(b.mctx, b.maddr, types.EmptyTSK)
|
2021-05-18 14:54:55 +00:00
|
|
|
if err != nil {
|
2023-04-10 20:24:13 +00:00
|
|
|
res.Error = err.Error()
|
2022-06-14 18:31:17 +00:00
|
|
|
return []sealiface.PreCommitBatchRes{res}, xerrors.Errorf("couldn't get miner info: %w", err)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 13:43:43 +00:00
|
|
|
maxFee := b.feeCfg.MaxPreCommitBatchGasFee.FeeForSectors(len(params.Sectors))
|
2021-10-01 14:23:21 +00:00
|
|
|
|
|
|
|
aggFeeRaw, err := policy.AggregatePreCommitNetworkFee(nv, len(params.Sectors), bf)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting aggregate precommit network fee: %s", err)
|
2023-04-10 20:24:13 +00:00
|
|
|
res.Error = err.Error()
|
2022-06-14 18:31:17 +00:00
|
|
|
return []sealiface.PreCommitBatchRes{res}, xerrors.Errorf("getting aggregate precommit network fee: %s", err)
|
2021-10-01 14:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aggFee := big.Div(big.Mul(aggFeeRaw, aggFeeNum), aggFeeDen)
|
|
|
|
|
|
|
|
needFunds := big.Add(deposit, aggFee)
|
|
|
|
needFunds, err = collateralSendAmount(b.mctx, b.api, b.maddr, cfg, needFunds)
|
|
|
|
if err != nil {
|
2022-06-14 18:31:17 +00:00
|
|
|
return []sealiface.PreCommitBatchRes{res}, err
|
2021-10-01 14:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goodFunds := big.Add(maxFee, needFunds)
|
2021-05-18 15:21:10 +00:00
|
|
|
|
2022-08-09 10:57:20 +00:00
|
|
|
from, _, err := b.addrSel.AddressFor(b.mctx, b.api, mi, api.PreCommitAddr, goodFunds, deposit)
|
2021-05-18 14:54:55 +00:00
|
|
|
if err != nil {
|
2022-06-14 18:31:17 +00:00
|
|
|
return []sealiface.PreCommitBatchRes{res}, xerrors.Errorf("no good address found: %w", err)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 10:40:08 +00:00
|
|
|
_, err = simulateMsgGas(b.mctx, b.api, from, b.maddr, builtin.MethodsMiner.PreCommitSectorBatch2, needFunds, maxFee, enc.Bytes())
|
2023-04-10 18:52:39 +00:00
|
|
|
|
|
|
|
if err != nil && (!api.ErrorIsIn(err, []error{&api.ErrOutOfGas{}}) || len(entries) == 1) {
|
|
|
|
res.Error = err.Error()
|
|
|
|
return []sealiface.PreCommitBatchRes{res}, xerrors.Errorf("simulating PreCommitBatch message failed: %w", err)
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 16:15:51 +00:00
|
|
|
// If we're out of gas, split the batch in half and evaluate again
|
2023-04-10 18:52:39 +00:00
|
|
|
if api.ErrorIsIn(err, []error{&api.ErrOutOfGas{}}) {
|
2023-04-13 01:45:43 +00:00
|
|
|
log.Warnf("PreCommitBatch out of gas, splitting batch in half and trying again")
|
2023-04-10 18:52:39 +00:00
|
|
|
mid := len(entries) / 2
|
2023-04-10 20:24:13 +00:00
|
|
|
ret0, _ := b.processPreCommitBatch(cfg, bf, entries[:mid], nv)
|
|
|
|
ret1, _ := b.processPreCommitBatch(cfg, bf, entries[mid:], nv)
|
2021-05-18 14:54:55 +00:00
|
|
|
|
2023-04-13 01:45:43 +00:00
|
|
|
return append(ret0, ret1...), nil
|
2023-04-10 18:52:39 +00:00
|
|
|
}
|
2021-05-18 14:54:55 +00:00
|
|
|
|
2023-04-10 18:52:39 +00:00
|
|
|
// If state call succeeds, we can send the message for real
|
2023-08-08 10:40:08 +00:00
|
|
|
mcid, err := sendMsg(b.mctx, b.api, from, b.maddr, builtin.MethodsMiner.PreCommitSectorBatch2, needFunds, maxFee, enc.Bytes())
|
2023-04-10 18:52:39 +00:00
|
|
|
if err != nil {
|
|
|
|
res.Error = err.Error()
|
|
|
|
return []sealiface.PreCommitBatchRes{res}, xerrors.Errorf("pushing message to mpool: %w", err)
|
|
|
|
}
|
|
|
|
res.Msg = &mcid
|
2022-06-14 18:31:17 +00:00
|
|
|
return []sealiface.PreCommitBatchRes{res}, nil
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 18:52:39 +00:00
|
|
|
func (b *PreCommitBatcher) processBatch(cfg sealiface.Config, tsk types.TipSetKey, bf abi.TokenAmount, nv network.Version) ([]sealiface.PreCommitBatchRes, error) {
|
|
|
|
var pcEntries []*preCommitEntry
|
|
|
|
for _, p := range b.todo {
|
|
|
|
pcEntries = append(pcEntries, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.processPreCommitBatch(cfg, bf, pcEntries, nv)
|
|
|
|
}
|
|
|
|
|
2021-05-18 14:54:55 +00:00
|
|
|
// register PreCommit, wait for batch message, return message CID
|
2022-06-14 18:31:17 +00:00
|
|
|
func (b *PreCommitBatcher) AddPreCommit(ctx context.Context, s SectorInfo, deposit abi.TokenAmount, in *miner.SectorPreCommitInfo) (res sealiface.PreCommitBatchRes, err error) {
|
2022-06-16 11:15:49 +00:00
|
|
|
ts, err := b.api.ChainHead(b.mctx)
|
2021-05-18 14:54:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getting chain head: %s", err)
|
2022-06-14 18:31:17 +00:00
|
|
|
return sealiface.PreCommitBatchRes{}, err
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:21:14 +00:00
|
|
|
dealStartCutoff := getDealStartCutoff(s)
|
|
|
|
if dealStartCutoff <= ts.Height() {
|
|
|
|
return sealiface.PreCommitBatchRes{}, xerrors.Errorf("cutoff has already passed (cutoff %d <= curEpoch %d)", dealStartCutoff, ts.Height())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocation cutoff is a soft deadline, so don't fail if we've passed it.
|
|
|
|
allocationCutoff := b.getAllocationCutoff(s)
|
|
|
|
|
|
|
|
var cutoffEpoch abi.ChainEpoch
|
|
|
|
if dealStartCutoff < allocationCutoff {
|
|
|
|
cutoffEpoch = dealStartCutoff
|
|
|
|
} else {
|
|
|
|
cutoffEpoch = allocationCutoff
|
2022-04-13 03:41:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:54:55 +00:00
|
|
|
sn := s.SectorNumber
|
|
|
|
|
|
|
|
b.lk.Lock()
|
2023-01-11 21:21:14 +00:00
|
|
|
b.cutoffs[sn] = time.Now().Add(time.Duration(cutoffEpoch-ts.Height()) * time.Duration(build.BlockDelaySecs) * time.Second)
|
2021-05-18 15:21:10 +00:00
|
|
|
b.todo[sn] = &preCommitEntry{
|
|
|
|
deposit: deposit,
|
|
|
|
pci: in,
|
|
|
|
}
|
2021-05-18 14:54:55 +00:00
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
sent := make(chan sealiface.PreCommitBatchRes, 1)
|
2021-05-18 14:54:55 +00:00
|
|
|
b.waiting[sn] = append(b.waiting[sn], sent)
|
|
|
|
|
|
|
|
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():
|
2022-06-14 18:31:17 +00:00
|
|
|
return sealiface.PreCommitBatchRes{}, ctx.Err()
|
2021-05-18 14:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 18:31:17 +00:00
|
|
|
func (b *PreCommitBatcher) Flush(ctx context.Context) ([]sealiface.PreCommitBatchRes, error) {
|
|
|
|
resCh := make(chan []sealiface.PreCommitBatchRes, 1)
|
2021-05-18 14:54:55 +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 *PreCommitBatcher) 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),
|
2021-05-18 15:21:10 +00:00
|
|
|
Number: s.pci.SectorNumber,
|
2021-05-18 14:54:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 *PreCommitBatcher) Stop(ctx context.Context) error {
|
|
|
|
close(b.stop)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-b.stopped:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 20:46:35 +00:00
|
|
|
|
2023-01-11 21:21:14 +00:00
|
|
|
func getDealStartCutoff(si SectorInfo) abi.ChainEpoch {
|
2021-06-08 20:46:35 +00:00
|
|
|
cutoffEpoch := si.TicketEpoch + policy.MaxPreCommitRandomnessLookback
|
|
|
|
for _, p := range si.Pieces {
|
2024-01-25 14:15:55 +00:00
|
|
|
if !p.HasDealInfo() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
startEpoch, err := p.StartEpoch()
|
|
|
|
if err != nil {
|
|
|
|
// almost definitely can't happen, but if it does there's less harm in
|
|
|
|
// just logging the error and moving on
|
|
|
|
log.Errorw("failed to get deal start epoch", "error", err)
|
2021-06-08 20:46:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if startEpoch < cutoffEpoch {
|
|
|
|
cutoffEpoch = startEpoch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:21:14 +00:00
|
|
|
return cutoffEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *PreCommitBatcher) getAllocationCutoff(si SectorInfo) abi.ChainEpoch {
|
|
|
|
cutoff := si.TicketEpoch + policy.MaxPreCommitRandomnessLookback
|
|
|
|
for _, p := range si.Pieces {
|
2024-01-25 14:15:55 +00:00
|
|
|
if !p.HasDealInfo() {
|
2023-01-11 21:21:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-06-08 20:46:35 +00:00
|
|
|
|
2024-01-25 14:15:55 +00:00
|
|
|
alloc, err := p.GetAllocation(b.mctx, b.api, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorw("failed to get deal allocation", "error", err)
|
|
|
|
}
|
2023-01-11 21:21:14 +00:00
|
|
|
// alloc is nil if this is not a verified deal in nv17 or later
|
|
|
|
if alloc == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-25 14:15:55 +00:00
|
|
|
|
2023-01-11 21:21:14 +00:00
|
|
|
if alloc.Expiration < cutoff {
|
|
|
|
cutoff = alloc.Expiration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cutoff
|
2021-06-08 20:46:35 +00:00
|
|
|
}
|