-
Notifications
You must be signed in to change notification settings - Fork 42
/
calls.go
169 lines (158 loc) · 4.47 KB
/
calls.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
// Copyright 2019 Martin Holst Swende
// This file is part of the goevmlab library.
//
// The 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.
//
// This 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 goevmlab library. If not, see <http://www.gnu.org/licenses/>.
package traces
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
)
type callInfo struct {
Ctx *common.Address
Dest *common.Address
Kind string
}
func (info *callInfo) String() string {
var (
cur = "NA"
dest = "NA"
)
if info.Ctx != nil {
cur = fmt.Sprintf("0x%x...", info.Ctx[:8])
}
if info.Dest != nil {
dest = fmt.Sprintf("0x%x...", info.Dest[:8])
}
if dest == cur {
return fmt.Sprintf("%s to %v", info.Kind, dest)
}
return fmt.Sprintf("%s to %v (ctx: %v)", info.Kind, dest, cur)
}
// stack is an object for basic stack operations.
type stack struct {
data []*callInfo
}
func newStack() *stack {
return &stack{data: make([]*callInfo, 0, 5)}
}
func (st *stack) copy() *stack {
cpy := &stack{data: make([]*callInfo, len(st.data))}
copy(cpy.data, st.data)
return cpy
}
func (st *stack) push(info *callInfo) {
st.data = append(st.data, info)
}
func (st *stack) pop() (ret *callInfo) {
if len(st.data) == 0 {
return nil
}
ret = st.data[len(st.data)-1]
st.data = st.data[:len(st.data)-1]
return
}
func (st *stack) peek() (ret *callInfo) {
if len(st.data) == 0 {
return nil
}
return st.data[len(st.data)-1]
}
// AnalyzeCalls scans through the ops, and assigns context-addresses to the
// lines.
func AnalyzeCalls(trace *Traces) {
callStack := newStack()
var currentAddress *common.Address
var prevLine *TraceLine
for _, line := range trace.Ops {
if prevLine != nil {
if cDepth, pDepth := line.Depth(), prevLine.Depth(); cDepth != pDepth {
// Make a new callstack
callStack = callStack.copy()
if cDepth > pDepth {
// A call or create was made here
newAddress, callDest, callName := determineDestination(prevLine.log, currentAddress)
currentAddress = newAddress
callStack.push(&callInfo{
Ctx: newAddress,
Dest: callDest,
Kind: callName,
})
} else {
// We backed out
callStack.pop()
info := callStack.peek()
if info != nil {
currentAddress = info.Ctx
} else {
currentAddress = nil
}
}
}
line.address = currentAddress
line.callStack = callStack.data
}
prevLine = line
}
}
// determineDestination looks at the stack args and determines what the call
// address is. Returns:
// contextAddr -- the execution context
// callDest -- the call destination
// name -- type of call
func determineDestination(log *logger.StructLog, current *common.Address) (contextAddr, callDest *common.Address, name string) {
switch log.Op {
case vm.CALL:
name = "CALL"
if len(log.Stack) > 1 {
a := common.Address(log.Stack[1].Bytes20())
callDest = &a
contextAddr = &a
}
case vm.STATICCALL:
name = "SCALL"
if len(log.Stack) > 1 {
a := common.Address(log.Stack[1].Bytes20())
callDest = &a
contextAddr = &a
}
case vm.DELEGATECALL:
// The stack index is 1, but the actual execution context remains the same
name = "DCALL"
if len(log.Stack) > 1 {
a := common.Address(log.Stack[1].Bytes20())
callDest = &a
contextAddr = current
}
case vm.CALLCODE:
// The stack index is 1, but the actual execution context remains the same
name = "CCALL"
if len(log.Stack) > 1 {
a := common.Address(log.Stack[1].Bytes20())
callDest = &a
contextAddr = current
}
case vm.CREATE:
// In order to figure this out, we would need both nonce and current address
// while we _may_ have the address, we don't have the nonce
name = "CREATE"
case vm.CREATE2:
// In order to figure this out, we needs salt, initcode and current address
// while we _may_ theoretically be able to sort it out, by inspecting the
// memory, it would be pretty flaky
name = "CREATE2"
}
return contextAddr, callDest, name
}