Skip to content

Commit 82eec5c

Browse files
committed
add mechanism to detect duplicate channel monitors getting deserialized
1 parent 225908e commit 82eec5c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

bindings/batteries/ChannelManagerConstructor.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
enum InvalidSerializedDataError: Error {
1111
case invalidSerializedChannelMonitor
1212
case invalidSerializedChannelManager
13+
case duplicateSerializedChannelMonitor
1314
}
1415

1516
public class ChannelManagerConstructor: NativeTypeWrapper {
@@ -49,6 +50,8 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
4950

5051
var monitors: [LDKChannelMonitor] = []
5152
self.channel_monitors = []
53+
54+
var monitorFundingSet = Set<[UInt8]>()
5255

5356
for currentSerializedChannelMonitor in channel_monitors_serialized {
5457
let channelMonitorResult: Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ = UtilMethods.constructor_BlockHashChannelMonitorZ_read(ser: currentSerializedChannelMonitor, arg: keys_interface)
@@ -60,13 +63,19 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
6063
let value: LDKCResult_C2Tuple_BlockHashChannelMonitorZDecodeErrorZPtr = channelMonitorResult.cOpaqueStruct!.contents
6164
let a: LDKThirtyTwoBytes = value.result!.pointee.a
6265
let b: LDKChannelMonitor = value.result!.pointee.b
66+
67+
let nativeA = Bindings.LDKThirtyTwoBytes_to_array(nativeType: a);
68+
if monitorFundingSet.contains(nativeA) {
69+
throw InvalidSerializedDataError.duplicateSerializedChannelMonitor
70+
}
71+
monitorFundingSet.insert(nativeA)
6372

6473
let clonedChannelMonitor = ChannelMonitor(pointer: b).dangle().clone()
6574
// var clonedChannelMonitor = currentChannelMonitor.clone(orig: currentChannelMonitor)
6675
clonedChannelMonitor.cOpaqueStruct?.is_owned = false // is_owned should never have to be modified
6776

6877
monitors.append(clonedChannelMonitor.cOpaqueStruct!)
69-
self.channel_monitors.append((clonedChannelMonitor, Bindings.LDKThirtyTwoBytes_to_array(nativeType: a)))
78+
self.channel_monitors.append((clonedChannelMonitor, nativeA))
7079
}
7180

7281
print("Collected channel monitors, reading channel manager")

0 commit comments

Comments
 (0)