diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/blocktest.go | 55 | ||||
-rw-r--r-- | tests/helper/vm.go | 90 | ||||
-rw-r--r-- | tests/vm/gh_test.go | 25 |
3 files changed, 106 insertions, 64 deletions
diff --git a/tests/blocktest.go b/tests/blocktest.go index 0b923f08b..e4d001089 100644 --- a/tests/blocktest.go +++ b/tests/blocktest.go @@ -12,8 +12,9 @@ import ( "strconv" "strings" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" ) @@ -101,12 +102,12 @@ func (t *BlockTest) InsertPreState(db common.Database) error { statedb := state.New(nil, db) for addrString, acct := range t.preAccounts { // XXX: is is worth it checking for errors here? - addr, _ := hex.DecodeString(addrString) + //addr, _ := hex.DecodeString(addrString) code, _ := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x")) balance, _ := new(big.Int).SetString(acct.Balance, 0) nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64) - obj := statedb.NewStateObject(addr) + obj := statedb.NewStateObject(common.HexToAddress(addrString)) obj.SetCode(code) obj.SetBalance(balance) obj.SetNonce(nonce) @@ -119,7 +120,7 @@ func (t *BlockTest) InsertPreState(db common.Database) error { // sync trie to disk statedb.Sync() - if !bytes.Equal(t.Genesis.Root(), statedb.Root()) { + if !bytes.Equal(t.Genesis.Root().Bytes(), statedb.Root()) { return errors.New("computed state root does not match genesis block") } return nil @@ -153,23 +154,25 @@ func mustConvertGenesis(testGenesis btHeader) *types.Block { func mustConvertHeader(in btHeader) *types.Header { // hex decode these fields - return &types.Header{ + header := &types.Header{ //SeedHash: mustConvertBytes(in.SeedHash), - MixDigest: mustConvertBytes(in.MixHash), - Bloom: mustConvertBytes(in.Bloom), - ReceiptHash: mustConvertBytes(in.ReceiptTrie), - TxHash: mustConvertBytes(in.TransactionsTrie), - Root: mustConvertBytes(in.StateRoot), - Coinbase: mustConvertBytes(in.Coinbase), - UncleHash: mustConvertBytes(in.UncleHash), - ParentHash: mustConvertBytes(in.ParentHash), - Nonce: mustConvertBytes(in.Nonce), + MixDigest: mustConvertHash(in.MixHash), + Bloom: mustConvertBloom(in.Bloom), + ReceiptHash: mustConvertHash(in.ReceiptTrie), + TxHash: mustConvertHash(in.TransactionsTrie), + Root: mustConvertHash(in.StateRoot), + Coinbase: mustConvertAddress(in.Coinbase), + UncleHash: mustConvertHash(in.UncleHash), + ParentHash: mustConvertHash(in.ParentHash), Extra: string(mustConvertBytes(in.ExtraData)), GasUsed: mustConvertBigInt10(in.GasUsed), GasLimit: mustConvertBigInt10(in.GasLimit), Difficulty: mustConvertBigInt10(in.Difficulty), Time: mustConvertUint(in.Timestamp), } + // XXX cheats? :-) + header.SetNonce(common.BytesToHash(mustConvertBytes(in.Nonce)).Big().Uint64()) + return header } func mustConvertBlocks(testBlocks []btBlock) []*types.Block { @@ -193,6 +196,30 @@ func mustConvertBytes(in string) []byte { return out } +func mustConvertHash(in string) common.Hash { + out, err := hex.DecodeString(strings.TrimPrefix(in, "0x")) + if err != nil { + panic(fmt.Errorf("invalid hex: %q", in)) + } + return common.BytesToHash(out) +} + +func mustConvertAddress(in string) common.Address { + out, err := hex.DecodeString(strings.TrimPrefix(in, "0x")) + if err != nil { + panic(fmt.Errorf("invalid hex: %q", in)) + } + return common.BytesToAddress(out) +} + +func mustConvertBloom(in string) core.Bloom { + out, err := hex.DecodeString(strings.TrimPrefix(in, "0x")) + if err != nil { + panic(fmt.Errorf("invalid hex: %q", in)) + } + return core.BytesToBloom(out) +} + func mustConvertBigInt10(in string) *big.Int { out, ok := new(big.Int).SetString(in, 10) if !ok { diff --git a/tests/helper/vm.go b/tests/helper/vm.go index a7fd98696..f59abd23a 100644 --- a/tests/helper/vm.go +++ b/tests/helper/vm.go @@ -4,9 +4,9 @@ import ( "errors" "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/vm" ) @@ -18,9 +18,9 @@ type Env struct { initial bool Gas *big.Int - origin []byte - parent []byte - coinbase []byte + origin common.Address + //parent common.Hash + coinbase common.Address number *big.Int time int64 @@ -41,9 +41,9 @@ func NewEnv(state *state.StateDB) *Env { func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues map[string]string) *Env { env := NewEnv(state) - env.origin = common.Hex2Bytes(exeValues["caller"]) - env.parent = common.Hex2Bytes(envValues["previousHash"]) - env.coinbase = common.Hex2Bytes(envValues["currentCoinbase"]) + env.origin = common.HexToAddress(exeValues["caller"]) + //env.parent = common.Hex2Bytes(envValues["previousHash"]) + env.coinbase = common.HexToAddress(envValues["currentCoinbase"]) env.number = common.Big(envValues["currentNumber"]) env.time = common.Big(envValues["currentTimestamp"]).Int64() env.difficulty = common.Big(envValues["currentDifficulty"]) @@ -53,17 +53,18 @@ func NewEnvFromMap(state *state.StateDB, envValues map[string]string, exeValues return env } -func (self *Env) Origin() []byte { return self.origin } -func (self *Env) BlockNumber() *big.Int { return self.number } -func (self *Env) PrevHash() []byte { return self.parent } -func (self *Env) Coinbase() []byte { return self.coinbase } -func (self *Env) Time() int64 { return self.time } -func (self *Env) Difficulty() *big.Int { return self.difficulty } -func (self *Env) State() *state.StateDB { return self.state } -func (self *Env) GasLimit() *big.Int { return self.gasLimit } -func (self *Env) VmType() vm.Type { return vm.StdVmTy } -func (self *Env) GetHash(n uint64) []byte { - return crypto.Sha3([]byte(big.NewInt(int64(n)).String())) +func (self *Env) Origin() common.Address { return self.origin } +func (self *Env) BlockNumber() *big.Int { return self.number } + +//func (self *Env) PrevHash() []byte { return self.parent } +func (self *Env) Coinbase() common.Address { return self.coinbase } +func (self *Env) Time() int64 { return self.time } +func (self *Env) Difficulty() *big.Int { return self.difficulty } +func (self *Env) State() *state.StateDB { return self.state } +func (self *Env) GasLimit() *big.Int { return self.gasLimit } +func (self *Env) VmType() vm.Type { return vm.StdVmTy } +func (self *Env) GetHash(n uint64) common.Hash { + return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String()))) } func (self *Env) AddLog(log state.Log) { self.logs = append(self.logs, log) @@ -87,37 +88,39 @@ func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error { return vm.Transfer(from, to, amount) } -func (self *Env) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution { +func (self *Env) vm(addr *common.Address, data []byte, gas, price, value *big.Int) *core.Execution { exec := core.NewExecution(self, addr, data, gas, price, value) return exec } -func (self *Env) Call(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { +func (self *Env) Call(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { if self.vmTest && self.depth > 0 { caller.ReturnGas(gas, price) return nil, nil } - exe := self.vm(addr, data, gas, price, value) + exe := self.vm(&addr, data, gas, price, value) ret, err := exe.Call(addr, caller) self.Gas = exe.Gas return ret, err } -func (self *Env) CallCode(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { +func (self *Env) CallCode(caller vm.ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) { if self.vmTest && self.depth > 0 { caller.ReturnGas(gas, price) return nil, nil } - exe := self.vm(caller.Address(), data, gas, price, value) + + caddr := caller.Address() + exe := self.vm(&caddr, data, gas, price, value) return exe.Call(addr, caller) } -func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { - exe := self.vm(addr, data, gas, price, value) +func (self *Env) Create(caller vm.ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { + exe := self.vm(nil, data, gas, price, value) if self.vmTest { caller.ReturnGas(gas, price) @@ -132,8 +135,8 @@ func (self *Env) Create(caller vm.ContextRef, addr, data []byte, gas, price, val func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Logs, *big.Int, error) { var ( - to = FromHex(exec["address"]) - from = FromHex(exec["caller"]) + to = common.HexToAddress(exec["address"]) + from = common.HexToAddress(exec["caller"]) data = FromHex(exec["data"]) gas = common.Big(exec["gas"]) price = common.Big(exec["gasPrice"]) @@ -156,14 +159,18 @@ func RunVm(state *state.StateDB, env, exec map[string]string) ([]byte, state.Log func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state.Logs, *big.Int, error) { var ( keyPair, _ = crypto.NewKeyPairFromSec([]byte(common.Hex2Bytes(tx["secretKey"]))) - to = FromHex(tx["to"]) data = FromHex(tx["data"]) gas = common.Big(tx["gasLimit"]) price = common.Big(tx["gasPrice"]) value = common.Big(tx["value"]) - caddr = FromHex(env["currentCoinbase"]) + caddr = common.HexToAddress(env["currentCoinbase"]) ) + var to *common.Address + if len(tx["to"]) > 2 { + t := common.HexToAddress(tx["to"]) + to = &t + } // Set pre compiled contracts vm.Precompiled = vm.PrecompiledContracts() @@ -171,9 +178,9 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state. coinbase := statedb.GetOrNewStateObject(caddr) coinbase.SetGasPool(common.Big(env["currentGasLimit"])) - message := NewMessage(keyPair.Address(), to, data, value, gas, price) + message := NewMessage(common.BytesToAddress(keyPair.Address()), to, data, value, gas, price) vmenv := NewEnvFromMap(statedb, env, tx) - vmenv.origin = keyPair.Address() + vmenv.origin = common.BytesToAddress(keyPair.Address()) ret, _, err := core.ApplyMessage(vmenv, message, coinbase) if core.IsNonceErr(err) || core.IsInvalidTxErr(err) { statedb.Set(snapshot) @@ -184,20 +191,21 @@ func RunState(statedb *state.StateDB, env, tx map[string]string) ([]byte, state. } type Message struct { - from, to []byte + from common.Address + to *common.Address value, gas, price *big.Int data []byte } -func NewMessage(from, to, data []byte, value, gas, price *big.Int) Message { +func NewMessage(from common.Address, to *common.Address, data []byte, value, gas, price *big.Int) Message { return Message{from, to, value, gas, price, data} } -func (self Message) Hash() []byte { return nil } -func (self Message) From() []byte { return self.from } -func (self Message) To() []byte { return self.to } -func (self Message) GasPrice() *big.Int { return self.price } -func (self Message) Gas() *big.Int { return self.gas } -func (self Message) Value() *big.Int { return self.value } -func (self Message) Nonce() uint64 { return 0 } -func (self Message) Data() []byte { return self.data } +func (self Message) Hash() []byte { return nil } +func (self Message) From() (common.Address, error) { return self.from, nil } +func (self Message) To() *common.Address { return self.to } +func (self Message) GasPrice() *big.Int { return self.price } +func (self Message) Gas() *big.Int { return self.gas } +func (self Message) Value() *big.Int { return self.value } +func (self Message) Nonce() uint64 { return 0 } +func (self Message) Data() []byte { return self.data } diff --git a/tests/vm/gh_test.go b/tests/vm/gh_test.go index 24718de7b..159561c62 100644 --- a/tests/vm/gh_test.go +++ b/tests/vm/gh_test.go @@ -6,11 +6,12 @@ import ( "strconv" "testing" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/tests/helper" + "github.com/ethereum/go-ethereum/vm" ) type Account struct { @@ -39,7 +40,7 @@ func (self Log) Topics() [][]byte { } func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject { - obj := state.NewStateObject(common.Hex2Bytes(addr), db) + obj := state.NewStateObject(common.HexToAddress(addr), db) obj.SetBalance(common.Big(account.Balance)) if common.IsHex(account.Code) { @@ -80,13 +81,18 @@ func RunVmTest(p string, t *testing.T) { helper.CreateFileTests(t, p, &tests) for name, test := range tests { + helper.Logger.SetLogLevel(5) + vm.Debug = true + if name != "TransactionCreateSuicideContract" { + continue + } db, _ := ethdb.NewMemDatabase() - statedb := state.New(nil, db) + statedb := state.New(common.Hash{}, db) for addr, account := range test.Pre { obj := StateObjectFromAccount(db, addr, account) statedb.SetStateObject(obj) for a, v := range account.Storage { - obj.SetState(helper.FromHex(a), common.NewValue(helper.FromHex(v))) + obj.SetState(common.HexToHash(a), common.NewValue(helper.FromHex(v))) } } @@ -134,30 +140,31 @@ func RunVmTest(p string, t *testing.T) { } for addr, account := range test.Post { - obj := statedb.GetStateObject(helper.FromHex(addr)) + obj := statedb.GetStateObject(common.HexToAddress(addr)) if obj == nil { continue } if len(test.Exec) == 0 { if obj.Balance().Cmp(common.Big(account.Balance)) != 0 { - t.Errorf("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance())) + 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())) } } for addr, value := range account.Storage { - v := obj.GetState(helper.FromHex(addr)).Bytes() + v := obj.GetState(common.HexToHash(addr)).Bytes() vexp := helper.FromHex(value) if bytes.Compare(v, vexp) != 0 { - t.Errorf("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v)) + 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)) } } } if !isVmTest { statedb.Sync() - if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { + //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { + if common.HexToHash(test.PostStateRoot) != statedb.Root() { t.Errorf("%s's : Post state root error. Expected %s, got %x", name, test.PostStateRoot, statedb.Root()) } } |