2024-09-13 01:14:47 -04:00
|
|
|
use core::future::Future;
|
|
|
|
|
|
2024-09-13 00:48:57 -04:00
|
|
|
use monero_wallet::rpc::{RpcError, Rpc as RpcTrait};
|
|
|
|
|
use monero_simple_request_rpc::SimpleRequestRpc;
|
2024-09-12 18:40:10 -04:00
|
|
|
|
|
|
|
|
use serai_client::primitives::{NetworkId, Coin, Amount};
|
|
|
|
|
|
2024-09-13 23:51:53 -04:00
|
|
|
use serai_db::Db;
|
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-13 23:51:53 -04:00
|
|
|
pub(crate) struct Rpc<D: Db> {
|
|
|
|
|
pub(crate) db: D,
|
2024-09-13 00:48:57 -04:00
|
|
|
pub(crate) rpc: SimpleRequestRpc,
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-13 23:51:53 -04:00
|
|
|
impl<D: Db> ScannerFeed for Rpc<D> {
|
2024-09-13 00:48:57 -04:00
|
|
|
const NETWORK: NetworkId = NetworkId::Monero;
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
type EphemeralError = RpcError;
|
|
|
|
|
|
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-13 23:51:53 -04:00
|
|
|
// The decoys task only indexes finalized blocks
|
|
|
|
|
crate::decoys::NextToIndexBlock::get(&self.db)
|
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
RpcError::InternalError("decoys task hasn't indexed any blocks yet".to_string())
|
|
|
|
|
})?
|
|
|
|
|
.checked_sub(1)
|
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
RpcError::InternalError("only the genesis block has been indexed".to_string())
|
|
|
|
|
})
|
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
|
|
|
|
|
let block_for_time_of = self.rpc.get_block_by_number(number.try_into().unwrap()).await?;
|
|
|
|
|
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 {
|
|
|
|
|
let parent_block = self.rpc.get_block(parent).await?;
|
|
|
|
|
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>>
|
|
|
|
|
{
|
|
|
|
|
async move { Ok(BlockHeader(self.rpc.get_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 {
|
|
|
|
|
Ok(Block(self.rpc.get_scannable_block_by_number(number.try_into().unwrap()).await?))
|
|
|
|
|
}
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dust(coin: Coin) -> Amount {
|
2024-09-13 00:48:57 -04:00
|
|
|
assert_eq!(coin, Coin::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,
|
|
|
|
|
coin: Coin,
|
|
|
|
|
_reference_block: &Self::Block,
|
2024-09-13 01:14:47 -04:00
|
|
|
) -> impl Send + Future<Output = Result<Amount, Self::EphemeralError>> {
|
|
|
|
|
async move {
|
|
|
|
|
assert_eq!(coin, Coin::Bitcoin);
|
|
|
|
|
// TODO
|
|
|
|
|
Ok(Amount(0))
|
|
|
|
|
}
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 23:51:53 -04:00
|
|
|
impl<D: Db> TransactionPublisher<Transaction> for Rpc<D> {
|
2024-09-12 18:40:10 -04:00
|
|
|
type EphemeralError = RpcError;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|