github.com/janotchain/janota@v0.0.0-20220824112012-93ea4c5dee78/miner/worker.go (about) 1 // Copyright 2015 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 "bytes" 21 "fmt" 22 "math/big" 23 "sync" 24 "sync/atomic" 25 "time" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/consensus" 29 "github.com/ethereum/go-ethereum/consensus/misc" 30 "github.com/ethereum/go-ethereum/core" 31 "github.com/ethereum/go-ethereum/core/state" 32 "github.com/ethereum/go-ethereum/core/types" 33 "github.com/ethereum/go-ethereum/core/vm" 34 "github.com/ethereum/go-ethereum/ethdb" 35 "github.com/ethereum/go-ethereum/event" 36 "github.com/ethereum/go-ethereum/log" 37 "github.com/ethereum/go-ethereum/params" 38 "gopkg.in/fatih/set.v0" 39 ) 40 41 const ( 42 resultQueueSize = 10 43 miningLogAtDepth = 5 44 45 // txChanSize is the size of channel listening to TxPreEvent. 46 // The number is referenced from the size of tx pool. 47 txChanSize = 4096 48 // chainHeadChanSize is the size of channel listening to ChainHeadEvent. 49 chainHeadChanSize = 10 50 // chainSideChanSize is the size of channel listening to ChainSideEvent. 51 chainSideChanSize = 10 52 ) 53 54 // Agent can register themself with the worker 55 type Agent interface { 56 Work() chan<- *Work 57 SetReturnCh(chan<- *Result) 58 Stop() 59 Start() 60 GetHashRate() int64 61 } 62 63 // Work is the workers current environment and holds 64 // all of the current state information 65 type Work struct { 66 config *params.ChainConfig 67 signer types.Signer 68 69 state *state.StateDB // apply state changes here 70 ancestors *set.Set // ancestor set (used for checking uncle parent validity) 71 family *set.Set // family set (used for checking uncle invalidity) 72 uncles *set.Set // uncle set 73 tcount int // tx count in cycle 74 75 Block *types.Block // the new block 76 77 header *types.Header 78 txs []*types.Transaction 79 receipts []*types.Receipt 80 81 createdAt time.Time 82 } 83 84 type Result struct { 85 Work *Work 86 Block *types.Block 87 } 88 89 // worker is the main object which takes care of applying messages to the new state 90 type worker struct { 91 config *params.ChainConfig 92 engine consensus.Engine 93 94 mu sync.Mutex 95 96 // update loop 97 mux *event.TypeMux 98 txCh chan core.TxPreEvent 99 txSub event.Subscription 100 chainHeadCh chan core.ChainHeadEvent 101 chainHeadSub event.Subscription 102 chainSideCh chan core.ChainSideEvent 103 chainSideSub event.Subscription 104 wg sync.WaitGroup 105 106 agents map[Agent]struct{} 107 recv chan *Result 108 109 eth Backend 110 chain *core.BlockChain 111 proc core.Validator 112 chainDb ethdb.Database 113 114 coinbase common.Address 115 extra []byte 116 117 currentMu sync.Mutex 118 current *Work 119 120 uncleMu sync.Mutex 121 possibleUncles map[common.Hash]*types.Block 122 123 unconfirmed *unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations 124 125 // atomic status counters 126 mining int32 127 atWork int32 128 } 129 130 func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker { 131 worker := &worker{ 132 config: config, 133 engine: engine, 134 eth: eth, 135 mux: mux, 136 txCh: make(chan core.TxPreEvent, txChanSize), 137 chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize), 138 chainSideCh: make(chan core.ChainSideEvent, chainSideChanSize), 139 chainDb: eth.ChainDb(), 140 recv: make(chan *Result, resultQueueSize), 141 chain: eth.BlockChain(), 142 proc: eth.BlockChain().Validator(), 143 possibleUncles: make(map[common.Hash]*types.Block), 144 coinbase: coinbase, 145 agents: make(map[Agent]struct{}), 146 unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), 147 } 148 // Subscribe TxPreEvent for tx pool 149 worker.txSub = eth.TxPool().SubscribeTxPreEvent(worker.txCh) 150 // Subscribe events for blockchain 151 worker.chainHeadSub = eth.BlockChain().SubscribeChainHeadEvent(worker.chainHeadCh) 152 worker.chainSideSub = eth.BlockChain().SubscribeChainSideEvent(worker.chainSideCh) 153 go worker.update() 154 155 go worker.wait() 156 worker.commitNewWork() 157 158 return worker 159 } 160 161 func (self *worker) setEtherbase(addr common.Address) { 162 self.mu.Lock() 163 defer self.mu.Unlock() 164 self.coinbase = addr 165 } 166 167 func (self *worker) setExtra(extra []byte) { 168 self.mu.Lock() 169 defer self.mu.Unlock() 170 self.extra = extra 171 } 172 173 func (self *worker) pending() (*types.Block, *state.StateDB) { 174 self.currentMu.Lock() 175 defer self.currentMu.Unlock() 176 177 if atomic.LoadInt32(&self.mining) == 0 { 178 return types.NewBlock( 179 self.current.header, 180 self.current.txs, 181 nil, 182 self.current.receipts, 183 ), self.current.state.Copy() 184 } 185 return self.current.Block, self.current.state.Copy() 186 } 187 188 func (self *worker) pendingBlock() *types.Block { 189 self.currentMu.Lock() 190 defer self.currentMu.Unlock() 191 192 if atomic.LoadInt32(&self.mining) == 0 { 193 return types.NewBlock( 194 self.current.header, 195 self.current.txs, 196 nil, 197 self.current.receipts, 198 ) 199 } 200 return self.current.Block 201 } 202 203 func (self *worker) start() { 204 self.mu.Lock() 205 defer self.mu.Unlock() 206 207 atomic.StoreInt32(&self.mining, 1) 208 209 // spin up agents 210 for agent := range self.agents { 211 agent.Start() 212 } 213 } 214 215 func (self *worker) stop() { 216 self.wg.Wait() 217 218 self.mu.Lock() 219 defer self.mu.Unlock() 220 if atomic.LoadInt32(&self.mining) == 1 { 221 for agent := range self.agents { 222 agent.Stop() 223 } 224 } 225 atomic.StoreInt32(&self.mining, 0) 226 atomic.StoreInt32(&self.atWork, 0) 227 } 228 229 func (self *worker) register(agent Agent) { 230 self.mu.Lock() 231 defer self.mu.Unlock() 232 self.agents[agent] = struct{}{} 233 agent.SetReturnCh(self.recv) 234 } 235 236 func (self *worker) unregister(agent Agent) { 237 self.mu.Lock() 238 defer self.mu.Unlock() 239 delete(self.agents, agent) 240 agent.Stop() 241 } 242 243 func (self *worker) update() { 244 defer self.txSub.Unsubscribe() 245 defer self.chainHeadSub.Unsubscribe() 246 defer self.chainSideSub.Unsubscribe() 247 248 for { 249 // A real event arrived, process interesting content 250 select { 251 // Handle ChainHeadEvent 252 case <-self.chainHeadCh: 253 self.commitNewWork() 254 255 // Handle ChainSideEvent 256 case ev := <-self.chainSideCh: 257 self.uncleMu.Lock() 258 self.possibleUncles[ev.Block.Hash()] = ev.Block 259 self.uncleMu.Unlock() 260 261 // Handle TxPreEvent 262 case ev := <-self.txCh: 263 // Apply transaction to the pending state if we're not mining 264 if atomic.LoadInt32(&self.mining) == 0 { 265 self.currentMu.Lock() 266 acc, _ := types.Sender(self.current.signer, ev.Tx) 267 txs := map[common.Address]types.Transactions{acc: {ev.Tx}} 268 txset := types.NewTransactionsByPriceAndNonce(self.current.signer, txs) 269 270 self.current.commitTransactions(self.mux, txset, self.chain, self.coinbase) 271 self.currentMu.Unlock() 272 } else { 273 // If we're mining, but nothing is being processed, wake on new transactions 274 if self.config.Clique != nil && self.config.Clique.Period == 0 { 275 self.commitNewWork() 276 } 277 } 278 279 // System stopped 280 case <-self.txSub.Err(): 281 return 282 case <-self.chainHeadSub.Err(): 283 return 284 case <-self.chainSideSub.Err(): 285 return 286 } 287 } 288 } 289 290 func (self *worker) wait() { 291 for { 292 mustCommitNewWork := true 293 for result := range self.recv { 294 atomic.AddInt32(&self.atWork, -1) 295 296 if result == nil { 297 continue 298 } 299 block := result.Block 300 work := result.Work 301 302 // Update the block hash in all logs since it is now available and not when the 303 // receipt/log of individual transactions were created. 304 for _, r := range work.receipts { 305 for _, l := range r.Logs { 306 l.BlockHash = block.Hash() 307 } 308 } 309 for _, log := range work.state.Logs() { 310 log.BlockHash = block.Hash() 311 } 312 stat, err := self.chain.WriteBlockAndState(block, work.receipts, work.state) 313 if err != nil { 314 log.Error("Failed writing block to chain", "err", err) 315 continue 316 } 317 // check if canon block and write transactions 318 if stat == core.CanonStatTy { 319 // implicit by posting ChainHeadEvent 320 mustCommitNewWork = false 321 } 322 // Broadcast the block and announce chain insertion event 323 self.mux.Post(core.NewMinedBlockEvent{Block: block}) 324 var ( 325 events []interface{} 326 logs = work.state.Logs() 327 ) 328 events = append(events, core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs}) 329 if stat == core.CanonStatTy { 330 events = append(events, core.ChainHeadEvent{Block: block}) 331 } 332 self.chain.PostChainEvents(events, logs) 333 334 // Insert the block into the set of pending ones to wait for confirmations 335 self.unconfirmed.Insert(block.NumberU64(), block.Hash()) 336 337 if mustCommitNewWork { 338 self.commitNewWork() 339 } 340 } 341 } 342 } 343 344 // push sends a new work task to currently live miner agents. 345 func (self *worker) push(work *Work) { 346 if atomic.LoadInt32(&self.mining) != 1 { 347 return 348 } 349 for agent := range self.agents { 350 atomic.AddInt32(&self.atWork, 1) 351 if ch := agent.Work(); ch != nil { 352 ch <- work 353 } 354 } 355 } 356 357 // makeCurrent creates a new environment for the current cycle. 358 func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error { 359 state, err := self.chain.StateAt(parent.Root()) 360 if err != nil { 361 return err 362 } 363 work := &Work{ 364 config: self.config, 365 signer: types.NewEIP155Signer(self.config.ChainId), 366 state: state, 367 ancestors: set.New(), 368 family: set.New(), 369 uncles: set.New(), 370 header: header, 371 createdAt: time.Now(), 372 } 373 374 // when 08 is processed ancestors contain 07 (quick block) 375 for _, ancestor := range self.chain.GetBlocksFromHash(parent.Hash(), 7) { 376 for _, uncle := range ancestor.Uncles() { 377 work.family.Add(uncle.Hash()) 378 } 379 work.family.Add(ancestor.Hash()) 380 work.ancestors.Add(ancestor.Hash()) 381 } 382 383 // Keep track of transactions which return errors so they can be removed 384 work.tcount = 0 385 self.current = work 386 return nil 387 } 388 389 func (self *worker) commitNewWork() { 390 self.mu.Lock() 391 defer self.mu.Unlock() 392 self.uncleMu.Lock() 393 defer self.uncleMu.Unlock() 394 self.currentMu.Lock() 395 defer self.currentMu.Unlock() 396 397 tstart := time.Now() 398 parent := self.chain.CurrentBlock() 399 400 tstamp := tstart.Unix() 401 if parent.Time().Cmp(new(big.Int).SetInt64(tstamp)) >= 0 { 402 tstamp = parent.Time().Int64() + 1 403 } 404 // this will ensure we're not going off too far in the future 405 if now := time.Now().Unix(); tstamp > now+1 { 406 wait := time.Duration(tstamp-now) * time.Second 407 log.Info("Mining too far in the future", "wait", common.PrettyDuration(wait)) 408 time.Sleep(wait) 409 } 410 411 num := parent.Number() 412 header := &types.Header{ 413 ParentHash: parent.Hash(), 414 Number: num.Add(num, common.Big1), 415 GasLimit: core.CalcGasLimit(parent), 416 GasUsed: new(big.Int), 417 Extra: self.extra, 418 Time: big.NewInt(tstamp), 419 } 420 // Only set the coinbase if we are mining (avoid spurious block rewards) 421 if atomic.LoadInt32(&self.mining) == 1 { 422 header.Coinbase = self.coinbase 423 } 424 if err := self.engine.Prepare(self.chain, header); err != nil { 425 log.Error("Failed to prepare header for mining", "err", err) 426 return 427 } 428 // If we are care about TheDAO hard-fork check whether to override the extra-data or not 429 if daoBlock := self.config.DAOForkBlock; daoBlock != nil { 430 // Check whether the block is among the fork extra-override range 431 limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange) 432 if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 { 433 // Depending whether we support or oppose the fork, override differently 434 if self.config.DAOForkSupport { 435 header.Extra = common.CopyBytes(params.DAOForkBlockExtra) 436 } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) { 437 header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data 438 } 439 } 440 } 441 // Could potentially happen if starting to mine in an odd state. 442 err := self.makeCurrent(parent, header) 443 if err != nil { 444 log.Error("Failed to create mining context", "err", err) 445 return 446 } 447 // Create the current work task and check any fork transitions needed 448 work := self.current 449 if self.config.DAOForkSupport && self.config.DAOForkBlock != nil && self.config.DAOForkBlock.Cmp(header.Number) == 0 { 450 misc.ApplyDAOHardFork(work.state) 451 } 452 pending, err := self.eth.TxPool().Pending() 453 if err != nil { 454 log.Error("Failed to fetch pending transactions", "err", err) 455 return 456 } 457 txs := types.NewTransactionsByPriceAndNonce(self.current.signer, pending) 458 work.commitTransactions(self.mux, txs, self.chain, self.coinbase) 459 460 // compute uncles for the new block. 461 var ( 462 uncles []*types.Header 463 badUncles []common.Hash 464 ) 465 for hash, uncle := range self.possibleUncles { 466 if len(uncles) == 2 { 467 break 468 } 469 if err := self.commitUncle(work, uncle.Header()); err != nil { 470 log.Trace("Bad uncle found and will be removed", "hash", hash) 471 log.Trace(fmt.Sprint(uncle)) 472 473 badUncles = append(badUncles, hash) 474 } else { 475 log.Debug("Committing new uncle to block", "hash", hash) 476 uncles = append(uncles, uncle.Header()) 477 } 478 } 479 for _, hash := range badUncles { 480 delete(self.possibleUncles, hash) 481 } 482 // Create the new block to seal with the consensus engine 483 if work.Block, err = self.engine.Finalize(self.chain, header, work.state, work.txs, uncles, work.receipts); err != nil { 484 log.Error("Failed to finalize block for sealing", "err", err) 485 return 486 } 487 // We only care about logging if we're actually mining. 488 if atomic.LoadInt32(&self.mining) == 1 { 489 log.Info("Commit new mining work", "number", work.Block.Number(), "txs", work.tcount, "uncles", len(uncles), "elapsed", common.PrettyDuration(time.Since(tstart))) 490 self.unconfirmed.Shift(work.Block.NumberU64() - 1) 491 } 492 self.push(work) 493 } 494 495 func (self *worker) commitUncle(work *Work, uncle *types.Header) error { 496 hash := uncle.Hash() 497 if work.uncles.Has(hash) { 498 return fmt.Errorf("uncle not unique") 499 } 500 if !work.ancestors.Has(uncle.ParentHash) { 501 return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4]) 502 } 503 if work.family.Has(hash) { 504 return fmt.Errorf("uncle already in family (%x)", hash) 505 } 506 work.uncles.Add(uncle.Hash()) 507 return nil 508 } 509 510 func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, bc *core.BlockChain, coinbase common.Address) { 511 gp := new(core.GasPool).AddGas(env.header.GasLimit) 512 513 var coalescedLogs []*types.Log 514 515 for { 516 // Retrieve the next transaction and abort if all done 517 tx := txs.Peek() 518 if tx == nil { 519 break 520 } 521 // Error may be ignored here. The error has already been checked 522 // during transaction acceptance is the transaction pool. 523 // 524 // We use the eip155 signer regardless of the current hf. 525 from, _ := types.Sender(env.signer, tx) 526 // Check whether the tx is replay protected. If we're not in the EIP155 hf 527 // phase, start ignoring the sender until we do. 528 if tx.Protected() && !env.config.IsEIP155(env.header.Number) { 529 log.Trace("Ignoring reply protected transaction", "hash", tx.Hash(), "eip155", env.config.EIP155Block) 530 531 txs.Pop() 532 continue 533 } 534 // Start executing the transaction 535 env.state.Prepare(tx.Hash(), common.Hash{}, env.tcount) 536 537 err, logs := env.commitTransaction(tx, bc, coinbase, gp) 538 switch err { 539 case core.ErrGasLimitReached: 540 // Pop the current out-of-gas transaction without shifting in the next from the account 541 log.Trace("Gas limit exceeded for current block", "sender", from) 542 txs.Pop() 543 544 case core.ErrNonceTooLow: 545 // New head notification data race between the transaction pool and miner, shift 546 log.Trace("Skipping transaction with low nonce", "sender", from, "nonce", tx.Nonce()) 547 txs.Shift() 548 549 case core.ErrNonceTooHigh: 550 // Reorg notification data race between the transaction pool and miner, skip account = 551 log.Trace("Skipping account with hight nonce", "sender", from, "nonce", tx.Nonce()) 552 txs.Pop() 553 554 case nil: 555 // Everything ok, collect the logs and shift in the next transaction from the same account 556 coalescedLogs = append(coalescedLogs, logs...) 557 env.tcount++ 558 txs.Shift() 559 560 default: 561 // Strange error, discard the transaction and get the next in line (note, the 562 // nonce-too-high clause will prevent us from executing in vain). 563 log.Debug("Transaction failed, account skipped", "hash", tx.Hash(), "err", err) 564 txs.Shift() 565 } 566 } 567 568 if len(coalescedLogs) > 0 || env.tcount > 0 { 569 // make a copy, the state caches the logs and these logs get "upgraded" from pending to mined 570 // logs by filling in the block hash when the block was mined by the local miner. This can 571 // cause a race condition if a log was "upgraded" before the PendingLogsEvent is processed. 572 cpy := make([]*types.Log, len(coalescedLogs)) 573 for i, l := range coalescedLogs { 574 cpy[i] = new(types.Log) 575 *cpy[i] = *l 576 } 577 go func(logs []*types.Log, tcount int) { 578 if len(logs) > 0 { 579 mux.Post(core.PendingLogsEvent{Logs: logs}) 580 } 581 if tcount > 0 { 582 mux.Post(core.PendingStateEvent{}) 583 } 584 }(cpy, env.tcount) 585 } 586 } 587 588 func (env *Work) commitTransaction(tx *types.Transaction, bc *core.BlockChain, coinbase common.Address, gp *core.GasPool) (error, []*types.Log) { 589 snap := env.state.Snapshot() 590 591 receipt, _, err := core.ApplyTransaction(env.config, bc, &coinbase, gp, env.state, env.header, tx, env.header.GasUsed, vm.Config{}) 592 if err != nil { 593 env.state.RevertToSnapshot(snap) 594 return err, nil 595 } 596 env.txs = append(env.txs, tx) 597 env.receipts = append(env.receipts, receipt) 598 599 return nil, receipt.Logs 600 }