Browse Source

Use identity more consistently

feature/reconnect-button
Daniel Karzel 3 years ago
parent
commit
302c1be86b
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 19
      daemon/src/connection.rs
  2. 4
      daemon/src/model.rs
  3. 4
      daemon/src/taker.rs
  4. 4
      daemon/tests/happy_path.rs
  5. 15
      daemon/tests/harness/mod.rs

19
daemon/src/connection.rs

@ -1,6 +1,6 @@
use crate::address_map::{AddressMap, Stopping}; use crate::address_map::{AddressMap, Stopping};
use crate::model::cfd::OrderId; 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::tokio_ext::FutureExt;
use crate::wire::EncryptedJsonCodec; use crate::wire::EncryptedJsonCodec;
use crate::{collab_settlement_taker, log_error, noise, send_to_socket, setup_taker, wire, Tasks}; 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 struct Connect {
pub maker_identity_pk: x25519_dalek::PublicKey, pub maker_identity: Identity,
pub maker_addr: SocketAddr, pub maker_addr: SocketAddr,
} }
@ -168,7 +168,7 @@ impl Actor {
&mut self, &mut self,
Connect { Connect {
maker_addr, maker_addr,
maker_identity_pk, maker_identity,
}: Connect, }: Connect,
ctx: &mut xtra::Context<Self>, ctx: &mut xtra::Context<Self>,
) -> Result<()> { ) -> Result<()> {
@ -186,9 +186,12 @@ impl Actor {
) )
})? })?
.with_context(|| format!("Failed to connect to {}", maker_addr))?; .with_context(|| format!("Failed to connect to {}", maker_addr))?;
let noise = let noise = noise::initiator_handshake(
noise::initiator_handshake(&mut connection, &self.identity_sk, &maker_identity_pk) &mut connection,
.await?; &self.identity_sk,
&maker_identity.pk(),
)
.await?;
let (read, write) = connection.into_split(); let (read, write) = connection.into_split();
(read, write, Arc::new(Mutex::new(noise))) (read, write, Arc::new(Mutex::new(noise)))
@ -318,7 +321,7 @@ impl xtra::Actor for Actor {}
pub async fn connect( pub async fn connect(
mut maker_online_status_feed_receiver: watch::Receiver<ConnectionStatus>, mut maker_online_status_feed_receiver: watch::Receiver<ConnectionStatus>,
connection_actor_addr: xtra::Address<Actor>, connection_actor_addr: xtra::Address<Actor>,
maker_identity_pk: x25519_dalek::PublicKey, maker_identity: Identity,
maker_addresses: Vec<SocketAddr>, maker_addresses: Vec<SocketAddr>,
) { ) {
loop { loop {
@ -327,7 +330,7 @@ pub async fn connect(
'connect: loop { 'connect: loop {
for address in &maker_addresses { for address in &maker_addresses {
let connect_msg = Connect { let connect_msg = Connect {
maker_identity_pk, maker_identity,
maker_addr: *address, maker_addr: *address,
}; };

4
daemon/src/model.rs

@ -395,6 +395,10 @@ impl Identity {
pub fn new(key: x25519_dalek::PublicKey) -> Self { pub fn new(key: x25519_dalek::PublicKey) -> Self {
Self(key) Self(key)
} }
pub fn pk(&self) -> x25519_dalek::PublicKey {
self.0
}
} }
impl Serialize for Identity { impl Serialize for Identity {

4
daemon/src/taker.rs

@ -6,7 +6,7 @@ use clap::{Parser, Subcommand};
use daemon::connection::connect; use daemon::connection::connect;
use daemon::db::load_all_cfds; use daemon::db::load_all_cfds;
use daemon::model::cfd::Role; use daemon::model::cfd::Role;
use daemon::model::WalletInfo; use daemon::model::{Identity, WalletInfo};
use daemon::seed::Seed; use daemon::seed::Seed;
use daemon::tokio_ext::FutureExt; use daemon::tokio_ext::FutureExt;
use daemon::{ use daemon::{
@ -274,7 +274,7 @@ async fn main() -> Result<()> {
tasks.add(connect( tasks.add(connect(
maker_online_status_feed_receiver.clone(), maker_online_status_feed_receiver.clone(),
connection_actor_addr, connection_actor_addr,
opts.maker_id, Identity::new(opts.maker_id),
possible_addresses, possible_addresses,
)); ));

4
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 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!( assert_eq!(
ConnectionStatus::Online, ConnectionStatus::Online,
@ -237,7 +237,7 @@ async fn start_from_open_cfd_state() -> (Maker, Taker, OrderId) {
let mut taker = Taker::start( let mut taker = Taker::start(
&TakerConfig::default().with_heartbeat_timeout(heartbeat_interval * 2), &TakerConfig::default().with_heartbeat_timeout(heartbeat_interval * 2),
maker.listen_addr, maker.listen_addr,
maker.identity_pk, maker.identity,
) )
.await; .await;

15
daemon/tests/harness/mod.rs

@ -44,12 +44,7 @@ fn oracle_pk() -> schnorrsig::PublicKey {
pub async fn start_both() -> (Maker, Taker) { pub async fn start_both() -> (Maker, Taker) {
let maker_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let maker_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let maker = Maker::start(&MakerConfig::default(), maker_listener).await; let maker = Maker::start(&MakerConfig::default(), maker_listener).await;
let taker = Taker::start( let taker = Taker::start(&TakerConfig::default(), maker.listen_addr, maker.identity).await;
&TakerConfig::default(),
maker.listen_addr,
maker.identity_pk,
)
.await;
(maker, taker) (maker, taker)
} }
@ -115,7 +110,7 @@ pub struct Maker {
pub mocks: mocks::Mocks, pub mocks: mocks::Mocks,
pub feeds: Feeds, pub feeds: Feeds,
pub listen_addr: SocketAddr, pub listen_addr: SocketAddr,
pub identity_pk: x25519_dalek::PublicKey, pub identity: model::Identity,
_tasks: Tasks, _tasks: Tasks,
} }
@ -195,7 +190,7 @@ impl Maker {
Self { Self {
system: maker, system: maker,
feeds, feeds,
identity_pk, identity: model::Identity::new(identity_pk),
listen_addr: address, listen_addr: address,
mocks, mocks,
_tasks: tasks, _tasks: tasks,
@ -266,7 +261,7 @@ impl Taker {
pub async fn start( pub async fn start(
config: &TakerConfig, config: &TakerConfig,
maker_address: SocketAddr, maker_address: SocketAddr,
maker_noise_pub_key: x25519_dalek::PublicKey, maker_identity: model::Identity,
) -> Self { ) -> Self {
let (identity_pk, identity_sk) = config.seed.derive_identity(); let (identity_pk, identity_sk) = config.seed.derive_identity();
@ -306,7 +301,7 @@ impl Taker {
tasks.add(connect( tasks.add(connect(
taker.maker_online_status_feed_receiver.clone(), taker.maker_online_status_feed_receiver.clone(),
taker.connection_actor_addr.clone(), taker.connection_actor_addr.clone(),
maker_noise_pub_key, maker_identity,
vec![maker_address], vec![maker_address],
)); ));

Loading…
Cancel
Save