Skip to content

Commit

Permalink
multi: source index notif. from block notif.
Browse files Browse the repository at this point in the history
This updates the index subscriber to use block notifications as the
source of index notifications.
  • Loading branch information
dnldd authored and davecgh committed Oct 20, 2021
1 parent 5c74266 commit cdf881d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
26 changes: 2 additions & 24 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,18 +747,6 @@ func (b *BlockChain) connectBlock(node *blockNode, block, parent *dcrutil.Block,
return err
}

// Notify subscribed indexes of the connected block.
if b.indexSubscriber != nil {
b.indexSubscriber.Notify(&indexers.IndexNtfn{
NtfnType: indexers.ConnectNtfn,
Block: block,
Parent: parent,
PrevScripts: prevScripter,
IsTreasuryEnabled: isTreasuryEnabled,
Done: make(chan bool),
})
}

// This node is now the end of the best chain.
b.bestChain.SetTip(node)
b.index.MaybePruneCachedTips(node)
Expand Down Expand Up @@ -796,6 +784,7 @@ func (b *BlockChain) connectBlock(node *blockNode, block, parent *dcrutil.Block,
Block: block,
ParentBlock: parent,
CheckTxFlags: checkTxFlags,
PrevScripts: prevScripter,
})
b.chainLock.Lock()

Expand Down Expand Up @@ -958,18 +947,6 @@ func (b *BlockChain) disconnectBlock(node *blockNode, block, parent *dcrutil.Blo
return err
}

// Notify subscribed indexes of the disconnected block.
if b.indexSubscriber != nil {
b.indexSubscriber.Notify(&indexers.IndexNtfn{
NtfnType: indexers.DisconnectNtfn,
Block: block,
Parent: parent,
PrevScripts: prevScripter,
IsTreasuryEnabled: isTreasuryEnabled,
Done: make(chan bool),
})
}

// This node's parent is now the end of the best chain.
b.bestChain.SetTip(node.parent)

Expand All @@ -990,6 +967,7 @@ func (b *BlockChain) disconnectBlock(node *blockNode, block, parent *dcrutil.Blo
Block: block,
ParentBlock: parent,
CheckTxFlags: checkTxFlags,
PrevScripts: prevScripter,
})
b.chainLock.Lock()

Expand Down
16 changes: 11 additions & 5 deletions blockchain/indexers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,18 @@ func recover(ctx context.Context, idx Indexer) error {
return err
}

isTreasuryEnabled, err := queryer.IsTreasuryAgendaActive(parentHash)
if err != nil {
return err
}

ntfn := &IndexNtfn{
NtfnType: DisconnectNtfn,
Block: block,
Parent: parent,
PrevScripts: prevScripts,
Done: make(chan bool),
NtfnType: DisconnectNtfn,
Block: block,
Parent: parent,
IsTreasuryEnabled: isTreasuryEnabled,
PrevScripts: prevScripts,
Done: make(chan bool),
}

err = updateIndex(ctx, idx, ntfn)
Expand Down
4 changes: 3 additions & 1 deletion blockchain/indexers/indexsubscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ func (s *IndexSubscriber) Run(ctx context.Context) {
}
}

close(ntfn.Done)
if ntfn.Done != nil {
close(ntfn.Done)
}

case <-ctx.Done():
log.Infof("Index subscriber shutting down")
Expand Down
9 changes: 9 additions & 0 deletions blockchain/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package blockchain
import (
"fmt"

"github.com/decred/dcrd/blockchain/v4/indexers"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil/v4"
)
Expand Down Expand Up @@ -131,6 +132,10 @@ type BlockConnectedNtfnsData struct {
// CheckTxFlags represents the agendas to consider as active when checking
// transactions for the block that was connected.
CheckTxFlags AgendaFlags

// PrevScripts provides access to previous transaction scripts and their
// associated versions spent by the connected block.
PrevScripts indexers.PrevScripter
}

// BlockDisconnectedNtfnsData is the structure for data indicating information
Expand All @@ -146,6 +151,10 @@ type BlockDisconnectedNtfnsData struct {
// CheckTxFlags represents the agendas to consider as active when checking
// transactions for the block that was **disconnected**.
CheckTxFlags AgendaFlags

// PrevScripts provides access to previous transaction scripts and their
// associated versions spent by the disconnected block.
PrevScripts indexers.PrevScripter
}

// ReorganizationNtfnsData is the structure for data indicating information
Expand Down
22 changes: 22 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,17 @@ func (s *server) handleBlockchainNotification(notification *blockchain.Notificat
s.bg.BlockConnected(block)
}

// Notify subscribed indexes of connected block.
if s.indexSubscriber != nil {
s.indexSubscriber.Notify(&indexers.IndexNtfn{
NtfnType: indexers.ConnectNtfn,
Block: block,
Parent: parentBlock,
IsTreasuryEnabled: isTreasuryEnabled,
PrevScripts: ntfn.PrevScripts,
})
}

// Proactively evict signature cache entries that are virtually
// guaranteed to no longer be useful.
s.proactivelyEvictSigCacheEntries(block.Height())
Expand Down Expand Up @@ -2865,6 +2876,17 @@ func (s *server) handleBlockchainNotification(notification *blockchain.Notificat
s.bg.BlockDisconnected(block)
}

// Notify subscribed indexes of disconnected block.
if s.indexSubscriber != nil {
s.indexSubscriber.Notify(&indexers.IndexNtfn{
NtfnType: indexers.DisconnectNtfn,
Block: block,
Parent: parentBlock,
IsTreasuryEnabled: isTreasuryEnabled,
PrevScripts: ntfn.PrevScripts,
})
}

// Notify registered websocket clients.
if r := s.rpcServer; r != nil {
// Filter and update the rebroadcast inventory.
Expand Down

0 comments on commit cdf881d

Please sign in to comment.