aboutsummaryrefslogtreecommitdiffstats
path: root/ethvm/vm_test.go
blob: 40fb985a617340624fcccb1d932fb508c72a9b10 (plain) (blame)
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
package ethvm

import (
    "fmt"
    "io/ioutil"
    "log"
    "math/big"
    "testing"

    "github.com/ethereum/eth-go/ethlog"
    "github.com/ethereum/eth-go/ethstate"
    "github.com/ethereum/eth-go/ethutil"
)

type TestEnv struct {
}

func (self TestEnv) Origin() []byte         { return nil }
func (self TestEnv) BlockNumber() *big.Int  { return nil }
func (self TestEnv) BlockHash() []byte      { return nil }
func (self TestEnv) PrevHash() []byte       { return nil }
func (self TestEnv) Coinbase() []byte       { return nil }
func (self TestEnv) Time() int64            { return 0 }
func (self TestEnv) Difficulty() *big.Int   { return nil }
func (self TestEnv) Value() *big.Int        { return nil }
func (self TestEnv) State() *ethstate.State { return nil }

func setup(level int, typ Type) (*Closure, VirtualMachine) {
    // Pipe output to /dev/null
    ethlog.AddLogSystem(ethlog.NewStdLogSystem(ioutil.Discard, log.LstdFlags, ethlog.LogLevel(level)))

    ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")

    stateObject := ethstate.NewStateObject([]byte{'j', 'e', 'f', 'f'})
    callerClosure := NewClosure(nil, stateObject, stateObject, []byte{
        PUSH1, 1,
        PUSH1, 0,
        MSTORE,
        PUSH1, 32,
        PUSH1, 0,
        RETURN,
    }, big.NewInt(1000000), big.NewInt(0))

    return callerClosure, New(TestEnv{}, typ)
}

func TestDebugVm(t *testing.T) {
    closure, vm := setup(4, DebugVmTy)
    ret, _, e := closure.Call(vm, nil)
    if e != nil {
        fmt.Println("error", e)
    }

    if ret[len(ret)-1] != 1 {
        t.Errorf("Expected VM to return 1, got", ret, "instead.")
    }
}

func TestVm(t *testing.T) {
    closure, vm := setup(4, StandardVmTy)
    ret, _, e := closure.Call(vm, nil)
    if e != nil {
        fmt.Println("error", e)
    }

    if ret[len(ret)-1] != 1 {
        t.Errorf("Expected VM to return 1, got", ret, "instead.")
    }
}

func BenchmarkDebugVm(b *testing.B) {
    closure, vm := setup(3, DebugVmTy)

    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        closure.Call(vm, nil)
    }
}

func BenchmarkVm(b *testing.B) {
    closure, vm := setup(3, StandardVmTy)

    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        closure.Call(vm, nil)
    }
}