Skip to content

Commit

Permalink
Merge pull request #270 from multiversx/add-in-block-unsigned-intra-s…
Browse files Browse the repository at this point in the history
…hard-mb

Intra miniblocks in block structure
  • Loading branch information
miiu96 authored Apr 8, 2024
2 parents 56519cb + 9522d68 commit e6fbcb3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
53 changes: 45 additions & 8 deletions process/elasticproc/block/blockProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (bp *blockProcessor) PrepareBlockForDB(obh *outport.OutportBlockWithHeader)
}

sizeTxs := computeSizeOfTransactions(obh.TransactionPool)
miniblocksHashes := bp.getEncodedMBSHashes(obh.BlockData.Body)
miniblocksHashes := bp.getEncodedMBSHashes(obh.BlockData.Body, obh.BlockData.IntraShardMiniBlocks)
leaderIndex := bp.getLeaderIndex(obh.SignersIndexes)

numTxs, notarizedTxs := getTxsCount(obh.Header)
Expand Down Expand Up @@ -126,7 +126,9 @@ func (bp *blockProcessor) PrepareBlockForDB(obh *outport.OutportBlockWithHeader)
}

bp.addEpochStartInfoForMeta(obh.Header, elasticBlock)
putMiniblocksDetailsInBlock(obh.Header, elasticBlock, obh.TransactionPool, obh.BlockData.Body)

appendBlockDetailsFromHeaders(elasticBlock, obh.Header, obh.BlockData.Body, obh.TransactionPool)
appendBlockDetailsFromIntraShardMbs(elasticBlock, obh.BlockData.IntraShardMiniBlocks, obh.TransactionPool, len(obh.Header.GetMiniBlockHeaderHandlers()))

return elasticBlock, nil
}
Expand Down Expand Up @@ -227,9 +229,10 @@ func (bp *blockProcessor) addEpochStartShardDataForMeta(epochStartShardData node
block.EpochStartShardsData = append(block.EpochStartShardsData, shardData)
}

