From 3b10afd966a327e9e57cc26ef62abb45d6b6c2a4 Mon Sep 17 00:00:00 2001 From: peekpi <894646171@qq.com> Date: Wed, 31 Mar 2021 18:35:22 +0800 Subject: [PATCH] [rpc] add new RPC 'trace_block' --- rpc/rpc.go | 8 +++++--- rpc/tracer.go | 10 +++++++--- rpc/tracerParity.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 rpc/tracerParity.go diff --git a/rpc/rpc.go b/rpc/rpc.go index 288057300..3cf3b1da2 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -21,6 +21,7 @@ const ( V2 Eth Debug + Trace ) const ( @@ -43,9 +44,9 @@ const ( var ( // HTTPModules .. - HTTPModules = []string{"hmy", "hmyv2", "eth", "debug", netNamespace, netV1Namespace, netV2Namespace, web3Namespace, "explorer"} + HTTPModules = []string{"hmy", "hmyv2", "eth", "debug", "trace", netNamespace, netV1Namespace, netV2Namespace, web3Namespace, "explorer"} // WSModules .. - WSModules = []string{"hmy", "hmyv2", "eth", "debug", netNamespace, netV1Namespace, netV2Namespace, web3Namespace, "web3"} + WSModules = []string{"hmy", "hmyv2", "eth", "debug", "trace", netNamespace, netV1Namespace, netV2Namespace, web3Namespace, "web3"} httpListener net.Listener httpHandler *rpc.Server @@ -140,7 +141,8 @@ func getAPIs(hmy *hmy.Harmony, debugEnable bool) []rpc.API { NewPublicPoolAPI(hmy, Eth), NewPublicStakingAPI(hmy, V1), NewPublicStakingAPI(hmy, V2), - NewPublicTracerAPI(hmy, Debug), + NewPublicTraceAPI(hmy, Debug), // Debug version means geth trace rpc + NewPublicTraceAPI(hmy, Trace), // Trace version means parity trace rpc // Legacy methods (subject to removal) v1.NewPublicLegacyAPI(hmy, "hmy"), eth.NewPublicEthService(hmy, "eth"), diff --git a/rpc/tracer.go b/rpc/tracer.go index beb3b489b..114f7e4c7 100644 --- a/rpc/tracer.go +++ b/rpc/tracer.go @@ -55,12 +55,16 @@ type PublicTracerService struct { version Version } -// NewPublicTracerAPI creates a new API for the RPC interface -func NewPublicTracerAPI(hmy *hmy.Harmony, version Version) rpc.API { +// NewPublicTraceAPI creates a new API for the RPC interface +func NewPublicTraceAPI(hmy *hmy.Harmony, version Version) rpc.API { + var service interface{} = &PublicTracerService{hmy, version} + if version == Trace { + service = &PublicParityTracerService{service.(*PublicTracerService)} + } return rpc.API{ Namespace: version.Namespace(), Version: APIVersion, - Service: &PublicTracerService{hmy, version}, + Service: service, Public: true, } } diff --git a/rpc/tracerParity.go b/rpc/tracerParity.go new file mode 100644 index 000000000..528c5e0f4 --- /dev/null +++ b/rpc/tracerParity.go @@ -0,0 +1,38 @@ +package rpc + +import ( + "context" + "encoding/json" + "errors" + + "github.com/ethereum/go-ethereum/rpc" + "github.com/harmony-one/harmony/hmy" +) + +type PublicParityTracerService struct { + *PublicTracerService +} + +// trace_block RPC +func (s *PublicParityTracerService) Block(ctx context.Context, number rpc.BlockNumber) (interface{}, error) { + block := s.hmy.BlockChain.GetBlockByNumber(uint64(number)) + if block == nil { + return block, errors.New("block not exist") + } + traceJs := "blockTracer" + results, err := s.hmy.TraceBlock(ctx, block, &hmy.TraceConfig{Tracer: &traceJs}) + if err != nil { + return results, err + } + var resultArray []interface{} + for _, result := range results { + raw, ok := result.Result.(json.RawMessage) + if !ok { + return results, errors.New("expected json.RawMessage") + } + var subArray []interface{} + json.Unmarshal(raw, &subArray) + resultArray = append(resultArray, subArray...) + } + return resultArray, nil +}