@@ -3,6 +3,7 @@ package syncing
33import (
44 "context"
55 crand "crypto/rand"
6+ "crypto/sha512"
67 "errors"
78 "testing"
89 "time"
@@ -44,7 +45,17 @@ func buildSyncTestSigner(tb testing.TB) (addr []byte, pub crypto.PubKey, signer
4445}
4546
4647// makeSignedHeaderBytes builds a valid SignedHeader and returns its binary encoding and the object
47- func makeSignedHeaderBytes (tb testing.TB , chainID string , height uint64 , proposer []byte , pub crypto.PubKey , signer signerpkg.Signer , appHash []byte , data * types.Data ) ([]byte , * types.SignedHeader ) {
48+ func makeSignedHeaderBytes (
49+ tb testing.TB ,
50+ chainID string ,
51+ height uint64 ,
52+ proposer []byte ,
53+ pub crypto.PubKey ,
54+ signer signerpkg.Signer ,
55+ appHash []byte ,
56+ data * types.Data ,
57+ lastHeaderHash []byte ,
58+ ) ([]byte , * types.SignedHeader ) {
4859 time := uint64 (time .Now ().UnixNano ())
4960 dataHash := common .DataHashForEmptyTxs
5061 if data != nil {
@@ -58,6 +69,7 @@ func makeSignedHeaderBytes(tb testing.TB, chainID string, height uint64, propose
5869 AppHash : appHash ,
5970 DataHash : dataHash ,
6071 ProposerAddress : proposer ,
72+ LastHeaderHash : lastHeaderHash ,
6173 },
6274 Signer : types.Signer {PubKey : pub , Address : proposer },
6375 }
@@ -97,7 +109,6 @@ func TestSyncer_validateBlock_DataHashMismatch(t *testing.T) {
97109
98110 cfg := config .DefaultConfig ()
99111 gen := genesis.Genesis {ChainID : "tchain" , InitialHeight : 1 , StartTime : time .Now ().Add (- time .Second ), ProposerAddress : addr }
100-
101112 mockExec := testmocks .NewMockExecutor (t )
102113
103114 s := NewSyncer (
@@ -114,24 +125,24 @@ func TestSyncer_validateBlock_DataHashMismatch(t *testing.T) {
114125 common .DefaultBlockOptions (),
115126 make (chan error , 1 ),
116127 )
117-
128+ require . NoError ( t , s . initializeState ())
118129 // Create header and data with correct hash
119130 data := makeData (gen .ChainID , 1 , 2 ) // non-empty
120- _ , header := makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , nil , data )
131+ _ , header := makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , nil , data , nil )
121132
122- err = s .validateBlock (header , data )
133+ err = s .validateBlock (s . GetLastState () , data , header )
123134 require .NoError (t , err )
124135
125136 // Create header and data with mismatched hash
126137 data = makeData (gen .ChainID , 1 , 2 ) // non-empty
127- _ , header = makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , nil , nil )
128- err = s .validateBlock (header , data )
138+ _ , header = makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , nil , nil , nil )
139+ err = s .validateBlock (s . GetLastState () , data , header )
129140 require .Error (t , err )
130141
131142 // Create header and empty data
132143 data = makeData (gen .ChainID , 1 , 0 ) // empty
133- _ , header = makeSignedHeaderBytes (t , gen .ChainID , 2 , addr , pub , signer , nil , nil )
134- err = s .validateBlock (header , data )
144+ _ , header = makeSignedHeaderBytes (t , gen .ChainID , 2 , addr , pub , signer , nil , nil , nil )
145+ err = s .validateBlock (s . GetLastState () , data , header )
135146 require .Error (t , err )
136147}
137148
@@ -169,7 +180,7 @@ func TestProcessHeightEvent_SyncsAndUpdatesState(t *testing.T) {
169180 // Create signed header & data for height 1
170181 lastState := s .GetLastState ()
171182 data := makeData (gen .ChainID , 1 , 0 )
172- _ , hdr := makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , lastState .AppHash , data )
183+ _ , hdr := makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , lastState .AppHash , data , nil )
173184
174185 // Expect ExecuteTxs call for height 1
175186 mockExec .EXPECT ().ExecuteTxs (mock .Anything , mock .Anything , uint64 (1 ), mock .Anything , lastState .AppHash ).
@@ -218,7 +229,7 @@ func TestSequentialBlockSync(t *testing.T) {
218229 // Sync two consecutive blocks via processHeightEvent so ExecuteTxs is called and state stored
219230 st0 := s .GetLastState ()
220231 data1 := makeData (gen .ChainID , 1 , 1 ) // non-empty
221- _ , hdr1 := makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , st0 .AppHash , data1 )
232+ _ , hdr1 := makeSignedHeaderBytes (t , gen .ChainID , 1 , addr , pub , signer , st0 .AppHash , data1 , st0 . LastHeaderHash )
222233 // Expect ExecuteTxs call for height 1
223234 mockExec .EXPECT ().ExecuteTxs (mock .Anything , mock .Anything , uint64 (1 ), mock .Anything , st0 .AppHash ).
224235 Return ([]byte ("app1" ), uint64 (1024 ), nil ).Once ()
@@ -227,7 +238,7 @@ func TestSequentialBlockSync(t *testing.T) {
227238
228239 st1 , _ := st .GetState (context .Background ())
229240 data2 := makeData (gen .ChainID , 2 , 0 ) // empty data
230- _ , hdr2 := makeSignedHeaderBytes (t , gen .ChainID , 2 , addr , pub , signer , st1 .AppHash , data2 )
241+ _ , hdr2 := makeSignedHeaderBytes (t , gen .ChainID , 2 , addr , pub , signer , st1 .AppHash , data2 , st1 . LastHeaderHash )
231242 // Expect ExecuteTxs call for height 2
232243 mockExec .EXPECT ().ExecuteTxs (mock .Anything , mock .Anything , uint64 (2 ), mock .Anything , st1 .AppHash ).
233244 Return ([]byte ("app2" ), uint64 (1024 ), nil ).Once ()
@@ -365,22 +376,27 @@ func TestSyncLoopPersistState(t *testing.T) {
365376 syncerInst1 .daRetriever , syncerInst1 .p2pHandler = daRtrMock , p2pHndlMock
366377
367378 // with n da blobs fetched
379+ var prevHeaderHash , prevAppHash []byte
368380 for i := range myFutureDAHeight - myDAHeightOffset {
369- chainHeight , daHeight := i , i + myDAHeightOffset
381+ chainHeight , daHeight := i + 1 , i + myDAHeightOffset
370382 emptyData := & types.Data {
371383 Metadata : & types.Metadata {
372384 ChainID : gen .ChainID ,
373385 Height : chainHeight ,
374386 Time : uint64 (time .Now ().Add (time .Duration (chainHeight ) * time .Second ).UnixNano ()),
375387 },
376388 }
377- _ , sigHeader := makeSignedHeaderBytes (t , gen .ChainID , chainHeight , addr , pub , signer , nil , emptyData )
389+ _ , sigHeader := makeSignedHeaderBytes (t , gen .ChainID , chainHeight , addr , pub , signer , prevAppHash , emptyData , prevHeaderHash )
378390 evts := []common.DAHeightEvent {{
379391 Header : sigHeader ,
380392 Data : emptyData ,
381393 DaHeight : daHeight ,
382394 }}
383395 daRtrMock .On ("RetrieveFromDA" , mock .Anything , daHeight ).Return (evts , nil )
396+ prevHeaderHash = sigHeader .Hash ()
397+ hasher := sha512 .New ()
398+ hasher .Write (prevAppHash )
399+ prevAppHash = hasher .Sum (nil )
384400 }
385401
386402 // stop at next height
@@ -395,7 +411,6 @@ func TestSyncLoopPersistState(t *testing.T) {
395411 Return (nil , coreda .ErrHeightFromFuture )
396412
397413 go syncerInst1 .processLoop ()
398-
399414 // dssync from DA until stop height reached
400415 syncerInst1 .syncLoop ()
401416 t .Log ("syncLoop on instance1 completed" )
0 commit comments