You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.9 KiB
127 lines
4.9 KiB
use crate::{
|
|
monero::TransferProof,
|
|
protocol::{bob, bob::BobState},
|
|
SwapAmounts,
|
|
};
|
|
use ::bitcoin::hashes::core::fmt::Display;
|
|
use monero_harness::rpc::wallet::BlockHeight;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
|
pub enum Bob {
|
|
Started {
|
|
state0: bob::State0,
|
|
amounts: SwapAmounts,
|
|
},
|
|
Negotiated {
|
|
state2: bob::State2,
|
|
},
|
|
BtcLocked {
|
|
state3: bob::State3,
|
|
},
|
|
XmrLockProofReceived {
|
|
state: bob::State3,
|
|
lock_transfer_proof: TransferProof,
|
|
monero_wallet_restore_blockheight: BlockHeight,
|
|
},
|
|
XmrLocked {
|
|
state4: bob::State4,
|
|
},
|
|
EncSigSent {
|
|
state4: bob::State4,
|
|
},
|
|
BtcRedeemed(bob::State5),
|
|
CancelTimelockExpired(bob::State4),
|
|
BtcCancelled(bob::State4),
|
|
Done(BobEndState),
|
|
}
|
|
|
|
#[derive(Clone, strum::Display, Debug, Deserialize, Serialize, PartialEq)]
|
|
pub enum BobEndState {
|
|
SafelyAborted,
|
|
XmrRedeemed { tx_lock_id: bitcoin::Txid },
|
|
BtcRefunded(Box<bob::State4>),
|
|
BtcPunished { tx_lock_id: bitcoin::Txid },
|
|
}
|
|
|
|
impl From<BobState> for Bob {
|
|
fn from(bob_state: BobState) -> Self {
|
|
match bob_state {
|
|
BobState::Started { state0, amounts } => Bob::Started { state0, amounts },
|
|
BobState::Negotiated(state2) => Bob::Negotiated { state2 },
|
|
BobState::BtcLocked(state3) => Bob::BtcLocked { state3 },
|
|
BobState::XmrLockProofReceived {
|
|
state,
|
|
lock_transfer_proof,
|
|
monero_wallet_restore_blockheight,
|
|
} => Bob::XmrLockProofReceived {
|
|
state,
|
|
lock_transfer_proof,
|
|
monero_wallet_restore_blockheight,
|
|
},
|
|
BobState::XmrLocked(state4) => Bob::XmrLocked { state4 },
|
|
BobState::EncSigSent(state4) => Bob::EncSigSent { state4 },
|
|
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
|
|
BobState::CancelTimelockExpired(state4) => Bob::CancelTimelockExpired(state4),
|
|
BobState::BtcCancelled(state4) => Bob::BtcCancelled(state4),
|
|
BobState::BtcRefunded(state4) => Bob::Done(BobEndState::BtcRefunded(Box::new(state4))),
|
|
BobState::XmrRedeemed { tx_lock_id } => {
|
|
Bob::Done(BobEndState::XmrRedeemed { tx_lock_id })
|
|
}
|
|
BobState::BtcPunished { tx_lock_id } => {
|
|
Bob::Done(BobEndState::BtcPunished { tx_lock_id })
|
|
}
|
|
BobState::SafelyAborted => Bob::Done(BobEndState::SafelyAborted),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Bob> for BobState {
|
|
fn from(db_state: Bob) -> Self {
|
|
match db_state {
|
|
Bob::Started { state0, amounts } => BobState::Started { state0, amounts },
|
|
Bob::Negotiated { state2 } => BobState::Negotiated(state2),
|
|
Bob::BtcLocked { state3 } => BobState::BtcLocked(state3),
|
|
Bob::XmrLockProofReceived {
|
|
state,
|
|
lock_transfer_proof,
|
|
monero_wallet_restore_blockheight,
|
|
} => BobState::XmrLockProofReceived {
|
|
state,
|
|
lock_transfer_proof,
|
|
monero_wallet_restore_blockheight,
|
|
},
|
|
Bob::XmrLocked { state4 } => BobState::XmrLocked(state4),
|
|
Bob::EncSigSent { state4 } => BobState::EncSigSent(state4),
|
|
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
|
|
Bob::CancelTimelockExpired(state4) => BobState::CancelTimelockExpired(state4),
|
|
Bob::BtcCancelled(state4) => BobState::BtcCancelled(state4),
|
|
Bob::Done(end_state) => match end_state {
|
|
BobEndState::SafelyAborted => BobState::SafelyAborted,
|
|
BobEndState::XmrRedeemed { tx_lock_id } => BobState::XmrRedeemed { tx_lock_id },
|
|
BobEndState::BtcRefunded(state4) => BobState::BtcRefunded(*state4),
|
|
BobEndState::BtcPunished { tx_lock_id } => BobState::BtcPunished { tx_lock_id },
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Display for Bob {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Bob::Started { .. } => write!(f, "Started"),
|
|
Bob::Negotiated { .. } => f.write_str("Negotiated"),
|
|
Bob::BtcLocked { .. } => f.write_str("Bitcoin locked"),
|
|
Bob::XmrLockProofReceived { .. } => {
|
|
f.write_str("XMR lock transaction transfer proof received")
|
|
}
|
|
Bob::XmrLocked { .. } => f.write_str("Monero locked"),
|
|
Bob::CancelTimelockExpired(_) => f.write_str("Cancel timelock is expired"),
|
|
Bob::BtcCancelled(_) => f.write_str("Bitcoin refundable"),
|
|
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
|
Bob::Done(end_state) => write!(f, "Done: {}", end_state),
|
|
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
|
}
|
|
}
|
|
}
|