diff --git a/backend/backend.go b/backend/backend.go index 36fc3ad82a..4e1ce4293b 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -538,6 +538,43 @@ func (backend *Backend) Coin(code coinpkg.Code) (coinpkg.Coin, error) { return coin, nil } +// ManualReconnect triggers reconnecting to Electrum servers if their connection is down. +// Only coin connections that were previously established are reconnected. +// Calling this is a no-op for coins that are already connected. +func (backend *Backend) ManualReconnect() { + var electrumCoinCodes []coinpkg.Code + if backend.arguments.Testing() { + electrumCoinCodes = []coinpkg.Code{ + coinpkg.CodeTBTC, + coinpkg.CodeTLTC, + } + } else { + electrumCoinCodes = []coinpkg.Code{ + coinpkg.CodeBTC, + coinpkg.CodeLTC, + } + } + backend.log.Info("Manually reconnecting") + for _, code := range electrumCoinCodes { + c, err := backend.Coin(code) + if err != nil { + backend.log.WithError(err).Errorf("could not find coin: %s", code) + continue + } + btcCoin, ok := c.(*btc.Coin) + if !ok { + backend.log.Errorf("Expected %s to be a btc coin", code) + continue + } + blockchain := btcCoin.Blockchain() + if blockchain == nil { + // Not initialized yet + continue + } + blockchain.ManualReconnect() + } +} + // Testing returns whether this backend is for testing only. func (backend *Backend) Testing() bool { return backend.arguments.Testing() diff --git a/backend/bridgecommon/bridgecommon.go b/backend/bridgecommon/bridgecommon.go index 86d71dc6ef..c34e355dae 100644 --- a/backend/bridgecommon/bridgecommon.go +++ b/backend/bridgecommon/bridgecommon.go @@ -166,6 +166,18 @@ func UsingMobileDataChanged() { }) } +// ManualReconnect exposes the ManualReconnect backend method. +func ManualReconnect() { + mu.Lock() + defer mu.Unlock() + + if globalBackend == nil { + return + } + globalBackend.ManualReconnect() + +} + // BackendEnvironment implements backend.Environment. type BackendEnvironment struct { NotifyUserFunc func(string) diff --git a/backend/coins/btc/blockchain/blockchain.go b/backend/coins/btc/blockchain/blockchain.go index 249ed2d6e0..eb74ae0085 100644 --- a/backend/coins/btc/blockchain/blockchain.go +++ b/backend/coins/btc/blockchain/blockchain.go @@ -118,4 +118,5 @@ type Interface interface { Close() ConnectionError() error RegisterOnConnectionErrorChangedEvent(func(error)) + ManualReconnect() } diff --git a/backend/coins/btc/electrum/failoverclient.go b/backend/coins/btc/electrum/failoverclient.go index a443e785e1..ef104be651 100644 --- a/backend/coins/btc/electrum/failoverclient.go +++ b/backend/coins/btc/electrum/failoverclient.go @@ -156,6 +156,10 @@ func (f *failoverClient) TransactionGet(txHash chainhash.Hash) (*wire.MsgTx, err }) } +func (f *failoverClient) ManualReconnect() { + f.failover.ManualReconnect() +} + func (f *failoverClient) Close() { f.failover.Close() } diff --git a/backend/mobileserver/mobileserver.go b/backend/mobileserver/mobileserver.go index 57b21bf8de..6764cf75d1 100644 --- a/backend/mobileserver/mobileserver.go +++ b/backend/mobileserver/mobileserver.go @@ -227,8 +227,13 @@ func CancelAuth() { bridgecommon.CancelAuth() } -// AuthResult triggers an auth feeedback notification (auth-ok/auth-err) towards the frontend, +// AuthResult triggers an auth feedback notification (auth-ok/auth-err) towards the frontend, // depending on the input value. func AuthResult(ok bool) { bridgecommon.AuthResult(ok) } + +// ManualReconnect wraps bridgecommon.ManualReconnet +func AuthResult() { + bridgecommon.ManualReconnect() +}