From f18d01dfafe47d9f31297c670dbfafb30681dcd2 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Fri, 8 Jan 2021 13:27:56 +1100 Subject: [PATCH] Deterministic peer id from seed for bob --- swap/src/main.rs | 5 ++++- swap/src/protocol/bob.rs | 16 +++++++++++++++- swap/tests/testutils/mod.rs | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/swap/src/main.rs b/swap/src/main.rs index 7cf0a65c..17d72d2b 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -167,6 +167,7 @@ async fn main() -> Result<()> { db, alice_peer_id, alice_addr, + &seed, ) .await?; } @@ -243,6 +244,7 @@ async fn main() -> Result<()> { db, alice_peer_id, alice_addr, + &seed, ) .await?; } @@ -319,8 +321,9 @@ async fn bob_swap( db: Database, alice_peer_id: PeerId, alice_addr: Multiaddr, + seed: &Seed, ) -> Result { - let bob_behaviour = bob::Behaviour::default(); + let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed.bytes())); let bob_transport = build(bob_behaviour.identity())?; let (event_loop, handle) = diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index 28789d12..a0968ef5 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -12,7 +12,7 @@ use crate::{ network::{ peer_tracker::{self, PeerTracker}, transport::SwapTransport, - TokioExecutor, + Seed, TokioExecutor, }, protocol::{alice, bob}, SwapAmounts, @@ -124,6 +124,20 @@ pub struct Behaviour { } impl Behaviour { + pub fn new(seed: Seed) -> Self { + let identity = seed.derive_libp2p_identity(); + + Self { + pt: PeerTracker::default(), + amounts: Amounts::default(), + message0: message0::Behaviour::default(), + message1: message1::Behaviour::default(), + message2: message2::Behaviour::default(), + message3: message3::Behaviour::default(), + identity, + } + } + pub fn identity(&self) -> Keypair { self.identity.clone() } diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 8fff163d..d35f76b6 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -192,7 +192,8 @@ pub fn init_bob_event_loop( alice_peer_id: PeerId, alice_addr: Multiaddr, ) -> (bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle) { - let bob_behaviour = bob::Behaviour::default(); + let seed = Seed::random().unwrap(); + let bob_behaviour = bob::Behaviour::new(network::Seed::new(seed.bytes())); let bob_transport = build(bob_behaviour.identity()).unwrap(); bob::event_loop::EventLoop::new(bob_transport, bob_behaviour, alice_peer_id, alice_addr) .unwrap()