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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/target
node_modules
config.json
sim.json
package-lock.json
*.json
activity-generator/releases/*
.DS_Store
/results
4 changes: 2 additions & 2 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ async fn main() -> anyhow::Result<()> {
serde_json::from_str(&std::fs::read_to_string(sim_path)?)
.map_err(|e| anyhow!("Could not deserialize node connection data or activity description from simulation file (line {}, col {}).", e.line(), e.column()))?;

let mut clients: HashMap<PublicKey, Arc<Mutex<dyn LightningNode + Send>>> = HashMap::new();
let mut clients: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>> = HashMap::new();
let mut pk_node_map = HashMap::new();
let mut alias_node_map = HashMap::new();

for connection in nodes {
// TODO: Feels like there should be a better way of doing this without having to Arc<Mutex<T>>> it at this time.
// Box sort of works, but we won't know the size of the dyn LightningNode at compile time so the compiler will
// scream at us when trying to create the Arc<Mutex>> later on while adding the node to the clients map
let node: Arc<Mutex<dyn LightningNode + Send>> = match connection {
let node: Arc<Mutex<dyn LightningNode>> = match connection {
NodeConnection::LND(c) => Arc::new(Mutex::new(LndNode::new(c).await?)),
NodeConnection::CLN(c) => Arc::new(Mutex::new(ClnNode::new(c).await?)),
};
Expand Down
85 changes: 85 additions & 0 deletions sim-lib/src/defined_activity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::{DestinationGenerator, NodeInfo, PaymentGenerationError, PaymentGenerator};
use std::fmt;
use tokio::time::Duration;

#[derive(Clone)]
pub struct DefinedPaymentActivity {
destination: NodeInfo,
wait: Duration,
amount: u64,
}

impl DefinedPaymentActivity {
pub fn new(destination: NodeInfo, wait: Duration, amount: u64) -> Self {
DefinedPaymentActivity {
destination,
wait,
amount,
}
}
}

impl fmt::Display for DefinedPaymentActivity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"static payment of {} to {} every {:?}",
self.amount, self.destination, self.wait
)
}
}

impl DestinationGenerator for DefinedPaymentActivity {
fn choose_destination(&self, _: bitcoin::secp256k1::PublicKey) -> (NodeInfo, Option<u64>) {
(self.destination.clone(), None)
}
}

impl PaymentGenerator for DefinedPaymentActivity {
fn next_payment_wait(&self) -> Duration {
self.wait
}

fn payment_amount(
&self,
destination_capacity: Option<u64>,
) -> Result<u64, crate::PaymentGenerationError> {
if destination_capacity.is_some() {
Err(PaymentGenerationError(
"destination amount must not be set for defined activity generator".to_string(),
))
} else {
Ok(self.amount)
}
}
}

#[cfg(test)]
mod tests {
use super::DefinedPaymentActivity;
use crate::test_utils::{create_nodes, get_random_keypair};
use crate::{DestinationGenerator, PaymentGenerationError, PaymentGenerator};
use std::time::Duration;

#[test]
fn test_defined_activity_generator() {
let node = create_nodes(1, 100000);
let node = &node.first().unwrap().0;

let source = get_random_keypair();
let payment_amt = 50;

let generator =
DefinedPaymentActivity::new(node.clone(), Duration::from_secs(60), payment_amt);

let (dest, dest_capacity) = generator.choose_destination(source.1);
assert_eq!(node.pubkey, dest.pubkey);
assert!(dest_capacity.is_none());

assert_eq!(payment_amt, generator.payment_amount(None).unwrap());
assert!(matches!(
generator.payment_amount(Some(10)),
Err(PaymentGenerationError(..))
));
}
}
Loading
Loading