Skip to content

Commit c1086aa

Browse files
authored
Merge pull request #7867 from multiversx/update-chain-handler
Update chain handler
2 parents 4d94bfb + f4d7923 commit c1086aa

24 files changed

Lines changed: 187 additions & 113 deletions

dataRetriever/blockchain/baseBlockchain.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ func (bbc *baseBlockChain) GetCurrentBlockHeaderHash() []byte {
7575
return bbc.currentBlockHeaderHash
7676
}
7777

78+
// GetCurrentBlockHeaderAndHash returns the current block header and hash
79+
func (bbc *baseBlockChain) GetCurrentBlockHeaderAndHash() (data.HeaderHandler, []byte) {
80+
bbc.mut.RLock()
81+
defer bbc.mut.RUnlock()
82+
83+
if check.IfNil(bbc.currentBlockHeader) || len(bbc.currentBlockHeaderHash) == 0 {
84+
return nil, nil
85+
}
86+
87+
return bbc.currentBlockHeader.ShallowClone(), bbc.currentBlockHeaderHash
88+
}
89+
7890
// SetCurrentBlockHeaderHash returns the current block header hash
7991
func (bbc *baseBlockChain) SetCurrentBlockHeaderHash(hash []byte) {
8092
bbc.mut.Lock()
@@ -85,12 +97,15 @@ func (bbc *baseBlockChain) SetCurrentBlockHeaderHash(hash []byte) {
8597
// SetFinalBlockInfo sets the nonce, hash and rootHash associated with the previous-to-final block
8698
func (bbc *baseBlockChain) SetFinalBlockInfo(nonce uint64, headerHash []byte, rootHash []byte) {
8799
bbc.mut.Lock()
100+
defer bbc.mut.Unlock()
101+
102+
bbc.setFinalBlockInfoUnprotected(nonce, headerHash, rootHash)
103+
}
88104

105+
func (bbc *baseBlockChain) setFinalBlockInfoUnprotected(nonce uint64, headerHash []byte, rootHash []byte) {
89106
bbc.finalBlockInfo.nonce = nonce
90107
bbc.finalBlockInfo.hash = headerHash
91108
bbc.finalBlockInfo.committedRootHash = rootHash
92-
93-
bbc.mut.Unlock()
94109
}
95110

96111
// GetFinalBlockInfo returns the nonce, hash and rootHash associated with the previous-to-final block
@@ -138,6 +153,14 @@ func (bbc *baseBlockChain) SetLastExecutedBlockHeaderAndRootHash(
138153
bbc.mut.Lock()
139154
defer bbc.mut.Unlock()
140155

156+
bbc.setLastExecutedBlockHeaderAndRootHashUnprotected(header, headerHash, rootHash)
157+
}
158+
159+
func (bbc *baseBlockChain) setLastExecutedBlockHeaderAndRootHashUnprotected(
160+
header data.HeaderHandler,
161+
headerHash []byte,
162+
rootHash []byte,
163+
) {
141164
if check.IfNil(header) {
142165
bbc.lastExecutedBlockHeader = nil
143166
bbc.lastExecutedBlockInfo.nonce = 0
@@ -162,11 +185,24 @@ func (bbc *baseBlockChain) GetLastExecutionResult() data.BaseExecutionResultHand
162185
return bbc.lastExecutionResult
163186
}
164187

165-
// SetLastExecutionResult sets the last execution result
166-
func (bbc *baseBlockChain) SetLastExecutionResult(result data.BaseExecutionResultHandler) {
188+
// SetLastExecutionInfo sets header, execution result and final block info atomically
189+
func (bbc *baseBlockChain) SetLastExecutionInfo(
190+
header data.HeaderHandler,
191+
result data.BaseExecutionResultHandler,
192+
) {
193+
if check.IfNil(header) || check.IfNil(result) {
194+
return
195+
}
196+
167197
bbc.mut.Lock()
168198
defer bbc.mut.Unlock()
169199

200+
headerHash := result.GetHeaderHash()
201+
rootHash := result.GetRootHash()
202+
headerNonce := result.GetHeaderNonce()
203+
204+
bbc.setFinalBlockInfoUnprotected(headerNonce, headerHash, rootHash)
205+
bbc.setLastExecutedBlockHeaderAndRootHashUnprotected(header, headerHash, rootHash)
170206
bbc.lastExecutionResult = result
171207
}
172208

dataRetriever/blockchain/baseBlockchain_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestBaseBlockchain_SetAndGetLastExecutedBlockHeaderAndRootHash(t *testing.T
109109
})
110110
}
111111

112-
func TestBaseBlockchain_SetAndGetLastExecutionResult(t *testing.T) {
112+
func TestBaseBlockchain_SetAndGetLastExecutionInfo(t *testing.T) {
113113
t.Parallel()
114114

115115
base := &baseBlockChain{
@@ -122,6 +122,10 @@ func TestBaseBlockchain_SetAndGetLastExecutionResult(t *testing.T) {
122122
hash := []byte("hash")
123123
rootHash := []byte("root-hash")
124124

125+
header1 := &block.HeaderV3{
126+
Nonce: nonce,
127+
}
128+
125129
execResult := &block.ExecutionResult{
126130
BaseExecutionResult: &block.BaseExecutionResult{
127131
RootHash: rootHash,
@@ -130,11 +134,13 @@ func TestBaseBlockchain_SetAndGetLastExecutionResult(t *testing.T) {
130134
},
131135
}
132136

133-
base.SetLastExecutionResult(execResult)
137+
base.SetLastExecutionInfo(header1, execResult)
134138

135139
retExecResult := base.GetLastExecutionResult()
136-
137140
require.Equal(t, execResult, retExecResult)
141+
142+
retLastExecHeader := base.GetLastExecutedBlockHeader()
143+
require.Equal(t, header1, retLastExecHeader)
138144
}
139145

140146
func TestBaseBlockchain_Concurrency(t *testing.T) {
@@ -181,7 +187,7 @@ func TestBaseBlockchain_Concurrency(t *testing.T) {
181187
case 9:
182188
bc.SetGenesisHeaderHash(headerHash)
183189
case 10:
184-
bc.SetLastExecutionResult(execResult)
190+
bc.SetLastExecutionInfo(header, execResult)
185191
case 11:
186192
_ = bc.GetLastExecutionResult()
187193
default:

dataRetriever/blockchain/blockchain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ func (bc *blockChain) setCurrentBlockHeaderUnprotected(header data.HeaderHandler
7777
return nil
7878
}
7979

80+
// SetCurrentBlockHeaderAndHash sets header and hash atomically
81+
func (bc *blockChain) SetCurrentBlockHeaderAndHash(
82+
headerHash []byte,
83+
header data.HeaderHandler,
84+
) error {
85+
bc.mut.Lock()
86+
defer bc.mut.Unlock()
87+
88+
bc.currentBlockHeaderHash = headerHash
89+
return bc.setCurrentBlockHeaderUnprotected(header)
90+
}
91+
8092
// SetCurrentBlockHeaderAndRootHash sets current block header pointer and the root hash
8193
func (bc *blockChain) SetCurrentBlockHeaderAndRootHash(header data.HeaderHandler, rootHash []byte) error {
8294
bc.mut.Lock()

dataRetriever/blockchain/metachain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ func (mc *metaChain) SetCurrentBlockHeader(header data.HeaderHandler) error {
5858
return mc.setCurrentBlockHeaderUnprotected(header)
5959
}
6060

61+
// SetCurrentBlockHeaderAndHash sets header and hash atomically
62+
func (mc *metaChain) SetCurrentBlockHeaderAndHash(
63+
headerHash []byte,
64+
header data.HeaderHandler,
65+
) error {
66+
mc.mut.Lock()
67+
defer mc.mut.Unlock()
68+
69+
mc.currentBlockHeaderHash = headerHash
70+
return mc.setCurrentBlockHeaderUnprotected(header)
71+
}
72+
6173
func (mc *metaChain) setCurrentBlockHeaderUnprotected(header data.HeaderHandler) error {
6274
if check.IfNil(header) {
6375
mc.currentBlockHeader = nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/libp2p/go-libp2p-pubsub v0.13.0
2020
github.com/mitchellh/mapstructure v1.5.0
2121
github.com/multiversx/mx-chain-communication-go v1.3.3-0.20260608072730-982186a1ad78
22-
github.com/multiversx/mx-chain-core-go v1.5.1-0.20260608073155-f1f550c8a612
22+
github.com/multiversx/mx-chain-core-go v1.5.1-0.20260618090441-fe4dfcdcb341
2323
github.com/multiversx/mx-chain-crypto-go v1.3.1
2424
github.com/multiversx/mx-chain-es-indexer-go v1.10.3-0.20260608081825-40e586306036
2525
github.com/multiversx/mx-chain-logger-go v1.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ github.com/multiversx/concurrent-map v0.1.4 h1:hdnbM8VE4b0KYJaGY5yJS2aNIW9TFFsUY
401401
github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7eFQ4U2vKMOp4A/9+o=
402402
github.com/multiversx/mx-chain-communication-go v1.3.3-0.20260608072730-982186a1ad78 h1:vdYSj8Jj83H5wMkQOcDKvBXf2yVLXgVmA1IthHuv3aY=
403403
github.com/multiversx/mx-chain-communication-go v1.3.3-0.20260608072730-982186a1ad78/go.mod h1:gDVWn6zUW6aCN1YOm/FbbT5MUmhgn/L1Rmpl8EoH3Yg=
404-
github.com/multiversx/mx-chain-core-go v1.5.1-0.20260608073155-f1f550c8a612 h1:Hol8/gBD3d84kIuVsss+1Zx+sUIOpshU/wHC1d2DFVI=
405-
github.com/multiversx/mx-chain-core-go v1.5.1-0.20260608073155-f1f550c8a612/go.mod h1:IO+vspNan+gT0WOHnJ95uvWygiziHZvfXpff6KnxV7g=
404+
github.com/multiversx/mx-chain-core-go v1.5.1-0.20260618090441-fe4dfcdcb341 h1:Q01qsxt5WFyI2xN/rYwBe3It76CP6G5h+MH37wS+q9g=
405+
github.com/multiversx/mx-chain-core-go v1.5.1-0.20260618090441-fe4dfcdcb341/go.mod h1:IO+vspNan+gT0WOHnJ95uvWygiziHZvfXpff6KnxV7g=
406406
github.com/multiversx/mx-chain-crypto-go v1.3.1 h1:tCoGkfiv0wz97kuW6AZPW4RVL0Yp7PBo8NKQj9f2oh4=
407407
github.com/multiversx/mx-chain-crypto-go v1.3.1/go.mod h1:nPIkxxzyTP8IquWKds+22Q2OJ9W7LtusC7cAosz7ojM=
408408
github.com/multiversx/mx-chain-es-indexer-go v1.10.3-0.20260608081825-40e586306036 h1:a0euQ0LrFvP3y7Uf4CJ39tXBo0tsTEzPLrvJriuO2oA=

node/chainSimulator/components/testOnlyProcessingNode.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,14 @@ func (node *testOnlyProcessingNode) setBlockchainRootHashIfSupernovaIsActive(
563563
metaResult, isMeta := lastExecutionResult.(*block.MetaExecutionResult)
564564
if isMeta {
565565
metaResult.ExecutionResult.BaseExecutionResult.RootHash = rootHash
566-
node.ChainHandler.SetLastExecutionResult(metaResult)
566+
node.ChainHandler.SetLastExecutionInfo(header, metaResult)
567567
return
568568
}
569569

570570
shardResult, isShard := lastExecutionResult.(*block.ExecutionResult)
571571
if isShard {
572572
shardResult.BaseExecutionResult.RootHash = rootHash
573-
node.ChainHandler.SetLastExecutionResult(shardResult)
573+
node.ChainHandler.SetLastExecutionInfo(header, shardResult)
574574
return
575575
}
576576

@@ -582,7 +582,7 @@ func (node *testOnlyProcessingNode) setBlockchainRootHashIfSupernovaIsActive(
582582
RootHash: rootHash,
583583
GasUsed: lastExecutionResult.GetGasUsed(),
584584
}
585-
node.ChainHandler.SetLastExecutionResult(updatedLastExecutionResult)
585+
node.ChainHandler.SetLastExecutionInfo(header, updatedLastExecutionResult)
586586
}
587587

588588
// RemoveAccount will remove the account for the given address

node/chainSimulator/components/testOnlyProcessingNode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func TestTestOnlyProcessingNode_SetBlockchainRootHashWithMetaExecutionResult(t *
539539
GetLastExecutionResultCalled: func() data.BaseExecutionResultHandler {
540540
return metaResult
541541
},
542-
SetLastExecutionResultCalled: func(result data.BaseExecutionResultHandler) {
542+
SetLastExecutionInfoCalled: func(header data.HeaderHandler, result data.BaseExecutionResultHandler) {
543543
capturedResult = result
544544
},
545545
}
@@ -590,7 +590,7 @@ func TestTestOnlyProcessingNode_SetBlockchainRootHashWithShardExecutionResult(t
590590
GetLastExecutionResultCalled: func() data.BaseExecutionResultHandler {
591591
return shardResult
592592
},
593-
SetLastExecutionResultCalled: func(result data.BaseExecutionResultHandler) {
593+
SetLastExecutionInfoCalled: func(header data.HeaderHandler, result data.BaseExecutionResultHandler) {
594594
capturedResult = result
595595
},
596596
}

process/asyncExecution/executionManager/executionManager.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ func (em *executionManager) ResetAndResumeExecution(lastNotarizedResult data.Bas
258258
func (em *executionManager) updateBlockchainAfterRemoval(lastNotarizedResult data.BaseExecutionResultHandler) error {
259259
lastExecutedHeaderHash := lastNotarizedResult.GetHeaderHash()
260260
lastExecutedHeaderNonce := lastNotarizedResult.GetHeaderNonce()
261-
lastExecutedHeaderRootHash := lastNotarizedResult.GetRootHash()
262261
pendingExecutionResults, err := em.executionResultsTracker.GetPendingExecutionResults()
263262
if err != nil {
264263
return err
@@ -270,7 +269,6 @@ func (em *executionManager) updateBlockchainAfterRemoval(lastNotarizedResult dat
270269
lastPending := pendingExecutionResults[len(pendingExecutionResults)-1]
271270
lastExecutedHeaderHash = lastPending.GetHeaderHash()
272271
lastExecutedHeaderNonce = lastPending.GetHeaderNonce()
273-
lastExecutedHeaderRootHash = lastPending.GetRootHash()
274272

275273
lastExecutionResult = lastPending
276274
}
@@ -286,14 +284,7 @@ func (em *executionManager) updateBlockchainAfterRemoval(lastNotarizedResult dat
286284
}
287285

288286
// update blockchain
289-
em.blockChain.SetFinalBlockInfo(
290-
lastExecutedHeaderNonce,
291-
lastExecutedHeaderHash,
292-
lastExecutedHeaderRootHash,
293-
)
294-
295-
em.blockChain.SetLastExecutedBlockHeaderAndRootHash(header, lastExecutedHeaderHash, lastExecutedHeaderRootHash)
296-
em.blockChain.SetLastExecutionResult(lastExecutionResult)
287+
em.blockChain.SetLastExecutionInfo(header, lastExecutionResult)
297288

298289
return nil
299290
}

process/asyncExecution/executionManager/executionManager_test.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
329329
counter += 1
330330
}
331331
},
332-
SetLastExecutionResultCalled: func(result data.BaseExecutionResultHandler) {
332+
SetLastExecutionInfoCalled: func(header data.HeaderHandler, result data.BaseExecutionResultHandler) {
333333
if bytes.Equal(result.GetHeaderHash(), []byte("lastNotarizedExecResultHash")) {
334334
counter += 1
335335
}
@@ -389,12 +389,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
389389
Nonce: 10,
390390
}
391391
},
392-
SetLastExecutedBlockHeaderAndRootHashCalled: func(header data.HeaderHandler, blockHash []byte, rootHash []byte) {
393-
if bytes.Equal(blockHash, []byte("lastNotarizedExecResultHash")) {
394-
counter += 1
395-
}
396-
},
397-
SetLastExecutionResultCalled: func(result data.BaseExecutionResultHandler) {
392+
SetLastExecutionInfoCalled: func(header data.HeaderHandler, result data.BaseExecutionResultHandler) {
398393
if bytes.Equal(result.GetHeaderHash(), []byte("lastNotarizedExecResultHash")) {
399394
counter += 1
400395
}
@@ -436,7 +431,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
436431

437432
err := em.AddPairForExecution(pair)
438433
require.Equal(t, errExpected, err)
439-
require.Equal(t, 2, counter)
434+
require.Equal(t, 1, counter)
440435
})
441436

442437
t.Run("should work when the execution results of the previous header are notarized", func(t *testing.T) {
@@ -450,12 +445,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
450445
Nonce: 10,
451446
}
452447
},
453-
SetLastExecutedBlockHeaderAndRootHashCalled: func(header data.HeaderHandler, blockHash []byte, rootHash []byte) {
454-
if bytes.Equal(blockHash, []byte("lastNotarizedExecResultHash")) {
455-
counter += 1
456-
}
457-
},
458-
SetLastExecutionResultCalled: func(result data.BaseExecutionResultHandler) {
448+
SetLastExecutionInfoCalled: func(header data.HeaderHandler, result data.BaseExecutionResultHandler) {
459449
if bytes.Equal(result.GetHeaderHash(), []byte("lastNotarizedExecResultHash")) {
460450
counter += 1
461451
}
@@ -498,7 +488,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
498488

499489
err := em.AddPairForExecution(pair)
500490
require.Nil(t, err)
501-
require.Equal(t, 3, counter)
491+
require.Equal(t, 2, counter)
502492
})
503493

504494
t.Run("should work if there are pending execution results of previous header", func(t *testing.T) {
@@ -512,12 +502,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
512502
Nonce: 10,
513503
}
514504
},
515-
SetLastExecutedBlockHeaderAndRootHashCalled: func(header data.HeaderHandler, blockHash []byte, rootHash []byte) {
516-
if bytes.Equal(blockHash, []byte("hashY")) {
517-
counter += 1
518-
}
519-
},
520-
SetLastExecutionResultCalled: func(result data.BaseExecutionResultHandler) {
505+
SetLastExecutionInfoCalled: func(header data.HeaderHandler, result data.BaseExecutionResultHandler) {
521506
if bytes.Equal(result.GetHeaderHash(), []byte("hashY")) {
522507
counter += 1
523508
}
@@ -572,7 +557,7 @@ func TestExecutionManager_AddPairForExecution(t *testing.T) {
572557

573558
err := em.AddPairForExecution(pair)
574559
require.Nil(t, err)
575-
require.Equal(t, 3, counter)
560+
require.Equal(t, 2, counter)
576561
})
577562
}
578563

0 commit comments

Comments
 (0)