From 480ccf2aa8f8baac7ae8478f51d74239cfd33a25 Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Wed, 5 Apr 2023 11:12:57 +0530 Subject: [PATCH] Optimized the dependency metadata structure (#804) * removed the first 2 element in TxDependency[i] * addressed comments --- core/parallel_state_processor.go | 28 ++++++++++------------------ core/types/block.go | 8 +++----- miner/worker.go | 20 ++++++++------------ miner/worker_test.go | 5 ++++- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/core/parallel_state_processor.go b/core/parallel_state_processor.go index 0c97d074ff..c4f3530374 100644 --- a/core/parallel_state_processor.go +++ b/core/parallel_state_processor.go @@ -297,7 +297,7 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat coinbase, _ := p.bc.Engine().Author(header) - deps, delayMap := GetDeps(block.Header().TxDependency) + deps := GetDeps(block.Header().TxDependency) if block.Header().TxDependency != nil { metadata = true @@ -315,9 +315,11 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat cleansdb := statedb.Copy() - if len(header.TxDependency) > 0 { - shouldDelayFeeCal = delayMap[i] + if msg.From() == coinbase { + shouldDelayFeeCal = false + } + if len(header.TxDependency) != len(block.Transactions()) { task := &ExecutionTask{ msg: msg, config: p.config, @@ -343,10 +345,6 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat tasks = append(tasks, task) } else { - if msg.From() == coinbase { - shouldDelayFeeCal = false - } - task := &ExecutionTask{ msg: msg, config: p.config, @@ -430,22 +428,16 @@ func (p *ParallelStateProcessor) Process(block *types.Block, statedb *state.Stat return receipts, allLogs, *usedGas, nil } -func GetDeps(txDependency [][]uint64) (map[int][]int, map[int]bool) { +func GetDeps(txDependency [][]uint64) map[int][]int { deps := make(map[int][]int) - delayMap := make(map[int]bool) for i := 0; i <= len(txDependency)-1; i++ { - idx := int(txDependency[i][0]) - shouldDelay := txDependency[i][1] == 1 + deps[i] = []int{} - delayMap[idx] = shouldDelay - - deps[idx] = []int{} - - for j := 2; j <= len(txDependency[i])-1; j++ { - deps[idx] = append(deps[idx], int(txDependency[i][j])) + for j := 0; j <= len(txDependency[i])-1; j++ { + deps[i] = append(deps[i], int(txDependency[i][j])) } } - return deps, delayMap + return deps } diff --git a/core/types/block.go b/core/types/block.go index 0d91e543da..0af6a35501 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -87,11 +87,9 @@ type Header struct { // BaseFee was added by EIP-1559 and is ignored in legacy headers. BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"` - // length of TxDependency -> n (n = number of transactions in the block) - // length of TxDependency[i] -> 2 + k (k = a whole number) - // first 2 element in TxDependency[i] -> transaction index, and flag representing if delay is allowed or not - // (0 -> delay is not allowed, 1 -> delay is allowed) - // next k elements in TxDependency[i] -> transaction indexes on which transaction i is dependent on + // length of TxDependency -> n (n = number of transactions in the block) + // length of TxDependency[i] -> k (k = a whole number) + // k elements in TxDependency[i] -> transaction indexes on which transaction i is dependent on TxDependency [][]uint64 `json:"txDependency" rlp:"optional"` /* diff --git a/miner/worker.go b/miner/worker.go index 3cc06644bc..b2e420746b 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -1133,28 +1133,20 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP if len(mvReadMapList) > 0 { tempDeps := make([][]uint64, len(mvReadMapList)) - // adding for txIdx = 0 - tempDeps[0] = []uint64{uint64(0)} - tempDeps[0] = append(tempDeps[0], 1) - for j := range deps[0] { tempDeps[0] = append(tempDeps[0], uint64(j)) } - for i := 1; i <= len(mvReadMapList)-1; i++ { - tempDeps[i] = []uint64{uint64(i)} + delayFlag := true + for i := 1; i <= len(mvReadMapList)-1; i++ { reads := mvReadMapList[i-1] _, ok1 := reads[blockstm.NewSubpathKey(env.coinbase, state.BalancePath)] _, ok2 := reads[blockstm.NewSubpathKey(common.HexToAddress(w.chainConfig.Bor.CalculateBurntContract(env.header.Number.Uint64())), state.BalancePath)] if ok1 || ok2 { - // 0 -> delay is not allowed - tempDeps[i] = append(tempDeps[i], 0) - } else { - // 1 -> delay is allowed - tempDeps[i] = append(tempDeps[i], 1) + delayFlag = false } for j := range deps[i] { @@ -1162,7 +1154,11 @@ func (w *worker) commitTransactions(env *environment, txs *types.TransactionsByP } } - env.header.TxDependency = tempDeps + if delayFlag { + env.header.TxDependency = tempDeps + } else { + env.header.TxDependency = nil + } } else { env.header.TxDependency = nil } diff --git a/miner/worker_test.go b/miner/worker_test.go index 3306ad4069..7bb5f5bf16 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -819,8 +819,11 @@ func BenchmarkBorMiningBlockSTMMetadata(b *testing.B) { } } else { deps := block.TxDependency() + if len(deps[0]) != 0 { + b.Fatalf("wrong dependency") + } for i := 1; i < block.Transactions().Len(); i++ { - if deps[i][0] != uint64(i) || deps[i][1] != uint64(0) || deps[i][2] != uint64(i-1) || len(deps[i]) != 3 { + if deps[i][0] != uint64(i-1) || len(deps[i]) != 1 { b.Fatalf("wrong dependency") } }