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

View file

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