2020-09-05 03:01:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-24 21:30:11 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/docker/go-units"
|
2020-09-29 04:24:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin"
|
2020-09-24 21:30:11 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/reward"
|
2020-09-05 03:01:36 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2020-09-24 21:30:11 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-09-05 03:01:36 +00:00
|
|
|
"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/big"
|
2020-09-21 20:43:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2020-09-05 03:01:36 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"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/extern/sector-storage/ffiwrapper"
|
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type accountInfo struct {
|
|
|
|
Address address.Address
|
|
|
|
Balance types.FIL
|
|
|
|
Type string
|
|
|
|
Power abi.StoragePower
|
|
|
|
Worker address.Address
|
|
|
|
Owner address.Address
|
|
|
|
InitialPledge types.FIL
|
|
|
|
PreCommits types.FIL
|
|
|
|
LockedFunds types.FIL
|
|
|
|
Sectors uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var auditsCmd = &cli.Command{
|
|
|
|
Name: "audits",
|
|
|
|
Description: "a collection of utilities for auditing the filecoin chain",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
chainBalanceCmd,
|
|
|
|
chainBalanceStateCmd,
|
2020-09-24 21:30:11 +00:00
|
|
|
chainPledgeCmd,
|
2020-09-05 03:01:36 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainBalanceCmd = &cli.Command{
|
|
|
|
Name: "chain-balances",
|
|
|
|
Description: "Produces a csv file of all account balances",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "tipset",
|
|
|
|
Usage: "specify tipset to start from",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer closer()
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
|
|
|
ts, err := lcli.LoadTipSet(ctx, cctx, api)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tsk := ts.Key()
|
|
|
|
actors, err := api.StateListActors(ctx, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var infos []accountInfo
|
|
|
|
for _, addr := range actors {
|
|
|
|
act, err := api.StateGetActor(ctx, addr, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ai := accountInfo{
|
|
|
|
Address: addr,
|
|
|
|
Balance: types.FIL(act.Balance),
|
|
|
|
Type: string(act.Code.Hash()[2:]),
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:24:38 +00:00
|
|
|
if builtin.IsStorageMinerActor(act.Code) {
|
2020-09-05 03:01:36 +00:00
|
|
|
pow, err := api.StateMinerPower(ctx, addr, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get power: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ai.Power = pow.MinerPower.RawBytePower
|
|
|
|
info, err := api.StateMinerInfo(ctx, addr, tsk)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get miner info: %w", err)
|
|
|
|
}
|
|
|
|
ai.Worker = info.Worker
|
|
|
|
ai.Owner = info.Owner
|
|
|
|
|
|
|
|
}
|
|
|
|
infos = append(infos, ai)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Address,Balance,Type,Power,Worker,Owner\n")
|
|
|
|
for _, acc := range infos {
|
|
|
|
fmt.Printf("%s,%s,%s,%s,%s,%s\n", acc.Address, acc.Balance, acc.Type, acc.Power, acc.Worker, acc.Owner)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainBalanceStateCmd = &cli.Command{
|
|
|
|
Name: "stateroot-balances",
|
|
|
|
Description: "Produces a csv file of all account balances from a given stateroot",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "miner-info",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
|
|
|
|
|
ds, err := lkrepo.Datastore("/chain")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mds, err := lkrepo.Datastore("/metadata")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := blockstore.NewBlockstore(ds)
|
|
|
|
|
|
|
|
cs := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))
|
|
|
|
|
|
|
|
cst := cbor.NewCborStore(bs)
|
2020-09-21 20:43:47 +00:00
|
|
|
store := adt.WrapStore(ctx, cst)
|
2020-09-05 03:01:36 +00:00
|
|
|
|
|
|
|
sm := stmgr.NewStateManager(cs)
|
|
|
|
|
2020-09-14 22:43:12 +00:00
|
|
|
tree, err := state.LoadStateTree(cst, sroot)
|
2020-09-05 03:01:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
minerInfo := cctx.Bool("miner-info")
|
|
|
|
|
|
|
|
var infos []accountInfo
|
|
|
|
err = tree.ForEach(func(addr address.Address, act *types.Actor) error {
|
|
|
|
|
|
|
|
ai := accountInfo{
|
|
|
|
Address: addr,
|
|
|
|
Balance: types.FIL(act.Balance),
|
|
|
|
Type: string(act.Code.Hash()[2:]),
|
|
|
|
Power: big.NewInt(0),
|
|
|
|
LockedFunds: types.FIL(big.NewInt(0)),
|
|
|
|
InitialPledge: types.FIL(big.NewInt(0)),
|
|
|
|
PreCommits: types.FIL(big.NewInt(0)),
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:24:38 +00:00
|
|
|
if minerInfo && builtin.IsStorageMinerActor(act.Code) {
|
2020-09-16 05:47:24 +00:00
|
|
|
pow, _, _, err := stmgr.GetPowerRaw(ctx, sm, sroot, addr)
|
2020-09-05 03:01:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get power: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ai.Power = pow.RawBytePower
|
|
|
|
|
2020-09-21 20:43:47 +00:00
|
|
|
st, err := miner.Load(store, act)
|
|
|
|
if err != nil {
|
2020-09-05 03:01:36 +00:00
|
|
|
return xerrors.Errorf("failed to read miner state: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-21 20:43:47 +00:00
|
|
|
liveSectorCount, err := st.NumLiveSectors()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to compute live sector count: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lockedFunds, err := st.LockedFunds()
|
2020-09-05 03:01:36 +00:00
|
|
|
if err != nil {
|
2020-09-21 20:43:47 +00:00
|
|
|
return xerrors.Errorf("failed to compute locked funds: %w", err)
|
2020-09-05 03:01:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 20:43:47 +00:00
|
|
|
ai.InitialPledge = types.FIL(lockedFunds.InitialPledgeRequirement)
|
|
|
|
ai.LockedFunds = types.FIL(lockedFunds.VestingFunds)
|
|
|
|
ai.PreCommits = types.FIL(lockedFunds.PreCommitDeposits)
|
|
|
|
ai.Sectors = liveSectorCount
|
2020-09-05 03:01:36 +00:00
|
|
|
|
2020-09-21 20:43:47 +00:00
|
|
|
minfo, err := st.Info()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get miner info: %w", err)
|
2020-09-05 03:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ai.Worker = minfo.Worker
|
|
|
|
ai.Owner = minfo.Owner
|
|
|
|
}
|
|
|
|
infos = append(infos, ai)
|
|
|
|
return nil
|
|
|
|
})
|
2020-09-07 23:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to loop over actors: %w", err)
|
|
|
|
}
|
2020-09-05 03:01:36 +00:00
|
|
|
|
|
|
|
if minerInfo {
|
|
|
|
fmt.Printf("Address,Balance,Type,Sectors,Worker,Owner,InitialPledge,Locked,PreCommits\n")
|
|
|
|
for _, acc := range infos {
|
|
|
|
fmt.Printf("%s,%s,%s,%d,%s,%s,%s,%s,%s\n", acc.Address, acc.Balance, acc.Type, acc.Sectors, acc.Worker, acc.Owner, acc.InitialPledge, acc.LockedFunds, acc.PreCommits)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Address,Balance,Type\n")
|
|
|
|
for _, acc := range infos {
|
|
|
|
fmt.Printf("%s,%s,%s\n", acc.Address, acc.Balance, acc.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-09-24 21:30:11 +00:00
|
|
|
|
|
|
|
var chainPledgeCmd = &cli.Command{
|
|
|
|
Name: "stateroot-pledge",
|
|
|
|
Description: "Calculate sector pledge numbers",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[stateroot epoch]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
logging.SetLogLevel("badger", "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)
|
|
|
|
}
|
|
|
|
|
|
|
|
epoch, err := strconv.ParseInt(cctx.Args().Get(1), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("parsing epoch arg: %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
|
|
|
|
|
|
|
|
ds, err := lkrepo.Datastore("/chain")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mds, err := lkrepo.Datastore("/metadata")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := blockstore.NewBlockstore(ds)
|
|
|
|
|
|
|
|
cs := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))
|
|
|
|
|
|
|
|
cst := cbor.NewCborStore(bs)
|
|
|
|
store := adt.WrapStore(ctx, cst)
|
|
|
|
|
|
|
|
sm := stmgr.NewStateManager(cs)
|
|
|
|
|
|
|
|
state, err := state.LoadStateTree(cst, sroot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-09-29 04:24:38 +00:00
|
|
|
powerSmoothed builtin.FilterEstimate
|
2020-09-24 21:30:11 +00:00
|
|
|
pledgeCollateral abi.TokenAmount
|
|
|
|
)
|
|
|
|
if act, err := state.GetActor(power.Address); err != nil {
|
|
|
|
return xerrors.Errorf("loading miner actor: %w", err)
|
|
|
|
} else if s, err := power.Load(store, act); err != nil {
|
|
|
|
return xerrors.Errorf("loading power actor state: %w", err)
|
|
|
|
} else if p, err := s.TotalPowerSmoothed(); err != nil {
|
|
|
|
return xerrors.Errorf("failed to determine total power: %w", err)
|
|
|
|
} else if c, err := s.TotalLocked(); err != nil {
|
|
|
|
return xerrors.Errorf("failed to determine pledge collateral: %w", err)
|
|
|
|
} else {
|
|
|
|
powerSmoothed = p
|
|
|
|
pledgeCollateral = c
|
|
|
|
}
|
|
|
|
|
|
|
|
circ, err := sm.GetCirculatingSupplyDetailed(ctx, abi.ChainEpoch(epoch), state)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("(real) circulating supply: ", types.FIL(circ.FilCirculating))
|
|
|
|
if circ.FilCirculating.LessThan(big.Zero()) {
|
|
|
|
circ.FilCirculating = big.Zero()
|
|
|
|
}
|
|
|
|
|
|
|
|
rewardActor, err := state.GetActor(reward.Address)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading miner actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rewardState, err := reward.Load(store, rewardActor)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading reward actor state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("FilVested", types.FIL(circ.FilVested))
|
|
|
|
fmt.Println("FilMined", types.FIL(circ.FilMined))
|
|
|
|
fmt.Println("FilBurnt", types.FIL(circ.FilBurnt))
|
|
|
|
fmt.Println("FilLocked", types.FIL(circ.FilLocked))
|
|
|
|
fmt.Println("FilCirculating", types.FIL(circ.FilCirculating))
|
|
|
|
|
|
|
|
for _, sectorWeight := range []abi.StoragePower{
|
|
|
|
types.NewInt(32 << 30),
|
|
|
|
types.NewInt(64 << 30),
|
|
|
|
types.NewInt(32 << 30 * 10),
|
|
|
|
types.NewInt(64 << 30 * 10),
|
|
|
|
} {
|
|
|
|
initialPledge, err := rewardState.InitialPledgeForPower(
|
|
|
|
sectorWeight,
|
|
|
|
pledgeCollateral,
|
|
|
|
&powerSmoothed,
|
|
|
|
circ.FilCirculating,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("calculating initial pledge: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("IP ", units.HumanSize(float64(sectorWeight.Uint64())), types.FIL(initialPledge))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|