Skip to content

Commit 23c8c80

Browse files
committed
notification: make each reorg handler indicate when it is finished
Do not leave OnReorganization before each handler sets the reorg flag. Make reorg channels unbuffered.
1 parent ce05eab commit 23c8c80

6 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎blockdata/chainmonitor.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) 2018, The Decred developers
12
// Copyright (c) 2017, Jonathan Chappelow
23
// See LICENSE for details.
34

@@ -17,6 +18,7 @@ type ReorgData struct {
1718
OldChainHeight int32
1819
NewChainHead chainhash.Hash
1920
NewChainHeight int32
21+
WG *sync.WaitGroup
2022
}
2123

2224
// for getblock, ticketfeeinfo, estimatestakediff, etc.
@@ -233,6 +235,8 @@ out:
233235
log.Infof("Reorganize started in blockdata. OLD head block %v at height %d.",
234236
oldHash, oldHeight)
235237

238+
reorgData.WG.Done()
239+
236240
case _, ok := <-p.quit:
237241
if !ok {
238242
log.Debugf("Got quit signal. Exiting reorg notification handler.")

‎db/dcrsqlite/chainmonitor.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) 2018, The Decred developers
12
// Copyright (c) 2017, Jonathan Chappelow
23
// See LICENSE for details.
34

@@ -17,6 +18,7 @@ type ReorgData struct {
1718
OldChainHeight int32
1819
NewChainHead chainhash.Hash
1920
NewChainHeight int32
21+
WG *sync.WaitGroup
2022
}
2123

2224
// ChainMonitor handles change notifications from the node client
@@ -247,6 +249,8 @@ out:
247249
log.Infof("Reorganize started. OLD head block %v at height %d.",
248250
oldHash, oldHeight)
249251

252+
reorgData.WG.Done()
253+
250254
case _, ok := <-p.quit:
251255
if !ok {
252256
log.Debugf("Got quit signal. Exiting reorg notification handler.")

‎ntfnchans.go‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) 2018, The Decred developers
12
// Copyright (c) 2017, Jonathan Chappelow
23
// See LICENSE for details.
34

@@ -25,14 +26,12 @@ const (
2526
// expNewTxChanBuffer is the size of the new transaction buffer for explorer
2627
expNewTxChanBuffer = 70
2728

28-
reorgBuffer = 2
29-
3029
// relevantMempoolTxChanBuffer is the size of the new transaction channel
3130
// buffer, for relevant transactions that are added into mempool.
3231
//relevantMempoolTxChanBuffer = 2048
3332
)
3433

