Skip to content

Commit 7b9fbb0

Browse files
committed
Flatten vm directory
1 parent 6415ed0 commit 7b9fbb0

4 files changed

Lines changed: 387 additions & 3 deletions

File tree

File renamed without changes.

‎tests/vm/.ethtest‎

Whitespace-only changes.

‎tests/vm/nowarn.go‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tests/vm_test.go‎

Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
package vm
2+
3+
import (
4+
"bytes"
5+
"math/big"
6+
"os"
7+
"path/filepath"
8+
"strconv"
9+
"testing"
10+
11+
"github.com/ethereum/go-ethereum/common"
12+
"github.com/ethereum/go-ethereum/core/state"
13+
"github.com/ethereum/go-ethereum/core/types"
14+
"github.com/ethereum/go-ethereum/ethdb"
15+
"github.com/ethereum/go-ethereum/logger"
16+
"github.com/ethereum/go-ethereum/tests/helper"
17+
)
18+
19+
type Account struct {
20+
Balance string
21+
Code string
22+
Nonce string
23+
Storage map[string]string
24+
}
25+
26+
type Log struct {
27+
AddressF string `json:"address"`
28+
DataF string `json:"data"`
29+
TopicsF []string `json:"topics"`
30+
BloomF string `json:"bloom"`
31+
}
32+
33+
func (self Log) Address() []byte { return common.Hex2Bytes(self.AddressF) }
34+
func (self Log) Data() []byte { return common.Hex2Bytes(self.DataF) }
35+
func (self Log) RlpData() interface{} { return nil }
36+
func (self Log) Topics() [][]byte {
37+
t := make([][]byte, len(self.TopicsF))
38+
for i, topic := range self.TopicsF {
39+
t[i] = common.Hex2Bytes(topic)
40+
}
41+
return t
42+
}
43+
44+
func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject {
45+
obj := state.NewStateObject(common.HexToAddress(addr), db)
46+
obj.SetBalance(common.Big(account.Balance))
47+
48+
if common.IsHex(account.Code) {
49+
account.Code = account.Code[2:]
50+
}
51+
obj.SetCode(common.Hex2Bytes(account.Code))
52+
obj.SetNonce(common.Big(account.Nonce).Uint64())
53+
54+
return obj
55+
}
56+
57+
type Env struct {
58+
CurrentCoinbase string
59+
CurrentDifficulty string
60+
CurrentGasLimit string
61+
CurrentNumber string
62+
CurrentTimestamp interface{}
63+
PreviousHash string
64+
}
65+
66+
type VmTest struct {
67+
Callcreates interface{}
68+
//Env map[string]string
69+
Env Env
70+
Exec map[string]string
71+
Transaction map[string]string
72+
Logs []Log
73+
Gas string
74+
Out string
75+
Post map[string]Account
76+
Pre map[string]Account
77+
PostStateRoot string
78+
}
79+
80+
func RunVmTest(p string, t *testing.T) {
81+
82+
tests := make(map[string]VmTest)
83+
helper.CreateFileTests(t, p, &tests)
84+
85+
for name, test := range tests {
86+
/*
87+
vm.Debug = true
88+
glog.SetV(4)
89+
glog.SetToStderr(true)
90+
if name != "Call50000_sha256" {
91+
continue
92+
}
93+
*/
94+
db, _ := ethdb.NewMemDatabase()
95+
statedb := state.New(common.Hash{}, db)
96+
for addr, account := range test.Pre {
97+
obj := StateObjectFromAccount(db, addr, account)
98+
statedb.SetStateObject(obj)
99+
for a, v := range account.Storage {
100+
obj.SetState(common.HexToHash(a), common.NewValue(helper.FromHex(v)))
101+
}
102+
}
103+
104+
// XXX Yeah, yeah...
105+
env := make(map[string]string)
106+
env["currentCoinbase"] = test.Env.CurrentCoinbase
107+
env["currentDifficulty"] = test.Env.CurrentDifficulty
108+
env["currentGasLimit"] = test.Env.CurrentGasLimit
109+
env["currentNumber"] = test.Env.CurrentNumber
110+
env["previousHash"] = test.Env.PreviousHash
111+
if n, ok := test.Env.CurrentTimestamp.(float64); ok {
112+
env["currentTimestamp"] = strconv.Itoa(int(n))
113+
} else {
114+
env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
115+
}
116+
117+
var (
118+
ret []byte
119+
gas *big.Int
120+
err error
121+
logs state.Logs
122+
)
123+
124+
isVmTest := len(test.Exec) > 0
125+
if isVmTest {
126+
ret, logs, gas, err = helper.RunVm(statedb, env, test.Exec)
127+
} else {
128+
ret, logs, gas, err = helper.RunState(statedb, env, test.Transaction)
129+
}
130+
131+
switch name {
132+
// the memory required for these tests (4294967297 bytes) would take too much time.
133+
// on 19 May 2015 decided to skip these tests their output.
134+
case "mload32bitBound_return", "mload32bitBound_return2":
135+
default:
136+
rexp := helper.FromHex(test.Out)
137+
if bytes.Compare(rexp, ret) != 0 {
138+
t.Errorf("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
139+
}
140+
}
141+
142+
if isVmTest {
143+
if len(test.Gas) == 0 && err == nil {
144+
t.Errorf("%s's gas unspecified, indicating an error. VM returned (incorrectly) successfull", name)
145+
} else {
146+
gexp := common.Big(test.Gas)
147+
if gexp.Cmp(gas) != 0 {
148+
t.Errorf("%s's gas failed. Expected %v, got %v\n", name, gexp, gas)
149+
}
150+
}
151+
}
152+
153+
for addr, account := range test.Post {
154+
obj := statedb.GetStateObject(common.HexToAddress(addr))
155+
if obj == nil {
156+
continue
157+
}
158+
159+
if len(test.Exec) == 0 {
160+
if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
161+
t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance()))
162+
}
163+
164+
if obj.Nonce() != common.String2Big(account.Nonce).Uint64() {
165+
t.Errorf("%s's : (%x) nonce failed. Expected %v, got %v\n", name, obj.Address().Bytes()[:4], account.Nonce, obj.Nonce())
166+
}
167+
168+
}
169+
170+
for addr, value := range account.Storage {
171+
v := obj.GetState(common.HexToHash(addr)).Bytes()
172+
vexp := helper.FromHex(value)
173+
174+
if bytes.Compare(v, vexp) != 0 {
175+
t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
176+
}
177+
}
178+
}
179+
180+
if !isVmTest {
181+
statedb.Sync()
182+
//if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
183+
if common.HexToHash(test.PostStateRoot) != statedb.Root() {
184+
t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
185+
}
186+
}
187+
188+
if len(test.Logs) > 0 {
189+
if len(test.Logs) != len(logs) {
190+
t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
191+
} else {
192+
for i, log := range test.Logs {
193+
if common.HexToAddress(log.AddressF) != logs[i].Address {
194+
t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address)
195+
}
196+
197+
if !bytes.Equal(logs[i].Data, helper.FromHex(log.DataF)) {
198+
t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data)
199+
}
200+
201+
if len(log.TopicsF) != len(logs[i].Topics) {
202+
t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics)
203+
} else {
204+
for j, topic := range log.TopicsF {
205+
if common.HexToHash(topic) != logs[i].Topics[j] {
206+
t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics[j])
207+
}
208+
}
209+
}
210+
genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
211+
212+
if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
213+
t.Errorf("'%s' bloom mismatch", name)
214+
}
215+
}
216+
}
217+
}
218+
//fmt.Println(string(statedb.Dump()))
219+
}
220+
logger.Flush()
221+
}
222+
223+
// I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
224+
func TestVMArithmetic(t *testing.T) {
225+
const fn = "../files/VMTests/vmArithmeticTest.json"
226+
RunVmTest(fn, t)
227+
}
228+
229+
func TestBitwiseLogicOperation(t *testing.T) {
230+
const fn = "../files/VMTests/vmBitwiseLogicOperationTest.json"
231+
RunVmTest(fn, t)
232+
}
233+
234+
func TestBlockInfo(t *testing.T) {
235+
const fn = "../files/VMTests/vmBlockInfoTest.json"
236+
RunVmTest(fn, t)
237+
}
238+
239+
func TestEnvironmentalInfo(t *testing.T) {
240+
const fn = "../files/VMTests/vmEnvironmentalInfoTest.json"
241+
RunVmTest(fn, t)
242+
}
243+
244+
func TestFlowOperation(t *testing.T) {
245+
const fn = "../files/VMTests/vmIOandFlowOperationsTest.json"
246+
RunVmTest(fn, t)
247+
}
248+
249+
func TestLogTest(t *testing.T) {
250+
const fn = "../files/VMTests/vmLogTest.json"
251+
RunVmTest(fn, t)
252+
}
253+
254+
func TestPerformance(t *testing.T) {
255+
const fn = "../files/VMTests/vmPerformanceTest.json"
256+
RunVmTest(fn, t)
257+
}
258+
259+
func TestPushDupSwap(t *testing.T) {
260+
const fn = "../files/VMTests/vmPushDupSwapTest.json"
261+
RunVmTest(fn, t)
262+
}
263+
264+
func TestVMSha3(t *testing.T) {
265+
const fn = "../files/VMTests/vmSha3Test.json"
266+
RunVmTest(fn, t)
267+
}
268+
269+
func TestVm(t *testing.T) {
270+
const fn = "../files/VMTests/vmtests.json"
271+
RunVmTest(fn, t)
272+
}
273+
274+
func TestVmLog(t *testing.T) {
275+
const fn = "../files/VMTests/vmLogTest.json"
276+
RunVmTest(fn, t)
277+
}
278+
279+
func TestInputLimits(t *testing.T) {
280+
const fn = "../files/VMTests/vmInputLimits.json"
281+
RunVmTest(fn, t)
282+
}
283+
284+
func TestInputLimitsLight(t *testing.T) {
285+
const fn = "../files/VMTests/vmInputLimitsLight.json"
286+
RunVmTest(fn, t)
287+
}
288+
289+
func TestStateSystemOperations(t *testing.T) {
290+
const fn = "../files/StateTests/stSystemOperationsTest.json"
291+
RunVmTest(fn, t)
292+
}
293+
294+
func TestStateExample(t *testing.T) {
295+
const fn = "../files/StateTests/stExample.json"
296+
RunVmTest(fn, t)
297+
}
298+
299+
func TestStatePreCompiledContracts(t *testing.T) {
300+
const fn = "../files/StateTests/stPreCompiledContracts.json"
301+
RunVmTest(fn, t)
302+
}
303+
304+
func TestStateRecursiveCreate(t *testing.T) {
305+
const fn = "../files/StateTests/stRecursiveCreate.json"
306+
RunVmTest(fn, t)
307+
}
308+
309+
func TestStateSpecial(t *testing.T) {
310+
const fn = "../files/StateTests/stSpecialTest.json"
311+
RunVmTest(fn, t)
312+
}
313+
314+
func TestStateRefund(t *testing.T) {
315+
const fn = "../files/StateTests/stRefundTest.json"
316+
RunVmTest(fn, t)
317+
}
318+
319+
func TestStateBlockHash(t *testing.T) {
320+
const fn = "../files/StateTests/stBlockHashTest.json"
321+
RunVmTest(fn, t)
322+
}
323+
324+
func TestStateInitCode(t *testing.T) {
325+
const fn = "../files/StateTests/stInitCodeTest.json"
326+
RunVmTest(fn, t)
327+
}
328+
329+
func TestStateLog(t *testing.T) {
330+
const fn = "../files/StateTests/stLogTests.json"
331+
RunVmTest(fn, t)
332+
}
333+
334+
func TestStateTransaction(t *testing.T) {
335+
const fn = "../files/StateTests/stTransactionTest.json"
336+
RunVmTest(fn, t)
337+
}
338+
339+
func TestCallCreateCallCode(t *testing.T) {
340+
const fn = "../files/StateTests/stCallCreateCallCodeTest.json"
341+
RunVmTest(fn, t)
342+
}
343+
344+
func TestMemory(t *testing.T) {
345+
const fn = "../files/StateTests/stMemoryTest.json"
346+
RunVmTest(fn, t)
347+
}
348+
349+
func TestMemoryStress(t *testing.T) {
350+
if os.Getenv("TEST_VM_COMPLEX") == "" {
351+
t.Skip()
352+
}
353+
const fn = "../files/StateTests/stMemoryStressTest.json"
354+
RunVmTest(fn, t)
355+
}
356+
357+
func TestQuadraticComplexity(t *testing.T) {
358+
if os.Getenv("TEST_VM_COMPLEX") == "" {
359+
t.Skip()
360+
}
361+
const fn = "../files/StateTests/stQuadraticComplexityTest.json"
362+
RunVmTest(fn, t)
363+
}
364+
365+
func TestSolidity(t *testing.T) {
366+
const fn = "../files/StateTests/stSolidityTest.json"
367+
RunVmTest(fn, t)
368+
}
369+
370+
func TestWallet(t *testing.T) {
371+
const fn = "../files/StateTests/stWalletTest.json"
372+
RunVmTest(fn, t)
373+
}
374+
375+
func TestStateTestsRandom(t *testing.T) {
376+
fns, _ := filepath.Glob("../files/StateTests/RandomTests/*")
377+
for _, fn := range fns {
378+
RunVmTest(fn, t)
379+
}
380+
}
381+
382+
func TestVMRandom(t *testing.T) {
383+
fns, _ := filepath.Glob("../files/VMTests/RandomTests/*")
384+
for _, fn := range fns {
385+
RunVmTest(fn, t)
386+
}
387+
}

0 commit comments

Comments
 (0)