-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
executable file
·210 lines (184 loc) · 7.73 KB
/
tracer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2017 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 rpc
import (
"bytes"
"context"
"errors"
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/zennittians/intelchain/core"
"github.com/zennittians/intelchain/core/rawdb"
"github.com/zennittians/intelchain/core/types"
"github.com/zennittians/intelchain/eth/rpc"
"github.com/zennittians/intelchain/itc"
)
const (
// defaultTraceTimeout is the amount of time a single transaction can execute
// by default before being forcefully aborted.
defaultTraceTimeout = 5 * time.Second
// defaultTraceReExec is the number of blocks the tracer is willing to go back
// and re-execute to produce missing historical state necessary to run a specific
// trace.
defaultTraceReexec = uint64(128)
)
var (
// ErrNotAvailable to indicate the RPC is not ready for public use
ErrNotAvailable = errors.New("RPC not available yet")
)
// PublicTracerService provides an API to access Intelchain's staking services.
// It offers only methods that operate on public data that is freely available to anyone.
type PublicTracerService struct {
itc *itc.Intelchain
version Version
}
// NewPublicTraceAPI creates a new API for the RPC interface
func NewPublicTraceAPI(itc *itc.Intelchain, version Version) rpc.API {
var service interface{} = &PublicTracerService{itc, version}
if version == Trace {
service = &PublicParityTracerService{service.(*PublicTracerService)}
}
return rpc.API{
Namespace: version.Namespace(),
Version: APIVersion,
Service: service,
Public: true,
}
}
// TraceChain returns the structured logs created during the execution of EVM
// between two blocks (excluding start) and returns them as a JSON object.
func (s *PublicTracerService) TraceChain(ctx context.Context, start, end rpc.BlockNumber, config *itc.TraceConfig) (*rpc.Subscription, error) {
timer := DoMetricRPCRequest(TraceChain)
defer DoRPCRequestDuration(TraceChain, timer)
// TODO (JL): Make API available after DoS testing
return nil, ErrNotAvailable
/*
if uint64(start) >= uint64(end) {
return nil, fmt.Errorf("start block can not be equal or greater than the end block")
}
currentBlock := s.itc.BlockChain.CurrentBlock().NumberU64()
if uint64(start) > currentBlock || uint64(end) > currentBlock {
return nil, ErrRequestedBlockTooHigh
}
from := s.itc.BlockChain.GetBlockByNumber(uint64(start))
if from == nil {
return nil, fmt.Errorf("start block #%d not found", start)
}
to := s.itc.BlockChain.GetBlockByNumber(uint64(end))
if to == nil {
return nil, fmt.Errorf("end block #%d not found", end)
}
return s.itc.TraceChain(ctx, from, to, config)
*/
}
// TraceBlockByNumber returns the structured logs created during the execution of
// EVM and returns them as a JSON object.
func (s *PublicTracerService) TraceBlockByNumber(ctx context.Context, number rpc.BlockNumber, config *itc.TraceConfig) ([]*itc.TxTraceResult, error) {
timer := DoMetricRPCRequest(TraceBlockByNumber)
defer DoRPCRequestDuration(TraceBlockByNumber, timer)
// Fetch the block that we want to trace
block := s.itc.BlockChain.GetBlockByNumber(uint64(number))
return s.itc.TraceBlock(ctx, block, config)
}
// TraceBlockByHash returns the structured logs created during the execution of
// EVM and returns them as a JSON object.
func (s *PublicTracerService) TraceBlockByHash(ctx context.Context, hash common.Hash, config *itc.TraceConfig) ([]*itc.TxTraceResult, error) {
timer := DoMetricRPCRequest(TraceBlockByHash)
defer DoRPCRequestDuration(TraceBlockByHash, timer)
block := s.itc.BlockChain.GetBlockByHash(hash)
if block == nil {
DoMetricRPCQueryInfo(TraceBlockByHash, FailedNumber)
return nil, fmt.Errorf("block %#x not found", hash)
}
return s.itc.TraceBlock(ctx, block, config)
}
// TraceBlock returns the structured logs created during the execution of EVM
// and returns them as a JSON object.
func (s *PublicTracerService) TraceBlock(ctx context.Context, blob []byte, config *itc.TraceConfig) ([]*itc.TxTraceResult, error) {
timer := DoMetricRPCRequest(TraceBlock)
defer DoRPCRequestDuration(TraceBlock, timer)
block := new(types.Block)
if err := rlp.Decode(bytes.NewReader(blob), block); err != nil {
DoMetricRPCQueryInfo(TraceBlock, FailedNumber)
return nil, fmt.Errorf("could not decode block: %v", err)
}
return s.itc.TraceBlock(ctx, block, config)
}
// TraceTransaction returns the structured logs created during the execution of EVM
// and returns them as a JSON object.
func (s *PublicTracerService) TraceTransaction(ctx context.Context, hash common.Hash, config *itc.TraceConfig) (interface{}, error) {
timer := DoMetricRPCRequest(TraceTransaction)
defer DoRPCRequestDuration(TraceTransaction, timer)
// Retrieve the transaction and assemble its EVM context
tx, blockHash, _, index := rawdb.ReadTransaction(s.itc.ChainDb(), hash)
if tx == nil {
DoMetricRPCQueryInfo(TraceTransaction, FailedNumber)
return nil, fmt.Errorf("transaction %#x not found", hash)
}
reexec := defaultTraceReexec
if config != nil && config.Reexec != nil {
reexec = *config.Reexec
}
// Retrieve the block
block := s.itc.BlockChain.GetBlockByHash(blockHash)
if block == nil {
DoMetricRPCQueryInfo(TraceTransaction, FailedNumber)
return nil, fmt.Errorf("block %#x not found", blockHash)
}
msg, vmctx, statedb, err := s.itc.ComputeTxEnv(block, int(index), reexec)
if err != nil {
DoMetricRPCQueryInfo(TraceTransaction, FailedNumber)
return nil, err
}
// Trace the transaction and return
statedb.Prepare(tx.ConvertToEth().Hash(), block.Hash(), int(index))
return s.itc.TraceTx(ctx, msg, vmctx, statedb, config)
}
// TraceCall lets you trace a given eth_call. It collects the structured logs created during the execution of EVM
// if the given transaction was added on top of the provided block and returns them as a JSON object.
// You can provide -2 as a block number to trace on top of the pending block.
// NOTE: Our version only supports block number as an input
func (s *PublicTracerService) TraceCall(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber, config *itc.TraceConfig) (interface{}, error) {
timer := DoMetricRPCRequest(TraceCall)
defer DoRPCRequestDuration(TraceCall, timer)
// First try to retrieve the state
statedb, header, err := s.itc.StateAndHeaderByNumber(ctx, blockNr)
if err != nil {
// Try to retrieve the specified block
block := s.itc.BlockChain.GetBlockByNumber(uint64(blockNr))
if block == nil {
DoMetricRPCQueryInfo(TraceCall, FailedNumber)
return nil, fmt.Errorf("block %v not found: %v", blockNr, err)
}
// try to recompute the state
reexec := defaultTraceReexec
if config != nil && config.Reexec != nil {
reexec = *config.Reexec
}
_, _, statedb, err = s.itc.ComputeTxEnv(block, 0, reexec)
if err != nil {
DoMetricRPCQueryInfo(TraceCall, FailedNumber)
return nil, err
}
}
// Execute the trace
msg := args.ToMessage(s.itc.RPCGasCap)
vmctx := core.NewEVMContext(msg, header, s.itc.BlockChain, nil)
// Trace the transaction and return
return s.itc.TraceTx(ctx, msg, vmctx, statedb, config)
}