use offline mode instead
This commit is contained in:
parent
b87ade6402
commit
2ec4ac3c0f
@ -4,174 +4,123 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"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/ipfs/go-cid"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
|
||||||
|
|
||||||
"github.com/filecoin-project/go-address"
|
"github.com/filecoin-project/go-address"
|
||||||
"github.com/filecoin-project/go-state-types/abi"
|
"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"
|
"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 {
|
type msigBriefInfo struct {
|
||||||
ID address.Address
|
ID address.Address
|
||||||
Signer interface{}
|
Signer interface{}
|
||||||
Balance abi.TokenAmount
|
Balance abi.TokenAmount
|
||||||
Threshold float64
|
Threshold uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
var msigCmd = &cli.Command{
|
var msigCmd = &cli.Command{
|
||||||
Name: "multisig",
|
Name: "msig",
|
||||||
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)",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
multisigGetAllCmd,
|
multisigGetAllCmd,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var multisigGetAllCmd = &cli.Command{
|
var multisigGetAllCmd = &cli.Command{
|
||||||
Name: "all",
|
Name: "all",
|
||||||
Usage: "get all multisig actor on chain with id, siigners, threshold and balance",
|
Usage: "get all multisig actor on chain with id, siigners, threshold and balance at a tipset",
|
||||||
|
ArgsUsage: "[state root]",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.UintFlag{
|
&cli.StringFlag{
|
||||||
Name: "network-version",
|
Name: "repo",
|
||||||
Value: uint(build.NewestNetworkVersion),
|
Value: "~/.lotus",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(cctx *cli.Context) error {
|
Action: func(cctx *cli.Context) error {
|
||||||
api, closer, err := GetFullNodeAPI(cctx)
|
ctx := context.TODO()
|
||||||
if err != nil {
|
if !cctx.Args().Present() {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
actors, err := api.StateListActors(ctx, ts.Key())
|
lkrepo, err := fsrepo.Lock(repo.FullNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
nv := network.Version(cctx.Uint64("network-version"))
|
defer lkrepo.Close() //nolint:errcheck
|
||||||
codeCids, err := api.StateActorCodeCIDs(ctx, nv)
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
msigCid, exists := codeCids["multisig"]
|
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
|
||||||
if !exists {
|
defer cs.Close() //nolint:errcheck
|
||||||
return xerrors.Errorf("bad code cid key")
|
|
||||||
|
cst := cbor.NewCborStore(bs)
|
||||||
|
store := adt.WrapStore(ctx, cst)
|
||||||
|
|
||||||
|
tree, err := state.LoadStateTree(cst, sroot)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var msigActorsInfo []msigBriefInfo
|
var msigActorsInfo []msigBriefInfo
|
||||||
for _, actor := range actors {
|
err = tree.ForEach(func(addr address.Address, act *types.Actor) error {
|
||||||
|
if builtin.IsMultisigActor(act.Code) {
|
||||||
act, err := api.StateGetActor(ctx, actor, ts.Key())
|
ms, err := multisig.Load(store, act)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if act.Code == msigCid {
|
|
||||||
|
|
||||||
actorState, err := api.StateReadState(ctx, actor, ts.Key())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
stateI, ok := actorState.State.(map[string]interface{})
|
signers, _ := ms.Signers()
|
||||||
if !ok {
|
threshold, _ := ms.Threshold()
|
||||||
return xerrors.Errorf("fail to map msig state")
|
|
||||||
}
|
|
||||||
|
|
||||||
signersI, _ := stateI["Signers"]
|
|
||||||
signers := signersI.([]interface{})
|
|
||||||
thresholdI, _ := stateI["NumApprovalsThreshold"]
|
|
||||||
threshold := thresholdI.(float64)
|
|
||||||
info := msigBriefInfo{
|
info := msigBriefInfo{
|
||||||
ID: actor,
|
ID: addr,
|
||||||
Signer: signers,
|
Signer: signers,
|
||||||
Balance: actorState.Balance,
|
Balance: act.Balance,
|
||||||
Threshold: threshold,
|
Threshold: threshold,
|
||||||
}
|
}
|
||||||
msigActorsInfo = append(msigActorsInfo, info)
|
msigActorsInfo = append(msigActorsInfo, info)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
|
})
|
||||||
out, err := json.MarshalIndent(msigActorsInfo, "", " ")
|
out, err := json.MarshalIndent(msigActorsInfo, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user