Skip to content

Commit d7cf5d5

Browse files
authored
Merge pull request #366 from multiversx/last-execution-nonce-and-hash-on-block
Last execution results nonce and hash on block
2 parents 3f37711 + 769957b commit d7cf5d5

File tree

3 files changed

+129
-41
lines changed

3 files changed

+129
-41
lines changed

data/block.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type Block struct {
5252
Reserved []byte `json:"reserved,omitempty"`
5353
ProposerBlsKey string `json:"proposerBlsKey,omitempty"`
5454
ExecutionResultBlockHashes []string `json:"executionResultBlockHashes,omitempty"`
55+
LastExecutionResultHash string `json:"lastExecutionResultHash,omitempty"`
56+
LastExecutionResultNonce uint64 `json:"lastExecutionResultNonce,omitempty"`
5557
}
5658

5759
// ExecutionResult is a structure containing all the fields that need

process/elasticproc/block/blockProcessor.go

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func (bp *blockProcessor) PrepareBlockForDB(obh *outport.OutportBlockWithHeader)
142142

143143
appendBlockDetailsFromIntraShardMbs(elasticBlock, obh.BlockData.IntraShardMiniBlocks, obh.TransactionPool, len(obh.Header.GetMiniBlockHeaderHandlers()))
144144

145+
addInBlockLastExecutionResultData(elasticBlock, obh)
145146
addProofs(elasticBlock, obh)
146147

147148
return &data.PreparedBlockResults{
@@ -150,6 +151,25 @@ func (bp *blockProcessor) PrepareBlockForDB(obh *outport.OutportBlockWithHeader)
150151
}, nil
151152
}
152153

