2014-07-22 09:54:48 +00:00
|
|
|
package ethvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-14 09:48:52 +00:00
|
|
|
"io/ioutil"
|
2014-07-22 09:54:48 +00:00
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
2014-08-04 08:38:18 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/eth-go/ethlog"
|
|
|
|
"github.com/ethereum/eth-go/ethstate"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
2014-07-22 09:54:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestEnv struct {
|
|
|
|
}
|
|
|
|
|
2014-07-22 13:57:54 +00:00
|
|
|
func (self TestEnv) Origin() []byte { return nil }
|
|
|
|
func (self TestEnv) BlockNumber() *big.Int { return nil }
|
2014-10-14 09:48:52 +00:00
|
|
|
func (self TestEnv) BlockHash() []byte { return nil }
|
2014-07-22 13:57:54 +00:00
|
|
|
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 }
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-10-14 09:48:52 +00:00
|
|
|
func setup(level int, typ Type) (*Closure, VirtualMachine) {
|
|
|
|
// Pipe output to /dev/null
|
|
|
|
ethlog.AddLogSystem(ethlog.NewStdLogSystem(ioutil.Discard, log.LstdFlags, ethlog.LogLevel(level)))
|
2014-07-22 09:54:48 +00:00
|
|
|
|
|
|
|
ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
|
|
|
|
|
|
|
|
stateObject := ethstate.NewStateObject([]byte{'j', 'e', 'f', 'f'})
|
2014-10-14 09:48:52 +00:00
|
|
|
callerClosure := NewClosure(nil, stateObject, stateObject, []byte{
|
|
|
|
PUSH1, 1,
|
|
|
|
PUSH1, 0,
|
|
|
|
MSTORE,
|
|
|
|
PUSH1, 32,
|
|
|
|
PUSH1, 0,
|
|
|
|
RETURN,
|
|
|
|
}, big.NewInt(1000000), big.NewInt(0))
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-10-14 09:48:52 +00:00
|
|
|
return callerClosure, New(TestEnv{}, typ)
|
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-10-14 09:48:52 +00:00
|
|
|
func TestDebugVm(t *testing.T) {
|
|
|
|
closure, vm := setup(4, DebugVmTy)
|
|
|
|
ret, _, e := closure.Call(vm, nil)
|
2014-07-22 09:54:48 +00:00
|
|
|
if e != nil {
|
|
|
|
fmt.Println("error", e)
|
|
|
|
}
|
2014-10-14 09:48:52 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
}
|