-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
160 lines (143 loc) · 4.62 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
// 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"
"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")
// Calling contract: Call contract B in a loop
a.Op(ops.PC) // Push 0
a.Op(ops.DUP1) // outsize = 0, on next iteration we use the return value of CALL
dest := a.Jumpdest() // Loop Head
a.Op(ops.DUP2) // outoffset = 0
a.Push(1305700) // insize = 1305700
a.Op(ops.DUP2) // inoffset = 0
a.Push(0xdeadbeef) // Push target address, alternatively we could call an empty contract here
a.Op(ops.GAS) // Pass along all gas
a.Op(ops.STATICCALL)
a.Jump(dest) // Jump back
alloc := make(types.GenesisAlloc)
alloc[aAddr] = types.Account{
Nonce: 0,
Code: a.Bytecode(),
Balance: big.NewInt(0xffffffff),
}
//-------------
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 {
counter uint64
}
func (d *dumbTracer) Hooks() *tracing.Hooks {
return &tracing.Hooks{
OnTxStart: d.CaptureStart,
OnOpcode: d.CaptureState,
OnFault: d.CaptureFault,
OnTxEnd: d.CaptureEnd,
}
}
func (d *dumbTracer) CaptureState(pc uint64, op byte, gas uint64, cost uint64, scope tracing.OpContext, input []byte, depth int, err error) {
if op == byte(vm.STATICCALL) {
d.counter++
}
if op == byte(vm.EXTCODESIZE) {
d.counter++
}
}
func (n *dumbTracer) CaptureFault(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, depth int, err error) {
fmt.Printf("CaptureFault %v\n", err)
}
func (n *dumbTracer) CaptureStart(vm *tracing.VMContext, tx *types.Transaction, from common.Address) {
fmt.Printf("captureStart\n")
fmt.Printf(" from: %v\n", from.Hex())
fmt.Printf(" to: %v\n", tx.To().Hex())
}
func (d *dumbTracer) CaptureEnd(receipt *types.Receipt, err error) {
fmt.Printf("\nCaptureEnd\n")
fmt.Printf("Counter: %d\n", d.counter)
}