diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/blocktest.go | 56 | ||||
-rw-r--r-- | tests/helper/vm.go | 88 | ||||
-rw-r--r-- | tests/vm/gh_test.go | 50 |
3 files changed, 122 insertions, 72 deletions
diff --git a/tests/blocktest.go b/tests/blocktest.go index 0b923f08b..44b459c8d 100644 --- a/tests/blocktest.go +++ b/tests/blocktest.go @@ -12,8 +12,8 @@ import ( "strconv" "strings" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" ) @@ -98,15 +98,15 @@ func LoadBlockTests(file string) (map[string]*BlockTest, error) { // InsertPreState populates the given database with the genesis // accounts defined by the test. func (t *BlockTest) InsertPreState(db common.Database) error { - statedb := state.New(nil, db) + statedb := state.New(common.Hash{}, 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 +119,7 @@ func (t *BlockTest) InsertPreState(db common.Database) error { // sync trie to disk statedb.Sync() - if !bytes.Equal(t.Genesis.Root(), statedb.Root()) { + if t.Genesis.Root() != statedb.Root() { return errors.New("computed state root does not match genesis block") } return nil @@ -153,23 +153,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 +195,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) types.Bloom { + out, err := hex.DecodeString(strings.TrimPrefix(in, "0x")) + if err != nil { + panic(fmt.Errorf("invalid hex: %q", in)) + } + return types.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..44c108870 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,36 +88,38 @@ 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) { +func (self *Env) Create(caller vm.ContextRef, addr *common.Address, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { exe := self.vm(addr, 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..68600d304 100644 --- a/tests/vm/gh_test.go +++ b/tests/vm/gh_test.go @@ -6,8 +6,9 @@ import ( "strconv" "testing" - "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "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" @@ -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) { @@ -81,12 +82,12 @@ func RunVmTest(p string, t *testing.T) { for name, test := range tests { 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 +135,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()) } } @@ -166,16 +168,30 @@ func RunVmTest(p string, t *testing.T) { if len(test.Logs) != len(logs) { t.Errorf("log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) } else { - /* - fmt.Println("A", test.Logs) - fmt.Println("B", logs) - for i, log := range test.Logs { - genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256) - if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) { - t.Errorf("bloom mismatch") + for i, log := range test.Logs { + if common.HexToAddress(log.AddressF) != logs[i].Address() { + t.Errorf("'%s' log address expected %v got %x", name, log.AddressF, logs[i].Address()) + } + + if !bytes.Equal(logs[i].Data(), helper.FromHex(log.DataF)) { + t.Errorf("'%s' log data expected %v got %x", name, log.DataF, logs[i].Data()) + } + + if len(log.TopicsF) != len(logs[i].Topics()) { + t.Errorf("'%s' log topics length expected %d got %d", name, len(log.TopicsF), logs[i].Topics()) + } else { + for j, topic := range log.TopicsF { + if common.HexToHash(topic) != logs[i].Topics()[j] { + t.Errorf("'%s' log topic[%d] expected %v got %x", name, j, topic, logs[i].Topics()[j]) } } - */ + } + genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256) + + if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) { + t.Errorf("'%s' bloom mismatch", name) + } + } } } //statedb.Trie().PrintRoot() |