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] - tortoise: don't use active set to compute number of expected ballots #5002

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion common/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ func (b *Ballot) ToTortoiseData() *BallotTortoiseData {
data.EpochData = &ReferenceData{
Beacon: b.EpochData.Beacon,
Eligibilities: uint32(b.EpochData.EligibilityCount),
ActiveSet: b.ActiveSet,
}
} else {
data.Ref = &b.RefBallot
Expand Down
10 changes: 0 additions & 10 deletions common/types/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ var (
// EmptyLayerHash is the layer hash for an empty layer.
EmptyLayerHash = Hash32{}

legacyLayer uint32

// layer at which optimistic filtering majority calculation is upgraded.
opUpgradeLayer uint32
)
Expand All @@ -32,14 +30,6 @@ func SetLayersPerEpoch(layers uint32) {
SetEffectiveGenesis(layers*2 - 1)
}

func SetLegacyLayers(layer uint32) {
legacyLayer = layer
}

func GetLegacyLayer() uint32 {
return legacyLayer
}

func SetOpUpgradeLayer(layer uint32) {
opUpgradeLayer = layer
}
Expand Down
6 changes: 2 additions & 4 deletions common/types/tortoise_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ func (b *BallotTortoiseData) MarshalLogObject(encoder log.ObjectEncoder) error {
}

type ReferenceData struct {
Beacon Beacon `json:"beacon"`
ActiveSet []ATXID `json:"set"`
Eligibilities uint32 `json:"elig"`
Beacon Beacon `json:"beacon"`
Eligibilities uint32 `json:"elig"`
}

func (r *ReferenceData) MarshalLogObject(encoder log.ObjectEncoder) error {
encoder.AddString("beacon", r.Beacon.String())
encoder.AddInt("set size", len(r.ActiveSet))
encoder.AddUint32("elig", r.Eligibilities)
return nil
}
6 changes: 4 additions & 2 deletions config/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ func MainnetConfig() Config {
DatabaseConnections: 16,
NetworkHRP: "sm",

LayerDuration: 5 * time.Minute,
LayerAvgSize: 50,
LayerDuration: 5 * time.Minute,
LayerAvgSize: 50,
// NOTE(dshulyak) this is only used to stop atx grading in hare.
// we should refactor that too.
LegacyLayer: 8180,
LayersPerEpoch: 4032,

Expand Down
6 changes: 1 addition & 5 deletions miner/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (o *Oracle) ProposalEligibility(lid types.LayerID, beacon types.Beacon, non
o.mu.Lock()
defer o.mu.Unlock()

if legacy := types.GetLegacyLayer(); legacy != 0 && lid.Uint32() == legacy+1 {
o.cache = &EpochEligibility{}
}

epoch := lid.GetEpoch()
if lid <= types.GetEffectiveGenesis() {
o.log.With().Panic("eligibility should not be queried during genesis", lid, epoch)
Expand Down Expand Up @@ -244,7 +240,7 @@ func (o *Oracle) calcEligibilityProofs(lid types.LayerID, epoch types.EpochID, b
)
var numEligibleSlots uint32
if ref == nil {
numEligibleSlots, err = proposals.GetLegacyNumEligible(lid, minerWeight, o.cfg.minActiveSetWeight, totalWeight, o.cfg.layerSize, o.cfg.layersPerEpoch)
numEligibleSlots, err = proposals.GetNumEligibleSlots(minerWeight, o.cfg.minActiveSetWeight, totalWeight, o.cfg.layerSize, o.cfg.layersPerEpoch)
if err != nil {
return nil, fmt.Errorf("oracle get num slots: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func GetCommand() *cobra.Command {

run := func(ctx context.Context) error {
types.SetLayersPerEpoch(app.Config.LayersPerEpoch)
types.SetLegacyLayers(app.Config.LegacyLayer)
// starting on 2023-09-14 20:00:00 +0000 UTC (~1 week into 4th epoch)
types.SetOpUpgradeLayer(18000)
// ensure all data folders exist
Expand Down Expand Up @@ -750,7 +749,7 @@ func (app *App) initServices(ctx context.Context) error {

hareCfg := app.Config.HARE
hareCfg.Hdist = app.Config.Tortoise.Hdist
hareCfg.StopAtxGrading = types.GetLegacyLayer()
hareCfg.StopAtxGrading = app.Config.LegacyLayer
app.hare = hare.New(
app.cachedDB,
hareCfg,
Expand Down
2 changes: 1 addition & 1 deletion proposals/eligibility_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (v *Validator) validateReference(ballot *types.Ballot, owned *types.Activat
}
totalWeight += atx.GetWeight()
}
numEligibleSlots, err := GetLegacyNumEligible(ballot.Layer, owned.GetWeight(), v.minActiveSetWeight, totalWeight, v.avgLayerSize, v.layersPerEpoch)
numEligibleSlots, err := GetNumEligibleSlots(owned.GetWeight(), v.minActiveSetWeight, totalWeight, v.avgLayerSize, v.layersPerEpoch)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions proposals/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
)

var (
CalcEligibleLayer = util.CalcEligibleLayer
GetNumEligibleSlots = util.GetNumEligibleSlots
GetLegacyNumEligible = util.GetLegacyNumEligible
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
)
Expand Down
8 changes: 0 additions & 8 deletions proposals/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ func CalcEligibleLayer(epochNumber types.EpochID, layersPerEpoch uint32, vrfSig
return epochNumber.FirstLayer().Add(uint32(eligibleLayerOffset))
}

func GetLegacyNumEligible(lid types.LayerID, weight, minWeight, totalWeight uint64, committeeSize, layersPerEpoch uint32) (uint32, error) {
legacyLayer := types.GetLegacyLayer()
if legacyLayer != 0 && legacyLayer >= lid.Uint32() {
return 1, nil
}
return GetNumEligibleSlots(weight, minWeight, totalWeight, committeeSize, layersPerEpoch)
}

// GetNumEligibleSlots calculates the number of eligible slots for a smesher in an epoch.
func GetNumEligibleSlots(weight, minWeight, totalWeight uint64, committeeSize, layersPerEpoch uint32) (uint32, error) {
if totalWeight == 0 {
Expand Down
22 changes: 0 additions & 22 deletions proposals/util/util_test.go

This file was deleted.

8 changes: 2 additions & 6 deletions tortoise/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import (

func TestGetBallot(t *testing.T) {
const n = 2
var activeset []*atxAction
s := newSession(t)
for i := 0; i < n; i++ {
activeset = append(
activeset,
s.smesher(i).atx(1, new(aopt).height(100).weight(400)),
)
s.smesher(i).atx(1, new(aopt).height(100).weight(400))
}

ref := s.smesher(0).atx(1).ballot(1, new(bopt).
beacon("a").
activeset(activeset...).
totalEligibilities(s.epochEligibilities()).
eligibilities(s.layerSize/n))

secondary := s.smesher(0).atx(1).ballot(2)
Expand Down
60 changes: 21 additions & 39 deletions tortoise/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,12 @@ func (b *bopt) eligibilities(value int) *bopt {
return b
}

func (b *bopt) activeset(values ...*atxAction) *bopt {
func (b *bopt) totalEligibilities(n int) *bopt {
b.opts = append(b.opts, func(ballot *ballotAction) {
ballot.activeset = values
for _, val := range values {
if ballot.EpochData == nil {
ballot.EpochData = &types.ReferenceData{}
}
ballot.EpochData.ActiveSet = append(ballot.EpochData.ActiveSet, val.header.ID)
if ballot.EpochData == nil {
ballot.EpochData = &types.ReferenceData{}
}
ballot.EpochData.Eligibilities = uint32(n)
})
return b
}
Expand Down Expand Up @@ -233,6 +230,7 @@ func (a *atxAction) rawballot(id types.BallotID, n int, opts ...*bopt) *ballotAc
}
if a.reference == nil {
a.reference = val
val.before = append(val.before, a)
} else {
val.Ref = &a.reference.ID
val.before = append(val.before, a.reference)
Expand All @@ -244,12 +242,6 @@ func (a *atxAction) rawballot(id types.BallotID, n int, opts ...*bopt) *ballotAc
if val.base != nil {
val.before = append(val.before, val.base)
}
for _, atx := range val.activeset {
val.before = append(val.before, atx)
}
if len(val.activeset) == 0 {
val.before = append(val.before, a)
}

a.reg.register(val)
a.ballots[id] = val
Expand All @@ -268,9 +260,8 @@ func (a *atxAction) ballot(n int, opts ...*bopt) *ballotAction {

type ballotAction struct {
types.BallotTortoiseData
base *ballotAction
activeset []*atxAction
before []action
base *ballotAction
before []action

onDecoded func(*DecodedBallot, error)
onStored func(error)
Expand Down Expand Up @@ -407,6 +398,10 @@ func (s *session) smesher(id int) *smesher {
return val
}

func (s *session) epochEligibilities() int {
return s.layerSize * s.epochSize / len(s.smeshers)
}

func (s *session) register(actions ...action) {
s.actions = append(s.actions, actions...)
}
Expand Down Expand Up @@ -740,19 +735,15 @@ func (s *session) tortoise() *Tortoise {

func TestSanity(t *testing.T) {
const n = 2
var activeset []*atxAction
s := newSession(t)
for i := 0; i < n; i++ {
activeset = append(
activeset,
s.smesher(i).atx(1, new(aopt).height(100).weight(400)),
)
s.smesher(i).atx(1, new(aopt).height(100).weight(400))
}
s.beacon(1, "a")
for i := 0; i < n; i++ {
s.smesher(i).atx(1).ballot(1, new(bopt).
beacon("a").
activeset(activeset...).
totalEligibilities(s.epochEligibilities()).
eligibilities(s.layerSize/n))
}
s.tally(1)
Expand All @@ -776,19 +767,15 @@ func TestSanity(t *testing.T) {

func TestDisagreement(t *testing.T) {
const n = 5
var activeset []*atxAction
s := newSession(t)
for i := 0; i < n; i++ {
activeset = append(
activeset,
s.smesher(i).atx(1, new(aopt).height(100).weight(400)),
)
s.smesher(i).atx(1, new(aopt).height(100).weight(400))
}
s.beacon(1, "a")
for i := 0; i < n; i++ {
s.smesher(i).atx(1).ballot(1, new(bopt).
beacon("a").
activeset(activeset...).
totalEligibilities(s.epochEligibilities()).
eligibilities(s.layerSize/n))
}
s.hareblock(1, "aa", 0)
Expand Down Expand Up @@ -816,15 +803,14 @@ func TestDisagreement(t *testing.T) {

func TestOpinion(t *testing.T) {
s := newSession(t)
var activeset []*atxAction
activeset = append(activeset,
s.smesher(0).atx(1, new(aopt).height(100).weight(2000)),
)

s.smesher(0).atx(1, new(aopt).height(100).weight(2000))

s.beacon(1, "a")
s.smesher(0).atx(1).ballot(1, new(bopt).
eligibilities(s.layerSize).
beacon("a").
activeset(activeset...),
totalEligibilities(s.epochEligibilities()),
)
for i := 2; i < s.epochSize; i++ {
id := strconv.Itoa(i)
Expand All @@ -842,19 +828,15 @@ func TestOpinion(t *testing.T) {
}

func TestEpochGap(t *testing.T) {
var activeset []*atxAction
s := newSession(t).withEpochSize(4)
for i := 0; i < 2; i++ {
activeset = append(
activeset,
s.smesher(i).atx(1, new(aopt).height(1).weight(10)),
)
s.smesher(i).atx(1, new(aopt).height(1).weight(10))
}
s.beacon(1, "a")
for i := 0; i < 2; i++ {
s.smesher(i).atx(1).ballot(1, new(bopt).
beacon("a").
activeset(activeset...).
totalEligibilities(s.epochEligibilities()).
eligibilities(s.layerSize/2))
}
rst := new(results).
Expand Down
15 changes: 12 additions & 3 deletions tortoise/full_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/proposals/util"
"github.com/spacemeshos/go-spacemesh/signing"
)

Expand Down Expand Up @@ -323,7 +324,11 @@ func TestFullCountVotes(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
logger := logtest.Zap(t)
tortoise := defaultAlgorithm(t)
var activeset []types.ATXID
var (
activeset []types.ATXID
total uint64
weights []uint64
)
for i := range tc.activeset {
atxid := types.ATXID{byte(i + 1)}
header := &types.ActivationTxHeader{
Expand All @@ -336,6 +341,8 @@ func TestFullCountVotes(t *testing.T) {
header.PublishEpoch = 1
tortoise.OnAtx(header.ToData())
activeset = append(activeset, atxid)
weights = append(weights, header.GetWeight())
total += header.GetWeight()
}

consensus := tortoise.trtl
Expand Down Expand Up @@ -372,8 +379,10 @@ func TestFullCountVotes(t *testing.T) {
ballot := &types.Ballot{}
ballot.EligibilityProofs = []types.VotingEligibility{{J: uint32(j)}}
ballot.AtxID = activeset[b.ATX]
ballot.EpochData = &types.EpochData{ActiveSetHash: types.Hash32{1, 2, 3}, EligibilityCount: 1}
ballot.ActiveSet = activeset

elig, err := util.GetNumEligibleSlots(weights[b.ATX], 0, total, consensus.LayerSize, types.GetLayersPerEpoch())
require.NoError(t, err)
ballot.EpochData = &types.EpochData{ActiveSetHash: types.Hash32{1, 2, 3}, EligibilityCount: elig}
ballot.Layer = lid
// don't vote on genesis for simplicity,
// since we don't care about block goodness in this test
Expand Down
10 changes: 3 additions & 7 deletions tortoise/model/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,17 @@ func (c *core) OnMessage(m Messenger, event Message) {
if c.refBallot != nil {
ballot.RefBallot = *c.refBallot
} else {
_, activeset, err := c.cdb.GetEpochWeight(ev.LayerID.GetEpoch())
if err != nil {
panic(err)
}
beacon, err := c.beacons.GetBeacon(ev.LayerID.GetEpoch())
if err != nil {
beacon = types.Beacon{}
c.rng.Read(beacon[:])
c.beacons.StoreBeacon(ev.LayerID.GetEpoch(), beacon)
}
ballot.EpochData = &types.EpochData{
ActiveSetHash: types.Hash32{1, 2, 3},
Beacon: beacon,
ActiveSetHash: types.Hash32{1, 2, 3},
Beacon: beacon,
EligibilityCount: c.eligibilities,
}
ballot.ActiveSet = activeset
}
ballot.Signature = c.signer.Sign(signing.BALLOT, ballot.SignedBytes())
ballot.SmesherID = c.signer.NodeID()
Expand Down
Loading
Loading