Skip to content

Commit 69813aa

Browse files
authored
Merge pull request #160 from carlaKC/refactor-dispatch
refactor: Unify Dispatch of Random and Defined Activity
2 parents 71d3aa3 + 75ac437 commit 69813aa

File tree

6 files changed

+299
-249
lines changed

6 files changed

+299
-249
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/target
22
node_modules
3-
config.json
4-
sim.json
5-
package-lock.json
3+
*.json
64
activity-generator/releases/*
75
.DS_Store
86
/results

sim-cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ async fn main() -> anyhow::Result<()> {
9292
serde_json::from_str(&std::fs::read_to_string(sim_path)?)
9393
.map_err(|e| anyhow!("Could not deserialize node connection data or activity description from simulation file (line {}, col {}).", e.line(), e.column()))?;
9494

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

9999
for connection in nodes {
100100
// TODO: Feels like there should be a better way of doing this without having to Arc<Mutex<T>>> it at this time.
101101
// Box sort of works, but we won't know the size of the dyn LightningNode at compile time so the compiler will
102102
// scream at us when trying to create the Arc<Mutex>> later on while adding the node to the clients map
103-
let node: Arc<Mutex<dyn LightningNode + Send>> = match connection {
103+
let node: Arc<Mutex<dyn LightningNode>> = match connection {
104104
NodeConnection::LND(c) => Arc::new(Mutex::new(LndNode::new(c).await?)),
105105
NodeConnection::CLN(c) => Arc::new(Mutex::new(ClnNode::new(c).await?)),
106106
};

sim-lib/src/defined_activity.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use crate::{DestinationGenerator, NodeInfo, PaymentGenerationError, PaymentGenerator};
2+
use std::fmt;
3+
use tokio::time::Duration;
4+
5+
#[derive(Clone)]
6+
pub struct DefinedPaymentActivity {
7+
destination: NodeInfo,
8+
wait: Duration,
9+
amount: u64,
10+
}
11+
12+
impl DefinedPaymentActivity {
13+
pub fn new(destination: NodeInfo, wait: Duration, amount: u64) -> Self {
14+
DefinedPaymentActivity {
15+
destination,
16+
wait,
17+
amount,
18+
}
19+
}
20+
}
21+
22+
impl fmt::Display for DefinedPaymentActivity {
23+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24+
write!(
25+
f,
26+
"static payment of {} to {} every {:?}",
27+
self.amount, self.destination, self.wait
28+
)
29+
}
30+
}
31+
32+
impl DestinationGenerator for DefinedPaymentActivity {
33+
fn choose_destination(&self, _: bitcoin::secp256k1::PublicKey) -> (NodeInfo, Option<u64>) {
34+
(self.destination.clone(), None)
35+
}
36+
}
37+
38+
impl PaymentGenerator for DefinedPaymentActivity {
39+
fn next_payment_wait(&self) -> Duration {
40+
self.wait
41+
}
42+
43+
fn payment_amount(
44+
&self,
45+
destination_capacity: Option<u64>,
46+
) -> Result<u64, crate::PaymentGenerationError> {
47+
if destination_capacity.is_some() {
48+
Err(PaymentGenerationError(
49+
"destination amount must not be set for defined activity generator".to_string(),
50+
))
51+
} else {
52+
Ok(self.amount)
53+
}
54+
}
55+
}
56+
57+
#[cfg(test)]
58+
mod tests {
59+
use super::DefinedPaymentActivity;
60+
use crate::test_utils::{create_nodes, get_random_keypair};
61+
use crate::{DestinationGenerator, PaymentGenerationError, PaymentGenerator};
62+
use std::time::Duration;
63+
64+
#[test]
65+
fn test_defined_activity_generator() {
66+
let node = create_nodes(1, 100000);
67+
let node = &node.first().unwrap().0;
68+
69+
let source = get_random_keypair();
70+
let payment_amt = 50;
71+
72+
let generator =
73+
DefinedPaymentActivity::new(node.clone(), Duration::from_secs(60), payment_amt);
74+
75+
let (dest, dest_capacity) = generator.choose_destination(source.1);
76+
assert_eq!(node.pubkey, dest.pubkey);
77+
assert!(dest_capacity.is_none());
78+
79+
assert_eq!(payment_amt, generator.payment_amount(None).unwrap());
80+
assert!(matches!(
81+
generator.payment_amount(Some(10)),
82+
Err(PaymentGenerationError(..))
83+
));
84+
}
85+
}

0 commit comments

Comments
 (0)