@@ -19,13 +19,27 @@ import (
1919)
2020
2121// Block Test JSON Format
22+ type BlockTest struct {
23+ Genesis * types.Block
24+
25+ Json * btJSON
26+ preAccounts map [string ]btAccount
27+ }
28+
2229type btJSON struct {
2330 Blocks []btBlock
2431 GenesisBlockHeader btHeader
2532 Pre map [string ]btAccount
2633 PostState map [string ]btAccount
2734}
2835
36+ type btBlock struct {
37+ BlockHeader * btHeader
38+ Rlp string
39+ Transactions []btTransaction
40+ UncleHeaders []* btHeader
41+ }
42+
2943type btAccount struct {
3044 Balance string
3145 Code string
@@ -65,20 +79,6 @@ type btTransaction struct {
6579 Value string
6680}
6781
68- type btBlock struct {
69- BlockHeader * btHeader
70- Rlp string
71- Transactions []btTransaction
72- UncleHeaders []* btHeader
73- }
74-
75- type BlockTest struct {
76- Genesis * types.Block
77-
78- json * btJSON
79- preAccounts map [string ]btAccount
80- }
81-
8282// LoadBlockTests loads a block test JSON file.
8383func LoadBlockTests (file string ) (map [string ]* BlockTest , error ) {
8484 bt := make (map [string ]* btJSON )
@@ -125,13 +125,43 @@ func (t *BlockTest) InsertPreState(db common.Database) (*state.StateDB, error) {
125125 return statedb , nil
126126}
127127
128- // InsertBlocks loads the test's blocks into the given chain.
129- func (t * BlockTest ) InsertBlocks (chain * core.ChainManager ) error {
130- blocks , err := t .convertBlocks ()
131- if err != nil {
132- return err
128+ /* See https://github.com/ethereum/tests/wiki/Blockchain-Tests-II
129+
130+ Whether a block is valid or not is a bit subtle, it's defined by presence of
131+ blockHeader, transactions and uncleHeaders fields. If they are missing, the block is
132+ invalid and we must verify that we do not accept it.
133+
134+ Since some tests mix valid and invalid blocks we need to check this for every block.
135+
136+ If a block is invalid it does not necessarily fail the test, if it's invalidness is
137+ expected we are expected to ignore it and continue processing and then validate the
138+ post state.
139+ */
140+ func (t * BlockTest ) TryBlocksInsert (chainManager * core.ChainManager ) error {
141+ // insert the test blocks, which will execute all transactions
142+ for _ , b := range t .Json .Blocks {
143+ cb , err := mustConvertBlock (b )
144+ if err != nil {
145+ if b .BlockHeader == nil {
146+ continue // OK - block is supposed to be invalid, continue with next block
147+ } else {
148+ return fmt .Errorf ("Block RLP decoding failed when expected to succeed: " , err )
149+ }
150+ }
151+ // RLP decoding worked, try to insert into chain:
152+ err = chainManager .InsertChain (types.Blocks {cb })
153+ if err != nil {
154+ if b .BlockHeader == nil {
155+ continue // OK - block is supposed to be invalid, continue with next block
156+ } else {
157+ return fmt .Errorf ("Block insertion into chain failed: " , err )
158+ }
159+ }
160+ if b .BlockHeader == nil {
161+ return fmt .Errorf ("Block insertion should have failed" )
162+ }
133163 }
134- return chain . InsertChain ( blocks )
164+ return nil
135165}
136166
137167func (t * BlockTest ) ValidatePostState (statedb * state.StateDB ) error {
@@ -159,21 +189,6 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
159189 return nil
160190}
161191
162- func (t * BlockTest ) convertBlocks () (blocks []* types.Block , err error ) {
163- // the conversion handles errors by catching panics.
164- // you might consider this ugly, but the alternative (passing errors)
165- // would be much harder to read.
166- defer func () {
167- if recovered := recover (); recovered != nil {
168- buf := make ([]byte , 64 << 10 )
169- buf = buf [:runtime .Stack (buf , false )]
170- err = fmt .Errorf ("%v\n %s" , recovered , buf )
171- }
172- }()
173- blocks = mustConvertBlocks (t .json .Blocks )
174- return blocks , nil
175- }
176-
177192func convertTest (in * btJSON ) (out * BlockTest , err error ) {
178193 // the conversion handles errors by catching panics.
179194 // you might consider this ugly, but the alternative (passing errors)
@@ -185,7 +200,7 @@ func convertTest(in *btJSON) (out *BlockTest, err error) {
185200 err = fmt .Errorf ("%v\n %s" , recovered , buf )
186201 }
187202 }()
188- out = & BlockTest {preAccounts : in .Pre , json : in }
203+ out = & BlockTest {preAccounts : in .Pre , Json : in }
189204 out .Genesis = mustConvertGenesis (in .GenesisBlockHeader )
190205 return out , err
191206}
@@ -221,17 +236,11 @@ func mustConvertHeader(in btHeader) *types.Header {
221236 return header
222237}
223238
224- func mustConvertBlocks (testBlocks []btBlock ) []* types.Block {
225- var out []* types.Block
226- for i , inb := range testBlocks {
227- var b types.Block
228- r := bytes .NewReader (mustConvertBytes (inb .Rlp ))
229- if err := rlp .Decode (r , & b ); err != nil {
230- panic (fmt .Errorf ("invalid block %d: %q\n error: %v" , i , inb .Rlp , err ))
231- }
232- out = append (out , & b )
233- }
234- return out
239+ func mustConvertBlock (testBlock btBlock ) (* types.Block , error ) {
240+ var b types.Block
241+ r := bytes .NewReader (mustConvertBytes (testBlock .Rlp ))
242+ err := rlp .Decode (r , & b )
243+ return & b , err
235244}
236245
237246func mustConvertBytes (in string ) []byte {
0 commit comments