lotus/cmd/lotus-sim/simulation/power.go

55 lines
1.5 KiB
Go
Raw Normal View History

2021-05-19 00:01:30 +00:00
package simulation
import (
"context"
"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"
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
)
// Load all power claims at the given height.
func (sim *Simulation) loadClaims(ctx context.Context, height abi.ChainEpoch) (map[address.Address]power.Claim, error) {
powerTable := make(map[address.Address]power.Claim)
store := sim.Chainstore.ActorStore(ctx)
ts, err := sim.Chainstore.GetTipsetByHeight(ctx, height, sim.head, true)
if err != nil {
return nil, xerrors.Errorf("when projecting growth, failed to lookup lookback epoch: %w", err)
}
powerActor, err := sim.StateManager.LoadActor(ctx, power.Address, ts)
2021-05-19 00:01:30 +00:00
if err != nil {
return nil, err
}
powerState, err := power.Load(store, powerActor)
if err != nil {
return nil, err
}
err = powerState.ForEachClaim(func(miner address.Address, claim power.Claim) error {
powerTable[miner] = claim
return nil
})
if err != nil {
return nil, err
}
return powerTable, nil
}
// Compute the number of sectors a miner has from their power claim.
func sectorsFromClaim(sectorSize abi.SectorSize, c power.Claim) int64 {
if c.RawBytePower.Int == nil {
return 0
}
sectorCount := big.Div(c.RawBytePower, big.NewIntUnsigned(uint64(sectorSize)))
if !sectorCount.IsInt64() {
panic("impossible number of sectors")
}
return sectorCount.Int64()
}