81ba6ab6f0
* feat: lp mig - first few steps * lp mig: default tasks * code comments * docs * lp-mig-progress * shared * comments and todos * fix: curio: rename lotus-provider to curio (#11645) * rename provider to curio * install gotext * fix lint errors, mod tidy * fix typo * fix API_INFO and add gotext to circleCI * add back gotext * add gotext after remerge * lp: channels doc * finish easy-migration TODOs * out generate * merging and more renames * avoid make-all * minor doc stuff * cu: make gen * make gen fix * make gen * tryfix * go mod tidy * minor ez migration fixes * ez setup - ui cleanups * better error message * guided setup colors * better path to saveconfigtolayer * loadconfigwithupgrades fix * readMiner oops * guided - homedir * err if miner is running * prompt error should exit * process already running, miner_id sectors in migration * dont prompt for language a second time * check miner stopped * unlock repo * render and sql oops * curio easyMig - some fixes * easyMigration runs successfully * lint * review fixes * fix backup path * fixes1 * fixes2 * fixes 3 --------- Co-authored-by: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Co-authored-by: LexLuthr <lexluthr@protocol.ai>
128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
package seal
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/go-bitfield"
|
|
rlepluslazy "github.com/filecoin-project/go-bitfield/rle"
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
|
)
|
|
|
|
type AllocAPI interface {
|
|
StateMinerAllocated(context.Context, address.Address, types.TipSetKey) (*bitfield.BitField, error)
|
|
}
|
|
|
|
func AllocateSectorNumbers(ctx context.Context, a AllocAPI, db *harmonydb.DB, maddr address.Address, count int, txcb ...func(*harmonydb.Tx, []abi.SectorNumber) (bool, error)) ([]abi.SectorNumber, error) {
|
|
chainAlloc, err := a.StateMinerAllocated(ctx, maddr, types.EmptyTSK)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("getting on-chain allocated sector numbers: %w", err)
|
|
}
|
|
|
|
mid, err := address.IDFromAddress(maddr)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("getting miner id: %w", err)
|
|
}
|
|
|
|
var res []abi.SectorNumber
|
|
|
|
comm, err := db.BeginTransaction(ctx, func(tx *harmonydb.Tx) (commit bool, err error) {
|
|
res = nil // reset result in case of retry
|
|
|
|
// query from db, if exists unmarsal to bitfield
|
|
var dbAllocated bitfield.BitField
|
|
var rawJson []byte
|
|
|
|
err = tx.QueryRow("SELECT COALESCE(allocated, '[0]') from sectors_allocated_numbers sa FULL OUTER JOIN (SELECT 1) AS d ON TRUE WHERE sp_id = $1 OR sp_id IS NULL", mid).Scan(&rawJson)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("querying allocated sector numbers: %w", err)
|
|
}
|
|
|
|
if rawJson != nil {
|
|
err = dbAllocated.UnmarshalJSON(rawJson)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("unmarshaling allocated sector numbers: %w", err)
|
|
}
|
|
}
|
|
|
|
if err := dbAllocated.UnmarshalJSON(rawJson); err != nil {
|
|
return false, xerrors.Errorf("unmarshaling allocated sector numbers: %w", err)
|
|
}
|
|
|
|
merged, err := bitfield.MergeBitFields(*chainAlloc, dbAllocated)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("merging allocated sector numbers: %w", err)
|
|
}
|
|
|
|
allAssignable, err := bitfield.NewFromIter(&rlepluslazy.RunSliceIterator{Runs: []rlepluslazy.Run{
|
|
{
|
|
Val: true,
|
|
Len: abi.MaxSectorNumber,
|
|
},
|
|
}})
|
|
if err != nil {
|
|
return false, xerrors.Errorf("creating assignable sector numbers: %w", err)
|
|
}
|
|
|
|
inverted, err := bitfield.SubtractBitField(allAssignable, merged)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("subtracting allocated sector numbers: %w", err)
|
|
}
|
|
|
|
toAlloc, err := inverted.Slice(0, uint64(count))
|
|
if err != nil {
|
|
return false, xerrors.Errorf("getting slice of allocated sector numbers: %w", err)
|
|
}
|
|
|
|
err = toAlloc.ForEach(func(u uint64) error {
|
|
res = append(res, abi.SectorNumber(u))
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return false, xerrors.Errorf("iterating allocated sector numbers: %w", err)
|
|
}
|
|
|
|
toPersist, err := bitfield.MergeBitFields(merged, toAlloc)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("merging allocated sector numbers: %w", err)
|
|
}
|
|
|
|
rawJson, err = toPersist.MarshalJSON()
|
|
if err != nil {
|
|
return false, xerrors.Errorf("marshaling allocated sector numbers: %w", err)
|
|
}
|
|
|
|
_, err = tx.Exec("INSERT INTO sectors_allocated_numbers(sp_id, allocated) VALUES($1, $2) ON CONFLICT(sp_id) DO UPDATE SET allocated = $2", mid, rawJson)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("persisting allocated sector numbers: %w", err)
|
|
}
|
|
|
|
for i, f := range txcb {
|
|
commit, err = f(tx, res)
|
|
if err != nil {
|
|
return false, xerrors.Errorf("executing tx callback %d: %w", i, err)
|
|
}
|
|
|
|
if !commit {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}, harmonydb.OptionRetry())
|
|
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("allocating sector numbers: %w", err)
|
|
}
|
|
if !comm {
|
|
return nil, xerrors.Errorf("allocating sector numbers: commit failed")
|
|
}
|
|
|
|
return res, nil
|
|
}
|