Skip to content

Commit

Permalink
core,btc,app: Manage SPV wallet peers
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Oct 28, 2022
1 parent 4e6a4cb commit 1051002
Show file tree
Hide file tree
Showing 19 changed files with 728 additions and 33 deletions.
20 changes: 18 additions & 2 deletions client/asset/bch/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var _ btc.BTCWallet = (*bchSPVWallet)(nil)

// openSPVWallet creates a bchSPVWallet, but does not Start.
// Satisfies btc.BTCWalletConstructor.
func openSPVWallet(dir string, cfg *btc.WalletConfig, btcParams *chaincfg.Params, log dex.Logger) btc.BTCWallet {
func openSPVWallet(dir string, cfg *btc.WalletConfig, btcParams *chaincfg.Params, log dex.Logger) (btc.BTCWallet, error) {
var bchParams *bchchaincfg.Params
switch btcParams.Name {
case dexbch.MainNetParams.Name:
Expand All @@ -108,7 +108,7 @@ func openSPVWallet(dir string, cfg *btc.WalletConfig, btcParams *chaincfg.Params
allowAutomaticRescan: !cfg.ActivelyUsed,
}
w.birthdayV.Store(cfg.AdjustedBirthday())
return w
return w, nil
}

// createSPVWallet creates a new SPV wallet.
Expand Down Expand Up @@ -746,6 +746,18 @@ func (w *bchSPVWallet) updateDBBirthday(bday time.Time) error {
})
}

func (w *bchSPVWallet) Peers() ([]*asset.WalletPeer, error) {
return nil, nil
}

func (w *bchSPVWallet) AddPeer(addr string) error {
return nil
}

func (w *bchSPVWallet) RemovePeer(addr string) error {
return nil
}

// secretSource is used to locate keys and redemption scripts while signing a
// transaction. secretSource satisfies the txauthor.SecretsSource interface.
type secretSource struct {
Expand Down Expand Up @@ -834,6 +846,10 @@ func (s *spvService) Peers() []btc.SPVPeer {
return peers
}

func (s *spvService) AddPeer(addr string) error {
return errors.New("AddPeer not implemented")
}

func (s *spvService) GetBlockHeight(h *chainhash.Hash) (int32, error) {
return s.ChainService.GetBlockHeight((*bchchainhash.Hash)(h))
}
Expand Down
23 changes: 22 additions & 1 deletion client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ var _ asset.Rescanner = (*ExchangeWalletSPV)(nil)
var _ asset.FeeRater = (*ExchangeWalletFullNode)(nil)
var _ asset.LogFiler = (*ExchangeWalletSPV)(nil)
var _ asset.Recoverer = (*ExchangeWalletSPV)(nil)
var _ asset.PeerManager = (*ExchangeWalletSPV)(nil)
var _ asset.TxFeeEstimator = (*intermediaryWallet)(nil)

// RecoveryCfg is the information that is transferred from the old wallet
Expand Down Expand Up @@ -895,6 +896,26 @@ func (btc *ExchangeWalletSPV) Rescan(_ context.Context) error {
return w.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()
}

// 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)
}

// 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)
}

// FeeRate satisfies asset.FeeRater.
func (btc *ExchangeWalletFullNode) FeeRate() uint64 {
rate, err := btc.estimateFee(btc.node, 1)
Expand Down Expand Up @@ -1173,7 +1194,7 @@ func OpenSPVWallet(cfg *BTCCloneCFG, walletConstructor BTCWalletConstructor) (*E
decodeAddr: btc.decodeAddr,
}

spvw.wallet = walletConstructor(spvw.dir, spvw.cfg, spvw.chainParams, spvw.log)
spvw.wallet, err = walletConstructor(spvw.dir, spvw.cfg, spvw.chainParams, spvw.log)

btc.node = spvw

Expand Down
Loading

0 comments on commit 1051002

Please sign in to comment.