func (bp *blockProcessor) getEncodedMBSHashes(body *block.Body) []string {
func (bp *blockProcessor) getEncodedMBSHashes(body *block.Body, intraShardMbs []*nodeBlock.MiniBlock) []string {
miniblocksHashes := make([]string, 0)
for _, miniblock := range body.MiniBlocks {
mbs := append(body.MiniBlocks, intraShardMbs...)
for _, miniblock := range mbs {
mbHash, errComputeHash := core.CalculateHash(bp.marshalizer, bp.hasher, miniblock)
if errComputeHash != nil {
log.Warn("internal error computing hash", "error", errComputeHash)
Expand All @@ -244,10 +247,8 @@ func (bp *blockProcessor) getEncodedMBSHashes(body *block.Body) []string {
return miniblocksHashes
}

func putMiniblocksDetailsInBlock(header coreData.HeaderHandler, block *data.Block, pool *outport.TransactionPool, body *block.Body) {
mbHeaders := header.GetMiniBlockHeaderHandlers()

for idx, mbHeader := range mbHeaders {
func appendBlockDetailsFromHeaders(block *data.Block, header coreData.HeaderHandler, body *block.Body, pool *outport.TransactionPool) {
for idx, mbHeader := range header.GetMiniBlockHeaderHandlers() {
mbType := nodeBlock.Type(mbHeader.GetTypeInt32())
if mbType == nodeBlock.PeerBlock {
continue
Expand All @@ -268,6 +269,42 @@ func putMiniblocksDetailsInBlock(header coreData.HeaderHandler, block *data.Bloc
}
}

func appendBlockDetailsFromIntraShardMbs(block *data.Block, intraShardMbs []*block.MiniBlock, pool *outport.TransactionPool, offset int) {
for idx, intraMB := range intraShardMbs {
if intraMB.Type == nodeBlock.PeerBlock {
continue
}

block.MiniBlocksDetails = append(block.MiniBlocksDetails, &data.MiniBlocksDetails{
IndexFirstProcessedTx: 0,
IndexLastProcessedTx: int32(len(intraMB.GetTxHashes()) - 1),
SenderShardID: intraMB.GetSenderShardID(),
ReceiverShardID: intraMB.GetReceiverShardID(),
MBIndex: idx + offset,
Type: intraMB.Type.String(),
ProcessingType: nodeBlock.Normal.String(),
TxsHashes: hexEncodeSlice(intraMB.TxHashes),
ExecutionOrderTxsIndices: extractExecutionOrderIntraShardMBUnsigned(intraMB, pool),
})
}
}

func extractExecutionOrderIntraShardMBUnsigned(mb *block.MiniBlock, pool *outport.TransactionPool) []int {
executionOrderTxsIndices := make([]int, len(mb.TxHashes))
for idx, txHash := range mb.TxHashes {
executionOrder, found := getExecutionOrderForTx(txHash, int32(mb.Type), pool)
if !found {
log.Warn("blockProcessor.extractExecutionOrderIntraShardMBUnsigned cannot find tx in pool", "txHash", hex.EncodeToString(txHash))
executionOrderTxsIndices[idx] = notFound
continue
}

executionOrderTxsIndices[idx] = int(executionOrder)
}

return executionOrderTxsIndices
}

func extractExecutionOrderIndicesFromPool(mbHeader coreData.MiniBlockHeaderHandler, txsHashes [][]byte, pool *outport.TransactionPool) []int {
mbType := mbHeader.GetTypeInt32()
executionOrderTxsIndices := make([]int, len(txsHashes))
Expand Down
22 changes: 20 additions & 2 deletions process/elasticproc/block/blockProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func TestBlockProcessor_PrepareBlockForDBMiniBlocksDetails(t *testing.T) {
}
mbhrBytes, _ := gogoMarshaller.Marshal(mbhr)

txHash, notExecutedTxHash, notFoundTxHash, invalidTxHash, rewardsTxHash, scrHash := "tx", "notExecuted", "notFound", "invalid", "reward", "scr"
txHash, notExecutedTxHash, notFoundTxHash, invalidTxHash, rewardsTxHash, scrHash, intraSCR := "tx", "notExecuted", "notFound", "invalid", "reward", "scr", "intraSCR"

header := &dataBlock.Header{
TxCount: 5,
Expand Down Expand Up @@ -397,6 +397,12 @@ func TestBlockProcessor_PrepareBlockForDBMiniBlocksDetails(t *testing.T) {
Header: header,
OutportBlock: &outport.OutportBlock{
BlockData: &outport.BlockData{
IntraShardMiniBlocks: []*dataBlock.MiniBlock{
{
Type: dataBlock.SmartContractResultBlock,
TxHashes: [][]byte{[]byte(intraSCR)},
},
},
HeaderBytes: headerBytes,
HeaderHash: []byte("hash"),
Body: &dataBlock.Body{
Expand Down Expand Up @@ -446,6 +452,10 @@ func TestBlockProcessor_PrepareBlockForDBMiniBlocksDetails(t *testing.T) {
SmartContractResult: &smartContractResult.SmartContractResult{},
ExecutionOrder: 0,
},
hex.EncodeToString([]byte(intraSCR)): {
SmartContractResult: &smartContractResult.SmartContractResult{},
ExecutionOrder: 4,
},
},
},
HeaderGasConsumption: &outport.HeaderGasConsumption{},
Expand All @@ -458,7 +468,7 @@ func TestBlockProcessor_PrepareBlockForDBMiniBlocksDetails(t *testing.T) {
require.Equal(t, &data.Block{
Hash: "68617368",
Size: int64(723),
SizeTxs: 15,
SizeTxs: 21,
AccumulatedFees: "0",
DeveloperFees: "0",
TxCount: uint32(5),
Expand All @@ -468,6 +478,7 @@ func TestBlockProcessor_PrepareBlockForDBMiniBlocksDetails(t *testing.T) {
"1183f422a5b76c3cb7b439334f1fe7235c8d09f577e0f1e15e62cd05b9a81950",
"b24e307f3917e84603d3ebfb9c03c8fc651b62cb68ca884c3ff015b66a610a79",
"c0a855563172b2f72be569963d26d4fae38d4371342e2bf3ded93466a72f36f3",
"381b0f52b35781ddce70dc7ee08907a29f49ed9c46ea0b7b59e5833ba3213d10",
},
MiniBlocksDetails: []*data.MiniBlocksDetails{
{
Expand Down Expand Up @@ -502,6 +513,13 @@ func TestBlockProcessor_PrepareBlockForDBMiniBlocksDetails(t *testing.T) {
ProcessingType: dataBlock.Normal.String(),
ExecutionOrderTxsIndices: []int{0},
TxsHashes: []string{"736372"}},
{IndexFirstProcessedTx: 0,
IndexLastProcessedTx: 0,
MBIndex: 4,
Type: dataBlock.SmartContractResultBlock.String(),
ProcessingType: dataBlock.Normal.String(),
ExecutionOrderTxsIndices: []int{4},
TxsHashes: []string{"696e747261534352"}},
},
}, dbBlock)
}

0 comments on commit e6fbcb3

Please sign in to comment.