Skip to content

Commit

Permalink
add test and rename ActiveSetFromBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
countvonzero committed Jul 31, 2023
1 parent d0279a1 commit 334d91b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hare/eligibility/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (o *Oracle) computeActiveSet(ctx context.Context, targetEpoch types.EpochID
return activeSet, nil
}

activeSet, err := miner.ActiveSetFromBlock(o.cdb, targetEpoch)
activeSet, err := miner.ActiveSetFromEpochFirstBlock(o.cdb, targetEpoch)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion miner/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (o *Oracle) ProposalEligibility(lid types.LayerID, beacon types.Beacon, non
}

func (o *Oracle) activesFromFirstBlock(targetEpoch types.EpochID) (uint64, uint64, []types.ATXID, error) {
activeSet, err := ActiveSetFromBlock(o.cdb, targetEpoch)
activeSet, err := ActiveSetFromEpochFirstBlock(o.cdb, targetEpoch)
if err != nil {
return 0, 0, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion miner/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,5 +396,5 @@ func TestOracle_NotSyncedBeforeLastEpoch(t *testing.T) {
ee, err := o.ProposalEligibility(lid, types.RandomBeacon(), types.VRFPostIndex(1))
require.NoError(t, err)
require.NotNil(t, ee)
require.Len(t, ee.ActiveSet, len(expected))
require.ElementsMatch(t, ee.ActiveSet, expected)
}
2 changes: 1 addition & 1 deletion miner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/spacemeshos/go-spacemesh/sql/certificates"
)

func ActiveSetFromBlock(db sql.Executor, epoch types.EpochID) ([]types.ATXID, error) {
func ActiveSetFromEpochFirstBlock(db sql.Executor, epoch types.EpochID) ([]types.ATXID, error) {
bid, err := certificates.FirstInEpoch(db, epoch)
if err != nil {
return nil, err
Expand Down
56 changes: 56 additions & 0 deletions miner/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package miner

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"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/blocks"
"github.com/spacemeshos/go-spacemesh/sql/certificates"
)

func TestActiveSetFromEpochFirstBlock(t *testing.T) {
epoch := types.EpochID(3)
cdb := datastore.NewCachedDB(sql.InMemory(), logtest.New(t))

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

var expected []types.ATXID
for i := uint32(0); i < layersPerEpoch; i++ {
lid := epoch.FirstLayer() + types.LayerID(i)
all := types.RandomActiveSet(10)
blts := createBallots(t, cdb, lid, 5, all)
block := &types.Block{
InnerBlock: types.InnerBlock{
LayerIndex: lid,
},
}
for _, b := range blts {
block.Rewards = append(block.Rewards, types.AnyReward{AtxID: b.AtxID})
all = append(all, b.AtxID)
}
block.Initialize()
require.NoError(t, blocks.Add(cdb, block))
require.NoError(t, certificates.Add(cdb, lid, &types.Certificate{BlockID: block.ID()}))
if i == 0 {
expected = all
}
for _, id := range all {
signer, err := signing.NewEdSigner()
require.NoError(t, err)
genMinerATX(t, cdb, id, (epoch - 1).FirstLayer(), signer, time.Now())
}
}

got, err = ActiveSetFromEpochFirstBlock(cdb, epoch)
require.NoError(t, err)
require.ElementsMatch(t, expected, got)
}

0 comments on commit 334d91b

Please sign in to comment.