lotus/cmd/lotus-shed/invariants.go

151 lines
3.9 KiB
Go
Raw Permalink Normal View History

2022-10-14 20:03:28 +00:00
package main
import (
"context"
"fmt"
"io"
"strconv"
2022-10-14 20:03:28 +00:00
"time"
"github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
2022-10-31 11:39:37 +00:00
"github.com/filecoin-project/go-state-types/abi"
2022-10-14 20:03:28 +00:00
actorstypes "github.com/filecoin-project/go-state-types/actors"
"github.com/filecoin-project/go-state-types/builtin"
v8 "github.com/filecoin-project/go-state-types/builtin/v8"
v9 "github.com/filecoin-project/go-state-types/builtin/v9"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors"
2022-11-22 09:59:43 +00:00
"github.com/filecoin-project/lotus/chain/consensus"
2022-10-14 20:03:28 +00:00
"github.com/filecoin-project/lotus/chain/consensus/filcns"
2023-03-12 13:33:36 +00:00
"github.com/filecoin-project/lotus/chain/index"
2022-10-14 20:03:28 +00:00
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/repo"
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper"
)
var invariantsCmd = &cli.Command{
Name: "check-invariants",
Description: "Check state invariants",
ArgsUsage: "[StateRootCid, height]",
2022-10-14 20:03:28 +00:00
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
2022-10-14 20:03:28 +00:00
},
},
Action: func(cctx *cli.Context) error {
ctx := context.TODO()
if cctx.NArg() != 2 {
2022-10-14 20:03:28 +00:00
return lcli.IncorrectNumArgs(cctx)
}
stateRootCid, err := cid.Decode(cctx.Args().Get(0))
if err != nil {
return fmt.Errorf("failed to parse state root cid: %w", err)
}
epoch, err := strconv.ParseInt(cctx.Args().Get(1), 10, 64)
2022-10-14 20:03:28 +00:00
if err != nil {
return fmt.Errorf("failed to parse epoch: %w", err)
2022-10-14 20:03:28 +00:00
}
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
2023-03-12 13:33:36 +00:00
sm, err := stmgr.NewStateManager(cs, consensus.NewTipSetExecutor(filcns.RewardFunc), vm.Syscalls(ffiwrapper.ProofVerifier), filcns.DefaultUpgradeSchedule(), nil, mds, index.DummyMsgIndex)
2022-10-14 20:03:28 +00:00
if err != nil {
return err
}
nv := sm.GetNetworkVersion(ctx, abi.ChainEpoch(epoch))
2022-10-14 20:03:28 +00:00
fmt.Println("Network Version ", nv)
av, err := actorstypes.VersionForNetwork(nv)
2022-11-25 21:19:20 +00:00
if err != nil {
return err
}
2022-10-14 20:03:28 +00:00
fmt.Println("Actors Version ", av)
actorCodeCids, err := actors.GetActorCodeIDs(av)
if err != nil {
return err
}
actorStore := store.ActorStore(ctx, blockstore.NewTieredBstore(bs, blockstore.NewMemorySync()))
// Load the state root.
var stateRoot types.StateRoot
if err := actorStore.Get(ctx, stateRootCid, &stateRoot); err != nil {
2022-10-14 20:03:28 +00:00
return xerrors.Errorf("failed to decode state root: %w", err)
}
actorTree, err := builtin.LoadTree(actorStore, stateRoot.Actors)
2022-11-25 21:19:20 +00:00
if err != nil {
return err
}
2022-10-14 20:03:28 +00:00
startTime := time.Now()
var messages *builtin.MessageAccumulator
switch av {
case actorstypes.Version8:
messages, err = v8.CheckStateInvariants(actorTree, abi.ChainEpoch(epoch), actorCodeCids)
2022-10-14 20:03:28 +00:00
if err != nil {
return xerrors.Errorf("checking state invariants: %w", err)
}
case actorstypes.Version9:
messages, err = v9.CheckStateInvariants(actorTree, abi.ChainEpoch(epoch), actorCodeCids)
2022-10-14 20:03:28 +00:00
if err != nil {
return xerrors.Errorf("checking state invariants: %w", err)
}
}
fmt.Println("completed, took ", time.Since(startTime))
for _, message := range messages.Messages() {
fmt.Println("got the following error: ", message)
}
return nil
},
}