From 3082ae92c3c6c184e91d0bbfc5f6ab902f1b513f Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 23 Mar 2018 15:44:35 +0100 Subject: [PATCH] rpcwebsocket: notify spends already in the mempool This commit adds a logic to the addSpentRequests that inspects the mempool for any spends of the outputs. Before this commit, a spend would only be checked when a transaction was first accepted into the mempool. --- rpcwebsocket.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rpcwebsocket.go b/rpcwebsocket.go index fcdcce2e..8b9d06b4 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -883,7 +883,7 @@ func (m *wsNotificationManager) RegisterSpentRequests(wsc *wsClient, ops []*wire // addSpentRequests modifies a map of watched outpoints to sets of websocket // clients to add a new request watch all of the outpoints in ops and create // and send a notification when spent to the websocket client wsc. -func (*wsNotificationManager) addSpentRequests(opMap map[wire.OutPoint]map[chan struct{}]*wsClient, +func (m *wsNotificationManager) addSpentRequests(opMap map[wire.OutPoint]map[chan struct{}]*wsClient, wsc *wsClient, ops []*wire.OutPoint) { for _, op := range ops { @@ -900,6 +900,22 @@ func (*wsNotificationManager) addSpentRequests(opMap map[wire.OutPoint]map[chan } cmap[wsc.quit] = wsc } + + // Check if any transactions spending these outputs already exists in + // the mempool, if so send the notification immediately. + spends := make(map[chainhash.Hash]*btcutil.Tx) + for _, op := range ops { + spend := m.server.cfg.TxMemPool.CheckSpend(*op) + if spend != nil { + rpcsLog.Debugf("Found existing mempool spend for "+ + "outpoint<%v>: %v", op, spend.Hash()) + spends[*spend.Hash()] = spend + } + } + + for _, spend := range spends { + m.notifyForTx(opMap, nil, spend, nil) + } } // UnregisterSpentRequest removes a request from the passed websocket client