Skip to content

Commit

Permalink
Merge pull request #2314 from wpaulino/chainnotifier-subserver
Browse files Browse the repository at this point in the history
chainntnfs+lnrpc/chainrpc: add ChainNotifier RPC subserver
  • Loading branch information
Roasbeef authored Jan 21, 2019
2 parents 4537c63 + 59e2be5 commit 375be93
Show file tree
Hide file tree
Showing 29 changed files with 4,569 additions and 1,402 deletions.
388 changes: 248 additions & 140 deletions chainntnfs/bitcoindnotify/bitcoind.go

Large diffs are not rendered by default.

58 changes: 47 additions & 11 deletions chainntnfs/bitcoindnotify/bitcoind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package bitcoindnotify

import (
"bytes"
"io/ioutil"
"testing"
"time"
Expand All @@ -14,6 +15,20 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
)

var (
testScript = []byte{
// OP_HASH160
0xA9,
// OP_DATA_20
0x14,
// <20-byte hash>
0xec, 0x6f, 0x7a, 0x5a, 0xa8, 0xf2, 0xb1, 0x0c, 0xa5, 0x15,
0x04, 0x52, 0x3a, 0x60, 0xd4, 0x03, 0x06, 0xf6, 0x96, 0xcd,
// OP_EQUAL
0x87,
}
)

