diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/ethereum/admin.go | 9 | ||||
-rw-r--r-- | cmd/ethereum/main.go | 2 | ||||
-rw-r--r-- | cmd/ethtest/main.go | 68 | ||||
-rw-r--r-- | cmd/utils/cmd.go | 36 |
4 files changed, 69 insertions, 46 deletions
diff --git a/cmd/ethereum/admin.go b/cmd/ethereum/admin.go index 880a22c22..41aaf46d8 100644 --- a/cmd/ethereum/admin.go +++ b/cmd/ethereum/admin.go @@ -8,8 +8,8 @@ import ( "time" "github.com/ethereum/go-ethereum/cmd/utils" - "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/rpc" "github.com/ethereum/go-ethereum/state" @@ -221,13 +221,10 @@ func (js *jsre) exportChain(call otto.FunctionCall) otto.Value { fmt.Println(err) return otto.FalseValue() } - - data := js.ethereum.ChainManager().Export() - if err := common.WriteFile(fn, data); err != nil { + if err := utils.ExportChain(js.ethereum.ChainManager(), fn); err != nil { fmt.Println(err) return otto.FalseValue() } - return otto.TrueValue() } @@ -239,7 +236,7 @@ func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value { block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num)) } else if call.Argument(0).IsString() { hash, _ := call.Argument(0).ToString() - block = js.ethereum.ChainManager().GetBlock(common.Hex2Bytes(hash)) + block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash)) } else { fmt.Println("invalid argument for dump. Either hex string or number") } diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index 38c04ec9d..afaf06948 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -314,7 +314,7 @@ func dump(ctx *cli.Context) { for _, arg := range ctx.Args() { var block *types.Block if hashish(arg) { - block = chainmgr.GetBlock(common.Hex2Bytes(arg)) + block = chainmgr.GetBlock(common.HexToHash(arg)) } else { num, _ := strconv.Atoi(arg) block = chainmgr.GetBlockByNumber(uint64(num)) diff --git a/cmd/ethtest/main.go b/cmd/ethtest/main.go index cf1ec6dfa..f2f7d27f3 100644 --- a/cmd/ethtest/main.go +++ b/cmd/ethtest/main.go @@ -33,6 +33,7 @@ import ( "strings" "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" @@ -66,7 +67,7 @@ type Account struct { } 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) { @@ -112,7 +113,7 @@ func RunVmTest(r io.Reader) (failed int) { 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) @@ -135,63 +136,82 @@ func RunVmTest(r io.Reader) (failed int) { rexp := helper.FromHex(test.Out) if bytes.Compare(rexp, ret) != 0 { - helper.Log.Infof("FAIL: %s's return failed. Expected %x, got %x\n", name, rexp, ret) + helper.Log.Infof("%s's return failed. Expected %x, got %x\n", name, rexp, ret) failed = 1 } 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 { - helper.Log.Infof("FAIL: %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()), - ) + helper.Log.Infof("%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())) failed = 1 } } 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 { - helper.Log.Infof("FAIL: %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)) + helper.Log.Infof("%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)) failed = 1 } } } - if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { - helper.Log.Infof("FAIL: %s's : Post state root error. Expected %s, got %x\n", name, test.PostStateRoot, statedb.Root()) + statedb.Sync() + //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) { + if common.HexToHash(test.PostStateRoot) != statedb.Root() { + helper.Log.Infof("%s's : Post state root failed. Expected %s, got %x", name, test.PostStateRoot, statedb.Root()) failed = 1 } if len(test.Logs) > 0 { if len(test.Logs) != len(logs) { - helper.Log.Infof("FAIL: log length mismatch. Expected %d, got %d", len(test.Logs), len(logs)) + helper.Log.Infof("log length failed. Expected %d, got %d", len(test.Logs), len(logs)) failed = 1 } 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() { + helper.Log.Infof("'%s' log address failed. Expected %v got %x", name, log.AddressF, logs[i].Address()) + failed = 1 + } + + if !bytes.Equal(logs[i].Data(), helper.FromHex(log.DataF)) { + helper.Log.Infof("'%s' log data failed. Expected %v got %x", name, log.DataF, logs[i].Data()) + failed = 1 + } + + if len(log.TopicsF) != len(logs[i].Topics()) { + helper.Log.Infof("'%s' log topics length failed. Expected %d got %d", name, len(log.TopicsF), logs[i].Topics()) + failed = 1 + } else { + for j, topic := range log.TopicsF { + if common.HexToHash(topic) != logs[i].Topics()[j] { + helper.Log.Infof("'%s' log topic[%d] failed. Expected %v got %x", name, j, topic, logs[i].Topics()[j]) + failed = 1 } } - */ + } + genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256) + + if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) { + helper.Log.Infof("'%s' bloom failed.", name) + failed = 1 + } + } } } + if failed == 1 { + helper.Log.Infoln(string(statedb.Dump())) + } + logger.Flush() } diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 74fd5334e..feea29d64 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -23,14 +23,15 @@ package utils import ( "fmt" + "io" "os" "os/signal" "regexp" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/eth" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/rlp" ) @@ -152,29 +153,34 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error { } defer fh.Close() - var blocks types.Blocks - if err := rlp.Decode(fh, &blocks); err != nil { - return err - } - chainmgr.Reset() - if err := chainmgr.InsertChain(blocks); err != nil { - return err + stream := rlp.NewStream(fh) + var i int + for ; ; i++ { + var b types.Block + if err := stream.Decode(&b); err == io.EOF { + break + } else if err != nil { + return fmt.Errorf("at block %d: %v", i, err) + } + if err := chainmgr.InsertChain(types.Blocks{&b}); err != nil { + return fmt.Errorf("invalid block %d: %v", i, err) + } } - fmt.Printf("imported %d blocks\n", len(blocks)) - + fmt.Printf("imported %d blocks\n", i) return nil } func ExportChain(chainmgr *core.ChainManager, fn string) error { fmt.Printf("exporting blockchain '%s'\n", fn) - - data := chainmgr.Export() - - if err := common.WriteFile(fn, data); err != nil { + fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) + if err != nil { + return err + } + defer fh.Close() + if err := chainmgr.Export(fh); err != nil { return err } fmt.Printf("exported blockchain\n") - return nil } |