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

View file

@ -51,6 +51,7 @@ pub trait Output: Sized + Clone {
pub trait Coin {
type Curve: Curve;
type Fee: Copy;
type Transaction;
type Block;
@ -82,7 +83,8 @@ pub trait Coin {
transcript: Transcript,
height: usize,
inputs: Vec<Self::Output>,
payments: &[(Self::Address, u64)]
payments: &[(Self::Address, u64)],
fee: Self::Fee
) -> Result<Self::SignableTransaction, CoinError>;
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
coin.mine_block(coin.address(<C::Curve as Curve>::G::generator())).await;
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 threshold = keys[&1].params().t();
let mut networks = LocalNetwork::new(threshold);
let mut wallets = vec![];
for i in 1 ..= 3 {
for i in 1 ..= threshold {
let mut wallet = Wallet::new(MemCoinDb::new(), coin.clone());
wallet.acknowledge_height(0, height);
wallet.add_keys(
@ -95,9 +96,12 @@ async fn test_send<C: Coin + Clone>(coin: C) {
wallet.acknowledge_height(1, height - 10);
let signable = wallet.prepare_sends(
1,
vec![(wallet.address(), 10000000000)]
vec![(wallet.address(), 10000000000)],
fee
).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!(
@ -108,5 +112,7 @@ async fn test_send<C: Coin + Clone>(coin: C) {
#[tokio::test]
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(
&mut self,
canonical: usize,
payments: Vec<(C::Address, u64)>
payments: Vec<(C::Address, u64)>,
fee: C::Fee
) -> Result<(Vec<(C::Address, u64)>, Vec<C::SignableTransaction>), CoinError> {
if payments.len() == 0 {
return Ok((vec![], vec![]));
@ -326,7 +327,8 @@ impl<D: CoinDb, C: Coin> Wallet<D, C> {
transcript,
acknowledged_height,
inputs,
&outputs
&outputs,
fee
).await?;
// self.db.save_tx(tx) // TODO
txs.push(tx);
@ -340,11 +342,11 @@ impl<D: CoinDb, C: Coin> Wallet<D, C> {
&mut self,
network: &mut N,
prepared: C::SignableTransaction,
included: &[u16]
included: Vec<u16>
) -> Result<(Vec<u8>, Vec<<C::Output as Output>::Id>), SignError> {
let mut attempt = self.coin.attempt_send(
prepared,
included
&included
).await.map_err(|e| SignError::CoinError(e))?;
let commitments = network.round(