cmd/evm/internal/t8ntool: polish the code

This commit is contained in:
Gary Rong 2025-01-21 13:22:38 +08:00
parent b96132b8db
commit 5e73c2be93
4 changed files with 11 additions and 17 deletions

View file

@ -127,8 +127,7 @@ type rejectedTx struct {
} }
// Apply applies a set of transactions to a pre-state // Apply applies a set of transactions to a pre-state
func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, txIt txIterator, miningReward int64) (*state.StateDB, *ExecutionResult, []byte, error) {
txIt txIterator, miningReward int64) (*state.StateDB, *ExecutionResult, []byte, error) {
// Capture errors for BLOCKHASH operation, if we haven't been supplied the // Capture errors for BLOCKHASH operation, if we haven't been supplied the
// required blockhashes // required blockhashes
var hashError error var hashError error

View file

@ -82,16 +82,16 @@ func newResultWriter(baseDir string, tracer *tracers.Tracer) *tracing.Hooks {
// OnTxStart creates a new output-file specific for this transaction, and invokes // OnTxStart creates a new output-file specific for this transaction, and invokes
// the inner OnTxStart handler. // the inner OnTxStart handler.
func (l *fileWritingTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) { func (l *fileWritingTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
// Open a new file, // Open a new file, or print a warning log if it's failed
fname := filepath.Join(l.baseDir, fmt.Sprintf("trace-%d-%v.%v", l.txIndex, tx.Hash().String(), l.suffix)) fname := filepath.Join(l.baseDir, fmt.Sprintf("trace-%d-%v.%v", l.txIndex, tx.Hash().String(), l.suffix))
traceFile, err := os.Create(fname) traceFile, err := os.Create(fname)
if err != nil { if err != nil {
log.Warn("Failed creating trace-file", "err", err) log.Warn("Failed creating trace-file", "err", err)
} } else {
log.Info("Created tracing-file", "path", fname) log.Info("Created tracing-file", "path", fname)
l.destination = traceFile l.destination = traceFile
}
if l.inner.OnTxStart != nil { if l.inner != nil && l.inner.OnTxStart != nil {
l.inner.OnTxStart(env, tx, from) l.inner.OnTxStart(env, tx, from)
} }
} }
@ -99,7 +99,7 @@ func (l *fileWritingTracer) OnTxStart(env *tracing.VMContext, tx *types.Transact
// OnTxEnd writes result (if getResult exist), closes any currently open output-file, // OnTxEnd writes result (if getResult exist), closes any currently open output-file,
// and invokes the inner OnTxEnd handler. // and invokes the inner OnTxEnd handler.
func (l *fileWritingTracer) OnTxEnd(receipt *types.Receipt, err error) { func (l *fileWritingTracer) OnTxEnd(receipt *types.Receipt, err error) {
if l.inner.OnTxEnd != nil { if l.inner != nil && l.inner.OnTxEnd != nil {
l.inner.OnTxEnd(receipt, err) l.inner.OnTxEnd(receipt, err)
} }
if l.getResult != nil && l.destination != nil { if l.getResult != nil && l.destination != nil {
@ -129,7 +129,7 @@ func (l *fileWritingTracer) hooks() *tracing.Hooks {
} }
}, },
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
if l.inner.OnOpcode != nil { if l.inner != nil && l.inner.OnOpcode != nil {
l.inner.OnOpcode(pc, op, gas, cost, scope, rData, depth, err) l.inner.OnOpcode(pc, op, gas, cost, scope, rData, depth, err)
} }
}, },

View file

@ -64,12 +64,10 @@ func (r *result) MarshalJSON() ([]byte, error) {
} }
func Transaction(ctx *cli.Context) error { func Transaction(ctx *cli.Context) error {
var (
err error
)
// We need to load the transactions. May be either in stdin input or in files. // We need to load the transactions. May be either in stdin input or in files.
// Check if anything needs to be read from stdin // Check if anything needs to be read from stdin
var ( var (
err error
txStr = ctx.String(InputTxsFlag.Name) txStr = ctx.String(InputTxsFlag.Name)
inputData = &input{} inputData = &input{}
chainConfig *params.ChainConfig chainConfig *params.ChainConfig
@ -82,6 +80,7 @@ func Transaction(ctx *cli.Context) error {
} }
// Set the chain id // Set the chain id
chainConfig.ChainID = big.NewInt(ctx.Int64(ChainIDFlag.Name)) chainConfig.ChainID = big.NewInt(ctx.Int64(ChainIDFlag.Name))
var body hexutil.Bytes var body hexutil.Bytes
if txStr == stdinSelector { if txStr == stdinSelector {
decoder := json.NewDecoder(os.Stdin) decoder := json.NewDecoder(os.Stdin)
@ -107,6 +106,7 @@ func Transaction(ctx *cli.Context) error {
} }
} }
signer := types.MakeSigner(chainConfig, new(big.Int), 0) signer := types.MakeSigner(chainConfig, new(big.Int), 0)
// We now have the transactions in 'body', which is supposed to be an // We now have the transactions in 'body', which is supposed to be an
// rlp list of transactions // rlp list of transactions
it, err := rlp.NewListIterator([]byte(body)) it, err := rlp.NewListIterator([]byte(body))

View file

@ -133,11 +133,6 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon
return evm return evm
} }
// SetTracer sets the tracer for following state transition.
func (evm *EVM) SetTracer(tracer *tracing.Hooks) {
evm.Config.Tracer = tracer
}
// SetPrecompiles sets the precompiled contracts for the EVM. // SetPrecompiles sets the precompiled contracts for the EVM.
// This method is only used through RPC calls. // This method is only used through RPC calls.
// It is not thread-safe. // It is not thread-safe.