Skip to content

Commit 836ffa8

Browse files
wgr523gzliudan
authored andcommitted
feat: eth/tracers: support for golang tracers + add golang callTracer
cf. ethereum#23708
1 parent 9a72be6 commit 836ffa8

28 files changed

+2847
-0
lines changed

cmd/XDC/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ import (
3737
"github.com/XinFinOrg/XDPoSChain/log"
3838
"github.com/XinFinOrg/XDPoSChain/metrics"
3939
"github.com/XinFinOrg/XDPoSChain/node"
40+
41+
// Force-load the native, to trigger registration
42+
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native"
43+
4044
"gopkg.in/urfave/cli.v1"
4145
)
4246

eth/tracers/native/call.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package native
18+
19+
import (
20+
"encoding/json"
21+
"errors"
22+
"math/big"
23+
"strconv"
24+
"strings"
25+
"sync/atomic"
26+
"time"
27+
28+
"github.com/XinFinOrg/XDPoSChain/common"
29+
"github.com/XinFinOrg/XDPoSChain/core/vm"
30+
"github.com/XinFinOrg/XDPoSChain/eth/tracers"
31+
)
32+
33+
func init() {
34+
tracers.RegisterNativeTracer("callTracerNative", NewCallTracer)
35+
}
36+
37+
type callFrame struct {
38+
Type string `json:"type"`
39+
From string `json:"from"`
40+
To string `json:"to,omitempty"`
41+
Value string `json:"value,omitempty"`
42+
Gas string `json:"gas"`
43+
GasUsed string `json:"gasUsed"`
44+
Input string `json:"input"`
45+
Output string `json:"output,omitempty"`
46+
Error string `json:"error,omitempty"`
47+
Calls []callFrame `json:"calls,omitempty"`
48+
}
49+
50+
type callTracer struct {
51+
callstack []callFrame
52+
interrupt uint32 // Atomic flag to signal execution interruption
53+
reason error // Textual reason for the interruption
54+
}
55+
56+
// NewCallTracer returns a native go tracer which tracks
57+
// call frames of a tx, and implements vm.EVMLogger.
58+
func NewCallTracer() tracers.Tracer {
59+
// First callframe contains tx context info
60+
// and is populated on start and end.
61+
t := &callTracer{callstack: make([]callFrame, 1)}
62+
return t
63+
}
64+
65+
func (t *callTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
66+
t.callstack[0] = callFrame{
67+
Type: "CALL",
68+
From: addrToHex(from),
69+
To: addrToHex(to),
70+
Input: bytesToHex(input),
71+
Gas: uintToHex(gas),
72+
Value: bigToHex(value),
73+
}
74+
if create {
75+
t.callstack[0].Type = "CREATE"
76+
}
77+
}
78+
79+
func (t *callTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
80+
t.callstack[0].GasUsed = uintToHex(gasUsed)
81+
if err != nil {
82+
t.callstack[0].Error = err.Error()
83+
if err.Error() == "execution reverted" && len(output) > 0 {
84+
t.callstack[0].Output = bytesToHex(output)
85+
}
86+
} else {
87+
t.callstack[0].Output = bytesToHex(output)
88+
}
89+
}
90+
91+
func (t *callTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
92+
}
93+
94+
func (t *callTracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, _ *vm.ScopeContext, depth int, err error) {
95+
}
96+
97+
func (t *callTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
98+
// Skip if tracing was interrupted
99+
if atomic.LoadUint32(&t.interrupt) > 0 {
100+
// TODO: env.Cancel()
101+
return
102+
}
103+
104+
call := callFrame{
105+
Type: typ.String(),
106+
From: addrToHex(from),
107+
To: addrToHex(to),
108+
Input: bytesToHex(input),
109+
Gas: uintToHex(gas),
110+
Value: bigToHex(value),
111+
}
112+
t.callstack = append(t.callstack, call)
113+
}
114+
115+
func (t *callTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
116+
size := len(t.callstack)
117+
if size <= 1 {
118+
return
119+
}
120+
// pop call
121+
call := t.callstack[size-1]
122+
t.callstack = t.callstack[:size-1]
123+
size -= 1
124+
125+
call.GasUsed = uintToHex(gasUsed)
126+
if err == nil {
127+
call.Output = bytesToHex(output)
128+
} else {
129+
call.Error = err.Error()
130+
if call.Type == "CREATE" || call.Type == "CREATE2" {
131+
call.To = ""
132+
}
133+
}
134+
t.callstack[size-1].Calls = append(t.callstack[size-1].Calls, call)
135+
}
136+
137+
func (t *callTracer) GetResult() (json.RawMessage, error) {
138+
if len(t.callstack) != 1 {
139+
return nil, errors.New("incorrect number of top-level calls")
140+
}
141+
res, err := json.Marshal(t.callstack[0])
142+
if err != nil {
143+
return nil, err
144+
}
145+
return json.RawMessage(res), t.reason
146+
}
147+
148+
func (t *callTracer) Stop(err error) {
149+
t.reason = err
150+
atomic.StoreUint32(&t.interrupt, 1)
151+
}
152+
153+
func bytesToHex(s []byte) string {
154+
return "0x" + common.Bytes2Hex(s)
155+
}
156+
157+
func bigToHex(n *big.Int) string {
158+
if n == nil {
159+
return ""
160+
}
161+
return "0x" + n.Text(16)
162+
}
163+
164+
func uintToHex(n uint64) string {
165+
return "0x" + strconv.FormatUint(n, 16)
166+
}
167+
168+
func addrToHex(a common.Address) string {
169+
return strings.ToLower(a.Hex())
170+
}

