eth: extract check for tracing transaction in block file (#19107)

Simplifies the transaction presense check to use a function to
determine if the transaction is present in the block provided
to trace, which originally had a redundant parenthesis and used
a `exist` flag to dictate control flow.
This commit is contained in:
Matthew Halpern 2019-02-19 09:49:24 -08:00 committed by Péter Szilágyi
parent 514a9472ad
commit 8af6c9e6a2
1 changed files with 12 additions and 7 deletions

View File

@ -526,13 +526,7 @@ func (api *PrivateDebugAPI) traceBlock(ctx context.Context, block *types.Block,
func (api *PrivateDebugAPI) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) { func (api *PrivateDebugAPI) standardTraceBlockToFile(ctx context.Context, block *types.Block, config *StdTraceConfig) ([]string, error) {
// If we're tracing a single transaction, make sure it's present // If we're tracing a single transaction, make sure it's present
if config != nil && config.TxHash != (common.Hash{}) { if config != nil && config.TxHash != (common.Hash{}) {
var exists bool if !containsTx(block, config.TxHash) {
for _, tx := range block.Transactions() {
if exists = (tx.Hash() == config.TxHash); exists {
break
}
}
if !exists {
return nil, fmt.Errorf("transaction %#x not found in block", config.TxHash) return nil, fmt.Errorf("transaction %#x not found in block", config.TxHash)
} }
} }
@ -625,6 +619,17 @@ func (api *PrivateDebugAPI) standardTraceBlockToFile(ctx context.Context, block
return dumps, nil return dumps, nil
} }
// containsTx reports whether the transaction with a certain hash
// is contained within the specified block.
func containsTx(block *types.Block, hash common.Hash) bool {
for _, tx := range block.Transactions() {
if tx.Hash() == hash {
return true
}
}
return false
}
// computeStateDB retrieves the state database associated with a certain block. // computeStateDB retrieves the state database associated with a certain block.
// If no state is locally available for the given block, a number of blocks are // If no state is locally available for the given block, a number of blocks are
// attempted to be reexecuted to generate the desired state. // attempted to be reexecuted to generate the desired state.