From a74a368c4168d6679109aae1ce6ea4e72c7d7553 Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Wed, 2 Sep 2020 23:08:37 -0400 Subject: [PATCH] Use new MinerEligibleForElection method --- chain/stmgr/utils.go | 30 ++++++++++++++++++++++++------ chain/sub/incoming.go | 2 +- chain/sync.go | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/chain/stmgr/utils.go b/chain/stmgr/utils.go index db6157b09..c6ca94c52 100644 --- a/chain/stmgr/utils.go +++ b/chain/stmgr/utils.go @@ -569,7 +569,7 @@ func MinerGetBaseInfo(ctx context.Context, sm *StateManager, bcn beacon.RandomBe return nil, xerrors.Errorf("resolving worker address: %w", err) } - hmp, err := MinerHasMinPower(ctx, sm, maddr, lbts) + hmp, err := MinerEligibleForElection(ctx, sm, maddr, lbts) if err != nil { return nil, xerrors.Errorf("determining if miner has min power failed: %w", err) } @@ -643,14 +643,32 @@ func GetReturnType(ctx context.Context, sm *StateManager, to address.Address, me return reflect.New(m.Ret.Elem()).Interface().(cbg.CBORUnmarshaler), nil } -func MinerHasMinPower(ctx context.Context, sm *StateManager, addr address.Address, ts *types.TipSet) (bool, error) { - var ps power.State - _, err := sm.LoadActorState(ctx, builtin.StoragePowerActorAddr, &ps, ts) +func MinerEligibleForElection(ctx context.Context, sm *StateManager, addr address.Address, ts *types.TipSet) (bool, error) { + + var act types.Actor + err := sm.WithParentState(ts, sm.WithActor(addr, GetActor(&act))) if err != nil { - return false, xerrors.Errorf("loading power actor state: %w", err) + return false, xerrors.Errorf("loading miner balance: %w", err) } - return ps.MinerNominalPowerMeetsConsensusMinimum(sm.ChainStore().Store(ctx), addr) + var ms miner.State + _, err = sm.LoadActorState(ctx, addr, &ms, ts) + if err != nil { + return false, xerrors.Errorf("loading miner actor state: %w", err) + } + + var rs reward.State + _, err = sm.LoadActorState(ctx, builtin.RewardActorAddr, &rs, ts) + if err != nil { + return false, xerrors.Errorf("loading reward actor state: %w", err) + } + + ret, err := miner.MinerEligibleForElection(sm.cs.Store(ctx), &ms, rs.ThisEpochReward, act.Balance, ts.Height()) + if err != nil { + return false, xerrors.Errorf("determining election eligibility: %w", err) + } + + return ret, nil } func CheckTotalFIL(ctx context.Context, sm *StateManager, ts *types.TipSet) (abi.TokenAmount, error) { diff --git a/chain/sub/incoming.go b/chain/sub/incoming.go index 5c28aa835..823a2411d 100644 --- a/chain/sub/incoming.go +++ b/chain/sub/incoming.go @@ -481,7 +481,7 @@ func (bv *BlockValidator) checkPowerAndGetWorkerKey(ctx context.Context, bh *typ return address.Undef, ErrSoftFailure } - hmp, err := stmgr.MinerHasMinPower(ctx, bv.stmgr, bh.Miner, lbts) + hmp, err := stmgr.MinerEligibleForElection(ctx, bv.stmgr, bh.Miner, lbts) if err != nil { log.Warnf("failed to determine if incoming block's miner has minimum power: %s", err) return address.Undef, ErrSoftFailure diff --git a/chain/sync.go b/chain/sync.go index 1b1cbdde9..31600b81d 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -774,7 +774,7 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) (er return xerrors.Errorf("block is not claiming to be a winner") } - hp, err := stmgr.MinerHasMinPower(ctx, syncer.sm, h.Miner, lbts) + hp, err := stmgr.MinerEligibleForElection(ctx, syncer.sm, h.Miner, lbts) if err != nil { return xerrors.Errorf("determining if miner has min power failed: %w", err) }