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.

```
go get github.com/BitBoxSwiss/block-client-go@924dde9
go mod tidy
go mod vendor
```
  • Loading branch information
benma committed Oct 9, 2024
1 parent 2cbe6ab commit b52d3ac
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 24 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.RLock()
defer mu.RUnlock()

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.

8 changes: 8 additions & 0 deletions backend/coins/btc/blockchain/mocks/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type BlockchainMock struct {
MockConnectionError func() error

MockRegisterOnConnectionErrorChangedEvent func(func(error))
MockManualReconnect func()
}

// ScriptHashGetHistory implements Interface.
Expand Down Expand Up @@ -135,3 +136,10 @@ func (b *BlockchainMock) RegisterOnConnectionErrorChangedEvent(f func(error)) {
b.MockRegisterOnConnectionErrorChangedEvent(f)
}
}

// ManualReconnect implements Interface.
func (b *BlockchainMock) ManualReconnect() {
if b.MockManualReconnect != nil {
b.MockManualReconnect()
}
}
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.ManualReconnect.
func ManualReconnect() {
bridgecommon.ManualReconnect()
}
12 changes: 6 additions & 6 deletions frontends/ios/BitBoxApp/BitBoxApp/BitBoxAppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,23 @@ class GoEnvironment: NSObject, MobileserverGoEnvironmentInterfaceProtocol {

class GoAPI: NSObject, MobileserverGoAPIInterfaceProtocol, SetMessageHandlersProtocol {
var handlers: MessageHandlersProtocol?

func pushNotify(_ msg: String?) {
self.handlers?.pushNotificationHandler(msg: msg!)
}

func respond(_ queryID: Int, response: String?) {
self.handlers?.callResponseHandler(queryID: queryID, response: response!)
}

func setMessageHandlers(handlers: MessageHandlersProtocol) {
self.handlers = handlers
}
}


@main
struct BitBoxAppApp: App {
var body: some Scene {
var body: some Scene {
WindowGroup {
GridLayout(alignment: .leading) {
let goAPI = GoAPI()
Expand All @@ -118,12 +117,13 @@ struct BitBoxAppApp: App {
setupGoAPI(goAPI: goAPI)
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
MobileserverTriggerAuth()
MobileserverManualReconnect()
MobileserverTriggerAuth()
}
}
}
}

func setupGoAPI(goAPI: MobileserverGoAPIInterfaceProtocol) {
let appSupportDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
do {
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-20241009081439-924dde98b9c1
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-20241009081439-924dde98b9c1 h1:5hjP8mYSVKFibesrz8L6U0Vp5zSJt0LwXB3DSZGhnSo=
github.com/BitBoxSwiss/block-client-go v0.0.0-20241009081439-924dde98b9c1/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 b52d3ac

Please sign in to comment.