chainntnfs: notify BlockHeight in Updates event
Enhance the ConfirmationEvent's Updates channel by including the BlockHeight alongside NumConfsLeft. Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
This commit is contained in:
3 files changed
+87
-64
No files matched your search
+18
-3
@@ -203,6 +203,20 @@ type TxConfirmation struct {
|
|||||||
Block *wire.MsgBlock
|
Block *wire.MsgBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TxUpdateInfo contains information about a transaction before it has reached
|
||||||
|
// its required number of confirmations. Transactions are registered for
|
||||||
|
// notification for a specific number of "required" confirmations, this struct
|
||||||
|
// will update the caller incrementally after each new block is found as long as
|
||||||
|
// the transaction is not yet fully regarded as confirmed.
|
||||||
|
type TxUpdateInfo struct {
|
||||||
|
// BlockHeight is the height of the block that contains the transaction.
|
||||||
|
BlockHeight uint32
|
||||||
|
|
||||||
|
// NumConfsLeft is the number of confirmations left for the transaction
|
||||||
|
// to be regarded as fully confirmed.
|
||||||
|
NumConfsLeft uint32
|
||||||
|
}
|
||||||
|
|
||||||
// ConfirmationEvent encapsulates a confirmation notification. With this struct,
|
// ConfirmationEvent encapsulates a confirmation notification. With this struct,
|
||||||
// callers can be notified of: the instance the target txid reaches the targeted
|
// callers can be notified of: the instance the target txid reaches the targeted
|
||||||
// number of confirmations, how many confirmations are left for the target txid
|
// number of confirmations, how many confirmations are left for the target txid
|
||||||
@@ -229,11 +243,12 @@ type ConfirmationEvent struct {
|
|||||||
|
|
||||||
// Updates is a channel that will sent upon, at every incremental
|
// Updates is a channel that will sent upon, at every incremental
|
||||||
// confirmation, how many confirmations are left to declare the
|
// confirmation, how many confirmations are left to declare the
|
||||||
// transaction as fully confirmed.
|
// transaction as fully confirmed, along with the height of the block
|
||||||
|
// that contains the transaction.
|
||||||
//
|
//
|
||||||
// NOTE: This channel must be buffered with the number of required
|
// NOTE: This channel must be buffered with the number of required
|
||||||
// confirmations.
|
// confirmations.
|
||||||
Updates chan uint32
|
Updates chan TxUpdateInfo
|
||||||
|
|
||||||
// NegativeConf is a channel that will be sent upon if the transaction
|
// NegativeConf is a channel that will be sent upon if the transaction
|
||||||
// confirms, but is later reorged out of the chain. The integer sent
|
// confirms, but is later reorged out of the chain. The integer sent
|
||||||
@@ -262,7 +277,7 @@ func NewConfirmationEvent(numConfs uint32, cancel func()) *ConfirmationEvent {
|
|||||||
// the channel so we need to create a larger buffer to avoid
|
// the channel so we need to create a larger buffer to avoid
|
||||||
// blocking the notifier.
|
// blocking the notifier.
|
||||||
Confirmed: make(chan *TxConfirmation, 1),
|
Confirmed: make(chan *TxConfirmation, 1),
|
||||||
Updates: make(chan uint32, numConfs),
|
Updates: make(chan TxUpdateInfo, numConfs),
|
||||||
NegativeConf: make(chan int32, 1),
|
NegativeConf: make(chan int32, 1),
|
||||||
Done: make(chan struct{}, 1),
|
Done: make(chan struct{}, 1),
|
||||||
Cancel: cancel,
|
Cancel: cancel,
|
||||||
|
|||||||
+28
-12
@@ -947,8 +947,12 @@ func (n *TxNotifier) dispatchConfDetails(
|
|||||||
|
|
||||||
// We'll send a 0 value to the Updates channel,
|
// We'll send a 0 value to the Updates channel,
|
||||||
// indicating that the transaction/output script has already
|
// indicating that the transaction/output script has already
|
||||||
// been confirmed.
|
// been confirmed, and include the block height at which the
|
||||||
err := n.notifyNumConfsLeft(ntfn, 0)
|
// transaction was included.
|
||||||
|
err := n.notifyNumConfsLeft(ntfn, TxUpdateInfo{
|
||||||
|
NumConfsLeft: 0,
|
||||||
|
BlockHeight: details.BlockHeight,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -977,7 +981,10 @@ func (n *TxNotifier) dispatchConfDetails(
|
|||||||
// confirmations are left for the transaction/output script to
|
// confirmations are left for the transaction/output script to
|
||||||
// be confirmed.
|
// be confirmed.
|
||||||
numConfsLeft := confHeight - n.currentHeight
|
numConfsLeft := confHeight - n.currentHeight
|
||||||
err := n.notifyNumConfsLeft(ntfn, numConfsLeft)
|
err := n.notifyNumConfsLeft(ntfn, TxUpdateInfo{
|
||||||
|
NumConfsLeft: numConfsLeft,
|
||||||
|
BlockHeight: details.BlockHeight,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1731,7 +1738,10 @@ func (n *TxNotifier) NotifyHeight(height uint32) error {
|
|||||||
for confRequest := range confRequests {
|
for confRequest := range confRequests {
|
||||||
confSet := n.confNotifications[confRequest]
|
confSet := n.confNotifications[confRequest]
|
||||||
for _, ntfn := range confSet.ntfns {
|
for _, ntfn := range confSet.ntfns {
|
||||||
txConfHeight := confSet.details.BlockHeight +
|
// blockHeight is the height of the block which
|
||||||
|
// contains the transaction.
|
||||||
|
blockHeight := confSet.details.BlockHeight
|
||||||
|
txConfHeight := blockHeight +
|
||||||
ntfn.NumConfirmations - 1
|
ntfn.NumConfirmations - 1
|
||||||
numConfsLeft := txConfHeight - height
|
numConfsLeft := txConfHeight - height
|
||||||
|
|
||||||
@@ -1744,7 +1754,10 @@ func (n *TxNotifier) NotifyHeight(height uint32) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err := n.notifyNumConfsLeft(ntfn, numConfsLeft)
|
err := n.notifyNumConfsLeft(ntfn, TxUpdateInfo{
|
||||||
|
NumConfsLeft: numConfsLeft,
|
||||||
|
BlockHeight: blockHeight,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -2113,25 +2126,28 @@ func (n *TxNotifier) TearDown() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// notifyNumConfsLeft sends the number of confirmations left to the
|
// notifyNumConfsLeft sends the number of confirmations left to the
|
||||||
// notification subscriber through the Event.Updates channel.
|
// notification subscriber through the Event.Updates channel, along with the
|
||||||
|
// block height in which the transaction was included.
|
||||||
//
|
//
|
||||||
// NOTE: must be used with the TxNotifier's lock held.
|
// NOTE: must be used with the TxNotifier's lock held.
|
||||||
func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn, num uint32) error {
|
func (n *TxNotifier) notifyNumConfsLeft(ntfn *ConfNtfn,
|
||||||
|
info TxUpdateInfo) error {
|
||||||
|
|
||||||
// If the number left is no less than the recorded value, we can skip
|
// If the number left is no less than the recorded value, we can skip
|
||||||
// sending it as it means this same value has already been sent before.
|
// sending it as it means this same value has already been sent before.
|
||||||
if num >= ntfn.numConfsLeft {
|
if info.NumConfsLeft >= ntfn.numConfsLeft {
|
||||||
Log.Debugf("Skipped dispatched update (numConfsLeft=%v) for "+
|
Log.Debugf("Skipped dispatched update (numConfsLeft=%v) for "+
|
||||||
"request %v conf_id=%v", num, ntfn.ConfRequest,
|
"request %v conf_id=%v", info.NumConfsLeft,
|
||||||
ntfn.ConfID)
|
ntfn.ConfRequest, ntfn.ConfID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the number of confirmations left to the notification.
|
// Update the number of confirmations left to the notification.
|
||||||
ntfn.numConfsLeft = num
|
ntfn.numConfsLeft = info.NumConfsLeft
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case ntfn.Event.Updates <- num:
|
case ntfn.Event.Updates <- info:
|
||||||
case <-n.quit:
|
case <-n.quit:
|
||||||
return ErrTxNotifierExiting
|
return ErrTxNotifierExiting
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -271,13 +271,12 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
|
|||||||
// We should only receive one update for tx1 since it only requires
|
// We should only receive one update for tx1 since it only requires
|
||||||
// one confirmation and it already met it.
|
// one confirmation and it already met it.
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn1.Event.Updates:
|
case updDetails := <-ntfn1.Event.Updates:
|
||||||
const expected = 0
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 0,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx1 "+
|
BlockHeight: 11,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx1")
|
t.Fatal("Expected confirmation update for tx1")
|
||||||
}
|
}
|
||||||
@@ -300,13 +299,12 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
|
|||||||
// We should only receive one update for tx2 since it only has one
|
// We should only receive one update for tx2 since it only has one
|
||||||
// confirmation so far and it requires two.
|
// confirmation so far and it requires two.
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn2.Event.Updates:
|
case updDetails := <-ntfn2.Event.Updates:
|
||||||
const expected = 1
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 1,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
BlockHeight: 11,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx2")
|
t.Fatal("Expected confirmation update for tx2")
|
||||||
}
|
}
|
||||||
@@ -341,13 +339,12 @@ func TestTxNotifierFutureConfDispatch(t *testing.T) {
|
|||||||
// We should only receive one update since the last at the new height,
|
// We should only receive one update since the last at the new height,
|
||||||
// indicating how many confirmations are still left.
|
// indicating how many confirmations are still left.
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn2.Event.Updates:
|
case updDetails := <-ntfn2.Event.Updates:
|
||||||
const expected = 0
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 0,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
BlockHeight: 11,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx2")
|
t.Fatal("Expected confirmation update for tx2")
|
||||||
}
|
}
|
||||||
@@ -411,13 +408,12 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
|
|||||||
err = n.UpdateConfDetails(ntfn1.HistoricalDispatch.ConfRequest, &txConf1)
|
err = n.UpdateConfDetails(ntfn1.HistoricalDispatch.ConfRequest, &txConf1)
|
||||||
require.NoError(t, err, "unable to update conf details")
|
require.NoError(t, err, "unable to update conf details")
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn1.Event.Updates:
|
case updDetails := <-ntfn1.Event.Updates:
|
||||||
const expected = 0
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 0,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx1 "+
|
BlockHeight: 9,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx1")
|
t.Fatal("Expected confirmation update for tx1")
|
||||||
}
|
}
|
||||||
@@ -443,13 +439,12 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
|
|||||||
err = n.UpdateConfDetails(ntfn2.HistoricalDispatch.ConfRequest, &txConf2)
|
err = n.UpdateConfDetails(ntfn2.HistoricalDispatch.ConfRequest, &txConf2)
|
||||||
require.NoError(t, err, "unable to update conf details")
|
require.NoError(t, err, "unable to update conf details")
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn2.Event.Updates:
|
case updDetails := <-ntfn2.Event.Updates:
|
||||||
const expected = 1
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 1,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
BlockHeight: 9,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx2")
|
t.Fatal("Expected confirmation update for tx2")
|
||||||
}
|
}
|
||||||
@@ -485,13 +480,12 @@ func TestTxNotifierHistoricalConfDispatch(t *testing.T) {
|
|||||||
// We should only receive one update for tx2 since the last one,
|
// We should only receive one update for tx2 since the last one,
|
||||||
// indicating how many confirmations are still left.
|
// indicating how many confirmations are still left.
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn2.Event.Updates:
|
case updDetails := <-ntfn2.Event.Updates:
|
||||||
const expected = 0
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 0,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
BlockHeight: 9,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx2")
|
t.Fatal("Expected confirmation update for tx2")
|
||||||
}
|
}
|
||||||
@@ -1490,13 +1484,12 @@ func TestTxNotifierConfReorg(t *testing.T) {
|
|||||||
// We should only receive one update for tx2 since it only requires
|
// We should only receive one update for tx2 since it only requires
|
||||||
// one confirmation and it already met it.
|
// one confirmation and it already met it.
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn2.Event.Updates:
|
case updDetails := <-ntfn2.Event.Updates:
|
||||||
const expected = 0
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: 0,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx2 "+
|
BlockHeight: 12,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx2")
|
t.Fatal("Expected confirmation update for tx2")
|
||||||
}
|
}
|
||||||
@@ -1520,15 +1513,14 @@ func TestTxNotifierConfReorg(t *testing.T) {
|
|||||||
// confirmations and it has already met them.
|
// confirmations and it has already met them.
|
||||||
for i := uint32(1); i <= 2; i++ {
|
for i := uint32(1); i <= 2; i++ {
|
||||||
select {
|
select {
|
||||||
case numConfsLeft := <-ntfn3.Event.Updates:
|
case updDetails := <-ntfn3.Event.Updates:
|
||||||
expected := tx3NumConfs - i
|
expected := chainntnfs.TxUpdateInfo{
|
||||||
if numConfsLeft != expected {
|
NumConfsLeft: tx3NumConfs - i,
|
||||||
t.Fatalf("Received incorrect confirmation update: tx3 "+
|
BlockHeight: 12,
|
||||||
"expected %d confirmations left, got %d",
|
|
||||||
expected, numConfsLeft)
|
|
||||||
}
|
}
|
||||||
|
require.Equal(t, expected, updDetails)
|
||||||
default:
|
default:
|
||||||
t.Fatal("Expected confirmation update for tx2")
|
t.Fatal("Expected confirmation update for tx3")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user