Skip to content

Commit

Permalink
refactor: rename reveal2 into reveal
Browse files Browse the repository at this point in the history
  • Loading branch information
h4sh3d committed Nov 28, 2022
1 parent cd17f13 commit f92024b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 58 deletions.
32 changes: 5 additions & 27 deletions src/bus/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ pub enum PeerMsg {
#[display("{0} taker commit")]
TakerCommit(TakerCommit),

//#[api(type = 33703)]
//#[display("reveal {0}")]
//Reveal(Reveal),
#[api(type = 33704)]
#[display("reveal {0}")]
Reveal2(Reveal2),
Reveal(Reveal),

#[api(type = 33720)]
#[display("refund procedure signatures")]
Expand Down Expand Up @@ -72,12 +69,7 @@ impl PeerMsg {
match self {
PeerMsg::MakerCommit(c) => c.swap_id(),
PeerMsg::TakerCommit(c) => c.swap_id(),
//PeerMsg::Reveal(m) => match m {
// Reveal::AliceParameters(n) => n.swap_id,
// Reveal::BobParameters(n) => n.swap_id,
// Reveal::Proof(n) => n.swap_id,
//},
PeerMsg::Reveal2(r) => r.swap_id(),
PeerMsg::Reveal(r) => r.swap_id(),
PeerMsg::RefundProcedureSignatures(RefundProcedureSignatures { swap_id, .. }) => {
*swap_id
}
Expand All @@ -101,8 +93,7 @@ impl PeerMsg {
self,
PeerMsg::MakerCommit(_)
| PeerMsg::TakerCommit(_)
//| PeerMsg::Reveal(_)
| PeerMsg::Reveal2(_)
| PeerMsg::Reveal(_)
| PeerMsg::RefundProcedureSignatures(_)
| PeerMsg::CoreArbitratingSetup(_)
| PeerMsg::BuyProcedureSignature(_)
Expand All @@ -116,29 +107,16 @@ impl PeerMsg {
self,
PeerMsg::MakerCommit(_)
| PeerMsg::TakerCommit(_)
//| PeerMsg::Reveal(_)
| PeerMsg::Reveal2(_)
| PeerMsg::Reveal(_)
| PeerMsg::RefundProcedureSignatures(_)
| PeerMsg::CoreArbitratingSetup(_)
| PeerMsg::BuyProcedureSignature(_)
)
}
}

/*
#[derive(Clone, Debug, Display, StrictEncode, StrictDecode)]
pub enum Reveal {
#[display("Alice parameters")]
AliceParameters(RevealAliceParameters),
#[display("Bob parameters")]
BobParameters(RevealBobParameters),
#[display("proof")]
Proof(RevealProof),
}
*/

#[derive(Clone, Debug, Display, StrictEncode, StrictDecode)]
pub enum Reveal2 {
#[display("Alice")]
Alice {
parameters: RevealAliceParameters,
Expand All @@ -152,7 +130,7 @@ pub enum Reveal2 {
},
}

impl Reveal2 {
impl Reveal {
pub fn swap_id(&self) -> SwapId {
match self {
Self::Alice { parameters, .. } => parameters.swap_id,
Expand Down
45 changes: 23 additions & 22 deletions src/swapd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
MoneroFundingInfo, Params, Tx,
},
bus::info::{InfoMsg, SwapInfo},
bus::p2p::{Commit, PeerMsg, /*Reveal,*/ Reveal2, TakerCommit},
bus::p2p::{Commit, PeerMsg, Reveal, TakerCommit},
bus::sync::SyncMsg,
bus::{BusMsg, Failure, FailureCode, Outcome, ServiceBus},
syncerd::{
Expand Down Expand Up @@ -663,14 +663,14 @@ impl Runtime {
//
// Message revealing the parameters and the zero-knowledge proof received by the wallet
// to trigger a state transition before forwarding it to counter-party.
PeerMsg::Reveal2(reveal)
PeerMsg::Reveal(reveal)
if source == ServiceId::Wallet
&& self.state.commit()
&& self.state.remote_commit().is_some() =>
{
// forward message to peer
debug!("{} | send reveal peer message to peerd", self.swap_id);
self.send_peer(endpoints, PeerMsg::Reveal2(reveal))?;
self.send_peer(endpoints, PeerMsg::Reveal(reveal))?;
// trigger state transition
debug!("{} | transition state", self.swap_id);
let next_state = self.state.clone().sup_commit_to_reveal();
Expand All @@ -681,19 +681,19 @@ impl Runtime {
// Message received by counter-party revealing they parameters
//
// Upon reception Alice needs to
// 1. Forward the reveal message to wallet
// 1. Validate the parameters
// 2. Forward the reveal message to wallet
//
// Upon reception Bob needs to
// 1. Add the reveal message to the pending message for later use
// 2. Send the funding information message to farcaster
// 3. Watch the arbitrating funding address if we are maker
PeerMsg::Reveal2(reveal) if self.state.commit() => {
// 1. Validate the parameters
// 2. Add the reveal message to the pending message for later use
// 3. Send the funding information message to farcaster
// 4. Watch the arbitrating funding address if we are maker
PeerMsg::Reveal(reveal) if self.state.commit() => {
let remote_commit = self.state.remote_commit().cloned().unwrap();
if let Ok(remote_params_candidate) =
remote_params_candidate2(&reveal, remote_commit)
{
debug!("{} | remote params validated", self.swap_id);
self.state.sup_remote_params(remote_params_candidate);
if let Ok(validated_params) = validate_reveal(&reveal, remote_commit) {
debug!("{} | remote params successfully validated", self.swap_id);
self.state.sup_remote_params(validated_params);
} else {
let msg = format!("{} | remote params validation failed", self.swap_id);
error!("{}", msg);
Expand All @@ -706,7 +706,7 @@ impl Runtime {
self.send_wallet(
ServiceBus::Msg,
endpoints,
BusMsg::P2p(PeerMsg::Reveal2(reveal)),
BusMsg::P2p(PeerMsg::Reveal(reveal)),
)?
}
SwapRole::Bob => {
Expand All @@ -725,7 +725,7 @@ impl Runtime {
self.identity(),
ServiceId::Wallet,
ServiceBus::Msg,
BusMsg::P2p(PeerMsg::Reveal2(reveal)),
BusMsg::P2p(PeerMsg::Reveal(reveal)),
),
);
// 2. send the funding information to farcasterd
Expand Down Expand Up @@ -755,7 +755,7 @@ impl Runtime {
source,
self.identity(),
ServiceBus::Msg,
BusMsg::P2p(PeerMsg::Reveal2(reveal.clone())),
BusMsg::P2p(PeerMsg::Reveal(reveal.clone())),
),
);
}
Expand Down Expand Up @@ -1265,7 +1265,7 @@ impl Runtime {
&PendingRequest {
dest: ServiceId::Wallet,
bus_id: ServiceBus::Msg,
request: BusMsg::P2p(PeerMsg::Reveal2(_)),
request: BusMsg::P2p(PeerMsg::Reveal(_)),
..
}
)
Expand Down Expand Up @@ -2571,7 +2571,7 @@ impl Runtime {
&i,
&PendingRequest {
bus_id: ServiceBus::Msg,
request: BusMsg::P2p(PeerMsg::Reveal2(_)),
request: BusMsg::P2p(PeerMsg::Reveal(_)),
dest: ServiceId::Swap(..),
..
}
Expand Down Expand Up @@ -2742,11 +2742,12 @@ fn aggregate_xmr_spend_view(
(alice_params.spend + bob_params.spend, alice_view + bob_view)
}

fn remote_params_candidate2(reveal: &Reveal2, remote_commit: Commit) -> Result<Params, Error> {
// parameter processing irrespective of maker & taker role
/// Parameter processing irrespective of maker & taker role. Return [`Params`] if the commit/reveal
/// matches.
fn validate_reveal(reveal: &Reveal, remote_commit: Commit) -> Result<Params, Error> {
let core_wallet = CommitmentEngine;
match reveal {
Reveal2::Alice { parameters, .. } => match &remote_commit {
Reveal::Alice { parameters, .. } => match &remote_commit {
Commit::AliceParameters(commit) => {
commit.verify_with_reveal(&core_wallet, parameters.clone())?;
Ok(Params::Alice(parameters.clone().into_parameters()))
Expand All @@ -2757,7 +2758,7 @@ fn remote_params_candidate2(reveal: &Reveal2, remote_commit: Commit) -> Result<P
Err(Error::Farcaster(err_msg.to_string()))
}
},
Reveal2::Bob { parameters, .. } => match &remote_commit {
Reveal::Bob { parameters, .. } => match &remote_commit {
Commit::BobParameters(commit) => {
commit.verify_with_reveal(&core_wallet, parameters.clone())?;
Ok(Params::Bob(parameters.clone().into_parameters()))
Expand Down
18 changes: 9 additions & 9 deletions src/walletd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::bus::{
self, Checkpoint, CheckpointState, CtlMsg, GetKeys, Keys, LaunchSwap, Params,
TakerCommitted, Token, Tx,
},
p2p::{Commit, PeerMsg, /*Reveal,*/ Reveal2, TakerCommit},
p2p::{Commit, PeerMsg, Reveal, TakerCommit},
AddressSecretKey, BusMsg, Outcome, ServiceBus,
};
use crate::databased::checkpoint_send;
Expand Down Expand Up @@ -203,14 +203,14 @@ impl Runtime {
}
// craft the correct reveal depending on role
let reveal = match self.wallets.get(&swap_id).unwrap() {
Wallet::Alice(AliceState { local_params, .. }) => Reveal2::Alice {
Wallet::Alice(AliceState { local_params, .. }) => Reveal::Alice {
parameters: local_params.clone().reveal_alice(swap_id),
proof: RevealProof {
swap_id,
proof: local_params.proof.clone().expect("local always some"),
},
},
Wallet::Bob(BobState { local_params, .. }) => Reveal2::Bob {
Wallet::Bob(BobState { local_params, .. }) => Reveal::Bob {
parameters: local_params.clone().reveal_bob(swap_id),
proof: RevealProof {
swap_id,
Expand All @@ -223,16 +223,16 @@ impl Runtime {
ServiceBus::Msg,
ServiceId::Wallet,
ServiceId::Swap(swap_id),
BusMsg::P2p(PeerMsg::Reveal2(reveal)),
BusMsg::P2p(PeerMsg::Reveal(reveal)),
)?;
}

// A message received from counterparty, forwarded by swap
PeerMsg::Reveal2(reveal) => {
PeerMsg::Reveal(reveal) => {
match reveal {
// receiving from counterparty Bob, thus wallet is acting as Alice (Maker or
// Taker)
Reveal2::Bob { parameters, proof } => {
Reveal::Bob { parameters, proof } => {
if let Some(Wallet::Alice(AliceState {
local_params,
key_manager,
Expand Down Expand Up @@ -271,7 +271,7 @@ impl Runtime {
ServiceBus::Msg,
ServiceId::Wallet,
ServiceId::Swap(swap_id),
BusMsg::P2p(PeerMsg::Reveal2(Reveal2::Alice {
BusMsg::P2p(PeerMsg::Reveal(Reveal::Alice {
parameters: local_params.clone().reveal_alice(swap_id),
proof: RevealProof {
swap_id,
Expand All @@ -292,7 +292,7 @@ impl Runtime {
}
// getting parameters from counterparty alice routed through
// swapd, thus I'm Bob on this swap: Bob can proceed
Reveal2::Alice { parameters, proof } => {
Reveal::Alice { parameters, proof } => {
if let Some(Wallet::Bob(BobState {
bob,
local_trade_role,
Expand Down Expand Up @@ -338,7 +338,7 @@ impl Runtime {
ServiceBus::Msg,
ServiceId::Wallet,
ServiceId::Swap(swap_id),
BusMsg::P2p(PeerMsg::Reveal2(Reveal2::Bob {
BusMsg::P2p(PeerMsg::Reveal(Reveal::Bob {
parameters: local_params.clone().reveal_bob(swap_id),
proof: RevealProof {
swap_id,
Expand Down

0 comments on commit f92024b

Please sign in to comment.