Skip to content

Commit

Permalink
Merge pull request #5613 from multiversx/MX-13777-add-round-in-hyperb…
Browse files Browse the repository at this point in the history
…lock-txs

MX-13777: added round in hyperblock transactions
  • Loading branch information
gabi-vuls authored Oct 6, 2023
2 parents b4df8fd + 816585e commit d0d5873
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
33 changes: 17 additions & 16 deletions node/external/blockAPI/baseBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (bap *baseAPIBlockProcessor) getIntrashardMiniblocksFromReceiptsStorage(hea

apiMiniblocks := make([]*api.MiniBlock, 0, len(receiptsHolder.GetMiniblocks()))
for _, miniblock := range receiptsHolder.GetMiniblocks() {
apiMiniblock, err := bap.convertMiniblockFromReceiptsStorageToApiMiniblock(miniblock, header.GetEpoch(), options)
apiMiniblock, err := bap.convertMiniblockFromReceiptsStorageToApiMiniblock(miniblock, header, options)
if err != nil {
return nil, err
}
Expand All @@ -82,7 +82,7 @@ func (bap *baseAPIBlockProcessor) getIntrashardMiniblocksFromReceiptsStorage(hea
return apiMiniblocks, nil
}

func (bap *baseAPIBlockProcessor) convertMiniblockFromReceiptsStorageToApiMiniblock(miniblock *block.MiniBlock, epoch uint32, options api.BlockQueryOptions) (*api.MiniBlock, error) {
func (bap *baseAPIBlockProcessor) convertMiniblockFromReceiptsStorageToApiMiniblock(miniblock *block.MiniBlock, header data.HeaderHandler, options api.BlockQueryOptions) (*api.MiniBlock, error) {
mbHash, err := core.CalculateHash(bap.marshalizer, bap.hasher, miniblock)
if err != nil {
return nil, err
Expand All @@ -102,7 +102,7 @@ func (bap *baseAPIBlockProcessor) convertMiniblockFromReceiptsStorageToApiMinibl
firstProcessed := int32(0)
lastProcessed := int32(len(miniblock.TxHashes) - 1)

err = bap.getAndAttachTxsToMbByEpoch(mbHash, miniblock, epoch, miniblockAPI, firstProcessed, lastProcessed, options)
err = bap.getAndAttachTxsToMbByEpoch(mbHash, miniblock, header, miniblockAPI, firstProcessed, lastProcessed, options)
if err != nil {
return nil, err
}
Expand All @@ -113,19 +113,19 @@ func (bap *baseAPIBlockProcessor) convertMiniblockFromReceiptsStorageToApiMinibl

func (bap *baseAPIBlockProcessor) getAndAttachTxsToMb(
mbHeader data.MiniBlockHeaderHandler,
epoch uint32,
header data.HeaderHandler,
apiMiniblock *api.MiniBlock,
options api.BlockQueryOptions,
) error {
miniblockHash := mbHeader.GetHash()
miniBlock, err := bap.getMiniblockByHashAndEpoch(miniblockHash, epoch)
miniBlock, err := bap.getMiniblockByHashAndEpoch(miniblockHash, header.GetEpoch())
if err != nil {
return err
}

firstProcessed := mbHeader.GetIndexOfFirstTxProcessed()
lastProcessed := mbHeader.GetIndexOfLastTxProcessed()
return bap.getAndAttachTxsToMbByEpoch(miniblockHash, miniBlock, epoch, apiMiniblock, firstProcessed, lastProcessed, options)
return bap.getAndAttachTxsToMbByEpoch(miniblockHash, miniBlock, header, apiMiniblock, firstProcessed, lastProcessed, options)
}

func (bap *baseAPIBlockProcessor) getMiniblockByHashAndEpoch(miniblockHash []byte, epoch uint32) (*block.MiniBlock, error) {
Expand All @@ -146,7 +146,7 @@ func (bap *baseAPIBlockProcessor) getMiniblockByHashAndEpoch(miniblockHash []byt
func (bap *baseAPIBlockProcessor) getAndAttachTxsToMbByEpoch(
miniblockHash []byte,
miniBlock *block.MiniBlock,
epoch uint32,
header data.HeaderHandler,
apiMiniblock *api.MiniBlock,
firstProcessedTxIndex int32,
lastProcessedTxIndex int32,
Expand All @@ -156,23 +156,23 @@ func (bap *baseAPIBlockProcessor) getAndAttachTxsToMbByEpoch(

switch miniBlock.Type {
case block.TxBlock:
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, epoch, transaction.TxTypeNormal, dataRetriever.TransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, header, transaction.TxTypeNormal, dataRetriever.TransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
case block.RewardsBlock:
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, epoch, transaction.TxTypeReward, dataRetriever.RewardTransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, header, transaction.TxTypeReward, dataRetriever.RewardTransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
case block.SmartContractResultBlock:
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, epoch, transaction.TxTypeUnsigned, dataRetriever.UnsignedTransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, header, transaction.TxTypeUnsigned, dataRetriever.UnsignedTransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
case block.InvalidBlock:
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, epoch, transaction.TxTypeInvalid, dataRetriever.TransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
apiMiniblock.Transactions, err = bap.getTxsFromMiniblock(miniBlock, miniblockHash, header, transaction.TxTypeInvalid, dataRetriever.TransactionUnit, firstProcessedTxIndex, lastProcessedTxIndex)
case block.ReceiptBlock:
apiMiniblock.Receipts, err = bap.getReceiptsFromMiniblock(miniBlock, epoch)
apiMiniblock.Receipts, err = bap.getReceiptsFromMiniblock(miniBlock, header.GetEpoch())
}

if err != nil {
return err
}

if options.WithLogs {
err = bap.logsFacade.IncludeLogsInTransactions(apiMiniblock.Transactions, miniBlock.TxHashes, epoch)
err = bap.logsFacade.IncludeLogsInTransactions(apiMiniblock.Transactions, miniBlock.TxHashes, header.GetEpoch())
if err != nil {
return err
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (bap *baseAPIBlockProcessor) getReceiptsFromMiniblock(miniblock *block.Mini
func (bap *baseAPIBlockProcessor) getTxsFromMiniblock(
miniblock *block.MiniBlock,
miniblockHash []byte,
epoch uint32,
header data.HeaderHandler,
txType transaction.TxType,
unit dataRetriever.UnitType,
firstProcessedTxIndex int32,
Expand All @@ -224,7 +224,7 @@ func (bap *baseAPIBlockProcessor) getTxsFromMiniblock(
start := time.Now()

executedTxHashes := extractExecutedTxHashes(miniblock.TxHashes, firstProcessedTxIndex, lastProcessedTxIndex)
marshalledTxs, err := storer.GetBulkFromEpoch(executedTxHashes, epoch)
marshalledTxs, err := storer.GetBulkFromEpoch(executedTxHashes, header.GetEpoch())
if err != nil {
return nil, fmt.Errorf("%w: %v, miniblock = %s", errCannotLoadTransactions, err, hex.EncodeToString(miniblockHash))
}
Expand All @@ -243,7 +243,8 @@ func (bap *baseAPIBlockProcessor) getTxsFromMiniblock(
tx.MiniBlockHash = hex.EncodeToString(miniblockHash)
tx.SourceShard = miniblock.SenderShardID
tx.DestinationShard = miniblock.ReceiverShardID
tx.Epoch = epoch
tx.Epoch = header.GetEpoch()
tx.Round = header.GetRound()
bap.apiTransactionHandler.PopulateComputedFields(tx)

// TODO : should check if tx is reward reverted
Expand Down
23 changes: 18 additions & 5 deletions node/external/blockAPI/baseBlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,12 @@ func TestBaseBlock_getAndAttachTxsToMb_MiniblockTxBlock(t *testing.T) {
Reserved: mbhrBytes,
}

testHeader := &block.Header{
Epoch: 0,
Round: 37,
}
apiMB := &api.MiniBlock{}
err := baseAPIBlockProc.getAndAttachTxsToMb(mbHeader, 0, apiMB, api.BlockQueryOptions{})
err := baseAPIBlockProc.getAndAttachTxsToMb(mbHeader, testHeader, apiMB, api.BlockQueryOptions{})
require.Nil(t, err)
require.Equal(t, &api.MiniBlock{
Transactions: []*transaction.ApiTransactionResult{
Expand All @@ -244,6 +248,8 @@ func TestBaseBlock_getAndAttachTxsToMb_MiniblockTxBlock(t *testing.T) {
Data: []byte("refund"),
MiniBlockType: "TxBlock",
MiniBlockHash: "6d6248617368",
Epoch: testHeader.GetEpoch(),
Round: testHeader.GetRound(),
},
},
}, apiMB)
Expand All @@ -252,11 +258,14 @@ func TestBaseBlock_getAndAttachTxsToMb_MiniblockTxBlock(t *testing.T) {
func TestBaseBlock_getAndAttachTxsToMbShouldIncludeLogsAsSpecified(t *testing.T) {
t.Parallel()

testEpoch := uint32(7)
testHeader := &block.Header{
Epoch: 7,
Round: 140,
}

marshalizer := &marshal.GogoProtoMarshalizer{}

storageService := genericMocks.NewChainStorerMock(testEpoch)
storageService := genericMocks.NewChainStorerMock(testHeader.GetEpoch())
processor := createBaseBlockProcessor()
processor.marshalizer = marshalizer
processor.store = storageService
Expand Down Expand Up @@ -308,7 +317,7 @@ func TestBaseBlock_getAndAttachTxsToMbShouldIncludeLogsAsSpecified(t *testing.T)
!bytes.Equal(logsKeys[2], []byte{0xcc}) {
return nil
}
if epoch != testEpoch {
if epoch != testHeader.GetEpoch() {
return nil
}

Expand All @@ -331,7 +340,7 @@ func TestBaseBlock_getAndAttachTxsToMbShouldIncludeLogsAsSpecified(t *testing.T)
// Now let's test the loading of transaction and logs
miniblockHeader := &block.MiniBlockHeader{Hash: miniblockHash}
miniblockOnApi := &api.MiniBlock{}
err := processor.getAndAttachTxsToMb(miniblockHeader, testEpoch, miniblockOnApi, api.BlockQueryOptions{WithLogs: true})
err := processor.getAndAttachTxsToMb(miniblockHeader, testHeader, miniblockOnApi, api.BlockQueryOptions{WithLogs: true})

require.Nil(t, err)
require.Len(t, miniblockOnApi.Transactions, 3)
Expand All @@ -341,6 +350,10 @@ func TestBaseBlock_getAndAttachTxsToMbShouldIncludeLogsAsSpecified(t *testing.T)
require.Equal(t, "first", miniblockOnApi.Transactions[0].Logs.Events[0].Identifier)
require.Nil(t, miniblockOnApi.Transactions[1].Logs)
require.Equal(t, "third", miniblockOnApi.Transactions[2].Logs.Events[0].Identifier)
for _, tx := range miniblockOnApi.Transactions {
require.Equal(t, testHeader.GetRound(), tx.Round)
require.Equal(t, testHeader.GetEpoch(), tx.Epoch)
}
}

func TestExtractExecutedTxHashes(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions node/external/blockAPI/metaBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ func (mbp *metaAPIBlockProcessor) convertMetaBlockBytesToAPIBlock(hash []byte, b
return nil, err
}

headerEpoch := blockHeader.Epoch

numOfTxs := uint32(0)
miniblocks := make([]*api.MiniBlock, 0)
for _, mb := range blockHeader.MiniBlockHeaders {
Expand All @@ -181,7 +179,7 @@ func (mbp *metaAPIBlockProcessor) convertMetaBlockBytesToAPIBlock(hash []byte, b
}
if options.WithTransactions {
miniBlockCopy := mb
err = mbp.getAndAttachTxsToMb(&miniBlockCopy, headerEpoch, miniblockAPI, options)
err = mbp.getAndAttachTxsToMb(&miniBlockCopy, blockHeader, miniblockAPI, options)
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions node/external/blockAPI/shardBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ func (sbp *shardAPIBlockProcessor) convertShardBlockBytesToAPIBlock(hash []byte,
return nil, err
}

headerEpoch := blockHeader.GetEpoch()

numOfTxs := uint32(0)
miniblocks := make([]*api.MiniBlock, 0)

Expand All @@ -187,7 +185,7 @@ func (sbp *shardAPIBlockProcessor) convertShardBlockBytesToAPIBlock(hash []byte,
}
if options.WithTransactions {
miniBlockCopy := mb
err = sbp.getAndAttachTxsToMb(miniBlockCopy, headerEpoch, miniblockAPI, options)
err = sbp.getAndAttachTxsToMb(miniBlockCopy, blockHeader, miniblockAPI, options)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d0d5873

Please sign in to comment.