Skip to content

Pre-C bindings cleanups (2) #638

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

Merged

Conversation

TheBlueMatt
Copy link
Collaborator

A second batch of assorted code cleanups that make C binding generation a bit easier. See individual commits for details, but they're all pretty straight forward.

@TheBlueMatt TheBlueMatt force-pushed the 2020-06-c-bindings-cleanups-2 branch 3 times, most recently from a85467f to b006acb Compare June 11, 2020 20:05
@codecov
Copy link

codecov bot commented Jun 11, 2020

Codecov Report

Merging #638 into master will decrease coverage by 0.07%.
The diff coverage is 89.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #638      +/-   ##
==========================================
- Coverage   91.30%   91.23%   -0.08%     
==========================================
  Files          35       35              
  Lines       21104    21114      +10     
==========================================
- Hits        19270    19263       -7     
- Misses       1834     1851      +17     
Impacted Files Coverage Δ
lightning/src/ln/msgs.rs 90.03% <ø> (ø)
lightning/src/util/ser.rs 87.45% <ø> (ø)
lightning/src/util/test_utils.rs 85.20% <0.00%> (ø)
lightning/src/routing/network_graph.rs 91.06% <38.09%> (-0.56%) ⬇️
lightning/src/ln/channel.rs 86.76% <75.00%> (-0.03%) ⬇️
lightning/src/chain/chaininterface.rs 91.93% <100.00%> (+0.04%) ⬆️
lightning/src/chain/keysinterface.rs 94.78% <100.00%> (ø)
lightning/src/ln/chanmon_update_fail_tests.rs 97.45% <100.00%> (ø)
lightning/src/ln/channelmanager.rs 85.45% <100.00%> (ø)
lightning/src/ln/channelmonitor.rs 95.70% <100.00%> (+<0.01%) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 70fca07...5c37023. Read the comment docs.

@jkczyz jkczyz self-requested a review June 12, 2020 18:16
Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes related to get_funding_txo in 0acdc55 belong in 897a1ab.

Comment on lines +238 to +239
{
let funding_txo = monitor.get_funding_txo();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this scope necessary because of the borrow here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for 1.22, but not for newer versions.

let mut matched_index = Vec::new();
{
let watched = self.watched.lock().unwrap();
for (index, transaction) in block.txdata.iter().enumerate() {
if self.does_match_tx_unguarded(transaction, &watched) {
matched.push(transaction);
matched_index.push(index as u32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this as usize and return Vec<usize> since you are just casting it back at the call site.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did so in a new commit since it had a bunch of followon effects.

@@ -257,7 +257,7 @@ fn test_update_fee_unordered_raa() {
// ...but before it's delivered, nodes[1] starts to send a payment back to nodes[0]...
let (_, our_payment_hash) = get_payment_preimage_hash!(nodes[0]);
let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
nodes[1].node.send_payment(&get_route(&nodes[1].node.get_our_node_id(), net_graph_msg_handler, &nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV, &logger).unwrap(), our_payment_hash, &None).unwrap();
nodes[1].node.send_payment(&get_route(&nodes[1].node.get_our_node_id(), &*net_graph_msg_handler.network_graph.read().unwrap(), &nodes[0].node.get_our_node_id(), None, &Vec::new(), 40000, TEST_FINAL_CLTV, &logger).unwrap(), our_payment_hash, &None).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dereference is unnecessary here and throughout.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types are different - without the ref+deref its a MutexGuard, with the ref+deref you get the NetworkGraph itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to explicitly dereference a MutexGuard. See how it is working correctly in fuzz/src/full_stack.rs without the dereference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, not sure if that is the same case, but it does compile here if you remove the dereference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'd tried previously without either the & or *, which obviously doesn't work, but you're right, works with only the &.

@TheBlueMatt TheBlueMatt force-pushed the 2020-06-c-bindings-cleanups-2 branch from 912d57a to ef57682 Compare June 13, 2020 18:12
@jkczyz
Copy link
Contributor

jkczyz commented Jun 13, 2020

The changes related to get_funding_txo in 0acdc55 belong in 897a1ab.

Not sure if you saw my previous top-level comment (quoted here) but otherwise looks good.

@TheBlueMatt TheBlueMatt force-pushed the 2020-06-c-bindings-cleanups-2 branch from ef57682 to 020ce15 Compare June 13, 2020 21:34
@TheBlueMatt
Copy link
Collaborator Author

I did not. Good catch, fixed.

@jkczyz
Copy link
Contributor

jkczyz commented Jun 14, 2020

Looks like a few places in the fuzz code need to be updated for 020ce15.

@TheBlueMatt TheBlueMatt force-pushed the 2020-06-c-bindings-cleanups-2 branch from 795ae9c to 7741f38 Compare June 16, 2020 19:56
Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partial review

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just some nits

@@ -3339,6 +3339,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
channel_id: self.channel_id(),
data: "funding tx had wrong script/value".to_owned()
});
} else if txo_idx > 0xff_ff {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these checks be tested? or a TODO, or issue to test them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, oops, duh, funding_txo.index is a u16, so this line is unreachable.

... instead of only the txid.

This is another instance of it not being possible to fully
re-implement SimpleManyChannelMonitor using only public methods. In
this case you couldn't properly register outpoints for monitoring
so that the funding transaction would be matched.
In general, we don't need an explicit lifetime when doing something
like:
fn get_thing(&self) -> &Thing { &self.thing }.

This also makes it easier to reason about what's going on in the
bindings generation.
This isn't a big difference in the API, but it avoids needing to
wrap a given NetworkGraph in a RwLock before passing it, which
makes it much easier to generate C bindings for.
This is more consistent with the way we use std::cmp over the
codebase and avoids `use std`, which is only actually needed to
support older rustcs, so feels a bit awkward.
@TheBlueMatt TheBlueMatt force-pushed the 2020-06-c-bindings-cleanups-2 branch from 7741f38 to 8b4c5a3 Compare June 23, 2020 04:09
non-mut references to primitives are only excess overhead, so
there's not much reason to ever have them. As a nice bonus, it also
is one less thing to worry about when generating C bindings
Instead of making the filter_block fn in the ChainWatchInterface
trait return both a list of indexes of transaction positions within
the block and references to the transactions themselves, return
only the list of indexes and then build the reference list at the
callsite.

While this may be slightly less effecient from a memory locality
perspective, it shouldn't be materially different.

This should make it more practical to generate bindings for
filter_block as it no longer needs to reference Rust Transaction
objects that are contained in a Rust Block object (which we'd
otherwise just pass over the FFI in fully-serialized form).
This was just an oversight when route calculation was split up into
parts - it makes no sense for get_route to require that we have a
full route message handler, only a network graph (which can always
be accessed from a NetGraphMsgHandler anyway).
We use them largely as indexes into a Vec<Transaction> so there's
little reason for them to be u32s. Instead, use them as usize
everywhere.

We also take this opportunity to add range checks before
short_channel_id calculation, as we could otherwise end up with a
bogus short_channel_id due to an output index out of range.
@TheBlueMatt
Copy link
Collaborator Author

Squashed.

@TheBlueMatt TheBlueMatt force-pushed the 2020-06-c-bindings-cleanups-2 branch from 8b4c5a3 to 5c37023 Compare June 23, 2020 20:13
Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, updates LGTM

@TheBlueMatt TheBlueMatt merged commit 8fae0c0 into lightningdevkit:master Jun 24, 2020
@jkczyz jkczyz mentioned this pull request Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants