Skip to content

Commit

Permalink
generator doesn't need to recompute eligibility count and loop over a…
Browse files Browse the repository at this point in the history
…txs (#5105)

closes: #5077
  • Loading branch information
dshulyak committed Sep 27, 2023
1 parent 68669d6 commit fe23c05
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 240 deletions.
28 changes: 19 additions & 9 deletions blocks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/proposals"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/layers"
"github.com/spacemeshos/go-spacemesh/sql/transactions"
"github.com/spacemeshos/go-spacemesh/txs"
Expand Down Expand Up @@ -225,16 +225,26 @@ func rewardInfoAndHeight(logger log.Log, cdb *datastore.CachedDB, cfg Config, pr
if atx.BaseTickHeight > max {
max = atx.BaseTickHeight
}
ballot := &p.Ballot
weightPer, err := proposals.ComputeWeightPerEligibility(cdb, ballot)
if err != nil {
logger.With().Error("failed to calculate weight per eligibility", p.ID(), log.Err(err))
return 0, nil, err
var count uint32
if p.Ballot.EpochData != nil {
count = p.Ballot.EpochData.EligibilityCount
} else {
ref, err := ballots.Get(cdb, p.RefBallot)
if err != nil {
return 0, nil, fmt.Errorf("get ballot %s: %w", p.RefBallot.String(), err)
}
if ref.EpochData == nil {
return 0, nil, fmt.Errorf("corrupted data: ref ballot %s with empty epoch data", p.RefBallot.String())
}
count = ref.EpochData.EligibilityCount
}
logger.With().Debug("weight per eligibility", p.ID(), log.Stringer("weight_per", weightPer))
actual := weightPer.Mul(weightPer, new(big.Rat).SetUint64(uint64(len(ballot.EligibilityProofs))))
if _, ok := weights[atx.ID]; !ok {
weights[atx.ID] = actual
weight := new(big.Rat).SetFrac(
new(big.Int).SetUint64(atx.GetWeight()),
new(big.Int).SetUint64(uint64(count)),
)
weight.Mul(weight, new(big.Rat).SetUint64(uint64(len(p.Ballot.EligibilityProofs))))
weights[atx.ID] = weight
atxids = append(atxids, atx.ID)
} else {
logger.With().Error("multiple proposals with the same ATX", atx.ID, p.ID())
Expand Down
2 changes: 0 additions & 2 deletions proposals/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
var (
CalcEligibleLayer = util.CalcEligibleLayer
GetNumEligibleSlots = util.GetNumEligibleSlots
// ComputeWeightPerEligibility computes the ballot weight per eligibility w.r.t the active set recorded in its reference ballot.
ComputeWeightPerEligibility = util.ComputeWeightPerEligibility
)

//go:generate scalegen -types VrfMessage
Expand Down
57 changes: 0 additions & 57 deletions proposals/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ package util
import (
"encoding/binary"
"errors"
"fmt"
"math/big"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
)

var (
Expand Down Expand Up @@ -41,55 +36,3 @@ func GetNumEligibleSlots(weight, minWeight, totalWeight uint64, committeeSize, l
}
return uint32(numEligible), nil
}

// ComputeWeightPerEligibility computes the ballot weight per eligibility w.r.t the active set recorded in its reference ballot.
func ComputeWeightPerEligibility(
cdb *datastore.CachedDB,
ballot *types.Ballot,
) (*big.Rat, error) {
var (
refBallot = ballot
hdr *types.ActivationTxHeader
err error
atxWeight uint64
)
if ballot.EpochData == nil {
if ballot.RefBallot == types.EmptyBallotID {
return nil, fmt.Errorf("%w: empty ref ballot but no epoch data %s", ErrBadBallotData, ballot.ID())
}
refBallot, err = ballots.Get(cdb, ballot.RefBallot)
if err != nil {
return nil, fmt.Errorf("%w: missing ref ballot %s (for %s)", err, ballot.RefBallot, ballot.ID())
}
}
if refBallot.EpochData == nil {
return nil, fmt.Errorf("epoch data is nil on ballot %d/%s", refBallot.Layer, refBallot.ID())
}
if refBallot.EpochData.EligibilityCount == 0 {
return nil, fmt.Errorf("eligibility count is 0 on ballot %d/%s", refBallot.Layer, refBallot.ID())
}
actives, err := activesets.Get(cdb, refBallot.EpochData.ActiveSetHash)
if err != nil {
return nil, fmt.Errorf("get active set %s (%s)", refBallot.EpochData.ActiveSetHash.ShortString(), refBallot.ID())
}
if len(actives.Set) == 0 {
return nil, fmt.Errorf("empty active set %s (%s)", refBallot.EpochData.ActiveSetHash.ShortString(), refBallot.ID())
}
for _, atxID := range actives.Set {
hdr, err = cdb.GetAtxHeader(atxID)
if err != nil {
return nil, fmt.Errorf("%w: missing atx %s in active set of %s (for %s)", err, atxID, refBallot.ID(), ballot.ID())
}
if atxID == ballot.AtxID {
atxWeight = hdr.GetWeight()
break
}
}
if atxWeight == 0 {
return nil, fmt.Errorf("atx id %v is not found in the active set of the reference ballot %v with atxid %v", ballot.AtxID, refBallot.ID(), refBallot.AtxID)
}
return new(big.Rat).SetFrac(
new(big.Int).SetUint64(atxWeight),
new(big.Int).SetUint64(uint64(refBallot.EpochData.EligibilityCount)),
), nil
}
172 changes: 0 additions & 172 deletions proposals/util_test.go

This file was deleted.

0 comments on commit fe23c05

Please sign in to comment.