Skip to content

Commit

Permalink
find first non-empty block
Browse files Browse the repository at this point in the history
  • Loading branch information
countvonzero committed Aug 11, 2023
1 parent ac66267 commit 1705d98
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sql/certificates/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ func FirstInEpoch(db sql.Executor, epoch types.EpochID) (types.BlockID, error) {
rows int
)
if rows, err = db.Exec(`
select block from certificates where layer between ?1 and ?2 and valid = 1 and cert is not null
select block from certificates where layer between ?1 and ?2 and valid = 1 and block != ?3 and cert is not null
order by layer 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
Expand Down
9 changes: 5 additions & 4 deletions sql/certificates/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ func TestFirstInEpoch(t *testing.T) {
db := sql.InMemory()

lyrBlocks := map[types.LayerID]types.BlockID{
types.LayerID(layersPerEpoch - 1): {1}, // epoch 0
types.LayerID(layersPerEpoch + 1): {2}, // 2nd layer of epoch 1
types.LayerID(layersPerEpoch + 2): {3}, // 3d layer of epoch 1
types.LayerID(2*layersPerEpoch - 1): {4}, // last layer of epoch 1
types.LayerID(layersPerEpoch - 1): {1}, // epoch 0
types.LayerID(layersPerEpoch): types.EmptyBlockID, // 1st layer of epoch 1
types.LayerID(layersPerEpoch + 1): {2}, // 2nd layer of epoch 1
types.LayerID(layersPerEpoch + 2): {3}, // 3d layer of epoch 1
types.LayerID(2*layersPerEpoch - 1): {4}, // last layer of epoch 1
// epoch 2 has no hare output
types.LayerID(3 * layersPerEpoch): {5}, // first layer of epoch 3
// epoch 4 has hare output but no cert
Expand Down

0 comments on commit 1705d98

Please sign in to comment.