From 2fec0c2440f2d6e6ceba5c0fae6702d9e1153f35 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 5 Jan 2021 17:53:27 +0800 Subject: [PATCH 1/3] add produced blocks info --- chain/actors/builtin/reward/reward.go | 2 +- cmd/lotus-miner/info.go | 61 +++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/chain/actors/builtin/reward/reward.go b/chain/actors/builtin/reward/reward.go index ebec85517..25ab3ef6b 100644 --- a/chain/actors/builtin/reward/reward.go +++ b/chain/actors/builtin/reward/reward.go @@ -123,7 +123,7 @@ type State interface { cbor.Marshaler ThisEpochBaselinePower() (abi.StoragePower, error) - ThisEpochReward() (abi.StoragePower, error) + ThisEpochReward() (abi.TokenAmount, error) ThisEpochRewardSmoothed() (builtin.FilterEstimate, error) EffectiveBaselinePower() (abi.StoragePower, error) diff --git a/cmd/lotus-miner/info.go b/cmd/lotus-miner/info.go index f37952057..9d1fa0300 100644 --- a/cmd/lotus-miner/info.go +++ b/cmd/lotus-miner/info.go @@ -18,18 +18,20 @@ import ( cbor "github.com/ipfs/go-ipld-cbor" "github.com/filecoin-project/go-fil-markets/retrievalmarket" + "github.com/filecoin-project/go-address" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/lotus/api/v0api" sealing "github.com/filecoin-project/lotus/extern/storage-sealing" + "github.com/filecoin-project/specs-actors/actors/builtin" "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/blockstore" "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/actors/adt" - "github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin/miner" + "github.com/filecoin-project/lotus/chain/actors/builtin/reward" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" ) @@ -45,6 +47,10 @@ var infoCmd = &cli.Command{ Name: "hide-sectors-info", Usage: "hide sectors info", }, + &cli.IntFlag{ + Name: "blocks", + Usage: "Log of produced newest blocks and rewards(Miner Fee excluded)", + }, }, Action: infoCmdAct, } @@ -141,6 +147,7 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full } tbs := blockstore.NewTieredBstore(blockstore.NewAPIBlockstore(fullapi), blockstore.NewMemory()) + mas, err := miner.Load(adt.WrapStore(ctx, cbor.NewCborStore(tbs)), mact) if err != nil { return err @@ -148,6 +155,7 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full // Sector size mi, err := fullapi.StateMinerInfo(ctx, maddr, types.EmptyTSK) + if err != nil { return err } @@ -178,6 +186,7 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full ), ) secCounts, err := fullapi.StateMinerSectorCount(ctx, maddr, types.EmptyTSK) + if err != nil { return err } @@ -275,6 +284,7 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full colorTokenAmount(" Available: %s\n", availBalance) mb, err := fullapi.StateMarketBalance(ctx, maddr, types.EmptyTSK) + if err != nil { return xerrors.Errorf("getting market balance: %w", err) } @@ -285,6 +295,7 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full colorTokenAmount(" Available: %s\n", big.Sub(mb.Escrow, mb.Locked)) wb, err := fullapi.WalletBalance(ctx, mi.Worker) + if err != nil { return xerrors.Errorf("getting worker balance: %w", err) } @@ -315,6 +326,13 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full } } + if cctx.IsSet("blocks") { + fmt.Println("Produced newest blocks:") + err = producedBlocks(ctx, cctx.Int("blocks"), head, maddr, fullapi) + if err != nil { + return err + } + } // TODO: grab actr state / info // * Sealed sectors (count / bytes) // * Power @@ -484,8 +502,8 @@ func init() { } } -func sectorsInfo(ctx context.Context, napi api.StorageMiner) error { - summary, err := napi.SectorsSummary(ctx) +func sectorsInfo(ctx context.Context, mapi api.StorageMiner) error { + summary, err := mapi.SectorsSummary(ctx) if err != nil { return err } @@ -523,3 +541,40 @@ func colorTokenAmount(format string, amount abi.TokenAmount) { color.Red(format, types.FIL(amount).Short()) } } + +func producedBlocks(ctx context.Context, count int, head *types.TipSet, maddr address.Address, napi v0api.FullNode) error { + var err error + ts := head + fmt.Printf(" Epoch | Block ID | Reward\n") + for count > 0 { + tsk := ts.Key() + bhs := ts.Blocks() + for _, bh := range bhs { + if bh.Miner == maddr { + rewardActor, err := napi.StateGetActor(ctx, reward.Address, tsk) + if err != nil { + return err + } + + rewardActorState, err := reward.Load(cw_util.NewAPIIpldStore(ctx, napi), rewardActor) + if err != nil { + return err + } + blockReward, err := rewardActorState.ThisEpochReward() + if err != nil { + return err + } + fmt.Printf("%8d | %s | %s\n", ts.Height(), bh.Cid(), + types.BigDiv(types.BigMul(types.NewInt(uint64(bh.ElectionProof.WinCount)), + blockReward), types.NewInt(uint64(builtin.ExpectedLeadersPerEpoch)))) + count-- + } + } + tsk = ts.Parents() + ts, err = napi.ChainGetTipSet(ctx, tsk) + if err != nil { + return err + } + } + return nil +} From 2744bb4e5a66e3788af887569222064b178d6777 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 4 Aug 2021 15:49:35 +0800 Subject: [PATCH 2/3] update --- cmd/lotus-miner/info.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/lotus-miner/info.go b/cmd/lotus-miner/info.go index 9d1fa0300..87abd474a 100644 --- a/cmd/lotus-miner/info.go +++ b/cmd/lotus-miner/info.go @@ -17,8 +17,8 @@ import ( cbor "github.com/ipfs/go-ipld-cbor" - "github.com/filecoin-project/go-fil-markets/retrievalmarket" "github.com/filecoin-project/go-address" + "github.com/filecoin-project/go-fil-markets/retrievalmarket" "github.com/filecoin-project/go-fil-markets/storagemarket" "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/big" @@ -34,6 +34,7 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin/reward" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" + "github.com/filecoin-project/lotus/tools/stats" ) var infoCmd = &cli.Command{ @@ -328,7 +329,7 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full if cctx.IsSet("blocks") { fmt.Println("Produced newest blocks:") - err = producedBlocks(ctx, cctx.Int("blocks"), head, maddr, fullapi) + err = producedBlocks(ctx, cctx.Int("blocks"), maddr, fullapi) if err != nil { return err } @@ -542,8 +543,12 @@ func colorTokenAmount(format string, amount abi.TokenAmount) { } } -func producedBlocks(ctx context.Context, count int, head *types.TipSet, maddr address.Address, napi v0api.FullNode) error { +func producedBlocks(ctx context.Context, count int, maddr address.Address, napi v0api.FullNode) error { var err error + head, err := napi.ChainHead(ctx) + if err != nil { + return err + } ts := head fmt.Printf(" Epoch | Block ID | Reward\n") for count > 0 { @@ -556,7 +561,7 @@ func producedBlocks(ctx context.Context, count int, head *types.TipSet, maddr ad return err } - rewardActorState, err := reward.Load(cw_util.NewAPIIpldStore(ctx, napi), rewardActor) + rewardActorState, err := reward.Load(stats.NewApiIpldStore(ctx, napi), rewardActor) if err != nil { return err } From 22d75f484376b7a37d9cf6005dbf10da8f6e8d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 27 Aug 2021 13:36:40 +0200 Subject: [PATCH 3/3] Tweak miner info --blocks output --- chain/actors/builtin/reward/reward.go | 2 +- cmd/lotus-miner/info.go | 26 +++++++++++++++++++++++--- documentation/en/cli-lotus-miner.md | 1 + 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/chain/actors/builtin/reward/reward.go b/chain/actors/builtin/reward/reward.go index 25ab3ef6b..ebec85517 100644 --- a/chain/actors/builtin/reward/reward.go +++ b/chain/actors/builtin/reward/reward.go @@ -123,7 +123,7 @@ type State interface { cbor.Marshaler ThisEpochBaselinePower() (abi.StoragePower, error) - ThisEpochReward() (abi.TokenAmount, error) + ThisEpochReward() (abi.StoragePower, error) ThisEpochRewardSmoothed() (builtin.FilterEstimate, error) EffectiveBaselinePower() (abi.StoragePower, error) diff --git a/cmd/lotus-miner/info.go b/cmd/lotus-miner/info.go index 14af8a27d..7be9e1fe1 100644 --- a/cmd/lotus-miner/info.go +++ b/cmd/lotus-miner/info.go @@ -12,6 +12,7 @@ import ( "time" "github.com/fatih/color" + "github.com/mattn/go-isatty" "github.com/urfave/cli/v2" "golang.org/x/xerrors" @@ -567,13 +568,23 @@ func producedBlocks(ctx context.Context, count int, maddr address.Address, napi tbs := blockstore.NewTieredBstore(blockstore.NewAPIBlockstore(napi), blockstore.NewMemory()) + tty := isatty.IsTerminal(os.Stderr.Fd()) + ts := head fmt.Printf(" Epoch | Block ID | Reward\n") for count > 0 { tsk := ts.Key() bhs := ts.Blocks() for _, bh := range bhs { + if ctx.Err() != nil { + return ctx.Err() + } + if bh.Miner == maddr { + if tty { + _, _ = fmt.Fprint(os.Stderr, "\r\x1b[0K") + } + rewardActor, err := napi.StateGetActor(ctx, reward.Address, tsk) if err != nil { return err @@ -587,10 +598,14 @@ func producedBlocks(ctx context.Context, count int, maddr address.Address, napi if err != nil { return err } - fmt.Printf("%8d | %s | %s\n", ts.Height(), bh.Cid(), - types.BigDiv(types.BigMul(types.NewInt(uint64(bh.ElectionProof.WinCount)), - blockReward), types.NewInt(uint64(builtin.ExpectedLeadersPerEpoch)))) + + minerReward := types.BigDiv(types.BigMul(types.NewInt(uint64(bh.ElectionProof.WinCount)), + blockReward), types.NewInt(uint64(builtin.ExpectedLeadersPerEpoch))) + + fmt.Printf("%8d | %s | %s\n", ts.Height(), bh.Cid(), types.FIL(minerReward)) count-- + } else if tty && bh.Height%120 == 0 { + _, _ = fmt.Fprintf(os.Stderr, "\r\x1b[0KChecking epoch %s", lcli.EpochTime(head.Height(), bh.Height)) } } tsk = ts.Parents() @@ -599,5 +614,10 @@ func producedBlocks(ctx context.Context, count int, maddr address.Address, napi return err } } + + if tty { + _, _ = fmt.Fprint(os.Stderr, "\r\x1b[0K") + } + return nil } diff --git a/documentation/en/cli-lotus-miner.md b/documentation/en/cli-lotus-miner.md index f69c8b144..c96b3140e 100644 --- a/documentation/en/cli-lotus-miner.md +++ b/documentation/en/cli-lotus-miner.md @@ -430,6 +430,7 @@ COMMANDS: OPTIONS: --hide-sectors-info hide sectors info (default: false) + --blocks value Log of produced newest blocks and rewards(Miner Fee excluded) (default: 0) --help, -h show help (default: false) --version, -v print the version (default: false)