Merge pull request #4315 from filecoin-project/feat/stats-ops

lotus-stats: optmize getting miner power
This commit is contained in:
Łukasz Magiera 2020-10-12 06:01:52 +02:00 committed by GitHub
commit 8fd9b5f235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 13 deletions

View File

@ -51,6 +51,7 @@ type State interface {
MinerPower(address.Address) (Claim, bool, error) MinerPower(address.Address) (Claim, bool, error)
MinerNominalPowerMeetsConsensusMinimum(address.Address) (bool, error) MinerNominalPowerMeetsConsensusMinimum(address.Address) (bool, error)
ListAllMiners() ([]address.Address, error) ListAllMiners() ([]address.Address, error)
ForEachClaim(func(miner address.Address, claim Claim) error) error
} }
type Claim struct { type Claim struct {

View File

@ -96,3 +96,22 @@ func (s *state0) ListAllMiners() ([]address.Address, error) {
return miners, nil return miners, nil
} }
func (s *state0) ForEachClaim(cb func(miner address.Address, claim Claim) error) error {
claims, err := adt0.AsMap(s.store, s.Claims)
if err != nil {
return err
}
var claim power0.Claim
return claims.ForEach(&claim, func(k string) error {
a, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
return cb(a, Claim{
RawBytePower: claim.RawBytePower,
QualityAdjPower: claim.QualityAdjPower,
})
})
}

View File

@ -96,3 +96,22 @@ func (s *state2) ListAllMiners() ([]address.Address, error) {
return miners, nil return miners, nil
} }
func (s *state2) ForEachClaim(cb func(miner address.Address, claim Claim) error) error {
claims, err := adt2.AsMap(s.store, s.Claims)
if err != nil {
return err
}
var claim power2.Claim
return claims.ForEach(&claim, func(k string) error {
a, err := address.NewFromBytes([]byte(k))
if err != nil {
return err
}
return cb(a, Claim{
RawBytePower: claim.RawBytePower,
QualityAdjPower: claim.QualityAdjPower,
})
})
}

View File

@ -13,6 +13,7 @@ import (
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/actors/builtin/reward" "github.com/filecoin-project/lotus/chain/actors/builtin/reward"
"github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
@ -269,25 +270,27 @@ func RecordTipsetStatePoints(ctx context.Context, api api.FullNode, pl *PointLis
p = NewPoint("chain.power", totalPower.TotalPower.QualityAdjPower.Int64()) p = NewPoint("chain.power", totalPower.TotalPower.QualityAdjPower.Int64())
pl.AddPoint(p) pl.AddPoint(p)
miners, err := api.StateListMiners(ctx, tipset.Key()) powerActor, err := api.StateGetActor(ctx, power.Address, tipset.Key())
if err != nil { if err != nil {
return err return err
} }
for _, addr := range miners { powerActorState, err := power.Load(&ApiIpldStore{ctx, api}, powerActor)
mp, err := api.StateMinerPower(ctx, addr, tipset.Key()) if err != nil {
if err != nil { return err
return err
}
if !mp.MinerPower.QualityAdjPower.IsZero() {
p = NewPoint("chain.miner_power", mp.MinerPower.QualityAdjPower.Int64())
p.AddTag("miner", addr.String())
pl.AddPoint(p)
}
} }
return nil return powerActorState.ForEachClaim(func(addr address.Address, claim power.Claim) error {
if claim.QualityAdjPower.Int64() == 0 {
return nil
}
p = NewPoint("chain.miner_power", claim.QualityAdjPower.Int64())
p.AddTag("miner", addr.String())
pl.AddPoint(p)
return nil
})
} }
type msgTag struct { type msgTag struct {