@@ -44,8 +44,9 @@ import (
4444type BlockTest struct {
4545 Genesis * types.Block
4646
47- Json * btJSON
48- preAccounts map [string ]btAccount
47+ Json * btJSON
48+ preAccounts map [string ]btAccount
49+ postAccounts map [string ]btAccount
4950}
5051
5152type btJSON struct {
@@ -147,7 +148,6 @@ func runBlockTests(bt map[string]*BlockTest, skipTests []string) error {
147148 glog .Infoln ("Skipping block test" , name )
148149 continue
149150 }
150-
151151 // test the block
152152 if err := runBlockTest (test ); err != nil {
153153 return fmt .Errorf ("%s: %v" , name , err )
@@ -173,7 +173,7 @@ func runBlockTest(test *BlockTest) error {
173173 }
174174
175175 // import pre accounts
176- statedb , err : = test .InsertPreState (ethereum )
176+ _ , err = test .InsertPreState (ethereum )
177177 if err != nil {
178178 return fmt .Errorf ("InsertPreState: %v" , err )
179179 }
@@ -183,7 +183,8 @@ func runBlockTest(test *BlockTest) error {
183183 return err
184184 }
185185
186- if err = test .ValidatePostState (statedb ); err != nil {
186+ newDB := ethereum .ChainManager ().State ()
187+ if err = test .ValidatePostState (newDB ); err != nil {
187188 return fmt .Errorf ("post state validation failed: %v" , err )
188189 }
189190 return nil
@@ -265,6 +266,7 @@ func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, erro
265266 post state.
266267*/
267268func (t * BlockTest ) TryBlocksInsert (chainManager * core.ChainManager ) error {
269+ blockNums := make (map [string ]bool )
268270 // insert the test blocks, which will execute all transactions
269271 for _ , b := range t .Json .Blocks {
270272 cb , err := mustConvertBlock (b )
@@ -287,9 +289,35 @@ func (t *BlockTest) TryBlocksInsert(chainManager *core.ChainManager) error {
287289 if b .BlockHeader == nil {
288290 return fmt .Errorf ("Block insertion should have failed" )
289291 }
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
293321 }
294322 }
295323 return nil
@@ -298,83 +326,84 @@ func (t *BlockTest) TryBlocksInsert(chainManager *core.ChainManager) error {
298326func (s * BlockTest ) validateBlockHeader (h * btHeader , h2 * types.Header ) error {
299327 expectedBloom := mustConvertBytes (h .Bloom )
300328 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 ())
302330 }
303331
304332 expectedCoinbase := mustConvertBytes (h .Coinbase )
305333 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 ())
307335 }
308336
309337 expectedMixHashBytes := mustConvertBytes (h .MixHash )
310338 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 ())
312340 }
313341
314342 expectedNonce := mustConvertBytes (h .Nonce )
315343 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 )
317345 }
318346
319347 expectedNumber := mustConvertBigInt (h .Number , 16 )
320348 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 )
322350 }
323351
324352 expectedParentHash := mustConvertBytes (h .ParentHash )
325353 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 ())
327355 }
328356
329357 expectedReceiptHash := mustConvertBytes (h .ReceiptTrie )
330358 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 ())
332360 }
333361
334362 expectedTxHash := mustConvertBytes (h .TransactionsTrie )
335363 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 ())
337365 }
338366
339367 expectedStateHash := mustConvertBytes (h .StateRoot )
340368 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 ())
342370 }
343371
344372 expectedUncleHash := mustConvertBytes (h .UncleHash )
345373 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 ())
347375 }
348376
349377 expectedExtraData := mustConvertBytes (h .ExtraData )
350378 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 )
352380 }
353381
354382 expectedDifficulty := mustConvertBigInt (h .Difficulty , 16 )
355383 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 )
357385 }
358386
359387 expectedGasLimit := mustConvertBigInt (h .GasLimit , 16 )
360388 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 )
362390 }
363391 expectedGasUsed := mustConvertBigInt (h .GasUsed , 16 )
364392 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 )
366394 }
367395
368396 expectedTimestamp := mustConvertBigInt (h .Timestamp , 16 )
369397 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 )
371399 }
372400
373401 return nil
374402}
375403
376404func (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 {
378407 // XXX: is is worth it checking for errors here?
379408 addr , err := hex .DecodeString (addrString )
380409 if err != nil {
@@ -398,13 +427,13 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
398427 balance2 := statedb .GetBalance (common .BytesToAddress (addr ))
399428 nonce2 := statedb .GetNonce (common .BytesToAddress (addr ))
400429 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 ))
402431 }
403432 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 )
405434 }
406435 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 )
408437 }
409438 }
410439 return nil
@@ -432,7 +461,7 @@ func convertBlockTest(in *btJSON) (out *BlockTest, err error) {
432461 err = fmt .Errorf ("%v\n %s" , recovered , buf )
433462 }
434463 }()
435- out = & BlockTest {preAccounts : in .Pre , Json : in }
464+ out = & BlockTest {preAccounts : in .Pre , postAccounts : in . PostState , Json : in }
436465 out .Genesis = mustConvertGenesis (in .GenesisBlockHeader )
437466 return out , err
438467}
0 commit comments