Skip to content

Commit

Permalink
fallback on first applied block for new miners
Browse files Browse the repository at this point in the history
  • Loading branch information
countvonzero committed Sep 11, 2023
1 parent 8b371b1 commit 3631e6a
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 36 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.ActiveSetFromEpochFirstBlock(o.cdb, targetEpoch)
activeSet, err := miner.ActiveSetFromEpochFirstCertifiedBlock(o.cdb, targetEpoch)
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, err
}
Expand Down
18 changes: 17 additions & 1 deletion miner/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ import (
"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 ActiveSetFromEpochFirstBlock(db sql.Executor, epoch types.EpochID) ([]types.ATXID, error) {
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) {
result, err := ActiveSetFromEpochFirstCertifiedBlock(db, epoch)
if err == nil {
return result, nil
}
bid, err := layers.FirstAppliedInEpoch(db, epoch)
if err != nil {
return nil, err
}
return activeSetFromBlock(db, bid)
}

func activeSetFromBlock(db sql.Executor, bid types.BlockID) ([]types.ATXID, error) {
block, err := blocks.Get(db, bid)
if err != nil {
return nil, fmt.Errorf("actives get block: %w", err)
Expand Down
103 changes: 69 additions & 34 deletions miner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,79 @@ import (
"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) {
epoch := types.EpochID(3)
cdb := datastore.NewCachedDB(sql.InMemory(), logtest.New(t))
for _, tc := range []struct {
desc string
certified, applied bool
err error
}{
{
desc: "with certified",
certified: true,
},
{
desc: "w/o certified",
applied: true,
},
{
desc: "nothing",
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 := ActiveSetFromEpochFirstBlock(cdb, epoch)
require.ErrorIs(t, err, sql.ErrNotFound)
require.Nil(t, got)
got, err := ActiveSetFromEpochFirstCertifiedBlock(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())
}
}
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))
if tc.certified {
require.NoError(t, certificates.Add(cdb, lid, &types.Certificate{BlockID: block.ID()}))
} else if tc.applied {
require.NoError(t, layers.SetApplied(cdb, lid, 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)
got, err = ActiveSetFromEpochFirstBlock(cdb, epoch)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
require.ElementsMatch(t, expected, got)
}
if tc.certified {
got, err = ActiveSetFromEpochFirstCertifiedBlock(cdb, epoch)
require.NoError(t, err)
require.ElementsMatch(t, expected, got)
}
})
}
}
24 changes: 24 additions & 0 deletions sql/layers/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ func GetApplied(db sql.Executor, lid types.LayerID) (rst types.BlockID, err erro
return rst, err
}

func FirstAppliedInEpoch(db sql.Executor, epoch types.EpochID) (types.BlockID, error) {
var (
result types.BlockID
err error
rows int
)
if rows, err = db.Exec(`
select applied_block from layers
where id between ?1 and ?2 and applied_block is not null and applied_block != ?3
order by id asc limit 1;`, func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(epoch.FirstLayer()))
stmt.BindInt64(2, int64((epoch+1).FirstLayer()-1))
stmt.BindBytes(3, types.EmptyBlockID[:])
}, func(stmt *sql.Statement) bool {
stmt.ColumnBytes(0, result[:])
return true
}); err != nil {
return types.EmptyBlockID, fmt.Errorf("FirstAppliedInEpoch %s: %w", epoch, err)

Check warning on line 160 in sql/layers/layers.go

View check run for this annotation

Codecov / codecov/patch

sql/layers/layers.go#L160

Added line #L160 was not covered by tests
} else if rows == 0 {
return types.EmptyBlockID, fmt.Errorf("FirstAppliedInEpoch %s: %w", epoch, sql.ErrNotFound)
}
return result, nil
}

// GetLastApplied for the applied block for layer.
func GetLastApplied(db sql.Executor) (types.LayerID, error) {
var lid types.LayerID
Expand Down
50 changes: 50 additions & 0 deletions sql/layers/layers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package layers

import (
"os"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -9,6 +10,15 @@ import (
"github.com/spacemeshos/go-spacemesh/sql"
)

const layersPerEpoch = 4

func TestMain(m *testing.M) {
types.SetLayersPerEpoch(layersPerEpoch)

res := m.Run()
os.Exit(res)
}

func TestWeakCoin(t *testing.T) {
db := sql.InMemory()
lid := types.LayerID(10)
Expand Down Expand Up @@ -56,6 +66,46 @@ func TestAppliedBlock(t *testing.T) {
require.ErrorIs(t, err, sql.ErrNotFound)
}

func TestFirstAppliedInEpoch(t *testing.T) {
db := sql.InMemory()
blks := map[types.LayerID]types.BlockID{
types.EpochID(1).FirstLayer(): {1},
types.EpochID(2).FirstLayer(): types.EmptyBlockID,
types.EpochID(2).FirstLayer() + 1: {2},
types.EpochID(3).FirstLayer() + 1: {3},
}
for _, epoch := range []types.EpochID{0, 1, 2, 3} {
// cause layer to be inserted
for j := 0; j < layersPerEpoch; j++ {
lid := epoch.FirstLayer() + types.LayerID(j)
require.NoError(t, SetWeakCoin(db, lid, false))
}
}
for lid, bid := range blks {
require.NoError(t, SetApplied(db, lid, bid))
}

got, err := FirstAppliedInEpoch(db, types.EpochID(0))
require.ErrorIs(t, err, sql.ErrNotFound)
require.Equal(t, types.EmptyBlockID, got)

got, err = FirstAppliedInEpoch(db, types.EpochID(1))
require.NoError(t, err)
require.Equal(t, types.BlockID{1}, got)

got, err = FirstAppliedInEpoch(db, types.EpochID(2))
require.NoError(t, err)
require.Equal(t, types.BlockID{2}, got)

got, err = FirstAppliedInEpoch(db, types.EpochID(3))
require.NoError(t, err)
require.Equal(t, types.BlockID{3}, got)

got, err = FirstAppliedInEpoch(db, types.EpochID(4))
require.ErrorIs(t, err, sql.ErrNotFound)
require.Equal(t, types.EmptyBlockID, got)
}

func TestUnsetAppliedFrom(t *testing.T) {
db := sql.InMemory()
lid := types.LayerID(10)
Expand Down

0 comments on commit 3631e6a

Please sign in to comment.