From 664958939d430b7e36189e21c1c674cfff18ce67 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Fri, 8 Jan 2021 14:52:29 +1100 Subject: [PATCH] Create network::Seed from swap::Seed instead of abstracting over byte array --- swap/src/main.rs | 16 ++++++++-------- swap/src/network.rs | 6 +++--- swap/tests/happy_path.rs | 2 +- swap/tests/happy_path_restart_alice.rs | 4 ++-- swap/tests/happy_path_restart_bob_after_comm.rs | 2 +- swap/tests/happy_path_restart_bob_before_comm.rs | 2 +- swap/tests/punish.rs | 2 +- swap/tests/refund_restart_alice.rs | 4 ++-- swap/tests/testutils/mod.rs | 8 ++++---- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/swap/src/main.rs b/swap/src/main.rs index 17d72d2b..2d4a2f98 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -114,7 +114,7 @@ async fn main() -> Result<()> { monero_wallet, config, db, - &seed, + seed, ) .await?; } @@ -167,7 +167,7 @@ async fn main() -> Result<()> { db, alice_peer_id, alice_addr, - &seed, + seed, ) .await?; } @@ -211,7 +211,7 @@ async fn main() -> Result<()> { monero_wallet, config, db, - &seed, + seed, ) .await?; } @@ -244,7 +244,7 @@ async fn main() -> Result<()> { db, alice_peer_id, alice_addr, - &seed, + seed, ) .await?; } @@ -288,9 +288,9 @@ async fn alice_swap( monero_wallet: Arc, config: Config, db: Database, - seed: &Seed, + seed: Seed, ) -> Result { - let alice_behaviour = alice::Behaviour::new(network::Seed::new(seed.bytes())); + let alice_behaviour = alice::Behaviour::new(network::Seed::new(seed)); let alice_peer_id = alice_behaviour.peer_id(); info!("Own Peer-ID: {}", alice_peer_id); let alice_transport = build(alice_behaviour.identity())?; @@ -321,9 +321,9 @@ async fn bob_swap( db: Database, alice_peer_id: PeerId, alice_addr: Multiaddr, - seed: &Seed, + seed: Seed, ) -> Result { - let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed.bytes())); + let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed)); let bob_transport = build(bob_behaviour.identity())?; let (event_loop, handle) = diff --git a/swap/src/network.rs b/swap/src/network.rs index 0884bea4..0b0a70df 100644 --- a/swap/src/network.rs +++ b/swap/src/network.rs @@ -25,17 +25,17 @@ pub struct Seed([u8; SEED_LENGTH]); impl Seed { /// prefix "NETWORK" to the provided seed and apply sha256 - pub fn new(seed: [u8; crate::seed::SEED_LENGTH]) -> Self { + pub fn new(seed: crate::seed::Seed) -> Self { let mut engine = sha256::HashEngine::default(); - engine.input(&seed); + engine.input(&seed.bytes()); engine.input(b"NETWORK"); let hash = sha256::Hash::from_engine(engine); Self(hash.into_inner()) } - pub fn bytes(&self) -> [u8; SEED_LENGTH] { + fn bytes(&self) -> [u8; SEED_LENGTH] { self.0 } diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index 5eb25ef5..c1e12e0e 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -66,7 +66,7 @@ async fn happy_path() { xmr_alice, alice_multiaddr.clone(), config, - &Seed::random().unwrap(), + Seed::random().unwrap(), ) .await; diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index 84ef611a..5c615227 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -59,7 +59,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { alice_xmr_starting_balance, alice_multiaddr.clone(), config, - &alice_seed, + alice_seed, ) .await; @@ -128,7 +128,7 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { }; let (mut event_loop_after_restart, event_loop_handle_after_restart) = - testutils::init_alice_event_loop(alice_multiaddr, &alice_seed); + testutils::init_alice_event_loop(alice_multiaddr, alice_seed); tokio::spawn(async move { event_loop_after_restart.run().await }); let alice_state = alice::swap::swap( diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 09042086..3b83d4b9 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -58,7 +58,7 @@ async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { alice_xmr_starting_balance, alice_multiaddr.clone(), config, - &Seed::random().unwrap(), + Seed::random().unwrap(), ) .await; diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index c8fb93d0..a3c5216e 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -60,7 +60,7 @@ async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { alice_xmr_starting_balance, alice_multiaddr.clone(), Config::regtest(), - &Seed::random().unwrap(), + Seed::random().unwrap(), ) .await; diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 276f9c63..4628d45b 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -64,7 +64,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { alice_xmr_starting_balance, alice_multiaddr.clone(), config, - &Seed::random().unwrap(), + Seed::random().unwrap(), ) .await; diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index 4f2b3170..0f9a062f 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -64,7 +64,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { alice_xmr_starting_balance, alice_multiaddr.clone(), Config::regtest(), - &alice_seed, + alice_seed, ) .await; @@ -124,7 +124,7 @@ async fn given_alice_restarts_after_xmr_is_locked_abort_swap() { }; let (mut alice_event_loop_2, alice_event_loop_handle_2) = - testutils::init_alice_event_loop(alice_multiaddr, &alice_seed); + testutils::init_alice_event_loop(alice_multiaddr, alice_seed); let alice_final_state = { let alice_db = Database::open(alice_db_datadir.path()).unwrap(); diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index d35f76b6..db5d7ee2 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -107,12 +107,12 @@ pub async fn init_alice_state( pub fn init_alice_event_loop( listen: Multiaddr, - seed: &Seed, + seed: Seed, ) -> ( alice::event_loop::EventLoop, alice::event_loop::EventLoopHandle, ) { - let alice_behaviour = alice::Behaviour::new(network::Seed::new(seed.bytes())); + let alice_behaviour = alice::Behaviour::new(network::Seed::new(seed)); let alice_transport = build(alice_behaviour.identity()).unwrap(); alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap() } @@ -126,7 +126,7 @@ pub async fn init_alice( xmr_starting_balance: monero::Amount, listen: Multiaddr, config: Config, - seed: &Seed, + seed: Seed, ) -> ( AliceState, alice::event_loop::EventLoop, @@ -193,7 +193,7 @@ pub fn init_bob_event_loop( alice_addr: Multiaddr, ) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { let seed = Seed::random().unwrap(); - let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed.bytes())); + let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed)); let bob_transport = build(bob_behaviour.identity()).unwrap(); bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr) .unwrap()