2021-05-19 00:01:30 +00:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-10 02:35:22 +00:00
|
|
|
"time"
|
2021-05-19 00:01:30 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2021-06-10 02:40:00 +00:00
|
|
|
"github.com/filecoin-project/go-bitfield"
|
2021-05-19 00:01:30 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2021-06-10 02:40:00 +00:00
|
|
|
|
|
|
|
miner5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/miner"
|
|
|
|
power5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/power"
|
|
|
|
|
2021-05-19 00:01:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2021-06-10 04:28:05 +00:00
|
|
|
// packProveCommits packs all prove-commits for all "ready to be proven" sectors until it fills the
|
2021-06-08 00:45:53 +00:00
|
|
|
// block or runs out.
|
2021-06-09 20:47:38 +00:00
|
|
|
func (ss *simulationState) packProveCommits(ctx context.Context, cb packFunc) (_err error) {
|
2021-06-08 00:45:53 +00:00
|
|
|
// Roll the commitQueue forward.
|
2021-05-19 00:01:30 +00:00
|
|
|
ss.commitQueue.advanceEpoch(ss.nextEpoch())
|
|
|
|
|
2021-06-10 02:35:22 +00:00
|
|
|
start := time.Now()
|
2021-06-10 05:13:38 +00:00
|
|
|
var failed, done, unbatched, count int
|
2021-05-19 00:01:30 +00:00
|
|
|
defer func() {
|
|
|
|
if _err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
remaining := ss.commitQueue.ready()
|
|
|
|
log.Debugw("packed prove commits",
|
|
|
|
"remaining", remaining,
|
|
|
|
"done", done,
|
|
|
|
"failed", failed,
|
|
|
|
"unbatched", unbatched,
|
|
|
|
"miners-processed", count,
|
2021-06-10 02:35:22 +00:00
|
|
|
"duration", time.Since(start),
|
2021-05-19 00:01:30 +00:00
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
addr, pending, ok := ss.commitQueue.nextMiner()
|
|
|
|
if !ok {
|
2021-06-09 20:47:38 +00:00
|
|
|
return nil
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 20:47:38 +00:00
|
|
|
res, err := ss.packProveCommitsMiner(ctx, cb, addr, pending)
|
2021-05-19 00:01:30 +00:00
|
|
|
if err != nil {
|
2021-06-09 20:47:38 +00:00
|
|
|
return err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
failed += res.failed
|
|
|
|
done += res.done
|
|
|
|
unbatched += res.unbatched
|
|
|
|
count++
|
2021-06-09 20:47:38 +00:00
|
|
|
if res.full {
|
|
|
|
return nil
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type proveCommitResult struct {
|
|
|
|
done, failed, unbatched int
|
2021-06-09 20:47:38 +00:00
|
|
|
full bool
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// packProveCommitsMiner enqueues a prove commits from the given miner until it runs out of
|
|
|
|
// available prove-commits, batching as much as possible.
|
|
|
|
//
|
|
|
|
// This function will fund as necessary from the "burnt funds actor" (look, it's convenient).
|
2021-05-19 00:01:30 +00:00
|
|
|
func (ss *simulationState) packProveCommitsMiner(
|
|
|
|
ctx context.Context, cb packFunc, minerAddr address.Address,
|
|
|
|
pending minerPendingCommits,
|
2021-06-09 20:47:38 +00:00
|
|
|
) (res proveCommitResult, _err error) {
|
2021-05-19 00:01:30 +00:00
|
|
|
info, err := ss.getMinerInfo(ctx, minerAddr)
|
|
|
|
if err != nil {
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 18:22:11 +00:00
|
|
|
nv := ss.StateManager.GetNtwkVersion(ctx, ss.nextEpoch())
|
2021-05-19 00:01:30 +00:00
|
|
|
for sealType, snos := range pending {
|
|
|
|
if nv >= network.Version13 {
|
|
|
|
for len(snos) > minProveCommitBatchSize {
|
|
|
|
batchSize := maxProveCommitBatchSize
|
|
|
|
if len(snos) < batchSize {
|
|
|
|
batchSize = len(snos)
|
|
|
|
}
|
|
|
|
batch := snos[:batchSize]
|
|
|
|
|
|
|
|
proof, err := mockAggregateSealProof(sealType, minerAddr, batchSize)
|
|
|
|
if err != nil {
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params := miner5.ProveCommitAggregateParams{
|
|
|
|
SectorNumbers: bitfield.New(),
|
|
|
|
AggregateProof: proof,
|
|
|
|
}
|
|
|
|
for _, sno := range batch {
|
|
|
|
params.SectorNumbers.Set(uint64(sno))
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := actors.SerializeParams(¶ms)
|
|
|
|
if err != nil {
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 20:47:38 +00:00
|
|
|
if _, err := sendAndFund(cb, &types.Message{
|
2021-05-19 00:01:30 +00:00
|
|
|
From: info.Worker,
|
|
|
|
To: minerAddr,
|
|
|
|
Value: abi.NewTokenAmount(0),
|
|
|
|
Method: miner.Methods.ProveCommitAggregate,
|
|
|
|
Params: enc,
|
2021-06-09 20:47:38 +00:00
|
|
|
}); err == nil {
|
|
|
|
res.done += len(batch)
|
|
|
|
} else if err == ErrOutOfGas {
|
|
|
|
res.full = true
|
|
|
|
return res, nil
|
|
|
|
} else if aerr, ok := err.(aerrors.ActorError); !ok || aerr.IsFatal() {
|
2021-05-19 00:01:30 +00:00
|
|
|
// If we get a random error, or a fatal actor error, bail.
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, err
|
|
|
|
} else if aerr.RetCode() == exitcode.ErrNotFound || aerr.RetCode() == exitcode.ErrIllegalArgument {
|
|
|
|
// If we get a "not-found" or illegal argument error, try to
|
|
|
|
// remove any missing prove-commits and continue. This can
|
|
|
|
// happen either because:
|
2021-06-08 17:27:20 +00:00
|
|
|
//
|
|
|
|
// 1. The pre-commit failed on execution (but not when
|
|
|
|
// packing). This shouldn't happen, but we might as well
|
|
|
|
// gracefully handle it.
|
|
|
|
// 2. The pre-commit has expired. We'd have to be really
|
|
|
|
// backloged to hit this case, but we might as well handle
|
|
|
|
// it.
|
2021-06-09 20:47:38 +00:00
|
|
|
// First, split into "good" and "missing"
|
|
|
|
good, err := ss.filterProveCommits(ctx, minerAddr, batch)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorw("failed to filter prove commits", "miner", minerAddr, "error", err)
|
|
|
|
// fail with the original error.
|
|
|
|
return res, aerr
|
|
|
|
}
|
|
|
|
removed := len(batch) - len(good)
|
|
|
|
if removed == 0 {
|
|
|
|
log.Errorw("failed to prove-commit for unknown reasons",
|
|
|
|
"error", aerr,
|
|
|
|
"miner", minerAddr,
|
|
|
|
"sectors", batch,
|
|
|
|
"epoch", ss.nextEpoch(),
|
|
|
|
)
|
|
|
|
res.failed += len(batch)
|
|
|
|
} else if len(good) == 0 {
|
|
|
|
log.Errorw("failed to prove commit missing pre-commits",
|
|
|
|
"error", aerr,
|
|
|
|
"miner", minerAddr,
|
|
|
|
"discarded", removed,
|
|
|
|
"epoch", ss.nextEpoch(),
|
|
|
|
)
|
|
|
|
res.failed += len(batch)
|
|
|
|
} else {
|
|
|
|
// update the pending sector numbers in-place to remove the expired ones.
|
|
|
|
snos = snos[removed:]
|
|
|
|
copy(snos, good)
|
|
|
|
pending.finish(sealType, removed)
|
2021-06-08 02:21:18 +00:00
|
|
|
|
2021-06-09 20:47:38 +00:00
|
|
|
log.Errorw("failed to prove commit expired/missing pre-commits",
|
|
|
|
"error", aerr,
|
|
|
|
"miner", minerAddr,
|
|
|
|
"discarded", removed,
|
|
|
|
"kept", len(good),
|
|
|
|
"epoch", ss.nextEpoch(),
|
|
|
|
)
|
|
|
|
res.failed += removed
|
2021-06-08 02:21:18 +00:00
|
|
|
|
2021-06-09 20:47:38 +00:00
|
|
|
// Then try again.
|
|
|
|
continue
|
2021-06-08 02:21:18 +00:00
|
|
|
}
|
2021-06-09 20:47:38 +00:00
|
|
|
log.Errorw("failed to prove commit missing sector(s)",
|
2021-05-19 00:01:30 +00:00
|
|
|
"error", err,
|
|
|
|
"miner", minerAddr,
|
|
|
|
"sectors", batch,
|
|
|
|
"epoch", ss.nextEpoch(),
|
|
|
|
)
|
2021-06-08 02:21:18 +00:00
|
|
|
res.failed += len(batch)
|
2021-05-19 00:01:30 +00:00
|
|
|
} else {
|
2021-06-09 20:47:38 +00:00
|
|
|
log.Errorw("failed to prove commit sector(s)",
|
|
|
|
"error", err,
|
|
|
|
"miner", minerAddr,
|
|
|
|
"sectors", batch,
|
|
|
|
"epoch", ss.nextEpoch(),
|
|
|
|
)
|
|
|
|
res.failed += len(batch)
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
2021-06-09 20:47:38 +00:00
|
|
|
pending.finish(sealType, len(batch))
|
|
|
|
snos = snos[len(batch):]
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for len(snos) > 0 && res.unbatched < power5.MaxMinerProveCommitsPerEpoch {
|
|
|
|
sno := snos[0]
|
|
|
|
snos = snos[1:]
|
|
|
|
|
|
|
|
proof, err := mockSealProof(sealType, minerAddr)
|
|
|
|
if err != nil {
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
params := miner.ProveCommitSectorParams{
|
|
|
|
SectorNumber: sno,
|
|
|
|
Proof: proof,
|
|
|
|
}
|
|
|
|
enc, err := actors.SerializeParams(¶ms)
|
|
|
|
if err != nil {
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, err
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
2021-06-09 20:47:38 +00:00
|
|
|
if _, err := sendAndFund(cb, &types.Message{
|
2021-05-19 00:01:30 +00:00
|
|
|
From: info.Worker,
|
|
|
|
To: minerAddr,
|
|
|
|
Value: abi.NewTokenAmount(0),
|
|
|
|
Method: miner.Methods.ProveCommitSector,
|
|
|
|
Params: enc,
|
2021-06-09 20:47:38 +00:00
|
|
|
}); err == nil {
|
|
|
|
res.unbatched++
|
|
|
|
res.done++
|
|
|
|
} else if err == ErrOutOfGas {
|
|
|
|
res.full = true
|
|
|
|
return res, nil
|
|
|
|
} else if aerr, ok := err.(aerrors.ActorError); !ok || aerr.IsFatal() {
|
|
|
|
return res, err
|
|
|
|
} else {
|
2021-05-19 00:01:30 +00:00
|
|
|
log.Errorw("failed to prove commit sector(s)",
|
|
|
|
"error", err,
|
|
|
|
"miner", minerAddr,
|
|
|
|
"sectors", []abi.SectorNumber{sno},
|
|
|
|
"epoch", ss.nextEpoch(),
|
|
|
|
)
|
|
|
|
res.failed++
|
|
|
|
}
|
|
|
|
// mark it as "finished" regardless so we skip it.
|
|
|
|
pending.finish(sealType, 1)
|
|
|
|
}
|
|
|
|
// if we get here, we can't pre-commit anything more.
|
|
|
|
}
|
2021-06-09 20:47:38 +00:00
|
|
|
return res, nil
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 00:45:53 +00:00
|
|
|
// loadProveCommitsMiner enqueue all pending prove-commits for the given miner. This is called on
|
|
|
|
// load to populate the commitQueue and should not need to be called later.
|
|
|
|
//
|
|
|
|
// It will drop any pre-commits that have already expired.
|
2021-05-19 00:01:30 +00:00
|
|
|
func (ss *simulationState) loadProveCommitsMiner(ctx context.Context, addr address.Address, minerState miner.State) error {
|
|
|
|
// Find all pending prove commits and group by proof type. Really, there should never
|
|
|
|
// (except during upgrades be more than one type.
|
|
|
|
nextEpoch := ss.nextEpoch()
|
2021-06-08 18:22:11 +00:00
|
|
|
nv := ss.StateManager.GetNtwkVersion(ctx, nextEpoch)
|
2021-05-19 00:01:30 +00:00
|
|
|
av := actors.VersionForNetwork(nv)
|
|
|
|
|
2021-06-08 02:21:18 +00:00
|
|
|
var total, dropped int
|
|
|
|
err := minerState.ForEachPrecommittedSector(func(info miner.SectorPreCommitOnChainInfo) error {
|
|
|
|
total++
|
2021-05-19 00:01:30 +00:00
|
|
|
msd := policy.GetMaxProveCommitDuration(av, info.Info.SealProof)
|
|
|
|
if nextEpoch > info.PreCommitEpoch+msd {
|
2021-06-08 02:21:18 +00:00
|
|
|
dropped++
|
2021-05-19 00:01:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ss.commitQueue.enqueueProveCommit(addr, info.PreCommitEpoch, info.Info)
|
|
|
|
})
|
2021-06-08 02:21:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if dropped > 0 {
|
|
|
|
log.Warnw("dropped expired pre-commits on load",
|
|
|
|
"miner", addr,
|
|
|
|
"total", total,
|
|
|
|
"expired", dropped,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:27:20 +00:00
|
|
|
// filterProveCommits filters out expired and/or missing pre-commits.
|
|
|
|
func (ss *simulationState) filterProveCommits(ctx context.Context, minerAddr address.Address, snos []abi.SectorNumber) ([]abi.SectorNumber, error) {
|
2021-06-08 02:21:18 +00:00
|
|
|
_, minerState, err := ss.getMinerState(ctx, minerAddr)
|
|
|
|
if err != nil {
|
2021-06-08 17:27:20 +00:00
|
|
|
return nil, err
|
2021-06-08 02:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextEpoch := ss.nextEpoch()
|
2021-06-08 18:22:11 +00:00
|
|
|
nv := ss.StateManager.GetNtwkVersion(ctx, nextEpoch)
|
2021-06-08 02:21:18 +00:00
|
|
|
av := actors.VersionForNetwork(nv)
|
|
|
|
|
2021-06-08 17:27:20 +00:00
|
|
|
good := make([]abi.SectorNumber, 0, len(snos))
|
2021-06-08 02:21:18 +00:00
|
|
|
for _, sno := range snos {
|
|
|
|
info, err := minerState.GetPrecommittedSector(sno)
|
|
|
|
if err != nil {
|
2021-06-08 17:27:20 +00:00
|
|
|
return nil, err
|
2021-06-08 02:21:18 +00:00
|
|
|
}
|
|
|
|
if info == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msd := policy.GetMaxProveCommitDuration(av, info.Info.SealProof)
|
|
|
|
if nextEpoch > info.PreCommitEpoch+msd {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
good = append(good, sno)
|
|
|
|
}
|
2021-06-08 17:27:20 +00:00
|
|
|
return good, nil
|
2021-05-19 00:01:30 +00:00
|
|
|
}
|