diff options
author | Determinant <[email protected]> | 2020-09-17 03:08:39 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2020-09-17 03:08:39 -0400 |
commit | aedd4b46cdc0a48c1b06a3e920bf57f8c28d401d (patch) | |
tree | 2614d9060c915cb368828e76c4a2d9a2bc20c61f | |
parent | 8a12d92f6460bc6caa1217cbfadb7799670c3b1c (diff) |
fix the block completion race
-rw-r--r-- | coreth.go | 5 | ||||
-rw-r--r-- | miner/miner.go | 4 | ||||
-rw-r--r-- | plugin/evm/vm.go | 21 |
3 files changed, 21 insertions, 9 deletions
@@ -17,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/trie" //"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" @@ -85,6 +86,10 @@ func (self *ETHChain) GenBlock() { self.backend.Miner().GenBlock() } +func (self *ETHChain) SubscribeNewMinedBlockEvent() *event.TypeMuxSubscription { + return self.backend.Miner().GetWorkerMux().Subscribe(core.NewMinedBlockEvent{}) +} + func (self *ETHChain) VerifyBlock(block *types.Block) bool { txnHash := types.DeriveSha(block.Transactions(), new(trie.Trie)) uncleHash := types.CalcUncleHash(block.Uncles()) diff --git a/miner/miner.go b/miner/miner.go index e8e59a4..bbe704f 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -135,3 +135,7 @@ func (miner *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscript func (miner *Miner) GenBlock() { miner.worker.genBlock() } + +func (miner *Miner) GetWorkerMux() *event.TypeMux { + return miner.worker.mux +} diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index c6bc728..c9836c6 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -160,7 +160,7 @@ type VM struct { chaindb Database newBlockChan chan *Block networkChan chan<- commonEng.Message - newTxPoolHeadChan chan core.NewTxPoolHeadEvent + newTxPoolHeadChan *event.TypeMuxSubscription acceptedDB database.Database @@ -344,23 +344,26 @@ func (vm *VM) Initialize( vm.bdTimerState = bdTimerStateLong vm.bdGenWaitFlag = true - vm.newTxPoolHeadChan = make(chan core.NewTxPoolHeadEvent, 1) + //vm.newTxPoolHeadChan = make(chan core.NewTxPoolHeadEvent, 1) vm.txPoolStabilizedOk = make(chan struct{}, 1) // TODO: read size from options vm.pendingAtomicTxs = make(chan *Tx, 1024) vm.atomicTxSubmitChan = make(chan struct{}, 1) - chain.GetTxPool().SubscribeNewHeadEvent(vm.newTxPoolHeadChan) + //chain.GetTxPool().SubscribeNewHeadEvent(vm.newTxPoolHeadChan) + vm.newTxPoolHeadChan = vm.chain.SubscribeNewMinedBlockEvent() // TODO: shutdown this go routine go ctx.Log.RecoverAndPanic(func() { for { select { - case h := <-vm.newTxPoolHeadChan: - vm.txPoolStabilizedLock.Lock() - if vm.txPoolStabilizedHead == h.Block.Hash() { - vm.txPoolStabilizedOk <- struct{}{} - vm.txPoolStabilizedHead = common.Hash{} + case e := <-vm.newTxPoolHeadChan.Chan(): + switch h := e.Data.(core.NewTxPoolHeadEvent) { + vm.txPoolStabilizedLock.Lock() + if vm.txPoolStabilizedHead == h.Block.Hash() { + vm.txPoolStabilizedOk <- struct{}{} + vm.txPoolStabilizedHead = common.Hash{} + } + vm.txPoolStabilizedLock.Unlock() } - vm.txPoolStabilizedLock.Unlock() } } }) |