@@ -44,8 +44,9 @@ import (
44
44
type BlockTest struct {
45
45
Genesis * types.Block
46
46
47
- Json * btJSON
48
- preAccounts map [string ]btAccount
47
+ Json * btJSON
48
+ preAccounts map [string ]btAccount
49
+ postAccounts map [string ]btAccount
49
50
}
50
51
51
52
type btJSON struct {
@@ -147,7 +148,6 @@ func runBlockTests(bt map[string]*BlockTest, skipTests []string) error {
147
148
glog .Infoln ("Skipping block test" , name )
148
149
continue
149
150
}
150
-
151
151
// test the block
152
152
if err := runBlockTest (test ); err != nil {
153
153
return fmt .Errorf ("%s: %v" , name , err )
@@ -173,7 +173,7 @@ func runBlockTest(test *BlockTest) error {
173
173
}
174
174
175
175
// import pre accounts
176
- statedb , err : = test .InsertPreState (ethereum )
176
+ _ , err = test .InsertPreState (ethereum )
177
177
if err != nil {
178
178
return fmt .Errorf ("InsertPreState: %v" , err )
179
179
}
@@ -183,7 +183,8 @@ func runBlockTest(test *BlockTest) error {
183
183
return err
184
184
}
185
185
186
- if err = test .ValidatePostState (statedb ); err != nil {
186
+ newDB := ethereum .ChainManager ().State ()
187
+ if err = test .ValidatePostState (newDB ); err != nil {
187
188
return fmt .Errorf ("post state validation failed: %v" , err )
188
189
}
189
190
return nil
@@ -265,6 +266,7 @@ func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, erro
265
266
post state.
266
267
*/
267
268
func (t * BlockTest ) TryBlocksInsert (chainManager * core.ChainManager ) error {
269
+ blockNums := make (map [string ]bool )
268
270
// insert the test blocks, which will execute all transactions
269
271
for _ , b := range t .Json .Blocks {
270
272
cb , err := mustConvertBlock (b )
@@ -287,9 +289,35 @@ func (t *BlockTest) TryBlocksInsert(chainManager *core.ChainManager) error {
287
289
if b .BlockHeader == nil {
288
290
return fmt .Errorf ("Block insertion should have failed" )
289
291
}
290
- err = t .validateBlockHeader (b .BlockHeader , cb .Header ())
291
- if err != nil {
292
- return fmt .Errorf ("Block header validation failed: %v" , err )
292
+
293
+ // validate RLP decoding by checking all values against test file JSON
294
+ if err = t .validateBlockHeader (b .BlockHeader , cb .Header ()); err != nil {
295
+ return fmt .Errorf ("Deserialised block header validation failed: %v" , err )
296
+ }
297
+
298
+ // validate the imported header against test file JSON
299
+
300
+ /*
301
+ TODO: currently test files do not contain information on what
302
+ reorg is expected other than possibly the post state (which may
303
+ or may not depend on a specific chain).
304
+
305
+ discussed with winswega and it was agreed to add this information
306
+ to the test files explicitly.
307
+
308
+ meanwhile we skip header validation on blocks with the same block
309
+ number as a prior block, since this test code cannot know what
310
+ blocks are in the longest chain without making use of the very
311
+ protocol rules the tests verify or rely on the correctness of the
312
+ code that is being tested.
313
+
314
+ */
315
+ if ! blockNums [b .BlockHeader .Number ] {
316
+ importedBlock := chainManager .CurrentBlock ()
317
+ if err = t .validateBlockHeader (b .BlockHeader , importedBlock .Header ()); err != nil {
318
+ return fmt .Errorf ("Imported block header validation failed: %v" , err )
319
+ }
320
+ blockNums [b .BlockHeader .Number ] = true
293
321
}
294
322
}
295
323
return nil
@@ -298,83 +326,84 @@ func (t *BlockTest) TryBlocksInsert(chainManager *core.ChainManager) error {
298
326
func (s * BlockTest ) validateBlockHeader (h * btHeader , h2 * types.Header ) error {
299
327
expectedBloom := mustConvertBytes (h .Bloom )
300
328
if ! bytes .Equal (expectedBloom , h2 .Bloom .Bytes ()) {
301
- return fmt .Errorf ("Bloom: expected : %v, decoded : %v " , expectedBloom , h2 .Bloom .Bytes ())
329
+ return fmt .Errorf ("Bloom: want : %x have : %x " , expectedBloom , h2 .Bloom .Bytes ())
302
330
}
303
331
304
332
expectedCoinbase := mustConvertBytes (h .Coinbase )
305
333
if ! bytes .Equal (expectedCoinbase , h2 .Coinbase .Bytes ()) {
306
- return fmt .Errorf ("Coinbase: expected : %v, decoded : %v " , expectedCoinbase , h2 .Coinbase .Bytes ())
334
+ return fmt .Errorf ("Coinbase: want : %x have : %x " , expectedCoinbase , h2 .Coinbase .Bytes ())
307
335
}
308
336
309
337
expectedMixHashBytes := mustConvertBytes (h .MixHash )
310
338
if ! bytes .Equal (expectedMixHashBytes , h2 .MixDigest .Bytes ()) {
311
- return fmt .Errorf ("MixHash: expected : %v, decoded : %v " , expectedMixHashBytes , h2 .MixDigest .Bytes ())
339
+ return fmt .Errorf ("MixHash: want : %x have : %x " , expectedMixHashBytes , h2 .MixDigest .Bytes ())
312
340
}
313
341
314
342
expectedNonce := mustConvertBytes (h .Nonce )
315
343
if ! bytes .Equal (expectedNonce , h2 .Nonce [:]) {
316
- return fmt .Errorf ("Nonce: expected : %v, decoded : %v " , expectedNonce , h2 .Nonce )
344
+ return fmt .Errorf ("Nonce: want : %x have : %x " , expectedNonce , h2 .Nonce )
317
345
}
318
346
319
347
expectedNumber := mustConvertBigInt (h .Number , 16 )
320
348
if expectedNumber .Cmp (h2 .Number ) != 0 {
321
- return fmt .Errorf ("Number: expected : %v, decoded : %v" , expectedNumber , h2 .Number )
349
+ return fmt .Errorf ("Number: want : %v have : %v" , expectedNumber , h2 .Number )
322
350
}
323
351
324
352
expectedParentHash := mustConvertBytes (h .ParentHash )
325
353
if ! bytes .Equal (expectedParentHash , h2 .ParentHash .Bytes ()) {
326
- return fmt .Errorf ("Parent hash: expected : %v, decoded : %v " , expectedParentHash , h2 .ParentHash .Bytes ())
354
+ return fmt .Errorf ("Parent hash: want : %x have : %x " , expectedParentHash , h2 .ParentHash .Bytes ())
327
355
}
328
356
329
357
expectedReceiptHash := mustConvertBytes (h .ReceiptTrie )
330
358
if ! bytes .Equal (expectedReceiptHash , h2 .ReceiptHash .Bytes ()) {
331
- return fmt .Errorf ("Receipt hash: expected : %v, decoded : %v " , expectedReceiptHash , h2 .ReceiptHash .Bytes ())
359
+ return fmt .Errorf ("Receipt hash: want : %x have : %x " , expectedReceiptHash , h2 .ReceiptHash .Bytes ())
332
360
}
333
361
334
362
expectedTxHash := mustConvertBytes (h .TransactionsTrie )
335
363
if ! bytes .Equal (expectedTxHash , h2 .TxHash .Bytes ()) {
336
- return fmt .Errorf ("Tx hash: expected : %v, decoded : %v " , expectedTxHash , h2 .TxHash .Bytes ())
364
+ return fmt .Errorf ("Tx hash: want : %x have : %x " , expectedTxHash , h2 .TxHash .Bytes ())
337
365
}
338
366
339
367
expectedStateHash := mustConvertBytes (h .StateRoot )
340
368
if ! bytes .Equal (expectedStateHash , h2 .Root .Bytes ()) {
341
- return fmt .Errorf ("State hash: expected : %v, decoded : %v " , expectedStateHash , h2 .Root .Bytes ())
369
+ return fmt .Errorf ("State hash: want : %x have : %x " , expectedStateHash , h2 .Root .Bytes ())
342
370
}
343
371
344
372
expectedUncleHash := mustConvertBytes (h .UncleHash )
345
373
if ! bytes .Equal (expectedUncleHash , h2 .UncleHash .Bytes ()) {
346
- return fmt .Errorf ("Uncle hash: expected : %v, decoded : %v " , expectedUncleHash , h2 .UncleHash .Bytes ())
374
+ return fmt .Errorf ("Uncle hash: want : %x have : %x " , expectedUncleHash , h2 .UncleHash .Bytes ())
347
375
}
348
376
349
377
expectedExtraData := mustConvertBytes (h .ExtraData )
350
378
if ! bytes .Equal (expectedExtraData , h2 .Extra ) {
351
- return fmt .Errorf ("Extra data: expected : %v, decoded : %v " , expectedExtraData , h2 .Extra )
379
+ return fmt .Errorf ("Extra data: want : %x have : %x " , expectedExtraData , h2 .Extra )
352
380
}
353
381
354
382
expectedDifficulty := mustConvertBigInt (h .Difficulty , 16 )
355
383
if expectedDifficulty .Cmp (h2 .Difficulty ) != 0 {
356
- return fmt .Errorf ("Difficulty: expected : %v, decoded : %v" , expectedDifficulty , h2 .Difficulty )
384
+ return fmt .Errorf ("Difficulty: want : %v have : %v" , expectedDifficulty , h2 .Difficulty )
357
385
}
358
386
359
387
expectedGasLimit := mustConvertBigInt (h .GasLimit , 16 )
360
388
if expectedGasLimit .Cmp (h2 .GasLimit ) != 0 {
361
- return fmt .Errorf ("GasLimit: expected : %v, decoded : %v" , expectedGasLimit , h2 .GasLimit )
389
+ return fmt .Errorf ("GasLimit: want : %v have : %v" , expectedGasLimit , h2 .GasLimit )
362
390
}
363
391
expectedGasUsed := mustConvertBigInt (h .GasUsed , 16 )
364
392
if expectedGasUsed .Cmp (h2 .GasUsed ) != 0 {
365
- return fmt .Errorf ("GasUsed: expected : %v, decoded : %v" , expectedGasUsed , h2 .GasUsed )
393
+ return fmt .Errorf ("GasUsed: want : %v have : %v" , expectedGasUsed , h2 .GasUsed )
366
394
}
367
395
368
396
expectedTimestamp := mustConvertBigInt (h .Timestamp , 16 )
369
397
if expectedTimestamp .Cmp (h2 .Time ) != 0 {
370
- return fmt .Errorf ("Timestamp: expected : %v, decoded : %v" , expectedTimestamp , h2 .Time )
398
+ return fmt .Errorf ("Timestamp: want : %v have : %v" , expectedTimestamp , h2 .Time )
371
399
}
372
400
373
401
return nil
374
402
}
375
403
376
404
func (t * BlockTest ) ValidatePostState (statedb * state.StateDB ) error {
377
- for addrString , acct := range t .preAccounts {
405
+ // validate post state accounts in test file against what we have in state db
406
+ for addrString , acct := range t .postAccounts {
378
407
// XXX: is is worth it checking for errors here?
379
408
addr , err := hex .DecodeString (addrString )
380
409
if err != nil {
@@ -398,13 +427,13 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
398
427
balance2 := statedb .GetBalance (common .BytesToAddress (addr ))
399
428
nonce2 := statedb .GetNonce (common .BytesToAddress (addr ))
400
429
if ! bytes .Equal (code2 , code ) {
401
- return fmt .Errorf ("account code mismatch, addr, found, expected: " , addrString , hex .EncodeToString (code2 ), hex .EncodeToString (code ))
430
+ return fmt .Errorf ("account code mismatch for addr: %s want: %s have: %s " , addrString , hex .EncodeToString (code ), hex .EncodeToString (code2 ))
402
431
}
403
432
if balance2 .Cmp (balance ) != 0 {
404
- return fmt .Errorf ("account balance mismatch, addr, found, expected: " , addrString , balance2 , balance )
433
+ return fmt .Errorf ("account balance mismatch for addr: %s, want: %d, have: %d " , addrString , balance , balance2 )
405
434
}
406
435
if nonce2 != nonce {
407
- return fmt .Errorf ("account nonce mismatch, addr, found, expected: " , addrString , nonce2 , nonce )
436
+ return fmt .Errorf ("account nonce mismatch for addr: %s want: %d have: %d " , addrString , nonce , nonce2 )
408
437
}
409
438
}
410
439
return nil
@@ -432,7 +461,7 @@ func convertBlockTest(in *btJSON) (out *BlockTest, err error) {
432
461
err = fmt .Errorf ("%v\n %s" , recovered , buf )
433
462
}
434
463
}()
435
- out = & BlockTest {preAccounts : in .Pre , Json : in }
464
+ out = & BlockTest {preAccounts : in .Pre , postAccounts : in . PostState , Json : in }
436
465
out .Genesis = mustConvertGenesis (in .GenesisBlockHeader )
437
466
return out , err
438
467
}
0 commit comments