@@ -55,26 +55,20 @@ type Agent interface {
5555 GetHashRate () int64
5656}
5757
58- type uint64RingBuffer struct {
59- ints []uint64 //array of all integers in buffer
60- next int //where is the next insertion? assert 0 <= next < len(ints)
61- }
62-
6358// Work is the workers current environment and holds
6459// all of the current state information
6560type Work struct {
6661 config * params.ChainConfig
6762 signer types.Signer
6863
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- ownedAccounts * set.Set
75- lowGasTxs types.Transactions
76- failedTxs types.Transactions
77- localMinedBlocks * uint64RingBuffer // the most recent block numbers that were mined locally (used to check block inclusion)
64+ state * state.StateDB // apply state changes here
65+ ancestors * set.Set // ancestor set (used for checking uncle parent validity)
66+ family * set.Set // family set (used for checking uncle invalidity)
67+ uncles * set.Set // uncle set
68+ tcount int // tx count in cycle
69+ ownedAccounts * set.Set
70+ lowGasTxs types.Transactions
71+ failedTxs types.Transactions
7872
7973 Block * types.Block // the new block
8074
@@ -123,6 +117,8 @@ type worker struct {
123117 txQueueMu sync.Mutex
124118 txQueue map [common.Hash ]* types.Transaction
125119
120+ unconfirmed * unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations
121+
126122 // atomic status counters
127123 mining int32
128124 atWork int32
@@ -144,6 +140,7 @@ func newWorker(config *params.ChainConfig, coinbase common.Address, eth Backend,
144140 coinbase : coinbase ,
145141 txQueue : make (map [common.Hash ]* types.Transaction ),
146142 agents : make (map [Agent ]struct {}),
143+ unconfirmed : newUnconfirmedBlocks (eth .BlockChain (), 5 ),
147144 fullValidation : false ,
148145 }
149146 worker .events = worker .mux .Subscribe (core.ChainHeadEvent {}, core.ChainSideEvent {}, core.TxPreEvent {})
@@ -269,18 +266,6 @@ func (self *worker) update() {
269266 }
270267}
271268
272- func newLocalMinedBlock (blockNumber uint64 , prevMinedBlocks * uint64RingBuffer ) (minedBlocks * uint64RingBuffer ) {
273- if prevMinedBlocks == nil {
274- minedBlocks = & uint64RingBuffer {next : 0 , ints : make ([]uint64 , miningLogAtDepth + 1 )}
275- } else {
276- minedBlocks = prevMinedBlocks
277- }
278-
279- minedBlocks .ints [minedBlocks .next ] = blockNumber
280- minedBlocks .next = (minedBlocks .next + 1 ) % len (minedBlocks .ints )
281- return minedBlocks
282- }
283-
284269func (self * worker ) wait () {
285270 for {
286271 mustCommitNewWork := true
@@ -355,17 +340,8 @@ func (self *worker) wait() {
355340 }
356341 }(block , work .state .Logs (), work .receipts )
357342 }
358-
359- // check staleness and display confirmation
360- var stale , confirm string
361- canonBlock := self .chain .GetBlockByNumber (block .NumberU64 ())
362- if canonBlock != nil && canonBlock .Hash () != block .Hash () {
363- stale = "stale "
364- } else {
365- confirm = "Wait 5 blocks for confirmation"
366- work .localMinedBlocks = newLocalMinedBlock (block .Number ().Uint64 (), work .localMinedBlocks )
367- }
368- glog .V (logger .Info ).Infof ("🔨 Mined %sblock (#%v / %x). %s" , stale , block .Number (), block .Hash ().Bytes ()[:4 ], confirm )
343+ // Insert the block into the set of pending ones to wait for confirmations
344+ self .unconfirmed .Insert (block .NumberU64 (), block .Hash ())
369345
370346 if mustCommitNewWork {
371347 self .commitNewWork ()
@@ -417,9 +393,6 @@ func (self *worker) makeCurrent(parent *types.Block, header *types.Header) error
417393 // Keep track of transactions which return errors so they can be removed
418394 work .tcount = 0
419395 work .ownedAccounts = accountAddressesSet (accounts )
420- if self .current != nil {
421- work .localMinedBlocks = self .current .localMinedBlocks
422- }
423396 self .current = work
424397 return nil
425398}
@@ -435,38 +408,6 @@ func (w *worker) setGasPrice(p *big.Int) {
435408 w .mux .Post (core.GasPriceChanged {Price : w .gasPrice })
436409}
437410
438- func (self * worker ) isBlockLocallyMined (current * Work , deepBlockNum uint64 ) bool {
439- //Did this instance mine a block at {deepBlockNum} ?
440- var isLocal = false
441- for idx , blockNum := range current .localMinedBlocks .ints {
442- if deepBlockNum == blockNum {
443- isLocal = true
444- current .localMinedBlocks .ints [idx ] = 0 //prevent showing duplicate logs
445- break
446- }
447- }
448- //Short-circuit on false, because the previous and following tests must both be true
449- if ! isLocal {
450- return false
451- }
452-
453- //Does the block at {deepBlockNum} send earnings to my coinbase?
454- var block = self .chain .GetBlockByNumber (deepBlockNum )
455- return block != nil && block .Coinbase () == self .coinbase
456- }
457-
458- func (self * worker ) logLocalMinedBlocks (current , previous * Work ) {
459- if previous != nil && current .localMinedBlocks != nil {
460- nextBlockNum := current .Block .NumberU64 ()
461- for checkBlockNum := previous .Block .NumberU64 (); checkBlockNum < nextBlockNum ; checkBlockNum ++ {
462- inspectBlockNum := checkBlockNum - miningLogAtDepth
463- if self .isBlockLocallyMined (current , inspectBlockNum ) {
464- glog .V (logger .Info ).Infof ("🔨 🔗 Mined %d blocks back: block #%v" , miningLogAtDepth , inspectBlockNum )
465- }
466- }
467- }
468- }
469-
470411func (self * worker ) commitNewWork () {
471412 self .mu .Lock ()
472413 defer self .mu .Unlock ()
@@ -513,7 +454,6 @@ func (self *worker) commitNewWork() {
513454 }
514455 }
515456 }
516- previous := self .current
517457 // Could potentially happen if starting to mine in an odd state.
518458 err := self .makeCurrent (parent , header )
519459 if err != nil {
@@ -574,7 +514,7 @@ func (self *worker) commitNewWork() {
574514 // We only care about logging if we're actually mining.
575515 if atomic .LoadInt32 (& self .mining ) == 1 {
576516 glog .V (logger .Info ).Infof ("commit new work on block %v with %d txs & %d uncles. Took %v\n " , work .Block .Number (), work .tcount , len (uncles ), time .Since (tstart ))
577- self .logLocalMinedBlocks (work , previous )
517+ self .unconfirmed . Shift (work . Block . NumberU64 () - 1 )
578518 }
579519 self .push (work )
580520}
0 commit comments