Skip to content

Commit 8144b39

Browse files
committed
add test
1 parent 73f58e4 commit 8144b39

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

block/internal/syncing/da_retriever.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ type DARetriever struct {
3737
namespaceBz []byte
3838
namespaceDataBz []byte
3939

40-
// todo: ensure that these indexes are filled on restart
40+
// transient cache, lost on restart, but this is ok
41+
// because da height only updated when new blocks are processed.
4142
pendingHeaders map[uint64]*types.SignedHeader
4243
pendingData map[uint64]*types.Data
4344
headerDAHeights map[uint64]uint64

block/internal/syncing/da_retriever_test.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ import (
2828
)
2929

3030
// makeSignedHeaderBytes builds a valid SignedHeader and returns its binary encoding and the object
31-
func makeSignedHeaderBytes(t *testing.T, chainID string, height uint64, proposer []byte, pub crypto.PubKey, signer signerpkg.Signer, appHash []byte) ([]byte, *types.SignedHeader) {
31+
func makeSignedHeaderBytes(t *testing.T, chainID string, height uint64, proposer []byte, pub crypto.PubKey, signer signerpkg.Signer, appHash []byte, dataHash []byte) ([]byte, *types.SignedHeader) {
3232
hdr := &types.SignedHeader{
3333
Header: types.Header{
3434
BaseHeader: types.BaseHeader{ChainID: chainID, Height: height, Time: uint64(time.Now().Add(time.Duration(height) * time.Second).UnixNano())},
3535
AppHash: appHash,
36+
DataHash: dataHash,
3637
ProposerAddress: proposer,
3738
},
3839
Signer: types.Signer{PubKey: pub, Address: proposer},
@@ -115,12 +116,8 @@ func TestDARetriever_ProcessBlobs_HeaderAndData_Success(t *testing.T) {
115116

116117
r := NewDARetriever(nil, cm, config.DefaultConfig(), gen, common.DefaultBlockOptions(), zerolog.Nop())
117118

118-
// Build one header and one data blob at same height
119-
_, lastState := types.State{}, types.State{}
120-
_ = lastState // placeholder to keep parity with helper pattern
121-
122-
hdrBin, _ := makeSignedHeaderBytes(t, gen.ChainID, 2, addr, pub, signer, nil)
123-
dataBin, _ := makeSignedDataBytes(t, gen.ChainID, 2, addr, pub, signer, 2)
119+
dataBin, data := makeSignedDataBytes(t, gen.ChainID, 2, addr, pub, signer, 2)
120+
hdrBin, _ := makeSignedHeaderBytes(t, gen.ChainID, 2, addr, pub, signer, nil, data.Hash())
124121

125122
events := r.processBlobs(context.Background(), [][]byte{hdrBin, dataBin}, 77)
126123
require.Len(t, events, 1)
@@ -141,7 +138,7 @@ func TestDARetriever_ProcessBlobs_HeaderOnly_EmptyDataExpected(t *testing.T) {
141138
r := NewDARetriever(nil, cm, config.DefaultConfig(), gen, common.DefaultBlockOptions(), zerolog.Nop())
142139

143140
// Header with no data hash present should trigger empty data creation (per current logic)
144-
hb, _ := makeSignedHeaderBytes(t, gen.ChainID, 3, addr, pub, signer, nil)
141+
hb, _ := makeSignedHeaderBytes(t, gen.ChainID, 3, addr, pub, signer, nil, nil)
145142

146143
events := r.processBlobs(context.Background(), [][]byte{hb}, 88)
147144
require.Len(t, events, 1)
@@ -160,7 +157,7 @@ func TestDARetriever_TryDecodeHeaderAndData_Basic(t *testing.T) {
160157
gen := genesis.Genesis{ChainID: "tchain", InitialHeight: 1, StartTime: time.Now().Add(-time.Second), ProposerAddress: addr}
161158
r := NewDARetriever(nil, cm, config.DefaultConfig(), gen, common.DefaultBlockOptions(), zerolog.Nop())
162159

163-
hb, sh := makeSignedHeaderBytes(t, gen.ChainID, 5, addr, pub, signer, nil)
160+
hb, sh := makeSignedHeaderBytes(t, gen.ChainID, 5, addr, pub, signer, nil, nil)
164161
gotH := r.tryDecodeHeader(hb, 123)
165162
require.NotNil(t, gotH)
166163
assert.Equal(t, sh.Hash().String(), gotH.Hash().String())
@@ -225,8 +222,8 @@ func TestDARetriever_RetrieveFromDA_TwoNamespaces_Success(t *testing.T) {
225222
gen := genesis.Genesis{ChainID: "tchain", InitialHeight: 1, StartTime: time.Now().Add(-time.Second), ProposerAddress: addr}
226223

227224
// Prepare header/data blobs
228-
hdrBin, _ := makeSignedHeaderBytes(t, gen.ChainID, 9, addr, pub, signer, nil)
229-
dataBin, _ := makeSignedDataBytes(t, gen.ChainID, 9, addr, pub, signer, 1)
225+
dataBin, data := makeSignedDataBytes(t, gen.ChainID, 9, addr, pub, signer, 1)
226+
hdrBin, _ := makeSignedHeaderBytes(t, gen.ChainID, 9, addr, pub, signer, nil, data.Hash())
230227

231228
cfg := config.DefaultConfig()
232229
cfg.DA.Namespace = "nsHdr"
@@ -255,3 +252,43 @@ func TestDARetriever_RetrieveFromDA_TwoNamespaces_Success(t *testing.T) {
255252
assert.Equal(t, uint64(9), events[0].Header.Height())
256253
assert.Equal(t, uint64(9), events[0].Data.Height())
257254
}
255+
256+
func TestDARetriever_ProcessBlobs_CrossDAHeightMatching(t *testing.T) {
257+
ds := dssync.MutexWrap(datastore.NewMapDatastore())
258+
st := store.New(ds)
259+
cm, err := cache.NewManager(config.DefaultConfig(), st, zerolog.Nop())
260+
require.NoError(t, err)
261+
262+
addr, pub, signer := buildSyncTestSigner(t)
263+
gen := genesis.Genesis{ChainID: "tchain", InitialHeight: 1, StartTime: time.Now().Add(-time.Second), ProposerAddress: addr}
264+
265+
r := NewDARetriever(nil, cm, config.DefaultConfig(), gen, common.DefaultBlockOptions(), zerolog.Nop())
266+
267+
// Create header and data for the same block height but from different DA heights
268+
dataBin, data := makeSignedDataBytes(t, gen.ChainID, 5, addr, pub, signer, 2)
269+
hdrBin, _ := makeSignedHeaderBytes(t, gen.ChainID, 5, addr, pub, signer, nil, data.Hash())
270+
271+
// Process header from DA height 100 first
272+
events1 := r.processBlobs(context.Background(), [][]byte{hdrBin}, 100)
273+
require.Len(t, events1, 0, "should not create event yet - data is missing")
274+
275+
// Verify header is stored in pending headers
276+
require.Contains(t, r.pendingHeaders, uint64(5), "header should be stored as pending")
277+
require.Contains(t, r.headerDAHeights, uint64(5), "header DA height should be tracked")
278+
assert.Equal(t, uint64(100), r.headerDAHeights[5])
279+
280+
// Process data from DA height 102
281+
events2 := r.processBlobs(context.Background(), [][]byte{dataBin}, 102)
282+
require.Len(t, events2, 1, "should create event when matching data arrives")
283+
284+
event := events2[0]
285+
assert.Equal(t, uint64(5), event.Header.Height())
286+
assert.Equal(t, uint64(5), event.Data.Height())
287+
assert.Equal(t, uint64(102), event.DaHeight, "DaHeight should be the height where data was processed")
288+
assert.Equal(t, uint64(100), event.HeaderDaIncludedHeight, "HeaderDaIncludedHeight should be where header was included")
289+
290+
// Verify pending maps are cleared
291+
require.NotContains(t, r.pendingHeaders, uint64(5), "header should be removed from pending")
292+
require.NotContains(t, r.pendingData, uint64(5), "data should be removed from pending")
293+
require.NotContains(t, r.headerDAHeights, uint64(5), "header DA height should be removed")
294+
}

block/internal/syncing/syncer_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ func TestSyncLoopPersistState(t *testing.T) {
188188
// with n da blobs fetched
189189
for i := range myFutureDAHeight - myDAHeightOffset {
190190
chainHeight, daHeight := i, i+myDAHeightOffset
191-
_, sigHeader := makeSignedHeaderBytes(t, gen.ChainID, chainHeight, addr, pub, signer, nil)
192-
//_, sigData := makeSignedDataBytes(t, gen.ChainID, chainHeight, addr, pub, signer, 1)
191+
_, sigHeader := makeSignedHeaderBytes(t, gen.ChainID, chainHeight, addr, pub, signer, nil, nil)
193192
emptyData := types.Data{
194193
Metadata: &types.Metadata{
195194
ChainID: sigHeader.ChainID(),

0 commit comments

Comments
 (0)