45
45
testBankAddress = crypto .PubkeyToAddress (testBankKey .PublicKey )
46
46
testBankFunds = big .NewInt (1000000000000000000 )
47
47
48
- acc1Key , _ = crypto .GenerateKey ()
49
- acc1Addr = crypto .PubkeyToAddress (acc1Key .PublicKey )
48
+ testUserKey , _ = crypto .GenerateKey ()
49
+ testUserAddress = crypto .PubkeyToAddress (testUserKey .PublicKey )
50
50
51
51
// Test transactions
52
52
pendingTxs []* types.Transaction
@@ -62,9 +62,9 @@ func init() {
62
62
Period : 10 ,
63
63
Epoch : 30000 ,
64
64
}
65
- tx1 , _ := types .SignTx (types .NewTransaction (0 , acc1Addr , big .NewInt (1000 ), params .TxGas , nil , nil ), types.HomesteadSigner {}, testBankKey )
65
+ tx1 , _ := types .SignTx (types .NewTransaction (0 , testUserAddress , big .NewInt (1000 ), params .TxGas , nil , nil ), types.HomesteadSigner {}, testBankKey )
66
66
pendingTxs = append (pendingTxs , tx1 )
67
- tx2 , _ := types .SignTx (types .NewTransaction (1 , acc1Addr , big .NewInt (1000 ), params .TxGas , nil , nil ), types.HomesteadSigner {}, testBankKey )
67
+ tx2 , _ := types .SignTx (types .NewTransaction (1 , testUserAddress , big .NewInt (1000 ), params .TxGas , nil , nil ), types.HomesteadSigner {}, testBankKey )
68
68
newTxs = append (newTxs , tx2 )
69
69
}
70
70
@@ -77,7 +77,7 @@ type testWorkerBackend struct {
77
77
uncleBlock * types.Block
78
78
}
79
79
80
- func newTestWorkerBackend (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine ) * testWorkerBackend {
80
+ func newTestWorkerBackend (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine , n int ) * testWorkerBackend {
81
81
var (
82
82
db = ethdb .NewMemDatabase ()
83
83
gspec = core.Genesis {
@@ -92,14 +92,28 @@ func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine
92
92
copy (gspec .ExtraData [32 :], testBankAddress [:])
93
93
case * ethash.Ethash :
94
94
default :
95
- t .Fatal ( "unexpect consensus engine type" )
95
+ t .Fatalf ( "unexpected consensus engine type: %T" , engine )
96
96
}
97
97
genesis := gspec .MustCommit (db )
98
98
99
99
chain , _ := core .NewBlockChain (db , nil , gspec .Config , engine , vm.Config {})
100
100
txpool := core .NewTxPool (testTxPoolConfig , chainConfig , chain )
101
- blocks , _ := core .GenerateChain (chainConfig , genesis , engine , db , 1 , func (i int , gen * core.BlockGen ) {
102
- gen .SetCoinbase (acc1Addr )
101
+
102
+ // Generate a small n-block chain and an uncle block for it
103
+ if n > 0 {
104
+ blocks , _ := core .GenerateChain (chainConfig , genesis , engine , db , n , func (i int , gen * core.BlockGen ) {
105
+ gen .SetCoinbase (testBankAddress )
106
+ })
107
+ if _ , err := chain .InsertChain (blocks ); err != nil {
108
+ t .Fatalf ("failed to insert origin chain: %v" , err )
109
+ }
110
+ }
111
+ parent := genesis
112
+ if n > 0 {
113
+ parent = chain .GetBlockByHash (chain .CurrentBlock ().ParentHash ())
114
+ }
115
+ blocks , _ := core .GenerateChain (chainConfig , parent , engine , db , 1 , func (i int , gen * core.BlockGen ) {
116
+ gen .SetCoinbase (testUserAddress )
103
117
})
104
118
105
119
return & testWorkerBackend {
@@ -116,8 +130,8 @@ func (b *testWorkerBackend) PostChainEvents(events []interface{}) {
116
130
b .chain .PostChainEvents (events , nil )
117
131
}
118
132
119
- func newTestWorker (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine ) (* worker , * testWorkerBackend ) {
120
- backend := newTestWorkerBackend (t , chainConfig , engine )
133
+ func newTestWorker (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine , blocks int ) (* worker , * testWorkerBackend ) {
134
+ backend := newTestWorkerBackend (t , chainConfig , engine , blocks )
121
135
backend .txPool .AddLocals (pendingTxs )
122
136
w := newWorker (chainConfig , engine , backend , new (event.TypeMux ), time .Second , params .GenesisGasLimit , params .GenesisGasLimit )
123
137
w .setEtherbase (testBankAddress )
@@ -134,24 +148,24 @@ func TestPendingStateAndBlockClique(t *testing.T) {
134
148
func testPendingStateAndBlock (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine ) {
135
149
defer engine .Close ()
136
150
137
- w , b := newTestWorker (t , chainConfig , engine )
151
+ w , b := newTestWorker (t , chainConfig , engine , 0 )
138
152
defer w .close ()
139
153
140
154
// Ensure snapshot has been updated.
141
155
time .Sleep (100 * time .Millisecond )
142
156
block , state := w .pending ()
143
157
if block .NumberU64 () != 1 {
144
- t .Errorf ("block number mismatch, has %d, want %d" , block .NumberU64 (), 1 )
158
+ t .Errorf ("block number mismatch: have %d, want %d" , block .NumberU64 (), 1 )
145
159
}
146
- if balance := state .GetBalance (acc1Addr ); balance .Cmp (big .NewInt (1000 )) != 0 {
147
- t .Errorf ("account balance mismatch, has %d, want %d" , balance , 1000 )
160
+ if balance := state .GetBalance (testUserAddress ); balance .Cmp (big .NewInt (1000 )) != 0 {
161
+ t .Errorf ("account balance mismatch: have %d, want %d" , balance , 1000 )
148
162
}
149
163
b .txPool .AddLocals (newTxs )
150
164
// Ensure the new tx events has been processed
151
165
time .Sleep (100 * time .Millisecond )
152
166
block , state = w .pending ()
153
- if balance := state .GetBalance (acc1Addr ); balance .Cmp (big .NewInt (2000 )) != 0 {
154
- t .Errorf ("account balance mismatch, has %d, want %d" , balance , 2000 )
167
+ if balance := state .GetBalance (testUserAddress ); balance .Cmp (big .NewInt (2000 )) != 0 {
168
+ t .Errorf ("account balance mismatch: have %d, want %d" , balance , 2000 )
155
169
}
156
170
}
157
171
@@ -165,7 +179,7 @@ func TestEmptyWorkClique(t *testing.T) {
165
179
func testEmptyWork (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine ) {
166
180
defer engine .Close ()
167
181
168
- w , _ := newTestWorker (t , chainConfig , engine )
182
+ w , _ := newTestWorker (t , chainConfig , engine , 0 )
169
183
defer w .close ()
170
184
171
185
var (
@@ -179,10 +193,10 @@ func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consens
179
193
receiptLen , balance = 1 , big .NewInt (1000 )
180
194
}
181
195
if len (task .receipts ) != receiptLen {
182
- t .Errorf ("receipt number mismatch has %d, want %d" , len (task .receipts ), receiptLen )
196
+ t .Errorf ("receipt number mismatch: have %d, want %d" , len (task .receipts ), receiptLen )
183
197
}
184
- if task .state .GetBalance (acc1Addr ).Cmp (balance ) != 0 {
185
- t .Errorf ("account balance mismatch has %d, want %d" , task .state .GetBalance (acc1Addr ), balance )
198
+ if task .state .GetBalance (testUserAddress ).Cmp (balance ) != 0 {
199
+ t .Errorf ("account balance mismatch: have %d, want %d" , task .state .GetBalance (testUserAddress ), balance )
186
200
}
187
201
}
188
202
@@ -219,19 +233,19 @@ func TestStreamUncleBlock(t *testing.T) {
219
233
ethash := ethash .NewFaker ()
220
234
defer ethash .Close ()
221
235
222
- w , b := newTestWorker (t , ethashChainConfig , ethash )
236
+ w , b := newTestWorker (t , ethashChainConfig , ethash , 1 )
223
237
defer w .close ()
224
238
225
239
var taskCh = make (chan struct {})
226
240
227
241
taskIndex := 0
228
242
w .newTaskHook = func (task * task ) {
229
- if task .block .NumberU64 () == 1 {
243
+ if task .block .NumberU64 () == 2 {
230
244
if taskIndex == 2 {
231
- has := task .block .Header ().UncleHash
245
+ have := task .block .Header ().UncleHash
232
246
want := types .CalcUncleHash ([]* types.Header {b .uncleBlock .Header ()})
233
- if has != want {
234
- t .Errorf ("uncle hash mismatch, has %s, want %s" , has .Hex (), want .Hex ())
247
+ if have != want {
248
+ t .Errorf ("uncle hash mismatch: have %s, want %s" , have .Hex (), want .Hex ())
235
249
}
236
250
}
237
251
taskCh <- struct {}{}
@@ -248,12 +262,12 @@ func TestStreamUncleBlock(t *testing.T) {
248
262
// Ensure worker has finished initialization
249
263
for {
250
264
b := w .pendingBlock ()
251
- if b != nil && b .NumberU64 () == 1 {
265
+ if b != nil && b .NumberU64 () == 2 {
252
266
break
253
267
}
254
268
}
255
-
256
269
w .start ()
270
+
257
271
// Ignore the first two works
258
272
for i := 0 ; i < 2 ; i += 1 {
259
273
select {
@@ -282,7 +296,7 @@ func TestRegenerateMiningBlockClique(t *testing.T) {
282
296
func testRegenerateMiningBlock (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine ) {
283
297
defer engine .Close ()
284
298
285
- w , b := newTestWorker (t , chainConfig , engine )
299
+ w , b := newTestWorker (t , chainConfig , engine , 0 )
286
300
defer w .close ()
287
301
288
302
var taskCh = make (chan struct {})
@@ -293,10 +307,10 @@ func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, en
293
307
if taskIndex == 2 {
294
308
receiptLen , balance := 2 , big .NewInt (2000 )
295
309
if len (task .receipts ) != receiptLen {
296
- t .Errorf ("receipt number mismatch has %d, want %d" , len (task .receipts ), receiptLen )
310
+ t .Errorf ("receipt number mismatch: have %d, want %d" , len (task .receipts ), receiptLen )
297
311
}
298
- if task .state .GetBalance (acc1Addr ).Cmp (balance ) != 0 {
299
- t .Errorf ("account balance mismatch has %d, want %d" , task .state .GetBalance (acc1Addr ), balance )
312
+ if task .state .GetBalance (testUserAddress ).Cmp (balance ) != 0 {
313
+ t .Errorf ("account balance mismatch: have %d, want %d" , task .state .GetBalance (testUserAddress ), balance )
300
314
}
301
315
}
302
316
taskCh <- struct {}{}
@@ -347,7 +361,7 @@ func TestAdjustIntervalClique(t *testing.T) {
347
361
func testAdjustInterval (t * testing.T , chainConfig * params.ChainConfig , engine consensus.Engine ) {
348
362
defer engine .Close ()
349
363
350
- w , _ := newTestWorker (t , chainConfig , engine )
364
+ w , _ := newTestWorker (t , chainConfig , engine , 0 )
351
365
defer w .close ()
352
366
353
367
w .skipSealHook = func (task * task ) bool {
@@ -387,10 +401,10 @@ func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine co
387
401
388
402
// Check interval
389
403
if minInterval != wantMinInterval {
390
- t .Errorf ("resubmit min interval mismatch want %s has %s " , wantMinInterval , minInterval )
404
+ t .Errorf ("resubmit min interval mismatch: have %v, want %v " , minInterval , wantMinInterval )
391
405
}
392
406
if recommitInterval != wantRecommitInterval {
393
- t .Errorf ("resubmit interval mismatch want %s has %s " , wantRecommitInterval , recommitInterval )
407
+ t .Errorf ("resubmit interval mismatch: have %v, want %v " , recommitInterval , wantRecommitInterval )
394
408
}
395
409
result = append (result , float64 (recommitInterval .Nanoseconds ()))
396
410
index += 1
0 commit comments