Skip to content

Commit

Permalink
hare: use epoch first applied block for hare active set (#5008)
Browse files Browse the repository at this point in the history
## Motivation
follow up on #4997 doing the same for hare active set
  • Loading branch information
countvonzero committed Sep 14, 2023
1 parent e0f2f27 commit 373798a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 41 deletions.
2 changes: 1 addition & 1 deletion hare/eligibility/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (o *Oracle) computeActiveSet(ctx context.Context, targetEpoch types.EpochID
return activeSet, nil
}

activeSet, err := miner.ActiveSetFromEpochFirstCertifiedBlock(o.cdb, targetEpoch)
activeSet, err := miner.ActiveSetFromEpochFirstBlock(o.cdb, targetEpoch)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions hare/eligibility/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
"github.com/spacemeshos/go-spacemesh/sql/layers"
"github.com/spacemeshos/go-spacemesh/system/mocks"
)

Expand Down Expand Up @@ -99,7 +99,7 @@ func createBlock(tb testing.TB, cdb *datastore.CachedDB, blts []*types.Ballot) {
}
block.Initialize()
require.NoError(tb, blocks.Add(cdb, block))
require.NoError(tb, certificates.Add(cdb, blts[0].Layer, &types.Certificate{BlockID: block.ID()}))
require.NoError(tb, layers.SetApplied(cdb, block.LayerIndex, block.ID()))
}

func createLayerData(tb testing.TB, cdb *datastore.CachedDB, lid types.LayerID, numMiners int) []types.NodeID {
Expand Down
9 changes: 0 additions & 9 deletions miner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@ import (
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)

func ActiveSetFromEpochFirstCertifiedBlock(db sql.Executor, epoch types.EpochID) ([]types.ATXID, error) {
bid, err := certificates.FirstInEpoch(db, epoch)
if err != nil {
return nil, err
}
return activeSetFromBlock(db, bid)
}

func ActiveSetFromEpochFirstBlock(db sql.Executor, epoch types.EpochID) ([]types.ATXID, error) {
bid, err := layers.FirstAppliedInEpoch(db, epoch)
if err != nil {
Expand Down
36 changes: 7 additions & 29 deletions miner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,27 @@ import (
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/blocks"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
"github.com/spacemeshos/go-spacemesh/sql/layers"
)

func TestActiveSetFromEpochFirstBlock(t *testing.T) {
for _, tc := range []struct {
desc string
miner, hare bool
err error
desc string
err error
}{
{
desc: "actives for miner",
miner: true,
desc: "actives",
},
{
desc: "no actives for miner",
miner: true,
err: sql.ErrNotFound,
},
{
desc: "actives for hare",
hare: true,
},
{
desc: "no actives for hare",
hare: true,
desc: "no actives",
err: sql.ErrNotFound,
},
} {
t.Run(tc.desc, func(t *testing.T) {
epoch := types.EpochID(3)
cdb := datastore.NewCachedDB(sql.InMemory(), logtest.New(t))

got, err := ActiveSetFromEpochFirstCertifiedBlock(cdb, epoch)
got, err := ActiveSetFromEpochFirstBlock(cdb, epoch)
require.ErrorIs(t, err, sql.ErrNotFound)
require.Nil(t, got)

Expand All @@ -66,12 +53,7 @@ func TestActiveSetFromEpochFirstBlock(t *testing.T) {
block.Initialize()
require.NoError(t, blocks.Add(cdb, block))
if tc.err == nil {
if tc.miner {
require.NoError(t, layers.SetApplied(cdb, lid, block.ID()))
}
if tc.hare {
require.NoError(t, certificates.Add(cdb, lid, &types.Certificate{BlockID: block.ID()}))
}
require.NoError(t, layers.SetApplied(cdb, lid, block.ID()))
}
if i == 0 {
expected = all
Expand All @@ -83,11 +65,7 @@ func TestActiveSetFromEpochFirstBlock(t *testing.T) {
}
}

if tc.miner {
got, err = ActiveSetFromEpochFirstBlock(cdb, epoch)
} else if tc.hare {
got, err = ActiveSetFromEpochFirstCertifiedBlock(cdb, epoch)
}
got, err = ActiveSetFromEpochFirstBlock(cdb, epoch)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
Expand Down

0 comments on commit 373798a

Please sign in to comment.