eth/tracers/native/noop.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package native
2+
3+
import (
4+
"encoding/json"
5+
"math/big"
6+
"time"
7+
8+
"github.com/XinFinOrg/XDPoSChain/common"
9+
"github.com/XinFinOrg/XDPoSChain/core/vm"
10+
"github.com/XinFinOrg/XDPoSChain/eth/tracers"
11+
)
12+
13+
func init() {
14+
tracers.RegisterNativeTracer("noopTracerNative", NewNoopTracer)
15+
}
16+
17+
type noopTracer struct{}
18+
19+
func NewNoopTracer() tracers.Tracer {
20+
return &noopTracer{}
21+
}
22+
23+
func (t *noopTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
24+
}
25+
26+
func (t *noopTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
27+
}
28+
29+
func (t *noopTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
30+
}
31+
32+
func (t *noopTracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, _ *vm.ScopeContext, depth int, err error) {
33+
}
34+
35+
func (t *noopTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
36+
}
37+
38+
func (t *noopTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
39+
}
40+
41+
func (t *noopTracer) GetResult() (json.RawMessage, error) {
42+
return json.RawMessage(`{}`), nil
43+
}
44+
45+
func (t *noopTracer) Stop(err error) {
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"context": {
3+
"difficulty": "3755480783",
4+
"gasLimit": "5401723",
5+
"miner": "0xd049bfd667cb46aa3ef5df0da3e57db3be39e511",
6+
"number": "2294702",
7+
"timestamp": "1513676146"
8+
},
9+
"genesis": {
10+
"alloc": {
11+
"0x13e4acefe6a6700604929946e70e6443e4e73447": {
12+
"balance": "0xcf3e0938579f000",
13+
"code": "0x",
14+
"nonce": "9",
15+
"storage": {}
16+
},
17+
"0x7dc9c9730689ff0b0fd506c67db815f12d90a448": {
18+
"balance": "0x0",
19+
"code": "0x",
20+
"nonce": "0",
21+
"storage": {}
22+
}
23+
},
24+
"config": {
25+
"byzantiumBlock": 1700000,
26+
"chainId": 3,
27+
"daoForkSupport": true,
28+
"eip150Block": 0,
29+
"eip150Hash": "0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d",
30+
"eip155Block": 10,
31+
"eip158Block": 10,
32+
"ethash": {},
33+
"homesteadBlock": 0
34+
},
35+
"difficulty": "3757315409",
36+
"extraData": "0x566961425443",
37+
"gasLimit": "5406414",
38+
"hash": "0xae107f592eebdd9ff8d6ba00363676096e6afb0e1007a7d3d0af88173077378d",
39+
"miner": "0xd049bfd667cb46aa3ef5df0da3e57db3be39e511",
40+
"mixHash": "0xc927aa05a38bc3de864e95c33b3ae559d3f39c4ccd51cef6f113f9c50ba0caf1",
41+
"nonce": "0x93363bbd2c95f410",
42+
"number": "2294701",
43+
"stateRoot": "0x6b6737d5bde8058990483e915866bd1578014baeff57bd5e4ed228a2bfad635c",
44+
"timestamp": "1513676127",
45+
"totalDifficulty": "7160808139332585"
46+
},
47+
"input": "0xf907ef098504e3b29200830897be8080b9079c606060405260405160208061077c83398101604052808051906020019091905050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415151561007d57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600460006101000a81548160ff02191690831515021790555050610653806101296000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029000000000000000000000000c65e620a3a55451316168d57e268f5702ef56a1129a01060f46676a5dff6f407f0f51eb6f37f5c8c54e238c70221e18e65fc29d3ea65a0557b01c50ff4ffaac8ed6e5d31237a4ecbac843ab1bfe8bb0165a0060df7c54f",
48+
"result": {
49+
"from": "0x13e4acefe6a6700604929946e70e6443e4e73447",
50+
"gas": "0x5e106",
51+
"gasUsed": "0x5e106",
52+
"input": "0x606060405260405160208061077c83398101604052808051906020019091905050600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415151561007d57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600460006101000a81548160ff02191690831515021790555050610653806101296000396000f300606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029000000000000000000000000c65e620a3a55451316168d57e268f5702ef56a11",
53+
"output": "0x606060405260043610610083576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806305e4382a146100855780631c02708d146100ae5780632e1a7d4d146100c35780635114cb52146100e6578063a37dda2c146100fe578063ae200e7914610153578063b5769f70146101a8575b005b341561009057600080fd5b6100986101d1565b6040518082815260200191505060405180910390f35b34156100b957600080fd5b6100c16101d7565b005b34156100ce57600080fd5b6100e460048080359060200190919050506102eb565b005b6100fc6004808035906020019091905050610513565b005b341561010957600080fd5b6101116105d6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561015e57600080fd5b6101666105fc565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156101b357600080fd5b6101bb610621565b6040518082815260200191505060405180910390f35b60025481565b60011515600460009054906101000a900460ff1615151415156101f957600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806102a15750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b15156102ac57600080fd5b6000600460006101000a81548160ff0219169083151502179055506003543073ffffffffffffffffffffffffffffffffffffffff163103600281905550565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614806103935750600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b151561039e57600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561048357600060025411801561040757506002548111155b151561041257600080fd5b80600254036002819055506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561047e57600080fd5b610510565b600060035411801561049757506003548111155b15156104a257600080fd5b8060035403600381905550600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f19350505050151561050f57600080fd5b5b50565b60011515600460009054906101000a900460ff16151514151561053557600080fd5b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614801561059657506003548160035401115b80156105bd575080600354013073ffffffffffffffffffffffffffffffffffffffff163110155b15156105c857600080fd5b806003540160038190555050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600354815600a165627a7a72305820c3b849e8440987ce43eae3097b77672a69234d516351368b03fe5b7de03807910029",
54+
"to": "0x7dc9c9730689ff0b0fd506c67db815f12d90a448",
55+
"type": "CREATE",
56+
"value": "0x0"
57+
}
58+
}

0 commit comments

Comments
 (0)