chainntnfs/tx_notifier: extract conf reorg dispatch into method
This commit is contained in:
parent
5ae8243d0d
commit
589dc96d88
@ -29,6 +29,9 @@ type ConfNtfn struct {
|
|||||||
|
|
||||||
// PkScript is the public key script of an outpoint created in this
|
// PkScript is the public key script of an outpoint created in this
|
||||||
// transaction.
|
// transaction.
|
||||||
|
//
|
||||||
|
// NOTE: This value MUST be set when the dispatch is to be performed
|
||||||
|
// using compact filters.
|
||||||
PkScript []byte
|
PkScript []byte
|
||||||
|
|
||||||
// NumConfirmations is the number of confirmations after which the
|
// NumConfirmations is the number of confirmations after which the
|
||||||
@ -60,6 +63,9 @@ type HistoricalConfDispatch struct {
|
|||||||
|
|
||||||
// PkScript is a public key script from an output created by this
|
// PkScript is a public key script from an output created by this
|
||||||
// transaction.
|
// transaction.
|
||||||
|
//
|
||||||
|
// NOTE: This value MUST be set when the dispatch is to be performed
|
||||||
|
// using compact filters.
|
||||||
PkScript []byte
|
PkScript []byte
|
||||||
|
|
||||||
// StartHeight specifies the block height at which to being the
|
// StartHeight specifies the block height at which to being the
|
||||||
@ -709,16 +715,50 @@ func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
|
|||||||
|
|
||||||
// Then, we'll check if the current transaction
|
// Then, we'll check if the current transaction
|
||||||
// was included in the block currently being
|
// was included in the block currently being
|
||||||
// disconnected. If it was, we'll need to take
|
// disconnected. If it was, we'll need to
|
||||||
// some necessary precautions.
|
// dispatch a reorg notification to the client.
|
||||||
if initialHeight == blockHeight {
|
if initialHeight == blockHeight {
|
||||||
// If the transaction's confirmation notification
|
err := tcn.dispatchConfReorg(
|
||||||
// has already been dispatched, we'll attempt to
|
ntfn, blockHeight,
|
||||||
// notify the client it was reorged out of the chain.
|
)
|
||||||
if ntfn.dispatched {
|
if err != nil {
|
||||||
// Attempt to drain the confirmation notification
|
return err
|
||||||
// to ensure sends to the Confirmed channel are
|
}
|
||||||
// always non-blocking.
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, we can remove the transactions we're currently watching that
|
||||||
|
// were included in this block height.
|
||||||
|
delete(tcn.txsByInitialHeight, blockHeight)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// dispatchConfReorg dispatches a reorg notification to the client if the
|
||||||
|
// confirmation notification was already delivered.
|
||||||
|
//
|
||||||
|
// NOTE: This must be called with the TxNotifier's lock held.
|
||||||
|
func (tcn *TxConfNotifier) dispatchConfReorg(
|
||||||
|
ntfn *ConfNtfn, heightDisconnected uint32) error {
|
||||||
|
|
||||||
|
// If the transaction's confirmation notification has yet to be
|
||||||
|
// dispatched, we'll need to clear its entry within the
|
||||||
|
// ntfnsByConfirmHeight index to prevent from notifiying the client once
|
||||||
|
// the notifier reaches the confirmation height.
|
||||||
|
if !ntfn.dispatched {
|
||||||
|
confHeight := heightDisconnected + ntfn.NumConfirmations - 1
|
||||||
|
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
|
||||||
|
if exists {
|
||||||
|
delete(ntfnSet, ntfn)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, the entry within the ntfnsByConfirmHeight has already been
|
||||||
|
// deleted, so we'll attempt to drain the confirmation notification to
|
||||||
|
// ensure sends to the Confirmed channel are always non-blocking.
|
||||||
select {
|
select {
|
||||||
case <-ntfn.Event.Confirmed:
|
case <-ntfn.Event.Confirmed:
|
||||||
case <-tcn.quit:
|
case <-tcn.quit:
|
||||||
@ -728,47 +768,14 @@ func (tcn *TxConfNotifier) DisconnectTip(blockHeight uint32) error {
|
|||||||
|
|
||||||
ntfn.dispatched = false
|
ntfn.dispatched = false
|
||||||
|
|
||||||
// Send a negative confirmation notification to the
|
// Send a negative confirmation notification to the client indicating
|
||||||
// client indicating how many blocks have been
|
// how many blocks have been disconnected successively.
|
||||||
// disconnected successively.
|
|
||||||
select {
|
select {
|
||||||
case ntfn.Event.NegativeConf <- int32(tcn.reorgDepth):
|
case ntfn.Event.NegativeConf <- int32(tcn.reorgDepth):
|
||||||
case <-tcn.quit:
|
case <-tcn.quit:
|
||||||
return ErrTxConfNotifierExiting
|
return ErrTxConfNotifierExiting
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, since the transactions was reorged out
|
|
||||||
// of the chain, we can safely remove its accompanying
|
|
||||||
// confirmation notification.
|
|
||||||
confHeight := blockHeight + ntfn.NumConfirmations - 1
|
|
||||||
ntfnSet, exists := tcn.ntfnsByConfirmHeight[confHeight]
|
|
||||||
if !exists {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
delete(ntfnSet, ntfn)
|
|
||||||
|
|
||||||
// Intuitively, we should also remove
|
|
||||||
// the txHash from confNotifications if
|
|
||||||
// the ntfnSet is now empty. However, we
|
|
||||||
// will not do so since we may want to
|
|
||||||
// continue rewinding the height hints
|
|
||||||
// for this txid.
|
|
||||||
//
|
|
||||||
// NOTE(conner): safe to delete if
|
|
||||||
// blockHeight is below client-provided
|
|
||||||
// height hint?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finally, we can remove the transactions we're currently watching that
|
|
||||||
// were included in this block height.
|
|
||||||
delete(tcn.txsByInitialHeight, blockHeight)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user