2021-12-20 01:44:19 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-01-13 19:11:13 +00:00
|
|
|
"strconv"
|
2021-12-20 01:44:19 +00:00
|
|
|
"time"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/urfave/cli/v2"
|
2022-10-13 19:29:49 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2022-10-06 20:00:47 +00:00
|
|
|
"golang.org/x/xerrors"
|
2021-12-20 01:44:19 +00:00
|
|
|
|
2022-10-11 18:50:37 +00:00
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
2022-10-06 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-10-11 18:50:37 +00:00
|
|
|
actorstypes "github.com/filecoin-project/go-state-types/actors"
|
2022-10-09 15:16:25 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
|
|
|
market8 "github.com/filecoin-project/go-state-types/builtin/v8/market"
|
|
|
|
adt8 "github.com/filecoin-project/go-state-types/builtin/v8/util/adt"
|
2022-10-21 12:54:55 +00:00
|
|
|
v9 "github.com/filecoin-project/go-state-types/builtin/v9"
|
2022-10-09 15:16:25 +00:00
|
|
|
market9 "github.com/filecoin-project/go-state-types/builtin/v9/market"
|
2022-10-11 18:50:37 +00:00
|
|
|
miner9 "github.com/filecoin-project/go-state-types/builtin/v9/miner"
|
2022-10-09 15:16:25 +00:00
|
|
|
adt9 "github.com/filecoin-project/go-state-types/builtin/v9/util/adt"
|
|
|
|
verifreg9 "github.com/filecoin-project/go-state-types/builtin/v9/verifreg"
|
2022-12-13 23:02:34 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/manifest"
|
2023-01-13 19:11:13 +00:00
|
|
|
mutil "github.com/filecoin-project/go-state-types/migration"
|
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/v7/actors/migration/nv15"
|
2021-12-20 01:44:19 +00:00
|
|
|
|
2022-10-06 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2022-10-11 18:50:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2022-10-06 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2022-10-13 20:15:59 +00:00
|
|
|
lbuiltin "github.com/filecoin-project/lotus/chain/actors/builtin"
|
2022-10-06 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/datacap"
|
2022-10-07 20:35:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
2022-10-11 18:50:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2022-10-06 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
|
2021-12-20 01:44:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/consensus/filcns"
|
2022-10-06 20:00:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
2021-12-20 01:44:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2022-09-14 19:38:15 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2021-12-20 01:44:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper"
|
2021-12-20 01:44:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var migrationsCmd = &cli.Command{
|
2023-01-13 19:11:13 +00:00
|
|
|
Name: "migrate-state",
|
|
|
|
Description: "Run a network upgrade migration",
|
|
|
|
ArgsUsage: "[new network version, block to look back from]",
|
2021-12-20 01:44:19 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2023-01-27 16:20:01 +00:00
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
2021-12-20 01:44:19 +00:00
|
|
|
},
|
2022-11-14 21:46:45 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "skip-pre-migration",
|
|
|
|
},
|
2022-10-20 17:10:45 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "check-invariants",
|
|
|
|
},
|
2021-12-20 01:44:19 +00:00
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
if cctx.NArg() != 2 {
|
|
|
|
return lcli.IncorrectNumArgs(cctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
nv, err := strconv.ParseUint(cctx.Args().Get(0), 10, 32)
|
2022-11-16 23:19:59 +00:00
|
|
|
if err != nil {
|
2023-01-13 19:11:13 +00:00
|
|
|
return fmt.Errorf("failed to parse network version: %w", err)
|
2022-11-16 23:19:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
upgradeActorsFunc, preUpgradeActorsFunc, checkInvariantsFunc, err := getMigrationFuncsForNetwork(network.Version(nv))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-20 01:44:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
blkCid, err := cid.Decode(cctx.Args().Get(1))
|
2021-12-20 01:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse input: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fsrepo, err := repo.NewFS(cctx.String("repo"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lkrepo, err := fsrepo.Lock(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer lkrepo.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
bs, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open blockstore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if c, ok := bs.(io.Closer); ok {
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
log.Warnf("failed to close blockstore: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
mds, err := lkrepo.Datastore(context.Background(), "/metadata")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
|
|
|
|
defer cs.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
sm, err := stmgr.NewStateManager(cs, filcns.NewTipSetExecutor(), vm.Syscalls(ffiwrapper.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err := cs.GetBlock(ctx, blkCid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
migrationTs, err := cs.LoadTipSet(ctx, types.NewTipSetKey(blk.Parents...))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
newCid2, err := upgradeActorsFunc(ctx, sm, nv15.NewMemMigrationCache(), nil, blk.ParentStateRoot, blk.Height-1, migrationTs)
|
2021-12-20 01:44:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
uncachedMigrationTime := time.Since(startTime)
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
fmt.Println("migration height ", blk.Height-1)
|
|
|
|
fmt.Println("old cid ", blk.ParentStateRoot)
|
|
|
|
fmt.Println("new cid ", newCid2)
|
|
|
|
fmt.Println("completed round actual (without cache), took ", uncachedMigrationTime)
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
if !cctx.IsSet("skip-pre-migration") {
|
2023-01-13 19:11:13 +00:00
|
|
|
cache := mutil.NewMemMigrationCache()
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
ts1, err := cs.GetTipsetByHeight(ctx, blk.Height-240, migrationTs, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2022-11-16 20:07:23 +00:00
|
|
|
startTime = time.Now()
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
err = preUpgradeActorsFunc(ctx, sm, cache, ts1.ParentState(), ts1.Height()-1, ts1)
|
2022-11-14 21:46:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
preMigration1Time := time.Since(startTime)
|
2021-12-20 01:44:19 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
ts2, err := cs.GetTipsetByHeight(ctx, blk.Height-15, migrationTs, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-20 01:44:19 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
startTime = time.Now()
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
err = preUpgradeActorsFunc(ctx, sm, cache, ts2.ParentState(), ts2.Height()-1, ts2)
|
2022-11-14 21:46:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-20 01:44:19 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
preMigration2Time := time.Since(startTime)
|
2021-12-20 01:44:19 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
startTime = time.Now()
|
2022-10-04 15:29:27 +00:00
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
newCid1, err := upgradeActorsFunc(ctx, sm, cache, nil, blk.ParentStateRoot, blk.Height-1, migrationTs)
|
2022-11-14 21:46:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 19:53:36 +00:00
|
|
|
|
2022-11-14 21:46:45 +00:00
|
|
|
cachedMigrationTime := time.Since(startTime)
|
|
|
|
|
|
|
|
if newCid1 != newCid2 {
|
|
|
|
return xerrors.Errorf("got different results with and without the cache: %s, %s", newCid1,
|
|
|
|
newCid2)
|
|
|
|
}
|
|
|
|
fmt.Println("completed premigration 1, took ", preMigration1Time)
|
|
|
|
fmt.Println("completed premigration 2, took ", preMigration2Time)
|
|
|
|
fmt.Println("completed round actual (with cache), took ", cachedMigrationTime)
|
|
|
|
}
|
2022-10-19 20:24:02 +00:00
|
|
|
|
2022-10-20 17:10:45 +00:00
|
|
|
if cctx.Bool("check-invariants") {
|
2023-01-13 19:11:13 +00:00
|
|
|
if checkInvariantsFunc == nil {
|
|
|
|
return xerrors.Errorf("check invariants not implemented for nv%d", nv)
|
|
|
|
}
|
|
|
|
err = checkInvariantsFunc(ctx, blk.ParentStateRoot, newCid2, bs, blk.Height-1)
|
2022-10-20 17:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-06 20:00:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 01:44:19 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-10-06 20:00:47 +00:00
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
func getMigrationFuncsForNetwork(nv network.Version) (UpgradeActorsFunc, PreUpgradeActorsFunc, CheckInvariantsFunc, error) {
|
|
|
|
switch nv {
|
|
|
|
case network.Version17:
|
|
|
|
return filcns.UpgradeActorsV9, filcns.PreUpgradeActorsV9, checkNv17Invariants, nil
|
|
|
|
case network.Version18:
|
|
|
|
return filcns.UpgradeActorsV10, filcns.PreUpgradeActorsV10, nil, nil
|
|
|
|
default:
|
|
|
|
return nil, nil, nil, xerrors.Errorf("migration not implemented for nv%d", nv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpgradeActorsFunc = func(context.Context, *stmgr.StateManager, stmgr.MigrationCache, stmgr.ExecMonitor, cid.Cid, abi.ChainEpoch, *types.TipSet) (cid.Cid, error)
|
|
|
|
type PreUpgradeActorsFunc = func(context.Context, *stmgr.StateManager, stmgr.MigrationCache, cid.Cid, abi.ChainEpoch, *types.TipSet) error
|
|
|
|
type CheckInvariantsFunc = func(context.Context, cid.Cid, cid.Cid, blockstore.Blockstore, abi.ChainEpoch) error
|
|
|
|
|
|
|
|
func checkNv17Invariants(ctx context.Context, v8StateRootCid cid.Cid, v9StateRootCid cid.Cid, bs blockstore.Blockstore, epoch abi.ChainEpoch) error {
|
2022-10-20 17:10:45 +00:00
|
|
|
actorStore := store.ActorStore(ctx, bs)
|
|
|
|
startTime := time.Now()
|
2022-10-06 20:00:47 +00:00
|
|
|
|
2022-10-27 11:37:31 +00:00
|
|
|
stateTreeV8, err := state.LoadStateTree(actorStore, v8StateRootCid)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:37:31 +00:00
|
|
|
stateTreeV9, err := state.LoadStateTree(actorStore, v9StateRootCid)
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
err = checkDatacaps(stateTreeV8, stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
err = checkPendingVerifiedDeals(stateTreeV8, stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
err = checkAllMinersUnsealedCID(stateTreeV9, actorStore)
|
2022-10-11 18:50:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-20 17:10:45 +00:00
|
|
|
// Load the state root.
|
2022-10-27 11:37:31 +00:00
|
|
|
var v9stateRoot types.StateRoot
|
|
|
|
if err := actorStore.Get(ctx, v9StateRootCid, &v9stateRoot); err != nil {
|
2022-10-21 12:54:55 +00:00
|
|
|
return xerrors.Errorf("failed to decode state root: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actorCodeCids, err := actors.GetActorCodeIDs(actorstypes.Version9)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 11:43:26 +00:00
|
|
|
v9actorTree, err := builtin.LoadTree(actorStore, v9stateRoot.Actors)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-27 11:37:31 +00:00
|
|
|
messages, err := v9.CheckStateInvariants(v9actorTree, epoch, actorCodeCids)
|
2022-10-21 12:54:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("checking state invariants: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, message := range messages.Messages() {
|
|
|
|
fmt.Println("got the following error: ", message)
|
|
|
|
}
|
2022-10-20 17:10:45 +00:00
|
|
|
|
|
|
|
fmt.Println("completed invariant checks, took ", time.Since(startTime))
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func checkDatacaps(stateTreeV8 *state.StateTree, stateTreeV9 *state.StateTree, actorStore adt.Store) error {
|
2022-10-07 20:35:47 +00:00
|
|
|
verifregDatacaps, err := getVerifreg8Datacaps(stateTreeV8, actorStore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newDatacaps, err := getDatacap9Datacaps(stateTreeV9, actorStore)
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-22 17:06:01 +00:00
|
|
|
// Should have all the v8 datacaps, plus the verifreg actor itself
|
|
|
|
if len(verifregDatacaps)+1 != len(newDatacaps) {
|
2022-10-06 20:00:47 +00:00
|
|
|
return xerrors.Errorf("size of datacap maps do not match. verifreg: %d, datacap: %d", len(verifregDatacaps), len(newDatacaps))
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr, oldDcap := range verifregDatacaps {
|
|
|
|
dcap, ok := newDatacaps[addr]
|
|
|
|
if !ok {
|
|
|
|
return xerrors.Errorf("datacap for address: %s not found in datacap state", addr)
|
|
|
|
}
|
2022-10-07 04:25:00 +00:00
|
|
|
if !dcap.Equals(oldDcap) {
|
2022-10-06 20:00:47 +00:00
|
|
|
return xerrors.Errorf("datacap for address: %s do not match. verifreg: %d, datacap: %d", addr, oldDcap, dcap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getVerifreg8Datacaps(stateTreeV8 *state.StateTree, actorStore adt.Store) (map[address.Address]abi.StoragePower, error) {
|
2022-10-09 15:16:25 +00:00
|
|
|
verifregStateV8, err := getVerifregActorV8(stateTreeV8, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get verifreg actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var verifregDatacaps = make(map[address.Address]abi.StoragePower)
|
|
|
|
err = verifregStateV8.ForEachClient(func(addr address.Address, dcap abi.StoragePower) error {
|
|
|
|
verifregDatacaps[addr] = dcap
|
|
|
|
return nil
|
|
|
|
})
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return verifregDatacaps, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getDatacap9Datacaps(stateTreeV9 *state.StateTree, actorStore adt.Store) (map[address.Address]abi.StoragePower, error) {
|
2022-10-09 15:16:25 +00:00
|
|
|
datacapStateV9, err := getDatacapActorV9(stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get datacap actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var datacaps = make(map[address.Address]abi.StoragePower)
|
|
|
|
err = datacapStateV9.ForEachClient(func(addr address.Address, dcap abi.StoragePower) error {
|
|
|
|
datacaps[addr] = dcap
|
|
|
|
return nil
|
|
|
|
})
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return datacaps, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func checkPendingVerifiedDeals(stateTreeV8 *state.StateTree, stateTreeV9 *state.StateTree, actorStore adt.Store) error {
|
2022-10-09 15:16:25 +00:00
|
|
|
marketActorV9, err := getMarketActorV9(stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-10-06 20:00:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
verifregActorV9, err := getVerifregActorV9(stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
verifregStateV9, err := getVerifregStateV9(stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
marketStateV8, err := getMarketStateV8(stateTreeV8, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
marketStateV9, err := getMarketStateV9(stateTreeV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
pendingProposalsV8, err := adt8.AsSet(actorStore, marketStateV8.PendingProposals, builtin.DefaultHamtBitwidth)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to load pending proposals: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dealProposalsV8, err := market8.AsDealProposalArray(actorStore, marketStateV8.Proposals)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get proposals: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-24 17:47:47 +00:00
|
|
|
// We only want those pending deals that haven't been activated -- an activated deal has an entry in dealStates8
|
|
|
|
dealStates8, err := adt9.AsArray(actorStore, marketStateV8.States, market8.StatesAmtBitwidth)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to load v8 states array: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
var numPendingVerifiedDeals = 0
|
2022-10-09 15:16:25 +00:00
|
|
|
var proposal market8.DealProposal
|
|
|
|
err = dealProposalsV8.ForEach(&proposal, func(dealID int64) error {
|
|
|
|
// If not verified, do nothing
|
|
|
|
if !proposal.VerifiedDeal {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-07 20:35:47 +00:00
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
pcid, err := proposal.Cid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isPending, err := pendingProposalsV8.Has(abi.CidKey(pcid))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to check pending: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do for not-pending deals
|
|
|
|
if !isPending {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-24 17:47:47 +00:00
|
|
|
var _dealState8 market8.DealState
|
|
|
|
found, err := dealStates8.Get(uint64(dealID), &_dealState8)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to lookup deal state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the deal has an entry in deal states, which means it's already been allocated, nothing to do
|
|
|
|
if found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
numPendingVerifiedDeals++
|
|
|
|
// Checks if allocation ID is in market map
|
|
|
|
allocationId, err := marketActorV9.GetAllocationIdForPendingDeal(abi.DealID(dealID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 20:15:59 +00:00
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
// Checks if allocation is in verifreg
|
2022-10-13 20:15:59 +00:00
|
|
|
allocation, found, err := verifregActorV9.GetAllocation(proposal.Client, allocationId)
|
2022-10-09 15:16:25 +00:00
|
|
|
if !found {
|
|
|
|
return xerrors.Errorf("allocation %d not found for address %s", allocationId, proposal.Client)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-10-07 20:35:47 +00:00
|
|
|
}
|
2022-10-13 20:15:59 +00:00
|
|
|
|
|
|
|
err = compareProposalToAllocation(proposal, *allocation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-06 20:00:47 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
fmt.Printf("Pending Verified deals in market v8: %d\n", numPendingVerifiedDeals)
|
|
|
|
|
|
|
|
numAllocationIds, err := countAllocationIds(actorStore, marketStateV9)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Allocation IDs in market v9: %d\n", numAllocationIds)
|
|
|
|
|
|
|
|
if numAllocationIds != numPendingVerifiedDeals {
|
|
|
|
return xerrors.Errorf("number of allocation IDsf: %d did not match the number of pending verified deals: %d", numAllocationIds, numPendingVerifiedDeals)
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:10:41 +00:00
|
|
|
numAllocations, err := countAllocations(verifregStateV9, actorStore)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-09 15:16:25 +00:00
|
|
|
fmt.Printf("Allocations in verifreg v9: %d\n", numAllocations)
|
2022-10-07 20:35:47 +00:00
|
|
|
|
|
|
|
if numAllocations != numPendingVerifiedDeals {
|
|
|
|
return xerrors.Errorf("number of allocations: %d did not match the number of pending verified deals: %d", numAllocations, numPendingVerifiedDeals)
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
nextAllocationId := int(verifregStateV9.NextAllocationId)
|
|
|
|
fmt.Printf("Next Allocation ID: %d\n", nextAllocationId)
|
|
|
|
|
|
|
|
if numAllocations+1 != nextAllocationId {
|
|
|
|
return xerrors.Errorf("number of allocations + 1: %d did not match the next allocation ID: %d", numAllocations+1, nextAllocationId)
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:15:59 +00:00
|
|
|
func compareProposalToAllocation(prop market8.DealProposal, alloc verifreg9.Allocation) error {
|
|
|
|
if prop.PieceCID != alloc.Data {
|
|
|
|
return xerrors.Errorf("piece cid mismatch between proposal and allocation: %s, %s", prop.PieceCID, alloc.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
proposalClientID, err := address.IDFromAddress(prop.Client)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("couldnt get ID from address")
|
|
|
|
}
|
|
|
|
if proposalClientID != uint64(alloc.Client) {
|
2022-11-24 16:32:27 +00:00
|
|
|
return xerrors.Errorf("client id mismatch between proposal and allocation: %v, %v", proposalClientID, alloc.Client)
|
2022-10-13 20:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proposalProviderID, err := address.IDFromAddress(prop.Provider)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("couldnt get ID from address")
|
|
|
|
}
|
|
|
|
if proposalProviderID != uint64(alloc.Provider) {
|
2022-11-24 16:32:27 +00:00
|
|
|
return xerrors.Errorf("provider id mismatch between proposal and allocation: %v, %v", proposalProviderID, alloc.Provider)
|
2022-10-13 20:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if prop.PieceSize != alloc.Size {
|
2022-11-24 16:32:27 +00:00
|
|
|
return xerrors.Errorf("piece size mismatch between proposal and allocation: %v, %v", prop.PieceSize, alloc.Size)
|
2022-10-13 20:15:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 16:27:37 +00:00
|
|
|
if alloc.TermMax != 540*builtin.EpochsInDay {
|
|
|
|
return xerrors.Errorf("allocation term should be 540 days. Got %d epochs", alloc.TermMax)
|
|
|
|
}
|
2022-10-13 20:15:59 +00:00
|
|
|
|
|
|
|
if prop.EndEpoch-prop.StartEpoch != alloc.TermMin {
|
|
|
|
return xerrors.Errorf("allocation term mismatch between proposal and allocation: %d, %d", prop.EndEpoch-prop.StartEpoch, alloc.TermMin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getMarketStateV8(stateTreeV8 *state.StateTree, actorStore adt.Store) (market8.State, error) {
|
2022-10-09 15:16:25 +00:00
|
|
|
marketV8, err := stateTreeV8.GetActor(market.Address)
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:16:25 +00:00
|
|
|
return market8.State{}, err
|
2022-10-06 20:00:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:16:25 +00:00
|
|
|
var marketStateV8 market8.State
|
|
|
|
if err = actorStore.Get(actorStore.Context(), marketV8.Head, &marketStateV8); err != nil {
|
|
|
|
return market8.State{}, xerrors.Errorf("failed to get market actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return marketStateV8, nil
|
2022-10-06 20:00:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getMarketStateV9(stateTreeV9 *state.StateTree, actorStore adt.Store) (market9.State, error) {
|
2022-10-09 15:16:25 +00:00
|
|
|
marketV9, err := stateTreeV9.GetActor(market.Address)
|
|
|
|
if err != nil {
|
|
|
|
return market9.State{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var marketStateV9 market9.State
|
|
|
|
if err = actorStore.Get(actorStore.Context(), marketV9.Head, &marketStateV9); err != nil {
|
|
|
|
return market9.State{}, xerrors.Errorf("failed to get market actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return marketStateV9, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getMarketActorV9(stateTreeV9 *state.StateTree, actorStore adt.Store) (market.State, error) {
|
2022-10-07 20:35:47 +00:00
|
|
|
marketV9, err := stateTreeV9.GetActor(market.Address)
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return market.Load(actorStore, marketV9)
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getVerifregActorV8(stateTreeV8 *state.StateTree, actorStore adt.Store) (verifreg.State, error) {
|
2022-10-07 20:35:47 +00:00
|
|
|
verifregV8, err := stateTreeV8.GetActor(verifreg.Address)
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return verifreg.Load(actorStore, verifregV8)
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getVerifregActorV9(stateTreeV9 *state.StateTree, actorStore adt.Store) (verifreg.State, error) {
|
2022-10-07 20:35:47 +00:00
|
|
|
verifregV9, err := stateTreeV9.GetActor(verifreg.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-10-06 20:00:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return verifreg.Load(actorStore, verifregV9)
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getVerifregStateV9(stateTreeV9 *state.StateTree, actorStore adt.Store) (verifreg9.State, error) {
|
2022-10-09 15:16:25 +00:00
|
|
|
verifregV9, err := stateTreeV9.GetActor(verifreg.Address)
|
|
|
|
if err != nil {
|
|
|
|
return verifreg9.State{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var verifregStateV9 verifreg9.State
|
|
|
|
if err = actorStore.Get(actorStore.Context(), verifregV9.Head, &verifregStateV9); err != nil {
|
|
|
|
return verifreg9.State{}, xerrors.Errorf("failed to get verifreg actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return verifregStateV9, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func getDatacapActorV9(stateTreeV9 *state.StateTree, actorStore adt.Store) (datacap.State, error) {
|
2022-10-07 20:35:47 +00:00
|
|
|
datacapV9, err := stateTreeV9.GetActor(datacap.Address)
|
2022-10-06 20:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return datacap.Load(actorStore, datacapV9)
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func checkAllMinersUnsealedCID(stateTreeV9 *state.StateTree, store adt.Store) error {
|
2022-10-13 22:08:28 +00:00
|
|
|
return stateTreeV9.ForEach(func(addr address.Address, actor *types.Actor) error {
|
2022-10-13 20:15:59 +00:00
|
|
|
if !lbuiltin.IsStorageMinerActor(actor.Code) {
|
2022-10-11 18:50:37 +00:00
|
|
|
return nil // no need to check
|
|
|
|
}
|
|
|
|
|
|
|
|
err := checkMinerUnsealedCID(actor, stateTreeV9, store)
|
|
|
|
if err != nil {
|
2022-10-13 22:08:28 +00:00
|
|
|
fmt.Println("failure for miner ", addr)
|
2022-10-11 18:50:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:34:42 +00:00
|
|
|
func checkMinerUnsealedCID(act *types.Actor, stateTreeV9 *state.StateTree, store adt.Store) error {
|
2022-12-13 23:02:34 +00:00
|
|
|
minerCodeCid, found := actors.GetActorCodeID(actorstypes.Version9, manifest.MinerKey)
|
2022-10-11 18:50:37 +00:00
|
|
|
if !found {
|
|
|
|
return xerrors.Errorf("could not find code cid for miner actor")
|
|
|
|
}
|
|
|
|
if minerCodeCid != act.Code {
|
|
|
|
return nil // no need to check
|
|
|
|
}
|
|
|
|
|
|
|
|
marketActorV9, err := getMarketActorV9(stateTreeV9, store)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dealProposals, err := marketActorV9.Proposals()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := miner.Load(store, act)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.ForEachPrecommittedSector(func(info miner9.SectorPreCommitOnChainInfo) error {
|
|
|
|
dealIDs := info.Info.DealIDs
|
|
|
|
|
|
|
|
if len(dealIDs) == 0 {
|
|
|
|
return nil // Nothing to check here
|
|
|
|
}
|
|
|
|
|
|
|
|
pieceCids := make([]abi.PieceInfo, len(dealIDs))
|
|
|
|
for i, dealId := range dealIDs {
|
|
|
|
dealProposal, found, err := dealProposals.Get(dealId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 22:08:28 +00:00
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-11 18:50:37 +00:00
|
|
|
|
|
|
|
pieceCids[i] = abi.PieceInfo{
|
|
|
|
Size: dealProposal.PieceSize,
|
|
|
|
PieceCID: dealProposal.PieceCID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:08:28 +00:00
|
|
|
if len(pieceCids) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Info.UnsealedCid == nil {
|
|
|
|
return xerrors.Errorf("nil unsealed CID for sector with deals")
|
|
|
|
}
|
|
|
|
|
2022-10-11 18:50:37 +00:00
|
|
|
pieceCID, err := ffi.GenerateUnsealedCID(abi.RegisteredSealProof_StackedDrg64GiBV1_1, pieceCids)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pieceCID != *info.Info.UnsealedCid {
|
|
|
|
return xerrors.Errorf("calculated piece CID %s did not match unsealed CID in precommitted sector info: %s", pieceCID, *info.Info.UnsealedCid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 19:10:41 +00:00
|
|
|
func countAllocations(verifregState verifreg9.State, store adt.Store) (int, error) {
|
|
|
|
var count = 0
|
|
|
|
|
|
|
|
actorToHamtMap, err := adt9.AsMap(store, verifregState.Allocations, builtin.DefaultHamtBitwidth)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("couldn't get outer map: %x", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var innerHamtCid cbg.CborCid
|
|
|
|
err = actorToHamtMap.ForEach(&innerHamtCid, func(key string) error {
|
|
|
|
innerMap, err := adt9.AsMap(store, cid.Cid(innerHamtCid), builtin.DefaultHamtBitwidth)
|
2022-10-07 20:35:47 +00:00
|
|
|
if err != nil {
|
2022-10-13 19:10:41 +00:00
|
|
|
return xerrors.Errorf("couldn't get outer map: %x", err)
|
2022-10-07 20:35:47 +00:00
|
|
|
}
|
2022-10-13 19:10:41 +00:00
|
|
|
|
|
|
|
var allocation verifreg9.Allocation
|
|
|
|
err = innerMap.ForEach(&allocation, func(key string) error {
|
|
|
|
count++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("couldn't iterate over inner map: %x", err)
|
|
|
|
}
|
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-13 19:10:41 +00:00
|
|
|
return 0, xerrors.Errorf("couldn't iterate over outer map: %x", err)
|
2022-10-07 20:35:47 +00:00
|
|
|
}
|
2022-10-13 19:10:41 +00:00
|
|
|
|
2022-10-07 20:35:47 +00:00
|
|
|
return count, nil
|
2022-10-06 20:00:47 +00:00
|
|
|
}
|
2022-10-09 15:16:25 +00:00
|
|
|
|
|
|
|
func countAllocationIds(store adt.Store, marketState market9.State) (int, error) {
|
|
|
|
allocationIds, err := adt9.AsMap(store, marketState.PendingDealAllocationIds, builtin.DefaultHamtBitwidth)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var numAllocationIds int
|
|
|
|
_ = allocationIds.ForEach(nil, func(key string) error {
|
|
|
|
numAllocationIds++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return numAllocationIds, nil
|
|
|
|
}
|