154+
func addInBlockLastExecutionResultData(elasticBlock *data.Block, obh *outport.OutportBlockWithHeader) {
155+
lastExecutionResult := obh.Header.GetLastExecutionResultHandler()
156+
if check.IfNil(lastExecutionResult) {
157+
return
158+
}
159+
160+
switch t := lastExecutionResult.(type) {
161+
case *nodeBlock.ExecutionResultInfo:
162+
elasticBlock.LastExecutionResultHash = hex.EncodeToString(t.ExecutionResult.GetHeaderHash())
163+
elasticBlock.LastExecutionResultNonce = t.ExecutionResult.GetHeaderNonce()
164+
165+
case *nodeBlock.MetaExecutionResultInfo:
166+
elasticBlock.LastExecutionResultHash = hex.EncodeToString(t.ExecutionResult.GetHeaderHash())
167+
elasticBlock.LastExecutionResultNonce = t.ExecutionResult.GetHeaderNonce()
168+
default:
169+
return
170+
}
171+
}
172+
153173
func (bp *blockProcessor) prepareExecutionResults(obh *outport.OutportBlockWithHeader) []*data.ExecutionResult {
154174
if !obh.Header.IsHeaderV3() {
155175
return []*data.ExecutionResult{}
@@ -244,19 +264,19 @@ func getTxsCount(header coreData.HeaderHandler) (numTxs, notarizedTxs uint32) {
244264
return numTxs, notarizedTxs
245265
}
246266

247-
metaHeader, ok := header.(*nodeBlock.MetaBlock)
267+
metaHeader, ok := header.(coreData.MetaHeaderHandler)
248268
if !ok {
249269
return 0, 0
250270
}
251271

252-
notarizedTxs = metaHeader.TxCount
272+
notarizedTxs = metaHeader.GetTxCount()
253273
numTxs = 0
254-
for _, mb := range metaHeader.MiniBlockHeaders {
255-
if mb.Type == nodeBlock.PeerBlock {
274+
for _, mb := range metaHeader.GetMiniBlockHeaderHandlers() {
275+
if mb.GetTypeInt32() == int32(nodeBlock.PeerBlock) {
256276
continue
257277
}
258278

259-
numTxs += mb.TxCount
279+
numTxs += mb.GetTxCount()
260280
}
261281

262282
notarizedTxs = notarizedTxs - numTxs
@@ -269,7 +289,7 @@ func (bp *blockProcessor) addEpochStartInfoForMeta(header coreData.HeaderHandler
269289
return
270290
}
271291

272-
metaHeader, ok := header.(*nodeBlock.MetaBlock)
292+
metaHeader, ok := header.(coreData.MetaHeaderHandler)
273293
if !ok {
274294
return
275295
}
@@ -278,55 +298,55 @@ func (bp *blockProcessor) addEpochStartInfoForMeta(header coreData.HeaderHandler
278298
return
279299
}
280300

281-
metaHeaderEconomics := metaHeader.EpochStart.Economics
301+
metaHeaderEconomics := metaHeader.GetEpochStartHandler().GetEconomicsHandler()
282302

283303
block.EpochStartInfo = &data.EpochStartInfo{
284-
TotalSupply: metaHeaderEconomics.TotalSupply.String(),
285-
TotalToDistribute: metaHeaderEconomics.TotalToDistribute.String(),
286-
TotalNewlyMinted: metaHeaderEconomics.TotalNewlyMinted.String(),
287-
RewardsPerBlock: metaHeaderEconomics.RewardsPerBlock.String(),
288-
RewardsForProtocolSustainability: metaHeaderEconomics.RewardsForProtocolSustainability.String(),
289-
NodePrice: metaHeaderEconomics.NodePrice.String(),
290-
PrevEpochStartRound: metaHeaderEconomics.PrevEpochStartRound,
291-
PrevEpochStartHash: hex.EncodeToString(metaHeaderEconomics.PrevEpochStartHash),
292-
}
293-
if len(metaHeader.EpochStart.LastFinalizedHeaders) == 0 {
304+
TotalSupply: metaHeaderEconomics.GetTotalSupply().String(),
305+
TotalToDistribute: metaHeaderEconomics.GetTotalToDistribute().String(),
306+
TotalNewlyMinted: metaHeaderEconomics.GetTotalNewlyMinted().String(),
307+
RewardsPerBlock: metaHeaderEconomics.GetRewardsPerBlock().String(),
308+
RewardsForProtocolSustainability: metaHeaderEconomics.GetRewardsForProtocolSustainability().String(),
309+
NodePrice: metaHeaderEconomics.GetNodePrice().String(),
310+
PrevEpochStartRound: metaHeaderEconomics.GetPrevEpochStartRound(),
311+
PrevEpochStartHash: hex.EncodeToString(metaHeaderEconomics.GetPrevEpochStartHash()),
312+
}
313+
if len(metaHeader.GetEpochStartHandler().GetLastFinalizedHeaderHandlers()) == 0 {
294314
return
295315
}
296316

297-
epochStartShardsData := metaHeader.EpochStart.LastFinalizedHeaders
298-
block.EpochStartShardsData = make([]*data.EpochStartShardData, 0, len(metaHeader.EpochStart.LastFinalizedHeaders))
299-
for _, epochStartShardData := range epochStartShardsData {
300-
bp.addEpochStartShardDataForMeta(epochStartShardData, block)
317+
epochStartShardsData := metaHeader.GetEpochStartHandler().GetLastFinalizedHeaderHandlers()
318+
block.EpochStartShardsData = make([]*data.EpochStartShardData, 0, len(epochStartShardsData))
319+
for _, epochStartShardDataHandler := range epochStartShardsData {
320+
bp.addEpochStartShardDataForMeta(epochStartShardDataHandler, block)
301321
}
302322
}
303323

304-
func (bp *blockProcessor) addEpochStartShardDataForMeta(epochStartShardData nodeBlock.EpochStartShardData, block *data.Block) {
324+
func (bp *blockProcessor) addEpochStartShardDataForMeta(epochStartShardData coreData.EpochStartShardDataHandler, block *data.Block) {
305325
shardData := &data.EpochStartShardData{
306-
ShardID: epochStartShardData.ShardID,
307-
Epoch: epochStartShardData.Epoch,
308-
Round: epochStartShardData.Round,
309-
Nonce: epochStartShardData.Nonce,
310-
HeaderHash: hex.EncodeToString(epochStartShardData.HeaderHash),
311-
RootHash: hex.EncodeToString(epochStartShardData.RootHash),
312-
ScheduledRootHash: hex.EncodeToString(epochStartShardData.ScheduledRootHash),
313-
FirstPendingMetaBlock: hex.EncodeToString(epochStartShardData.FirstPendingMetaBlock),
314-
LastFinishedMetaBlock: hex.EncodeToString(epochStartShardData.LastFinishedMetaBlock),
315-
}
316-
317-
if len(epochStartShardData.PendingMiniBlockHeaders) == 0 {
326+
ShardID: epochStartShardData.GetShardID(),
327+
Epoch: epochStartShardData.GetEpoch(),
328+
Round: epochStartShardData.GetRound(),
329+
Nonce: epochStartShardData.GetNonce(),
330+
HeaderHash: hex.EncodeToString(epochStartShardData.GetHeaderHash()),
331+
RootHash: hex.EncodeToString(epochStartShardData.GetRootHash()),
332+
ScheduledRootHash: hex.EncodeToString(epochStartShardData.GetScheduledRootHash()),
333+
FirstPendingMetaBlock: hex.EncodeToString(epochStartShardData.GetFirstPendingMetaBlock()),
334+
LastFinishedMetaBlock: hex.EncodeToString(epochStartShardData.GetLastFinishedMetaBlock()),
335+
}
336+
337+
if len(epochStartShardData.GetPendingMiniBlockHeaderHandlers()) == 0 {
318338
block.EpochStartShardsData = append(block.EpochStartShardsData, shardData)
319339
return
320340
}
321341

322-
shardData.PendingMiniBlockHeaders = make([]*data.Miniblock, 0, len(epochStartShardData.PendingMiniBlockHeaders))
323-
for _, pendingMb := range epochStartShardData.PendingMiniBlockHeaders {
342+
shardData.PendingMiniBlockHeaders = make([]*data.Miniblock, 0, len(epochStartShardData.GetPendingMiniBlockHeaderHandlers()))
343+
for _, pendingMb := range epochStartShardData.GetPendingMiniBlockHeaderHandlers() {
324344
shardData.PendingMiniBlockHeaders = append(shardData.PendingMiniBlockHeaders, &data.Miniblock{
325-
Hash: hex.EncodeToString(pendingMb.Hash),
326-
SenderShardID: pendingMb.SenderShardID,
327-
ReceiverShardID: pendingMb.ReceiverShardID,
328-
Type: pendingMb.Type.String(),
329-
Reserved: pendingMb.Reserved,
345+
Hash: hex.EncodeToString(pendingMb.GetHash()),
346+
SenderShardID: pendingMb.GetSenderShardID(),
347+
ReceiverShardID: pendingMb.GetReceiverShardID(),
348+
Type: nodeBlock.Type(pendingMb.GetTypeInt32()).String(),
349+
Reserved: pendingMb.GetReserved(),
330350
})
331351
}
332352

process/elasticproc/block/blockProcessor_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,69 @@ func TestPrepareExecutionResult(t *testing.T) {
662662
},
663663
}, results.ExecutionResults[0])
664664
}
665+
666+
func TestAddInBlockLastExecutionResultData(t *testing.T) {
667+
t.Parallel()
668+
669+
t.Run("nil last execution result", func(t *testing.T) {
670+
elasticBlock := &data.Block{}
671+
obh := &outport.OutportBlockWithHeader{
672+
Header: &dataBlock.Header{},
673+
}
674+
675+
addInBlockLastExecutionResultData(elasticBlock, obh)
676+
677+
require.Equal(t, "", elasticBlock.LastExecutionResultHash)
678+
require.Equal(t, uint64(0), elasticBlock.LastExecutionResultNonce)
679+
})
680+
681+
t.Run("with ExecutionResultInfo", func(t *testing.T) {
682+
elasticBlock := &data.Block{}
683+
headerHash := []byte("headerHash")
684+
nonce := uint64(10)
685+
686+
execResultInfo := &dataBlock.ExecutionResultInfo{
687+
ExecutionResult: &dataBlock.BaseExecutionResult{
688+
HeaderHash: headerHash,
689+
HeaderNonce: nonce,
690+
},
691+
}
692+
693+
obh := &outport.OutportBlockWithHeader{
694+
Header: &dataBlock.HeaderV3{
695+
LastExecutionResult: execResultInfo,
696+
},
697+
}
698+
699+
addInBlockLastExecutionResultData(elasticBlock, obh)
700+
701+
require.Equal(t, hex.EncodeToString(headerHash), elasticBlock.LastExecutionResultHash)
702+
require.Equal(t, nonce, elasticBlock.LastExecutionResultNonce)
703+
})
704+
705+
t.Run("with MetaExecutionResultInfo", func(t *testing.T) {
706+
elasticBlock := &data.Block{}
707+
headerHash := []byte("headerHashMeta")
708+
nonce := uint64(20)
709+
710+
execResultInfo := &dataBlock.MetaExecutionResultInfo{
711+
ExecutionResult: &dataBlock.BaseMetaExecutionResult{
712+
BaseExecutionResult: &dataBlock.BaseExecutionResult{
713+
HeaderHash: headerHash,
714+
HeaderNonce: nonce,
715+
},
716+
},
717+
}
718+
719+
obh := &outport.OutportBlockWithHeader{
720+
Header: &dataBlock.MetaBlockV3{
721+
LastExecutionResult: execResultInfo,
722+
},
723+
}
724+
725+
addInBlockLastExecutionResultData(elasticBlock, obh)
726+
727+
require.Equal(t, hex.EncodeToString(headerHash), elasticBlock.LastExecutionResultHash)
728+
require.Equal(t, nonce, elasticBlock.LastExecutionResultNonce)
729+
})
730+
}

0 commit comments

Comments
 (0)