multi: add bitcoind drivers and tests

This commit is contained in:
Alex authored and Olaoluwa Osuntokun committed 2018-01-15 13:59:34 -08:00
1 parent 244ae4b571
commit 187f59556a
13 files changed
+2138 -241

No files matched your search

+23 -61
View File
@@ -29,23 +29,7 @@ var (
//
// This method is a part of the lnwallet.BlockChainIO interface.
func (b *BtcWallet) GetBestBlock() (*chainhash.Hash, int32, error) {
switch backend := b.chain.(type) {
case *chain.NeutrinoClient:
header, height, err := backend.CS.BlockHeaders.ChainTip()
if err != nil {
return nil, -1, err
}
blockHash := header.BlockHash()
return &blockHash, int32(height), nil
case *chain.RPCClient:
return backend.GetBestBlock()
default:
return nil, -1, fmt.Errorf("unknown backend")
}
return b.chain.GetBestBlock()
}
// GetUtxo returns the original output referenced by the passed outpoint.
@@ -100,6 +84,26 @@ func (b *BtcWallet) GetUtxo(op *wire.OutPoint, heightHint uint32) (*wire.TxOut,
PkScript: pkScript,
}, nil
case *chain.BitcoindClient:
txout, err := backend.GetTxOut(&op.Hash, op.Index, false)
if err != nil {
return nil, err
} else if txout == nil {
return nil, ErrOutputSpent
}
pkScript, err := hex.DecodeString(txout.ScriptPubKey.Hex)
if err != nil {
return nil, err
}
return &wire.TxOut{
// Sadly, gettxout returns the output value in BTC
// instead of satoshis.
Value: int64(txout.Value * 1e8),
PkScript: pkScript,
}, nil
default:
return nil, fmt.Errorf("unknown backend")
}
@@ -109,27 +113,7 @@ func (b *BtcWallet) GetUtxo(op *wire.OutPoint, heightHint uint32) (*wire.TxOut,
//
// This method is a part of the lnwallet.BlockChainIO interface.
func (b *BtcWallet) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
switch backend := b.chain.(type) {
case *chain.NeutrinoClient:
block, err := backend.CS.GetBlockFromNetwork(*blockHash)
if err != nil {
return nil, err
}
return block.MsgBlock(), nil
case *chain.RPCClient:
block, err := backend.GetBlock(blockHash)
if err != nil {
return nil, err
}
return block, nil
default:
return nil, fmt.Errorf("unknown backend")
}
return b.chain.GetBlock(blockHash)
}
// GetBlockHash returns the hash of the block in the best blockchain at the
@@ -137,29 +121,7 @@ func (b *BtcWallet) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error)
//
// This method is a part of the lnwallet.BlockChainIO interface.
func (b *BtcWallet) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
switch backend := b.chain.(type) {
case *chain.NeutrinoClient:
height := uint32(blockHeight)
blockHeader, err := backend.CS.BlockHeaders.FetchHeaderByHeight(height)
if err != nil {
return nil, err
}
blockHash := blockHeader.BlockHash()
return &blockHash, nil
case *chain.RPCClient:
blockHash, err := backend.GetBlockHash(blockHeight)
if err != nil {
return nil, err
}
return blockHash, nil
default:
return nil, fmt.Errorf("unknown backend")
}
return b.chain.GetBlockHash(blockHeight)
}
// A compile time check to ensure that BtcWallet implements the BlockChainIO
+17 -31
View File
@@ -119,6 +119,17 @@ func New(cfg Config) (*BtcWallet, error) {
}, nil
}
// BackEnd returns the underlying ChainService's name as a string.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) BackEnd() string {
if b.chain != nil {
return b.chain.BackEnd()
}
return ""
}
// Start initializes the underlying rpc connection, the wallet itself, and
// begins syncing to the current available blockchain state.
//
@@ -668,22 +679,9 @@ func (b *BtcWallet) IsSynced() (bool, error) {
// Next, query the chain backend to grab the info about the tip of the
// main chain.
switch backend := b.cfg.ChainSource.(type) {
case *chain.NeutrinoClient:
header, height, err := backend.CS.BlockHeaders.ChainTip()
if err != nil {
return false, err
}
bh := header.BlockHash()
bestHash = &bh
bestHeight = int32(height)
case *chain.RPCClient:
bestHash, bestHeight, err = backend.GetBestBlock()
if err != nil {
return false, err
}
bestHash, bestHeight, err = b.cfg.ChainSource.GetBestBlock()
if err != nil {
return false, err
}
// If the wallet hasn't yet fully synced to the node's best chain tip,
@@ -696,21 +694,9 @@ func (b *BtcWallet) IsSynced() (bool, error) {
// still may not yet be synced as the chain backend may still be
// catching up to the main chain. So we'll grab the block header in
// order to make a guess based on the current time stamp.
var blockHeader *wire.BlockHeader
switch backend := b.cfg.ChainSource.(type) {
case *chain.NeutrinoClient:
bh, _, err := backend.CS.BlockHeaders.FetchHeader(bestHash)
if err != nil {
return false, err
}
blockHeader = bh
case *chain.RPCClient:
blockHeader, err = backend.GetBlockHeader(bestHash)
if err != nil {
return false, err
}
blockHeader, err := b.cfg.ChainSource.GetBlockHeader(bestHash)
if err != nil {
return false, err
}
// If the timestamp no the best header is more than 2 hours in the
+2
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/roasbeef/btcwallet/chain"
)
const (
@@ -36,6 +37,7 @@ func init() {
driver := &lnwallet.WalletDriver{
WalletType: walletType,
New: createNewWallet,
BackEnds: chain.BackEnds,
}
if err := lnwallet.RegisterWallet(driver); err != nil {