-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.go
168 lines (149 loc) · 4.51 KB
/
main.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
// Copyright 2019 Martin Holst Swende, Hubert Ritzdorf
// 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 main
import (
"encoding/json"
"fmt"
"math/big"
"os"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/vm/runtime"
"github.com/ethereum/go-ethereum/params"
common2 "github.com/holiman/goevmlab/common"
"github.com/holiman/goevmlab/ops"
"github.com/holiman/goevmlab/program"
"github.com/holiman/uint256"
)
func main() {
if err := program.RunProgram(runit); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
}
func runit() error {
a := program.NewProgram()
aAddr := common.HexToAddress("0xff0a")
bAddr := common.HexToAddress("0xff0b")
// Callling contract : call contract B, modify storage, revert
a.Call(nil, 0xff0b, 1, 0, 0, 0, 0)
// Call it again, send 1 wei to signal no revert
a.Call(nil, 0xff0b, 0, 0, 0, 0, 0)
aBytes := a.Bytecode()
fmt.Printf("A: %x\n", aBytes)
b := program.NewProgram()
b.Op(ops.CALLVALUE)
b.Op(ops.ISZERO)
b.Push(1) // placeholder
b.Op(ops.JUMPI)
// Path A
b.Sstore(0, 2)
b.Op(0xFE)
// Path B
label := b.Jumpdest()
fmt.Printf("label: %d\n", label)
b.Sstore(0, 1)
bBytes := b.Bytecode()
bBytes[3] = byte(label)
alloc := make(types.GenesisAlloc)
alloc[aAddr] = types.Account{
Nonce: 0,
Code: a.Bytecode(),
Balance: big.NewInt(0xffffffff),
}
alloc[bAddr] = types.Account{
Nonce: 0,
Code: bBytes,
Balance: big.NewInt(0),
}
//-------------
outp, err := json.MarshalIndent(alloc, "", " ")
if err != nil {
fmt.Printf("error : %v", err)
os.Exit(1)
}
fmt.Printf("output \n%v\n", string(outp))
//----------
var (
statedb, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
sender = common.BytesToAddress([]byte("sender"))
)
for addr, acc := range alloc {
statedb.CreateAccount(addr)
statedb.SetCode(addr, acc.Code)
statedb.SetNonce(addr, acc.Nonce)
if acc.Balance != nil {
statedb.SetBalance(addr, uint256.MustFromBig(acc.Balance), tracing.BalanceChangeUnspecified)
}
}
statedb.CreateAccount(sender)
runtimeConfig := runtime.Config{
Origin: sender,
State: statedb,
GasLimit: 10000000,
Difficulty: big.NewInt(0x200000),
BlockNumber: new(big.Int).SetUint64(1),
ChainConfig: ¶ms.ChainConfig{
ChainID: big.NewInt(1),
HomesteadBlock: new(big.Int),
ByzantiumBlock: new(big.Int),
ConstantinopleBlock: new(big.Int),
EIP150Block: new(big.Int),
EIP155Block: new(big.Int),
EIP158Block: new(big.Int),
PetersburgBlock: new(big.Int),
IstanbulBlock: new(big.Int),
},
EVMConfig: vm.Config{
Tracer: new(dumbTracer).Hooks(),
},
}
// Run with tracing
_, _, _ = runtime.Call(aAddr, nil, &runtimeConfig)
// Diagnose it
runtimeConfig.EVMConfig = vm.Config{}
t0 := time.Now()
_, _, err = runtime.Call(aAddr, nil, &runtimeConfig)
t1 := time.Since(t0)
fmt.Printf("Time elapsed: %v\n", t1)
return err
}
type dumbTracer struct {
common2.BasicTracer
counter uint64
}
func (d *dumbTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
if op == vm.STATICCALL {
d.counter++
}
if op == vm.EXTCODESIZE {
d.counter++
}
}
func (d *dumbTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
fmt.Printf("captureStart\n")
fmt.Printf(" from: %v\n", from.Hex())
fmt.Printf(" to: %v\n", to.Hex())
}
func (d *dumbTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {
fmt.Printf("\nCaptureEnd\n")
fmt.Printf("Counter: %d\n", d.counter)
}