forked from Fantom-foundation/substate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstate_task.go
More file actions
271 lines (224 loc) · 6.87 KB
/
Copy pathsubstate_task.go
File metadata and controls
271 lines (224 loc) · 6.87 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package substate
import (
"fmt"
"runtime"
"sort"
"sync"
"sync/atomic"
"time"
"github.com/urfave/cli/v2"
)
var (
WorkersFlag = cli.IntFlag{
Name: "workers",
Usage: "Number of worker threads that execute in parallel",
Value: 4,
}
SkipTransferTxsFlag = cli.BoolFlag{
Name: "skip-transfer-txs",
Usage: "Skip executing transactions that only transfer ETH",
}
SkipCallTxsFlag = cli.BoolFlag{
Name: "skip-call-txs",
Usage: "Skip executing CALL transactions to accounts with contract bytecode",
}
SkipCreateTxsFlag = cli.BoolFlag{
Name: "skip-create-txs",
Usage: "Skip executing CREATE transactions",
}
)
type SubstateBlockFunc func(block uint64, transactions map[int]*Substate, taskPool *SubstateTaskPool) error
type SubstateTaskFunc func(block uint64, tx int, substate *Substate, taskPool *SubstateTaskPool) error
type SubstateTaskPool struct {
Name string
BlockFunc SubstateBlockFunc
TaskFunc SubstateTaskFunc
First uint64
Last uint64
Workers int
SkipTransferTxs bool
SkipCallTxs bool
SkipCreateTxs bool
Ctx *cli.Context // CLI context required to read additional flags
DB *DB
}
func NewSubstateTaskPool(name string, taskFunc SubstateTaskFunc, first, last uint64, ctx *cli.Context) *SubstateTaskPool {
return &SubstateTaskPool{
Name: name,
TaskFunc: taskFunc,
First: first,
Last: last,
Workers: ctx.Int(WorkersFlag.Name),
SkipTransferTxs: ctx.Bool(SkipTransferTxsFlag.Name),
SkipCallTxs: ctx.Bool(SkipCallTxsFlag.Name),
SkipCreateTxs: ctx.Bool(SkipCreateTxsFlag.Name),
Ctx: ctx,
DB: staticSubstateDB,
}
}
// ExecuteBlock function iterates on substates of a given block call TaskFunc
func (pool *SubstateTaskPool) ExecuteBlock(block uint64) (numTx int64, gas int64, err error) {
transactions := pool.DB.GetBlockSubstates(block)
if pool.BlockFunc != nil {
err := pool.BlockFunc(block, transactions, pool)
if err != nil {
return numTx, gas, fmt.Errorf("%s: block %v: %v", pool.Name, block, err)
}
}
if pool.TaskFunc == nil {
return int64(len(transactions)), 0, nil
}
// Fix the order in which transactions are processed in a block
// such that in cases where only a single worker is processing
// transactions the execution order is deterministic.
txNumbers := make([]int, 0, len(transactions))
for tx := range transactions {
txNumbers = append(txNumbers, tx)
}
sort.Slice(txNumbers, func(i, j int) bool { return txNumbers[i] < txNumbers[j] })
for _, tx := range txNumbers {
substate := transactions[tx]
alloc := substate.InputAlloc
msg := substate.Message
to := msg.To
if pool.SkipTransferTxs && to != nil {
// skip regular transactions (ETH transfer)
if account, exist := alloc[*to]; !exist || len(account.Code) == 0 {
continue
}
}
if pool.SkipCallTxs && to != nil {
// skip CALL trasnactions with contract bytecode
if account, exist := alloc[*to]; exist && len(account.Code) > 0 {
continue
}
}
if pool.SkipCreateTxs && to == nil {
// skip CREATE transactions
continue
}
err = pool.TaskFunc(block, tx, substate, pool)
if err != nil {
return numTx, gas, fmt.Errorf("%s: %v_%v: %v", pool.Name, block, tx, err)
}
numTx++
gas += int64(substate.Result.GasUsed)
}
return numTx, gas, nil
}
// Execute function spawns worker goroutines and schedule tasks.
func (pool *SubstateTaskPool) Execute() error {
start := time.Now()
var totalNumBlock, totalNumTx, totalGas atomic.Int64
defer func() {
duration := time.Since(start) + 1*time.Nanosecond
sec := duration.Seconds()
nb, nt, ng := totalNumBlock.Load(), totalNumTx.Load(), totalGas.Load()
blkPerSec := float64(nb) / sec
txPerSec := float64(nt) / sec
gasPerSec := float64(ng) / sec
fmt.Printf("%s: block range = %v %v\n", pool.Name, pool.First, pool.Last)
fmt.Printf("%s: total #block = %v\n", pool.Name, nb)
fmt.Printf("%s: total #tx = %v\n", pool.Name, nt)
fmt.Printf("%s: %.2f blk/s, %.2f tx/s, %.2f Mgas/s\n", pool.Name, blkPerSec, txPerSec, gasPerSec/1e6)
fmt.Printf("%s done in %v\n", pool.Name, duration.Round(1*time.Millisecond))
}()
// numProcs = numWorker + work producer (1) + main thread (1)
numProcs := pool.Workers + 2
if goMaxProcs := runtime.GOMAXPROCS(0); goMaxProcs < numProcs {
runtime.GOMAXPROCS(numProcs)
}
fmt.Printf("%s: block range = %v %v\n", pool.Name, pool.First, pool.Last)
fmt.Printf("%s: #CPU = %v, #worker = %v\n", pool.Name, runtime.NumCPU(), pool.Workers)
workChan := make(chan uint64, pool.Workers*10)
doneChan := make(chan interface{}, pool.Workers*10)
stopChan := make(chan struct{}, pool.Workers)
wg := sync.WaitGroup{}
defer func() {
// stop all workers
for i := 0; i < pool.Workers; i++ {
stopChan <- struct{}{}
}
// stop work producer (1)
stopChan <- struct{}{}
wg.Wait()
close(workChan)
close(doneChan)
}()
// dynamically schedule one block per worker
for i := 0; i < pool.Workers; i++ {
wg.Add(1)
// worker goroutine
go func() {
defer wg.Done()
for {
select {
case block := <-workChan:
nt, ng, err := pool.ExecuteBlock(block)
totalGas.Add(ng)
totalNumTx.Add(nt)
totalNumBlock.Add(1)
if err != nil {
doneChan <- err
} else {
doneChan <- block
}
case <-stopChan:
return
}
}
}()
}
// wait until all workers finish all tasks
wg.Add(1)
go func() {
defer wg.Done()
for block := pool.First; block <= pool.Last; block++ {
select {
case workChan <- block:
continue
case <-stopChan:
return
}
}
}()
// Count finished blocks in order and report execution speed
var lastSec float64
var lastNumBlock, lastNumTx, lastGas int64
waitMap := make(map[uint64]struct{})
for block := pool.First; block <= pool.Last; {
// Count finshed blocks from waitMap in order
if _, ok := waitMap[block]; ok {
delete(waitMap, block)
block++
continue
}
duration := time.Since(start) + 1*time.Nanosecond
sec := duration.Seconds()
if block == pool.Last ||
(block%10000 == 0 && sec > lastSec+5) ||
(block%1000 == 0 && sec > lastSec+10) ||
(block%100 == 0 && sec > lastSec+20) ||
(block%10 == 0 && sec > lastSec+40) ||
(sec > lastSec+60) {
nb, nt, ng := totalNumBlock.Load(), totalNumTx.Load(), totalGas.Load()
blkPerSec := float64(nb-lastNumBlock) / (sec - lastSec)
txPerSec := float64(nt-lastNumTx) / (sec - lastSec)
gasPerSec := float64(ng-lastGas) / (sec - lastSec)
fmt.Printf("%s: elapsed time: %v, number = %v\n", pool.Name, duration.Round(1*time.Millisecond), block)
fmt.Printf("%s: %.2f blk/s, %.2f tx/s, %.2f Mgas/s\n", pool.Name, blkPerSec, txPerSec, gasPerSec/1e6)
lastSec, lastNumBlock, lastNumTx, lastGas = sec, nb, nt, ng
}
data := <-doneChan
switch t := data.(type) {
case uint64:
waitMap[data.(uint64)] = struct{}{}
case error:
err := data.(error)
return err
default:
panic(fmt.Errorf("%s: unknown type %T value from doneChan", pool.Name, t))
}
}
return nil
}