use offline mode instead

This commit is contained in:
jennijuju 2022-09-22 02:56:08 -04:00 committed by Jennifer Wang
parent b87ade6402
commit 2ec4ac3c0f

View File

@ -4,174 +4,123 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"io"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/node/repo"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
cliutil "github.com/filecoin-project/lotus/cli/util"
)
var GetFullNodeAPI = cliutil.GetFullNodeAPI
var ReqContext = cliutil.ReqContext
func LoadTipSet(ctx context.Context, cctx *cli.Context, api v0api.FullNode) (*types.TipSet, error) {
tss := cctx.String("tipset")
if tss == "" {
return api.ChainHead(ctx)
}
return ParseTipSetRef(ctx, api, tss)
}
func ParseTipSetRef(ctx context.Context, api v0api.FullNode, tss string) (*types.TipSet, error) {
if tss[0] == '@' {
if tss == "@head" {
return api.ChainHead(ctx)
}
var h uint64
if _, err := fmt.Sscanf(tss, "@%d", &h); err != nil {
return nil, xerrors.Errorf("parsing height tipset ref: %w", err)
}
return api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(h), types.EmptyTSK)
}
cids, err := ParseTipSetString(tss)
if err != nil {
return nil, err
}
if len(cids) == 0 {
return nil, nil
}
k := types.NewTipSetKey(cids...)
ts, err := api.ChainGetTipSet(ctx, k)
if err != nil {
return nil, err
}
return ts, nil
}
func ParseTipSetString(ts string) ([]cid.Cid, error) {
strs := strings.Split(ts, ",")
var cids []cid.Cid
for _, s := range strs {
c, err := cid.Parse(strings.TrimSpace(s))
if err != nil {
return nil, err
}
cids = append(cids, c)
}
return cids, nil
}
type msigBriefInfo struct {
ID address.Address
Signer interface{}
Balance abi.TokenAmount
Threshold float64
Threshold uint64
}
var msigCmd = &cli.Command{
Name: "multisig",
Usage: "utils for multisig actors",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset to call method on (pass comma separated array of cids)",
},
},
Name: "msig",
Subcommands: []*cli.Command{
multisigGetAllCmd,
},
}
var multisigGetAllCmd = &cli.Command{
Name: "all",
Usage: "get all multisig actor on chain with id, siigners, threshold and balance",
Name: "all",
Usage: "get all multisig actor on chain with id, siigners, threshold and balance at a tipset",
ArgsUsage: "[state root]",
Flags: []cli.Flag{
&cli.UintFlag{
Name: "network-version",
Value: uint(build.NewestNetworkVersion),
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
ctx := context.TODO()
if !cctx.Args().Present() {
return fmt.Errorf("must pass state root")
}
defer closer()
ctx := ReqContext(cctx)
sroot, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse input: %w", err)
}
ts, err := LoadTipSet(ctx, cctx, api)
fsrepo, err := repo.NewFS(cctx.String("repo"))
if err != nil {
return err
}
actors, err := api.StateListActors(ctx, ts.Key())
lkrepo, err := fsrepo.Lock(repo.FullNode)
if err != nil {
return err
}
nv := network.Version(cctx.Uint64("network-version"))
codeCids, err := api.StateActorCodeCIDs(ctx, nv)
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
}
msigCid, exists := codeCids["multisig"]
if !exists {
return xerrors.Errorf("bad code cid key")
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
defer cs.Close() //nolint:errcheck
cst := cbor.NewCborStore(bs)
store := adt.WrapStore(ctx, cst)
tree, err := state.LoadStateTree(cst, sroot)
if err != nil {
return err
}
var msigActorsInfo []msigBriefInfo
for _, actor := range actors {
act, err := api.StateGetActor(ctx, actor, ts.Key())
if err != nil {
return err
}
if act.Code == msigCid {
actorState, err := api.StateReadState(ctx, actor, ts.Key())
err = tree.ForEach(func(addr address.Address, act *types.Actor) error {
if builtin.IsMultisigActor(act.Code) {
ms, err := multisig.Load(store, act)
if err != nil {
return err
}
stateI, ok := actorState.State.(map[string]interface{})
if !ok {
return xerrors.Errorf("fail to map msig state")
}
signersI, _ := stateI["Signers"]
signers := signersI.([]interface{})
thresholdI, _ := stateI["NumApprovalsThreshold"]
threshold := thresholdI.(float64)
signers, _ := ms.Signers()
threshold, _ := ms.Threshold()
info := msigBriefInfo{
ID: actor,
ID: addr,
Signer: signers,
Balance: actorState.Balance,
Balance: act.Balance,
Threshold: threshold,
}
msigActorsInfo = append(msigActorsInfo, info)
}
}
return nil
})
out, err := json.MarshalIndent(msigActorsInfo, "", " ")
if err != nil {
return err