add trace_trasnaction api

This commit is contained in:
barryz 2022-06-28 09:20:01 +08:00
parent 7dd5de08bb
commit 7895734fa0
2 changed files with 71 additions and 0 deletions

65
eth/api_tx_trace.go Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
//
// // 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
}

View file

@ -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,
},
}...)
}