diff --git a/eth/api_tx_trace.go b/eth/api_tx_trace.go new file mode 100644 index 0000000000..4576335d48 --- /dev/null +++ b/eth/api_tx_trace.go @@ -0,0 +1,65 @@ +// Copyright 2022 The go-ethereum Authors +// // This file is part of the go-ethereum library. +// // +// // The go-ethereum library is free software: you can redistribute it and/or modify +// // it under the terms of the GNU Lesser General Public License as published by +// // the Free Software Foundation, either version 3 of the License, or +// // (at your option) any later version. +// // +// // The go-ethereum library is distributed in the hope that it will be useful, +// // but WITHOUT ANY WARRANTY; without even the implied warranty of +// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// // GNU Lesser General Public License for more details. +// // +// // You should have received a copy of the GNU Lesser General Public License +// // along with the go-ethereum library. If not, see . +// +// // Package ethapi implements the general Ethereum API functions. + +package eth + +import ( + "bytes" + "context" + "fmt" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" + + txtracelib "github.com/DeBankDeFi/etherlib/pkg/txtracev2" +) + +// PublicTxTraceAPI provides an API to tracing transaction or block information. +// // It offers only methods that operate on public data that is freely available to anyone. +type PublicTxTraceAPI struct { + e *Ethereum +} + +// NewPublicTxTraceAPI creates a new trace API. +func NewPublicTxTraceAPI(e *Ethereum) *PublicTxTraceAPI { + return &PublicTxTraceAPI{e: e} +} + +// Transaction trace_transaction function returns transaction traces. +func (api *PublicTxTraceAPI) Transaction(ctx context.Context, txHash common.Hash) (interface{}, error) { + if api.e.blockchain == nil { + return []byte{}, fmt.Errorf("blockchain corruput") + } + + raw, err := api.e.blockchain.TxTraceStore().ReadTxTrace(ctx, txHash) + if err != nil { + return []byte{}, err + } + + if bytes.Equal(raw, []byte{}) { // empty response + return nil, fmt.Errorf("trace result of tx {%#v} not found in tracedb", txHash) + } + + flatten := new(txtracelib.ActionTraceList) + err = rlp.DecodeBytes(raw, flatten) + if err != nil { + return nil, fmt.Errorf("failed to decode rlp flatten traces: %v", err) + } + + return *flatten, nil +} diff --git a/eth/backend.go b/eth/backend.go index c60f3ff765..87da61d2bf 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -368,6 +368,12 @@ func (s *Ethereum) APIs() []rpc.API { Version: "1.0", Service: NewPreExecAPI(s), Public: true, + }, { + + Namespace: "trace", + Version: "1.0", + Service: NewPublicTxTraceAPI(s), + Public: true, }, }...) }