lotus/cmd/lotus-shed/msig.go

136 lines
3.0 KiB
Go
Raw Normal View History

2022-09-15 07:34:24 +00:00
package main
import (
"context"
"encoding/json"
"fmt"
2022-09-22 06:56:08 +00:00
"io"
2022-09-15 07:34:24 +00:00
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
2022-09-15 21:05:07 +00:00
"github.com/urfave/cli/v2"
2022-09-15 07:34:24 +00:00
"github.com/filecoin-project/go-address"
2022-09-15 21:05:07 +00:00
"github.com/filecoin-project/go-state-types/abi"
2023-11-14 00:06:11 +00:00
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/store"
2022-09-15 21:05:07 +00:00
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/repo"
2022-09-15 07:34:24 +00:00
)
type msigBriefInfo struct {
ID address.Address
2022-09-23 03:28:46 +00:00
Signer []address.Address
2022-09-15 07:34:24 +00:00
Balance abi.TokenAmount
2022-09-22 06:56:08 +00:00
Threshold uint64
2022-09-15 07:34:24 +00:00
}
var msigCmd = &cli.Command{
2022-09-22 06:56:08 +00:00
Name: "msig",
2022-09-15 07:34:24 +00:00
Subcommands: []*cli.Command{
multisigGetAllCmd,
},
}
var multisigGetAllCmd = &cli.Command{
2022-09-22 06:56:08 +00:00
Name: "all",
Usage: "get all multisig actor on chain with id, signers, threshold and balance at a tipset",
2022-09-22 06:56:08 +00:00
ArgsUsage: "[state root]",
2022-09-15 07:34:24 +00:00
Flags: []cli.Flag{
2022-09-22 06:56:08 +00:00
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
2022-09-15 07:34:24 +00:00
},
},
Action: func(cctx *cli.Context) error {
2022-09-22 06:56:08 +00:00
ctx := context.TODO()
if !cctx.Args().Present() {
return fmt.Errorf("must pass state root")
2022-09-15 07:34:24 +00:00
}
2022-09-22 06:56:08 +00:00
sroot, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse input: %w", err)
}
2022-09-15 07:34:24 +00:00
2022-09-22 06:56:08 +00:00
fsrepo, err := repo.NewFS(cctx.String("repo"))
2022-09-15 07:34:24 +00:00
if err != nil {
return err
}
2022-09-22 06:56:08 +00:00
lkrepo, err := fsrepo.Lock(repo.FullNode)
2022-09-15 07:34:24 +00:00
if err != nil {
return err
}
2022-09-22 06:56:08 +00:00
defer lkrepo.Close() //nolint:errcheck
bs, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
2022-09-15 07:34:24 +00:00
if err != nil {
2022-09-22 06:56:08 +00:00
return fmt.Errorf("failed to open blockstore: %w", err)
2022-09-15 07:34:24 +00:00
}
2022-09-22 06:56:08 +00:00
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
2022-09-15 07:34:24 +00:00
}
2022-09-22 06:56:08 +00:00
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
defer cs.Close() //nolint:errcheck
2022-09-15 07:34:24 +00:00
2022-09-22 06:56:08 +00:00
cst := cbor.NewCborStore(bs)
store := adt.WrapStore(ctx, cst)
2022-09-15 07:34:24 +00:00
2022-09-22 06:56:08 +00:00
tree, err := state.LoadStateTree(cst, sroot)
if err != nil {
return err
}
2022-09-15 07:34:24 +00:00
2022-09-22 06:56:08 +00:00
var msigActorsInfo []msigBriefInfo
err = tree.ForEach(func(addr address.Address, act *types.Actor) error {
if builtin.IsMultisigActor(act.Code) {
ms, err := multisig.Load(store, act)
2022-09-15 07:34:24 +00:00
if err != nil {
return fmt.Errorf("load msig failed %v", err)
2022-09-15 07:34:24 +00:00
}
2022-09-22 06:56:08 +00:00
signers, _ := ms.Signers()
threshold, _ := ms.Threshold()
2022-09-15 07:34:24 +00:00
info := msigBriefInfo{
2022-09-22 06:56:08 +00:00
ID: addr,
2022-09-15 07:34:24 +00:00
Signer: signers,
2022-09-22 06:56:08 +00:00
Balance: act.Balance,
2022-09-15 07:34:24 +00:00
Threshold: threshold,
}
msigActorsInfo = append(msigActorsInfo, info)
2022-09-22 06:56:08 +00:00
2022-09-15 07:34:24 +00:00
}
2022-09-22 06:56:08 +00:00
return nil
})
2022-11-25 21:36:33 +00:00
if err != nil {
return err
}
2022-09-15 07:34:24 +00:00
out, err := json.MarshalIndent(msigActorsInfo, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}