func initHintCache(t *testing.T) *chainntnfs.HeightHintCache {
t.Helper()

Expand Down Expand Up @@ -41,7 +56,10 @@ func setUpNotifier(t *testing.T, bitcoindConn *chain.BitcoindConn,

t.Helper()

notifier := New(bitcoindConn, spendHintCache, confirmHintCache)
notifier := New(
bitcoindConn, chainntnfs.NetParams, spendHintCache,
confirmHintCache,
)
if err := notifier.Start(); err != nil {
t.Fatalf("unable to start notifier: %v", err)
}
Expand Down Expand Up @@ -104,8 +122,13 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
// A transaction unknown to the node should not be found within the
// txindex even if it is enabled, so we should not proceed with any
// fallback methods.
var zeroHash chainhash.Hash
_, txStatus, err := notifier.historicalConfDetails(&zeroHash, 0, 0)
var unknownHash chainhash.Hash
copy(unknownHash[:], bytes.Repeat([]byte{0x10}, 32))
unknownConfReq, err := chainntnfs.NewConfRequest(&unknownHash, testScript)
if err != nil {
t.Fatalf("unable to create conf request: %v", err)
}
_, txStatus, err := notifier.historicalConfDetails(unknownConfReq, 0, 0)
if err != nil {
t.Fatalf("unable to retrieve historical conf details: %v", err)
}
Expand All @@ -120,16 +143,20 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {

// Now, we'll create a test transaction, confirm it, and attempt to
// retrieve its confirmation details.
txid, _, err := chainntnfs.GetTestTxidAndScript(miner)
txid, pkScript, err := chainntnfs.GetTestTxidAndScript(miner)
if err != nil {
t.Fatalf("unable to create tx: %v", err)
}
if err := chainntnfs.WaitForMempoolTx(miner, txid); err != nil {
t.Fatal(err)
}
confReq, err := chainntnfs.NewConfRequest(txid, pkScript)
if err != nil {
t.Fatalf("unable to create conf request: %v", err)
}

// The transaction should be found in the mempool at this point.
_, txStatus, err = notifier.historicalConfDetails(txid, 0, 0)
_, txStatus, err = notifier.historicalConfDetails(confReq, 0, 0)
if err != nil {
t.Fatalf("unable to retrieve historical conf details: %v", err)
}
Expand All @@ -151,7 +178,7 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
// the txindex includes the transaction just mined.
syncNotifierWithMiner(t, notifier, miner)

_, txStatus, err = notifier.historicalConfDetails(txid, 0, 0)
_, txStatus, err = notifier.historicalConfDetails(confReq, 0, 0)
if err != nil {
t.Fatalf("unable to retrieve historical conf details: %v", err)
}
Expand Down Expand Up @@ -186,10 +213,15 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
// Since the node has its txindex disabled, we fall back to scanning the
// chain manually. A transaction unknown to the network should not be
// found.
var zeroHash chainhash.Hash
var unknownHash chainhash.Hash
copy(unknownHash[:], bytes.Repeat([]byte{0x10}, 32))
unknownConfReq, err := chainntnfs.NewConfRequest(&unknownHash, testScript)
if err != nil {
t.Fatalf("unable to create conf request: %v", err)
}
broadcastHeight := syncNotifierWithMiner(t, notifier, miner)
_, txStatus, err := notifier.historicalConfDetails(
&zeroHash, uint32(broadcastHeight), uint32(broadcastHeight),
unknownConfReq, uint32(broadcastHeight), uint32(broadcastHeight),
)
if err != nil {
t.Fatalf("unable to retrieve historical conf details: %v", err)
Expand All @@ -210,8 +242,8 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
// one output, which we will manually spend. The backend node's
// transaction index should also be disabled, which we've already
// ensured above.
output, pkScript := chainntnfs.CreateSpendableOutput(t, miner)
spendTx := chainntnfs.CreateSpendTx(t, output, pkScript)
outpoint, output, privKey := chainntnfs.CreateSpendableOutput(t, miner)
spendTx := chainntnfs.CreateSpendTx(t, outpoint, output, privKey)
spendTxHash, err := miner.Node.SendRawTransaction(spendTx, true)
if err != nil {
t.Fatalf("unable to broadcast tx: %v", err)
Expand All @@ -225,9 +257,13 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {

// Ensure the notifier and miner are synced to the same height to ensure
// we can find the transaction when manually scanning the chain.
confReq, err := chainntnfs.NewConfRequest(&outpoint.Hash, output.PkScript)
if err != nil {
t.Fatalf("unable to create conf request: %v", err)
}
currentHeight := syncNotifierWithMiner(t, notifier, miner)
_, txStatus, err = notifier.historicalConfDetails(
&output.Hash, uint32(broadcastHeight), uint32(currentHeight),
confReq, uint32(broadcastHeight), uint32(currentHeight),
)
if err != nil {
t.Fatalf("unable to retrieve historical conf details: %v", err)
Expand Down
19 changes: 13 additions & 6 deletions chainntnfs/bitcoindnotify/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"errors"
"fmt"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/chain"
"github.com/lightningnetwork/lnd/chainntnfs"
)

// createNewNotifier creates a new instance of the ChainNotifier interface
// implemented by BitcoindNotifier.
func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) {
if len(args) != 3 {
if len(args) != 4 {
return nil, fmt.Errorf("incorrect number of arguments to "+
".New(...), expected 2, instead passed %v", len(args))
".New(...), expected 4, instead passed %v", len(args))
}

chainConn, ok := args[0].(*chain.BitcoindConn)
Expand All @@ -22,19 +23,25 @@ func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) {
"is incorrect, expected a *chain.BitcoindConn")
}

spendHintCache, ok := args[1].(chainntnfs.SpendHintCache)
chainParams, ok := args[1].(*chaincfg.Params)
if !ok {
return nil, errors.New("second argument to bitcoindnotify.New " +
"is incorrect, expected a chainntnfs.SpendHintCache")
"is incorrect, expected a *chaincfg.Params")
}

confirmHintCache, ok := args[2].(chainntnfs.ConfirmHintCache)
spendHintCache, ok := args[2].(chainntnfs.SpendHintCache)
if !ok {
return nil, errors.New("third argument to bitcoindnotify.New " +
"is incorrect, expected a chainntnfs.SpendHintCache")
}

confirmHintCache, ok := args[3].(chainntnfs.ConfirmHintCache)
if !ok {
return nil, errors.New("fourth argument to bitcoindnotify.New " +
"is incorrect, expected a chainntnfs.ConfirmHintCache")
}

return New(chainConn, spendHintCache, confirmHintCache), nil
return New(chainConn, chainParams, spendHintCache, confirmHintCache), nil
}

// init registers a driver for the BtcdNotifier concrete implementation of the
Expand Down
Loading

0 comments on commit 375be93

Please sign in to comment.