Skip to content

Commit 211cb03

Browse files
committed
Merge branch 'develop' of github.com-obscure:ethereum/go-ethereum into develop
2 parents 253ecdc + 9562a98 commit 211cb03

7 files changed

Lines changed: 78 additions & 24 deletions

File tree

‎cmd/ethereum/blocktest.go‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55

66
"github.com/codegangsta/cli"
77
"github.com/ethereum/go-ethereum/cmd/utils"
8+
"github.com/ethereum/go-ethereum/common"
89
"github.com/ethereum/go-ethereum/eth"
910
"github.com/ethereum/go-ethereum/ethdb"
10-
"github.com/ethereum/go-ethereum/common"
1111
"github.com/ethereum/go-ethereum/tests"
1212
)
1313

@@ -26,10 +26,10 @@ be able to interact with the chain defined by the test.
2626
}
2727

2828
func runblocktest(ctx *cli.Context) {
29-
if len(ctx.Args()) != 2 {
30-
utils.Fatalf("This command requires two arguments.")
29+
if len(ctx.Args()) != 3 {
30+
utils.Fatalf("Usage: ethereum blocktest <path-to-test-file> <test-name> {rpc, norpc}")
3131
}
32-
file, testname := ctx.Args()[0], ctx.Args()[1]
32+
file, testname, startrpc := ctx.Args()[0], ctx.Args()[1], ctx.Args()[2]
3333

3434
bt, err := tests.LoadBlockTests(file)
3535
if err != nil {
@@ -42,6 +42,7 @@ func runblocktest(ctx *cli.Context) {
4242

4343
cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
4444
cfg.NewDB = func(path string) (common.Database, error) { return ethdb.NewMemDatabase() }
45+
cfg.MaxPeers = 0 // disable network
4546
ethereum, err := eth.New(cfg)
4647
if err != nil {
4748
utils.Fatalf("%v", err)
@@ -51,7 +52,8 @@ func runblocktest(ctx *cli.Context) {
5152
ethereum.ResetWithGenesisBlock(test.Genesis)
5253

5354
// import pre accounts
54-
if err := test.InsertPreState(ethereum.StateDb()); err != nil {
55+
statedb, err := test.InsertPreState(ethereum.StateDb())
56+
if err != nil {
5557
utils.Fatalf("could not insert genesis accounts: %v", err)
5658
}
5759

@@ -60,7 +62,19 @@ func runblocktest(ctx *cli.Context) {
6062
if err := chain.InsertChain(test.Blocks); err != nil {
6163
utils.Fatalf("Block Test load error: %v", err)
6264
} else {
63-
fmt.Println("Block Test chain loaded, starting ethereum.")
65+
fmt.Println("Block Test chain loaded")
66+
}
67+
68+
if err := test.ValidatePostState(statedb); err != nil {
69+
utils.Fatalf("post state validation failed: %v", err)
70+
}
71+
fmt.Println("Block Test post state validated, starting ethereum.")
72+
73+
if startrpc == "rpc" {
74+
startEth(ctx, ethereum)
75+
utils.StartRPC(ethereum, ctx)
76+
ethereum.WaitForShutdown()
77+
} else {
78+
startEth(ctx, ethereum)
6479
}
65-
startEth(ctx, ethereum)
6680
}

‎cmd/ethtest/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func RunVmTest(r io.Reader) (failed int) {
219219
}
220220

221221
func main() {
222-
helper.Logger.SetLogLevel(5)
222+
//helper.Logger.SetLogLevel(5)
223223
vm.Debug = true
224224

225225
if len(os.Args) > 1 {

‎cmd/utils/flags.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func StartRPC(eth *eth.Ethereum, ctx *cli.Context) {
256256
addr := ctx.GlobalString(RPCListenAddrFlag.Name)
257257
port := ctx.GlobalInt(RPCPortFlag.Name)
258258
dataDir := ctx.GlobalString(DataDirFlag.Name)
259-
259+
fmt.Println("Starting RPC on port: ", port)
260260
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port))
261261
if err != nil {
262262
Fatalf("Can't listen on %s:%d: %v", addr, port, err)

‎core/chain_manager.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ type StateQuery interface {
3232
func CalcDifficulty(block, parent *types.Header) *big.Int {
3333
diff := new(big.Int)
3434

35-
min := big.NewInt(2048)
36-
adjust := new(big.Int).Div(parent.Difficulty, min)
35+
diffBoundDiv := big.NewInt(2048)
36+
min := big.NewInt(131072)
37+
38+
adjust := new(big.Int).Div(parent.Difficulty, diffBoundDiv)
3739
if (block.Time - parent.Time) < 8 {
3840
diff.Add(parent.Difficulty, adjust)
3941
} else {

‎rpc/args.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/ethereum/go-ethereum/common"
99
)
1010

11-
func blockAge(raw interface{}, number *int64) (err error) {
11+
func blockHeight(raw interface{}, number *int64) (err error) {
1212
// Parse as integer
1313
num, ok := raw.(float64)
1414
if ok {
@@ -26,7 +26,7 @@ func blockAge(raw interface{}, number *int64) (err error) {
2626
case "latest":
2727
*number = -1
2828
case "pending":
29-
*number = 0
29+
*number = -2
3030
default:
3131
*number = common.String2Big(str).Int64()
3232
}
@@ -137,7 +137,7 @@ func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
137137
return NewDecodeParamError(err.Error())
138138
}
139139

140-
if err := blockAge(raw, &args.BlockNumber); err != nil {
140+
if err := blockHeight(raw, &args.BlockNumber); err != nil {
141141
return err
142142
}
143143
}
@@ -174,7 +174,7 @@ func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
174174
args.Address = addstr
175175

176176
if len(obj) > 1 {
177-
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
177+
if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
178178
return err
179179
}
180180
}
@@ -218,7 +218,7 @@ func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
218218
args.Key = keystr
219219

220220
if len(obj) > 2 {
221-
if err := blockAge(obj[2], &args.BlockNumber); err != nil {
221+
if err := blockHeight(obj[2], &args.BlockNumber); err != nil {
222222
return err
223223
}
224224
}
@@ -259,7 +259,7 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
259259
args.Address = addstr
260260

261261
if len(obj) > 1 {
262-
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
262+
if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
263263
return err
264264
}
265265
}
@@ -296,7 +296,7 @@ func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
296296
args.Address = addstr
297297

298298
if len(obj) > 1 {
299-
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
299+
if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
300300
return err
301301
}
302302
}
@@ -333,7 +333,7 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
333333
args.Address = addstr
334334

335335
if len(obj) > 1 {
336-
if err := blockAge(obj[1], &args.BlockNumber); err != nil {
336+
if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
337337
return err
338338
}
339339
}

‎tests/blocktest.go‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
)
2020

2121
// Block Test JSON Format
22-
2322
type btJSON struct {
2423
Blocks []btBlock
2524
GenesisBlockHeader btHeader
2625
Pre map[string]btAccount
26+
PostState map[string]btAccount
2727
}
2828

2929
type btAccount struct {
@@ -97,7 +97,7 @@ func LoadBlockTests(file string) (map[string]*BlockTest, error) {
9797

9898
// InsertPreState populates the given database with the genesis
9999
// accounts defined by the test.
100-
func (t *BlockTest) InsertPreState(db common.Database) error {
100+
func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
101101
statedb := state.New(common.Hash{}, db)
102102
for addrString, acct := range t.preAccounts {
103103
// XXX: is is worth it checking for errors here?
@@ -119,8 +119,33 @@ func (t *BlockTest) InsertPreState(db common.Database) error {
119119
// sync trie to disk
120120
statedb.Sync()
121121

122-
if t.Genesis.Root() != statedb.Root() {
123-
return errors.New("computed state root does not match genesis block")
122+
if !bytes.Equal(t.Genesis.Root().Bytes(), statedb.Root().Bytes()) {
123+
return nil, errors.New("computed state root does not match genesis block")
124+
}
125+
return statedb, nil
126+
}
127+
128+
func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
129+
for addrString, acct := range t.preAccounts {
130+
// XXX: is is worth it checking for errors here?
131+
addr, _ := hex.DecodeString(addrString)
132+
code, _ := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x"))
133+
balance, _ := new(big.Int).SetString(acct.Balance, 0)
134+
nonce, _ := strconv.ParseUint(acct.Nonce, 16, 64)
135+
136+
// address is indirectly verified by the other fields, as it's the db key
137+
code2 := statedb.GetCode(common.BytesToAddress(addr))
138+
balance2 := statedb.GetBalance(common.BytesToAddress(addr))
139+
nonce2 := statedb.GetNonce(common.BytesToAddress(addr))
140+
if !bytes.Equal(code2, code) {
141+
return fmt.Errorf("account code mismatch, addr, found, expected: ", addrString, hex.EncodeToString(code2), hex.EncodeToString(code))
142+
}
143+
if balance2.Cmp(balance) != 0 {
144+
return fmt.Errorf("account balance mismatch, addr, found, expected: ", addrString, balance2, balance)
145+
}
146+
if nonce2 != nonce {
147+
return fmt.Errorf("account nonce mismatch, addr, found, expected: ", addrString, nonce2, nonce)
148+
}
124149
}
125150
return nil
126151
}

‎xeth/xeth.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,12 @@ func (self *XEth) AtStateNum(num int64) *XEth {
176176
chain := self.Backend().ChainManager()
177177
var block *types.Block
178178

179+
// -1 generally means "latest"
180+
// -2 means "pending", which has no blocknum
179181
if num < 0 {
180-
num = chain.CurrentBlock().Number().Int64() + num + 1
182+
num = chain.CurrentBlock().Number().Int64()
181183
}
184+
182185
block = chain.GetBlockByNumber(uint64(num))
183186

184187
var st *state.StateDB
@@ -229,6 +232,11 @@ func (self *XEth) EthTransactionByHash(hash string) *types.Transaction {
229232
}
230233

231234
func (self *XEth) BlockByNumber(num int64) *Block {
235+
if num == -2 {
236+
// "pending" is non-existant
237+
return &Block{}
238+
}
239+
232240
if num == -1 {
233241
return NewBlock(self.chainManager.CurrentBlock())
234242
}
@@ -237,6 +245,11 @@ func (self *XEth) BlockByNumber(num int64) *Block {
237245
}
238246

239247
func (self *XEth) EthBlockByNumber(num int64) *types.Block {
248+
if num == -2 {
249+
// "pending" is non-existant
250+
return &types.Block{}
251+
}
252+
240253
if num == -1 {
241254
return self.chainManager.CurrentBlock()
242255
}

0 commit comments

Comments
 (0)