From 33c5186fd06d890e590b18cc6f8479070c2e51e8 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Fri, 20 Mar 2015 01:59:38 +0100 Subject: [PATCH 1/7] In blocktest cmd, disable network and add RPC flag --- cmd/ethereum/blocktest.go | 15 +++++++++++---- cmd/utils/flags.go | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/ethereum/blocktest.go b/cmd/ethereum/blocktest.go index e6d701d2c8..f2014acfde 100644 --- a/cmd/ethereum/blocktest.go +++ b/cmd/ethereum/blocktest.go @@ -26,10 +26,10 @@ be able to interact with the chain defined by the test. } func runblocktest(ctx *cli.Context) { - if len(ctx.Args()) != 2 { - utils.Fatalf("This command requires two arguments.") + if len(ctx.Args()) != 3 { + utils.Fatalf("Usage: ethereum blocktest {rpc, norpc}") } - file, testname := ctx.Args()[0], ctx.Args()[1] + file, testname, startrpc := ctx.Args()[0], ctx.Args()[1], ctx.Args()[2] bt, err := tests.LoadBlockTests(file) if err != nil { @@ -42,6 +42,7 @@ func runblocktest(ctx *cli.Context) { cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx) cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() } + cfg.MaxPeers = 0 // disable network ethereum, err := eth.New(cfg) if err != nil { utils.Fatalf("%v", err) @@ -62,5 +63,11 @@ func runblocktest(ctx *cli.Context) { } else { fmt.Println("Block Test chain loaded, starting ethereum.") } - startEth(ctx, ethereum) + if startrpc == "rpc" { + startEth(ctx, ethereum) + utils.StartRPC(ethereum, ctx) + ethereum.WaitForShutdown() + } else { + startEth(ctx, ethereum) + } } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f87d25ce9f..9a4ab58044 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -256,7 +256,7 @@ func StartRPC(eth *eth.Ethereum, ctx *cli.Context) { addr := ctx.GlobalString(RPCListenAddrFlag.Name) port := ctx.GlobalInt(RPCPortFlag.Name) dataDir := ctx.GlobalString(DataDirFlag.Name) - + fmt.Println("Starting RPC on port: ", port) l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port)) if err != nil { Fatalf("Can't listen on %s:%d: %v", addr, port, err) From 08bb472c913a2b4cf0838785616b1ec7712d0b00 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Fri, 20 Mar 2015 09:10:13 +0100 Subject: [PATCH 2/7] Add validation of post state accounts to block tests --- cmd/ethereum/blocktest.go | 11 +++++++++-- tests/blocktest.go | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cmd/ethereum/blocktest.go b/cmd/ethereum/blocktest.go index f2014acfde..b75518138d 100644 --- a/cmd/ethereum/blocktest.go +++ b/cmd/ethereum/blocktest.go @@ -52,7 +52,8 @@ func runblocktest(ctx *cli.Context) { ethereum.ResetWithGenesisBlock(test.Genesis) // import pre accounts - if err := test.InsertPreState(ethereum.StateDb()); err != nil { + statedb, err := test.InsertPreState(ethereum.StateDb()) + if err != nil { utils.Fatalf("could not insert genesis accounts: %v", err) } @@ -61,8 +62,14 @@ func runblocktest(ctx *cli.Context) { if err := chain.InsertChain(test.Blocks); err != nil { utils.Fatalf("Block Test load error: %v", err) } else { - fmt.Println("Block Test chain loaded, starting ethereum.") + fmt.Println("Block Test chain loaded") + } + + if err := test.ValidatePostState(statedb); err != nil { + utils.Fatalf("post state validation failed: %v", err) } + fmt.Println("Block Test post state validated, starting ethereum.") + if startrpc == "rpc" { startEth(ctx, ethereum) utils.StartRPC(ethereum, ctx) diff --git a/tests/blocktest.go b/tests/blocktest.go index 44b459c8d6..1c26bd6491 100644 --- a/tests/blocktest.go +++ b/tests/blocktest.go @@ -19,11 +19,11 @@ import ( ) // Block Test JSON Format - type btJSON struct { Blocks []btBlock GenesisBlockHeader btHeader Pre map[string]btAccount + PostState map[string]btAccount } type btAccount struct { @@ -97,7 +97,7 @@ 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 { +func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) { statedb := state.New(common.Hash{}, db) for addrString, acct := range t.preAccounts { // XXX: is is worth it checking for errors here? @@ -119,8 +119,35 @@ func (t *BlockTest) InsertPreState(db common.Database) error { // sync trie to disk statedb.Sync() - if t.Genesis.Root() != statedb.Root() { - return errors.New("computed state root does not match genesis block") + if !bytes.Equal(t.Genesis.Root(), statedb.Root()) { + return nil, errors.New("computed state root does not match genesis block") + } + return statedb, nil +} + +func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error { + for addrString, acct := range t.preAccounts { + // XXX: is is worth it checking for errors here? + 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) + + // address is indirectly verified by the other fields, as it's the db key + code2 := statedb.GetCode(addr) + balance2 := statedb.GetBalance(addr) + nonce2 := statedb.GetNonce(addr) + if !bytes.Equal(code2, code) { + return fmt.Errorf("account code mismatch, addr, found, expected: ", addrString, hex.EncodeToString(code2), hex.EncodeToString(code)) + } + if balance2.Cmp(balance) != 0 { + return fmt.Errorf("account balance mismatch, addr, found, expected: ", addrString, balance2, balance) + } + if nonce2 != nonce { + return fmt.Errorf("account nonce mismatch, addr, found, expected: ", addrString, nonce2, nonce) + } + +>>>>>>> Add validation of post state accounts to block tests } return nil } From 60de4d6dd1403abd4daa142bb2e43cb917fe7b15 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Fri, 20 Mar 2015 09:13:17 +0100 Subject: [PATCH 3/7] gofmt --- cmd/ethereum/blocktest.go | 2 +- tests/blocktest.go | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cmd/ethereum/blocktest.go b/cmd/ethereum/blocktest.go index b75518138d..d9cdfa83f0 100644 --- a/cmd/ethereum/blocktest.go +++ b/cmd/ethereum/blocktest.go @@ -5,9 +5,9 @@ import ( "github.com/codegangsta/cli" "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/tests" ) diff --git a/tests/blocktest.go b/tests/blocktest.go index 1c26bd6491..2ce27cc388 100644 --- a/tests/blocktest.go +++ b/tests/blocktest.go @@ -134,9 +134,9 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error { nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64) // address is indirectly verified by the other fields, as it's the db key - code2 := statedb.GetCode(addr) + code2 := statedb.GetCode(addr) balance2 := statedb.GetBalance(addr) - nonce2 := statedb.GetNonce(addr) + nonce2 := statedb.GetNonce(addr) if !bytes.Equal(code2, code) { return fmt.Errorf("account code mismatch, addr, found, expected: ", addrString, hex.EncodeToString(code2), hex.EncodeToString(code)) } @@ -146,8 +146,6 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error { if nonce2 != nonce { return fmt.Errorf("account nonce mismatch, addr, found, expected: ", addrString, nonce2, nonce) } - ->>>>>>> Add validation of post state accounts to block tests } return nil } From 5d31a475e93807fb1b3a71765fe66138cacd15e7 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Sat, 21 Mar 2015 20:29:12 +0100 Subject: [PATCH 4/7] Fix new types for blocktests and comment out non-working log level setter --- cmd/ethtest/main.go | 2 +- tests/blocktest.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/ethtest/main.go b/cmd/ethtest/main.go index f2f7d27f37..fdf573fd8a 100644 --- a/cmd/ethtest/main.go +++ b/cmd/ethtest/main.go @@ -219,7 +219,7 @@ func RunVmTest(r io.Reader) (failed int) { } func main() { - helper.Logger.SetLogLevel(5) + //helper.Logger.SetLogLevel(5) vm.Debug = true if len(os.Args) > 1 { diff --git a/tests/blocktest.go b/tests/blocktest.go index 2ce27cc388..5719a835ba 100644 --- a/tests/blocktest.go +++ b/tests/blocktest.go @@ -119,7 +119,7 @@ func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) { // sync trie to disk statedb.Sync() - if !bytes.Equal(t.Genesis.Root(), statedb.Root()) { + if !bytes.Equal(t.Genesis.Root().Bytes(), statedb.Root().Bytes()) { return nil, errors.New("computed state root does not match genesis block") } return statedb, nil @@ -134,9 +134,9 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error { nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64) // address is indirectly verified by the other fields, as it's the db key - code2 := statedb.GetCode(addr) - balance2 := statedb.GetBalance(addr) - nonce2 := statedb.GetNonce(addr) + code2 := statedb.GetCode(common.BytesToAddress(addr)) + balance2 := statedb.GetBalance(common.BytesToAddress(addr)) + nonce2 := statedb.GetNonce(common.BytesToAddress(addr)) if !bytes.Equal(code2, code) { return fmt.Errorf("account code mismatch, addr, found, expected: ", addrString, hex.EncodeToString(code2), hex.EncodeToString(code)) } From becc503230ecf259fefbd261ea0abf54bbbb0f5b Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Mon, 23 Mar 2015 16:31:50 +0100 Subject: [PATCH 5/7] Correct difficulty calculation to use new difficulty minimum --- core/chain_manager.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/chain_manager.go b/core/chain_manager.go index 1bc8edea6f..73b68358bb 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -32,8 +32,10 @@ type StateQuery interface { func CalcDifficulty(block, parent *types.Header) *big.Int { diff := new(big.Int) - min := big.NewInt(2048) - adjust := new(big.Int).Div(parent.Difficulty, min) + diffBoundDiv := big.NewInt(2048) + min := big.NewInt(131072) + + adjust := new(big.Int).Div(parent.Difficulty, diffBoundDiv) if (block.Time - parent.Time) < 8 { diff.Add(parent.Difficulty, adjust) } else { From 2f8601ef382aeff6cdb31fe163cfac3b26bf56de Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Mon, 23 Mar 2015 16:34:50 +0100 Subject: [PATCH 6/7] Rename blockAge to blockHeight --- rpc/args.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rpc/args.go b/rpc/args.go index 06dab99bca..4b59f83b7d 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -8,7 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" ) -func blockAge(raw interface{}, number *int64) (err error) { +func blockHeight(raw interface{}, number *int64) (err error) { // Parse as integer num, ok := raw.(float64) if ok { @@ -137,7 +137,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) { return NewDecodeParamError(err.Error()) } - if err := blockAge(raw, &args.BlockNumber); err != nil { + if err := blockHeight(raw, &args.BlockNumber); err != nil { return err } } @@ -174,7 +174,7 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -218,7 +218,7 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) { args.Key = keystr if len(obj) > 2 { - if err := blockAge(obj[2], &args.BlockNumber); err != nil { + if err := blockHeight(obj[2], &args.BlockNumber); err != nil { return err } } @@ -259,7 +259,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -296,7 +296,7 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } @@ -333,7 +333,7 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) { args.Address = addstr if len(obj) > 1 { - if err := blockAge(obj[1], &args.BlockNumber); err != nil { + if err := blockHeight(obj[1], &args.BlockNumber); err != nil { return err } } From 5707912e2f7d49b3654d616ddae3439e6dc201c4 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Mon, 23 Mar 2015 16:36:12 +0100 Subject: [PATCH 7/7] "pending" convention should be -2 instead of 0 --- rpc/args.go | 2 +- xeth/xeth.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/rpc/args.go b/rpc/args.go index 4b59f83b7d..6e02b65ef3 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -26,7 +26,7 @@ func blockHeight(raw interface{}, number *int64) (err error) { case "latest": *number = -1 case "pending": - *number = 0 + *number = -2 default: *number = common.String2Big(str).Int64() } diff --git a/xeth/xeth.go b/xeth/xeth.go index 23e5239801..f7f4ce0cfb 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -176,9 +176,12 @@ func (self *XEth) AtStateNum(num int64) *XEth { chain := self.Backend().ChainManager() var block *types.Block + // -1 generally means "latest" + // -2 means "pending", which has no blocknum if num < 0 { - num = chain.CurrentBlock().Number().Int64() + num + 1 + num = chain.CurrentBlock().Number().Int64() } + block = chain.GetBlockByNumber(uint64(num)) var st *state.StateDB @@ -229,6 +232,11 @@ func (self *XEth) EthTransactionByHash(hash string) *types.Transaction { } func (self *XEth) BlockByNumber(num int64) *Block { + if num == -2 { + // "pending" is non-existant + return &Block{} + } + if num == -1 { return NewBlock(self.chainManager.CurrentBlock()) } @@ -237,6 +245,11 @@ func (self *XEth) BlockByNumber(num int64) *Block { } func (self *XEth) EthBlockByNumber(num int64) *types.Block { + if num == -2 { + // "pending" is non-existant + return &types.Block{} + } + if num == -1 { return self.chainManager.CurrentBlock() }