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] - remove read/write for ballot.ActiveSet #5024

Closed
wants to merge 5 commits into from
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Support for old certificate sync protocol is dropped. This update is incompatibl

### Improvements
* [#5021](https://github.com/spacemeshos/go-spacemesh/pull/5021) Drop support for old certificate sync protocol.
* [#5024](https://github.com/spacemeshos/go-spacemesh/pull/5024) Active set will be saved in state separately from ballots.

## v1.1.5

Expand All @@ -41,7 +42,7 @@ active set will not be gossipped together with proposals. That was the main netw
* `postdata_metadata.json` is now updated atomically to prevent corruption of the file.
* [#4956](https://github.com/spacemeshos/go-spacemesh/pull/4956) Active set is will not be gossipped in every proposal.
Active set usually contains list of atxs that targets current epoch. As the number of atxs grows this object grows as well.
* [#4993](https://github.com/spacemeshos/go-spacemesh/pull/4993) Drop proposals after genering a block. This limits growth of the state.
* [#4993](https://github.com/spacemeshos/go-spacemesh/pull/4993) Drop proposals after generating a block. This limits growth of the state.

## v1.1.4

Expand Down
14 changes: 8 additions & 6 deletions api/grpcserver/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/accounts"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/identities"
"github.com/spacemeshos/go-spacemesh/system"
Expand All @@ -58,7 +59,6 @@ import (

const (
labelsPerUnit = 2048
bitsPerLabel = 8
numUnits = 2
genTimeUnix = 1000000
layerDuration = 10 * time.Second
Expand All @@ -71,7 +71,6 @@ const (
accountBalance = 8675301
accountCounter = 0
rewardAmount = 5551234
activesetSize = 10001
)

var (
Expand Down Expand Up @@ -214,8 +213,7 @@ func TestMain(m *testing.M) {
// These create circular dependencies so they have to be initialized
// after the global vars
ballot1.AtxID = globalAtx.ID()
ballot1.EpochData = &types.EpochData{}
ballot1.ActiveSet = []types.ATXID{globalAtx.ID(), globalAtx2.ID()}
ballot1.EpochData = &types.EpochData{ActiveSetHash: types.ATXIDList{globalAtx.ID(), globalAtx2.ID()}.Hash()}

globalTx = NewTx(0, addr1, signer1)
globalTx2 = NewTx(1, addr2, signer2)
Expand Down Expand Up @@ -1043,7 +1041,9 @@ func TestMeshService(t *testing.T) {
genesis := time.Unix(genTimeUnix, 0)
genTime.EXPECT().GenesisTime().Return(genesis)
genTime.EXPECT().CurrentLayer().Return(layerCurrent).AnyTimes()
grpcService := NewMeshService(datastore.NewCachedDB(sql.InMemory(), logtest.New(t)), meshAPIMock, conStateAPI, genTime, layersPerEpoch, types.Hash20{}, layerDuration, layerAvgSize, txsPerProposal)
db := datastore.NewCachedDB(sql.InMemory(), logtest.New(t))
grpcService := NewMeshService(db, meshAPIMock, conStateAPI, genTime, layersPerEpoch, types.Hash20{}, layerDuration, layerAvgSize, txsPerProposal)
require.NoError(t, activesets.Add(db, ballot1.EpochData.ActiveSetHash, &types.EpochActiveSet{Set: types.ATXIDList{globalAtx.ID(), globalAtx2.ID()}}))
t.Cleanup(launchServer(t, cfg, grpcService))

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Expand Down Expand Up @@ -2163,7 +2163,9 @@ func TestLayerStream_comprehensive(t *testing.T) {

ctrl := gomock.NewController(t)
genTime := NewMockgenesisTimeAPI(ctrl)
grpcService := NewMeshService(datastore.NewCachedDB(sql.InMemory(), logtest.New(t)), meshAPIMock, conStateAPI, genTime, layersPerEpoch, types.Hash20{}, layerDuration, layerAvgSize, txsPerProposal)
db := datastore.NewCachedDB(sql.InMemory(), logtest.New(t))
grpcService := NewMeshService(db, meshAPIMock, conStateAPI, genTime, layersPerEpoch, types.Hash20{}, layerDuration, layerAvgSize, txsPerProposal)
require.NoError(t, activesets.Add(db, ballot1.EpochData.ActiveSetHash, &types.EpochActiveSet{Set: types.ATXIDList{globalAtx.ID(), globalAtx2.ID()}}))
t.Cleanup(launchServer(t, cfg, grpcService))

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down
19 changes: 13 additions & 6 deletions api/grpcserver/mesh_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
)

// MeshService exposes mesh data such as accounts, blocks, and transactions.
Expand Down Expand Up @@ -124,17 +125,19 @@

func (s MeshService) getFilteredActivations(ctx context.Context, startLayer types.LayerID, addr types.Address) (activations []*types.VerifiedActivationTx, err error) {
// We have no way to look up activations by coinbase so we have no choice but to read all of them.
// TODO: index activations by layer (and maybe by coinbase)
// See https://github.com/spacemeshos/go-spacemesh/issues/2064.
var atxids []types.ATXID
for l := startLayer; !l.After(s.mesh.LatestLayer()); l = l.Add(1) {
layer, err := s.mesh.GetLayer(l)
if layer == nil || err != nil {
return nil, status.Errorf(codes.Internal, "error retrieving layer data")
}
for _, b := range layer.Ballots() {
if b.EpochData != nil && b.ActiveSet != nil {
atxids = append(atxids, b.ActiveSet...)
if b.EpochData != nil {
actives, err := activesets.Get(s.cdb, b.EpochData.ActiveSetHash)
if err != nil {
return nil, status.Errorf(codes.Internal, "error retrieving active set %s (%s)", b.ID().String(), b.EpochData.ActiveSetHash.ShortString())
}

Check warning on line 139 in api/grpcserver/mesh_service.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/mesh_service.go#L138-L139

Added lines #L138 - L139 were not covered by tests
atxids = append(atxids, actives.Set...)
}
}
}
Expand Down Expand Up @@ -334,8 +337,12 @@
}

for _, b := range layer.Ballots() {
if b.EpochData != nil && b.ActiveSet != nil {
activations = append(activations, b.ActiveSet...)
if b.EpochData != nil {
actives, err := activesets.Get(s.cdb, b.EpochData.ActiveSetHash)
if err != nil {
return nil, status.Errorf(codes.Internal, "error retrieving active set %s (%s)", b.ID().String(), b.EpochData.ActiveSetHash.ShortString())
}

Check warning on line 344 in api/grpcserver/mesh_service.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/mesh_service.go#L343-L344

Added lines #L343 - L344 were not covered by tests
activations = append(activations, actives.Set...)
}
}

Expand Down
4 changes: 3 additions & 1 deletion blocks/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/layers"
Expand Down Expand Up @@ -192,10 +193,10 @@ func createProposal(
EpochData: &types.EpochData{
Beacon: types.RandomBeacon(),
EligibilityCount: uint32(layerSize * epochSize / len(activeSet)),
ActiveSetHash: activeSet.Hash(),
},
},
EligibilityProofs: make([]types.VotingEligibility, numEligibility),
ActiveSet: activeSet,
},
TxIDs: txIDs,
MeshHash: meshHash,
Expand All @@ -207,6 +208,7 @@ func createProposal(
require.NoError(t, p.Initialize())
require.NoError(t, ballots.Add(db, &p.Ballot))
require.NoError(t, proposals.Add(db, p))
activesets.Add(db, activeSet.Hash(), &types.EpochActiveSet{Set: activeSet})
return p
}

Expand Down
10 changes: 7 additions & 3 deletions blocks/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)

Expand Down Expand Up @@ -103,7 +104,7 @@ func Test_getProposalMetadata(t *testing.T) {
cfg := Config{OptFilterThreshold: 70}
lid := types.LayerID(111)
_, atxs := createATXs(t, cdb, (lid.GetEpoch() - 1).FirstLayer(), 10)
actives := types.ToATXIDs(atxs)
actives := types.ATXIDList(types.ToATXIDs(atxs))
props := make([]*types.Proposal, 0, 10)
hash1 := types.Hash32{1, 2, 3}
hash2 := types.Hash32{3, 2, 1}
Expand All @@ -119,11 +120,14 @@ func Test_getProposalMetadata(t *testing.T) {
for j := 0; j <= i; j++ {
p.EligibilityProofs = append(p.EligibilityProofs, types.VotingEligibility{J: uint32(j + 1)})
}
p.EpochData = &types.EpochData{}
p.ActiveSet = actives
p.EpochData = &types.EpochData{ActiveSetHash: actives.Hash()}
p.EpochData.EligibilityCount = uint32(i + 1)
props = append(props, &p)
}
require.NoError(t, activesets.Add(cdb, actives.Hash(), &types.EpochActiveSet{
Epoch: lid.GetEpoch(),
Set: actives,
}))
require.NoError(t, layers.SetMeshHash(cdb, lid-1, hash2))

// only 5 / 10 proposals has the same state
Expand Down
8 changes: 4 additions & 4 deletions common/types/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@
// MarshalLogObject implements logging encoder for Ballot.
func (b *Ballot) MarshalLogObject(encoder log.ObjectEncoder) error {
var (
activeSetSize = 0
beacon Beacon
activeHash Hash32
beacon Beacon

Check warning on line 293 in common/types/ballot.go

View check run for this annotation

Codecov / codecov/patch

common/types/ballot.go#L292-L293

Added lines #L292 - L293 were not covered by tests
)

if b.EpochData != nil {
activeSetSize = len(b.ActiveSet)
activeHash = b.EpochData.ActiveSetHash

Check warning on line 297 in common/types/ballot.go

View check run for this annotation

Codecov / codecov/patch

common/types/ballot.go#L297

Added line #L297 was not covered by tests
beacon = b.EpochData.Beacon
}

Expand All @@ -309,7 +309,7 @@
encoder.AddInt("abstain", len(b.Votes.Abstain))
encoder.AddString("atx_id", b.AtxID.String())
encoder.AddString("ref_ballot", b.RefBallot.String())
encoder.AddInt("active_set_size", activeSetSize)
encoder.AddString("active set hash", activeHash.ShortString())

Check warning on line 312 in common/types/ballot.go

View check run for this annotation

Codecov / codecov/patch

common/types/ballot.go#L312

Added line #L312 was not covered by tests
encoder.AddString("beacon", beacon.ShortString())
encoder.AddObject("votes", &b.Votes)
return nil
Expand Down
1 change: 0 additions & 1 deletion config/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func MainnetConfig() Config {
// 1000 - is assumed minimal number of units
// 5000 - half of the expected poet ticks
MinimalActiveSetWeight: 1000 * 5000,
EmitEmptyActiveSet: 20000,
},
HARE: hareConfig.Config{
N: 200,
Expand Down
18 changes: 16 additions & 2 deletions hare/eligibility/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"github.com/spacemeshos/go-spacemesh/miner"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/activesets"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/system"
)
Expand Down Expand Up @@ -462,11 +463,24 @@
o.Log.With().Debug("beacon mismatch", log.Stringer("local", beacon), log.Object("ballot", ballot))
continue
}
for _, id := range ballot.ActiveSet {
actives, err := activesets.Get(o.cdb, ballot.EpochData.ActiveSetHash)
if err != nil {
o.Log.With().Error("failed to get active set",
log.String("actives hash", ballot.EpochData.ActiveSetHash.ShortString()),
log.String("ballot ", ballot.ID().String()),
log.Err(err),
)
continue

Check warning on line 473 in hare/eligibility/oracle.go

View check run for this annotation

Codecov / codecov/patch

hare/eligibility/oracle.go#L468-L473

Added lines #L468 - L473 were not covered by tests
}
for _, id := range actives.Set {
activeMap[id] = struct{}{}
}
}
o.Log.With().Warning("using tortoise active set", log.Uint32("epoch", epoch.Uint32()), log.Stringer("beacon", beacon))
o.Log.With().Warning("using tortoise active set",
log.Int("actives size", len(activeMap)),
log.Uint32("epoch", epoch.Uint32()),
log.Stringer("beacon", beacon),
)
return maps.Keys(activeMap), nil
}

Expand Down