-
Notifications
You must be signed in to change notification settings - Fork 42
/
nethermindbatcher.go
130 lines (121 loc) · 3.73 KB
/
nethermindbatcher.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
// Copyright 2022 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 evms
import (
"fmt"
"io"
"os/exec"
"sync"
"time"
"github.com/ethereum/go-ethereum/log"
)
// The NethermindBatchVM spins up one 'master' instance of the VM, and uses that to execute tests
type NethermindBatchVM struct {
NethermindVM
cmd *exec.Cmd // the 'master' process
procOut io.ReadCloser
stdin io.WriteCloser
mu sync.Mutex
}
func NewNethermindBatchVM(path, name string) *NethermindBatchVM {
return &NethermindBatchVM{
NethermindVM: *NewNethermindVM(path, name),
}
}
func (evm *NethermindBatchVM) Instance(threadId int) Evm {
return &NethermindBatchVM{
NethermindVM: NethermindVM{
path: evm.path,
name: fmt.Sprintf("%v-%d", evm.name, threadId),
stats: evm.stats,
},
}
}
// RunStateTest implements the Evm interface
func (evm *NethermindBatchVM) RunStateTest(path string, out io.Writer, speedTest bool) (*tracingResult, error) {
var (
t0 = time.Now()
err error
procOut io.ReadCloser
stdin io.WriteCloser
cmd = exec.Command(evm.path, "-x", "--trace", "-m")
)
if evm.cmd == nil {
if !speedTest {
// in normal execution, we read traces from standard error
if procOut, err = cmd.StderrPipe(); err != nil {
return &tracingResult{Cmd: cmd.String()}, err
}
} else {
// In speedtest-mode, we don't want the actual traces, but we do
// need to read the stateroot. The stateroot can be found on stdout
cmd = exec.Command(evm.path, "-x", "-m", "--neverTrace")
if procOut, err = cmd.StdoutPipe(); err != nil {
return &tracingResult{Cmd: cmd.String()}, err
}
}
if stdin, err = cmd.StdinPipe(); err != nil {
return &tracingResult{Cmd: cmd.String()}, err
}
if err = cmd.Start(); err != nil {
return &tracingResult{Cmd: cmd.String()}, err
}
evm.cmd = cmd
evm.procOut = procOut
evm.stdin = stdin
}
evm.mu.Lock()
defer evm.mu.Unlock()
_, err = evm.stdin.Write([]byte(fmt.Sprintf("%v\n", path)))
if err != nil {
log.Error("Error writing to nethermind", "err", err)
}
// copy everything for the _current_ statetest to the given writer
evm.copyUntilEnd(out, evm.procOut, speedTest)
duration, slow := evm.stats.TraceDone(t0)
return &tracingResult{
Slow: slow,
ExecTime: duration,
Cmd: evm.cmd.String(),
}, nil
}
func (vm *NethermindBatchVM) Close() {
if vm.stdin != nil {
vm.stdin.Close()
}
if vm.cmd != nil {
_ = vm.cmd.Wait()
}
}
func (evm *NethermindBatchVM) GetStateRoot(path string) (root, command string, err error) {
if evm.cmd == nil {
evm.cmd = exec.Command(evm.path, "--neverTrace", "-m", "-s", "-x")
if evm.procOut, err = evm.cmd.StdoutPipe(); err != nil {
return "", evm.cmd.String(), err
}
if evm.stdin, err = evm.cmd.StdinPipe(); err != nil {
return "", evm.cmd.String(), err
}
if err = evm.cmd.Start(); err != nil {
return "", evm.cmd.String(), err
}
}
evm.mu.Lock()
defer evm.mu.Unlock()
_, _ = evm.stdin.Write([]byte(fmt.Sprintf("%v\n", path)))
sRoot := evm.copyUntilEnd(io.Discard, evm.procOut, true)
return sRoot.StateRoot, evm.cmd.String(), nil
}