This repository was archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtrace.go
More file actions
98 lines (77 loc) · 1.72 KB
/
trace.go
File metadata and controls
98 lines (77 loc) · 1.72 KB
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
package ethertest
import (
"encoding/json"
"fmt"
)
type Contract struct {
Name string `json:"name"`
Source string `json:"source"`
}
func (c Contract) lines(from, to int) (int, string) {
var realFrom, realTo int
for realFrom = from; realFrom > 0 && c.Source[realFrom] != '\n'; realFrom-- {
}
for realTo = to; realTo < len(c.Source) && c.Source[realTo] != '\n'; realTo++ {
}
lineNumber := 0
for i := 0; i < realFrom; i++ {
if c.Source[i] == '\n' {
lineNumber++
}
}
return lineNumber, c.Source[realFrom:realTo]
}
type Step struct {
contractIndex int
from int
to int
}
func (s Step) MarshalJSON() ([]byte, error) {
return json.Marshal([]int{s.contractIndex, s.from, s.to})
}
type Trace struct {
Contracts []Contract `json:"contracts"`
Steps []Step `json:"steps"`
}
func (t *Trace) LastStep() string {
if len(t.Steps) == 0 {
return "N/A"
}
step := t.Steps[len(t.Steps)-1]
contract := t.Contracts[step.contractIndex]
lineNr, source := contract.lines(step.from, step.to)
return fmt.Sprintf("%s:%d\n%s\n", contract.Name, lineNr+1, source)
}
type tracer struct {
trace *Trace
}
func newTracer() *tracer {
return &tracer{
trace: &Trace{},
}
}
func (t *tracer) reset() {
t.trace = &Trace{}
}
func (t *tracer) executed(name, source string, start, end int) {
idx := -1
for i, c := range t.trace.Contracts {
if c.Name == name {
idx = i
}
}
if idx == -1 {
idx = len(t.trace.Contracts)
t.trace.Contracts = append(t.trace.Contracts, Contract{
Name: name,
Source: source,
})
}
newStep := Step{idx, start, end}
if len(t.trace.Steps) > 0 {
if t.trace.Steps[len(t.trace.Steps)-1] == newStep {
return
}
}
t.trace.Steps = append(t.trace.Steps, newStep)
}