From 3c9ebc29b0da44b49902f349c1dc0a2d92482092 Mon Sep 17 00:00:00 2001 From: Bui Quang Minh Date: Fri, 13 Dec 2024 10:07:03 +0700 Subject: [PATCH] eth/tracers: make transaction trace response compatible with geth The current transactionHash field in transaction trace response should be named txHash to be compatible with go-ethereum. This commit adds txHash instead of replacing the current transactionHash field and make them have the same value. This can help to avoid breaking change for current users of these methods since users are unlikely to use strict DisallowUnknownFields when decoding the json response. --- eth/tracers/api.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 8032874b55..4443db66b9 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -184,11 +184,32 @@ type StdTraceConfig struct { // txTraceResult is the result of a single transaction trace. type txTraceResult struct { - TransactionHash common.Hash `json:"transactionHash,omitempty"` + TransactionHash common.Hash `json:"transactionHash"` Result interface{} `json:"result,omitempty"` // Trace results produced by the tracer Error string `json:"error,omitempty"` // Trace failure produced by the tracer } +// Create a response that is compatible with go-ethereum +// with txHash field but still keep transactionHash field +// to avoid breaking change for current user. +// FIXME: Remove this function and change transactionHash +// to txHash after everyone has changed to use the new one. +func (result txTraceResult) MarshalJSON() ([]byte, error) { + newResult := struct { + TxHash common.Hash `json:"txHash"` + TransactionHash common.Hash `json:"transactionHash"` + Result interface{} `json:"result,omitempty"` + Error string `json:"error,omitempty"` + }{ + TxHash: result.TransactionHash, + TransactionHash: result.TransactionHash, + Result: result.Result, + Error: result.Error, + } + + return json.Marshal(&newResult) +} + // internalAndAccountResult is the result of a single transaction trace. type internalAndAccountResult struct { InternalTxs []*txTraceResult `json:"internalTxs,omitempty"` // Trace results produced by the tracer