github.com/defi-hunter/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/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 "math/big" 21 "math/rand" 22 "testing" 23 "time" 24 25 "github.com/Fantom-foundation/go-ethereum/accounts" 26 "github.com/Fantom-foundation/go-ethereum/common" 27 "github.com/Fantom-foundation/go-ethereum/consensus" 28 "github.com/Fantom-foundation/go-ethereum/consensus/clique" 29 "github.com/Fantom-foundation/go-ethereum/consensus/ethash" 30 "github.com/Fantom-foundation/go-ethereum/core" 31 "github.com/Fantom-foundation/go-ethereum/core/rawdb" 32 "github.com/Fantom-foundation/go-ethereum/core/types" 33 "github.com/Fantom-foundation/go-ethereum/core/vm" 34 "github.com/Fantom-foundation/go-ethereum/crypto" 35 "github.com/Fantom-foundation/go-ethereum/ethdb" 36 "github.com/Fantom-foundation/go-ethereum/event" 37 "github.com/Fantom-foundation/go-ethereum/params" 38 ) 39 40 const ( 41 // testCode is the testing contract binary code which will initialises some 42 // variables in constructor 43 testCode = "0x60806040527fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0060005534801561003457600080fd5b5060fc806100436000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80630c4dae8814603757806398a213cf146053575b600080fd5b603d607e565b6040518082815260200191505060405180910390f35b607c60048036036020811015606757600080fd5b81019080803590602001909291905050506084565b005b60005481565b806000819055507fe9e44f9f7da8c559de847a3232b57364adc0354f15a2cd8dc636d54396f9587a6000546040518082815260200191505060405180910390a15056fea265627a7a723058208ae31d9424f2d0bc2a3da1a5dd659db2d71ec322a17db8f87e19e209e3a1ff4a64736f6c634300050a0032" 44 45 // testGas is the gas required for contract deployment. 46 testGas = 144109 47 ) 48 49 var ( 50 // Test chain configurations 51 testTxPoolConfig core.TxPoolConfig 52 ethashChainConfig *params.ChainConfig 53 cliqueChainConfig *params.ChainConfig 54 55 // Test accounts 56 testBankKey, _ = crypto.GenerateKey() 57 testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) 58 testBankFunds = big.NewInt(1000000000000000000) 59 60 testUserKey, _ = crypto.GenerateKey() 61 testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey) 62 63 // Test transactions 64 pendingTxs []*types.Transaction 65 newTxs []*types.Transaction 66 67 testConfig = &Config{ 68 Recommit: time.Second, 69 GasFloor: params.GenesisGasLimit, 70 GasCeil: params.GenesisGasLimit, 71 } 72 ) 73 74 func init() { 75 testTxPoolConfig = core.DefaultTxPoolConfig 76 testTxPoolConfig.Journal = "" 77 ethashChainConfig = params.TestChainConfig 78 cliqueChainConfig = params.TestChainConfig 79 cliqueChainConfig.Clique = ¶ms.CliqueConfig{ 80 Period: 10, 81 Epoch: 30000, 82 } 83 tx1, _ := types.SignTx(types.NewTransaction(0, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) 84 pendingTxs = append(pendingTxs, tx1) 85 tx2, _ := types.SignTx(types.NewTransaction(1, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) 86 newTxs = append(newTxs, tx2) 87 rand.Seed(time.Now().UnixNano()) 88 } 89 90 // testWorkerBackend implements worker.Backend interfaces and wraps all information needed during the testing. 91 type testWorkerBackend struct { 92 db ethdb.Database 93 txPool *core.TxPool 94 chain *core.BlockChain 95 testTxFeed event.Feed 96 genesis *core.Genesis 97 uncleBlock *types.Block 98 } 99 100 func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, n int) *testWorkerBackend { 101 var gspec = core.Genesis{ 102 Config: chainConfig, 103 Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}, 104 } 105 106 switch e := engine.(type) { 107 case *clique.Clique: 108 gspec.ExtraData = make([]byte, 32+common.AddressLength+crypto.SignatureLength) 109 copy(gspec.ExtraData[32:32+common.AddressLength], testBankAddress.Bytes()) 110 e.Authorize(testBankAddress, func(account accounts.Account, s string, data []byte) ([]byte, error) { 111 return crypto.Sign(crypto.Keccak256(data), testBankKey) 112 }) 113 case *ethash.Ethash: 114 default: 115 t.Fatalf("unexpected consensus engine type: %T", engine) 116 } 117 genesis := gspec.MustCommit(db) 118 119 chain, _ := core.NewBlockChain(db, &core.CacheConfig{TrieDirtyDisabled: true}, gspec.Config, engine, vm.Config{}, nil) 120 txpool := core.NewTxPool(testTxPoolConfig, chainConfig, chain) 121 122 // Generate a small n-block chain and an uncle block for it 123 if n > 0 { 124 blocks, _ := core.GenerateChain(chainConfig, genesis, engine, db, n, func(i int, gen *core.BlockGen) { 125 gen.SetCoinbase(testBankAddress) 126 }) 127 if _, err := chain.InsertChain(blocks); err != nil { 128 t.Fatalf("failed to insert origin chain: %v", err) 129 } 130 } 131 parent := genesis 132 if n > 0 { 133 parent = chain.GetBlockByHash(chain.CurrentBlock().ParentHash()) 134 } 135 blocks, _ := core.GenerateChain(chainConfig, parent, engine, db, 1, func(i int, gen *core.BlockGen) { 136 gen.SetCoinbase(testUserAddress) 137 }) 138 139 return &testWorkerBackend{ 140 db: db, 141 chain: chain, 142 txPool: txpool, 143 genesis: &gspec, 144 uncleBlock: blocks[0], 145 } 146 } 147 148 func (b *testWorkerBackend) BlockChain() *core.BlockChain { return b.chain } 149 func (b *testWorkerBackend) TxPool() *core.TxPool { return b.txPool } 150 func (b *testWorkerBackend) PostChainEvents(events []interface{}) { 151 b.chain.PostChainEvents(events, nil) 152 } 153 154 func (b *testWorkerBackend) newRandomUncle() *types.Block { 155 var parent *types.Block 156 cur := b.chain.CurrentBlock() 157 if cur.NumberU64() == 0 { 158 parent = b.chain.Genesis() 159 } else { 160 parent = b.chain.GetBlockByHash(b.chain.CurrentBlock().ParentHash()) 161 } 162 blocks, _ := core.GenerateChain(b.chain.Config(), parent, b.chain.Engine(), b.db, 1, func(i int, gen *core.BlockGen) { 163 var addr = make([]byte, common.AddressLength) 164 rand.Read(addr) 165 gen.SetCoinbase(common.BytesToAddress(addr)) 166 }) 167 return blocks[0] 168 } 169 170 func (b *testWorkerBackend) newRandomTx(creation bool) *types.Transaction { 171 var tx *types.Transaction 172 if creation { 173 tx, _ = types.SignTx(types.NewContractCreation(b.txPool.Nonce(testBankAddress), big.NewInt(0), testGas, nil, common.FromHex(testCode)), types.HomesteadSigner{}, testBankKey) 174 } else { 175 tx, _ = types.SignTx(types.NewTransaction(b.txPool.Nonce(testBankAddress), testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) 176 } 177 return tx 178 } 179 180 func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, blocks int) (*worker, *testWorkerBackend) { 181 backend := newTestWorkerBackend(t, chainConfig, engine, db, blocks) 182 backend.txPool.AddLocals(pendingTxs) 183 w := newWorker(testConfig, chainConfig, engine, backend, new(event.TypeMux), nil) 184 w.setEtherbase(testBankAddress) 185 return w, backend 186 } 187 188 func TestGenerateBlockAndImportEthash(t *testing.T) { 189 testGenerateBlockAndImport(t, false) 190 } 191 192 func TestGenerateBlockAndImportClique(t *testing.T) { 193 testGenerateBlockAndImport(t, true) 194 } 195 196 func testGenerateBlockAndImport(t *testing.T, isClique bool) { 197 var ( 198 engine consensus.Engine 199 chainConfig *params.ChainConfig 200 db = rawdb.NewMemoryDatabase() 201 ) 202 if isClique { 203 chainConfig = params.AllCliqueProtocolChanges 204 chainConfig.Clique = ¶ms.CliqueConfig{Period: 1, Epoch: 30000} 205 engine = clique.New(chainConfig.Clique, db) 206 } else { 207 chainConfig = params.AllEthashProtocolChanges 208 engine = ethash.NewFaker() 209 } 210 211 w, b := newTestWorker(t, chainConfig, engine, db, 0) 212 defer w.close() 213 214 db2 := rawdb.NewMemoryDatabase() 215 b.genesis.MustCommit(db2) 216 chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil) 217 defer chain.Stop() 218 219 newBlock := make(chan struct{}) 220 listenNewBlock := func() { 221 sub := w.mux.Subscribe(core.NewMinedBlockEvent{}) 222 defer sub.Unsubscribe() 223 224 for item := range sub.Chan() { 225 block := item.Data.(core.NewMinedBlockEvent).Block 226 _, err := chain.InsertChain([]*types.Block{block}) 227 if err != nil { 228 t.Fatalf("Failed to insert new mined block:%d, error:%v", block.NumberU64(), err) 229 } 230 newBlock <- struct{}{} 231 } 232 } 233 234 // Ensure worker has finished initialization 235 for { 236 b := w.pendingBlock() 237 if b != nil && b.NumberU64() == 1 { 238 break 239 } 240 } 241 w.start() // Start mining! 242 243 // Ignore first 2 commits caused by start operation 244 ignored := make(chan struct{}, 2) 245 w.skipSealHook = func(task *task) bool { 246 ignored <- struct{}{} 247 return true 248 } 249 for i := 0; i < 2; i++ { 250 <-ignored 251 } 252 253 go listenNewBlock() 254 255 // Ignore empty commit here for less noise 256 w.skipSealHook = func(task *task) bool { 257 return len(task.receipts) == 0 258 } 259 for i := 0; i < 5; i++ { 260 b.txPool.AddLocal(b.newRandomTx(true)) 261 b.txPool.AddLocal(b.newRandomTx(false)) 262 b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}}) 263 b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.newRandomUncle()}}) 264 select { 265 case <-newBlock: 266 case <-time.NewTimer(3 * time.Second).C: // Worker needs 1s to include new changes. 267 t.Fatalf("timeout") 268 } 269 } 270 } 271 272 func TestPendingStateAndBlockEthash(t *testing.T) { 273 testPendingStateAndBlock(t, ethashChainConfig, ethash.NewFaker()) 274 } 275 func TestPendingStateAndBlockClique(t *testing.T) { 276 testPendingStateAndBlock(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 277 } 278 279 func testPendingStateAndBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 280 defer engine.Close() 281 282 w, b := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 283 defer w.close() 284 285 // Ensure snapshot has been updated. 286 time.Sleep(100 * time.Millisecond) 287 block, state := w.pending() 288 if block.NumberU64() != 1 { 289 t.Errorf("block number mismatch: have %d, want %d", block.NumberU64(), 1) 290 } 291 if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(1000)) != 0 { 292 t.Errorf("account balance mismatch: have %d, want %d", balance, 1000) 293 } 294 b.txPool.AddLocals(newTxs) 295 296 // Ensure the new tx events has been processed 297 time.Sleep(100 * time.Millisecond) 298 block, state = w.pending() 299 if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(2000)) != 0 { 300 t.Errorf("account balance mismatch: have %d, want %d", balance, 2000) 301 } 302 } 303 304 func TestEmptyWorkEthash(t *testing.T) { 305 testEmptyWork(t, ethashChainConfig, ethash.NewFaker()) 306 } 307 func TestEmptyWorkClique(t *testing.T) { 308 testEmptyWork(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 309 } 310 311 func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 312 defer engine.Close() 313 314 w, _ := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 315 defer w.close() 316 317 var ( 318 taskCh = make(chan struct{}, 2) 319 taskIndex int 320 ) 321 322 checkEqual := func(t *testing.T, task *task, index int) { 323 receiptLen, balance := 0, big.NewInt(0) 324 if index == 1 { 325 receiptLen, balance = 1, big.NewInt(1000) 326 } 327 if len(task.receipts) != receiptLen { 328 t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen) 329 } 330 if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 { 331 t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance) 332 } 333 } 334 335 w.newTaskHook = func(task *task) { 336 if task.block.NumberU64() == 1 { 337 checkEqual(t, task, taskIndex) 338 taskIndex += 1 339 taskCh <- struct{}{} 340 } 341 } 342 w.fullTaskHook = func() { 343 time.Sleep(100 * time.Millisecond) 344 } 345 346 // Ensure worker has finished initialization 347 for { 348 b := w.pendingBlock() 349 if b != nil && b.NumberU64() == 1 { 350 break 351 } 352 } 353 354 w.start() 355 for i := 0; i < 2; i += 1 { 356 select { 357 case <-taskCh: 358 case <-time.NewTimer(2 * time.Second).C: 359 t.Error("new task timeout") 360 } 361 } 362 } 363 364 func TestStreamUncleBlock(t *testing.T) { 365 ethash := ethash.NewFaker() 366 defer ethash.Close() 367 368 w, b := newTestWorker(t, ethashChainConfig, ethash, rawdb.NewMemoryDatabase(), 1) 369 defer w.close() 370 371 var taskCh = make(chan struct{}) 372 373 taskIndex := 0 374 w.newTaskHook = func(task *task) { 375 if task.block.NumberU64() == 2 { 376 if taskIndex == 2 { 377 have := task.block.Header().UncleHash 378 want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()}) 379 if have != want { 380 t.Errorf("uncle hash mismatch: have %s, want %s", have.Hex(), want.Hex()) 381 } 382 } 383 taskCh <- struct{}{} 384 taskIndex += 1 385 } 386 } 387 w.skipSealHook = func(task *task) bool { 388 return true 389 } 390 w.fullTaskHook = func() { 391 time.Sleep(100 * time.Millisecond) 392 } 393 394 // Ensure worker has finished initialization 395 for { 396 b := w.pendingBlock() 397 if b != nil && b.NumberU64() == 2 { 398 break 399 } 400 } 401 w.start() 402 403 // Ignore the first two works 404 for i := 0; i < 2; i += 1 { 405 select { 406 case <-taskCh: 407 case <-time.NewTimer(time.Second).C: 408 t.Error("new task timeout") 409 } 410 } 411 b.PostChainEvents([]interface{}{core.ChainSideEvent{Block: b.uncleBlock}}) 412 413 select { 414 case <-taskCh: 415 case <-time.NewTimer(time.Second).C: 416 t.Error("new task timeout") 417 } 418 } 419 420 func TestRegenerateMiningBlockEthash(t *testing.T) { 421 testRegenerateMiningBlock(t, ethashChainConfig, ethash.NewFaker()) 422 } 423 424 func TestRegenerateMiningBlockClique(t *testing.T) { 425 testRegenerateMiningBlock(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 426 } 427 428 func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 429 defer engine.Close() 430 431 w, b := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 432 defer w.close() 433 434 var taskCh = make(chan struct{}) 435 436 taskIndex := 0 437 w.newTaskHook = func(task *task) { 438 if task.block.NumberU64() == 1 { 439 if taskIndex == 2 { 440 receiptLen, balance := 2, big.NewInt(2000) 441 if len(task.receipts) != receiptLen { 442 t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen) 443 } 444 if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 { 445 t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance) 446 } 447 } 448 taskCh <- struct{}{} 449 taskIndex += 1 450 } 451 } 452 w.skipSealHook = func(task *task) bool { 453 return true 454 } 455 w.fullTaskHook = func() { 456 time.Sleep(100 * time.Millisecond) 457 } 458 // Ensure worker has finished initialization 459 for { 460 b := w.pendingBlock() 461 if b != nil && b.NumberU64() == 1 { 462 break 463 } 464 } 465 466 w.start() 467 // Ignore the first two works 468 for i := 0; i < 2; i += 1 { 469 select { 470 case <-taskCh: 471 case <-time.NewTimer(time.Second).C: 472 t.Error("new task timeout") 473 } 474 } 475 b.txPool.AddLocals(newTxs) 476 time.Sleep(time.Second) 477 478 select { 479 case <-taskCh: 480 case <-time.NewTimer(time.Second).C: 481 t.Error("new task timeout") 482 } 483 } 484 485 func TestAdjustIntervalEthash(t *testing.T) { 486 testAdjustInterval(t, ethashChainConfig, ethash.NewFaker()) 487 } 488 489 func TestAdjustIntervalClique(t *testing.T) { 490 testAdjustInterval(t, cliqueChainConfig, clique.New(cliqueChainConfig.Clique, rawdb.NewMemoryDatabase())) 491 } 492 493 func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { 494 defer engine.Close() 495 496 w, _ := newTestWorker(t, chainConfig, engine, rawdb.NewMemoryDatabase(), 0) 497 defer w.close() 498 499 w.skipSealHook = func(task *task) bool { 500 return true 501 } 502 w.fullTaskHook = func() { 503 time.Sleep(100 * time.Millisecond) 504 } 505 var ( 506 progress = make(chan struct{}, 10) 507 result = make([]float64, 0, 10) 508 index = 0 509 start = false 510 ) 511 w.resubmitHook = func(minInterval time.Duration, recommitInterval time.Duration) { 512 // Short circuit if interval checking hasn't started. 513 if !start { 514 return 515 } 516 var wantMinInterval, wantRecommitInterval time.Duration 517 518 switch index { 519 case 0: 520 wantMinInterval, wantRecommitInterval = 3*time.Second, 3*time.Second 521 case 1: 522 origin := float64(3 * time.Second.Nanoseconds()) 523 estimate := origin*(1-intervalAdjustRatio) + intervalAdjustRatio*(origin/0.8+intervalAdjustBias) 524 wantMinInterval, wantRecommitInterval = 3*time.Second, time.Duration(estimate)*time.Nanosecond 525 case 2: 526 estimate := result[index-1] 527 min := float64(3 * time.Second.Nanoseconds()) 528 estimate = estimate*(1-intervalAdjustRatio) + intervalAdjustRatio*(min-intervalAdjustBias) 529 wantMinInterval, wantRecommitInterval = 3*time.Second, time.Duration(estimate)*time.Nanosecond 530 case 3: 531 wantMinInterval, wantRecommitInterval = time.Second, time.Second 532 } 533 534 // Check interval 535 if minInterval != wantMinInterval { 536 t.Errorf("resubmit min interval mismatch: have %v, want %v ", minInterval, wantMinInterval) 537 } 538 if recommitInterval != wantRecommitInterval { 539 t.Errorf("resubmit interval mismatch: have %v, want %v", recommitInterval, wantRecommitInterval) 540 } 541 result = append(result, float64(recommitInterval.Nanoseconds())) 542 index += 1 543 progress <- struct{}{} 544 } 545 // Ensure worker has finished initialization 546 for { 547 b := w.pendingBlock() 548 if b != nil && b.NumberU64() == 1 { 549 break 550 } 551 } 552 553 w.start() 554 555 time.Sleep(time.Second) 556 557 start = true 558 w.setRecommitInterval(3 * time.Second) 559 select { 560 case <-progress: 561 case <-time.NewTimer(time.Second).C: 562 t.Error("interval reset timeout") 563 } 564 565 w.resubmitAdjustCh <- &intervalAdjust{inc: true, ratio: 0.8} 566 select { 567 case <-progress: 568 case <-time.NewTimer(time.Second).C: 569 t.Error("interval reset timeout") 570 } 571 572 w.resubmitAdjustCh <- &intervalAdjust{inc: false} 573 select { 574 case <-progress: 575 case <-time.NewTimer(time.Second).C: 576 t.Error("interval reset timeout") 577 } 578 579 w.setRecommitInterval(500 * time.Millisecond) 580 select { 581 case <-progress: 582 case <-time.NewTimer(time.Second).C: 583 t.Error("interval reset timeout") 584 } 585 }