|
|
@ -1,14 +1,18 @@ |
|
|
|
package ethvm |
|
|
|
package ethvm |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"bytes" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
|
|
|
|
"os" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/eth-go/ethcrypto" |
|
|
|
"github.com/ethereum/eth-go/ethlog" |
|
|
|
"github.com/ethereum/eth-go/ethlog" |
|
|
|
"github.com/ethereum/eth-go/ethstate" |
|
|
|
"github.com/ethereum/eth-go/ethstate" |
|
|
|
|
|
|
|
"github.com/ethereum/eth-go/ethtrie" |
|
|
|
"github.com/ethereum/eth-go/ethutil" |
|
|
|
"github.com/ethereum/eth-go/ethutil" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -23,7 +27,9 @@ func (self TestEnv) Coinbase() []byte { return nil } |
|
|
|
func (self TestEnv) Time() int64 { return 0 } |
|
|
|
func (self TestEnv) Time() int64 { return 0 } |
|
|
|
func (self TestEnv) Difficulty() *big.Int { return nil } |
|
|
|
func (self TestEnv) Difficulty() *big.Int { return nil } |
|
|
|
func (self TestEnv) Value() *big.Int { return nil } |
|
|
|
func (self TestEnv) Value() *big.Int { return nil } |
|
|
|
func (self TestEnv) State() *ethstate.State { 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, "")) } |
|
|
|
|
|
|
|
|
|
|
|
const mutcode = ` |
|
|
|
const mutcode = ` |
|
|
|
var x = 0; |
|
|
|
var x = 0; |
|
|
@ -93,3 +99,58 @@ func BenchmarkVm(b *testing.B) { |
|
|
|
closure.Call(vm, nil) |
|
|
|
closure.Call(vm, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func RunCode(mutCode string, typ Type) []byte { |
|
|
|
|
|
|
|
code, err := ethutil.Compile(mutCode, true) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pipe output to /dev/null
|
|
|
|
|
|
|
|
ethlog.AddLogSystem(ethlog.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlog.LogLevel(4))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|