mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
miner: announce chan events synchronously
This commit is contained in:
parent
4b06d4b5b2
commit
076bc1b60f
1 changed files with 30 additions and 45 deletions
|
|
@ -125,8 +125,6 @@ type worker struct {
|
||||||
// atomic status counters
|
// atomic status counters
|
||||||
mining int32
|
mining int32
|
||||||
atWork int32
|
atWork int32
|
||||||
|
|
||||||
fullValidation bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker {
|
func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase common.Address, eth Backend, mux *event.TypeMux) *worker {
|
||||||
|
|
@ -146,7 +144,6 @@ func newWorker(config *params.ChainConfig, engine consensus.Engine, coinbase com
|
||||||
coinbase: coinbase,
|
coinbase: coinbase,
|
||||||
agents: make(map[Agent]struct{}),
|
agents: make(map[Agent]struct{}),
|
||||||
unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth),
|
unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth),
|
||||||
fullValidation: false,
|
|
||||||
}
|
}
|
||||||
// Subscribe TxPreEvent for tx pool
|
// Subscribe TxPreEvent for tx pool
|
||||||
worker.txSub = eth.TxPool().SubscribeTxPreEvent(worker.txCh)
|
worker.txSub = eth.TxPool().SubscribeTxPreEvent(worker.txCh)
|
||||||
|
|
@ -297,50 +294,38 @@ func (self *worker) wait() {
|
||||||
block := result.Block
|
block := result.Block
|
||||||
work := result.Work
|
work := result.Work
|
||||||
|
|
||||||
if self.fullValidation {
|
// Update the block hash in all logs since it is now available and not when the
|
||||||
if _, err := self.chain.InsertChain(types.Blocks{block}); err != nil {
|
// receipt/log of individual transactions were created.
|
||||||
log.Error("Mined invalid block", "err", err)
|
for _, r := range work.receipts {
|
||||||
continue
|
for _, l := range r.Logs {
|
||||||
|
l.BlockHash = block.Hash()
|
||||||
}
|
}
|
||||||
go self.mux.Post(core.NewMinedBlockEvent{Block: block})
|
|
||||||
} else {
|
|
||||||
// Update the block hash in all logs since it is now available and not when the
|
|
||||||
// receipt/log of individual transactions were created.
|
|
||||||
for _, r := range work.receipts {
|
|
||||||
for _, l := range r.Logs {
|
|
||||||
l.BlockHash = block.Hash()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, log := range work.state.Logs() {
|
|
||||||
log.BlockHash = block.Hash()
|
|
||||||
}
|
|
||||||
stat, err := self.chain.WriteBlockAndState(block, work.receipts, work.state)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed writing block to chain", "err", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if canon block and write transactions
|
|
||||||
if stat == core.CanonStatTy {
|
|
||||||
// implicit by posting ChainHeadEvent
|
|
||||||
mustCommitNewWork = false
|
|
||||||
}
|
|
||||||
// broadcast before waiting for validation
|
|
||||||
go func(block *types.Block, logs []*types.Log, receipts []*types.Receipt) {
|
|
||||||
self.mux.Post(core.NewMinedBlockEvent{Block: block})
|
|
||||||
var (
|
|
||||||
events []interface{}
|
|
||||||
coalescedLogs []*types.Log
|
|
||||||
)
|
|
||||||
events = append(events, core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
|
|
||||||
if stat == core.CanonStatTy {
|
|
||||||
events = append(events, core.ChainHeadEvent{Block: block})
|
|
||||||
coalescedLogs = logs
|
|
||||||
}
|
|
||||||
// post blockchain events
|
|
||||||
self.chain.PostChainEvents(events, coalescedLogs)
|
|
||||||
}(block, work.state.Logs(), work.receipts)
|
|
||||||
}
|
}
|
||||||
|
for _, log := range work.state.Logs() {
|
||||||
|
log.BlockHash = block.Hash()
|
||||||
|
}
|
||||||
|
stat, err := self.chain.WriteBlockAndState(block, work.receipts, work.state)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed writing block to chain", "err", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// check if canon block and write transactions
|
||||||
|
if stat == core.CanonStatTy {
|
||||||
|
// implicit by posting ChainHeadEvent
|
||||||
|
mustCommitNewWork = false
|
||||||
|
}
|
||||||
|
// Broadcast the block and announce chain insertion event
|
||||||
|
self.mux.Post(core.NewMinedBlockEvent{Block: block})
|
||||||
|
var (
|
||||||
|
events []interface{}
|
||||||
|
logs = work.state.Logs()
|
||||||
|
)
|
||||||
|
events = append(events, core.ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
|
||||||
|
if stat == core.CanonStatTy {
|
||||||
|
events = append(events, core.ChainHeadEvent{Block: block})
|
||||||
|
}
|
||||||
|
self.chain.PostChainEvents(events, logs)
|
||||||
|
|
||||||
// Insert the block into the set of pending ones to wait for confirmations
|
// Insert the block into the set of pending ones to wait for confirmations
|
||||||
self.unconfirmed.Insert(block.NumberU64(), block.Hash())
|
self.unconfirmed.Insert(block.NumberU64(), block.Hash())
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue