2014-10-18 11:31:20 +00:00
|
|
|
package vm
|
2014-07-22 09:54:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-10-14 11:37:26 +00:00
|
|
|
"bytes"
|
2014-07-22 09:54:48 +00:00
|
|
|
"fmt"
|
2014-10-14 09:48:52 +00:00
|
|
|
"io/ioutil"
|
2014-07-22 09:54:48 +00:00
|
|
|
"log"
|
|
|
|
"math/big"
|
2014-10-14 11:37:26 +00:00
|
|
|
"os"
|
2014-07-22 09:54:48 +00:00
|
|
|
"testing"
|
2014-08-04 08:38:18 +00:00
|
|
|
|
2014-10-23 13:01:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethcrypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethlog"
|
|
|
|
"github.com/ethereum/go-ethereum/ethstate"
|
|
|
|
"github.com/ethereum/go-ethereum/ethtrie"
|
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-07-22 09:54:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestEnv struct {
|
|
|
|
}
|
|
|
|
|
2014-10-14 11:37:26 +00:00
|
|
|
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 }
|
|
|
|
|
|
|
|
// This is likely to fail if anything ever gets looked up in the state trie :-)
|
|
|
|
func (self TestEnv) State() *ethstate.State { return ethstate.New(ethtrie.New(nil, "")) }
|
2014-07-22 09:54:48 +00:00
|
|
|
|
2014-10-14 10:21:46 +00:00
|
|
|
const mutcode = `
|
|
|
|
var x = 0;
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
x = i
|
|
|
|
}
|
|
|
|
|
|
|
|
return x`
|
|
|
|
|
2014-10-16 08:47:12 +00:00
|
|
|
func setup(level ethlog.LogLevel, typ Type) (*Closure, VirtualMachine) {
|
2014-10-14 10:21:46 +00:00
|
|
|
code, err := ethutil.Compile(mutcode, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-14 09:48:52 +00:00
|
|
|
// Pipe output to /dev/null
|
2014-10-16 08:47:12 +00:00
|
|
|
ethlog.AddLogSystem(ethlog.NewStdLogSystem(ioutil.Discard, log.LstdFlags, 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 10:21:46 +00:00
|
|
|
callerClosure := NewClosure(nil, stateObject, stateObject, code, 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) {
|
2014-10-16 08:47:12 +00:00
|
|
|
closure, vm := setup(ethlog.DebugLevel, DebugVmTy)
|
2014-10-14 09:48:52 +00:00
|
|
|
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
|
|
|
|
2014-10-14 10:21:46 +00:00
|
|
|
if ret[len(ret)-1] != 9 {
|
2014-10-14 13:31:25 +00:00
|
|
|
t.Errorf("Expected VM to return 9, got", ret, "instead.")
|
2014-10-14 09:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVm(t *testing.T) {
|
2014-10-16 08:47:12 +00:00
|
|
|
closure, vm := setup(ethlog.DebugLevel, StandardVmTy)
|
2014-10-14 09:48:52 +00:00
|
|
|
ret, _, e := closure.Call(vm, nil)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println("error", e)
|
|
|
|
}
|
|
|
|
|
2014-10-14 10:21:46 +00:00
|
|
|
if ret[len(ret)-1] != 9 {
|
2014-10-14 13:31:25 +00:00
|
|
|
t.Errorf("Expected VM to return 9, got", ret, "instead.")
|
2014-10-14 09:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDebugVm(b *testing.B) {
|
2014-10-16 08:47:12 +00:00
|
|
|
closure, vm := setup(ethlog.InfoLevel, DebugVmTy)
|
2014-10-14 09:48:52 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
closure.Call(vm, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkVm(b *testing.B) {
|
2014-10-16 08:47:12 +00:00
|
|
|
closure, vm := setup(ethlog.InfoLevel, StandardVmTy)
|
2014-10-14 09:48:52 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
closure.Call(vm, nil)
|
|
|
|
}
|
2014-07-22 09:54:48 +00:00
|
|
|
}
|
2014-10-14 11:37:26 +00:00
|
|
|
|
|
|
|
func RunCode(mutCode string, typ Type) []byte {
|
|
|
|
code, err := ethutil.Compile(mutCode, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-16 08:47:12 +00:00
|
|
|
ethlog.AddLogSystem(ethlog.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlog.InfoLevel))
|
2014-10-14 11:37:26 +00:00
|
|
|
|
|
|
|
ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "")
|
|
|
|
|
|
|
|
stateObject := ethstate.NewStateObject([]byte{'j', 'e', 'f', 'f'})
|
|
|
|
closure := NewClosure(nil, stateObject, stateObject, code, big.NewInt(1000000), big.NewInt(0))
|
|
|
|
|
|
|
|
vm := New(TestEnv{}, typ)
|
|
|
|
ret, _, e := closure.Call(vm, nil)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildInSha256(t *testing.T) {
|
|
|
|
ret := RunCode(`
|
|
|
|
var in = 42
|
|
|
|
var out = 0
|
|
|
|
|
|
|
|
call(0x2, 0, 10000, in, out)
|
|
|
|
|
|
|
|
return out
|
|
|
|
`, DebugVmTy)
|
|
|
|
|
|
|
|
exp := ethcrypto.Sha256(ethutil.LeftPadBytes([]byte{42}, 32))
|
|
|
|
if bytes.Compare(ret, exp) != 0 {
|
|
|
|
t.Errorf("Expected %x, got %x", exp, ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildInRipemd(t *testing.T) {
|
|
|
|
ret := RunCode(`
|
|
|
|
var in = 42
|
|
|
|
var out = 0
|
|
|
|
|
|
|
|
call(0x3, 0, 10000, in, out)
|
|
|
|
|
|
|
|
return out
|
|
|
|
`, DebugVmTy)
|
|
|
|
|
|
|
|
exp := ethutil.RightPadBytes(ethcrypto.Ripemd160(ethutil.LeftPadBytes([]byte{42}, 32)), 32)
|
|
|
|
if bytes.Compare(ret, exp) != 0 {
|
|
|
|
t.Errorf("Expected %x, got %x", exp, ret)
|
|
|
|
}
|
|
|
|
}
|