X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Frouting%2Fnetwork_graph.rs;h=b4c8de585c55dd57450f91c5d300a21de35aa0b8;hb=cb83cfe366aaa07179cac1079694e9ea5c6cc9c6;hp=89f20fd4889487224864a0846ce22b2a70e0e6c0;hpb=96b2927a9a548509d77fba3130791ebb61dfded6;p=rust-lightning diff --git a/lightning-c-bindings/src/routing/network_graph.rs b/lightning-c-bindings/src/routing/network_graph.rs index 89f20fd4..b4c8de58 100644 --- a/lightning-c-bindings/src/routing/network_graph.rs +++ b/lightning-c-bindings/src/routing/network_graph.rs @@ -12,7 +12,7 @@ type nativeNetworkGraph = nativeNetworkGraphImport; #[must_use] #[repr(C)] pub struct NetworkGraph { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeNetworkGraph, pub is_owned: bool, @@ -52,7 +52,7 @@ type nativeLockedNetworkGraph = nativeLockedNetworkGraphImport<'static>; #[must_use] #[repr(C)] pub struct LockedNetworkGraph { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeLockedNetworkGraph, pub is_owned: bool, @@ -84,7 +84,7 @@ impl LockedNetworkGraph { } use lightning::routing::network_graph::NetGraphMsgHandler as nativeNetGraphMsgHandlerImport; -type nativeNetGraphMsgHandler = nativeNetGraphMsgHandlerImport; +type nativeNetGraphMsgHandler = nativeNetGraphMsgHandlerImport; /// Receives and validates network updates from peers, /// stores authentic and relevant data as a network graph. @@ -94,7 +94,7 @@ type nativeNetGraphMsgHandler = nativeNetGraphMsgHandlerImport NetGraphMsgHandler { - let mut ret = lightning::routing::network_graph::NetGraphMsgHandler::new(chain_monitor, logger); +pub extern "C" fn NetGraphMsgHandler_new(chain_access: *mut crate::chain::Access, mut logger: crate::util::logger::Logger) -> NetGraphMsgHandler { + let mut local_chain_access = if chain_access == std::ptr::null_mut() { None } else { Some( { unsafe { *Box::from_raw(chain_access) } }) }; + let mut ret = lightning::routing::network_graph::NetGraphMsgHandler::new(local_chain_access, logger); NetGraphMsgHandler { inner: Box::into_raw(Box::new(ret)), is_owned: true } } @@ -140,8 +141,9 @@ pub extern "C" fn NetGraphMsgHandler_new(mut chain_monitor: crate::chain::chaini /// assuming an existing Network Graph. #[must_use] #[no_mangle] -pub extern "C" fn NetGraphMsgHandler_from_net_graph(mut chain_monitor: crate::chain::chaininterface::ChainWatchInterface, mut logger: crate::util::logger::Logger, mut network_graph: crate::routing::network_graph::NetworkGraph) -> NetGraphMsgHandler { - let mut ret = lightning::routing::network_graph::NetGraphMsgHandler::from_net_graph(chain_monitor, logger, *unsafe { Box::from_raw(network_graph.take_ptr()) }); +pub extern "C" fn NetGraphMsgHandler_from_net_graph(chain_access: *mut crate::chain::Access, mut logger: crate::util::logger::Logger, mut network_graph: crate::routing::network_graph::NetworkGraph) -> NetGraphMsgHandler { + let mut local_chain_access = if chain_access == std::ptr::null_mut() { None } else { Some( { unsafe { *Box::from_raw(chain_access) } }) }; + let mut ret = lightning::routing::network_graph::NetGraphMsgHandler::from_net_graph(local_chain_access, logger, *unsafe { Box::from_raw(network_graph.take_ptr()) }); NetGraphMsgHandler { inner: Box::into_raw(Box::new(ret)), is_owned: true } } @@ -214,7 +216,7 @@ extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_get_next_node_announcemen local_ret.into() } #[must_use] -extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_should_request_full_sync(this_arg: *const c_void, _node_id: crate::c_types::PublicKey) -> bool { +extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_should_request_full_sync(this_arg: *const c_void, mut _node_id: crate::c_types::PublicKey) -> bool { let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.should_request_full_sync(&_node_id.into_rust()); ret } @@ -228,7 +230,7 @@ type nativeDirectionalChannelInfo = nativeDirectionalChannelInfoImport; #[must_use] #[repr(C)] pub struct DirectionalChannelInfo { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeDirectionalChannelInfo, pub is_owned: bool, @@ -344,7 +346,7 @@ type nativeChannelInfo = nativeChannelInfoImport; #[must_use] #[repr(C)] pub struct ChannelInfo { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeChannelInfo, pub is_owned: bool, @@ -374,6 +376,17 @@ impl ChannelInfo { ret } } +/// Protocol features of a channel communicated during its announcement +#[no_mangle] +pub extern "C" fn ChannelInfo_get_features(this_ptr: &ChannelInfo) -> crate::ln::features::ChannelFeatures { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.features; + crate::ln::features::ChannelFeatures { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } +} +/// Protocol features of a channel communicated during its announcement +#[no_mangle] +pub extern "C" fn ChannelInfo_set_features(this_ptr: &mut ChannelInfo, mut val: crate::ln::features::ChannelFeatures) { + unsafe { &mut *this_ptr.inner }.features = *unsafe { Box::from_raw(val.take_ptr()) }; +} /// Source node of the first direction of a channel #[no_mangle] pub extern "C" fn ChannelInfo_get_node_one(this_ptr: &ChannelInfo) -> crate::c_types::PublicKey { @@ -461,7 +474,7 @@ type nativeRoutingFees = nativeRoutingFeesImport; #[must_use] #[repr(C)] pub struct RoutingFees { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeRoutingFees, pub is_owned: bool, @@ -504,6 +517,10 @@ impl Clone for RoutingFees { pub(crate) extern "C" fn RoutingFees_clone_void(this_ptr: *const c_void) -> *mut c_void { Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoutingFees)).clone() })) as *mut c_void } +#[no_mangle] +pub extern "C" fn RoutingFees_clone(orig: &RoutingFees) -> RoutingFees { + RoutingFees { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true } +} /// Flat routing fee in satoshis #[no_mangle] pub extern "C" fn RoutingFees_get_base_msat(this_ptr: &RoutingFees) -> u32 { @@ -556,7 +573,7 @@ type nativeNodeAnnouncementInfo = nativeNodeAnnouncementInfoImport; #[must_use] #[repr(C)] pub struct NodeAnnouncementInfo { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeNodeAnnouncementInfo, pub is_owned: bool, @@ -586,6 +603,17 @@ impl NodeAnnouncementInfo { ret } } +/// Protocol features the node announced support for +#[no_mangle] +pub extern "C" fn NodeAnnouncementInfo_get_features(this_ptr: &NodeAnnouncementInfo) -> crate::ln::features::NodeFeatures { + let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.features; + crate::ln::features::NodeFeatures { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false } +} +/// Protocol features the node announced support for +#[no_mangle] +pub extern "C" fn NodeAnnouncementInfo_set_features(this_ptr: &mut NodeAnnouncementInfo, mut val: crate::ln::features::NodeFeatures) { + unsafe { &mut *this_ptr.inner }.features = *unsafe { Box::from_raw(val.take_ptr()) }; +} /// When the last known update to the node state was issued. /// Value is opaque, as set in the announcement. #[no_mangle] @@ -650,6 +678,20 @@ pub extern "C" fn NodeAnnouncementInfo_set_announcement_message(this_ptr: &mut N let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) }; unsafe { &mut *this_ptr.inner }.announcement_message = local_val; } +#[must_use] +#[no_mangle] +pub extern "C" fn NodeAnnouncementInfo_new(mut features_arg: crate::ln::features::NodeFeatures, mut last_update_arg: u32, mut rgb_arg: crate::c_types::ThreeBytes, mut alias_arg: crate::c_types::ThirtyTwoBytes, mut addresses_arg: crate::c_types::derived::CVec_NetAddressZ, mut announcement_message_arg: crate::ln::msgs::NodeAnnouncement) -> NodeAnnouncementInfo { + let mut local_addresses_arg = Vec::new(); for mut item in addresses_arg.into_rust().drain(..) { local_addresses_arg.push( { item.into_native() }); }; + let mut local_announcement_message_arg = if announcement_message_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(announcement_message_arg.take_ptr()) } }) }; + NodeAnnouncementInfo { inner: Box::into_raw(Box::new(nativeNodeAnnouncementInfo { + features: *unsafe { Box::from_raw(features_arg.take_ptr()) }, + last_update: last_update_arg, + rgb: rgb_arg.data, + alias: alias_arg.data, + addresses: local_addresses_arg, + announcement_message: local_announcement_message_arg, + })), is_owned: true } +} #[no_mangle] pub extern "C" fn NodeAnnouncementInfo_write(obj: *const NodeAnnouncementInfo) -> crate::c_types::derived::CVec_u8Z { crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) }) @@ -670,7 +712,7 @@ type nativeNodeInfo = nativeNodeInfoImport; #[must_use] #[repr(C)] pub struct NodeInfo { - /// Nearly everyhwere, inner must be non-null, however in places where + /// Nearly everywhere, inner must be non-null, however in places where /// the Rust equivalent takes an Option, it may be set to null to indicate None. pub inner: *mut nativeNodeInfo, pub is_owned: bool, @@ -784,6 +826,64 @@ pub extern "C" fn NetworkGraph_new() -> crate::routing::network_graph::NetworkGr crate::routing::network_graph::NetworkGraph { inner: Box::into_raw(Box::new(ret)), is_owned: true } } +/// For an already known node (from channel announcements), update its stored properties from a +/// given node announcement. +/// +/// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's +/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept +/// routing messages from a source using a protocol other than the lightning P2P protocol. +#[must_use] +#[no_mangle] +pub extern "C" fn NetworkGraph_update_node_from_announcement(this_arg: &mut NetworkGraph, msg: &crate::ln::msgs::NodeAnnouncement) -> crate::c_types::derived::CResult_NoneLightningErrorZ { + let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.update_node_from_announcement(unsafe { &*msg.inner }, &bitcoin::secp256k1::Secp256k1::new()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) }; + local_ret +} + +/// For an already known node (from channel announcements), update its stored properties from a +/// given node announcement without verifying the associated signatures. Because we aren't +/// given the associated signatures here we cannot relay the node announcement to any of our +/// peers. +#[must_use] +#[no_mangle] +pub extern "C" fn NetworkGraph_update_node_from_unsigned_announcement(this_arg: &mut NetworkGraph, msg: &crate::ln::msgs::UnsignedNodeAnnouncement) -> crate::c_types::derived::CResult_NoneLightningErrorZ { + let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.update_node_from_unsigned_announcement(unsafe { &*msg.inner }); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) }; + local_ret +} + +/// Store or update channel info from a channel announcement. +/// +/// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's +/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept +/// routing messages from a source using a protocol other than the lightning P2P protocol. +/// +/// If a `chain::Access` object is provided via `chain_access`, it will be called to verify +/// the corresponding UTXO exists on chain and is correctly-formatted. +#[must_use] +#[no_mangle] +pub extern "C" fn NetworkGraph_update_channel_from_announcement(this_arg: &mut NetworkGraph, msg: &crate::ln::msgs::ChannelAnnouncement, chain_access: *mut crate::chain::Access) -> crate::c_types::derived::CResult_NoneLightningErrorZ { + let mut local_chain_access = if chain_access == std::ptr::null_mut() { None } else { Some( { unsafe { *Box::from_raw(chain_access) } }) }; + let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.update_channel_from_announcement(unsafe { &*msg.inner }, &local_chain_access, &bitcoin::secp256k1::Secp256k1::new()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) }; + local_ret +} + +/// Store or update channel info from a channel announcement without verifying the associated +/// signatures. Because we aren't given the associated signatures here we cannot relay the +/// channel announcement to any of our peers. +/// +/// If a `chain::Access` object is provided via `chain_access`, it will be called to verify +/// the corresponding UTXO exists on chain and is correctly-formatted. +#[must_use] +#[no_mangle] +pub extern "C" fn NetworkGraph_update_channel_from_unsigned_announcement(this_arg: &mut NetworkGraph, msg: &crate::ln::msgs::UnsignedChannelAnnouncement, chain_access: *mut crate::chain::Access) -> crate::c_types::derived::CResult_NoneLightningErrorZ { + let mut local_chain_access = if chain_access == std::ptr::null_mut() { None } else { Some( { unsafe { *Box::from_raw(chain_access) } }) }; + let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.update_channel_from_unsigned_announcement(unsafe { &*msg.inner }, &local_chain_access); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) }; + local_ret +} + /// Close a channel if a corresponding HTLC fail was sent. /// If permanent, removes a channel from the local storage. /// May cause the removal of nodes too, if this was their last channel. @@ -793,3 +893,28 @@ pub extern "C" fn NetworkGraph_close_channel_from_update(this_arg: &mut NetworkG unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.close_channel_from_update(short_channel_id, is_permanent) } +/// For an already known (from announcement) channel, update info about one of the directions +/// of the channel. +/// +/// You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's +/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept +/// routing messages from a source using a protocol other than the lightning P2P protocol. +#[must_use] +#[no_mangle] +pub extern "C" fn NetworkGraph_update_channel(this_arg: &mut NetworkGraph, msg: &crate::ln::msgs::ChannelUpdate) -> crate::c_types::derived::CResult_NoneLightningErrorZ { + let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.update_channel(unsafe { &*msg.inner }, &bitcoin::secp256k1::Secp256k1::new()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) }; + local_ret +} + +/// For an already known (from announcement) channel, update info about one of the directions +/// of the channel without verifying the associated signatures. Because we aren't given the +/// associated signatures here we cannot relay the channel update to any of our peers. +#[must_use] +#[no_mangle] +pub extern "C" fn NetworkGraph_update_channel_unsigned(this_arg: &mut NetworkGraph, msg: &crate::ln::msgs::UnsignedChannelUpdate) -> crate::c_types::derived::CResult_NoneLightningErrorZ { + let mut ret = unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.update_channel_unsigned(unsafe { &*msg.inner }); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) }; + local_ret +} +