mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-14 12:06:40 +00:00
core: clean up parallal state processor
This commit is contained in:
parent
97d384f1bb
commit
1d942bddf3
1 changed files with 36 additions and 21 deletions
|
|
@ -46,7 +46,7 @@ func NewParallelStateProcessor(chain *HeaderChain, vmConfig *vm.Config) Parallel
|
||||||
// performs post-tx state transition (system contracts and withdrawals)
|
// performs post-tx state transition (system contracts and withdrawals)
|
||||||
// and calculates the ProcessResult, returning it to be sent on resCh
|
// and calculates the ProcessResult, returning it to be sent on resCh
|
||||||
// by resultHandler
|
// by resultHandler
|
||||||
func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateReads *bal.StateAccesses, tExecStart time.Time, postTxState *state.StateDB, receipts types.Receipts) *ProcessResultWithMetrics {
|
func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateReads *bal.StateAccesses, tExecStart time.Time, postTxState *state.StateDB, results []txExecResult) *ProcessResultWithMetrics {
|
||||||
tExec := time.Since(tExecStart)
|
tExec := time.Since(tExecStart)
|
||||||
var requests [][]byte
|
var requests [][]byte
|
||||||
tPostprocessStart := time.Now()
|
tPostprocessStart := time.Now()
|
||||||
|
|
@ -73,21 +73,32 @@ func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateR
|
||||||
|
|
||||||
// 1. order the receipts by tx index
|
// 1. order the receipts by tx index
|
||||||
// 2. correctly calculate the cumulative gas used per receipt, returning bad block error if it goes over the allowed
|
// 2. correctly calculate the cumulative gas used per receipt, returning bad block error if it goes over the allowed
|
||||||
slices.SortFunc(receipts, func(a, b *types.Receipt) int {
|
slices.SortFunc(results, func(a, b txExecResult) int {
|
||||||
return cmp.Compare(a.TransactionIndex, b.TransactionIndex)
|
return cmp.Compare(a.receipt.TransactionIndex, b.receipt.TransactionIndex)
|
||||||
})
|
})
|
||||||
|
|
||||||
var cumulativeGasUsed uint64
|
var (
|
||||||
|
// We are maintaining two counters here:
|
||||||
|
// one counts all the cumulativeGas which includes refunds
|
||||||
|
// while the other counts only the usedGas which excludes refunds after Amsterdam
|
||||||
|
// We need the cumulativeGas for receipts and the usedGas for the block gas limit
|
||||||
|
cumulativeGas = uint64(0)
|
||||||
|
usedGas = uint64(0)
|
||||||
|
)
|
||||||
|
|
||||||
var allLogs []*types.Log
|
var allLogs []*types.Log
|
||||||
for _, receipt := range receipts {
|
var allReceipts []*types.Receipt
|
||||||
receipt.CumulativeGasUsed = cumulativeGasUsed + receipt.GasUsed
|
for _, result := range results {
|
||||||
cumulativeGasUsed += receipt.GasUsed
|
cumulativeGas += result.cumulativeGas
|
||||||
if receipt.CumulativeGasUsed > header.GasLimit {
|
usedGas += result.usedGas
|
||||||
|
result.receipt.CumulativeGasUsed = cumulativeGas
|
||||||
|
if result.receipt.CumulativeGasUsed > header.GasLimit {
|
||||||
return &ProcessResultWithMetrics{
|
return &ProcessResultWithMetrics{
|
||||||
ProcessResult: &ProcessResult{Error: fmt.Errorf("gas limit exceeded")},
|
ProcessResult: &ProcessResult{Error: fmt.Errorf("gas limit exceeded")},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
allLogs = append(allLogs, receipt.Logs...)
|
allLogs = append(allLogs, result.receipt.Logs...)
|
||||||
|
allReceipts = append(allReceipts, result.receipt)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read requests if Prague is enabled.
|
// Read requests if Prague is enabled.
|
||||||
|
|
@ -145,10 +156,10 @@ func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateR
|
||||||
|
|
||||||
return &ProcessResultWithMetrics{
|
return &ProcessResultWithMetrics{
|
||||||
ProcessResult: &ProcessResult{
|
ProcessResult: &ProcessResult{
|
||||||
Receipts: receipts,
|
Receipts: allReceipts,
|
||||||
Requests: requests,
|
Requests: requests,
|
||||||
Logs: allLogs,
|
Logs: allLogs,
|
||||||
GasUsed: cumulativeGasUsed,
|
GasUsed: usedGas,
|
||||||
},
|
},
|
||||||
PostProcessTime: tPostprocess,
|
PostProcessTime: tPostprocess,
|
||||||
ExecTime: tExec,
|
ExecTime: tExec,
|
||||||
|
|
@ -156,9 +167,11 @@ func (p *ParallelStateProcessor) prepareExecResult(block *types.Block, allStateR
|
||||||
}
|
}
|
||||||
|
|
||||||
type txExecResult struct {
|
type txExecResult struct {
|
||||||
idx int // transaction index
|
idx int // transaction index
|
||||||
receipt *types.Receipt
|
receipt *types.Receipt
|
||||||
err error // non-EVM error which would render the block invalid
|
err error // non-EVM error which would render the block invalid
|
||||||
|
cumulativeGas uint64
|
||||||
|
usedGas uint64
|
||||||
|
|
||||||
stateReads bal.StateAccesses
|
stateReads bal.StateAccesses
|
||||||
}
|
}
|
||||||
|
|
@ -168,7 +181,7 @@ type txExecResult struct {
|
||||||
func (p *ParallelStateProcessor) resultHandler(block *types.Block, preTxStateReads bal.StateAccesses, postTxState *state.StateDB, tExecStart time.Time, txResCh <-chan txExecResult, stateRootCalcResCh <-chan stateRootCalculationResult, resCh chan *ProcessResultWithMetrics) {
|
func (p *ParallelStateProcessor) resultHandler(block *types.Block, preTxStateReads bal.StateAccesses, postTxState *state.StateDB, tExecStart time.Time, txResCh <-chan txExecResult, stateRootCalcResCh <-chan stateRootCalculationResult, resCh chan *ProcessResultWithMetrics) {
|
||||||
// 1. if the block has transactions, receive the execution results from all of them and return an error on resCh if any txs err'd
|
// 1. if the block has transactions, receive the execution results from all of them and return an error on resCh if any txs err'd
|
||||||
// 2. once all txs are executed, compute the post-tx state transition and produce the ProcessResult sending it on resCh (or an error if the post-tx state didn't match what is reported in the BAL)
|
// 2. once all txs are executed, compute the post-tx state transition and produce the ProcessResult sending it on resCh (or an error if the post-tx state didn't match what is reported in the BAL)
|
||||||
var receipts []*types.Receipt
|
var results []txExecResult
|
||||||
gp := new(GasPool)
|
gp := new(GasPool)
|
||||||
gp.SetGas(block.GasLimit())
|
gp.SetGas(block.GasLimit())
|
||||||
var execErr error
|
var execErr error
|
||||||
|
|
@ -188,7 +201,7 @@ func (p *ParallelStateProcessor) resultHandler(block *types.Block, preTxStateRea
|
||||||
if err := gp.SubGas(res.receipt.GasUsed); err != nil {
|
if err := gp.SubGas(res.receipt.GasUsed); err != nil {
|
||||||
execErr = err
|
execErr = err
|
||||||
} else {
|
} else {
|
||||||
receipts = append(receipts, res.receipt)
|
results = append(results, res)
|
||||||
allReads.Merge(res.stateReads)
|
allReads.Merge(res.stateReads)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -206,7 +219,7 @@ func (p *ParallelStateProcessor) resultHandler(block *types.Block, preTxStateRea
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
execResults := p.prepareExecResult(block, &allReads, tExecStart, postTxState, receipts)
|
execResults := p.prepareExecResult(block, &allReads, tExecStart, postTxState, results)
|
||||||
rootCalcRes := <-stateRootCalcResCh
|
rootCalcRes := <-stateRootCalcResCh
|
||||||
|
|
||||||
if execResults.ProcessResult.Error != nil {
|
if execResults.ProcessResult.Error != nil {
|
||||||
|
|
@ -271,7 +284,7 @@ func (p *ParallelStateProcessor) execTx(block *types.Block, tx *types.Transactio
|
||||||
gp := new(GasPool)
|
gp := new(GasPool)
|
||||||
gp.SetGas(block.GasLimit())
|
gp.SetGas(block.GasLimit())
|
||||||
db.SetTxContext(tx.Hash(), txIdx)
|
db.SetTxContext(tx.Hash(), txIdx)
|
||||||
receipt, _, err := ApplyTransactionWithEVM(msg, gp, db, block.Number(), block.Hash(), context.Time, tx, 0, evm)
|
receipt, cumulativeGas, err := ApplyTransactionWithEVM(msg, gp, db, block.Number(), block.Hash(), context.Time, tx, 0, evm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("could not apply tx %d [%v]: %w", txIdx, tx.Hash().Hex(), err)
|
err := fmt.Errorf("could not apply tx %d [%v]: %w", txIdx, tx.Hash().Hex(), err)
|
||||||
return &txExecResult{err: err}
|
return &txExecResult{err: err}
|
||||||
|
|
@ -283,9 +296,11 @@ func (p *ParallelStateProcessor) execTx(block *types.Block, tx *types.Transactio
|
||||||
}
|
}
|
||||||
|
|
||||||
return &txExecResult{
|
return &txExecResult{
|
||||||
idx: txIdx,
|
idx: txIdx,
|
||||||
receipt: receipt,
|
receipt: receipt,
|
||||||
stateReads: accesses,
|
stateReads: accesses,
|
||||||
|
cumulativeGas: cumulativeGas,
|
||||||
|
usedGas: receipt.GasUsed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue