Skip to content

Commit bdcbe71

Browse files
authored
Merge pull request #7019 from multiversx/barnard-to-gov-29-may
Barnard to gov 29 may
2 parents c02f3d1 + 97bf9ed commit bdcbe71

File tree

2 files changed

+213
-1
lines changed

2 files changed

+213
-1
lines changed

update/sync/syncEpochStartShardHeaders.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func (p *pendingEpochStartShardHeader) syncEpochStartShardHeader(shardId uint32,
132132
p.mutPending.Unlock()
133133
return nil
134134
case <-p.chNew:
135+
p.mutPending.RLock()
135136
nonce = p.latestReceivedHeader.GetNonce()
137+
p.mutPending.RUnlock()
136138
continue
137139
case <-ctx.Done():
138140
p.mutPending.Lock()
@@ -198,8 +200,10 @@ func (p *pendingEpochStartShardHeader) receivedProof(proof data.HeaderProofHandl
198200
return
199201
}
200202
p.latestReceivedProof = proof
203+
lastReceivedHeader := p.latestReceivedHeader
204+
lastReceivedHash := p.latestReceivedHash
201205
p.mutPending.Unlock()
202-
p.updateReceivedHeaderAndProof(p.latestReceivedHeader, p.latestReceivedHash)
206+
p.updateReceivedHeaderAndProof(lastReceivedHeader, lastReceivedHash)
203207
}
204208

205209
// GetEpochStartHeader returns the synced epoch start header

update/sync/syncEpochStartShardHeaders_test.go

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ func TestNewPendingEpochStartShardHeaderSyncer_NilRequestHandler(t *testing.T) {
7575
require.Nil(t, syncer)
7676
}
7777

