@@ -2214,7 +2214,15 @@ func GenerateHeaders() (generator *testGenerator, tests [][]TestInstance) {
2214
2214
blockHeight := g .blockHeights [blockName ]
2215
2215
return RejectedHeader {blockName , blockHeader , blockHeight , code }
2216
2216
}
2217
-
2217
+ acceptBlock := func (blockName string , block * wire.MsgBlock , isMainChain , isOrphan bool ) TestInstance {
2218
+ blockHeight := g .blockHeights [blockName ]
2219
+ return AcceptedBlock {blockName , block , blockHeight , isMainChain ,
2220
+ isOrphan }
2221
+ }
2222
+ rejectBlock := func (blockName string , block * wire.MsgBlock , code blockchain.ErrorCode ) TestInstance {
2223
+ blockHeight := g .blockHeights [blockName ]
2224
+ return RejectedBlock {blockName , block , blockHeight , code }
2225
+ }
2218
2226
// Define some convenience helper functions to populate the tests slice
2219
2227
// with test instances that have the described characteristics.
2220
2228
//
@@ -2224,6 +2232,13 @@ func GenerateHeaders() (generator *testGenerator, tests [][]TestInstance) {
2224
2232
//
2225
2233
// rejected creates and appends a single rejectHeader test instance for
2226
2234
// the current tip.
2235
+ //
2236
+ // acceptedBlock creates and appends a single acceptBlock test instance for
2237
+ // the current tip which expects the block to be accepted to the main
2238
+ // chain.
2239
+ //
2240
+ // rejectedBlock creates and appends a single rejectBlock test instance for
2241
+ // the current tip.
2227
2242
accepted := func () {
2228
2243
tests = append (tests , []TestInstance {
2229
2244
acceptHeader (g .tipName , g .tip ),
@@ -2234,6 +2249,16 @@ func GenerateHeaders() (generator *testGenerator, tests [][]TestInstance) {
2234
2249
rejectHeader (g .tipName , g .tip , code ),
2235
2250
})
2236
2251
}
2252
+ acceptedBlock := func () {
2253
+ tests = append (tests , []TestInstance {
2254
+ acceptBlock (g .tipName , g .tip , true , false ),
2255
+ })
2256
+ }
2257
+ rejectedBlock := func (code blockchain.ErrorCode ) {
2258
+ tests = append (tests , []TestInstance {
2259
+ rejectBlock (g .tipName , g .tip , code ),
2260
+ })
2261
+ }
2237
2262
// ---------------------------------------------------------------------
2238
2263
// Generate enough blocks to have mature coinbase outputs to work with.
2239
2264
//
@@ -2317,11 +2342,79 @@ func GenerateHeaders() (generator *testGenerator, tests [][]TestInstance) {
2317
2342
g .updateBlockState ("b3" , origHash , "b3" , b3 )
2318
2343
}
2319
2344
rejected (blockchain .ErrUnexpectedDifficulty )
2320
- // Adding a block with valid header
2345
+ // Adding a block with valid header but invalid spend
2321
2346
//
2322
- // ... -> b0() -> b4(1 )
2347
+ // ... -> b0() -> b4(2 )
2323
2348
g .setTip ("b0" )
2324
- g .nextBlock ("b4" , outs [1 ])
2349
+ g .nextBlock ("b4" , outs [2 ])
2325
2350
accepted ()
2351
+ // Adding a block with valid header and valid spend, but invalid parent
2352
+ //
2353
+ // ... -> b0() -> b5(1)
2354
+ g .nextBlock ("b5" , outs [1 ])
2355
+ accepted ()
2356
+ // Adding a block with valid header and valid spend and valid parent
2357
+ //
2358
+ // ... -> b0() -> b6(1)
2359
+ g .setTip ("b0" )
2360
+ g .nextBlock ("b6" , outs [1 ])
2361
+ // Accepting/Rejecting the blocks for the headers that were
2362
+ // accepted/rejected
2363
+ testInstances = make ([]TestInstance , 0 )
2364
+ for i := uint16 (0 ); i < coinbaseMaturity ; i ++ {
2365
+ blockName := fmt .Sprintf ("bm%d" , i )
2366
+ g .setTip (blockName )
2367
+ testInstances = append (testInstances , acceptBlock (g .tipName ,
2368
+ g .tip , true , false ))
2369
+ }
2370
+ tests = append (tests , testInstances )
2371
+ // Accepting the block b0
2372
+ //
2373
+ // ... -> b0
2374
+ g .setTip ("b0" )
2375
+ acceptedBlock ()
2376
+ // Rejecting the block data for b1 because of high hash
2377
+ // ... -> b0()
2378
+ // \-> b1(1)
2379
+ g .setTip ("b1" )
2380
+ rejectedBlock (blockchain .ErrHighHash )
2381
+ // Acccept the block as orphan
2382
+ //
2383
+ // -> b1a(1)
2384
+ g .setTip ("b1a" )
2385
+ tests = append (tests , []TestInstance {
2386
+ acceptBlock (g .tipName , g .tip , false , true ),
2387
+ })
2388
+ // Reject the block with invalid proof of work
2389
+ //
2390
+ // ... -> b0()
2391
+ // \-> b2(1)
2392
+ g .setTip ("b2" )
2393
+ rejectedBlock (blockchain .ErrUnexpectedDifficulty )
2394
+ // Reject the block with invalid negative proof of work
2395
+ //
2396
+ // ... -> b0()
2397
+ // \-> b3(1)
2398
+ g .setTip ("b3" )
2399
+ rejectedBlock (blockchain .ErrUnexpectedDifficulty )
2400
+ // Rejecting the block with valid header but invalid spend
2401
+ //
2402
+ // ... -> b0()
2403
+ // \-> b4(2)
2404
+ g .setTip ("b4" )
2405
+ rejectedBlock (blockchain .ErrImmatureSpend )
2406
+ // Since the block is rejected, so rejecting the header also
2407
+ // which was earlier accepted.
2408
+ rejected (blockchain .ErrKnownInvalidBlock )
2409
+ // Rejecting a block with valid header and valid spend, but invalid parent
2410
+ //
2411
+ // -> b4(2) -> b5(1)
2412
+ g .setTip ("b5" )
2413
+ rejectedBlock (blockchain .ErrInvalidAncestorBlock )
2414
+ // Accepting the block
2415
+ //
2416
+ // ... -> b0() -> b6(1)
2417
+ g .setTip ("b6" )
2418
+ acceptedBlock ()
2326
2419
return & g , tests
2327
2420
}
0 commit comments