Update the processor to use the coin's specified fee

This commit is contained in:
Luke Parker 2022-06-19 12:19:32 -04:00
parent f50f249468
commit b6ea654823
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
5 changed files with 31 additions and 25 deletions

View file

@ -13,7 +13,7 @@ use monero_serai::{
frost::Ed25519, frost::Ed25519,
transaction::{Timelock, Transaction}, transaction::{Timelock, Transaction},
rpc::Rpc, rpc::Rpc,
wallet::{SpendableOutput, SignableTransaction as MSignableTransaction, TransactionMachine} wallet::{Fee, SpendableOutput, SignableTransaction as MSignableTransaction, TransactionMachine}
}; };
use crate::{Transcript, CoinError, Output as OutputTrait, Coin, view_key}; use crate::{Transcript, CoinError, Output as OutputTrait, Coin, view_key};
@ -59,7 +59,7 @@ pub struct SignableTransaction(
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Monero { pub struct Monero {
rpc: Rpc, pub(crate) rpc: Rpc,
view: Scalar, view: Scalar,
view_pub: PublicKey view_pub: PublicKey
} }
@ -79,6 +79,7 @@ impl Monero {
impl Coin for Monero { impl Coin for Monero {
type Curve = Ed25519; type Curve = Ed25519;
type Fee = Fee;
type Transaction = Transaction; type Transaction = Transaction;
type Block = Vec<Transaction>; type Block = Vec<Transaction>;
@ -132,7 +133,8 @@ impl Coin for Monero {
transcript: Transcript, transcript: Transcript,
height: usize, height: usize,
mut inputs: Vec<Output>, mut inputs: Vec<Output>,
payments: &[(Address, u64)] payments: &[(Address, u64)],
fee: Fee
) -> Result<SignableTransaction, CoinError> { ) -> Result<SignableTransaction, CoinError> {
let spend = keys.group_key(); let spend = keys.group_key();
Ok( Ok(
@ -143,8 +145,8 @@ impl Coin for Monero {
MSignableTransaction::new( MSignableTransaction::new(
inputs.drain(..).map(|input| input.0).collect(), inputs.drain(..).map(|input| input.0).collect(),
payments.to_vec(), payments.to_vec(),
self.address(spend), Some(self.address(spend)),
100000000 // TODO fee
).map_err(|_| CoinError::ConnectionError)? ).map_err(|_| CoinError::ConnectionError)?
) )
) )
@ -156,7 +158,6 @@ impl Coin for Monero {
included: &[u16] included: &[u16]
) -> Result<Self::TransactionMachine, CoinError> { ) -> Result<Self::TransactionMachine, CoinError> {
transaction.3.clone().multisig( transaction.3.clone().multisig(
&mut OsRng,
&self.rpc, &self.rpc,
(*transaction.0).clone(), (*transaction.0).clone(),
transaction.1.clone(), transaction.1.clone(),
@ -213,8 +214,8 @@ impl Coin for Monero {
let tx = MSignableTransaction::new( let tx = MSignableTransaction::new(
outputs, outputs,
vec![(address, amount - fee)], vec![(address, amount - fee)],
temp, Some(temp),
fee / 2000 self.rpc.get_fee().await.unwrap()
).unwrap().sign(&mut OsRng, &self.rpc, &Scalar::one()).await.unwrap(); ).unwrap().sign(&mut OsRng, &self.rpc, &Scalar::one()).await.unwrap();
self.rpc.publish_transaction(&tx).await.unwrap(); self.rpc.publish_transaction(&tx).await.unwrap();
self.mine_block(temp).await; self.mine_block(temp).await;

View file

@ -51,6 +51,7 @@ pub trait Output: Sized + Clone {
pub trait Coin { pub trait Coin {
type Curve: Curve; type Curve: Curve;
type Fee: Copy;
type Transaction; type Transaction;
type Block; type Block;
@ -82,7 +83,8 @@ pub trait Coin {
transcript: Transcript, transcript: Transcript,
height: usize, height: usize,
inputs: Vec<Self::Output>, inputs: Vec<Self::Output>,
payments: &[(Self::Address, u64)] payments: &[(Self::Address, u64)],
fee: Self::Fee
) -> Result<Self::SignableTransaction, CoinError>; ) -> Result<Self::SignableTransaction, CoinError>;
async fn attempt_send( async fn attempt_send(

View file

@ -1,5 +0,0 @@
struct Scanner<C: Coin> {}
impl Scanner {
}

View file

@ -57,16 +57,17 @@ impl Network for LocalNetwork {
} }
} }
async fn test_send<C: Coin + Clone>(coin: C) { async fn test_send<C: Coin + Clone>(coin: C, fee: C::Fee) {
// Mine a block so there's a confirmed height // Mine a block so there's a confirmed height
coin.mine_block(coin.address(<C::Curve as Curve>::G::generator())).await; coin.mine_block(coin.address(<C::Curve as Curve>::G::generator())).await;
let height = coin.get_height().await.unwrap(); let height = coin.get_height().await.unwrap();
let mut networks = LocalNetwork::new(3);
let mut keys = frost::tests::key_gen::<_, C::Curve>(&mut OsRng); let mut keys = frost::tests::key_gen::<_, C::Curve>(&mut OsRng);
let threshold = keys[&1].params().t();
let mut networks = LocalNetwork::new(threshold);
let mut wallets = vec![]; let mut wallets = vec![];
for i in 1 ..= 3 { for i in 1 ..= threshold {
let mut wallet = Wallet::new(MemCoinDb::new(), coin.clone()); let mut wallet = Wallet::new(MemCoinDb::new(), coin.clone());
wallet.acknowledge_height(0, height); wallet.acknowledge_height(0, height);
wallet.add_keys( wallet.add_keys(
@ -95,9 +96,12 @@ async fn test_send<C: Coin + Clone>(coin: C) {
wallet.acknowledge_height(1, height - 10); wallet.acknowledge_height(1, height - 10);
let signable = wallet.prepare_sends( let signable = wallet.prepare_sends(
1, 1,
vec![(wallet.address(), 10000000000)] vec![(wallet.address(), 10000000000)],
fee
).await.unwrap().1.swap_remove(0); ).await.unwrap().1.swap_remove(0);
futures.push(wallet.attempt_send(network, signable, &[1, 2, 3])); futures.push(
wallet.attempt_send(network, signable, (1 ..= threshold).into_iter().collect::<Vec<_>>())
);
} }
println!( println!(
@ -108,5 +112,7 @@ async fn test_send<C: Coin + Clone>(coin: C) {
#[tokio::test] #[tokio::test]
async fn monero() { async fn monero() {
test_send(Monero::new("http://127.0.0.1:18081".to_string())).await; let monero = Monero::new("http://127.0.0.1:18081".to_string());
let fee = monero.rpc.get_fee().await.unwrap();
test_send(monero, fee).await;
} }

View file

@ -281,7 +281,8 @@ impl<D: CoinDb, C: Coin> Wallet<D, C> {
pub async fn prepare_sends( pub async fn prepare_sends(
&mut self, &mut self,
canonical: usize, canonical: usize,
payments: Vec<(C::Address, u64)> payments: Vec<(C::Address, u64)>,
fee: C::Fee
) -> Result<(Vec<(C::Address, u64)>, Vec<C::SignableTransaction>), CoinError> { ) -> Result<(Vec<(C::Address, u64)>, Vec<C::SignableTransaction>), CoinError> {
if payments.len() == 0 { if payments.len() == 0 {
return Ok((vec![], vec![])); return Ok((vec![], vec![]));
@ -326,7 +327,8 @@ impl<D: CoinDb, C: Coin> Wallet<D, C> {
transcript, transcript,
acknowledged_height, acknowledged_height,
inputs, inputs,
&outputs &outputs,
fee
).await?; ).await?;
// self.db.save_tx(tx) // TODO // self.db.save_tx(tx) // TODO
txs.push(tx); txs.push(tx);
@ -340,11 +342,11 @@ impl<D: CoinDb, C: Coin> Wallet<D, C> {
&mut self, &mut self,
network: &mut N, network: &mut N,
prepared: C::SignableTransaction, prepared: C::SignableTransaction,
included: &[u16] included: Vec<u16>
) -> Result<(Vec<u8>, Vec<<C::Output as Output>::Id>), SignError> { ) -> Result<(Vec<u8>, Vec<<C::Output as Output>::Id>), SignError> {
let mut attempt = self.coin.attempt_send( let mut attempt = self.coin.attempt_send(
prepared, prepared,
included &included
).await.map_err(|e| SignError::CoinError(e))?; ).await.map_err(|e| SignError::CoinError(e))?;
let commitments = network.round( let commitments = network.round(