Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions block/internal/cache/generic_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ func (c *Cache) getDAIncludedByHeight(blockHeight uint64) (uint64, bool) {
return v, exists
}

// getDAIncludedByHeightAndHash is getDAIncludedByHeight for the block whose
// hash is known. A placeholder restored from the store is accepted, since its
// hash is unknown until the entry is set again.
func (c *Cache) getDAIncludedByHeightAndHash(blockHeight uint64, hash string) (uint64, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
stored, ok := c.hashByHeight[blockHeight]
if !ok || (stored != hash && stored != HeightPlaceholderKey(c.storeKeyPrefix, blockHeight)) {
return 0, false
}
v, exists := c.daIncluded[stored]
return v, exists
}

// setDAIncluded records DA inclusion in memory.
// If a previous entry already exists at blockHeight (e.g. a placeholder from
// RestoreFromStore), it is evicted from daIncluded to avoid orphan leaks.
Expand Down
23 changes: 23 additions & 0 deletions block/internal/cache/generic_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ func TestCache_RestoreFromStore_PlaceholderOverwrittenByRealHash(t *testing.T) {
assert.Equal(t, uint64(99), daH2)
}

func TestCache_getDAIncludedByHeightAndHash(t *testing.T) {
st := testMemStore(t)
writeSnapshot(t, st, "hdr/", []snapshotEntry{{blockHeight: 3, daHeight: 99}})
c := NewCache(st, "hdr/")
require.NoError(t, c.RestoreFromStore(context.Background(), 0))

daH, ok := c.getDAIncludedByHeightAndHash(3, "anyHash")
require.True(t, ok, "a restored placeholder stands in for any hash")
assert.Equal(t, uint64(99), daH)

c.setDAIncluded("hashA", 100, 4)
daH, ok = c.getDAIncludedByHeightAndHash(4, "hashA")
require.True(t, ok)
assert.Equal(t, uint64(100), daH)

_, ok = c.getDAIncludedByHeightAndHash(4, "hashB")
assert.False(t, ok, "a marker for another block at the same height")

c.removeDAIncluded("hashA")
_, ok = c.getDAIncludedByHeightAndHash(4, "hashA")
assert.False(t, ok)
}

