Monero time_for_block, dust

This commit is contained in:
Luke Parker
2024-09-13 19:24:45 -04:00
parent 947e1067d9
commit e56af7fc51
3 changed files with 47 additions and 72 deletions

View File

@@ -54,7 +54,38 @@ impl ScannerFeed for Rpc {
&self,
number: u64,
) -> impl Send + Future<Output = Result<u64, Self::EphemeralError>> {
async move { todo!("TODO") }
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)
}
}
fn unchecked_block_header_by_number(
@@ -66,17 +97,21 @@ impl ScannerFeed for Rpc {
async move { Ok(BlockHeader(self.rpc.get_block_by_number(number.try_into().unwrap()).await?)) }
}
#[rustfmt::skip] // It wants to improperly format the `async move` to a single line
fn unchecked_block_by_number(
&self,
number: u64,
) -> impl Send + Future<Output = Result<Self::Block, Self::EphemeralError>> {
async move { todo!("TODO") }
async move {
Ok(Block(self.rpc.get_scannable_block_by_number(number.try_into().unwrap()).await?))
}
}
fn dust(coin: Coin) -> Amount {
assert_eq!(coin, Coin::Monero);
todo!("TODO")
// 0.01 XMR
Amount(10_000_000_000)
}
fn cost_to_aggregate(