Skip to content

Commit

Permalink
feat: Introduce ClaimMeetsConsensusMinimums method across all versions
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Sztandera <[email protected]>
  • Loading branch information
Kubuxu committed Dec 10, 2024
1 parent 719bee9 commit 27c2d58
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 165 deletions.
41 changes: 23 additions & 18 deletions builtin/v10/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,8 @@ type CronEvent struct {
CallbackPayload []byte
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
Expand All @@ -155,6 +139,27 @@ func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

return st.ClaimMeetsConsensusMinimums(claim)
}

func (st *State) GetClaim(s adt.Store, a addr.Address) (*Claim, bool, error) {
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
41 changes: 23 additions & 18 deletions builtin/v11/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,8 @@ type CronEvent struct {
CallbackPayload []byte
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
Expand All @@ -155,6 +139,27 @@ func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

return st.ClaimMeetsConsensusMinimums(claim)
}

func (st *State) GetClaim(s adt.Store, a addr.Address) (*Claim, bool, error) {
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
41 changes: 23 additions & 18 deletions builtin/v12/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,8 @@ type CronEvent struct {
CallbackPayload []byte
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
Expand All @@ -156,6 +140,27 @@ func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

return st.ClaimMeetsConsensusMinimums(claim)
}

func (st *State) GetClaim(s adt.Store, a addr.Address) (*Claim, bool, error) {
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
41 changes: 23 additions & 18 deletions builtin/v13/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,8 @@ type CronEvent struct {
CallbackPayload []byte
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
Expand All @@ -156,6 +140,27 @@ func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

return st.ClaimMeetsConsensusMinimums(claim)
}

func (st *State) GetClaim(s adt.Store, a addr.Address) (*Claim, bool, error) {
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
41 changes: 23 additions & 18 deletions builtin/v14/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,8 @@ type CronEvent struct {
CallbackPayload []byte
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
Expand All @@ -156,6 +140,27 @@ func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

return st.ClaimMeetsConsensusMinimums(claim)
}

func (st *State) GetClaim(s adt.Store, a addr.Address) (*Claim, bool, error) {
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
41 changes: 23 additions & 18 deletions builtin/v15/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,8 @@ type CronEvent struct {
CallbackPayload []byte
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
Expand All @@ -167,6 +151,27 @@ func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
// miners and has power > zero the miner meets the minimum.
func (st *State) MinerNominalPowerMeetsConsensusMinimum(s adt.Store, miner addr.Address) (bool, error) { //nolint:deadcode,unused
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
return false, xerrors.Errorf("failed to load claims: %w", err)
}

claim, ok, err := getClaim(claims, miner)
if err != nil {
return false, err
}
if !ok {
return false, xerrors.Errorf("no claim for actor %w", miner)
}

return st.ClaimMeetsConsensusMinimums(claim)
}

func (st *State) GetClaim(s adt.Store, a addr.Address) (*Claim, bool, error) {
claims, err := adt.AsMap(s, st.Claims, builtin.DefaultHamtBitwidth)
if err != nil {
Expand Down
43 changes: 22 additions & 21 deletions builtin/v16/power/power_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ type CronEvent struct {
CallbackPayload []byte
}

// ClaimMeetsConsensusMinimums checks if given claim meets the minimums set by the network for mining.
func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
return false, xerrors.Errorf("could not get miner min power from proof type: %w", err)
}

// if miner is larger than min power requirement, we're set
if minerNominalPower.GreaterThanEqual(minerMinPower) {
return true, nil
}

// otherwise, if ConsensusMinerMinMiners miners meet min power requirement, return false
if st.MinerAboveMinPowerCount >= ConsensusMinerMinMiners {
return false, nil
}

// If fewer than ConsensusMinerMinMiners over threshold miner can win a block with non-zero power
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}

// MinerNominalPowerMeetsConsensusMinimum is used to validate Election PoSt
// winners outside the chain state. If the miner has over a threshold of power
// the miner meets the minimum. If the network is a below a threshold of
Expand Down Expand Up @@ -169,24 +191,3 @@ func getClaim(claims *adt.Map, a addr.Address) (*Claim, bool, error) {
}
return &out, true, nil
}

func (st *State) ClaimMeetsConsensusMinimums(claim *Claim) (bool, error) {
minerNominalPower := claim.RawBytePower
minerMinPower, err := builtin.ConsensusMinerMinPower(claim.WindowPoStProofType)
if err != nil {
return false, xerrors.Errorf("could not get miner min power from proof type: %w", err)
}

// if miner is larger than min power requirement, we're set
if minerNominalPower.GreaterThanEqual(minerMinPower) {
return true, nil
}

// otherwise, if ConsensusMinerMinMiners miners meet min power requirement, return false
if st.MinerAboveMinPowerCount >= ConsensusMinerMinMiners {
return false, nil
}

// If fewer than ConsensusMinerMinMiners over threshold miner can win a block with non-zero power
return minerNominalPower.GreaterThan(abi.NewStoragePower(0)), nil
}
Loading

0 comments on commit 27c2d58

Please sign in to comment.