Browse Source

Rename `TakerId` to `Identity`

With #665, we will use this struct on both maker and taker side.
debug-collab-settlement
Thomas Eizinger 3 years ago
parent
commit
ffe3d1439b
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 46
      daemon/src/maker_cfd.rs
  2. 12
      daemon/src/maker_inc_connections.rs
  3. 12
      daemon/src/model.rs
  4. 4
      daemon/src/model/cfd.rs
  5. 8
      daemon/src/projection.rs
  6. 4
      daemon/src/to_sse_event.rs
  7. 4
      daemon/tests/happy_path.rs
  8. 8
      daemon/tests/harness/mod.rs

46
daemon/src/maker_cfd.rs

@ -4,7 +4,7 @@ use crate::model::cfd::{
Cfd, CfdState, CfdStateCommon, CollaborativeSettlement, Dlc, Order, OrderId, Origin, Role,
RollOverProposal, SettlementKind, SettlementProposal, UpdateCfdProposal,
};
use crate::model::{Price, TakerId, Timestamp, Usd};
use crate::model::{Identity, Price, Timestamp, Usd};
use crate::monitor::MonitorParams;
use crate::projection::Update;
use crate::setup_contract::{RolloverParams, SetupParams};
@ -55,11 +55,11 @@ pub struct NewOrder {
}
pub struct TakerConnected {
pub id: TakerId,
pub id: Identity,
}
pub struct TakerDisconnected {
pub id: TakerId,
pub id: Identity,
}
pub struct CfdSetupCompleted {
@ -73,7 +73,7 @@ pub struct CfdRollOverCompleted {
}
pub struct FromTaker {
pub taker_id: TakerId,
pub taker_id: Identity,
pub msg: wire::TakerToMaker,
}
@ -94,16 +94,16 @@ pub struct Actor<
setup_state: SetupState,
roll_over_state: RollOverState,
oracle_actor: Address<O>,
// Maker needs to also store TakerId to be able to send a reply back
current_pending_proposals: HashMap<OrderId, (UpdateCfdProposal, TakerId)>,
current_agreed_proposals: HashMap<OrderId, (SettlementProposal, TakerId)>,
connected_takers: HashSet<TakerId>,
// Maker needs to also store Identity to be able to send a reply back
current_pending_proposals: HashMap<OrderId, (UpdateCfdProposal, Identity)>,
current_agreed_proposals: HashMap<OrderId, (SettlementProposal, Identity)>,
connected_takers: HashSet<Identity>,
n_payouts: usize,
}
enum SetupState {
Active {
taker: TakerId,
taker: Identity,
sender: mpsc::UnboundedSender<wire::SetupMsg>,
_task: RemoteHandle<()>,
},
@ -112,7 +112,7 @@ enum SetupState {
enum RollOverState {
Active {
taker: TakerId,
taker: Identity,
sender: mpsc::UnboundedSender<wire::RollOverMsg>,
_task: RemoteHandle<()>,
},
@ -154,7 +154,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
async fn handle_propose_roll_over(
&mut self,
proposal: RollOverProposal,
taker_id: TakerId,
taker_id: Identity,
) -> Result<()> {
tracing::info!(
"Received proposal from the taker {}: {:?} to roll over order {}",
@ -193,7 +193,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
async fn handle_propose_settlement(
&mut self,
taker_id: TakerId,
taker_id: Identity,
proposal: SettlementProposal,
) -> Result<()> {
tracing::info!(
@ -217,7 +217,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
async fn handle_inc_protocol_msg(
&mut self,
taker_id: TakerId,
taker_id: Identity,
msg: wire::SetupMsg,
) -> Result<()> {
match &mut self.setup_state {
@ -237,7 +237,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
async fn handle_inc_roll_over_protocol_msg(
&mut self,
taker_id: TakerId,
taker_id: Identity,
msg: wire::RollOverMsg,
) -> Result<()> {
match &mut self.roll_over_state {
@ -254,7 +254,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
}
/// Send pending proposals for the purposes of UI updates.
/// Filters out the TakerIds, as they are an implementation detail inside of
/// Filters out the Identities, as they are an implementation detail inside of
/// the actor
async fn send_pending_proposals(&self) -> Result<()> {
let pending_proposal = self
@ -278,7 +278,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
Ok(())
}
fn get_taker_id_of_proposal(&self, order_id: &OrderId) -> Result<TakerId> {
fn get_taker_id_of_proposal(&self, order_id: &OrderId) -> Result<Identity> {
let (_, taker_id) = self
.current_pending_proposals
.get(order_id)
@ -286,7 +286,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
Ok(*taker_id)
}
fn get_settlement_proposal(&self, order_id: OrderId) -> Result<(SettlementProposal, TakerId)> {
fn get_settlement_proposal(&self, order_id: OrderId) -> Result<(SettlementProposal, Identity)> {
let (update_proposal, taker_id) = self
.current_pending_proposals
.get(&order_id)
@ -307,7 +307,7 @@ impl<O, M, T, W> Actor<O, M, T, W> {
self.connected_takers
.clone()
.into_iter()
.collect::<Vec<TakerId>>(),
.collect::<Vec<Identity>>(),
))
.await?;
Ok(())
@ -342,7 +342,7 @@ impl<O, M, T, W> Actor<O, M, T, W>
where
T: xtra::Handler<maker_inc_connections::TakerMessage>,
{
async fn handle_taker_connected(&mut self, taker_id: TakerId) -> Result<()> {
async fn handle_taker_connected(&mut self, taker_id: Identity) -> Result<()> {
let mut conn = self.db.acquire().await?;
let current_order = match self.current_order_id {
@ -367,7 +367,7 @@ where
Ok(())
}
async fn handle_taker_disconnected(&mut self, taker_id: TakerId) -> Result<()> {
async fn handle_taker_disconnected(&mut self, taker_id: Identity) -> Result<()> {
if !self.connected_takers.remove(&taker_id) {
tracing::warn!("Removed unknown taker: {:?}", &taker_id);
}
@ -377,7 +377,7 @@ where
async fn reject_order(
&mut self,
taker_id: TakerId,
taker_id: Identity,
mut cfd: Cfd,
mut conn: PoolConnection<Sqlite>,
) -> Result<()> {
@ -403,7 +403,7 @@ where
{
async fn handle_take_order(
&mut self,
taker_id: TakerId,
taker_id: Identity,
order_id: OrderId,
quantity: Usd,
) -> Result<()> {
@ -930,7 +930,7 @@ where
{
async fn handle_initiate_settlement(
&mut self,
taker_id: TakerId,
taker_id: Identity,
order_id: OrderId,
sig_taker: Signature,
) -> Result<()> {

12
daemon/src/maker_inc_connections.rs

@ -1,6 +1,6 @@
use crate::maker_cfd::{FromTaker, TakerConnected, TakerDisconnected};
use crate::model::cfd::Order;
use crate::model::TakerId;
use crate::model::Identity;
use crate::noise::TransportStateExt;
use crate::tokio_ext::FutureExt;
use crate::{forward_only_ok, maker_cfd, noise, send_to_socket, wire, Tasks};
@ -20,7 +20,7 @@ use xtra_productivity::xtra_productivity;
pub struct BroadcastOrder(pub Option<Order>);
pub struct TakerMessage {
pub taker_id: TakerId,
pub taker_id: Identity,
pub msg: wire::MakerToTaker,
}
@ -35,7 +35,7 @@ pub enum ListenerMessage {
}
pub struct Actor {
write_connections: HashMap<TakerId, Address<send_to_socket::Actor<wire::MakerToTaker>>>,
write_connections: HashMap<Identity, Address<send_to_socket::Actor<wire::MakerToTaker>>>,
taker_connected_channel: Box<dyn MessageChannel<TakerConnected>>,
taker_disconnected_channel: Box<dyn MessageChannel<TakerDisconnected>>,
taker_msg_channel: Box<dyn MessageChannel<FromTaker>>,
@ -65,7 +65,7 @@ impl Actor {
async fn send_to_taker(
&mut self,
taker_id: &TakerId,
taker_id: &Identity,
msg: wire::MakerToTaker,
) -> Result<(), NoConnection> {
let conn = self
@ -95,7 +95,7 @@ impl Actor {
_: &mut Context<Self>,
) -> Result<()> {
let transport_state = noise::responder_handshake(&mut stream, &self.noise_priv_key).await?;
let taker_id = TakerId::new(transport_state.get_remote_public_key()?);
let taker_id = Identity::new(transport_state.get_remote_public_key()?);
tracing::info!(%taker_id, address = %taker_address, "New taker connected");
@ -151,7 +151,7 @@ impl Actor {
#[derive(Debug, thiserror::Error)]
#[error("No connection to taker {0}")]
pub struct NoConnection(TakerId);
pub struct NoConnection(Identity);
#[xtra_productivity]
impl Actor {

12
daemon/src/model.rs

@ -389,15 +389,15 @@ pub enum Position {
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct TakerId(x25519_dalek::PublicKey);
pub struct Identity(x25519_dalek::PublicKey);
impl TakerId {
impl Identity {
pub fn new(key: x25519_dalek::PublicKey) -> Self {
Self(key)
}
}
impl Serialize for TakerId {
impl Serialize for Identity {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
@ -406,7 +406,7 @@ impl Serialize for TakerId {
}
}
impl<'de> Deserialize<'de> for TakerId {
impl<'de> Deserialize<'de> for Identity {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
@ -420,7 +420,7 @@ impl<'de> Deserialize<'de> for TakerId {
}
}
impl fmt::Display for TakerId {
impl fmt::Display for Identity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0.as_bytes()))
}
@ -665,7 +665,7 @@ mod tests {
#[test]
fn roundtrip_taker_id_serde() {
let id = TakerId::new(x25519_dalek::PublicKey::from([42u8; 32]));
let id = Identity::new(x25519_dalek::PublicKey::from([42u8; 32]));
serde_test::assert_tokens(
&id,

4
daemon/src/model/cfd.rs

@ -1,5 +1,5 @@
use crate::model::{
BitMexPriceEventId, InversePrice, Leverage, Percent, Position, Price, TakerId, Timestamp,
BitMexPriceEventId, Identity, InversePrice, Leverage, Percent, Position, Price, Timestamp,
TradingPair, Usd,
};
use crate::{monitor, oracle, payout_curve};
@ -193,7 +193,7 @@ pub enum CfdState {
/// This state applies to the maker only.
IncomingOrderRequest {
common: CfdStateCommon,
taker_id: TakerId,
taker_id: Identity,
},
/// The maker has accepted the CFD take request, but the contract is not set up on chain yet.

8
daemon/src/projection.rs

@ -1,7 +1,7 @@
use std::collections::HashMap;
use crate::model::cfd::OrderId;
use crate::model::{Leverage, Position, TakerId, Timestamp, TradingPair};
use crate::model::{Identity, Leverage, Position, Timestamp, TradingPair};
use crate::{bitmex_price_feed, model, Cfd, Order, UpdateCfdProposals};
use rust_decimal::Decimal;
use serde::Serialize;
@ -18,7 +18,7 @@ pub struct Feeds {
// TODO: Convert items below here into projections
pub cfds: watch::Receiver<Vec<Cfd>>,
pub settlements: watch::Receiver<UpdateCfdProposals>,
pub connected_takers: watch::Receiver<Vec<TakerId>>,
pub connected_takers: watch::Receiver<Vec<Identity>>,
}
impl Actor {
@ -58,7 +58,7 @@ struct Tx {
pub settlements: watch::Sender<UpdateCfdProposals>,
// TODO: Use this channel to communicate maker status as well with generic
// ID of connected counterparties
pub connected_takers: watch::Sender<Vec<TakerId>>,
pub connected_takers: watch::Sender<Vec<Identity>>,
}
pub struct Update<T>(pub T);
@ -77,7 +77,7 @@ impl Actor {
fn handle(&mut self, msg: Update<UpdateCfdProposals>) {
let _ = self.tx.settlements.send(msg.0);
}
fn handle(&mut self, msg: Update<Vec<TakerId>>) {
fn handle(&mut self, msg: Update<Vec<Identity>>) {
let _ = self.tx.connected_takers.send(msg.0);
}
}

4
daemon/src/to_sse_event.rs

@ -2,7 +2,7 @@ use crate::connection::ConnectionStatus;
use crate::model::cfd::{
Dlc, OrderId, Payout, Role, SettlementKind, UpdateCfdProposal, UpdateCfdProposals,
};
use crate::model::{Leverage, Position, TakerId, Timestamp, TradingPair};
use crate::model::{Identity, Leverage, Position, Timestamp, TradingPair};
use crate::projection::{CfdOrder, Price, Quote, Usd};
use crate::{bitmex_price_feed, model};
use bdk::bitcoin::{Amount, Network, SignedAmount, Txid};
@ -253,7 +253,7 @@ impl ToSseEvent for CfdsWithAuxData {
}
}
impl ToSseEvent for Vec<TakerId> {
impl ToSseEvent for Vec<Identity> {
fn to_sse_event(&self) -> Event {
let takers = self.iter().map(|x| x.to_string()).collect::<Vec<_>>();
Event::json(&takers).event("takers")

4
daemon/tests/happy_path.rs

@ -7,7 +7,7 @@ use crate::harness::{
};
use daemon::connection::ConnectionStatus;
use daemon::model::cfd::CfdState;
use daemon::model::{TakerId, Usd};
use daemon::model::{Identity, Usd};
use maia::secp256k1_zkp::schnorrsig;
use rust_decimal_macros::dec;
use tokio::time::sleep;
@ -177,7 +177,7 @@ async fn maker_notices_lack_of_taker() {
std::mem::drop(taker);
assert_eq!(
Vec::<TakerId>::new(),
Vec::<Identity>::new(),
next(maker.connected_takers_feed()).await.unwrap()
);
}

8
daemon/tests/harness/mod.rs

@ -5,7 +5,7 @@ use crate::schnorrsig;
use daemon::bitmex_price_feed::Quote;
use daemon::connection::{connect, ConnectionStatus};
use daemon::model::cfd::Cfd;
use daemon::model::{Price, TakerId, Timestamp, Usd};
use daemon::model::{Identity, Price, Timestamp, Usd};
use daemon::projection::{CfdOrder, Feeds};
use daemon::seed::Seed;
use daemon::{
@ -128,7 +128,7 @@ impl Maker {
&mut self.feeds.order
}
pub fn connected_takers_feed(&mut self) -> &mut watch::Receiver<Vec<TakerId>> {
pub fn connected_takers_feed(&mut self) -> &mut watch::Receiver<Vec<Identity>> {
&mut self.feeds.connected_takers
}
@ -239,7 +239,7 @@ impl Maker {
/// Taker Test Setup
pub struct Taker {
pub id: TakerId,
pub id: Identity,
pub system: daemon::TakerActorSystem<OracleActor, MonitorActor, WalletActor>,
pub mocks: mocks::Mocks,
pub feeds: Feeds,
@ -311,7 +311,7 @@ impl Taker {
));
Self {
id: TakerId::new(identity_pk),
id: Identity::new(identity_pk),
system: taker,
feeds,
mocks,

Loading…
Cancel
Save