xmr-btc-swap/swap-core/src/bitcoin/timelocks.rs
Mohan 4ae47e57f9
refactor: swap-core / swap-machine (#530)
* progress

* fix thread safety

* move monero types from swap into swap_core

* just fmt

* move non test code above test code

* revert removed tracing in bitcoin-wallet/src/primitives.rs

* Use existing private_key_from_secp256k1_scalar

* remove unused monero chose code

* fix some clippy warnings due to imports

* move state machine types into the new `swap-machine` crate

* remove monero_c orphan submodule

* rm bdk_test and sqlx_test from ci

* move proptest.rs into swap-proptest

* increase stack size to 12mb

* properly increase stack size

* fix merge conflict in ci.yml

* don't increase stack size on mac

* fix infinite recursion

* fix integration tests

* fix some compile errors

* fix compilation errors

* rustfmt

* ignore unstaged patches we applied to monero submodule when running git status

* fix some test compilation errors

* use BitcoinWallet trait instead of concrete type everywhere

* add just test command to run integration tests

* remove test_utils features from bdk in swap-core

---------

Co-authored-by: einliterflasche <einliterflasche@pm.me>
Co-authored-by: binarybaron <binarybaron@mail.mail>
2025-10-10 16:29:00 +02:00

65 lines
1.7 KiB
Rust

use anyhow::Context;
use bdk_electrum::electrum_client::HeaderNotification;
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::ops::Add;
use typeshare::typeshare;
/// Represent a block height, or block number, expressed in absolute block
/// count.
///
/// E.g. The transaction was included in block #655123, 655123 blocks
/// after the genesis block.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BlockHeight(u32);
impl From<BlockHeight> for u32 {
fn from(height: BlockHeight) -> Self {
height.0
}
}
impl From<u32> for BlockHeight {
fn from(height: u32) -> Self {
Self(height)
}
}
impl TryFrom<HeaderNotification> for BlockHeight {
type Error = anyhow::Error;
fn try_from(value: HeaderNotification) -> Result<Self, Self::Error> {
Ok(Self(
value
.height
.try_into()
.context("Failed to fit usize into u32")?,
))
}
}
impl Add<u32> for BlockHeight {
type Output = BlockHeight;
fn add(self, rhs: u32) -> Self::Output {
BlockHeight(self.0 + rhs)
}
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(tag = "type", content = "content")]
pub enum ExpiredTimelocks {
None { blocks_left: u32 },
Cancel { blocks_left: u32 },
Punish,
}
impl ExpiredTimelocks {
/// Check whether the timelock on the cancel transaction has expired.
///
/// Retuns `true` even if the swap has already been canceled or punished.
pub fn cancel_timelock_expired(&self) -> bool {
!matches!(self, ExpiredTimelocks::None { .. })
}
}