Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - generator doesn't need to recompute eligibility count and loop over atxs #5105

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
"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 @@
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

Check warning on line 239 in blocks/utils.go

View check run for this annotation

Codecov / codecov/patch

blocks/utils.go#L232-L239

Added lines #L232 - L239 were not covered by tests
}
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.