35-
// Channels are package-level variables for simplicity
34+
// NtfnChans collects the chain server notification channels
3635
var ntfnChans struct {
3736
connectChan chan *chainhash.Hash
3837
reorgChanBlockData chan *blockdata.ReorgData
@@ -63,9 +62,9 @@ func makeNtfnChans(cfg *config) {
6362
ntfnChans.connectChanStakeDB = make(chan *chainhash.Hash)
6463

6564
// Reorg data channels
66-
ntfnChans.reorgChanBlockData = make(chan *blockdata.ReorgData, reorgBuffer)
67-
ntfnChans.reorgChanWiredDB = make(chan *dcrsqlite.ReorgData, reorgBuffer)
68-
ntfnChans.reorgChanStakeDB = make(chan *stakedb.ReorgData, reorgBuffer)
65+
ntfnChans.reorgChanBlockData = make(chan *blockdata.ReorgData)
66+
ntfnChans.reorgChanWiredDB = make(chan *dcrsqlite.ReorgData)
67+
ntfnChans.reorgChanStakeDB = make(chan *stakedb.ReorgData)
6968

7069
// To update app status
7170
ntfnChans.updateStatusNodeHeight = make(chan uint32, blockConnChanBuffer)

‎ntfnhandlers.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,49 @@ func makeNodeNtfnHandlers(cfg *config) (*rpcclient.NotificationHandlers, *collec
152152
},
153153
OnReorganization: func(oldHash *chainhash.Hash, oldHeight int32,
154154
newHash *chainhash.Hash, newHeight int32) {
155+
wg := new(sync.WaitGroup)
155156
// Send reorg data to dcrsqlite's monitor
157+
wg.Add(1)
156158
select {
157159
case ntfnChans.reorgChanWiredDB <- &dcrsqlite.ReorgData{
158160
OldChainHead: *oldHash,
159161
OldChainHeight: oldHeight,
160162
NewChainHead: *newHash,
161163
NewChainHeight: newHeight,
164+
WG: wg,
162165
}:
163166
default:
167+
wg.Done()
164168
}
169+
165170
// Send reorg data to blockdata's monitor (so that it stops collecting)
171+
wg.Add(1)
166172
select {
167173
case ntfnChans.reorgChanBlockData <- &blockdata.ReorgData{
168174
OldChainHead: *oldHash,
169175
OldChainHeight: oldHeight,
170176
NewChainHead: *newHash,
171177
NewChainHeight: newHeight,
178+
WG: wg,
172179
}:
173180
default:
181+
wg.Done()
174182
}
183+
175184
// Send reorg data to stakedb's monitor
185+
wg.Add(1)
176186
select {
177187
case ntfnChans.reorgChanStakeDB <- &stakedb.ReorgData{
178188
OldChainHead: *oldHash,
179189
OldChainHeight: oldHeight,
180190
NewChainHead: *newHash,
181191
NewChainHeight: newHeight,
192+
WG: wg,
182193
}:
183194
default:
195+
wg.Done()
184196
}
197+
wg.Wait()
185198
},
186199

187200
OnWinningTickets: func(blockHash *chainhash.Hash, blockHeight int64,

‎stakedb/chainmonitor.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) 2018, The Decred developers
12
// Copyright (c) 2017, Jonathan Chappelow
23
// See LICENSE for details.
34

@@ -17,6 +18,7 @@ type ReorgData struct {
1718
OldChainHeight int32
1819
NewChainHead chainhash.Hash
1920
NewChainHeight int32
21+
WG *sync.WaitGroup
2022
}
2123

2224
// ChainMonitor connects blocks to the stake DB as they come in.
@@ -241,6 +243,8 @@ out:
241243
log.Infof("Reorganize started. OLD head block %v at height %d.",
242244
oldHash, oldHeight)
243245

246+
reorgData.WG.Done()
247+
244248
case _, ok := <-p.quit:
245249
if !ok {
246250
log.Debugf("Got quit signal. Exiting reorg notification handler.")

‎stakedb/stakedb.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Copyright (c) 2018, The dcrdata developers.
1+
// Copyright (c) 2018, The Decred developers
2+
// Copyright (c) 2018, The dcrdata developers
23
// Copyright (c) 2017, Jonathan Chappelow
34
// See LICENSE for details.
45

@@ -417,11 +418,15 @@ func (db *StakeDatabase) connectBlock(block *dcrutil.Block, spent []chainhash.Ha
417418
return fmt.Errorf("unable to serialize block header: %v", err)
418419
}
419420

420-
db.BestNode, err = db.BestNode.ConnectNode(stake.CalcHash256PRNGIV(hB),
421+
bestNode, err := db.BestNode.ConnectNode(stake.CalcHash256PRNGIV(hB),
421422
spent, revoked, maturing)
422423
if err != nil {
423424
return err
424425
}
426+
if bestNode == nil {
427+
return fmt.Errorf("failed to ConnectNode at BestNode")
428+
}
429+
db.BestNode = bestNode
425430

426431
if err = db.StakeDB.Update(func(dbTx database.Tx) error {
427432
return stake.WriteConnectedBestNode(dbTx, db.BestNode, *block.Hash())
@@ -506,6 +511,9 @@ func (db *StakeDatabase) disconnectBlock() error {
506511
if err != nil {
507512
return err
508513
}
514+
if parentStakeNode == nil {
515+
return fmt.Errorf("failed to DisconnectNode at BestNode")
516+
}
509517
db.BestNode = parentStakeNode
510518

511519
return db.StakeDB.Update(func(dbTx database.Tx) error {
@@ -641,6 +649,11 @@ func (db *StakeDatabase) expires() ([]chainhash.Hash, []bool) {
641649
// structure including ticket pool value, size, and average value.
642650
func (db *StakeDatabase) PoolInfoBest() *apitypes.TicketPoolInfo {
643651
db.nodeMtx.RLock()
652+
if db.BestNode == nil {
653+
db.nodeMtx.RUnlock()
654+
log.Errorf("PoolInfoBest: BestNode is nil!")
655+
return nil
656+
}
644657
//poolSize := db.BestNode.PoolSize()
645658
liveTickets := db.BestNode.LiveTickets()
646659
winningTickets := db.BestNode.Winners()

0 commit comments

Comments
 (0)
⌂ 换网址