Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into abortAbortFix
Browse files Browse the repository at this point in the history
  • Loading branch information
h4sh3d committed Dec 28, 2022
2 parents 4684fd5 + 624bc1b commit e3dbf4a
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 125 deletions.
4 changes: 2 additions & 2 deletions src/swapd/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::swapd::temporal_safety::SWEEP_MONERO_THRESHOLD;
use crate::swapd::Opts;
use crate::syncerd::bitcoin_syncer::p2wpkh_signed_tx_fee;
use crate::syncerd::types::{Event, TransactionConfirmations};
use crate::syncerd::{Abort, Boolean, Task, TaskTarget};
use crate::syncerd::{Abort, Task, TaskTarget};
use crate::{
bus::ctl::{BitcoinFundingInfo, Checkpoint, CtlMsg, FundingInfo},
bus::info::{InfoMsg, SwapInfo},
Expand Down Expand Up @@ -830,7 +830,7 @@ impl Runtime {
pub fn abort_all_syncer_tasks(&mut self, endpoints: &mut Endpoints) -> Result<(), Error> {
let abort_all = Task::Abort(Abort {
task_target: TaskTarget::AllTasks,
respond: Boolean::False,
respond: false,
});

endpoints.send_to(
Expand Down
33 changes: 6 additions & 27 deletions src/swapd/swap_key_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ impl_strict_encoding!(WrappedEncryptedSignature);
#[display("Alice's Swap Key Manager")]
pub struct AliceSwapKeyManager {
pub alice: Alice,
pub local_trade_role: TradeRole,
pub local_params: Parameters,
pub key_manager: KeyManager,
pub target_bitcoin_address: bitcoin::Address,
Expand All @@ -97,7 +96,6 @@ pub struct AliceSwapKeyManager {
impl Encodable for AliceSwapKeyManager {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
let mut len = self.alice.consensus_encode(writer)?;
len += self.local_trade_role.consensus_encode(writer)?;
len += self.local_params.consensus_encode(writer)?;
len += self.key_manager.consensus_encode(writer)?;
len += self
Expand All @@ -116,7 +114,6 @@ impl Decodable for AliceSwapKeyManager {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(AliceSwapKeyManager {
alice: Decodable::consensus_decode(d)?,
local_trade_role: Decodable::consensus_decode(d)?,
local_params: Decodable::consensus_decode(d)?,
key_manager: Decodable::consensus_decode(d)?,
target_bitcoin_address: bitcoin::Address::from_canonical_bytes(
Expand All @@ -134,15 +131,13 @@ impl_strict_encoding!(AliceSwapKeyManager);
impl AliceSwapKeyManager {
pub fn new(
alice: Alice,
local_trade_role: TradeRole,
local_params: Parameters,
key_manager: KeyManager,
target_bitcoin_address: bitcoin::Address,
target_monero_address: monero::Address,
) -> Self {
Self {
alice,
local_trade_role,
local_params,
key_manager,
target_bitcoin_address,
Expand All @@ -155,7 +150,6 @@ impl AliceSwapKeyManager {
#[display("Bob's Swap Key Manager")]
pub struct BobSwapKeyManager {
pub bob: Bob,
pub local_trade_role: TradeRole,
pub local_params: Parameters,
pub key_manager: KeyManager,
pub funding_tx: FundingTx,
Expand All @@ -166,7 +160,6 @@ pub struct BobSwapKeyManager {
impl Encodable for BobSwapKeyManager {
fn consensus_encode<W: io::Write>(&self, writer: &mut W) -> Result<usize, io::Error> {
let mut len = self.bob.consensus_encode(writer)?;
len += self.local_trade_role.consensus_encode(writer)?;
len += self.local_params.consensus_encode(writer)?;
len += self.key_manager.consensus_encode(writer)?;
len += self.funding_tx.consensus_encode(writer)?;
Expand All @@ -186,7 +179,6 @@ impl Decodable for BobSwapKeyManager {
fn consensus_decode<D: io::Read>(d: &mut D) -> Result<Self, consensus::Error> {
Ok(BobSwapKeyManager {
bob: Decodable::consensus_decode(d)?,
local_trade_role: Decodable::consensus_decode(d)?,
local_params: Decodable::consensus_decode(d)?,
key_manager: Decodable::consensus_decode(d)?,
funding_tx: Decodable::consensus_decode(d)?,
Expand All @@ -203,7 +195,6 @@ impl Decodable for BobSwapKeyManager {
impl BobSwapKeyManager {
pub fn new(
bob: Bob,
local_trade_role: TradeRole,
local_params: Parameters,
key_manager: KeyManager,
funding_tx: FundingTx,
Expand All @@ -212,7 +203,6 @@ impl BobSwapKeyManager {
) -> Self {
Self {
bob,
local_trade_role,
local_params,
key_manager,
funding_tx,
Expand Down Expand Up @@ -245,7 +235,6 @@ impl AliceSwapKeyManager {
let local_params = alice.generate_parameters(&mut key_manager, &runtime.deal)?;
Ok(AliceSwapKeyManager::new(
alice,
TradeRole::Taker,
local_params,
key_manager,
// None,
Expand All @@ -255,7 +244,7 @@ impl AliceSwapKeyManager {
}

pub fn taker_commit(
&mut self,
&self,
event: &mut Event,
runtime: &mut Runtime,
) -> Result<CommitAliceParameters, Error> {
Expand Down Expand Up @@ -291,10 +280,8 @@ impl AliceSwapKeyManager {
);
let local_params = alice.generate_parameters(&mut key_manager, &runtime.deal)?;
runtime.log_info(format!("Loading {}", "Alice Swap Key Manager".label()));
let local_trade_role = TradeRole::Maker;
Ok(AliceSwapKeyManager::new(
alice,
local_trade_role,
local_params,
key_manager,
target_bitcoin_address,
Expand All @@ -303,7 +290,7 @@ impl AliceSwapKeyManager {
}

pub fn maker_commit(
&mut self,
&self,
event: &mut Event,
runtime: &mut Runtime,
) -> Result<CommitAliceParameters, Error> {
Expand Down Expand Up @@ -372,10 +359,7 @@ impl AliceSwapKeyManager {
// Nothing to do yet, waiting for Msg CoreArbitratingSetup to proceed
}

pub fn create_reveal_from_local_params(
&mut self,
runtime: &mut Runtime,
) -> Result<Reveal, Error> {
pub fn create_reveal_from_local_params(&self, runtime: &mut Runtime) -> Result<Reveal, Error> {
let AliceSwapKeyManager { local_params, .. } = self;
runtime.log_debug("Generating reveal alice message");
Ok(Reveal::Alice {
Expand Down Expand Up @@ -671,7 +655,6 @@ impl BobSwapKeyManager {
runtime.log_info(format!("Loading {}", "Bob's Swap Key Manager".label()));
Ok(BobSwapKeyManager::new(
bob,
TradeRole::Taker,
local_params,
key_manager,
funding,
Expand All @@ -681,7 +664,7 @@ impl BobSwapKeyManager {
}

pub fn taker_commit(
&mut self,
&self,
event: &mut Event,
runtime: &mut Runtime,
) -> Result<CommitBobParameters, Error> {
Expand Down Expand Up @@ -736,7 +719,6 @@ impl BobSwapKeyManager {
runtime.log_info(format!("Loading {}", "Bob's Swap Key Manager".label()));
Ok(BobSwapKeyManager::new(
bob,
TradeRole::Maker,
local_params,
key_manager,
funding,
Expand All @@ -746,7 +728,7 @@ impl BobSwapKeyManager {
}

pub fn maker_commit(
&mut self,
&self,
event: &mut Event,
runtime: &mut Runtime,
) -> Result<CommitBobParameters, Error> {
Expand All @@ -769,10 +751,7 @@ impl BobSwapKeyManager {
Ok(local_params.commit_bob(runtime.swap_id, &engine))
}

pub fn create_reveal_from_local_params(
&mut self,
runtime: &mut Runtime,
) -> Result<Reveal, Error> {
pub fn create_reveal_from_local_params(&self, runtime: &mut Runtime) -> Result<Reveal, Error> {
let BobSwapKeyManager { local_params, .. } = self;
// craft the correct reveal depending on role
Ok(Reveal::Bob {
Expand Down
42 changes: 9 additions & 33 deletions src/swapd/swap_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ use microservices::esb::Handler;
use monero::ViewPair;
use strict_encoding::{StrictDecode, StrictEncode};

use crate::{
bus::ctl::MoneroFundingInfo,
syncerd::{Abort, AddressTransaction, Boolean, TaskTarget},
};
use crate::{bus::ctl::MoneroFundingInfo, syncerd::AddressTransaction};
use crate::{bus::p2p::Reveal, swapd::temporal_safety::SWEEP_MONERO_THRESHOLD};
use crate::{
bus::{
Expand Down Expand Up @@ -130,7 +127,7 @@ use super::{
/// |
/// V
/// SwapEnd
///
///
/// ```
#[derive(Debug, Display, Clone, StrictDecode, StrictEncode)]
Expand Down Expand Up @@ -633,7 +630,7 @@ fn attempt_transition_to_init_taker(

match swap_role {
SwapRole::Bob => {
let mut swap_key_manager = BobSwapKeyManager::new_taker_bob(
let swap_key_manager = BobSwapKeyManager::new_taker_bob(
&mut event,
runtime,
target_bitcoin_address.clone(),
Expand Down Expand Up @@ -665,7 +662,7 @@ fn attempt_transition_to_init_taker(
})))
}
SwapRole::Alice => {
let mut swap_key_manager = AliceSwapKeyManager::new_taker_alice(
let swap_key_manager = AliceSwapKeyManager::new_taker_alice(
runtime,
target_bitcoin_address.clone(),
target_monero_address,
Expand Down Expand Up @@ -741,7 +738,7 @@ fn attempt_transition_to_init_maker(
"Local swap role is Bob, but received Bob remote commit".to_string(),
));
};
let mut swap_key_manager = BobSwapKeyManager::new_maker_bob(
let swap_key_manager = BobSwapKeyManager::new_maker_bob(
&mut event,
runtime,
target_bitcoin_address,
Expand Down Expand Up @@ -781,7 +778,7 @@ fn attempt_transition_to_init_maker(
"Local swap role is Bob, but received Bob remote commit".to_string(),
));
};
let mut swap_key_manager = AliceSwapKeyManager::new_maker_alice(
let swap_key_manager = AliceSwapKeyManager::new_maker_alice(
runtime,
target_bitcoin_address,
target_monero_address,
Expand Down Expand Up @@ -823,9 +820,7 @@ fn try_bob_init_taker_to_bob_taker_maker_commit(
runtime: &mut Runtime,
bob_init_taker: BobInitTaker,
) -> Result<Option<SwapStateMachine>, Error> {
let BobInitTaker {
mut swap_key_manager,
} = bob_init_taker;
let BobInitTaker { swap_key_manager } = bob_init_taker;
match event.request.clone() {
BusMsg::P2p(PeerMsg::DealNotFound(_)) => {
runtime.log_error(format!(
Expand Down Expand Up @@ -856,11 +851,9 @@ fn try_bob_init_taker_to_bob_taker_maker_commit(
fn try_alice_init_taker_to_alice_taker_maker_commit(
event: Event,
runtime: &mut Runtime,
bob_init_taker: AliceInitTaker,
alice_init_taker: AliceInitTaker,
) -> Result<Option<SwapStateMachine>, Error> {
let AliceInitTaker {
mut swap_key_manager,
} = bob_init_taker;
let AliceInitTaker { swap_key_manager } = alice_init_taker;
match event.request {
BusMsg::P2p(PeerMsg::DealNotFound(_)) => {
runtime.log_error(format!(
Expand Down Expand Up @@ -2016,23 +2009,6 @@ fn try_alice_canceled_to_alice_refund_or_alice_punish(
CtlMsg::FundingCompleted(Blockchain::Monero),
)?;
}
let abort_all = Task::Abort(Abort {
task_target: TaskTarget::AllTasks,
respond: Boolean::False,
});
event.send_sync_service(
runtime.syncer_state.monero_syncer(),
SyncMsg::Task(abort_all.clone()),
)?;
event.send_sync_service(
runtime.syncer_state.bitcoin_syncer(),
SyncMsg::Task(abort_all),
)?;
// remove txs to invalidate outdated states
runtime.txs.remove(&TxLabel::Cancel);
runtime.txs.remove(&TxLabel::Refund);
runtime.txs.remove(&TxLabel::Buy);
runtime.txs.remove(&TxLabel::Punish);
Ok(Some(SwapStateMachine::SwapEnd(Outcome::FailureRefund)))
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/swapd/syncer_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
bus::ServiceBus,
service::{Endpoints, LogStyle},
syncerd::{
Abort, AddressAddendum, Boolean, BroadcastTransaction, BtcAddressAddendum, GetTx,
SweepAddress, SweepAddressAddendum, SweepBitcoinAddress, SweepMoneroAddress, TaskTarget,
Abort, AddressAddendum, BroadcastTransaction, BtcAddressAddendum, GetTx, SweepAddress,
SweepAddressAddendum, SweepBitcoinAddress, SweepMoneroAddress, TaskTarget,
TransactionBroadcasted, TxFilter, Txid, WatchAddress, WatchEstimateFee, WatchHeight,
WatchTransaction, XmrAddressAddendum,
},
Expand Down Expand Up @@ -97,7 +97,7 @@ impl SyncerState {
pub fn abort_task(&mut self, id: TaskId) -> Task {
Task::Abort(Abort {
task_target: TaskTarget::TaskId(id),
respond: Boolean::False,
respond: false,
})
}

Expand Down Expand Up @@ -200,7 +200,7 @@ impl SyncerState {
id,
lifetime: self.task_lifetime(Blockchain::Bitcoin),
addendum: AddressAddendum::Bitcoin(addendum),
include_tx: Boolean::True,
include_tx: true,
filter,
});
self.tasks.tasks.insert(id, task.clone());
Expand Down Expand Up @@ -258,7 +258,7 @@ impl SyncerState {
id,
lifetime: self.task_lifetime(Blockchain::Monero),
addendum: AddressAddendum::Monero(addendum),
include_tx: Boolean::False,
include_tx: false,
filter: TxFilter::Incoming,
};
let task = Task::WatchAddress(watch_addr);
Expand Down
8 changes: 2 additions & 6 deletions src/syncerd/bitcoin_syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::syncerd::runtime::SyncerdTask;
use crate::syncerd::runtime::Synclet;
use crate::syncerd::syncer_state::{AddressTx, BalanceServiceIdPair, TransactionServiceIdPair};
use crate::syncerd::syncer_state::{GetTxServiceIdPair, SyncerState};
use crate::syncerd::types::{AddressAddendum, Boolean, SweepAddressAddendum, Task};
use crate::syncerd::types::{AddressAddendum, SweepAddressAddendum, Task};
use crate::syncerd::BtcAddressAddendum;
use crate::syncerd::Event;
use crate::syncerd::FeeEstimations;
Expand Down Expand Up @@ -650,12 +650,8 @@ async fn run_syncerd_task_receiver(
},
Task::Abort(task) => {
let mut state_guard = state.lock().await;
let respond = match task.respond {
Boolean::True => true,
Boolean::False => false,
};
state_guard
.abort(task.task_target, syncerd_task.source, respond)
.abort(task.task_target, syncerd_task.source, task.respond)
.await;
drop(state_guard);
}
Expand Down
8 changes: 2 additions & 6 deletions src/syncerd/monero_syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::syncerd::runtime::Synclet;
use crate::syncerd::syncer_state::create_set;
use crate::syncerd::syncer_state::AddressTx;
use crate::syncerd::syncer_state::SyncerState;
use crate::syncerd::types::{AddressAddendum, Boolean, SweepAddressAddendum, Task};
use crate::syncerd::types::{AddressAddendum, SweepAddressAddendum, Task};
use crate::syncerd::TaskTarget;
use crate::syncerd::TransactionBroadcasted;
use crate::syncerd::XmrAddressAddendum;
Expand Down Expand Up @@ -485,12 +485,8 @@ async fn run_syncerd_task_receiver(
},
Task::Abort(task) => {
let mut state_guard = state.lock().await;
let respond = match task.respond {
Boolean::True => true,
Boolean::False => false,
};
state_guard
.abort(task.task_target, syncerd_task.source, respond)
.abort(task.task_target, syncerd_task.source, task.respond)
.await;
}
Task::BroadcastTransaction(task) => {
Expand Down
4 changes: 2 additions & 2 deletions src/syncerd/syncer_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,14 +955,14 @@ async fn syncer_state_addresses() {
id: TaskId(0),
lifetime: 1,
addendum: addendum.clone(),
include_tx: Boolean::False,
include_tx: false,
filter: TxFilter::All,
};
let address_task_two = WatchAddress {
id: TaskId(0),
lifetime: 1,
addendum: addendum.clone(),
include_tx: Boolean::False,
include_tx: false,
filter: TxFilter::All,
};
let source1 = ServiceId::Syncer(Blockchain::Bitcoin, Network::Mainnet);
Expand Down
Loading

0 comments on commit e3dbf4a

Please sign in to comment.