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

Hook for logging bump force close txs to the LSP #234

Merged
merged 2 commits into from
Apr 25, 2024
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
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 @@
TCreatePaymentReq,
TBackupServerDetails,
IAddress,
TLspLogPayload,

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

View workflow job for this annotation

GitHub Actions / Run lint check

'TLspLogPayload' is defined but never used

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

if (!this.baseStoragePath) {
return this.handleStartError(
Expand Down Expand Up @@ -1905,7 +1911,7 @@
}

private onChannelManagerPendingHtlcsForwardable(
res: TChannelManagerPendingHtlcsForwardable,

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

View workflow job for this annotation

GitHub Actions / Run lint check

'res' is defined but never used. Allowed unused args must match /^_/u

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

View workflow job for this annotation

GitHub Actions / mocha-ios

'res' is defined but never used. Allowed unused args must match /^_/u
): void {
ldk.processPendingHtlcForwards().catch(console.error);
}
Expand Down Expand Up @@ -2051,6 +2057,18 @@
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
Loading