Skip to content

Commit

Permalink
Merge pull request #270 from synonymdev/fix/2133-handle-dropped-peers…
Browse files Browse the repository at this point in the history
…-crash

Android: Fix crash in handleDroppedPeers
  • Loading branch information
Jasonvdb authored Sep 9, 2024
2 parents 3767cc7 + 981c124 commit ebb1440
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ cd ios && pod install && cd ../
### Android
- Open `lib/android` in Android Studio
- To enable documentation for the LDK code, follow this guide: [How to attach JavaDoc to the library in Android Studio](https://medium.com/@mydogtom/tip-how-to-attach-javadoc-to-the-library-in-android-studio-5ff43c4303b3), ie.:
1. Switch to `Project` view in the Project browser tool windo
1. Switch to `Project` view in the Project browser tool window
2. Expand `External Libraries`
3. Right click on `Gradle: ./libs/LDK-release.aar` then `Library Properties…`
4. Tap the ➕ button and select the `./lib/android/libs/ldk-java-javadoc.jar` file
5. In the popup that appears select `JavaDocs` and tap `OK` then `OK` again
### Version Bump
```sh
# apply your changes
cd example
yarn reinstall # bump versions package.json & podfile
cd ../
# copy version from `./lib/package.json` to `backup-server/package.json`
```
## Running example app
See also [`./example/README.md`](./example/README.md)
```bash
Expand Down
2 changes: 1 addition & 1 deletion backup-server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backup-server",
"version": "0.0.146",
"version": "0.0.151",
"description": "",
"main": "index.js",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ PODS:
- React-jsinspector (0.72.4)
- React-logger (0.72.4):
- glog
- react-native-ldk (0.0.150):
- react-native-ldk (0.0.151):
- React
- react-native-randombytes (3.6.1):
- React-Core
Expand Down Expand Up @@ -621,7 +621,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: c7f826e40fa9cab5d37cab6130b1af237332b594
React-jsinspector: aaed4cf551c4a1c98092436518c2d267b13a673f
React-logger: da1ebe05ae06eb6db4b162202faeafac4b435e77
react-native-ldk: 2b19de9eb94dcfd46f3f2a7191502292b75a5d7a
react-native-ldk: a7e71785237dd3d12dc52b4287abd88c865f5262
react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846
react-native-tcp-socket: c1b7297619616b4c9caae6889bcb0aba78086989
React-NativeModulesApple: edb5ace14f73f4969df6e7b1f3e41bef0012740f
Expand Down
27 changes: 13 additions & 14 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.ldk.enums.Currency
import org.ldk.enums.Network
import org.ldk.enums.Recipient
import org.ldk.enums.RetryableSendFailure
import org.ldk.impl.bindings.LDKPaymentSendFailure.DuplicatePayment
import org.ldk.impl.bindings.get_ldk_c_bindings_version
import org.ldk.impl.bindings.get_ldk_version
import org.ldk.structs.*
Expand All @@ -29,6 +28,10 @@ import java.nio.file.Files
import java.nio.file.Paths
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.ScheduledThreadPoolExecutor
import java.util.concurrent.TimeUnit


//MARK: ************Replicate in typescript and swift************
Expand Down Expand Up @@ -182,9 +185,9 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
private var currentBlockchainHeight: Double? = null

//List of peers that "should" remain connected. Stores address: String, port: Double, pubKey: String
private var addedPeers: MutableList<HashMap<String, Any>> = mutableListOf()
private var currentlyConnectingPeers: MutableList<String> = mutableListOf()
private var timerTaskScheduled: Boolean = false
private var addedPeers = ConcurrentLinkedQueue<Map<String, Any>>()
private var currentlyConnectingPeers = ConcurrentLinkedQueue<String>()
private var periodicDroppedPeersHandler: ScheduledFuture<*>? = null

//Static to be accessed from other classes
companion object {
Expand Down Expand Up @@ -512,14 +515,10 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod

peerHandler = channelManagerConstructor!!.nio_peer_handler

//Start watching for dropped peers every 1 second
if (!timerTaskScheduled) {
Timer().schedule(object : TimerTask() {
override fun run() {
handleDroppedPeers()
}
}, 1000, 3000)
timerTaskScheduled = true
//after 1s, Start watching for dropped peers every 3 seconds
if (periodicDroppedPeersHandler == null) {
periodicDroppedPeersHandler = ScheduledThreadPoolExecutor(1)
.scheduleWithFixedDelay(::handleDroppedPeers,1, 3, TimeUnit.SECONDS)
}

//Cached for restarts
Expand Down Expand Up @@ -621,7 +620,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
handleResolve(promise, LdkCallbackResponses.chain_sync_success)
}

fun handleDroppedPeers() {
private fun handleDroppedPeers() {
peerHandler ?: return LdkEventEmitter.send(EventTypes.native_log, "Handling dropped peers error. Peer handler not initialized.")

LdkEventEmitter.send(EventTypes.native_log, "Checking for dropped peers")
Expand Down Expand Up @@ -679,7 +678,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
currentlyConnectingPeers.remove(pubKey)

//Should retry if success or fail
if (!addedPeers.map { it["pubKey"] as String }.contains(pubKey)) {
if (addedPeers.none { it["pubKey"] as String == pubKey }) {
addedPeers.add(hashMapOf(
"address" to address,
"port" to port,
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@synonymdev/react-native-ldk",
"title": "React Native LDK",
"version": "0.0.150",
"version": "0.0.151",
"description": "React Native wrapper for LDK",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down

0 comments on commit ebb1440

Please sign in to comment.