Skip to content

Commit

Permalink
Review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Nov 3, 2022
1 parent f2798b0 commit 34d186b
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 212 deletions.
20 changes: 2 additions & 18 deletions client/asset/bch/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"sync/atomic"
Expand Down Expand Up @@ -36,7 +35,6 @@ import (
"github.com/gcash/bchd/bchec"
bchchaincfg "github.com/gcash/bchd/chaincfg"
bchchainhash "github.com/gcash/bchd/chaincfg/chainhash"
"github.com/gcash/bchd/peer"
bchtxscript "github.com/gcash/bchd/txscript"
bchwire "github.com/gcash/bchd/wire"
"github.com/gcash/bchlog"
Expand Down Expand Up @@ -249,10 +247,7 @@ func (w *bchSPVWallet) Start() (btc.SPVService, error) {
case bchwire.TestNet, bchwire.SimNet: // plain "wire.TestNet" is regnet!
defaultPeers = []string{"localhost:21577"}
}
peerManager, err := btc.NewSPVPeerManager(&spvService{w.cl}, defaultPeers, w.dir, w.log)
if err != nil {
return nil, fmt.Errorf("failed to create peer manager: %w", err)
}
peerManager := btc.NewSPVPeerManager(&spvService{w.cl}, defaultPeers, w.dir, w.log, w.chainParams.DefaultPort)
w.peerManager = peerManager

if err = w.chainClient.Start(); err != nil { // lazily starts connmgr
Expand Down Expand Up @@ -850,18 +845,7 @@ func (s *spvService) Peers() []btc.SPVPeer {
}

func (s *spvService) AddPeer(addr string) error {
serverPeer := neutrino.NewServerPeer(s.ChainService, true)
peer, err := peer.NewOutboundPeer(neutrino.NewPeerConfig(serverPeer), addr)
if err != nil {
return err
}
conn, err := net.Dial("tcp", peer.Addr())
if err != nil {
return err
}
peer.AssociateConnection(conn)
serverPeer.Peer = peer
return nil
return s.ChainService.ConnectNode(addr, true)
}

func (s *spvService) GetBlockHeight(h *chainhash.Hash) (int32, error) {
Expand Down
27 changes: 10 additions & 17 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ type intermediaryWallet struct {
// method to implement asset.Rescanner.
type ExchangeWalletSPV struct {
*intermediaryWallet

spvNode *spvWallet
}

// ExchangeWalletFullNode implements Wallet and adds the FeeRate method.
Expand Down Expand Up @@ -868,9 +870,7 @@ type RecoveryCfg struct {
// back to its previous state after it is recreated. Part of the
// Recoverer interface.
func (btc *ExchangeWalletSPV) GetRecoveryCfg() (map[string]string, error) {
w := btc.node.(*spvWallet)

internal, external, err := w.numDerivedAddresses()
internal, external, err := btc.spvNode.numDerivedAddresses()
if err != nil {
return nil, err
}
Expand All @@ -890,8 +890,7 @@ func (btc *ExchangeWalletSPV) GetRecoveryCfg() (map[string]string, error) {
// Destroy will delete all the wallet files so the wallet can be recreated.
// Part of the Recoverer interface.
func (btc *ExchangeWalletSPV) Move(backupDir string) error {
w := btc.node.(*spvWallet)
err := w.moveWalletData(backupDir)
err := btc.spvNode.moveWalletData(backupDir)
if err != nil {
return fmt.Errorf("unable to move wallet data: %w", err)
}
Expand All @@ -902,32 +901,26 @@ func (btc *ExchangeWalletSPV) Move(backupDir string) error {
// Rescan satisfies the asset.Rescanner interface, and issues a rescan wallet
// command if the backend is an SPV wallet.
func (btc *ExchangeWalletSPV) Rescan(_ context.Context) error {
// This will panic if not an spvWallet, which would indicate that
// openSPVWallet was not used to construct this instance.
w := btc.node.(*spvWallet)
atomic.StoreInt64(&btc.tipAtConnect, 0) // for progress
// Caller should start calling SyncStatus on a ticker.
return w.wallet.RescanAsync()
return btc.spvNode.wallet.RescanAsync()
}

// Peers returns a list of peers that the wallet is connected to.
func (btc *ExchangeWalletSPV) Peers() ([]*asset.WalletPeer, error) {
w := btc.node.(*spvWallet)
return w.peers()
return btc.spvNode.peers()
}

// AddPeer connects the wallet to a new peer. The peer's address will be
// persisted and connected to each time the wallet is started up.
func (btc *ExchangeWalletSPV) AddPeer(addr string) error {
w := btc.node.(*spvWallet)
return w.addPeer(addr)
return btc.spvNode.addPeer(addr)
}

// RemovePeer will remove a peer that was added by AddPeer. This peer may
// still be connected to by the wallet if it discovers it on it's own.
func (btc *ExchangeWalletSPV) RemovePeer(addr string) error {
w := btc.node.(*spvWallet)
return w.removePeer(addr)
return btc.spvNode.removePeer(addr)
}

// FeeRate satisfies asset.FeeRater.
Expand All @@ -942,8 +935,7 @@ func (btc *ExchangeWalletFullNode) FeeRate() uint64 {

// LogFilePath returns the path to the neutrino log file.
func (btc *ExchangeWalletSPV) LogFilePath() string {
w := btc.node.(*spvWallet)
return w.logFilePath()
return btc.spvNode.logFilePath()
}

type block struct {
Expand Down Expand Up @@ -1221,6 +1213,7 @@ func OpenSPVWallet(cfg *BTCCloneCFG, walletConstructor BTCWalletConstructor) (*E
txFeeEstimator: spvw,
tipRedeemer: spvw,
},
spvNode: spvw,
}, nil
}

Expand Down
5 changes: 1 addition & 4 deletions client/asset/btc/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ func (w *btcSPVWallet) Start() (SPVService, error) {
case wire.TestNet, wire.SimNet: // plain "wire.TestNet" is regnet!
defaultPeers = []string{"localhost:20575"}
}
peerManager, err := NewSPVPeerManager(&btcChainService{w.cl}, defaultPeers, w.dir, w.log)
if err != nil {
return nil, fmt.Errorf("failed to create peer manager: %w", err)
}
peerManager := NewSPVPeerManager(&btcChainService{w.cl}, defaultPeers, w.dir, w.log, w.chainParams.DefaultPort)
w.peerManager = peerManager

w.chainClient = chain.NewNeutrinoClient(w.chainParams, w.cl)
Expand Down
Loading

0 comments on commit 34d186b

Please sign in to comment.