Skip to content

Commit

Permalink
remove iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
AdoAdoAdo committed Apr 26, 2024
1 parent 0442db9 commit 4794d97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
29 changes: 18 additions & 11 deletions process/block/postprocess/basePostProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const defaultCapacity = 10000

var log = logger.GetOrCreate("process/block/postprocess")

type processedResult struct {
type aggProcessedResults struct {
lastInitializedKey []byte
initializedKeys [][]byte
mapProcessedResult map[string][][]byte
keys []string
}

type basePostProcessor struct {
Expand All @@ -48,7 +49,7 @@ type basePostProcessor struct {

mutInterResultsForBlock sync.Mutex
interResultsForBlock map[string]*txInfo
processedResult processedResult
processedResult aggProcessedResults
intraShardMiniBlock *block.MiniBlock
economicsFee process.FeeHandler
index uint32
Expand Down Expand Up @@ -87,12 +88,17 @@ func (bpp *basePostProcessor) CreateBlockStarted() {
bpp.mutInterResultsForBlock.Lock()
bpp.interResultsForBlock = make(map[string]*txInfo)
bpp.intraShardMiniBlock = nil
bpp.processedResult.mapProcessedResult = make(map[string][][]byte)
bpp.processedResult.keys = make([]string, 0)
bpp.initProcessedResults()
bpp.index = 0
bpp.mutInterResultsForBlock.Unlock()
}

func (bpp *basePostProcessor) initProcessedResults() {
bpp.processedResult.mapProcessedResult = make(map[string][][]byte)
bpp.processedResult.initializedKeys = make([][]byte, 0, defaultCapacity)
bpp.processedResult.lastInitializedKey = nil
}

// CreateMarshalledData creates the marshalled data for broadcasting purposes
func (bpp *basePostProcessor) CreateMarshalledData(txHashes [][]byte) ([][]byte, error) {
bpp.mutInterResultsForBlock.Lock()
Expand Down Expand Up @@ -185,6 +191,8 @@ func (bpp *basePostProcessor) RemoveProcessedResults(key []byte) [][]byte {
return nil
}

delete(bpp.processedResult.mapProcessedResult, string(key))

for _, txHash := range txHashes {
delete(bpp.interResultsForBlock, string(txHash))
}
Expand All @@ -198,7 +206,8 @@ func (bpp *basePostProcessor) InitProcessedResults(key []byte) {
defer bpp.mutInterResultsForBlock.Unlock()

bpp.processedResult.mapProcessedResult[string(key)] = make([][]byte, 0, defaultCapacity)
bpp.processedResult.keys = append(bpp.processedResult.keys, string(key))
bpp.processedResult.initializedKeys = append(bpp.processedResult.initializedKeys, key)
bpp.processedResult.lastInitializedKey = key
}

func (bpp *basePostProcessor) splitMiniBlocksIfNeeded(miniBlocks []*block.MiniBlock) []*block.MiniBlock {
Expand Down Expand Up @@ -292,9 +301,7 @@ func (bpp *basePostProcessor) addIntermediateTxToResultsForBlock(
bpp.index++
bpp.interResultsForBlock[string(txHash)] = scrInfo

var mapValue [][]byte
for _, key := range bpp.processedResult.keys {
mapValue = bpp.processedResult.mapProcessedResult[key]
bpp.processedResult.mapProcessedResult[key] = append(mapValue, txHash)
}
key := string(bpp.processedResult.lastInitializedKey)
mapValue := bpp.processedResult.mapProcessedResult[key]
bpp.processedResult.mapProcessedResult[key] = append(mapValue, txHash)
}
5 changes: 3 additions & 2 deletions process/block/postprocess/intermediateResults.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ func NewIntermediateResultsProcessor(
shardCoordinator: args.Coordinator,
store: args.Store,
storageType: dataRetriever.UnsignedTransactionUnit,
processedResult: processedResult{
processedResult: aggProcessedResults{
lastInitializedKey: nil,
initializedKeys: nil,
mapProcessedResult: make(map[string][][]byte),
keys: make([]string, 0),
},
economicsFee: args.EconomicsFee,
}
Expand Down
10 changes: 5 additions & 5 deletions process/block/postprocess/intermediateResults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,19 @@ func TestIntermediateResultsProcessor_AddIntermediateTransactionsAddAndRevert(t
err = irp.AddIntermediateTransactions(txs)
assert.Nil(t, err)
irp.mutInterResultsForBlock.Lock()
assert.Equal(t, len(irp.processedResult.mapProcessedResult[string(key)]), len(txs))
assert.Equal(t, len(txs), calledCount)
assert.Equal(t, len(txs), len(irp.processedResult.mapProcessedResult[string(key)]))
assert.Equal(t, calledCount, len(txs))
irp.mutInterResultsForBlock.Unlock()

irp.RemoveProcessedResults(key)
irp.mutInterResultsForBlock.Lock()
assert.Equal(t, len(irp.interResultsForBlock), 0)
assert.Equal(t, len(irp.processedResult.mapProcessedResult[string(key)]), len(txs))
assert.Equal(t, 0, len(irp.interResultsForBlock))
assert.Equal(t, 0, len(irp.processedResult.mapProcessedResult[string(key)]))
irp.mutInterResultsForBlock.Unlock()

irp.InitProcessedResults(key)
irp.mutInterResultsForBlock.Lock()
assert.Equal(t, len(irp.processedResult.mapProcessedResult[string(key)]), 0)
assert.Equal(t, 0, len(irp.processedResult.mapProcessedResult[string(key)]))
irp.mutInterResultsForBlock.Unlock()
}

Expand Down
5 changes: 3 additions & 2 deletions process/block/postprocess/oneMBPostProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ func NewOneMiniBlockPostProcessor(
shardCoordinator: coordinator,
store: store,
storageType: storageType,
processedResult: processedResult{
processedResult: aggProcessedResults{
lastInitializedKey: nil,
initializedKeys: nil,
mapProcessedResult: make(map[string][][]byte),
keys: make([]string, 0),
},
economicsFee: economicsFee,
}
Expand Down

0 comments on commit 4794d97

Please sign in to comment.