Skip to content

Commit

Permalink
feat: add scroll_getTxBlockTraceOnTopOfBlock API (#558)
Browse files Browse the repository at this point in the history
* init

* more

* fix draft

* refactor

* fix

* clean up

* minor

* fix

* fix

* clean up

* clean up

* improve
  • Loading branch information
0xmountaintop authored Nov 20, 2023
1 parent 3f9b23d commit f4b247d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
58 changes: 46 additions & 12 deletions eth/tracers/api_blocktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,56 @@ import (

type TraceBlock interface {
GetBlockTraceByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (trace *types.BlockTrace, err error)
GetTxBlockTraceOnTopOfBlock(ctx context.Context, tx *types.Transaction, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (*types.BlockTrace, error)
}

// GetBlockTraceByNumberOrHash replays the block and returns the structured BlockTrace by hash or number.
func (api *API) GetBlockTraceByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (trace *types.BlockTrace, err error) {
var block *types.Block
if number, ok := blockNrOrHash.Number(); ok {
block, err = api.blockByNumber(ctx, number)
}
if hash, ok := blockNrOrHash.Hash(); ok {
} else if hash, ok := blockNrOrHash.Hash(); ok {
block, err = api.blockByHash(ctx, hash)
} else {
return nil, errors.New("invalid arguments; neither block number nor hash specified")
}
if err != nil {
return nil, err
}
if block.NumberU64() == 0 {
return nil, errors.New("genesis is not traceable")
}
if config == nil {
config = &TraceConfig{
LogConfig: &vm.LogConfig{
EnableMemory: false,
EnableReturnData: true,
},
}
} else if config.Tracer != nil {
config.Tracer = nil
log.Warn("Tracer params is unsupported")

env, err := api.createTraceEnv(ctx, config, block)
if err != nil {
return nil, err
}

return env.GetBlockTrace(block)
}

func (api *API) GetTxBlockTraceOnTopOfBlock(ctx context.Context, tx *types.Transaction, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (*types.BlockTrace, error) {
// Try to retrieve the specified block
var (
err error
block *types.Block
)
if number, ok := blockNrOrHash.Number(); ok {
block, err = api.blockByNumber(ctx, number)
} else if hash, ok := blockNrOrHash.Hash(); ok {
block, err = api.blockByHash(ctx, hash)
} else {
return nil, errors.New("invalid arguments; neither block number nor hash specified")
}
if err != nil {
return nil, err
}
if block.NumberU64() == 0 {
return nil, errors.New("genesis is not traceable")
}

block = types.NewBlockWithHeader(block.Header()).WithBody([]*types.Transaction{tx}, nil)

// create current execution environment.
env, err := api.createTraceEnv(ctx, config, block)
if err != nil {
Expand All @@ -53,6 +74,18 @@ func (api *API) GetBlockTraceByNumberOrHash(ctx context.Context, blockNrOrHash r

// Make trace environment for current block.
func (api *API) createTraceEnv(ctx context.Context, config *TraceConfig, block *types.Block) (*core.TraceEnv, error) {
if config == nil {
config = &TraceConfig{
LogConfig: &vm.LogConfig{
EnableMemory: false,
EnableReturnData: true,
},
}
} else if config.Tracer != nil {
config.Tracer = nil
log.Warn("Tracer params is unsupported")
}

parent, err := api.blockByNumberAndHash(ctx, rpc.BlockNumber(block.NumberU64()-1), block.ParentHash())
if err != nil {
return nil, err
Expand All @@ -65,6 +98,7 @@ func (api *API) createTraceEnv(ctx context.Context, config *TraceConfig, block *
if err != nil {
return nil, err
}

chaindb := api.backend.ChainDb()
return core.CreateTraceEnv(api.backend.ChainConfig(), api.chainContext(ctx), api.backend.Engine(), chaindb, statedb, parent, block, true)
}
6 changes: 6 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,12 @@ web3._extend({
call: 'scroll_getBlockTraceByNumberOrHash',
params: 1
}),
new web3._extend.Method({
name: 'getTxBlockTraceOnTopOfBlock',
call: 'scroll_getTxBlockTraceOnTopOfBlock',
params: 3,
inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null, null]
}),
new web3._extend.Method({
name: 'getL1MessageByIndex',
call: 'scroll_getL1MessageByIndex',
Expand Down
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 1 // Minor version component of the current release
VersionPatch = 3 // Patch version component of the current release
VersionPatch = 4 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down

0 comments on commit f4b247d

Please sign in to comment.