forked from ethereum/go-ethereum
/
tracers_parallel.go
153 lines (134 loc) · 3.75 KB
/
tracers_parallel.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
package trie
import (
"github.com/arcology-network/evm/trie/trienode"
)
type AccessListCache struct {
tx uint32
keys [][]byte
data [][]byte
}
func NewAccessListCaches(num int) []*AccessListCache {
list := make([]*AccessListCache, num)
for i := 0; i < len(list); i++ {
list[i] = &AccessListCache{
keys: [][]byte{},
data: [][]byte{},
}
}
return list
}
func (this *AccessListCache) Add(key []byte, val []byte) {
this.keys = append(this.keys, key)
this.data = append(this.data, val)
}
func (this *AccessListCache) Merge(accesses ...*AccessListCache) {
for _, v := range accesses {
this.keys = append(this.keys, v.keys...)
this.data = append(this.data, v.data...)
}
}
func (this *AccessListCache) ToMap() map[string][]byte {
hashmap := map[string][]byte{}
for i, k := range this.keys {
hashmap[string(k)] = this.data[i]
}
return hashmap
}
func (this *AccessListCache) Unique() ([]string, [][]byte) {
hashmap := this.ToMap()
keys, values := make([]string, 0, len(hashmap)), make([][]byte, 0, len(hashmap))
for k, v := range hashmap {
keys = append(keys, k)
values = append(values, v)
}
return keys, values
}
type parallelTracer struct {
tracers [17]*tracer
}
// newTracer initializes the parallelTracer for capturing trie changes.
func newParaTracer() tracerInterface {
paraTracer := ¶llelTracer{}
for i := 0; i < len(paraTracer.tracers); i++ {
paraTracer.tracers[i] = newTracer()
}
return paraTracer
}
// onRead tracks the newly loaded trie node and caches the rlp-encoded
// blob internally. Don't change the value outside of function since
// it's not deep-copied.
func (t *parallelTracer) onRead(path []byte, val []byte) {
if len(path) > 0 {
t.tracers[(path[0])].onRead(path, val)
return
}
t.tracers[16].onRead(path, val)
}
// onInsert tracks the newly inserted trie node. If it's already
// in the deletion set (resurrected node), then just wipe it from
// the deletion set as it's "untouched".
func (t *parallelTracer) onInsert(path []byte) {
if len(path) > 0 {
t.tracers[path[0]].onInsert(path)
return
}
t.tracers[16].onInsert(path)
}
// onDelete tracks the newly deleted trie node. If it's already
// in the addition set, then just wipe it from the addition set
// as it's untouched.
func (t *parallelTracer) onDelete(path []byte) {
if len(path) > 0 {
t.tracers[path[0]].onDelete(path)
return
}
t.tracers[16].onDelete(path)
}
// reset clears the content tracked by parallelTracer.
func (t *parallelTracer) reset() {
for i := 0; i < len(t.tracers); i++ {
t.tracers[i].reset()
}
}
// copy returns a deep copied parallelTracer instance.
func (t *parallelTracer) copy() tracerInterface {
paraTracer := newParaTracer().(*parallelTracer) //.(*parallelTracer)
for i := 0; i < len(t.tracers); i++ {
paraTracer.tracers[i] = t.tracers[i].copy().(*tracer)
}
return paraTracer
}
// markDeletions puts all tracked deletions into the provided nodeset.
func (t *parallelTracer) markDeletions(set *trienode.NodeSet) {
for i := 0; i < len(t.tracers); i++ {
t.tracers[i].markDeletions(set)
}
}
// markDeletions puts all tracked deletions into the provided nodeset.
func (t *parallelTracer) getAccessList() map[string][]byte {
accessList := map[string][]byte{}
// for i := 0; i < len(t.tracers); i++ {
// for k, v := range t.tracers[i].accessList {
// accessList[k] = v
// }
// }
return accessList
}
func (t *parallelTracer) getDeletes() map[string]struct{} {
deletes := map[string]struct{}{}
for i := 0; i < len(t.tracers); i++ {
for k, v := range t.tracers[i].deletes {
deletes[k] = v
}
}
return deletes
}
func (t *parallelTracer) getInserts() map[string]struct{} {
inserts := map[string]struct{}{}
for i := 0; i < len(t.tracers); i++ {
for k, v := range t.tracers[i].inserts {
inserts[k] = v
}
}
return inserts
}