Skip to content

Commit 961d33c

Browse files
authored
Merge pull request #7866 from multiversx/update-context-for-replaced-header
Update context for replaced header optimization
2 parents c1086aa + 56ab9b4 commit 961d33c

2 files changed

Lines changed: 31 additions & 15 deletions

File tree

process/common.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,13 +1632,19 @@ func UpdateContextForReplacedHeader(
16321632
}
16331633

16341634
currentExecResult := blockChain.GetLastExecutionResult()
1635-
if !check.IfNil(currentExecResult) && !bytes.Equal(currentExecResult.GetHeaderHash(), executionResultToSet.GetHeaderHash()) {
1635+
if !check.IfNil(currentExecResult) {
1636+
if bytes.Equal(currentExecResult.GetHeaderHash(), executionResultToSet.GetHeaderHash()) {
1637+
// already at the desired state
1638+
return nil
1639+
}
1640+
16361641
err = CleanCachesForExecutionResult(currentExecResult, postProcessTransactions, executedMiniBlocks)
16371642
if err != nil {
16381643
return err
16391644
}
16401645
}
16411646

1647+
16421648
log.Debug("UpdateContextForReplacedHeader last executed header",
16431649
"round", headerToSet.GetRound(),
16441650
"nonce", headerToSet.GetNonce(),

process/common_test.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3584,9 +3584,9 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
35843584
0,
35853585
)
35863586
require.Nil(t, err)
3587-
require.True(t, setExecutedCalled)
3588-
require.True(t, setExecutionResultCalled)
3589-
require.True(t, removePendingCalled)
3587+
require.False(t, setExecutedCalled)
3588+
require.False(t, setExecutionResultCalled)
3589+
require.False(t, removePendingCalled)
35903590
})
35913591

35923592
t.Run("nil storage should error", func(t *testing.T) {
@@ -3767,6 +3767,13 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
37673767
t.Run("successful path with lastNotarizedResult", func(t *testing.T) {
37683768
t.Parallel()
37693769

3770+
prevNotarizedResult := &block.BaseExecutionResult{
3771+
HeaderHash: []byte("prevNotarizedHash"),
3772+
HeaderNonce: 4,
3773+
HeaderRound: 4,
3774+
RootHash: []byte("prevNotarizedRoot"),
3775+
}
3776+
37703777
lastNotarizedResult := &block.BaseExecutionResult{
37713778
HeaderHash: []byte("lastNotarizedHash"),
37723779
HeaderNonce: 5,
@@ -3776,9 +3783,6 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
37763783

37773784
removePendingCalled := false
37783785
executionManager := &processMocks.ExecutionManagerMock{
3779-
GetPendingExecutionResultsCalled: func() ([]data.BaseExecutionResultHandler, error) {
3780-
return []data.BaseExecutionResultHandler{}, nil
3781-
},
37823786
GetLastNotarizedExecutionResultCalled: func() (data.BaseExecutionResultHandler, error) {
37833787
return lastNotarizedResult, nil
37843788
},
@@ -3789,15 +3793,14 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
37893793
},
37903794
}
37913795

3796+
headerToSet := &block.HeaderV3{
3797+
Nonce: 5,
3798+
}
37923799
header := &block.HeaderV3{
37933800
Nonce: 6,
37943801
PrevHash: []byte("lastNotarizedHash"),
37953802
}
37963803

3797-
headerToSet := &block.HeaderV3{
3798-
Nonce: 5,
3799-
}
3800-
38013804
setLastExecutedCalled := false
38023805
setLastExecutionResultCalled := false
38033806
blockChain := &testscommon.ChainHandlerStub{
@@ -3813,7 +3816,7 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
38133816
},
38143817
GetLastExecutionResultCalled: func() data.BaseExecutionResultHandler {
38153818
return &block.ExecutionResult{
3816-
BaseExecutionResult: lastNotarizedResult,
3819+
BaseExecutionResult: prevNotarizedResult,
38173820
}
38183821
},
38193822
}
@@ -3941,12 +3944,19 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
39413944
RootHash: []byte("lastNotarizedRoot"),
39423945
}
39433946

3947+
currentExecResult := &block.BaseExecutionResult{
3948+
HeaderHash: []byte("currentExecResultHash"),
3949+
HeaderNonce: 6,
3950+
HeaderRound: 6,
3951+
RootHash: []byte("currentExecResultRootHash"),
3952+
}
3953+
39443954
executionManager := &processMocks.ExecutionManagerMock{
39453955
GetPendingExecutionResultsCalled: func() ([]data.BaseExecutionResultHandler, error) {
39463956
return []data.BaseExecutionResultHandler{}, nil
39473957
},
39483958
GetLastNotarizedExecutionResultCalled: func() (data.BaseExecutionResultHandler, error) {
3949-
return lastNotarizedResult, nil
3959+
return currentExecResult, nil
39503960
},
39513961
RemovePendingExecutionResultsFromNonceCalled: func(nonce uint64) error {
39523962
return errExpected
@@ -3955,7 +3965,7 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
39553965

39563966
header := &block.HeaderV3{
39573967
Nonce: 6,
3958-
PrevHash: []byte("lastNotarizedHash"),
3968+
PrevHash: []byte("currentExecResultHash"),
39593969
}
39603970

39613971
headerToSet := &block.HeaderV3{
@@ -3976,7 +3986,7 @@ func Test_UpdateContextForReplacedHeader(t *testing.T) {
39763986

39773987
headersPool := &mock.HeadersCacherStub{
39783988
GetHeaderByHashCalled: func(hash []byte) (data.HeaderHandler, error) {
3979-
if bytes.Equal(hash, []byte("lastNotarizedHash")) {
3989+
if bytes.Equal(hash, []byte("currentExecResultHash")) {
39803990
return headerToSet, nil
39813991
}
39823992
return nil, errors.New("not found")

0 commit comments

Comments
 (0)