-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9322 from filecoin-project/jen/msigall
shed: util: get all msig
- Loading branch information
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ func main() { | |
migrationsCmd, | ||
diffCmd, | ||
itestdCmd, | ||
msigCmd, | ||
} | ||
|
||
app := &cli.App{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/ipfs/go-cid" | ||
cbor "github.com/ipfs/go-ipld-cbor" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/abi" | ||
|
||
"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" | ||
"github.com/filecoin-project/lotus/chain/types" | ||
"github.com/filecoin-project/lotus/node/repo" | ||
) | ||
|
||
type msigBriefInfo struct { | ||
ID address.Address | ||
Signer interface{} | ||
Balance abi.TokenAmount | ||
Threshold uint64 | ||
} | ||
|
||
var msigCmd = &cli.Command{ | ||
Name: "msig", | ||
Subcommands: []*cli.Command{ | ||
multisigGetAllCmd, | ||
}, | ||
} | ||
|
||
var multisigGetAllCmd = &cli.Command{ | ||
Name: "all", | ||
Usage: "get all multisig actor on chain with id, signers, threshold and balance at a tipset", | ||
ArgsUsage: "[state root]", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "repo", | ||
Value: "~/.lotus", | ||
}, | ||
}, | ||
Action: func(cctx *cli.Context) error { | ||
ctx := context.TODO() | ||
if !cctx.Args().Present() { | ||
return fmt.Errorf("must pass state root") | ||
} | ||
|
||
sroot, err := cid.Decode(cctx.Args().First()) | ||
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 | ||
|
||
cst := cbor.NewCborStore(bs) | ||
store := adt.WrapStore(ctx, cst) | ||
|
||
tree, err := state.LoadStateTree(cst, sroot) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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) | ||
if err != nil { | ||
return fmt.Errorf("load msig failed %v", err) | ||
|
||
} | ||
|
||
signers, _ := ms.Signers() | ||
threshold, _ := ms.Threshold() | ||
info := msigBriefInfo{ | ||
ID: addr, | ||
Signer: signers, | ||
Balance: act.Balance, | ||
Threshold: threshold, | ||
} | ||
msigActorsInfo = append(msigActorsInfo, info) | ||
|
||
} | ||
return nil | ||
}) | ||
out, err := json.MarshalIndent(msigActorsInfo, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(out)) | ||
return nil | ||
}, | ||
} |