Skip to content
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

refactor: Unify Dispatch of Random and Defined Activity #160

Merged
merged 12 commits into from
Feb 2, 2024

Conversation

carlaKC
Copy link
Contributor

@carlaKC carlaKC commented Nov 17, 2023

This PR unifies dispatch of random and defined activities. I think that this is worthwhile doing:

  1. As a pathway to allowing users to specify both random and defined activity in future changes
  2. To reduce code duplication (functionality is similar in produce_events and produce_random_events)
  3. Possibly provide a different impl of things like sim-lib: Allows amount and interval ranges in manual activity definition #153 (though the existing one is quite simple so this is a weak motivation)

Opening up in draft for an early look (still needs some cleanup). This is my first foray into trait objects, so this PR only exists by the good graces of crust of rust / suggestions for more canonical rust ways of doing this are very welcome!

@carlaKC carlaKC requested review from sr-gi and okjodom November 21, 2023 14:03
@okjodom
Copy link
Collaborator

okjodom commented Nov 22, 2023

Concept ack ✅ And yes the Rust looks crusty!
Will take a closer look and test

@sr-gi
Copy link
Member

sr-gi commented Dec 4, 2023

I'll be reviewing the PR this week, sorry for taking so long

Copy link
Member

@sr-gi sr-gi left a comment

Choose a reason for hiding this comment

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

Approack ACK. There are some simplifications that we can discuss once the code is cleaned up.

There is only one comment I have regarding the approach. Check inline.

sim-lib/src/lib.rs Show resolved Hide resolved
sim-lib/src/lib.rs Outdated Show resolved Hide resolved
This trait is used to pick destinations for sending payments, so we
rename it to more accurately reflect this.
In preparation for different implementations of this trait, we
rename sample_node_by_capacity to a more general choose_destination
to accommodate different ways of picking destinations.
…ivity

In preparation for other types of payment activity generators, rename
random implementation to be more specific.
@carlaKC carlaKC force-pushed the refactor-dispatch branch 2 times, most recently from df00c4e to 3ac677c Compare January 19, 2024 15:28
@carlaKC carlaKC marked this pull request as ready for review January 19, 2024 15:29
@carlaKC
Copy link
Contributor Author

carlaKC commented Jan 19, 2024

Ready for another review with all the proposed name changed, apologies for the long turnaround!

@enigbe this has quite a few renames / refactors in it, so will be worthwhile for you to take a pass while you're getting familiar with the codebase.

@carlaKC carlaKC requested a review from sr-gi January 19, 2024 15:39
sim-lib/src/lib.rs Outdated Show resolved Hide resolved
sim-lib/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@okjodom okjodom left a comment

Choose a reason for hiding this comment

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

tACK 3ac677c

Left stylistic comments on trait declarations, otherwise the change looks good to me
Here are the suggested patches
dyn-traits-patch.txt

@carlaKC carlaKC force-pushed the refactor-dispatch branch 2 times, most recently from ceaeec0 to 9ff717b Compare January 24, 2024 17:08
@carlaKC
Copy link
Contributor Author

carlaKC commented Jan 24, 2024

dyn-traits-patch.txt

Whoop, just saw this now. Added a commit that does the same for LightningNode + Send!

Copy link
Collaborator

@okjodom okjodom left a comment

Choose a reason for hiding this comment

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

looks good!

@sr-gi
Copy link
Member

sr-gi commented Jan 24, 2024

Just dropping by to acknowledge that I owe a review here, will do it ASAP (hopefully this week)

Copy link
Member

@sr-gi sr-gi left a comment

Choose a reason for hiding this comment

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

LGTM, I left some comments but mostly about rustlang idiomatics, the logic looks sound.

.gitignore Outdated
/results
Copy link
Member

Choose a reason for hiding this comment

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

nit: remove empty trailing line

Comment on lines 206 to 223
pub trait DestinationGenerator: Send {
// choose_destination picks a destination node within the network, returning the node's information and its
// capacity (if available).
fn choose_destination(&self, source: PublicKey) -> (NodeInfo, Option<u64>);
}

#[derive(Debug, Error)]
#[error("Payment generation error: {0}")]
pub struct PaymentGenerationError(String);
pub trait PaymentGenerator {

pub trait PaymentGenerator: Display + Send {
// Returns the number of seconds that a node should wait until firing its next payment.
fn next_payment_wait(&self) -> time::Duration;

// Returns a payment amount based on the capacity of the sending and receiving node.
fn payment_amount(&self, destination_capacity: u64) -> Result<u64, PaymentGenerationError>;
// Returns a payment amount based, with a destination capacity optionally provided to inform the amount picked.
fn payment_amount(
&self,
destination_capacity: Option<u64>,
) -> Result<u64, PaymentGenerationError>;
Copy link
Member

Choose a reason for hiding this comment

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

Aren't all these supposed to be docs? (///)

Copy link
Contributor Author

@carlaKC carlaKC Jan 30, 2024

Choose a reason for hiding this comment

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

Codebase is a little all over the place, going to add a commit to fix all the // -> /// in one go.
70ca4ba

sim-lib/src/lib.rs Outdated Show resolved Hide resolved
sim-lib/src/random_activity.rs Outdated Show resolved Hide resolved
sim-lib/src/random_activity.rs Outdated Show resolved Hide resolved
sim-lib/src/defined_activity.rs Outdated Show resolved Hide resolved
impl DestinationGenerator for DefinedPaymentActivity {
fn choose_destination(
&self,
_source: bitcoin::secp256k1::PublicKey,
Copy link
Member

Choose a reason for hiding this comment

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

nit: this could be just _: bitcoin::secp256k1::PublicKey

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is just a _ idiomatic rust, out of interest?
(old golang habits die hard)

Copy link
Member

Choose a reason for hiding this comment

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

Ummm, not sure if I picked this up from rustlang or where if I'm honest

sim-lib/src/defined_activity.rs Outdated Show resolved Hide resolved
sim-lib/src/lib.rs Outdated Show resolved Hide resolved
In preparation for implementing our generation traits for defined
activities, we loosen the tie to destination capacity in our trait
definition (which is a characteristic of the random activity generation
implementation).
@carlaKC carlaKC force-pushed the refactor-dispatch branch 2 times, most recently from 9aab9e1 to 3e85c5a Compare January 30, 2024 14:34
@carlaKC carlaKC requested a review from sr-gi January 30, 2024 14:47
Copy link
Member

@sr-gi sr-gi left a comment

Choose a reason for hiding this comment

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

ACK 75ac437

@carlaKC carlaKC merged commit 69813aa into bitcoin-dev-project:main Feb 2, 2024
2 checks passed
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.

4 participants