Skip to content

Commit

Permalink
convert lotus-shed balances
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Sep 21, 2020
1 parent a41bf74 commit 916421b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
10 changes: 10 additions & 0 deletions chain/actors/builtin/miner/miner.go
Expand Up @@ -18,6 +18,15 @@ import (
"github.com/filecoin-project/lotus/chain/types"
)

// Returns true if the specified actor code ID is a miner actor.
func Is(code cid.Cid) bool {
switch code {
case builtin0.StorageMinerActorCodeID:
return true
}
return false
}

func Load(store adt.Store, act *types.Actor) (st State, err error) {
switch act.Code {
case builtin0.StorageMinerActorCodeID:
Expand Down Expand Up @@ -46,6 +55,7 @@ type State interface {
GetSectorExpiration(abi.SectorNumber) (*SectorExpiration, error)
GetPrecommittedSector(abi.SectorNumber) (*SectorPreCommitOnChainInfo, error)
LoadSectors(sectorNos *bitfield.BitField) ([]*SectorOnChainInfo, error)
NumLiveSectors() (uint64, error)
IsAllocated(abi.SectorNumber) (bool, error)

LoadDeadline(idx uint64) (Deadline, error)
Expand Down
15 changes: 15 additions & 0 deletions chain/actors/builtin/miner/v0.go
Expand Up @@ -79,6 +79,21 @@ func (s *state0) FindSector(num abi.SectorNumber) (*SectorLocation, error) {
}, nil
}

func (s *state0) NumLiveSectors() (uint64, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
return 0, err
}
var total uint64
if err := dls.ForEach(s.store, func(dlIdx uint64, dl *miner0.Deadline) error {
total += dl.LiveSectors
return nil
}); err != nil {
return 0, err
}
return total, nil
}

// GetSectorExpiration returns the effective expiration of the given sector.
//
// If the sector isn't found or has already been terminated, this method returns
Expand Down
37 changes: 21 additions & 16 deletions cmd/lotus-shed/balances.go
Expand Up @@ -12,6 +12,8 @@ import (
"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/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
Expand All @@ -21,9 +23,6 @@ import (
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/lib/blockstore"
"github.com/filecoin-project/lotus/node/repo"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
"github.com/filecoin-project/specs-actors/actors/util/adt"
)

type accountInfo struct {
Expand Down Expand Up @@ -90,7 +89,7 @@ var chainBalanceCmd = &cli.Command{
Type: string(act.Code.Hash()[2:]),
}

if act.Code == builtin.StorageMinerActorCodeID {
if miner.Is(act.Code) {
pow, err := api.StateMinerPower(ctx, addr, tsk)
if err != nil {
return xerrors.Errorf("failed to get power: %w", err)
Expand Down Expand Up @@ -167,6 +166,7 @@ var chainBalanceStateCmd = &cli.Command{
cs := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))

cst := cbor.NewCborStore(bs)
store := adt.WrapStore(ctx, cst)

sm := stmgr.NewStateManager(cs)

Expand All @@ -191,32 +191,37 @@ var chainBalanceStateCmd = &cli.Command{
PreCommits: types.FIL(big.NewInt(0)),
}

if act.Code == builtin.StorageMinerActorCodeID && minerInfo {
if minerInfo && miner.Is(act.Code) {
pow, _, _, err := stmgr.GetPowerRaw(ctx, sm, sroot, addr)
if err != nil {
return xerrors.Errorf("failed to get power: %w", err)
}

ai.Power = pow.RawBytePower

var st miner.State
if err := cst.Get(ctx, act.Head, &st); err != nil {
st, err := miner.Load(store, act)
if err != nil {
return xerrors.Errorf("failed to read miner state: %w", err)
}

sectors, err := adt.AsArray(cs.Store(ctx), st.Sectors)
liveSectorCount, err := st.NumLiveSectors()
if err != nil {
return xerrors.Errorf("failed to compute live sector count: %w", err)
}

lockedFunds, err := st.LockedFunds()
if err != nil {
return xerrors.Errorf("failed to load sector set: %w", err)
return xerrors.Errorf("failed to compute locked funds: %w", err)
}

ai.InitialPledge = types.FIL(st.InitialPledgeRequirement)
ai.LockedFunds = types.FIL(st.LockedFunds)
ai.PreCommits = types.FIL(st.PreCommitDeposits)
ai.Sectors = sectors.Length()
ai.InitialPledge = types.FIL(lockedFunds.InitialPledgeRequirement)
ai.LockedFunds = types.FIL(lockedFunds.VestingFunds)
ai.PreCommits = types.FIL(lockedFunds.PreCommitDeposits)
ai.Sectors = liveSectorCount

var minfo miner.MinerInfo
if err := cst.Get(ctx, st.Info, &minfo); err != nil {
return xerrors.Errorf("failed to read miner info: %w", err)
minfo, err := st.Info()
if err != nil {
return xerrors.Errorf("failed to get miner info: %w", err)
}

ai.Worker = minfo.Worker
Expand Down

0 comments on commit 916421b

Please sign in to comment.