Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check rgs updates every hour #187

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/android/src/main/java/com/reactnativeldk/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fun RapidGossipSync.downloadAndUpdateGraph(downloadUrl: String, tempStoragePath:
return@downloadFile
}

val res = update_network_graph_no_std(File(destinationFile).readBytes(), Option_u64Z.some((System.currentTimeMillis() / 1000)))
val res = update_network_graph(File(destinationFile).readBytes())
if (!res.is_ok()) {
val error = res as? Result_u32GraphSyncErrorZ.Result_u32GraphSyncErrorZ_Err

Expand Down
10 changes: 5 additions & 5 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,13 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod

//If it's been more than 24 hours then we need to update RGS
val timestamp = if (networkGraph!!._last_rapid_gossip_sync_timestamp is Option_u32Z.Some) (networkGraph!!._last_rapid_gossip_sync_timestamp as Option_u32Z.Some).some.toLong() else 0 // (networkGraph!!._last_rapid_gossip_sync_timestamp as Option_u32Z.Some).some
val hoursDiffSinceLastRGS = (System.currentTimeMillis() / 1000 - timestamp) / 60 / 60
if (hoursDiffSinceLastRGS < 24) {
LdkEventEmitter.send(EventTypes.native_log, "Skipping rapid gossip sync. Last updated $hoursDiffSinceLastRGS hours ago.")
val minutesDiffSinceLastRGS = (System.currentTimeMillis() / 1000 - timestamp) / 60
if (minutesDiffSinceLastRGS < 60) {
LdkEventEmitter.send(EventTypes.native_log, "Skipping rapid gossip sync. Last updated ${minutesDiffSinceLastRGS/60} hours ago.")
return handleResolve(promise, LdkCallbackResponses.network_graph_init_success)
}

LdkEventEmitter.send(EventTypes.native_log, "Rapid gossip sync applying update. Last updated $hoursDiffSinceLastRGS hours ago.")
LdkEventEmitter.send(EventTypes.native_log, "Rapid gossip sync applying update. Last updated ${minutesDiffSinceLastRGS/60} hours ago.")

rapidGossipSync!!.downloadAndUpdateGraph(rapidGossipSyncUrl, "$accountStoragePath/rapid_gossip_sync/", timestamp) { error ->
if (error != null) {
Expand Down Expand Up @@ -1091,7 +1091,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
logDump.add("Claimable balances unavailable. Chain monitor not set yet")
}

networkGraph?._last_rapid_gossip_sync_timestamp?.let { res ->
channelManagerConstructor?.net_graph?._last_rapid_gossip_sync_timestamp?.let { res ->
val syncTimestamp = if (res is Option_u32Z.Some) (res as Option_u32Z.Some).some.toLong() else 0
if (syncTimestamp == 0L) {
logDump.add("Last rapid gossip sync time: NEVER")
Expand Down
10 changes: 6 additions & 4 deletions lib/ios/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ extension RouteHintHop {
//Our own channels
extension ChannelDetails {
var asJson: [String: Any] {
let shortChannelId = getShortChannelId() != nil ? String(getShortChannelId()!) : ""

return [
"channel_id": Data(getChannelId() ?? []).hexEncodedString(),
"is_public": getIsPublic(),
Expand All @@ -124,9 +126,9 @@ extension ChannelDetails {
"channel_type": Data(getChannelType()?.write() ?? []).hexEncodedString(),
"user_channel_id": Data(getUserChannelId()).hexEncodedString(), //String
"confirmations_required": getConfirmationsRequired() as Any, // Optional number
"short_channel_id": getShortChannelId() != nil ? String(getShortChannelId()!) : "", //String
"inbound_scid_alias": getInboundScidAlias() != nil ? String(getInboundScidAlias()!) : "", //String
"inbound_payment_scid": getInboundPaymentScid() != nil ? String(getInboundPaymentScid()!) : "", //String
"short_channel_id": shortChannelId,
"inbound_scid_alias": getInboundScidAlias() != nil ? String(getInboundScidAlias()!) : shortChannelId, //String
"inbound_payment_scid": getInboundPaymentScid() as Any, //Optional number,
"inbound_capacity_sat": getInboundCapacityMsat() / 1000,
"outbound_capacity_sat": getOutboundCapacityMsat() / 1000,
"channel_value_satoshis": getChannelValueSatoshis(),
Expand Down Expand Up @@ -279,7 +281,7 @@ extension RapidGossipSync {

guard let self = self else { return }

let res = self.updateNetworkGraphNoStd(updateData: [UInt8](try! Data(contentsOf: destinationFile)), currentTimeUnix: UInt64(Date().timeIntervalSince1970))
let res = self.updateNetworkGraph(updateData: [UInt8](try! Data(contentsOf: destinationFile)))
guard res.isOk() else {
var errorMessage = "Failed to update network graph."
switch res.getError()?.getValueType() {
Expand Down
10 changes: 5 additions & 5 deletions lib/ios/Ldk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ class Ldk: NSObject {

//If it's been more than 24 hours then we need to update RGS
let timestamp = networkGraph?.getLastRapidGossipSyncTimestamp() ?? 0
let hoursDiffSinceLastRGS = (Calendar.current.dateComponents([.hour], from: Date.init(timeIntervalSince1970: TimeInterval(timestamp)), to: Date()).hour)!
let minutesDiffSinceLastRGS = (Calendar.current.dateComponents([.minute], from: Date.init(timeIntervalSince1970: TimeInterval(timestamp)), to: Date()).minute)!

guard hoursDiffSinceLastRGS > 24 else {
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Skipping rapid gossip sync. Last updated \(hoursDiffSinceLastRGS) hours ago.")
guard minutesDiffSinceLastRGS > 60 else {
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Skipping rapid gossip sync. Last updated \(minutesDiffSinceLastRGS/60) hours ago.")
return handleResolve(resolve, .network_graph_init_success)
}

LdkEventEmitter.shared.send(withEvent: .native_log, body: "Rapid gossip sync applying update. Last updated \(hoursDiffSinceLastRGS) hours ago.")
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Rapid gossip sync applying update. Last updated \(minutesDiffSinceLastRGS/60) hours ago.")

rapidGossipSync!.downloadAndUpdateGraph(downloadUrl: String(rapidGossipSyncUrl), tempStoragePath: rapidGossipSyncStoragePath, timestamp: timestamp) { [weak self] error in
guard let self = self else { return }
Expand Down Expand Up @@ -1176,7 +1176,7 @@ class Ldk: NSObject {
logDump.append("Claimable balances unavailable. Chain monitor not set yet")
}

if let syncTimestamp = networkGraph?.getLastRapidGossipSyncTimestamp() {
if let syncTimestamp = channelManagerConstructor?.netGraph?.getLastRapidGossipSyncTimestamp() {
let date = Date(timeIntervalSince1970: TimeInterval(syncTimestamp))

let dateFormatter = DateFormatter()
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.118",
"version": "0.0.119",
"description": "React Native wrapper for LDK",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
Loading