github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/miner/worker_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package miner 18 19 import ( 20 "crypto/rand" 21 "errors" 22 "math/big" 23 "sync/atomic" 24 "testing" 25 "time" 26 27 "github.com/tacshi/go-ethereum/accounts" 28 "github.com/tacshi/go-ethereum/common" 29 "github.com/tacshi/go-ethereum/consensus" 30 "github.com/tacshi/go-ethereum/consensus/clique" 31 "github.com/tacshi/go-ethereum/consensus/ethash" 32 "github.com/tacshi/go-ethereum/core" 33 "github.com/tacshi/go-ethereum/core/rawdb" 34 "github.com/tacshi/go-ethereum/core/state" 35 "github.com/tacshi/go-ethereum/core/txpool" 36 "github.com/tacshi/go-ethereum/core/types" 37 "github.com/tacshi/go-ethereum/core/vm" 38 "github.com/tacshi/go-ethereum/crypto" 39 "github.com/tacshi/go-ethereum/ethdb" 40 "github.com/tacshi/go-ethereum/event" 41 "github.com/tacshi/go-ethereum/params" 42 ) 43 44 const ( 45 // testCode is the testing contract binary code which will initialises some 46 // variables in constructor 47 testCode = "0x60806040527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0060005534801561003457600080fd5b5060fc806100436000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80630c4dae8814603757806398a213cf146053575b600080fd5b603d607e565b6040518082815260200191505060405180910390f35b607c60048036036020811015606757600080fd5b81019080803590602001909291905050506084565b005b60005481565b806000819055507fe9e44f9f7da8c559de847a3232b57364adc0354f15a2cd8dc636d54396f9587a6000546040518082815260200191505060405180910390a15056fea265627a7a723058208ae31d9424f2d0bc2a3da1a5dd659db2d71ec322a17db8f87e19e209e3a1ff4a64736f6c634300050a0032" 48 49 // testGas is the gas required for contract deployment. 50 testGas = 144109 51 ) 52 53 var ( 54 // Test chain configurations 55 testTxPoolConfig txpool.Config 56 ethashChainConfig *params.ChainConfig 57 cliqueChainConfig *params.ChainConfig 58 59 // Test accounts 60 testBankKey, _ = crypto.GenerateKey() 61 testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) 62 testBankFunds = big.NewInt(1000000000000000000) 63 64 testUserKey, _ = crypto.GenerateKey() 65 testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey) 66 67 // Test transactions 68 pendingTxs []*types.Transaction 69 newTxs []*types.Transaction 70 71 testConfig = &Config{ 72 Recommit: time.Second, 73 GasCeil: params.GenesisGasLimit, 74 } 75 ) 76 77 func init() { 78 testTxPoolConfig = txpool.DefaultConfig 79 testTxPoolConfig.Journal = "" 80 ethashChainConfig = new(params.ChainConfig) 81 *ethashChainConfig = *params.TestChainConfig 82 cliqueChainConfig = new(params.ChainConfig) 83 *cliqueChainConfig = *params.TestChainConfig 84 cliqueChainConfig.Clique = ¶ms.CliqueConfig{ 85 Period: 10, 86 Epoch: 30000, 87 } 88 89 signer := types.LatestSigner(params.TestChainConfig) 90 tx1 := types.MustSignNewTx(testBankKey, signer, &types.AccessListTx{ 91 ChainID: params.TestChainConfig.ChainID, 92 Nonce: 0, 93 To: &testUserAddress, 94 Value: big.NewInt(1000), 95 Gas: params.TxGas, 96 GasPrice: big.NewInt(params.InitialBaseFee), 97 }) 98 pendingTxs = append(pendingTxs, tx1) 99 100 tx2 := types.MustSignNewTx(testBankKey, signer, &types.LegacyTx{ 101 Nonce: 1, 102 To: &testUserAddress, 103 Value: big.NewInt(1000), 104 Gas: params.TxGas, 105 GasPrice: big.NewInt(params.InitialBaseFee), 106 }) 107 newTxs = append(newTxs, tx2) 108 } 109 110 // testWorkerBackend implements worker.Backend interfaces and wraps all information needed during the testing. 111 type testWorkerBackend struct { 112 db ethdb.Database 113 txPool *txpool.TxPool 114 chain *core.BlockChain 115 genesis *core.Genesis 116 uncleBlock *types.Block 117 } 118 119 func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend { 120 var gspec = &core.Genesis{ 121 Config: chainConfig, 122 Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}, 123 } 124 switch e := engine.(type) { 125 case *clique.Clique: 126 gspec.ExtraData = make([]byte, 32+common.AddressLength+crypto.SignatureLength) 127 copy(gspec.ExtraData[32:32+common.AddressLength], testBankAddress.Bytes()) 128 e.Authorize(testBankAddress, func(account accounts.Account, s string, data []byte) ([]byte, error) { 129 return crypto.Sign(crypto.Keccak256(data), testBankKey) 130 }) 131 case *ethash.Ethash: 132 default: 133 t.Fatalf("unexpected consensus engine type: %T", engine) 134 } 135 chain, err := core.NewBlockChain(db, &core.CacheConfig{TrieDirtyDisabled: true, TriesInMemory: 128}, nil, gspec, nil, engine, vm.Config{}, nil, nil) 136 if err != nil { 137 t.Fatalf("core.NewBlockChain failed: %v", err) 138 } 139 txpool := txpool.NewTxPool(testTxPoolConfig, chainConfig, chain) 140 141 // Generate a small n-block chain and an uncle block for it 142 var uncle *types.Block 143 if n > 0 { 144 genDb, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, n, func(i int, gen *core.BlockGen) { 145 gen.SetCoinbase(testBankAddress) 146 }) 147 if _, err := chain.InsertChain(blocks); err != nil { 148 t.Fatalf("failed to insert origin chain: %v", err) 149 } 150 parent := chain.GetBlockByHash(chain.CurrentBlock().ParentHash) 151 blocks, _ = core.GenerateChain(chainConfig, parent, engine, genDb, 1, func(i int, gen *core.BlockGen) { 152 gen.SetCoinbase(testUserAddress) 153 }) 154 uncle = blocks[0] 155 } else { 156 _, blocks, _ := core.GenerateChainWithGenesis(gspec, engine, 1, func(i int, gen *core.BlockGen) { 157 gen.SetCoinbase(testUserAddress) 158 }) 159 uncle = blocks[0] 160 } 161 return &testWorkerBackend{ 162 db: db, 163 chain: chain, 164 txPool: txpool, 165 genesis: gspec, 166 uncleBlock: uncle, 167 } 168 } 169 170 func (b *testWorkerBackend) BlockChain() *core.BlockChain { return b.chain } 171 func (b *testWorkerBackend) TxPool() *txpool.TxPool { return b.txPool } 172 func (b *testWorkerBackend) StateAtBlock(block *types.Block, reexec uint64, base *state.StateDB, checkLive bool, preferDisk bool) (statedb *state.StateDB, err error) { 173 return nil, errors.New("not supported") 174 } 175 176 func (b *testWorkerBackend) newRandomUncle() *types.Block { 177 var parent *types.Block 178 cur := b.chain.CurrentBlock() 179 if cur.Number.Uint64() == 0 { 180 parent = b.chain.Genesis() 181 } else { 182 parent = b.chain.GetBlockByHash(b.chain.CurrentBlock().ParentHash) 183 } 184 blocks, _ := core.GenerateChain(b.chain.Config(), parent, b.chain.Engine(), b.db, 1, func(i int, gen *core.BlockGen) { 185 var addr = make([]byte, common.AddressLength) 186 rand.Read(addr) 187 gen.SetCoinbase(common.BytesToAddress(addr)) 188 }) 189 return blocks[0] 190 } 191 192 func (b *testWorkerBackend) newRandomTx(creation bool) *types.Transaction { 193 var tx *types.Transaction 194 gasPrice := big.NewInt(10 * params.InitialBaseFee) 195 if creation { 196 tx, _ = types.SignTx(types.NewContractCreation(b.txPool.Nonce(testBankAddress), big.NewInt(0), testGas, gasPrice, common.FromHex(testCode)), types.HomesteadSigner{}, testBankKey) 197 } else { 198 tx, _ = types.SignTx(types.NewTransaction(b.txPool.Nonce(testBankAddress), testUserAddress, big.NewInt(1000), params.TxGas, gasPrice, nil), types.HomesteadSigner{}, testBankKey) 199 } 200 return tx 201 } 202 203 func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, blocks int) (*worker, *testWorkerBackend) { 204 backend := newTestWorkerBackend(t, chainConfig, engine, db, blocks) 205 backend.txPool.AddLocals(pendingTxs) 206 w := newWorker(testConfig, chainConfig, engine, backend, new(event.TypeMux), nil, false) 207 w.setEtherbase(testBankAddress) 208 return w, backend 209 } 210 211 func TestGenerateBlockAndImportEthash(t *testing.T) { 212 testGenerateBlockAndImport(t, false) 213 } 214 215 func TestGenerateBlockAndImportClique(t *testing.T) { 216 testGenerateBlockAndImport(t, true) 217 } 218 219 func testGenerateBlockAndImport(t *testing.T, isClique bool) { 220 var ( 221 engine consensus.Engine 222 chainConfig params.ChainConfig 223 db = rawdb.NewMemoryDatabase() 224 ) 225 if isClique { 226 chainConfig = *params.AllCliqueProtocolChanges 227 chainConfig.Clique = ¶ms.CliqueConfig{Period: 1, Epoch: 30000} 228 engine = clique.New(chainConfig.Clique, db) 229 } else { 230 chainConfig = *params.AllEthashProtocolChanges 231 engine = ethash.NewFaker() 232 } 233 w, b := newTestWorker(t, &chainConfig, engine, db, 0) 234 defer w.close() 235 236 // This test chain imports the mined blocks. 237 chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), nil, nil, b.genesis, nil, engine, vm.Config{}, nil, nil) 238 defer chain.Stop() 239 240 // Ignore empty commit here for less noise. 241 w.skipSealHook = func(task *task) bool { 242 return len(task.receipts) == 0 243 } 244 245 // Wait for mined blocks. 246 sub := w.mux.Subscribe(core.NewMinedBlockEvent{}) 247 defer sub.Unsubscribe() 248 249 // Start mining! 250 w.start() 251 252 for i := 0; i < 5; i++ { 253 b.txPool.AddLocal(b.newRandomTx(true)) 254 b.txPool.AddLocal(b.newRandomTx(false)) 255 w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()}) 256 w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()}) 257 258 select { 259 case ev := <-sub.Chan(): 260 block := ev.Data.(core.NewMinedBlockEvent).Block 261 if _, err := chain.InsertChain([]*types.Block{block}); err != nil { 262 t.Fatalf("failed to insert new mined block %d: %v", block.NumberU64(), err) 263 } 264 case <-time.After(3 * time.Second): // Worker needs 1s to include new changes. 265 t.Fatalf("timeout") 266 } 267 } 268 } 269 270 func TestEmptyWorkEthash(t *testing.T) { 271 testEmptyWork(t, ethashChainConfig, ethash.NewFaker()) 272 } 273 func TestEmptyWorkClique(t *testing.T) { 274 testEmptyWork(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 275 } 276 277 func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 278 defer engine.Close() 279 280 w, _ := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 281 defer w.close() 282 283 var ( 284 taskIndex int 285 taskCh = make(chan struct{}, 2) 286 ) 287 checkEqual := func(t *testing.T, task *task, index int) { 288 // The first empty work without any txs included 289 receiptLen, balance := 0, big.NewInt(0) 290 if index == 1 { 291 // The second full work with 1 tx included 292 receiptLen, balance = 1, big.NewInt(1000) 293 } 294 if len(task.receipts) != receiptLen { 295 t.Fatalf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen) 296 } 297 if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 { 298 t.Fatalf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance) 299 } 300 } 301 w.newTaskHook = func(task *task) { 302 if task.block.NumberU64() == 1 { 303 checkEqual(t, task, taskIndex) 304 taskIndex += 1 305 taskCh <- struct{}{} 306 } 307 } 308 w.skipSealHook = func(task *task) bool { return true } 309 w.fullTaskHook = func() { 310 time.Sleep(100 * time.Millisecond) 311 } 312 w.start() // Start mining! 313 for i := 0; i < 2; i += 1 { 314 select { 315 case <-taskCh: 316 case <-time.NewTimer(3 * time.Second).C: 317 t.Error("new task timeout") 318 } 319 } 320 } 321 322 func TestStreamUncleBlock(t *testing.T) { 323 ethash := ethash.NewFaker() 324 defer ethash.Close() 325 326 w, b := newTestWorker(t, ethashChainConfig, ethash, rawdb.NewMemoryDatabase(), 1) 327 defer w.close() 328 329 var taskCh = make(chan struct{}, 3) 330 331 taskIndex := 0 332 w.newTaskHook = func(task *task) { 333 if task.block.NumberU64() == 2 { 334 // The first task is an empty task, the second 335 // one has 1 pending tx, the third one has 1 tx 336 // and 1 uncle. 337 if taskIndex == 2 { 338 have := task.block.Header().UncleHash 339 want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()}) 340 if have != want { 341 t.Errorf("uncle hash mismatch: have %s, want %s", have.Hex(), want.Hex()) 342 } 343 } 344 taskCh <- struct{}{} 345 taskIndex += 1 346 } 347 } 348 w.skipSealHook = func(task *task) bool { 349 return true 350 } 351 w.fullTaskHook = func() { 352 time.Sleep(100 * time.Millisecond) 353 } 354 w.start() 355 356 for i := 0; i < 2; i += 1 { 357 select { 358 case <-taskCh: 359 case <-time.NewTimer(time.Second).C: 360 t.Error("new task timeout") 361 } 362 } 363 364 w.postSideBlock(core.ChainSideEvent{Block: b.uncleBlock}) 365 366 select { 367 case <-taskCh: 368 case <-time.NewTimer(time.Second).C: 369 t.Error("new task timeout") 370 } 371 } 372 373 func TestRegenerateMiningBlockEthash(t *testing.T) { 374 testRegenerateMiningBlock(t, ethashChainConfig, ethash.NewFaker()) 375 } 376 377 func TestRegenerateMiningBlockClique(t *testing.T) { 378 testRegenerateMiningBlock(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 379 } 380 381 func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 382 defer engine.Close() 383 384 w, b := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 385 defer w.close() 386 387 var taskCh = make(chan struct{}, 3) 388 389 taskIndex := 0 390 w.newTaskHook = func(task *task) { 391 if task.block.NumberU64() == 1 { 392 // The first task is an empty task, the second 393 // one has 1 pending tx, the third one has 2 txs 394 if taskIndex == 2 { 395 receiptLen, balance := 2, big.NewInt(2000) 396 if len(task.receipts) != receiptLen { 397 t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen) 398 } 399 if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 { 400 t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance) 401 } 402 } 403 taskCh <- struct{}{} 404 taskIndex += 1 405 } 406 } 407 w.skipSealHook = func(task *task) bool { 408 return true 409 } 410 w.fullTaskHook = func() { 411 time.Sleep(100 * time.Millisecond) 412 } 413 414 w.start() 415 // Ignore the first two works 416 for i := 0; i < 2; i += 1 { 417 select { 418 case <-taskCh: 419 case <-time.NewTimer(time.Second).C: 420 t.Error("new task timeout") 421 } 422 } 423 b.txPool.AddLocals(newTxs) 424 time.Sleep(time.Second) 425 426 select { 427 case <-taskCh: 428 case <-time.NewTimer(time.Second).C: 429 t.Error("new task timeout") 430 } 431 } 432 433 func TestAdjustIntervalEthash(t *testing.T) { 434 testAdjustInterval(t, ethashChainConfig, ethash.NewFaker()) 435 } 436 437 func TestAdjustIntervalClique(t *testing.T) { 438 testAdjustInterval(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 439 } 440 441 func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 442 defer engine.Close() 443 444 w, _ := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 445 defer w.close() 446 447 w.skipSealHook = func(task *task) bool { 448 return true 449 } 450 w.fullTaskHook = func() { 451 time.Sleep(100 * time.Millisecond) 452 } 453 var ( 454 progress = make(chan struct{}, 10) 455 result = make([]float64, 0, 10) 456 index = 0 457 start uint32 458 ) 459 w.resubmitHook = func(minInterval time.Duration, recommitInterval time.Duration) { 460 // Short circuit if interval checking hasn't started. 461 if atomic.LoadUint32(&start) == 0 { 462 return 463 } 464 var wantMinInterval, wantRecommitInterval time.Duration 465 466 switch index { 467 case 0: 468 wantMinInterval, wantRecommitInterval = 3*time.Second, 3*time.Second 469 case 1: 470 origin := float64(3 * time.Second.Nanoseconds()) 471 estimate := origin*(1-intervalAdjustRatio) + intervalAdjustRatio*(origin/0.8+intervalAdjustBias) 472 wantMinInterval, wantRecommitInterval = 3*time.Second, time.Duration(estimate)*time.Nanosecond 473 case 2: 474 estimate := result[index-1] 475 min := float64(3 * time.Second.Nanoseconds()) 476 estimate = estimate*(1-intervalAdjustRatio) + intervalAdjustRatio*(min-intervalAdjustBias) 477 wantMinInterval, wantRecommitInterval = 3*time.Second, time.Duration(estimate)*time.Nanosecond 478 case 3: 479 wantMinInterval, wantRecommitInterval = time.Second, time.Second 480 } 481 482 // Check interval 483 if minInterval != wantMinInterval { 484 t.Errorf("resubmit min interval mismatch: have %v, want %v ", minInterval, wantMinInterval) 485 } 486 if recommitInterval != wantRecommitInterval { 487 t.Errorf("resubmit interval mismatch: have %v, want %v", recommitInterval, wantRecommitInterval) 488 } 489 result = append(result, float64(recommitInterval.Nanoseconds())) 490 index += 1 491 progress <- struct{}{} 492 } 493 w.start() 494 495 time.Sleep(time.Second) // Ensure two tasks have been submitted due to start opt 496 atomic.StoreUint32(&start, 1) 497 498 w.setRecommitInterval(3 * time.Second) 499 select { 500 case <-progress: 501 case <-time.NewTimer(time.Second).C: 502 t.Error("interval reset timeout") 503 } 504 505 w.resubmitAdjustCh <- &intervalAdjust{inc: true, ratio: 0.8} 506 select { 507 case <-progress: 508 case <-time.NewTimer(time.Second).C: 509 t.Error("interval reset timeout") 510 } 511 512 w.resubmitAdjustCh <- &intervalAdjust{inc: false} 513 select { 514 case <-progress: 515 case <-time.NewTimer(time.Second).C: 516 t.Error("interval reset timeout") 517 } 518 519 w.setRecommitInterval(500 * time.Millisecond) 520 select { 521 case <-progress: 522 case <-time.NewTimer(time.Second).C: 523 t.Error("interval reset timeout") 524 } 525 } 526 527 func TestGetSealingWorkEthash(t *testing.T) { 528 testGetSealingWork(t, ethashChainConfig, ethash.NewFaker()) 529 } 530 531 func TestGetSealingWorkClique(t *testing.T) { 532 testGetSealingWork(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 533 } 534 535 func TestGetSealingWorkPostMerge(t *testing.T) { 536 local := new(params.ChainConfig) 537 *local = *ethashChainConfig 538 local.TerminalTotalDifficulty = big.NewInt(0) 539 testGetSealingWork(t, local, ethash.NewFaker()) 540 } 541 542 func testGetSealingWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 543 defer engine.Close() 544 545 w, b := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 546 defer w.close() 547 548 w.setExtra([]byte{0x01, 0x02}) 549 w.postSideBlock(core.ChainSideEvent{Block: b.uncleBlock}) 550 551 w.skipSealHook = func(task *task) bool { 552 return true 553 } 554 w.fullTaskHook = func() { 555 time.Sleep(100 * time.Millisecond) 556 } 557 timestamp := uint64(time.Now().Unix()) 558 assertBlock := func(block *types.Block, number uint64, coinbase common.Address, random common.Hash) { 559 if block.Time() != timestamp { 560 // Sometime the timestamp will be mutated if the timestamp 561 // is even smaller than parent block's. It's OK. 562 t.Logf("Invalid timestamp, want %d, get %d", timestamp, block.Time()) 563 } 564 if len(block.Uncles()) != 0 { 565 t.Error("Unexpected uncle block") 566 } 567 _, isClique := engine.(*clique.Clique) 568 if !isClique { 569 if len(block.Extra()) != 2 { 570 t.Error("Unexpected extra field") 571 } 572 if block.Coinbase() != coinbase { 573 t.Errorf("Unexpected coinbase got %x want %x", block.Coinbase(), coinbase) 574 } 575 } else { 576 if block.Coinbase() != (common.Address{}) { 577 t.Error("Unexpected coinbase") 578 } 579 } 580 if !isClique { 581 if block.MixDigest() != random { 582 t.Error("Unexpected mix digest") 583 } 584 } 585 if block.Nonce() != 0 { 586 t.Error("Unexpected block nonce") 587 } 588 if block.NumberU64() != number { 589 t.Errorf("Mismatched block number, want %d got %d", number, block.NumberU64()) 590 } 591 } 592 var cases = []struct { 593 parent common.Hash 594 coinbase common.Address 595 random common.Hash 596 expectNumber uint64 597 expectErr bool 598 }{ 599 { 600 b.chain.Genesis().Hash(), 601 common.HexToAddress("0xdeadbeef"), 602 common.HexToHash("0xcafebabe"), 603 uint64(1), 604 false, 605 }, 606 { 607 b.chain.CurrentBlock().Hash(), 608 common.HexToAddress("0xdeadbeef"), 609 common.HexToHash("0xcafebabe"), 610 b.chain.CurrentBlock().Number.Uint64() + 1, 611 false, 612 }, 613 { 614 b.chain.CurrentBlock().Hash(), 615 common.Address{}, 616 common.HexToHash("0xcafebabe"), 617 b.chain.CurrentBlock().Number.Uint64() + 1, 618 false, 619 }, 620 { 621 b.chain.CurrentBlock().Hash(), 622 common.Address{}, 623 common.Hash{}, 624 b.chain.CurrentBlock().Number.Uint64() + 1, 625 false, 626 }, 627 { 628 common.HexToHash("0xdeadbeef"), 629 common.HexToAddress("0xdeadbeef"), 630 common.HexToHash("0xcafebabe"), 631 0, 632 true, 633 }, 634 } 635 636 // This API should work even when the automatic sealing is not enabled 637 for _, c := range cases { 638 block, _, err := w.getSealingBlock(c.parent, timestamp, c.coinbase, c.random, nil, false) 639 if c.expectErr { 640 if err == nil { 641 t.Error("Expect error but get nil") 642 } 643 } else { 644 if err != nil { 645 t.Errorf("Unexpected error %v", err) 646 } 647 assertBlock(block, c.expectNumber, c.coinbase, c.random) 648 } 649 } 650 651 // This API should work even when the automatic sealing is enabled 652 w.start() 653 for _, c := range cases { 654 block, _, err := w.getSealingBlock(c.parent, timestamp, c.coinbase, c.random, nil, false) 655 if c.expectErr { 656 if err == nil { 657 t.Error("Expect error but get nil") 658 } 659 } else { 660 if err != nil { 661 t.Errorf("Unexpected error %v", err) 662 } 663 assertBlock(block, c.expectNumber, c.coinbase, c.random) 664 } 665 } 666 }