From 89dc87d890a8794718e050833c8f8aaae3113e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 22 Aug 2019 00:14:58 +0200 Subject: [PATCH] use StateMinerPower in 'state power' cmd --- cli/state.go | 38 ++++++++------------------------------ node/impl/full/state.go | 36 ++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/cli/state.go b/cli/state.go index 9f1f8851b..ce799093f 100644 --- a/cli/state.go +++ b/cli/state.go @@ -2,13 +2,10 @@ package cli import ( "fmt" - "math/big" "gopkg.in/urfave/cli.v2" - "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" - types "github.com/filecoin-project/go-lotus/chain/types" ) var stateCmd = &cli.Command{ @@ -32,43 +29,24 @@ var statePowerCmd = &cli.Command{ ctx := ReqContext(cctx) - var msg *types.Message + var maddr address.Address if cctx.Args().Present() { - maddr, err := address.NewFromString(cctx.Args().First()) + maddr, err = address.NewFromString(cctx.Args().First()) if err != nil { return err } - - enc, err := actors.SerializeParams(&actors.PowerLookupParams{ - Miner: maddr, - }) - if err != nil { - return err - } - - msg = &types.Message{ - To: actors.StorageMarketAddress, - From: actors.StorageMarketAddress, - Method: actors.SMAMethods.PowerLookup, - Params: enc, - } - } else { - msg = &types.Message{ - To: actors.StorageMarketAddress, - From: actors.StorageMarketAddress, - Method: actors.SMAMethods.GetTotalStorage, - } } - ret, err := api.ChainCall(ctx, msg, nil) + power, err := api.StateMinerPower(ctx, maddr, nil) if err != nil { return err } - if ret.ExitCode != 0 { - return fmt.Errorf("call to get power failed: %d", ret.ExitCode) + + res := power.TotalPower + if cctx.Args().Present() { + res = power.MinerPower } - v := big.NewInt(0).SetBytes(ret.Return) - fmt.Println(v.String()) + fmt.Println(res.String()) return nil }, } diff --git a/node/impl/full/state.go b/node/impl/full/state.go index c27abe68e..bcf518837 100644 --- a/node/impl/full/state.go +++ b/node/impl/full/state.go @@ -148,23 +148,27 @@ func (a *StateAPI) StateMinerPower(ctx context.Context, maddr address.Address, t return api.MinerPower{}, err } + var mpow types.BigInt + + if maddr != address.Undef { + ret, err := vm.Call(ctx, a.Chain, &types.Message{ + From: maddr, + To: actors.StorageMarketAddress, + Method: actors.SMAMethods.PowerLookup, + Params: enc, + }, ts) + if err != nil { + return api.MinerPower{}, xerrors.Errorf("failed to get miner power from chain: %w", err) + } + if ret.ExitCode != 0 { + return api.MinerPower{}, xerrors.Errorf("failed to get miner power from chain (exit code %d)", ret.ExitCode) + } + + mpow = types.BigFromBytes(ret.Return) + } + ret, err := vm.Call(ctx, a.Chain, &types.Message{ - From: maddr, - To: actors.StorageMarketAddress, - Method: actors.SMAMethods.PowerLookup, - Params: enc, - }, ts) - if err != nil { - return api.MinerPower{}, xerrors.Errorf("failed to get miner power from chain: %w", err) - } - if ret.ExitCode != 0 { - return api.MinerPower{}, xerrors.Errorf("failed to get miner power from chain (exit code %d)", ret.ExitCode) - } - - mpow := types.BigFromBytes(ret.Return) - - ret, err = vm.Call(ctx, a.Chain, &types.Message{ - From: maddr, + From: actors.StorageMarketAddress, To: actors.StorageMarketAddress, Method: actors.SMAMethods.GetTotalStorage, }, ts)