Skip to content

Commit

Permalink
opt: upgrade blockResult process flow (ethereum#68)
Browse files Browse the repository at this point in the history
* upgrade blockResult process flow

* fix format optimization
  • Loading branch information
mask-pp authored Mar 31, 2022
1 parent a96775b commit 40e8f08
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
17 changes: 11 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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()),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions core/vm/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,20 @@ 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
)
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()
Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40e8f08

Please sign in to comment.