Skip to content

Commit

Permalink
backend: add ManualReconnect method and call on iOS
Browse files Browse the repository at this point in the history
When the iOS app goes into the background, its TCP
connections (Electrum server connetions) are killed after a
while (~30s).

When the user goes back into the app, we want to immediatelly trigger
a reconnect, not waiting for the regular 30s reconnect timer.
  • Loading branch information
benma committed Sep 30, 2024
1 parent 2cbe6ab commit 45c246e
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 21 deletions.
37 changes: 37 additions & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions backend/bridgecommon/bridgecommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions backend/coins/btc/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@ type Interface interface {
Close()
ConnectionError() error
RegisterOnConnectionErrorChangedEvent(func(error))
ManualReconnect()
}
81 changes: 69 additions & 12 deletions backend/coins/btc/blockchain/mocks/Interface.go

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

4 changes: 4 additions & 0 deletions backend/coins/btc/electrum/failoverclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
7 changes: 6 additions & 1 deletion backend/mobileserver/mobileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ManualReconnect() {
bridgecommon.ManualReconnect()
}
7 changes: 4 additions & 3 deletions frontends/ios/BitBoxApp/BitBoxApp/BitBoxAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SwiftUI
import Mobileserver
import LocalAuthentication
import UIKit

func authenticateUser(completion: @escaping (Bool) -> Void) {
let context = LAContext()
Expand Down Expand Up @@ -105,10 +106,9 @@ class GoAPI: NSObject, MobileserverGoAPIInterfaceProtocol, SetMessageHandlersPro
}
}


@main
struct BitBoxAppApp: App {
var body: some Scene {
var body: some Scene {
WindowGroup {
GridLayout(alignment: .leading) {
let goAPI = GoAPI()
Expand All @@ -118,7 +118,8 @@ struct BitBoxAppApp: App {
setupGoAPI(goAPI: goAPI)
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
MobileserverTriggerAuth()
MobileserverManualReconnect()
MobileserverTriggerAuth()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22

require (
github.com/BitBoxSwiss/bitbox02-api-go v0.0.0-20240925080402-a2115fee878e
github.com/BitBoxSwiss/block-client-go v0.0.0-20240516081043-0d604acd6519
github.com/BitBoxSwiss/block-client-go v0.0.0-20240930105623-12964f9ccf6d
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.4
github.com/btcsuite/btcd/btcutil v1.1.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/BitBoxSwiss/bitbox02-api-go v0.0.0-20240925080402-a2115fee878e h1:wEIIFhiZ58RsVjoKfwSBBD0i75uZ7KATOI/elaBWOOY=
github.com/BitBoxSwiss/bitbox02-api-go v0.0.0-20240925080402-a2115fee878e/go.mod h1:Spf6hQRSylrvdjd7Cv4Tc8rFwlcamJAC8EuA61ARy7U=
github.com/BitBoxSwiss/block-client-go v0.0.0-20240516081043-0d604acd6519 h1:diVA/i8TJFBl9ZyMNX15KjZBpI2Gu63xQTozu6FsTrA=
github.com/BitBoxSwiss/block-client-go v0.0.0-20240516081043-0d604acd6519/go.mod h1:SJTiQZU9ggBzVKMni97rpNS9GddPKErndFXNSDrfEGc=
github.com/BitBoxSwiss/block-client-go v0.0.0-20240930105623-12964f9ccf6d h1:J66dy8IA06jzssANqrdfK7LdiVFIOtuat1nmL6p6zdU=
github.com/BitBoxSwiss/block-client-go v0.0.0-20240930105623-12964f9ccf6d/go.mod h1:SJTiQZU9ggBzVKMni97rpNS9GddPKErndFXNSDrfEGc=
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
Expand Down
Loading

0 comments on commit 45c246e

Please sign in to comment.