2024-09-13 01:14:47 -04:00
|
|
|
use core::future::Future;
|
|
|
|
|
|
2025-09-18 19:00:10 -04:00
|
|
|
use monero_simple_request_rpc::{prelude::*, SimpleRequestTransport};
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2025-11-04 19:02:37 -05:00
|
|
|
use serai_primitives::{network_id::ExternalNetworkId, coin::ExternalCoin, balance::Amount};
|
2024-09-12 18:40:10 -04:00
|
|
|
|
|
|
|
|
use scanner::ScannerFeed;
|
|
|
|
|
use signers::TransactionPublisher;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
transaction::Transaction,
|
|
|
|
|
block::{BlockHeader, Block},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2024-09-14 01:09:35 -04:00
|
|
|
pub(crate) struct Rpc {
|
2025-09-18 19:00:10 -04:00
|
|
|
pub(crate) rpc: MoneroDaemon<SimpleRequestTransport>,
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-14 01:09:35 -04:00
|
|
|
impl ScannerFeed for Rpc {
|
2025-01-30 03:14:24 -05:00
|
|
|
const NETWORK: ExternalNetworkId = ExternalNetworkId::Monero;
|
2024-09-13 00:48:57 -04:00
|
|
|
// Outputs aren't spendable until 10 blocks later due to the 10-block lock
|
|
|
|
|
// Since we assumed scanned outputs are spendable, that sets a minimum confirmation depth of 10
|
|
|
|
|
// A 10-block reorganization hasn't been observed in years and shouldn't occur
|
|
|
|
|
const CONFIRMATIONS: u64 = 10;
|
|
|
|
|
// The window length should be roughly an hour
|
|
|
|
|
const WINDOW_LENGTH: u64 = 30;
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2024-09-13 00:48:57 -04:00
|
|
|
const TEN_MINUTES: u64 = 5;
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2024-09-13 00:48:57 -04:00
|
|
|
type Block = Block;
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2025-09-18 19:00:10 -04:00
|
|
|
type EphemeralError = InterfaceError;
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2024-09-13 01:14:47 -04:00
|
|
|
fn latest_finalized_block_number(
|
|
|
|
|
&self,
|
|
|
|
|
) -> impl Send + Future<Output = Result<u64, Self::EphemeralError>> {
|
|
|
|
|
async move {
|
2024-09-14 01:09:35 -04:00
|
|
|
Ok(
|
2025-09-18 19:00:10 -04:00
|
|
|
u64::try_from(self.rpc.latest_block_number().await?)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.saturating_sub(Self::CONFIRMATIONS - 1),
|
2024-09-14 01:09:35 -04:00
|
|
|
)
|
2024-09-13 01:14:47 -04:00
|
|
|
}
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 01:14:47 -04:00
|
|
|
fn time_of_block(
|
|
|
|
|
&self,
|
|
|
|
|
number: u64,
|
|
|
|
|
) -> impl Send + Future<Output = Result<u64, Self::EphemeralError>> {
|
2024-09-13 19:24:45 -04:00
|
|
|
async move {
|
|
|
|
|
// Constant from Monero
|
|
|
|
|
const BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW: u64 = 60;
|
|
|
|
|
|
|
|
|
|
// If Monero doesn't have enough blocks to build a window, it doesn't define a network time
|
|
|
|
|
if (number + 1) < BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW {
|
|
|
|
|
return Ok(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch all the timestamps within the window
|
2025-09-18 19:00:10 -04:00
|
|
|
let block_for_time_of = self.rpc.block_by_number(number.try_into().unwrap()).await?;
|
2024-09-13 19:24:45 -04:00
|
|
|
let mut timestamps = vec![block_for_time_of.header.timestamp];
|
|
|
|
|
let mut parent = block_for_time_of.header.previous;
|
|
|
|
|
for _ in 1 .. BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW {
|
2025-09-18 19:00:10 -04:00
|
|
|
let parent_block = self.rpc.block(parent).await?;
|
2024-09-13 19:24:45 -04:00
|
|
|
timestamps.push(parent_block.header.timestamp);
|
|
|
|
|
parent = parent_block.header.previous;
|
|
|
|
|
}
|
|
|
|
|
timestamps.sort();
|
|
|
|
|
|
|
|
|
|
// Because there are two timestamps equidistance from the ends, Monero's epee picks the
|
|
|
|
|
// in-between value, calculated by the following formula (from the "get_mid" function)
|
|
|
|
|
let n = timestamps.len() / 2;
|
|
|
|
|
let a = timestamps[n - 1];
|
|
|
|
|
let b = timestamps[n];
|
|
|
|
|
#[rustfmt::skip] // Enables Ctrl+F'ing for everything after the `= `
|
|
|
|
|
let res = (a/2) + (b/2) + ((a - 2*(a/2)) + (b - 2*(b/2)))/2;
|
|
|
|
|
|
|
|
|
|
// Monero does check that the new block's time is greater than the median, causing the median
|
|
|
|
|
// to be monotonic
|
|
|
|
|
Ok(res)
|
|
|
|
|
}
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 01:14:47 -04:00
|
|
|
fn unchecked_block_header_by_number(
|
2024-09-12 18:40:10 -04:00
|
|
|
&self,
|
|
|
|
|
number: u64,
|
2024-09-13 01:14:47 -04:00
|
|
|
) -> impl Send
|
|
|
|
|
+ Future<Output = Result<<Self::Block as primitives::Block>::Header, Self::EphemeralError>>
|
|
|
|
|
{
|
2025-09-18 19:00:10 -04:00
|
|
|
async move { Ok(BlockHeader(self.rpc.block_by_number(number.try_into().unwrap()).await?)) }
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 19:24:45 -04:00
|
|
|
#[rustfmt::skip] // It wants to improperly format the `async move` to a single line
|
2024-09-13 01:14:47 -04:00
|
|
|
fn unchecked_block_by_number(
|
2024-09-12 18:40:10 -04:00
|
|
|
&self,
|
|
|
|
|
number: u64,
|
2024-09-13 01:14:47 -04:00
|
|
|
) -> impl Send + Future<Output = Result<Self::Block, Self::EphemeralError>> {
|
2024-09-13 19:24:45 -04:00
|
|
|
async move {
|
2025-09-18 19:00:10 -04:00
|
|
|
Ok(Block(self.rpc.scannable_block_by_number(number.try_into().unwrap()).await?))
|
2024-09-13 19:24:45 -04:00
|
|
|
}
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-30 03:14:24 -05:00
|
|
|
fn dust(coin: ExternalCoin) -> Amount {
|
|
|
|
|
assert_eq!(coin, ExternalCoin::Monero);
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2024-09-13 19:24:45 -04:00
|
|
|
// 0.01 XMR
|
|
|
|
|
Amount(10_000_000_000)
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 01:14:47 -04:00
|
|
|
fn cost_to_aggregate(
|
2024-09-12 18:40:10 -04:00
|
|
|
&self,
|
2025-01-30 03:14:24 -05:00
|
|
|
coin: ExternalCoin,
|
2024-09-12 18:40:10 -04:00
|
|
|
_reference_block: &Self::Block,
|
2024-09-13 01:14:47 -04:00
|
|
|
) -> impl Send + Future<Output = Result<Amount, Self::EphemeralError>> {
|
|
|
|
|
async move {
|
2025-01-30 03:14:24 -05:00
|
|
|
assert_eq!(coin, ExternalCoin::Bitcoin);
|
2024-09-13 01:14:47 -04:00
|
|
|
// TODO
|
|
|
|
|
Ok(Amount(0))
|
|
|
|
|
}
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 01:09:35 -04:00
|
|
|
impl TransactionPublisher<Transaction> for Rpc {
|
2025-09-18 19:00:10 -04:00
|
|
|
type EphemeralError = PublishTransactionError;
|
2024-09-12 18:40:10 -04:00
|
|
|
|
2024-09-13 01:14:47 -04:00
|
|
|
fn publish(
|
|
|
|
|
&self,
|
|
|
|
|
tx: Transaction,
|
|
|
|
|
) -> impl Send + Future<Output = Result<(), Self::EphemeralError>> {
|
|
|
|
|
async move { self.rpc.publish_transaction(&tx.0).await }
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
}
|