traits.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use crate::ethereum::swap_creator::Swap; // TODO: shouldn't depend on this
  2. use crate::{error::Error, ethereum::swap_creator::SwapCreator, protocol::initiator::Event};
  3. use darkfi_serial::async_trait;
  4. use ethers::{prelude::*, utils::hex};
  5. use smol::channel;
  6. use std::{
  7. fmt,
  8. fmt::{Display, Formatter},
  9. };
  10. // Initial parameters required by the swap initiator.
  11. // TODO: make Address/U256 generic; these are ethers-specific right now
  12. #[allow(dead_code)]
  13. #[derive(Debug, Clone)]
  14. pub(crate) struct InitiationArgs {
  15. pub(crate) claim_commitment: [u8; 32],
  16. pub(crate) claimer: Address,
  17. pub(crate) timeout_duration_1: U256,
  18. pub(crate) timeout_duration_2: U256,
  19. pub(crate) asset: Address,
  20. pub(crate) value: U256,
  21. pub(crate) nonce: U256,
  22. }
  23. // TODO: make Address/U256 generic; these are ethers-specific right now
  24. #[derive(Debug)]
  25. pub(crate) struct InitiateSwapArgs {
  26. pub(crate) claim_commitment: [u8; 32],
  27. pub(crate) refund_commitment: [u8; 32],
  28. pub(crate) claimer: Address,
  29. pub(crate) timeout_duration_1: U256,
  30. pub(crate) timeout_duration_2: U256,
  31. pub(crate) asset: Address,
  32. pub(crate) value: U256,
  33. pub(crate) nonce: U256,
  34. }
  35. // TODO: make this generic for both chains
  36. #[allow(dead_code)]
  37. #[derive(Debug)]
  38. pub(crate) struct CounterpartyKeys {
  39. pub(crate) secp256k1_public_key: [u8; 33],
  40. }
  41. impl Display for CounterpartyKeys {
  42. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  43. write!(
  44. f,
  45. "CounterpartyKeys {{ secp256k1_public_key: {:?} }}",
  46. hex::encode(self.secp256k1_public_key)
  47. )
  48. }
  49. }
  50. #[allow(dead_code)]
  51. #[derive(Debug, Clone)]
  52. pub(crate) struct HandleCounterpartyKeysReceivedResult {
  53. // the ID of the swap within the on-chain contract
  54. pub(crate) contract_swap_id: [u8; 32],
  55. // the details of the swap within the on-chain contract
  56. pub(crate) contract_swap: Swap,
  57. // the block number at which the swap was initiated
  58. pub(crate) block_number: u64,
  59. }
  60. /// the chain that initiates the swap; ie. the first-mover
  61. ///
  62. /// the implementation of this trait must hold a signing key for
  63. /// chain A and chain B.
  64. ///
  65. /// TODO: [`Swap`] should be a non-chain-specific type
  66. #[async_trait]
  67. pub(crate) trait Initiator {
  68. // initiates the swap by locking funds on chain A
  69. async fn handle_counterparty_keys_received(
  70. &self,
  71. args: InitiateSwapArgs,
  72. ) -> Result<HandleCounterpartyKeysReceivedResult, Error>;
  73. // handles the counterparty locking funds
  74. async fn handle_counterparty_funds_locked(
  75. &self,
  76. swap: Swap,
  77. swap_id: [u8; 32],
  78. ) -> Result<(), Error>;
  79. // handles the counterparty claiming funds
  80. async fn handle_counterparty_funds_claimed(
  81. &self,
  82. counterparty_secret: [u8; 32],
  83. ) -> Result<(), Error>;
  84. // handles the timeout cases where we need to refund funds
  85. async fn handle_should_refund(&self, swap: Swap) -> Result<(), Error>;
  86. }
  87. #[async_trait]
  88. pub(crate) trait InitiatorEventWatcher {
  89. async fn run_received_counterparty_keys_watcher(
  90. event_tx: channel::Sender<Event>,
  91. counterparty_keys_rx: channel::Receiver<CounterpartyKeys>,
  92. ) -> Result<(), Error>;
  93. async fn run_counterparty_funds_locked_watcher(
  94. event_tx: channel::Sender<Event>,
  95. ) -> Result<(), Error>;
  96. // TODO: make this generic for both chains
  97. async fn run_counterparty_funds_claimed_watcher<M: Middleware>(
  98. event_tx: channel::Sender<Event>,
  99. contract: SwapCreator<M>,
  100. contract_swap_id: &[u8; 32],
  101. from_block: u64,
  102. ) -> Result<(), Error>;
  103. async fn run_timeout_1_watcher(
  104. event_tx: channel::Sender<Event>,
  105. timeout_1: u64,
  106. buffer_seconds: u64,
  107. ) -> Result<(), Error>;
  108. async fn run_timeout_2_watcher(
  109. event_tx: channel::Sender<Event>,
  110. timeout_2: u64,
  111. ) -> Result<(), Error>;
  112. }
  113. /// the chain that is the counterparty to the swap; ie. the second-mover
  114. pub(crate) trait Follower {
  115. // handle the swap initiation by locking funds on chain B
  116. fn handle_counterparty_funds_locked(&self);
  117. // handle the funds being ready to be claimed by us
  118. fn handle_ready_to_claim(&self);
  119. // handle the counterparty refunding their funds, in case of a timeout
  120. fn handle_counterparty_funds_refunded(&self);
  121. }