Add an ID function to Coin::Block

Also updates to the latest Monero lib API.
This commit is contained in:
Luke Parker 2023-01-07 04:03:11 -05:00
parent 1d6df0099c
commit ccf4ca2215
No known key found for this signature in database
2 changed files with 23 additions and 7 deletions

View file

@ -19,6 +19,11 @@ pub enum CoinError {
ConnectionError,
}
pub trait Block: Sized + Clone {
type Id: Clone + Copy + AsRef<[u8]>;
fn id(&self) -> Self::Id;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum OutputType {
External,
@ -27,7 +32,7 @@ pub enum OutputType {
}
pub trait Output: Sized + Clone {
type Id: AsRef<[u8]>;
type Id: Clone + Copy + AsRef<[u8]>;
fn kind(&self) -> OutputType;
@ -44,7 +49,7 @@ pub trait Coin {
type Fee: Copy;
type Transaction;
type Block;
type Block: Block;
type Output: Output;
type SignableTransaction;

View file

@ -10,7 +10,7 @@ use frost::{curve::Ed25519, ThresholdKeys};
use monero_serai::{
transaction::Transaction,
block::Block,
block::Block as MBlock,
rpc::Rpc,
wallet::{
ViewPair, Scanner,
@ -21,9 +21,18 @@ use monero_serai::{
use crate::{
additional_key,
coin::{CoinError, OutputType, Output as OutputTrait, Coin},
coin::{CoinError, Block as BlockTrait, OutputType, Output as OutputTrait, Coin},
};
#[derive(Clone, Debug)]
pub struct Block([u8; 32], MBlock);
impl BlockTrait for Block {
type Id = [u8; 32];
fn id(&self) -> Self::Id {
self.0
}
}
#[derive(Clone, Debug)]
pub struct Output(SpendableOutput);
impl From<SpendableOutput> for Output {
@ -162,7 +171,9 @@ impl Coin for Monero {
}
async fn get_block(&self, number: usize) -> Result<Self::Block, CoinError> {
self.rpc.get_block(number).await.map_err(|_| CoinError::ConnectionError)
let hash = self.rpc.get_block_hash(number).await.map_err(|_| CoinError::ConnectionError)?;
let block = self.rpc.get_block(hash).await.map_err(|_| CoinError::ConnectionError)?;
Ok(Block(hash, block))
}
async fn get_outputs(
@ -172,7 +183,7 @@ impl Coin for Monero {
) -> Result<Vec<Self::Output>, CoinError> {
let mut transactions = self
.scanner(key)
.scan(&self.rpc, block)
.scan(&self.rpc, &block.1)
.await
.map_err(|_| CoinError::ConnectionError)?
.iter()
@ -288,7 +299,7 @@ impl Coin for Monero {
}
let outputs = Self::test_scanner()
.scan(&self.rpc, &self.rpc.get_block(new_block).await.unwrap())
.scan(&self.rpc, &self.rpc.get_block_by_number(new_block).await.unwrap())
.await
.unwrap()
.swap_remove(0)