diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-10-12 20:04:38 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-10-12 21:22:03 +0800 |
commit | 402fd6e8c6a2e379351e0aae10a833fae6bcae6c (patch) | |
tree | 30ab93e93af7c70e5df213eb3665f51a293bc4a9 /xeth | |
parent | 315a422ba754eae10db21990a809f608f7af62d4 (diff) | |
download | dexon-402fd6e8c6a2e379351e0aae10a833fae6bcae6c.tar.gz dexon-402fd6e8c6a2e379351e0aae10a833fae6bcae6c.tar.zst dexon-402fd6e8c6a2e379351e0aae10a833fae6bcae6c.zip |
core, eth, event, miner, xeth: fix event post / subscription race
Diffstat (limited to 'xeth')
-rw-r--r-- | xeth/xeth.go | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/xeth/xeth.go b/xeth/xeth.go index da712a984..13e171270 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -244,30 +244,37 @@ func (self *XEth) State() *State { return self.state } func (self *XEth) UpdateState() (wait chan *big.Int) { wait = make(chan *big.Int) go func() { - sub := self.backend.EventMux().Subscribe(core.ChainHeadEvent{}) + eventSub := self.backend.EventMux().Subscribe(core.ChainHeadEvent{}) + defer eventSub.Unsubscribe() + var m, n *big.Int var ok bool - out: + + eventCh := eventSub.Chan() for { select { - case event := <-sub.Chan(): - ev, ok := event.(core.ChainHeadEvent) - if ok { - m = ev.Block.Number() + case event, ok := <-eventCh: + if !ok { + // Event subscription closed, set the channel to nil to stop spinning + eventCh = nil + continue + } + // A real event arrived, process if new head block assignment + if event, ok := event.Data.(core.ChainHeadEvent); ok { + m = event.Block.Number() if n != nil && n.Cmp(m) < 0 { wait <- n n = nil } - statedb := state.New(ev.Block.Root(), self.backend.ChainDb()) + statedb := state.New(event.Block.Root(), self.backend.ChainDb()) self.state = NewState(self, statedb) } case n, ok = <-wait: if !ok { - break out + return } } } - sub.Unsubscribe() }() return } |