Skip to content

Commit fea3e0c

Browse files
authored
Merge pull request lightningdevkit#45 from TheBlueMatt/main
Correct clone logic for `Str`.
2 parents 551090b + b187ebb commit fea3e0c

File tree

6 files changed

+56
-31
lines changed

6 files changed

+56
-31
lines changed

lightning-c-bindings/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ crate-type = ["staticlib"
1818
bitcoin = "0.27"
1919
secp256k1 = { version = "0.20.3", features = ["global-context-less-secure"] }
2020
# Note that the following line is matched by genbindings to update the path
21-
lightning = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "367a2ccf2c1856bba67f12c82a4ab80d7941616f", features = ["allow_wallclock_use"] }
22-
lightning-persister = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "367a2ccf2c1856bba67f12c82a4ab80d7941616f" }
23-
lightning-invoice = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "367a2ccf2c1856bba67f12c82a4ab80d7941616f" }
24-
lightning-background-processor = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "367a2ccf2c1856bba67f12c82a4ab80d7941616f" }
21+
lightning = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "ad819ea70572b4775691e3fc386a788ff9438975", features = ["allow_wallclock_use"] }
22+
lightning-persister = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "ad819ea70572b4775691e3fc386a788ff9438975" }
23+
lightning-invoice = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "ad819ea70572b4775691e3fc386a788ff9438975" }
24+
lightning-background-processor = { git = "https://github.com/rust-bitcoin/rust-lightning", rev = "ad819ea70572b4775691e3fc386a788ff9438975" }
2525

2626
# Always force panic=abort, further options are set in the genbindings.sh build script
2727
[profile.dev]

lightning-c-bindings/include/lightning.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11994,6 +11994,12 @@ struct LDKCResult_NetworkGraphDecodeErrorZ CResult_NetworkGraphDecodeErrorZ_err(
1199411994
*/
1199511995
void CResult_NetworkGraphDecodeErrorZ_free(struct LDKCResult_NetworkGraphDecodeErrorZ _res);
1199611996

11997+
/**
11998+
* Creates a new CResult_NetworkGraphDecodeErrorZ which has the same data as `orig`
11999+
* but with all dynamically-allocated buffers duplicated in new buffers.
12000+
*/
12001+
struct LDKCResult_NetworkGraphDecodeErrorZ CResult_NetworkGraphDecodeErrorZ_clone(const struct LDKCResult_NetworkGraphDecodeErrorZ *NONNULL_PTR orig);
12002+
1199712003
/**
1199812004
* Creates a new CResult_NetAddressu8Z in the success state.
1199912005
*/
@@ -19434,16 +19440,6 @@ struct LDKCVec_u8Z ShutdownScript_write(const struct LDKShutdownScript *NONNULL_
1943419440
*/
1943519441
struct LDKCResult_ShutdownScriptDecodeErrorZ ShutdownScript_read(struct LDKu8slice ser);
1943619442

19437-
/**
19438-
* Generates a P2PKH script pubkey from the given [`PubkeyHash`].
19439-
*/
19440-
MUST_USE_RES struct LDKShutdownScript ShutdownScript_new_p2pkh(const uint8_t (*pubkey_hash)[20]);
19441-
19442-
/**
19443-
* Generates a P2SH script pubkey from the given [`ScriptHash`].
19444-
*/
19445-
MUST_USE_RES struct LDKShutdownScript ShutdownScript_new_p2sh(const uint8_t (*script_hash)[20]);
19446-
1944719443
/**
1944819444
* Generates a P2WPKH script pubkey from the given [`WPubkeyHash`].
1944919445
*/
@@ -19821,6 +19817,11 @@ struct LDKCResult_RouteLightningErrorZ get_route(struct LDKPublicKey our_node_id
1982119817
*/
1982219818
void NetworkGraph_free(struct LDKNetworkGraph this_obj);
1982319819

19820+
/**
19821+
* Creates a copy of the NetworkGraph
19822+
*/
19823+
struct LDKNetworkGraph NetworkGraph_clone(const struct LDKNetworkGraph *NONNULL_PTR orig);
19824+
1982419825
/**
1982519826
* Frees any resources used by the ReadOnlyNetworkGraph, if is_owned is set and inner is non-NULL.
1982619827
*/

lightning-c-bindings/src/c_types/derived.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8397,6 +8397,23 @@ impl From<crate::c_types::CResultTempl<crate::lightning::routing::network_graph:
83978397
}
83988398
}
83998399
}
8400+
impl Clone for CResult_NetworkGraphDecodeErrorZ {
8401+
fn clone(&self) -> Self {
8402+
if self.result_ok {
8403+
Self { result_ok: true, contents: CResult_NetworkGraphDecodeErrorZPtr {
8404+
result: Box::into_raw(Box::new(<crate::lightning::routing::network_graph::NetworkGraph>::clone(unsafe { &*self.contents.result })))
8405+
} }
8406+
} else {
8407+
Self { result_ok: false, contents: CResult_NetworkGraphDecodeErrorZPtr {
8408+
err: Box::into_raw(Box::new(<crate::lightning::ln::msgs::DecodeError>::clone(unsafe { &*self.contents.err })))
8409+
} }
8410+
}
8411+
}
8412+
}
8413+
#[no_mangle]
8414+
/// Creates a new CResult_NetworkGraphDecodeErrorZ which has the same data as `orig`
8415+
/// but with all dynamically-allocated buffers duplicated in new buffers.
8416+
pub extern "C" fn CResult_NetworkGraphDecodeErrorZ_clone(orig: &CResult_NetworkGraphDecodeErrorZ) -> CResult_NetworkGraphDecodeErrorZ { Clone::clone(&orig) }
84008417
#[repr(C)]
84018418
/// The contents of CResult_NetAddressu8Z
84028419
pub union CResult_NetAddressu8ZPtr {

lightning-c-bindings/src/c_types/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ pub(crate) fn deserialize_obj_arg<A, I: lightning::util::ser::ReadableArgs<A>>(s
435435
}
436436

437437
#[repr(C)]
438-
#[derive(Clone)]
439438
/// A Rust str object, ie a reference to a UTF8-valid string.
440439
/// This is *not* null-terminated so cannot be used directly as a C string!
441440
pub struct Str {
@@ -479,6 +478,11 @@ impl Into<Str> for String {
479478
Str { chars: s.as_ptr(), len: s.len(), chars_is_owned: true }
480479
}
481480
}
481+
impl Clone for Str {
482+
fn clone(&self) -> Self {
483+
self.into_str().clone().into()
484+
}
485+
}
482486

483487
impl Drop for Str {
484488
fn drop(&mut self) {

lightning-c-bindings/src/lightning/ln/script.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,6 @@ pub extern "C" fn ShutdownScript_read(ser: crate::c_types::u8slice) -> crate::c_
176176
let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
177177
local_res
178178
}
179-
/// Generates a P2PKH script pubkey from the given [`PubkeyHash`].
180-
#[must_use]
181-
#[no_mangle]
182-
pub extern "C" fn ShutdownScript_new_p2pkh(pubkey_hash: *const [u8; 20]) -> ShutdownScript {
183-
let mut ret = lightning::ln::script::ShutdownScript::new_p2pkh(&bitcoin::hash_types::PubkeyHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *pubkey_hash }.clone())));
184-
ShutdownScript { inner: ObjOps::heap_alloc(ret), is_owned: true }
185-
}
186-
187-
/// Generates a P2SH script pubkey from the given [`ScriptHash`].
188-
#[must_use]
189-
#[no_mangle]
190-
pub extern "C" fn ShutdownScript_new_p2sh(script_hash: *const [u8; 20]) -> ShutdownScript {
191-
let mut ret = lightning::ln::script::ShutdownScript::new_p2sh(&bitcoin::hash_types::ScriptHash::from_hash(bitcoin::hashes::Hash::from_inner(unsafe { *script_hash }.clone())));
192-
ShutdownScript { inner: ObjOps::heap_alloc(ret), is_owned: true }
193-
}
194-
195179
/// Generates a P2WPKH script pubkey from the given [`WPubkeyHash`].
196180
#[must_use]
197181
#[no_mangle]

lightning-c-bindings/src/lightning/routing/network_graph.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ impl NetworkGraph {
6565
ret
6666
}
6767
}
68+
impl Clone for NetworkGraph {
69+
fn clone(&self) -> Self {
70+
Self {
71+
inner: if <*mut nativeNetworkGraph>::is_null(self.inner) { std::ptr::null_mut() } else {
72+
ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
73+
is_owned: true,
74+
}
75+
}
76+
}
77+
#[allow(unused)]
78+
/// Used only if an object of this type is returned as a trait impl by a method
79+
pub(crate) extern "C" fn NetworkGraph_clone_void(this_ptr: *const c_void) -> *mut c_void {
80+
Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeNetworkGraph)).clone() })) as *mut c_void
81+
}
82+
#[no_mangle]
83+
/// Creates a copy of the NetworkGraph
84+
pub extern "C" fn NetworkGraph_clone(orig: &NetworkGraph) -> NetworkGraph {
85+
orig.clone()
86+
}
6887

6988
use lightning::routing::network_graph::ReadOnlyNetworkGraph as nativeReadOnlyNetworkGraphImport;
7089
type nativeReadOnlyNetworkGraph = nativeReadOnlyNetworkGraphImport<'static>;

0 commit comments

Comments
 (0)