78+
func TestNewPendingEpochStartShardHeaderSyncer_NilProofsPool(t *testing.T) {
79+
t.Parallel()
80+
81+
args := createMockArgsPendingEpochStartShardHeader()
82+
args.ProofsPool = nil
83+
84+
syncer, err := NewPendingEpochStartShardHeaderSyncer(args)
85+
require.Equal(t, process.ErrNilProofsPool, err)
86+
require.Nil(t, syncer)
87+
}
88+
89+
func TestNewPendingEpochStartShardHeaderSyncer_NilEnableEpochsHandler(t *testing.T) {
90+
t.Parallel()
91+
92+
args := createMockArgsPendingEpochStartShardHeader()
93+
args.EnableEpochsHandler = nil
94+
95+
syncer, err := NewPendingEpochStartShardHeaderSyncer(args)
96+
require.Equal(t, process.ErrNilEnableEpochsHandler, err)
97+
require.Nil(t, syncer)
98+
}
99+
78100
func TestSyncEpochStartShardHeader_Success(t *testing.T) {
79101
t.Parallel()
80102

@@ -432,6 +454,192 @@ func TestPendingEpochStartShardHeader_IsInterfaceNil(t *testing.T) {
432454
require.False(t, p.IsInterfaceNil())
433455
}
434456

457+
func TestSyncEpochStartShardHeader_TwoConsecutiveProofsWithSameHeaderHash(t *testing.T) {
458+
t.Parallel()
459+
460+
shardID := uint32(1)
461+
epoch := uint32(10)
462+
startNonce := uint64(100)
463+
464+
nonHeaderHash := []byte("ignoredHash")
465+
headerHash := []byte("headerHash")
466+
467+
nonEpochStartHeader := &block.Header{
468+
ShardID: shardID,
469+
Nonce: startNonce + 2,
470+
Epoch: epoch - 1,
471+
EpochStartMetaHash: []byte("ignoreMetaHash"),
472+
}
473+
nonEpochStartProof := &block.HeaderProof{
474+
HeaderShardId: shardID,
475+
HeaderNonce: startNonce + 2,
476+
HeaderHash: nonHeaderHash,
477+
HeaderEpoch: epoch - 1,
478+
}
479+
480+
epochStartHeader := &block.Header{
481+
ShardID: shardID,
482+
Nonce: startNonce + 2,
483+
Epoch: epoch,
484+
EpochStartMetaHash: []byte("metaHash"),
485+
}
486+
487+
args := createPendingEpochStartShardHeaderSyncerArgs()
488+
syncer, err := NewPendingEpochStartShardHeaderSyncer(args)
489+
require.Nil(t, err)
490+
491+
go func() {
492+
// first send the nonEpochStartHeader with the nonEpochStartProof
493+
syncer.receivedHeader(nonEpochStartHeader, nonHeaderHash)
494+
syncer.receivedProof(nonEpochStartProof)
495+
496+
// then, send the epochStartHeader also with the nonEpochStartProof
497+
syncer.receivedHeader(epochStartHeader, headerHash)
498+
syncer.receivedProof(nonEpochStartProof)
499+
}()
500+
501+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
502+
defer cancel()
503+
504+
err = syncer.SyncEpochStartShardHeader(shardID, epoch, startNonce, ctx)
505+
require.Equal(t, update.ErrTimeIsOut, err)
506+
507+
_, _, errGet := syncer.GetEpochStartHeader()
508+
require.Equal(t, update.ErrNotSynced, errGet)
509+
}
510+
511+
func TestSyncEpochStartShardHeader_ProofsBeforeHeaderShouldWork(t *testing.T) {
512+
t.Parallel()
513+
514+
shardID := uint32(1)
515+
epoch := uint32(10)
516+
startNonce := uint64(100)
517+
518+
headerHash := []byte("epochStartHash")
519+
520+
epochStartHeader := &block.Header{
521+
ShardID: shardID,
522+
Nonce: startNonce + 2,
523+
Epoch: epoch,
524+
EpochStartMetaHash: []byte("metaHash"),
525+
}
526+
epochStartProof := &block.HeaderProof{
527+
HeaderShardId: shardID,
528+
HeaderNonce: startNonce + 2,
529+
HeaderHash: headerHash,
530+
HeaderEpoch: epoch,
531+
}
532+
533+
headersPool := &mock.HeadersCacherStub{}
534+
proofsPool := &dataRetrieverMocks.ProofsPoolMock{
535+
HasProofCalled: func(shardID uint32, headerHash []byte) bool {
536+
return true
537+
},
538+
}
539+
args := ArgsPendingEpochStartShardHeaderSyncer{
540+
HeadersPool: headersPool,
541+
Marshalizer: &mock.MarshalizerFake{},
542+
RequestHandler: &testscommon.RequestHandlerStub{
543+
RequestShardHeaderByNonceCalled: func(shardID uint32, nonce uint64) {},
544+
},
545+
ProofsPool: proofsPool,
546+
EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{
547+
IsFlagEnabledInEpochCalled: func(flag core.EnableEpochFlag, epoch uint32) bool {
548+
return flag == common.AndromedaFlag
549+
},
550+
IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool {
551+
return flag == common.AndromedaFlag
552+
},
553+
},
554+
}
555+
syncer, err := NewPendingEpochStartShardHeaderSyncer(args)
556+
require.Nil(t, err)
557+
558+
go func() {
559+
// first receive proof
560+
syncer.receivedProof(epochStartProof)
561+
// then receive header
562+
syncer.receivedHeader(epochStartHeader, headerHash)
563+
}()
564+
565+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
566+
defer cancel()
567+
568+
err = syncer.SyncEpochStartShardHeader(shardID, epoch, startNonce, ctx)
569+
require.Nil(t, err)
570+
571+
h, hHash, errGet := syncer.GetEpochStartHeader()
572+
require.Nil(t, errGet)
573+
require.Equal(t, epochStartHeader, h)
574+
require.Equal(t, headerHash, hHash)
575+
}
576+
577+
func TestSyncEpochStartShardHeader_ShouldWorkWithoutAndromedaActivated(t *testing.T) {
578+
t.Parallel()
579+
580+
shardID := uint32(1)
581+
epoch := uint32(10)
582+
startNonce := uint64(100)
583+
584+
headerHash := []byte("epochStartHash")
585+
586+
epochStartHeader := &block.Header{
587+
ShardID: shardID,
588+
Nonce: startNonce + 2,
589+
Epoch: epoch,
590+
EpochStartMetaHash: []byte("metaHash"),
591+
}
592+
epochStartProof := &block.HeaderProof{
593+
HeaderShardId: shardID,
594+
HeaderNonce: startNonce + 2,
595+
HeaderHash: headerHash,
596+
HeaderEpoch: epoch,
597+
}
598+
599+
headersPool := &mock.HeadersCacherStub{}
600+
proofsPool := &dataRetrieverMocks.ProofsPoolMock{
601+
HasProofCalled: func(shardID uint32, headerHash []byte) bool {
602+
return false
603+
},
604+
}
605+
args := ArgsPendingEpochStartShardHeaderSyncer{
606+
HeadersPool: headersPool,
607+
Marshalizer: &mock.MarshalizerFake{},
608+
RequestHandler: &testscommon.RequestHandlerStub{
609+
RequestShardHeaderByNonceCalled: func(shardID uint32, nonce uint64) {},
610+
},
611+
ProofsPool: proofsPool,
612+
EnableEpochsHandler: &enableEpochsHandlerMock.EnableEpochsHandlerStub{
613+
IsFlagEnabledInEpochCalled: func(flag core.EnableEpochFlag, epoch uint32) bool {
614+
return false
615+
},
616+
IsFlagEnabledCalled: func(flag core.EnableEpochFlag) bool {
617+
return flag == common.AndromedaFlag
618+
},
619+
},
620+
}
621+
syncer, err := NewPendingEpochStartShardHeaderSyncer(args)
622+
require.Nil(t, err)
623+
624+
go func() {
625+
// first receive proof
626+
syncer.receivedProof(epochStartProof)
627+
// then receive header
628+
syncer.receivedHeader(epochStartHeader, headerHash)
629+
}()
630+
631+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
632+
defer cancel()
633+
634+
err = syncer.SyncEpochStartShardHeader(shardID, epoch, startNonce, ctx)
635+
require.Nil(t, err)
636+
637+
h, hHash, errGet := syncer.GetEpochStartHeader()
638+
require.Nil(t, errGet)
639+
require.Equal(t, epochStartHeader, h)
640+
require.Equal(t, headerHash, hHash)
641+
}
642+
435643
func createPendingEpochStartShardHeaderSyncerArgs() ArgsPendingEpochStartShardHeaderSyncer {
436644
headersPool := &mock.HeadersCacherStub{}
437645
proofsPool := &dataRetrieverMocks.ProofsPoolMock{}

0 commit comments

Comments
 (0)