Skip to content

Commit

Permalink
Merge pull request #1959 from halseth/neutrino-api
Browse files Browse the repository at this point in the history
Update to new Neutrino API
  • Loading branch information
Roasbeef committed Oct 17, 2018
2 parents db62508 + 29acb9f commit 7755370
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 63 deletions.
8 changes: 4 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Expand Up @@ -44,7 +44,7 @@

[[constraint]]
name = "github.com/lightninglabs/neutrino"
revision = "838f7ba74d217d188efc223604bd280b4e3f0238"
revision = "4d60692991302a44509d1a9234ccd51373c120b4"

[[constraint]]
name = "github.com/lightningnetwork/lightning-onion"
Expand Down Expand Up @@ -72,7 +72,7 @@

[[constraint]]
name = "github.com/btcsuite/btcwallet"
revision = "421298df22601db0fe4adb8f4be71b7014324ba9"
revision = "c4dd27e481f9801866cf5226bfe532084772ec2a"

[[constraint]]
name = "github.com/tv42/zbase32"
Expand Down
46 changes: 15 additions & 31 deletions chainntnfs/neutrinonotify/neutrino.go
Expand Up @@ -137,15 +137,12 @@ func (n *NeutrinoNotifier) Start() error {
// start the auto-rescan from this point. Once a caller actually wishes
// to register a chain view, the rescan state will be rewound
// accordingly.
bestHeader, bestHeight, err := n.p2pNode.BlockHeaders.ChainTip()
startingPoint, err := n.p2pNode.BestBlock()
if err != nil {
return err
}
startingPoint := &waddrmgr.BlockStamp{
Height: int32(bestHeight),
Hash: bestHeader.BlockHash(),
}
n.bestHeight = bestHeight

n.bestHeight = uint32(startingPoint.Height)

// Next, we'll create our set of rescan options. Currently it's
// required that a user MUST set an addr/outpoint/txid when creating a
Expand All @@ -165,7 +162,7 @@ func (n *NeutrinoNotifier) Start() error {
}

n.txConfNotifier = chainntnfs.NewTxConfNotifier(
bestHeight, reorgSafetyLimit, n.confirmHintCache,
n.bestHeight, reorgSafetyLimit, n.confirmHintCache,
)

n.chainConn = &NeutrinoChainConn{n.p2pNode}
Expand Down Expand Up @@ -476,20 +473,17 @@ out:
"blocks, attempting to catch up")
}

