From 40e8f088b6adb8dcdc050759d978d19d18327a77 Mon Sep 17 00:00:00 2001 From: maskpp Date: Thu, 31 Mar 2022 18:01:14 +0800 Subject: [PATCH] opt: upgrade blockResult process flow (#68) * upgrade blockResult process flow * fix format optimization --- core/blockchain.go | 17 +++++++++++------ core/vm/logger.go | 8 ++++---- miner/worker.go | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index a34779ee76..8c7fb7a177 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1188,17 +1188,17 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error { } // WriteBlockWithState writes the block and all associated state to the database. -func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, blockResult *types.BlockResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { +func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, evmTraces []*types.ExecutionResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { if !bc.chainmu.TryLock() { return NonStatTy, errInsertionInterrupted } defer bc.chainmu.Unlock() - return bc.writeBlockWithState(block, receipts, logs, blockResult, state, emitHeadEvent) + return bc.writeBlockWithState(block, receipts, logs, evmTraces, state, emitHeadEvent) } // writeBlockWithState writes the block and all associated state to the database, // but is expects the chain mutex to be held. -func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, blockResult *types.BlockResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { +func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, evmTraces []*types.ExecutionResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) { if bc.insertStopped() { return NonStatTy, errInsertionInterrupted } @@ -1320,8 +1320,9 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.futureBlocks.Remove(block.Hash()) // Fill blockResult content - if blockResult != nil { - bc.writeBlockResult(state, block, blockResult) + var blockResult *types.BlockResult + if evmTraces != nil { + blockResult = bc.writeBlockResult(state, block, evmTraces) bc.blockResultCache.Add(block.Hash(), blockResult) } @@ -1345,7 +1346,10 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. } // Fill blockResult content -func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block, blockResult *types.BlockResult) { +func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block, evmTraces []*types.ExecutionResult) *types.BlockResult { + blockResult := &types.BlockResult{ + ExecutionResults: evmTraces, + } coinbase := types.AccountProofWrapper{ Address: block.Coinbase(), Nonce: state.GetNonce(block.Coinbase()), @@ -1396,6 +1400,7 @@ func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block, evmTrace.ByteCode = hexutil.Encode(tx.Data()) } } + return blockResult } // addFutureBlock checks if the block is within the max allowed window to get diff --git a/core/vm/logger.go b/core/vm/logger.go index 419fda7a59..5195ba3c8d 100644 --- a/core/vm/logger.go +++ b/core/vm/logger.go @@ -181,7 +181,7 @@ func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scop } } var ( - recordStorageDetail bool = false + recordStorageDetail bool storage Storage storageKey common.Hash storageValue common.Hash @@ -189,12 +189,12 @@ func (l *StructLogger) CaptureState(pc uint64, op OpCode, gas, cost uint64, scop if !l.cfg.DisableStorage { if op == SLOAD && stack.len() >= 1 { recordStorageDetail = true - storageKey = common.Hash(stack.data[stack.len()-1].Bytes32()) + storageKey = stack.data[stack.len()-1].Bytes32() storageValue = l.env.StateDB.GetState(contract.Address(), storageKey) } else if op == SSTORE && stack.len() >= 2 { recordStorageDetail = true - storageKey = common.Hash(stack.data[stack.len()-1].Bytes32()) - storageValue = common.Hash(stack.data[stack.len()-2].Bytes32()) + storageKey = stack.data[stack.len()-1].Bytes32() + storageValue = stack.data[stack.len()-2].Bytes32() } } extraData := types.NewExtraData() diff --git a/miner/worker.go b/miner/worker.go index ce8de2d085..da67107a70 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -668,7 +668,7 @@ func (w *worker) resultLoop() { logs = append(logs, receipt.Logs...) } // Commit block and state to database. - _, err := w.chain.WriteBlockWithState(block, receipts, logs, &types.BlockResult{ExecutionResults: evmTraces}, task.state, true) + _, err := w.chain.WriteBlockWithState(block, receipts, logs, evmTraces, task.state, true) if err != nil { log.Error("Failed writing block to chain", "err", err) continue