From 302c1be86b5f066e54f7366d10737afd9d785b31 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 2 Dec 2021 14:57:57 +1100 Subject: [PATCH] Use identity more consistently --- daemon/src/connection.rs | 19 +++++++++++-------- daemon/src/model.rs | 4 ++++ daemon/src/taker.rs | 4 ++-- daemon/tests/happy_path.rs | 4 ++-- daemon/tests/harness/mod.rs | 15 +++++---------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/daemon/src/connection.rs b/daemon/src/connection.rs index 950fe1e..3680d95 100644 --- a/daemon/src/connection.rs +++ b/daemon/src/connection.rs @@ -1,6 +1,6 @@ use crate::address_map::{AddressMap, Stopping}; use crate::model::cfd::OrderId; -use crate::model::{Price, Timestamp, Usd}; +use crate::model::{Identity, Price, Timestamp, Usd}; use crate::tokio_ext::FutureExt; use crate::wire::EncryptedJsonCodec; use crate::{collab_settlement_taker, log_error, noise, send_to_socket, setup_taker, wire, Tasks}; @@ -41,7 +41,7 @@ pub struct Actor { } pub struct Connect { - pub maker_identity_pk: x25519_dalek::PublicKey, + pub maker_identity: Identity, pub maker_addr: SocketAddr, } @@ -168,7 +168,7 @@ impl Actor { &mut self, Connect { maker_addr, - maker_identity_pk, + maker_identity, }: Connect, ctx: &mut xtra::Context, ) -> Result<()> { @@ -186,9 +186,12 @@ impl Actor { ) })? .with_context(|| format!("Failed to connect to {}", maker_addr))?; - let noise = - noise::initiator_handshake(&mut connection, &self.identity_sk, &maker_identity_pk) - .await?; + let noise = noise::initiator_handshake( + &mut connection, + &self.identity_sk, + &maker_identity.pk(), + ) + .await?; let (read, write) = connection.into_split(); (read, write, Arc::new(Mutex::new(noise))) @@ -318,7 +321,7 @@ impl xtra::Actor for Actor {} pub async fn connect( mut maker_online_status_feed_receiver: watch::Receiver, connection_actor_addr: xtra::Address, - maker_identity_pk: x25519_dalek::PublicKey, + maker_identity: Identity, maker_addresses: Vec, ) { loop { @@ -327,7 +330,7 @@ pub async fn connect( 'connect: loop { for address in &maker_addresses { let connect_msg = Connect { - maker_identity_pk, + maker_identity, maker_addr: *address, }; diff --git a/daemon/src/model.rs b/daemon/src/model.rs index 26fa746..170116d 100644 --- a/daemon/src/model.rs +++ b/daemon/src/model.rs @@ -395,6 +395,10 @@ impl Identity { pub fn new(key: x25519_dalek::PublicKey) -> Self { Self(key) } + + pub fn pk(&self) -> x25519_dalek::PublicKey { + self.0 + } } impl Serialize for Identity { diff --git a/daemon/src/taker.rs b/daemon/src/taker.rs index 03b217a..9f93899 100644 --- a/daemon/src/taker.rs +++ b/daemon/src/taker.rs @@ -6,7 +6,7 @@ use clap::{Parser, Subcommand}; use daemon::connection::connect; use daemon::db::load_all_cfds; use daemon::model::cfd::Role; -use daemon::model::WalletInfo; +use daemon::model::{Identity, WalletInfo}; use daemon::seed::Seed; use daemon::tokio_ext::FutureExt; use daemon::{ @@ -274,7 +274,7 @@ async fn main() -> Result<()> { tasks.add(connect( maker_online_status_feed_receiver.clone(), connection_actor_addr, - opts.maker_id, + Identity::new(opts.maker_id), possible_addresses, )); diff --git a/daemon/tests/happy_path.rs b/daemon/tests/happy_path.rs index 2b01383..c80a29f 100644 --- a/daemon/tests/happy_path.rs +++ b/daemon/tests/happy_path.rs @@ -177,7 +177,7 @@ async fn taker_notices_lack_of_maker() { let taker_config = TakerConfig::default().with_heartbeat_timeout(short_interval * 2); - let mut taker = Taker::start(&taker_config, maker.listen_addr, maker.identity_pk).await; + let mut taker = Taker::start(&taker_config, maker.listen_addr, maker.identity).await; assert_eq!( ConnectionStatus::Online, @@ -237,7 +237,7 @@ async fn start_from_open_cfd_state() -> (Maker, Taker, OrderId) { let mut taker = Taker::start( &TakerConfig::default().with_heartbeat_timeout(heartbeat_interval * 2), maker.listen_addr, - maker.identity_pk, + maker.identity, ) .await; diff --git a/daemon/tests/harness/mod.rs b/daemon/tests/harness/mod.rs index ae8901b..d6c4eec 100644 --- a/daemon/tests/harness/mod.rs +++ b/daemon/tests/harness/mod.rs @@ -44,12 +44,7 @@ fn oracle_pk() -> schnorrsig::PublicKey { pub async fn start_both() -> (Maker, Taker) { let maker_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let maker = Maker::start(&MakerConfig::default(), maker_listener).await; - let taker = Taker::start( - &TakerConfig::default(), - maker.listen_addr, - maker.identity_pk, - ) - .await; + let taker = Taker::start(&TakerConfig::default(), maker.listen_addr, maker.identity).await; (maker, taker) } @@ -115,7 +110,7 @@ pub struct Maker { pub mocks: mocks::Mocks, pub feeds: Feeds, pub listen_addr: SocketAddr, - pub identity_pk: x25519_dalek::PublicKey, + pub identity: model::Identity, _tasks: Tasks, } @@ -195,7 +190,7 @@ impl Maker { Self { system: maker, feeds, - identity_pk, + identity: model::Identity::new(identity_pk), listen_addr: address, mocks, _tasks: tasks, @@ -266,7 +261,7 @@ impl Taker { pub async fn start( config: &TakerConfig, maker_address: SocketAddr, - maker_noise_pub_key: x25519_dalek::PublicKey, + maker_identity: model::Identity, ) -> Self { let (identity_pk, identity_sk) = config.seed.derive_identity(); @@ -306,7 +301,7 @@ impl Taker { tasks.add(connect( taker.maker_online_status_feed_receiver.clone(), taker.connection_actor_addr.clone(), - maker_noise_pub_key, + maker_identity, vec![maker_address], ));