traits.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. use crate::ethereum::swap_creator::Swap; // TODO: shouldn't depend on this
  2. use crate::{
  3. error::Error,
  4. ethereum::swap_creator::SwapCreator,
  5. protocol::{follower, initiator},
  6. };
  7. use darkfi_serial::async_trait;
  8. use ethers::{prelude::*, utils::hex};
  9. use pasta_curves::pallas;
  10. use smol::channel;
  11. use std::{
  12. fmt,
  13. fmt::{Display, Formatter},
  14. };
  15. pub(crate) use crate::ethereum::swap_creator::Swap as ContractSwapArgs;
  16. // Initial parameters required by the swap initiator.
  17. // TODO: make Address/U256 generic; these are ethers-specific right now
  18. #[allow(dead_code)]
  19. #[derive(Debug, Clone)]
  20. pub(crate) struct InitiationArgs {
  21. pub(crate) owner: Address,
  22. pub(crate) claimer: Address,
  23. pub(crate) claim_commitment: [u8; 32],
  24. pub(crate) timeout_duration_1: U256,
  25. pub(crate) timeout_duration_2: U256,
  26. pub(crate) asset: Address,
  27. pub(crate) value: U256,
  28. pub(crate) nonce: U256,
  29. }
  30. // // TODO: make Address/U256 generic; these are ethers-specific right now
  31. // #[derive(Debug, Clone)]
  32. // pub(crate) struct ContractSwapArgs {
  33. // pub(crate) owner: Address,
  34. // pub(crate) claimer: Address,
  35. // pub(crate) claim_commitment: [u8; 32],
  36. // pub(crate) refund_commitment: [u8; 32],
  37. // pub(crate) timeout_duration_1: U256,
  38. // pub(crate) timeout_duration_2: U256,
  39. // pub(crate) asset: Address,
  40. // pub(crate) value: U256,
  41. // pub(crate) nonce: U256,
  42. // }
  43. // impl From<ContractSwapArgs> for crate::ethereum::swap_creator::Swap {
  44. // fn from(args: ContractSwapArgs) -> Self {
  45. // Self {
  46. // owner: args.owner,
  47. // claim_commitment: args.claim_commitment,
  48. // refund_commitment: args.refund_commitment,
  49. // claimer: args.claimer,
  50. // timeout_1: args.timeout_duration_1,
  51. // timeout_2: args.timeout_duration_2,
  52. // asset: args.asset,
  53. // value: args.value,
  54. // nonce: args.nonce,
  55. // }
  56. // }
  57. // }
  58. // Initial parameters required by the swap follower.
  59. #[derive(Debug)]
  60. pub(crate) struct FollowerArgs {
  61. pub(crate) counterparty_public_key: pallas::Point, // TODO: make this generic
  62. pub(crate) value: u128, // TODO: is u128 sufficient?
  63. }
  64. // TODO: make this generic for both chains
  65. #[allow(dead_code)]
  66. #[derive(Debug)]
  67. pub(crate) struct CounterpartyKeys {
  68. pub(crate) secp256k1_public_key: [u8; 33],
  69. }
  70. impl Display for CounterpartyKeys {
  71. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  72. write!(
  73. f,
  74. "CounterpartyKeys {{ secp256k1_public_key: {:?} }}",
  75. hex::encode(self.secp256k1_public_key)
  76. )
  77. }
  78. }
  79. #[allow(dead_code)]
  80. #[derive(Debug, Clone)]
  81. pub(crate) struct HandleCounterpartyKeysReceivedResult {
  82. // the ID of the swap within the on-chain contract
  83. pub(crate) contract_swap_id: [u8; 32],
  84. // the details of the swap within the on-chain contract
  85. pub(crate) contract_swap: Swap,
  86. // the block number at which the swap was initiated
  87. pub(crate) block_number: u64,
  88. }
  89. /// the chain that initiates the swap; ie. the first-mover
  90. ///
  91. /// the implementation of this trait must hold a signing key for
  92. /// chain A and chain B.
  93. ///
  94. /// TODO: [`Swap`] should be a non-chain-specific type
  95. #[async_trait]
  96. pub(crate) trait Initiator {
  97. // initiates the swap by locking funds on chain A
  98. async fn handle_counterparty_keys_received(
  99. &self,
  100. args: ContractSwapArgs,
  101. ) -> Result<HandleCounterpartyKeysReceivedResult, Error>;
  102. // handles the counterparty locking funds
  103. async fn handle_counterparty_funds_locked(
  104. &self,
  105. swap: Swap,
  106. swap_id: [u8; 32],
  107. ) -> Result<(), Error>;
  108. // handles the counterparty claiming funds
  109. async fn handle_counterparty_funds_claimed(
  110. &self,
  111. counterparty_secret: [u8; 32],
  112. ) -> Result<(), Error>;
  113. // handles the timeout cases where we need to refund funds
  114. async fn handle_should_refund(&self, swap: Swap) -> Result<(), Error>;
  115. }
  116. #[async_trait]
  117. pub(crate) trait InitiatorEventWatcher {
  118. async fn run_received_counterparty_keys_watcher(
  119. event_tx: channel::Sender<initiator::Event>,
  120. counterparty_keys_rx: channel::Receiver<CounterpartyKeys>,
  121. ) -> Result<(), Error>;
  122. async fn run_counterparty_funds_locked_watcher(
  123. event_tx: channel::Sender<initiator::Event>,
  124. ) -> Result<(), Error>;
  125. // TODO: make this generic for both chains
  126. async fn run_counterparty_funds_claimed_watcher<M: Middleware>(
  127. event_tx: channel::Sender<initiator::Event>,
  128. contract: SwapCreator<M>,
  129. contract_swap_id: &[u8; 32],
  130. from_block: u64,
  131. ) -> Result<(), Error>;
  132. async fn run_timeout_1_watcher(
  133. event_tx: channel::Sender<initiator::Event>,
  134. timeout_1: u64,
  135. buffer_seconds: u64,
  136. ) -> Result<(), Error>;
  137. async fn run_timeout_2_watcher(
  138. event_tx: channel::Sender<initiator::Event>,
  139. timeout_2: u64,
  140. ) -> Result<(), Error>;
  141. }
  142. /// the chain that is the counterparty to the swap; ie. the second-mover
  143. pub(crate) trait Follower {
  144. // handle the swap initiation by locking funds on chain B
  145. fn handle_counterparty_funds_locked(
  146. &mut self,
  147. contract_swap_id: ContractSwapArgs,
  148. ) -> Result<(), crate::Error>;
  149. // handle the funds being ready to be claimed by us
  150. fn handle_ready_to_claim(&self) -> Result<(), crate::Error>;
  151. // handle the counterparty refunding their funds, in case of a timeout
  152. fn handle_counterparty_funds_refunded(
  153. &self,
  154. counterparty_secret: [u8; 32],
  155. ) -> Result<(), crate::Error>;
  156. }
  157. #[async_trait]
  158. pub(crate) trait FollowerEventWatcher {
  159. async fn run_counterparty_funds_locked_watcher<M: Middleware>(
  160. event_tx: channel::Sender<follower::Event>,
  161. contract: SwapCreator<M>,
  162. middleware: std::sync::Arc<M>,
  163. claim_commitment: [u8; 32],
  164. refund_commitment: [u8; 32],
  165. from_block: u64,
  166. ) -> Result<(), Error>;
  167. async fn run_ready_to_claim_watcher<M: Middleware>(
  168. event_tx: channel::Sender<follower::Event>,
  169. contract: SwapCreator<M>,
  170. contract_swap_id: &[u8; 32],
  171. from_block: u64,
  172. ) -> Result<(), Error>;
  173. async fn run_counterparty_funds_refunded_watcher<M: Middleware>(
  174. event_tx: channel::Sender<follower::Event>,
  175. contract: SwapCreator<M>,
  176. contract_swap_id: &[u8; 32],
  177. from_block: u64,
  178. ) -> Result<(), Error>;
  179. }