Skip to content

Commit

Permalink
chain: add new method LookupInputMempoolSpend
Browse files Browse the repository at this point in the history
This commit adds a new method to look up mempool spent for a given
outpoint.
  • Loading branch information
yyforyongyu committed Feb 23, 2024
1 parent 633e12c commit 9344055
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions chain/bitcoind_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1400,3 +1400,11 @@ func (c *BitcoindClient) filterTx(txDetails *btcutil.Tx,

return true, rec, nil
}

// LookupInputMempoolSpend returns the transaction hash and true if the given
// input is found being spent in mempool, otherwise it returns nil and false.
func (c *BitcoindClient) LookupInputMempoolSpend(op wire.OutPoint) (
chainhash.Hash, bool) {

return c.chainConn.events.LookupInputSpend(op)
}
43 changes: 43 additions & 0 deletions chain/bitcoind_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func TestBitcoindEvents(t *testing.T) {
// mempool.
btcClient = setupBitcoind(t, addr, test.rpcPolling)
testNotifySpentMempool(t, miner1, btcClient)

// Test looking up mempool for input spent.
testLookupInputMempoolSpend(t, miner1, btcClient)
})
}
}
Expand Down Expand Up @@ -214,6 +217,46 @@ func testNotifySpentMempool(t *testing.T, miner *rpctest.Harness,
}
}

// testLookupInputMempoolSpend tests that LookupInputMempoolSpend returns the
// correct tx hash and whether the input has been spent in the mempool.
func testLookupInputMempoolSpend(t *testing.T, miner *rpctest.Harness,
client *BitcoindClient) {

require := require.New(t)

script, _, err := randPubKeyHashScript()
require.NoError(err)

// Create a test tx.
tx, err := miner.CreateTransaction(
[]*wire.TxOut{{Value: 1000, PkScript: script}}, 5, false,
)
require.NoError(err)

// Lookup the input in mempool.
op := tx.TxIn[0].PreviousOutPoint
txid, found := client.LookupInputMempoolSpend(op)

// Expect that the input has not been spent in the mempool.
require.False(found)
require.Zero(txid)

// Send the tx which will put it in the mempool.
_, err = client.SendRawTransaction(tx, true)
require.NoError(err)

// Lookup the input again should return the spending tx.
//
// NOTE: We need to wait for the tx to propagate to the mempool.
require.Eventually(func() bool {
txid, found = client.LookupInputMempoolSpend(op)
return found
}, 5*time.Second, 100*time.Millisecond)

// Check the expected txid is returned.
require.Equal(tx.TxHash(), txid)
}

// testReorg tests that the given BitcoindClient correctly responds to a chain
// re-org.
func testReorg(t *testing.T, miner1, miner2 *rpctest.Harness,
Expand Down
8 changes: 8 additions & 0 deletions chain/btcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,11 @@ func (c *RPCClient) POSTClient() (*rpcclient.Client, error) {
configCopy.HTTPPostMode = true
return rpcclient.New(&configCopy, nil)
}

// LookupInputMempoolSpend returns the transaction hash and true if the given
// input is found being spent in mempool, otherwise it returns nil and false.
func (c *RPCClient) LookupInputMempoolSpend(op wire.OutPoint) (
chainhash.Hash, bool) {

return getTxSpendingPrevOut(op, c.Client)
}

0 comments on commit 9344055

Please sign in to comment.