header, err := n.p2pNode.BlockHeaders.FetchHeaderByHeight(
n.bestHeight,
)
hash, err := n.p2pNode.GetBlockHash(int64(n.bestHeight))
if err != nil {
chainntnfs.Log.Errorf("Unable to fetch header"+
chainntnfs.Log.Errorf("Unable to fetch block hash"+
"for height %d: %v", n.bestHeight, err)
n.heightMtx.Unlock()
continue
}

hash := header.BlockHash()
notifierBestBlock := chainntnfs.BlockEpoch{
Height: int32(n.bestHeight),
Hash: &hash,
Hash: hash,
}
newBestBlock, err := chainntnfs.RewindChain(
n.chainConn, n.txConfNotifier, notifierBestBlock,
Expand Down Expand Up @@ -536,17 +530,16 @@ func (n *NeutrinoNotifier) historicalConfDetails(targetHash *chainhash.Hash,

// First, we'll fetch the block header for this height so we
// can compute the current block hash.
header, err := n.p2pNode.BlockHeaders.FetchHeaderByHeight(scanHeight)
blockHash, err := n.p2pNode.GetBlockHash(int64(scanHeight))
if err != nil {
return nil, fmt.Errorf("unable to get header for height=%v: %v",
scanHeight, err)
}
blockHash := header.BlockHash()

// With the hash computed, we can now fetch the basic filter
// for this height.
regFilter, err := n.p2pNode.GetCFilter(
blockHash, wire.GCSFilterRegular,
*blockHash, wire.GCSFilterRegular,
)
if err != nil {
return nil, fmt.Errorf("unable to retrieve regular filter for "+
Expand All @@ -562,7 +555,7 @@ func (n *NeutrinoNotifier) historicalConfDetails(targetHash *chainhash.Hash,

// In the case that the filter exists, we'll attempt to see if
// any element in it matches our target public key script.
key := builder.DeriveKey(&blockHash)
key := builder.DeriveKey(blockHash)
match, err := regFilter.Match(key, pkScript)
if err != nil {
return nil, fmt.Errorf("unable to query filter: %v", err)
Expand All @@ -577,15 +570,15 @@ func (n *NeutrinoNotifier) historicalConfDetails(targetHash *chainhash.Hash,
// In the case that we do have a match, we'll fetch the block
// from the network so we can find the positional data required
// to send the proper response.
block, err := n.p2pNode.GetBlock(blockHash)
block, err := n.p2pNode.GetBlock(*blockHash)
if err != nil {
return nil, fmt.Errorf("unable to get block from network: %v", err)
}
for j, tx := range block.Transactions() {
txHash := tx.Hash()
if txHash.IsEqual(targetHash) {
confDetails := chainntnfs.TxConfirmation{
BlockHash: &blockHash,
BlockHash: blockHash,
BlockHeight: scanHeight,
TxIndex: uint32(j),
}
Expand Down Expand Up @@ -1085,19 +1078,15 @@ type NeutrinoChainConn struct {

// GetBlockHeader returns the block header for a hash.
func (n *NeutrinoChainConn) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
header, _, err := n.p2pNode.BlockHeaders.FetchHeader(blockHash)
if err != nil {
return nil, err
}
return header, nil
return n.p2pNode.GetBlockHeader(blockHash)
}

// GetBlockHeaderVerbose returns a verbose block header result for a hash. This
// result only contains the height with a nil hash.
func (n *NeutrinoChainConn) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (
*btcjson.GetBlockHeaderVerboseResult, error) {

_, height, err := n.p2pNode.BlockHeaders.FetchHeader(blockHash)
height, err := n.p2pNode.GetBlockHeight(blockHash)
if err != nil {
return nil, err
}
Expand All @@ -1107,10 +1096,5 @@ func (n *NeutrinoChainConn) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (

// GetBlockHash returns the hash from a block height.
func (n *NeutrinoChainConn) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
header, err := n.p2pNode.BlockHeaders.FetchHeaderByHeight(uint32(blockHeight))
if err != nil {
return nil, err
}
hash := header.BlockHash()
return &hash, nil
return n.p2pNode.GetBlockHash(blockHeight)
}
7 changes: 1 addition & 6 deletions chainntnfs/neutrinonotify/neutrino_dev.go
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/chainntnfs"
)
Expand All @@ -26,14 +25,10 @@ func (n *NeutrinoNotifier) UnsafeStart(bestHeight int32, bestHash *chainhash.Has
// start the auto-rescan from this point. Once a caller actually wishes
// to register a chain view, the rescan state will be rewound
// accordingly.
header, height, err := n.p2pNode.BlockHeaders.ChainTip()
startingPoint, err := n.p2pNode.BestBlock()
if err != nil {
return err
}
startingPoint := &waddrmgr.BlockStamp{
Height: int32(height),
Hash: header.BlockHash(),
}

// Next, we'll create our set of rescan options. Currently it's
// required that a user MUST set an addr/outpoint/txid when creating a
Expand Down
12 changes: 0 additions & 12 deletions lnwallet/btcwallet/btcwallet.go
Expand Up @@ -750,17 +750,5 @@ func (b *BtcWallet) IsSynced() (bool, int64, error) {
return false, bestTimestamp, nil
}

// If this is neutrino, then we'll also want to wait until the set of
// filter headers also match
if neutrinoNode, ok := b.chain.(*chain.NeutrinoClient); ok {
filterDB := neutrinoNode.CS.RegFilterHeaders
_, filterHeaderTip, err := filterDB.ChainTip()
if err != nil {
return false, 0, err
}

return filterHeaderTip == uint32(bestHeight), bestTimestamp, nil
}

return true, bestTimestamp, nil
}
11 changes: 3 additions & 8 deletions routing/chainview/neutrino.go
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/gcs/builder"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/channeldb"
)
Expand Down Expand Up @@ -84,14 +83,10 @@ func (c *CfFilteredChainView) Start() error {
// start the auto-rescan from this point. Once a caller actually wishes
// to register a chain view, the rescan state will be rewound
// accordingly.
bestHeader, bestHeight, err := c.p2pNode.BlockHeaders.ChainTip()
startingPoint, err := c.p2pNode.BestBlock()
if err != nil {
return err
}
startingPoint := &waddrmgr.BlockStamp{
Height: int32(bestHeight),
Hash: bestHeader.BlockHash(),
}

// Next, we'll create our set of rescan options. Currently it's
// required that an user MUST set a addr/outpoint/txid when creating a
Expand Down Expand Up @@ -215,14 +210,14 @@ func (c *CfFilteredChainView) chainFilterer() {
func (c *CfFilteredChainView) FilterBlock(blockHash *chainhash.Hash) (*FilteredBlock, error) {
// First, we'll fetch the block header itself so we can obtain the
// height which is part of our return value.
_, blockHeight, err := c.p2pNode.BlockHeaders.FetchHeader(blockHash)
blockHeight, err := c.p2pNode.GetBlockHeight(blockHash)
if err != nil {
return nil, err
}

filteredBlock := &FilteredBlock{
Hash: *blockHash,
Height: blockHeight,
Height: uint32(blockHeight),
}

// If we don't have any items within our current chain filter, then we
Expand Down

0 comments on commit 7755370

Please sign in to comment.