// TestCache_RestoreFromStore_RoundTrip verifies that SaveToStore persists a
// snapshot that a freshly-constructed cache can fully recover.
func TestCache_RestoreFromStore_RoundTrip(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions block/internal/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type CacheManager interface {
SetHeaderSeen(hash string, blockHeight uint64)
GetHeaderDAIncludedByHash(hash string) (uint64, bool)
GetHeaderDAIncludedByHeight(blockHeight uint64) (uint64, bool)
GetHeaderDAIncludedByHeightAndHash(blockHeight uint64, hash string) (uint64, bool)
SetHeaderDAIncluded(hash string, daHeight uint64, blockHeight uint64)
RemoveHeaderDAIncluded(hash string)

Expand All @@ -43,6 +44,7 @@ type CacheManager interface {
SetDataSeen(hash string, blockHeight uint64)
GetDataDAIncludedByHash(daCommitmentHash string) (uint64, bool)
GetDataDAIncludedByHeight(blockHeight uint64) (uint64, bool)
GetDataDAIncludedByHeightAndHash(blockHeight uint64, daCommitmentHash string) (uint64, bool)
SetDataDAIncluded(daCommitmentHash string, daHeight uint64, blockHeight uint64)
RemoveDataDAIncluded(hash string)

Expand Down Expand Up @@ -157,6 +159,10 @@ func (m *implementation) GetHeaderDAIncludedByHeight(blockHeight uint64) (uint64
return m.headerCache.getDAIncludedByHeight(blockHeight)
}

func (m *implementation) GetHeaderDAIncludedByHeightAndHash(blockHeight uint64, hash string) (uint64, bool) {
return m.headerCache.getDAIncludedByHeightAndHash(blockHeight, hash)
}

func (m *implementation) SetHeaderDAIncluded(hash string, daHeight uint64, blockHeight uint64) {
m.headerCache.setDAIncluded(hash, daHeight, blockHeight)
}
Expand Down Expand Up @@ -186,6 +192,10 @@ func (m *implementation) GetDataDAIncludedByHeight(blockHeight uint64) (uint64,
return m.dataCache.getDAIncludedByHeight(blockHeight)
}

func (m *implementation) GetDataDAIncludedByHeightAndHash(blockHeight uint64, daCommitmentHash string) (uint64, bool) {
return m.dataCache.getDAIncludedByHeightAndHash(blockHeight, daCommitmentHash)
}

func (m *implementation) SetDataDAIncluded(daCommitmentHash string, daHeight uint64, blockHeight uint64) {
m.dataCache.setDAIncluded(daCommitmentHash, daHeight, blockHeight)
}
Expand Down
23 changes: 13 additions & 10 deletions block/internal/submitting/submitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,20 @@ func (s *Submitter) processDAInclusionLoop() {
nextHeight := currentDAIncluded + 1

// Get block data first
_, data, err := s.store.GetBlockData(s.ctx, nextHeight)
header, data, err := s.store.GetBlockData(s.ctx, nextHeight)
if err != nil {
break
}

// Check if this height is DA included
if included, err := s.IsHeightDAIncluded(nextHeight, data); err != nil || !included {
if included, err := s.IsHeightDAIncluded(nextHeight, header, data); err != nil || !included {
break
}

s.logger.Debug().Uint64("height", nextHeight).Msg("advancing DA included height")

// Set node height to DA height mapping using already retrieved data
if err := s.setNodeHeightToDAHeight(s.ctx, nextHeight, data, currentDAIncluded == 0); err != nil {
if err := s.setNodeHeightToDAHeight(s.ctx, nextHeight, header, data, currentDAIncluded == 0); err != nil {
s.logger.Error().Err(err).Uint64("height", nextHeight).Msg("failed to set node height to DA height mapping")
break
}
Expand Down Expand Up @@ -446,10 +446,10 @@ func (s *Submitter) sendCriticalError(err error) {

// setNodeHeightToDAHeight persists the DA heights for a block's header and data.
// For empty-tx blocks, both use the header DA height since no data blob is posted.
func (s *Submitter) setNodeHeightToDAHeight(ctx context.Context, height uint64, data *types.Data, genesisInclusion bool) error {
func (s *Submitter) setNodeHeightToDAHeight(ctx context.Context, height uint64, header *types.SignedHeader, data *types.Data, genesisInclusion bool) error {
dataHash := data.DACommitment()

daHeightForHeader, ok := s.cache.GetHeaderDAIncludedByHeight(height)
daHeightForHeader, ok := s.cache.GetHeaderDAIncludedByHeightAndHash(height, header.Hash().String())
if !ok {
return fmt.Errorf("header for height %d not found in cache", height)
}
Expand All @@ -462,7 +462,7 @@ func (s *Submitter) setNodeHeightToDAHeight(ctx context.Context, height uint64,
// For empty transactions, use the same DA height as the header.
dataDAHeight := daHeightForHeader
if !bytes.Equal(dataHash, common.DataHashForEmptyTxs) {
daHeightForData, ok := s.cache.GetDataDAIncludedByHeight(height)
daHeightForData, ok := s.cache.GetDataDAIncludedByHeightAndHash(height, dataHash.String())
if !ok {
return fmt.Errorf("data for height %d not found in cache", height)
}
Expand All @@ -488,8 +488,11 @@ func (s *Submitter) setNodeHeightToDAHeight(ctx context.Context, height uint64,
return nil
}

// IsHeightDAIncluded reports whether the block at height has been DA-included.
func (s *Submitter) IsHeightDAIncluded(height uint64, data *types.Data) (bool, error) {
// IsHeightDAIncluded reports whether the stored block at height has been
// DA-included. The markers must be for this block: a header or data at the
// same height from another proposer, e.g. one rotated out before it was
// applied, does not count.
func (s *Submitter) IsHeightDAIncluded(height uint64, header *types.SignedHeader, data *types.Data) (bool, error) {
// Already finalized — cache entries were cleared, but we know it's included.
if height <= s.GetDAIncludedHeight() {
return true, nil
Expand All @@ -506,8 +509,8 @@ func (s *Submitter) IsHeightDAIncluded(height uint64, data *types.Data) (bool, e

dataCommitment := data.DACommitment()

_, headerIncluded := s.cache.GetHeaderDAIncludedByHeight(height)
_, dataIncluded := s.cache.GetDataDAIncludedByHeight(height)
_, headerIncluded := s.cache.GetHeaderDAIncludedByHeightAndHash(height, header.Hash().String())
_, dataIncluded := s.cache.GetDataDAIncludedByHeightAndHash(height, dataCommitment.String())

dataIncluded = bytes.Equal(dataCommitment, common.DataHashForEmptyTxs) || dataIncluded

Expand Down
63 changes: 49 additions & 14 deletions block/internal/submitting/submitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestSubmitter_IsHeightDAIncluded(t *testing.T) {

h1, d1 := newHeaderAndData("chain", 3, true)
h2, d2 := newHeaderAndData("chain", 4, true)
_, d3 := newHeaderAndData("chain", 2, true) // already DA included, cache was cleared
h3, d3 := newHeaderAndData("chain", 2, true) // already DA included, cache was cleared

cm.SetHeaderDAIncluded(h1.Hash().String(), 100, 3)
cm.SetDataDAIncluded(d1.DACommitment().String(), 100, 3)
Expand All @@ -134,20 +134,21 @@ func TestSubmitter_IsHeightDAIncluded(t *testing.T) {

specs := map[string]struct {
height uint64
header *types.SignedHeader
data *types.Data
exp bool
expErr bool
}{
"below store height and cached": {height: 3, data: d1, exp: true},
"above store height": {height: 6, data: d2, exp: false},
"data missing": {height: 4, data: d2, exp: false},
"at daIncludedHeight - cache cleared": {height: 2, data: d3, exp: true},
"below daIncludedHeight - cache cleared": {height: 1, data: d3, exp: true},
"below store height and cached": {height: 3, header: h1, data: d1, exp: true},
"above store height": {height: 6, header: h2, data: d2, exp: false},
"data missing": {height: 4, header: h2, data: d2, exp: false},
"at daIncludedHeight - cache cleared": {height: 2, header: h3, data: d3, exp: true},
"below daIncludedHeight - cache cleared": {height: 1, header: h3, data: d3, exp: true},
}

for name, spec := range specs {
t.Run(name, func(t *testing.T) {
included, err := s.IsHeightDAIncluded(spec.height, spec.data)
included, err := s.IsHeightDAIncluded(spec.height, spec.header, spec.data)
if spec.expErr {
require.Error(t, err)
return
Expand All @@ -158,6 +159,40 @@ func TestSubmitter_IsHeightDAIncluded(t *testing.T) {
}
}

func TestSubmitter_IsHeightDAIncluded_IgnoresMarkersForAnotherBlock(t *testing.T) {
ctx := t.Context()
cm, st := newTestCacheAndStore(t)
batch, err := st.NewBatch(ctx)
require.NoError(t, err)
require.NoError(t, batch.SetHeight(5))
require.NoError(t, batch.Commit())

s := &Submitter{store: st, cache: cm, logger: zerolog.Nop(), daIncludedHeight: &atomic.Uint64{}}
s.ctx = ctx

// Markers left by another proposer's block at height 5, e.g. one rotated
// out before its block was applied.
otherHeader, otherData := newHeaderAndData("chain", 5, true)
cm.SetHeaderDAIncluded(otherHeader.Hash().String(), 100, 5)
cm.SetDataDAIncluded(otherData.DACommitment().String(), 100, 5)

header, data := newHeaderAndData("chain", 5, true)
included, err := s.IsHeightDAIncluded(5, header, data)
require.NoError(t, err)
assert.False(t, included)

cm.SetHeaderDAIncluded(header.Hash().String(), 101, 5)
included, err = s.IsHeightDAIncluded(5, header, data)
require.NoError(t, err)
assert.False(t, included, "the data marker is still the other block's")
assert.Error(t, s.setNodeHeightToDAHeight(ctx, 5, header, data, false))

cm.SetDataDAIncluded(data.DACommitment().String(), 101, 5)
included, err = s.IsHeightDAIncluded(5, header, data)
require.NoError(t, err)
assert.True(t, included)
}

func TestSubmitter_setSequencerHeightToDAHeight(t *testing.T) {
ctx := t.Context()
cm, _ := newTestCacheAndStore(t)
Expand Down Expand Up @@ -199,7 +234,7 @@ func TestSubmitter_setSequencerHeightToDAHeight(t *testing.T) {
mockStore.On("SetMetadata", mock.Anything, dataKey, dBz).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, store.GenesisDAHeightKey, gBz).Return(nil).Once()

require.NoError(t, s.setNodeHeightToDAHeight(ctx, 1, d, true))
require.NoError(t, s.setNodeHeightToDAHeight(ctx, 1, h, d, true))
}

func TestSubmitter_setNodeHeightToDAHeight_Errors(t *testing.T) {
Expand All @@ -213,11 +248,11 @@ func TestSubmitter_setNodeHeightToDAHeight_Errors(t *testing.T) {
// No cache entries -> expect error on missing header
_, ok := cm.GetHeaderDAIncludedByHash(h.Hash().String())
assert.False(t, ok)
assert.Error(t, s.setNodeHeightToDAHeight(ctx, 1, d, false))
assert.Error(t, s.setNodeHeightToDAHeight(ctx, 1, h, d, false))

// Add header, missing data
cm.SetHeaderDAIncluded(h.Hash().String(), 10, 1)
assert.Error(t, s.setNodeHeightToDAHeight(ctx, 1, d, false))
assert.Error(t, s.setNodeHeightToDAHeight(ctx, 1, h, d, false))
}

func TestSubmitter_initializeDAIncludedHeight(t *testing.T) {
Expand Down Expand Up @@ -708,7 +743,7 @@ func TestSubmitter_IsHeightDAIncluded_AfterRestart(t *testing.T) {
_, realHeaderFound := cm2.GetHeaderDAIncludedByHash(h3.Hash().String())
assert.False(t, realHeaderFound, "real hash must not be present before DA retriever re-fires")

included, err := s.IsHeightDAIncluded(3, d3)
included, err := s.IsHeightDAIncluded(3, h3, d3)
require.NoError(t, err)
assert.True(t, included,
"IsHeightDAIncluded must return true for in-flight height using snapshot placeholder, "+
Expand All @@ -721,7 +756,7 @@ func TestSubmitter_IsHeightDAIncluded_AfterRestart(t *testing.T) {
_, realHeaderFound = cm2.GetHeaderDAIncludedByHash(h3.Hash().String())
assert.True(t, realHeaderFound, "real hash must be present after DA retriever re-fires")

included, err = s.IsHeightDAIncluded(3, d3)
included, err = s.IsHeightDAIncluded(3, h3, d3)
require.NoError(t, err)
assert.True(t, included, "IsHeightDAIncluded must still return true after real hash is written")
}
Expand Down Expand Up @@ -794,7 +829,7 @@ func TestSubmitter_setNodeHeightToDAHeight_AfterRestart(t *testing.T) {
// ── Step 3: call setNodeHeightToDAHeight — must succeed via fallback ──────
// Before this fix, GetHeaderDAIncludedByHash(realHash) would miss and the function
// would return an error, stalling processDAInclusionLoop.
err = s.setNodeHeightToDAHeight(ctx, 3, d3, false)
err = s.setNodeHeightToDAHeight(ctx, 3, h3, d3, false)
require.NoError(t, err,
"setNodeHeightToDAHeight must succeed via height-based lookup before DA retriever re-fires")

Expand All @@ -815,6 +850,6 @@ func TestSubmitter_setNodeHeightToDAHeight_AfterRestart(t *testing.T) {
cm2.SetHeaderDAIncluded(h3.Hash().String(), 12, 3)
cm2.SetDataDAIncluded(d3.DACommitment().String(), 12, 3)

err = s.setNodeHeightToDAHeight(ctx, 3, d3, false)
err = s.setNodeHeightToDAHeight(ctx, 3, h3, d3, false)
require.NoError(t, err, "setNodeHeightToDAHeight must still work once real hashes are populated")
}
3 changes: 1 addition & 2 deletions block/internal/syncing/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"errors"
"fmt"

"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/types"
)

func assertValidSignedData(signedData *types.SignedData, genesis genesis.Genesis) error {
func assertValidSignedData(signedData *types.SignedData) error {
if signedData == nil || signedData.Txs == nil {
return errors.New("empty signed data")
}
Expand Down
12 changes: 8 additions & 4 deletions block/internal/syncing/da_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,20 @@ func (f *daFollower) HandleEvent(ctx context.Context, ev datypes.SubscriptionEve
}

events := f.retriever.ProcessBlobs(ctx, ev.Blobs, ev.Height)
if len(events) == 0 {
return errors.New("skip inline: no complete events") // Split namespace, subscriber rolls back
}

completed := false
for _, event := range events {
if err := f.eventSink.PipeEvent(ctx, event); err != nil {
f.logger.Warn().Err(err).Uint64("da_height", ev.Height).
Msg("failed to pipe inline event, catchup will retry")
return err // Actual pipe failure, subscriber rolls back
}
completed = completed || event.DaHeight == ev.Height
}
// Events from earlier DA heights, e.g. held for a proposer rotation, say
// nothing about this height: with split namespaces its other half may still
// be on its way.
if !completed {
return errors.New("skip inline: no complete events at this height") // subscriber rolls back
}
Comment thread
damiannolan marked this conversation as resolved.
Resolved

f.logger.Debug().Uint64("da_height", ev.Height).Int("events", len(events)).
Expand Down
13 changes: 10 additions & 3 deletions block/internal/syncing/da_follower_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func TestDAFollower_HandleEvent(t *testing.T) {
mockEvents: []common.DAHeightEvent{},
expectedError: "skip inline: no complete events",
},
{
name: "error_only_events_from_earlier_heights",
isInline: true,
blobs: [][]byte{[]byte("blob")},
mockEvents: []common.DAHeightEvent{{DaHeight: 99}},
expectedError: "skip inline: no complete events",
},
{
name: "error_pipe_fails",
isInline: true,
Expand Down Expand Up @@ -87,9 +94,9 @@ func TestDAFollower_HandleEvent(t *testing.T) {
assert.Contains(t, err.Error(), tt.expectedError)
} else {
require.NoError(t, err)
if tt.isInline && len(tt.blobs) > 0 {
assert.Len(t, pipedEvents, len(tt.mockEvents))
}
}
if tt.isInline && len(tt.blobs) > 0 {
assert.Len(t, pipedEvents, len(tt.mockEvents))
}
})
}
Expand Down
Loading
Loading