Skip to content

Commit 84716ff

Browse files
authored
Merge pull request #758 from TheBlueMatt/2020-11-bindings-fix
Split `NetworkGraph` message handling fns into unsigned and signed (+update bindings)
2 parents fc7df54 + ccd4a7a commit 84716ff

File tree

7 files changed

+363
-203
lines changed

7 files changed

+363
-203
lines changed

.travis.yml

Lines changed: 0 additions & 48 deletions
This file was deleted.

fuzz/src/router.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use bitcoin::blockdata::script::Builder;
1111
use bitcoin::blockdata::transaction::TxOut;
1212
use bitcoin::hash_types::BlockHash;
1313

14-
use bitcoin::secp256k1;
15-
1614
use lightning::chain;
1715
use lightning::ln::channelmanager::ChannelDetails;
1816
use lightning::ln::features::InitFeatures;
@@ -120,7 +118,10 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
120118
($MsgType: path, $len: expr) => {{
121119
let mut reader = ::std::io::Cursor::new(get_slice!($len));
122120
match <$MsgType>::read(&mut reader) {
123-
Ok(msg) => msg,
121+
Ok(msg) => {
122+
assert_eq!(reader.position(), $len as u64);
123+
msg
124+
},
124125
Err(e) => match e {
125126
msgs::DecodeError::UnknownVersion => return,
126127
msgs::DecodeError::UnknownRequiredFeature => return,
@@ -134,10 +135,10 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
134135
}
135136

136137
macro_rules! decode_msg_with_len16 {
137-
($MsgType: path, $begin_len: expr, $excess: expr) => {
138+
($MsgType: path, $excess: expr) => {
138139
{
139-
let extra_len = slice_to_be16(&get_slice_nonadvancing!($begin_len as usize + 2)[$begin_len..$begin_len + 2]);
140-
decode_msg!($MsgType, $begin_len as usize + 2 + (extra_len as usize) + $excess)
140+
let extra_len = slice_to_be16(get_slice_nonadvancing!(2));
141+
decode_msg!($MsgType, 2 + (extra_len as usize) + $excess)
141142
}
142143
}
143144
}
@@ -162,30 +163,29 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
162163
loop {
163164
match get_slice!(1)[0] {
164165
0 => {
165-
let start_len = slice_to_be16(&get_slice_nonadvancing!(64 + 2)[64..64 + 2]) as usize;
166-
let addr_len = slice_to_be16(&get_slice_nonadvancing!(64+start_len+2 + 74)[64+start_len+2 + 72..64+start_len+2 + 74]);
166+
let start_len = slice_to_be16(&get_slice_nonadvancing!(2)[0..2]) as usize;
167+
let addr_len = slice_to_be16(&get_slice_nonadvancing!(start_len+2 + 74)[start_len+2 + 72..start_len+2 + 74]);
167168
if addr_len > (37+1)*4 {
168169
return;
169170
}
170-
let msg = decode_msg_with_len16!(msgs::NodeAnnouncement, 64, 288);
171-
node_pks.insert(msg.contents.node_id);
172-
let _ = net_graph.update_node_from_announcement::<secp256k1::VerifyOnly>(&msg, None);
171+
let msg = decode_msg_with_len16!(msgs::UnsignedNodeAnnouncement, 288);
172+
node_pks.insert(msg.node_id);
173+
let _ = net_graph.update_node_from_unsigned_announcement(&msg);
173174
},
174175
1 => {
175-
let msg = decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4);
176-
node_pks.insert(msg.contents.node_id_1);
177-
node_pks.insert(msg.contents.node_id_2);
178-
let _ = net_graph.update_channel_from_announcement::<secp256k1::VerifyOnly>(&msg, None, None);
176+
let msg = decode_msg_with_len16!(msgs::UnsignedChannelAnnouncement, 32+8+33*4);
177+
node_pks.insert(msg.node_id_1);
178+
node_pks.insert(msg.node_id_2);
179+
let _ = net_graph.update_channel_from_unsigned_announcement::<&FuzzChainSource>(&msg, &None);
179180
},
180181
2 => {
181-
let msg = decode_msg_with_len16!(msgs::ChannelAnnouncement, 64*4, 32+8+33*4);
182-
node_pks.insert(msg.contents.node_id_1);
183-
node_pks.insert(msg.contents.node_id_2);
184-
let val = slice_to_be64(get_slice!(8));
185-
let _ = net_graph.update_channel_from_announcement::<secp256k1::VerifyOnly>(&msg, Some(val), None);
182+
let msg = decode_msg_with_len16!(msgs::UnsignedChannelAnnouncement, 32+8+33*4);
183+
node_pks.insert(msg.node_id_1);
184+
node_pks.insert(msg.node_id_2);
185+
let _ = net_graph.update_channel_from_unsigned_announcement(&msg, &Some(&FuzzChainSource { input: Arc::clone(&input) }));
186186
},
187187
3 => {
188-
let _ = net_graph.update_channel(&decode_msg!(msgs::ChannelUpdate, 136), None);
188+
let _ = net_graph.update_channel_unsigned(&decode_msg!(msgs::UnsignedChannelUpdate, 72));
189189
},
190190
4 => {
191191
let short_channel_id = slice_to_be64(get_slice!(8));

lightning-c-bindings/include/lightning.h

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,32 @@ typedef struct LDKC2TupleTempl_HTLCOutputInCommitment__Signature {
586586

587587
typedef LDKC2TupleTempl_HTLCOutputInCommitment__Signature LDKC2Tuple_HTLCOutputInCommitmentSignatureZ;
588588

589+
590+
591+
/**
592+
* An Err type for failure to process messages.
593+
*/
594+
typedef struct MUST_USE_STRUCT LDKLightningError {
595+
/**
596+
* Nearly everywhere, inner must be non-null, however in places where
597+
* the Rust equivalent takes an Option, it may be set to null to indicate None.
598+
*/
599+
LDKnativeLightningError *inner;
600+
bool is_owned;
601+
} LDKLightningError;
602+
603+
typedef union LDKCResultPtr_u8__LightningError {
604+
uint8_t *result;
605+
LDKLightningError *err;
606+
} LDKCResultPtr_u8__LightningError;
607+
608+
typedef struct LDKCResultTempl_u8__LightningError {
609+
LDKCResultPtr_u8__LightningError contents;
610+
bool result_ok;
611+
} LDKCResultTempl_u8__LightningError;
612+
613+
typedef LDKCResultTempl_u8__LightningError LDKCResult_NoneLightningErrorZ;
614+
589615
typedef struct LDKPublicKey {
590616
uint8_t compressed_form[33];
591617
} LDKPublicKey;
@@ -2641,20 +2667,6 @@ typedef struct MUST_USE_STRUCT LDKGossipTimestampFilter {
26412667
bool is_owned;
26422668
} LDKGossipTimestampFilter;
26432669

2644-
2645-
2646-
/**
2647-
* An Err type for failure to process messages.
2648-
*/
2649-
typedef struct MUST_USE_STRUCT LDKLightningError {
2650-
/**
2651-
* Nearly everywhere, inner must be non-null, however in places where
2652-
* the Rust equivalent takes an Option, it may be set to null to indicate None.
2653-
*/
2654-
LDKnativeLightningError *inner;
2655-
bool is_owned;
2656-
} LDKLightningError;
2657-
26582670
typedef struct LDKCVecTempl_UpdateAddHTLC {
26592671
LDKUpdateAddHTLC *data;
26602672
uintptr_t datalen;
@@ -3153,6 +3165,10 @@ extern const LDKCResult_NoneChannelMonitorUpdateErrZ (*CResult_NoneChannelMonito
31533165

31543166
extern const void (*CResult_NoneChannelMonitorUpdateErrZ_free)(LDKCResult_NoneChannelMonitorUpdateErrZ);
31553167

3168+
extern const LDKCResult_NoneLightningErrorZ (*CResult_NoneLightningErrorZ_err)(LDKLightningError);
3169+
3170+
extern const void (*CResult_NoneLightningErrorZ_free)(LDKCResult_NoneLightningErrorZ);
3171+
31563172
extern const LDKCResult_NoneMonitorUpdateErrorZ (*CResult_NoneMonitorUpdateErrorZ_err)(LDKMonitorUpdateError);
31573173

31583174
extern const void (*CResult_NoneMonitorUpdateErrorZ_free)(LDKCResult_NoneMonitorUpdateErrorZ);
@@ -3301,6 +3317,8 @@ LDKCResult_NonePeerHandleErrorZ CResult_NonePeerHandleErrorZ_ok(void);
33013317

33023318
LDKC2Tuple_HTLCOutputInCommitmentSignatureZ C2Tuple_HTLCOutputInCommitmentSignatureZ_new(LDKHTLCOutputInCommitment a, LDKSignature b);
33033319

3320+
LDKCResult_NoneLightningErrorZ CResult_NoneLightningErrorZ_ok(void);
3321+
33043322
void Event_free(LDKEvent this_ptr);
33053323

33063324
LDKEvent Event_clone(const LDKEvent *orig);
@@ -7262,6 +7280,46 @@ LDKNetworkGraph NetworkGraph_read(LDKu8slice ser);
72627280
*/
72637281
MUST_USE_RES LDKNetworkGraph NetworkGraph_new(void);
72647282

7283+
/**
7284+
* For an already known node (from channel announcements), update its stored properties from a
7285+
* given node announcement.
7286+
*
7287+
* You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
7288+
* RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
7289+
* routing messages from a source using a protocol other than the lightning P2P protocol.
7290+
*/
7291+
MUST_USE_RES LDKCResult_NoneLightningErrorZ NetworkGraph_update_node_from_announcement(LDKNetworkGraph *this_arg, const LDKNodeAnnouncement *msg);
7292+
7293+
/**
7294+
* For an already known node (from channel announcements), update its stored properties from a
7295+
* given node announcement without verifying the associated signatures. Because we aren't
7296+
* given the associated signatures here we cannot relay the node announcement to any of our
7297+
* peers.
7298+
*/
7299+
MUST_USE_RES LDKCResult_NoneLightningErrorZ NetworkGraph_update_node_from_unsigned_announcement(LDKNetworkGraph *this_arg, const LDKUnsignedNodeAnnouncement *msg);
7300+
7301+
/**
7302+
* Store or update channel info from a channel announcement.
7303+
*
7304+
* You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
7305+
* RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
7306+
* routing messages from a source using a protocol other than the lightning P2P protocol.
7307+
*
7308+
* If a `chain::Access` object is provided via `chain_access`, it will be called to verify
7309+
* the corresponding UTXO exists on chain and is correctly-formatted.
7310+
*/
7311+
MUST_USE_RES LDKCResult_NoneLightningErrorZ NetworkGraph_update_channel_from_announcement(LDKNetworkGraph *this_arg, const LDKChannelAnnouncement *msg, LDKAccess *chain_access);
7312+
7313+
/**
7314+
* Store or update channel info from a channel announcement without verifying the associated
7315+
* signatures. Because we aren't given the associated signatures here we cannot relay the
7316+
* channel announcement to any of our peers.
7317+
*
7318+
* If a `chain::Access` object is provided via `chain_access`, it will be called to verify
7319+
* the corresponding UTXO exists on chain and is correctly-formatted.
7320+
*/
7321+
MUST_USE_RES LDKCResult_NoneLightningErrorZ NetworkGraph_update_channel_from_unsigned_announcement(LDKNetworkGraph *this_arg, const LDKUnsignedChannelAnnouncement *msg, LDKAccess *chain_access);
7322+
72657323
/**
72667324
* Close a channel if a corresponding HTLC fail was sent.
72677325
* If permanent, removes a channel from the local storage.
@@ -7270,4 +7328,21 @@ MUST_USE_RES LDKNetworkGraph NetworkGraph_new(void);
72707328
*/
72717329
void NetworkGraph_close_channel_from_update(LDKNetworkGraph *this_arg, uint64_t short_channel_id, bool is_permanent);
72727330

7331+
/**
7332+
* For an already known (from announcement) channel, update info about one of the directions
7333+
* of the channel.
7334+
*
7335+
* You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
7336+
* RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
7337+
* routing messages from a source using a protocol other than the lightning P2P protocol.
7338+
*/
7339+
MUST_USE_RES LDKCResult_NoneLightningErrorZ NetworkGraph_update_channel(LDKNetworkGraph *this_arg, const LDKChannelUpdate *msg);
7340+
7341+
/**
7342+
* For an already known (from announcement) channel, update info about one of the directions
7343+
* of the channel without verifying the associated signatures. Because we aren't given the
7344+
* associated signatures here we cannot relay the channel update to any of our peers.
7345+
*/
7346+
MUST_USE_RES LDKCResult_NoneLightningErrorZ NetworkGraph_update_channel_unsigned(LDKNetworkGraph *this_arg, const LDKUnsignedChannelUpdate *msg);
7347+
72737348
/* Text to put at the end of the generated file */

lightning-c-bindings/include/lightningpp.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,20 @@ class CResult_RouteLightningErrorZ {
18301830
const LDKCResult_RouteLightningErrorZ* operator &() const { return &self; }
18311831
const LDKCResult_RouteLightningErrorZ* operator ->() const { return &self; }
18321832
};
1833+
class CResult_NoneLightningErrorZ {
1834+
private:
1835+
LDKCResult_NoneLightningErrorZ self;
1836+
public:
1837+
CResult_NoneLightningErrorZ(const CResult_NoneLightningErrorZ&) = delete;
1838+
~CResult_NoneLightningErrorZ() { CResult_NoneLightningErrorZ_free(self); }
1839+
CResult_NoneLightningErrorZ(CResult_NoneLightningErrorZ&& o) : self(o.self) { memset(&o, 0, sizeof(CResult_NoneLightningErrorZ)); }
1840+
CResult_NoneLightningErrorZ(LDKCResult_NoneLightningErrorZ&& m_self) : self(m_self) { memset(&m_self, 0, sizeof(LDKCResult_NoneLightningErrorZ)); }
1841+
operator LDKCResult_NoneLightningErrorZ() { LDKCResult_NoneLightningErrorZ res = self; memset(&self, 0, sizeof(LDKCResult_NoneLightningErrorZ)); return res; }
1842+
LDKCResult_NoneLightningErrorZ* operator &() { return &self; }
1843+
LDKCResult_NoneLightningErrorZ* operator ->() { return &self; }
1844+
const LDKCResult_NoneLightningErrorZ* operator &() const { return &self; }
1845+
const LDKCResult_NoneLightningErrorZ* operator ->() const { return &self; }
1846+
};
18331847
class CResult_CVec_SignatureZNoneZ {
18341848
private:
18351849
LDKCResult_CVec_SignatureZNoneZ self;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,16 @@ pub static CResult_RouteLightningErrorZ_ok: extern "C" fn (crate::routing::route
400400
pub static CResult_RouteLightningErrorZ_err: extern "C" fn (crate::ln::msgs::LightningError) -> CResult_RouteLightningErrorZ =
401401
crate::c_types::CResultTempl::<crate::routing::router::Route, crate::ln::msgs::LightningError>::err;
402402

403+
#[no_mangle]
404+
pub type CResult_NoneLightningErrorZ = crate::c_types::CResultTempl<u8, crate::ln::msgs::LightningError>;
405+
#[no_mangle]
406+
pub static CResult_NoneLightningErrorZ_free: extern "C" fn(CResult_NoneLightningErrorZ) = crate::c_types::CResultTempl_free::<u8, crate::ln::msgs::LightningError>;
407+
#[no_mangle]
408+
pub extern "C" fn CResult_NoneLightningErrorZ_ok() -> CResult_NoneLightningErrorZ {
409+
crate::c_types::CResultTempl::ok(0)
410+
}
411+
412+
#[no_mangle]
413+
pub static CResult_NoneLightningErrorZ_err: extern "C" fn (crate::ln::msgs::LightningError) -> CResult_NoneLightningErrorZ =
414+
crate::c_types::CResultTempl::<u8, crate::ln::msgs::LightningError>::err;
415+

0 commit comments

Comments
 (0)