Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug and trace method in eth client #313

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions domain/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,29 @@ type TransactionReceipt struct {
TransactionIndex *string `json:"transactionIndex"`
}

// TraceCallTransaction contains the fields of the to-be-simulated transaction.
type TraceCallTransaction struct {
From string `json:"from"`
To string `json:"to"`
Gas *int64 `json:"gas,omitempty"`
GasPrice *int64 `json:"gasPrice,omitempty"`
Value *int64 `json:"value,omitempty"`
Data string `json:"data"`
}

// TraceCallConfig contains the tracer configuration to be used while simulating the transaction.
type TraceCallConfig struct {
Tracer string `json:"tracer,omitempty"`
TracerConfig *TracerConfig `json:"tracerConfig,omitempty"`
StateOverrides map[string]interface{} `json:"stateOverrides,omitempty"`
}

// TracerConfig contains some extra tracer parameters.
type TracerConfig struct {
WithLog bool `json:"withLog,omitempty"`
OnlyTopCall bool `json:"onlyTopCall,omitempty"`
}

// TraceAction is an element of a trace_block Trace response
type TraceAction struct {
From *string `json:"from"`
Expand Down
55 changes: 48 additions & 7 deletions ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,27 @@ type Client interface {
TransactionReceipt(ctx context.Context, txHash string) (*domain.TransactionReceipt, error)
ChainID(ctx context.Context) (*big.Int, error)
TraceBlock(ctx context.Context, number *big.Int) ([]domain.Trace, error)
DebugTraceCall(
ctx context.Context, req *domain.TraceCallTransaction,
block any, traceCallConfig domain.TraceCallConfig,
result interface{},
) error
GetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)
SubscribeToHead(ctx context.Context) (domain.HeaderCh, error)

health.Reporter
}

const blocksByNumber = "eth_getBlockByNumber"
const blocksByHash = "eth_getBlockByHash"
const blockNumber = "eth_blockNumber"
const getLogs = "eth_getLogs"
const transactionReceipt = "eth_getTransactionReceipt"
const traceBlock = "trace_block"
const chainId = "eth_chainId"
const (
blocksByNumber = "eth_getBlockByNumber"
blocksByHash = "eth_getBlockByHash"
blockNumber = "eth_blockNumber"
getLogs = "eth_getLogs"
transactionReceipt = "eth_getTransactionReceipt"
traceBlock = "trace_block"
debugTraceCall = "debug_traceCall"
chainId = "eth_chainId"
)

const defaultRetryInterval = time.Second * 15

Expand Down Expand Up @@ -242,6 +250,39 @@ func (e *streamEthClient) TraceBlock(ctx context.Context, number *big.Int) ([]do
return result, err
}

// DebugTraceCall returns the traces of a call.
func (e *streamEthClient) DebugTraceCall(
ctx context.Context, req *domain.TraceCallTransaction,
block any, traceCallConfig domain.TraceCallConfig,
result interface{},
) error {
name := fmt.Sprintf("%s(%v)", debugTraceCall, req)
log.Debugf(name)

switch block.(type) {
case string:
case *rpc.BlockNumberOrHash:
default:
return errors.New("invalid block number type")
}

args := []interface{}{req, block, traceCallConfig}

err := withBackoff(ctx, name, func(ctx context.Context) error {
err := e.rpcClient.CallContext(ctx, &result, debugTraceCall, args...)
if err != nil {
return err
}

return nil
}, RetryOptions{
MinBackoff: pointDur(e.retryInterval),
MaxElapsedTime: pointDur(1 * time.Minute),
MaxBackoff: pointDur(e.retryInterval),
}, nil, nil)
return err
}

// GetLogs returns the set of logs for a block
func (e *streamEthClient) GetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
name := fmt.Sprintf("%s(%v)", getLogs, q)
Expand Down
14 changes: 14 additions & 0 deletions ethereum/mocks/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading