Skip to content

Commit

Permalink
Merge pull request #234 from synonymdev/log-hook
Browse files Browse the repository at this point in the history
Hook for logging bump force close txs to the LSP
  • Loading branch information
Jasonvdb authored Apr 25, 2024
2 parents c31e14e + 85b6938 commit a3b90be
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
3 changes: 3 additions & 0 deletions example/ldk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ export const setupLdk = async (
},
trustedZeroConfPeers: [peers.lnd.pubKey],
skipRemoteBackups: !backupServerDetails,
lspLogEvent: async (payload) => {
console.log('Log event for LSP:', JSON.stringify(payload));
},
});

if (lmStart.isErr()) {
Expand Down
3 changes: 2 additions & 1 deletion lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ enum class EventTypes {
new_channel,
network_graph_updated,
channel_manager_restarted,
backup_state_update
backup_state_update,
lsp_log
}
//*****************************************************************

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import com.facebook.react.bridge.WritableMap
import com.reactnativeldk.*
import org.json.JSONArray
import org.ldk.batteries.ChannelManagerConstructor
import org.ldk.structs.BumpTransactionEvent
import org.ldk.structs.ClosureReason
import org.ldk.structs.Event
import org.ldk.structs.Event.BumpTransaction
import org.ldk.structs.Option_ThirtyTwoBytesZ
import org.ldk.structs.Option_u64Z
import org.ldk.structs.PaymentPurpose
Expand Down Expand Up @@ -190,6 +192,18 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {

(event as? Event.ChannelPending)?.let { channelPending ->
}

(event as? Event.BumpTransaction)?.let { bumpTransaction ->
LdkEventEmitter.send(EventTypes.native_log, "BumpTransaction request")

(bumpTransaction.bump_transaction as? BumpTransactionEvent.ChannelClose)?.let { channelClose ->
val body = Arguments.createMap()
body.putString("commitment_tx", channelClose.commitment_tx.hexEncodedString())
body.putInt("commitment_tx_fee", channelClose.commitment_tx_fee_satoshis.toInt())
body.putInt("pending_htlcs_count", channelClose.pending_htlcs.size)
return LdkEventEmitter.send(EventTypes.lsp_log, body)
}
}
}

override fun persist_manager(channel_manager_bytes: ByteArray?) {
Expand Down
19 changes: 16 additions & 3 deletions lib/ios/Classes/LdkChannelManagerPersister.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
"reason": reasonString
]
)

return
case .DiscardFunding:
guard let discardFunding = event.getValueAsDiscardFunding() else {
Expand Down Expand Up @@ -293,8 +294,21 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
guard let bumpTransaction = event.getValueAsBumpTransaction() else {
return handleEventError(event)
}

LdkEventEmitter.shared.send(withEvent: .native_log, body: "TODO📣: BumpTransaction")

LdkEventEmitter.shared.send(withEvent: .native_log, body: "BumpTransaction request")

if let channelClose = bumpTransaction.getValueAsChannelClose() {
let body: [String: Encodable] = [
"commitment_tx": Data(channelClose.getCommitmentTx()).hexEncodedString(),
"commitment_tx_fee": channelClose.getCommitmentTxFeeSatoshis(),
"pending_htlcs_count": channelClose.getPendingHtlcs().count
]

LdkEventEmitter.shared.send(withEvent: .lsp_log, body: body)
return
}

LdkEventEmitter.shared.send(withEvent: .native_log, body: "BumpTransaction event not handled")
return
case .ProbeFailed:
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Unused Persister event: ProbeFailed")
Expand All @@ -308,7 +322,6 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
case .HTLCIntercepted:
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Unused Persister event: HTLCIntercepted")
return

case .HTLCHandlingFailed:
LdkEventEmitter.shared.send(withEvent: .native_log, body: "Unused Persister event: HTLCHandlingFailed")
return
Expand Down
3 changes: 2 additions & 1 deletion lib/ios/Ldk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum EventTypes: String, CaseIterable {
case network_graph_updated = "network_graph_updated"
case channel_manager_restarted = "channel_manager_restarted"
case backup_state_update = "backup_state_update"
case lsp_log = "lsp_log"
}
//*****************************************************************

Expand Down Expand Up @@ -493,7 +494,7 @@ class Ldk: NSObject {
currentBlockchainTipHash = blockHash
currentBlockchainHeight = blockHeight
addForegroundObserver()

return handleResolve(resolve, .channel_manager_init_success)
}

Expand Down
18 changes: 18 additions & 0 deletions lib/src/lightning-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import {
TCreatePaymentReq,
TBackupServerDetails,
IAddress,
TLspLogPayload,

Check warning on line 58 in lib/src/lightning-manager.ts

View workflow job for this annotation

GitHub Actions / mocha-ios

'TLspLogPayload' is defined but never used
TLspLogEvent,
} from './utils/types';
import {
appendPath,
Expand Down Expand Up @@ -136,6 +138,7 @@ class LightningManager {
});
trustedZeroConfPeers: string[] = [];
broadcastTransaction: TBroadcastTransaction = async (): Promise<any> => {};
lspLogEvent: TLspLogEvent | undefined;
pathFailedSubscription: EmitterSubscription | undefined;
paymentFailedSubscription: EmitterSubscription | undefined;
paymentSentSubscription: EmitterSubscription | undefined;
Expand Down Expand Up @@ -225,6 +228,7 @@ class LightningManager {
EEventTypes.channel_manager_restarted,
this.onChannelManagerRestarted.bind(this),
);
ldk.onEvent(EEventTypes.lsp_log, this.onLspLogEvent.bind(this));
}

/**
Expand Down Expand Up @@ -293,6 +297,7 @@ class LightningManager {
trustedZeroConfPeers = [],
skipParamCheck = false,
skipRemoteBackups = false,
lspLogEvent,
}: TLdkStart): Promise<Result<string>> {
if (!account) {
return err(
Expand Down Expand Up @@ -367,6 +372,7 @@ class LightningManager {
this.watchOutputs = [];
this.confirmedWatchOutputs = await this.getConfirmedWatchOutputs();
this.trustedZeroConfPeers = trustedZeroConfPeers;
this.lspLogEvent = lspLogEvent;

if (!this.baseStoragePath) {
return this.handleStartError(
Expand Down Expand Up @@ -2051,6 +2057,18 @@ class LightningManager {
await this.syncLdk();
}

private async onLspLogEvent(payload: any): Promise<void> {
if (!this.lspLogEvent) {
return;
}
const nodeIdRes = await ldk.nodeId();

await this.lspLogEvent({
body: JSON.stringify(payload),
nodeId: nodeIdRes.isOk() ? nodeIdRes.value : '',
});
}

/**
* Called with each sync to remove expired and unclaimed invoices.
* An invoice is expired if there is no payments claimed (received) for it
Expand Down
5 changes: 5 additions & 0 deletions lib/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export enum EEventTypes {
network_graph_updated = 'network_graph_updated',
channel_manager_restarted = 'channel_manager_restarted',
backup_state_update = 'backup_state_update',
lsp_log = 'lsp_log',
}

//LDK event responses
Expand Down Expand Up @@ -569,6 +570,7 @@ export type TLdkStart = {
trustedZeroConfPeers?: string[];
skipParamCheck?: boolean;
skipRemoteBackups?: boolean;
lspLogEvent?: TLspLogEvent;
};

export interface IAddress {
Expand All @@ -586,6 +588,9 @@ export type TGetScriptPubKeyHistoryResponse = { height: number; txid: string };

export type TBroadcastTransaction = (rawTx: string) => Promise<any>;

export type TLspLogPayload = { nodeId: string; body: string };
export type TLspLogEvent = (payload: TLspLogPayload) => Promise<void>;

export type TGetFees = () => Promise<TFeeUpdateReq>;

export type TVout = { hex: string; n: number; value: number };
Expand Down

0 comments on commit a3b90be

Please sign in to comment.