use StateMinerPower in 'state power' cmd

This commit is contained in:
Łukasz Magiera 2019-08-22 00:14:58 +02:00
parent fd2f0ed4f2
commit 89dc87d890
2 changed files with 28 additions and 46 deletions

View File

@ -2,13 +2,10 @@ package cli
import ( import (
"fmt" "fmt"
"math/big"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
types "github.com/filecoin-project/go-lotus/chain/types"
) )
var stateCmd = &cli.Command{ var stateCmd = &cli.Command{
@ -32,43 +29,24 @@ var statePowerCmd = &cli.Command{
ctx := ReqContext(cctx) ctx := ReqContext(cctx)
var msg *types.Message var maddr address.Address
if cctx.Args().Present() { if cctx.Args().Present() {
maddr, err := address.NewFromString(cctx.Args().First()) maddr, err = address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
}
power, err := api.StateMinerPower(ctx, maddr, nil)
if err != nil { if err != nil {
return err return err
} }
enc, err := actors.SerializeParams(&actors.PowerLookupParams{ res := power.TotalPower
Miner: maddr, if cctx.Args().Present() {
}) res = power.MinerPower
if err != nil {
return err
} }
msg = &types.Message{ fmt.Println(res.String())
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)
if err != nil {
return err
}
if ret.ExitCode != 0 {
return fmt.Errorf("call to get power failed: %d", ret.ExitCode)
}
v := big.NewInt(0).SetBytes(ret.Return)
fmt.Println(v.String())
return nil return nil
}, },
} }

View File

@ -148,6 +148,9 @@ func (a *StateAPI) StateMinerPower(ctx context.Context, maddr address.Address, t
return api.MinerPower{}, err return api.MinerPower{}, err
} }
var mpow types.BigInt
if maddr != address.Undef {
ret, err := vm.Call(ctx, a.Chain, &types.Message{ ret, err := vm.Call(ctx, a.Chain, &types.Message{
From: maddr, From: maddr,
To: actors.StorageMarketAddress, To: actors.StorageMarketAddress,
@ -161,10 +164,11 @@ func (a *StateAPI) StateMinerPower(ctx context.Context, maddr address.Address, t
return api.MinerPower{}, xerrors.Errorf("failed to get miner power from chain (exit code %d)", ret.ExitCode) return api.MinerPower{}, xerrors.Errorf("failed to get miner power from chain (exit code %d)", ret.ExitCode)
} }
mpow := types.BigFromBytes(ret.Return) mpow = types.BigFromBytes(ret.Return)
}
ret, err = vm.Call(ctx, a.Chain, &types.Message{ ret, err := vm.Call(ctx, a.Chain, &types.Message{
From: maddr, From: actors.StorageMarketAddress,
To: actors.StorageMarketAddress, To: actors.StorageMarketAddress,
Method: actors.SMAMethods.GetTotalStorage, Method: actors.SMAMethods.GetTotalStorage,
}, ts) }, ts)