[rpc] add new RPC 'trace_block'

pull/3640/head
peekpi 4 years ago
parent 1ec45f9c6d
commit 3b10afd966
  1. 8
      rpc/rpc.go
  2. 10
      rpc/tracer.go
  3. 38
      rpc/tracerParity.go

@ -21,6 +21,7 @@ const (
V2 V2
Eth Eth
Debug Debug
Trace
) )
const ( const (
@ -43,9 +44,9 @@ const (
var ( var (
// HTTPModules .. // 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 ..
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 httpListener net.Listener
httpHandler *rpc.Server httpHandler *rpc.Server
@ -140,7 +141,8 @@ func getAPIs(hmy *hmy.Harmony, debugEnable bool) []rpc.API {
NewPublicPoolAPI(hmy, Eth), NewPublicPoolAPI(hmy, Eth),
NewPublicStakingAPI(hmy, V1), NewPublicStakingAPI(hmy, V1),
NewPublicStakingAPI(hmy, V2), 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) // Legacy methods (subject to removal)
v1.NewPublicLegacyAPI(hmy, "hmy"), v1.NewPublicLegacyAPI(hmy, "hmy"),
eth.NewPublicEthService(hmy, "eth"), eth.NewPublicEthService(hmy, "eth"),

@ -55,12 +55,16 @@ type PublicTracerService struct {
version Version version Version
} }
// NewPublicTracerAPI creates a new API for the RPC interface // NewPublicTraceAPI creates a new API for the RPC interface
func NewPublicTracerAPI(hmy *hmy.Harmony, version Version) rpc.API { 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{ return rpc.API{
Namespace: version.Namespace(), Namespace: version.Namespace(),
Version: APIVersion, Version: APIVersion,
Service: &PublicTracerService{hmy, version}, Service: service,
Public: true, Public: true,
} }
} }

@ -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
}
Loading…
Cancel
Save