2019-12-19 18:46:30 +00:00
|
|
|
package stmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-01-16 22:21:16 +00:00
|
|
|
amt "github.com/filecoin-project/go-amt-ipld/v2"
|
2020-01-17 02:33:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2019-12-20 09:51:13 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-01-17 02:33:43 +00:00
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
2020-01-16 22:21:16 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
|
|
"golang.org/x/xerrors"
|
2019-12-19 18:46:30 +00:00
|
|
|
)
|
|
|
|
|
2019-12-19 20:00:59 +00:00
|
|
|
func (sm *StateManager) handleStateForks(ctx context.Context, pstate cid.Cid, height, parentH uint64) (_ cid.Cid, err error) {
|
|
|
|
for i := parentH; i < height; i++ {
|
|
|
|
switch i {
|
2020-01-17 02:33:43 +00:00
|
|
|
case build.ForkBlizzardHeight:
|
2020-01-18 05:35:45 +00:00
|
|
|
log.Warnw("Executing blizzard fork logic", "height", i)
|
2020-01-19 16:27:43 +00:00
|
|
|
pstate, err = fixBlizzardAMTBug(ctx, sm, pstate)
|
2020-01-17 02:33:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("blizzard bug fix failed: %w", err)
|
|
|
|
}
|
2020-01-18 05:20:56 +00:00
|
|
|
case build.ForkFrigidHeight:
|
2020-01-18 05:35:45 +00:00
|
|
|
log.Warnw("Executing frigid fork logic", "height", i)
|
2020-01-19 16:27:43 +00:00
|
|
|
pstate, err = fixBlizzardAMTBug(ctx, sm, pstate)
|
2020-01-18 05:20:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("frigid bug fix failed: %w", err)
|
|
|
|
}
|
2020-01-19 16:27:43 +00:00
|
|
|
case build.ForkBootyBayHeight:
|
|
|
|
log.Warnw("Executing booty bay fork logic", "height", i)
|
|
|
|
pstate, err = fixBlizzardAMTBug(ctx, sm, pstate)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("booty bay bug fix failed: %w", err)
|
|
|
|
}
|
2019-12-19 20:00:59 +00:00
|
|
|
}
|
2019-12-19 18:46:30 +00:00
|
|
|
}
|
2019-12-19 20:00:59 +00:00
|
|
|
|
|
|
|
return pstate, nil
|
2019-12-19 18:46:30 +00:00
|
|
|
}
|
2020-01-16 22:21:16 +00:00
|
|
|
|
2020-01-17 02:33:43 +00:00
|
|
|
/*
|
|
|
|
1) Iterate through each miner in the chain:
|
|
|
|
1.1) Fixup their sector set and proving set
|
|
|
|
1.2) Change their code cid to point to the new miner actor code
|
|
|
|
*/
|
|
|
|
func fixBlizzardAMTBug(ctx context.Context, sm *StateManager, pstate cid.Cid) (cid.Cid, error) {
|
|
|
|
cst := hamt.CSTFromBstore(sm.cs.Blockstore())
|
|
|
|
st, err := state.LoadStateTree(cst, pstate)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
spa, err := st.GetActor(actors.StoragePowerAddress)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to get storage power actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var spast actors.StoragePowerState
|
|
|
|
if err := cst.Get(ctx, spa.Head, &spast); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
miners, err := actors.MinerSetList(ctx, cst, spast.Miners)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range miners {
|
|
|
|
mact, err := st.GetActor(m)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("getting miner actor to fix: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nhead, err := fixMiner(ctx, cst, sm.cs.Blockstore(), mact.Head)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("fixing miner: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nhead != mact.Head {
|
|
|
|
log.Warnf("Miner %s had changes", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
mact.Head = nhead
|
|
|
|
mact.Code = actors.StorageMiner2CodeCid
|
|
|
|
|
|
|
|
if err := st.SetActor(m, mact); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:53:06 +00:00
|
|
|
return st.Flush(ctx)
|
2020-01-17 02:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fixMiner(ctx context.Context, cst *hamt.CborIpldStore, bs blockstore.Blockstore, mscid cid.Cid) (cid.Cid, error) {
|
|
|
|
var mstate actors.StorageMinerActorState
|
|
|
|
if err := cst.Get(ctx, mscid, &mstate); err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to load miner actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amts := amt.WrapBlockstore(bs)
|
|
|
|
|
|
|
|
nsectors, err := amtFsck(amts, mstate.Sectors)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error fsck'ing sector set: %w", err)
|
|
|
|
}
|
|
|
|
mstate.Sectors = nsectors
|
|
|
|
|
|
|
|
nproving, err := amtFsck(amts, mstate.ProvingSet)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("error fsck'ing proving set: %w", err)
|
|
|
|
}
|
|
|
|
mstate.ProvingSet = nproving
|
|
|
|
|
|
|
|
nmcid, err := cst.Put(ctx, &mstate)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to put modified miner state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nmcid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func amtFsck(s amt.Blocks, ss cid.Cid) (cid.Cid, error) {
|
|
|
|
a, err := amt.LoadAMT(s, ss)
|
2020-01-16 22:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("could not load AMT: %w", a)
|
|
|
|
}
|
|
|
|
|
2020-01-17 02:33:43 +00:00
|
|
|
b := amt.NewAMT(s)
|
2020-01-16 22:21:16 +00:00
|
|
|
|
|
|
|
err = a.ForEach(func(id uint64, data *cbg.Deferred) error {
|
|
|
|
err := b.Set(id, data)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("could not copy at idx (%d): %w", id, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("could not copy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nss, err := b.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("could not flush: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nss, nil
|
|
|
|
}
|