mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
monero-serai: fee calculation parity with Monero's wallet2 (#301)
* monero-serai: fee calculation parity with Monero's wallet2 * Minor lint --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
@@ -14,8 +14,9 @@ use monero_serai::{
|
||||
wallet::{
|
||||
ViewPair, Scanner,
|
||||
address::{Network, AddressType, AddressSpec, AddressMeta, MoneroAddress},
|
||||
SpendableOutput,
|
||||
SpendableOutput, Fee,
|
||||
},
|
||||
transaction::Transaction,
|
||||
};
|
||||
|
||||
pub fn random_address() -> (Scalar, ViewPair, MoneroAddress) {
|
||||
@@ -72,6 +73,18 @@ pub async fn get_miner_tx_output(rpc: &Rpc<HttpRpc>, view: &ViewPair) -> Spendab
|
||||
scanner.scan(rpc, &block).await.unwrap().swap_remove(0).ignore_timelock().swap_remove(0)
|
||||
}
|
||||
|
||||
/// Make sure the weight and fee match the expected calculation.
|
||||
pub fn check_weight_and_fee(tx: &Transaction, fee_rate: Fee) {
|
||||
let fee = tx.rct_signatures.base.fee;
|
||||
|
||||
let weight = tx.weight();
|
||||
let expected_weight = fee_rate.calculate_weight_from_fee(fee);
|
||||
assert_eq!(weight, expected_weight);
|
||||
|
||||
let expected_fee = fee_rate.calculate_fee_from_weight(weight);
|
||||
assert_eq!(fee, expected_fee);
|
||||
}
|
||||
|
||||
pub async fn rpc() -> Rpc<HttpRpc> {
|
||||
let rpc = HttpRpc::new("http://127.0.0.1:18081".to_string()).unwrap();
|
||||
|
||||
@@ -154,12 +167,15 @@ macro_rules! test {
|
||||
use monero_serai::{
|
||||
random_scalar,
|
||||
wallet::{
|
||||
address::{Network, AddressSpec}, ViewPair, Scanner, Change, SignableTransaction,
|
||||
SignableTransactionBuilder,
|
||||
address::{Network, AddressSpec}, ViewPair, Scanner, Change, Decoys, FeePriority,
|
||||
SignableTransaction, SignableTransactionBuilder,
|
||||
},
|
||||
};
|
||||
|
||||
use runner::{random_address, rpc, mine_until_unlocked, get_miner_tx_output};
|
||||
use runner::{
|
||||
random_address, rpc, mine_until_unlocked, get_miner_tx_output,
|
||||
check_weight_and_fee,
|
||||
};
|
||||
|
||||
type Builder = SignableTransactionBuilder;
|
||||
|
||||
@@ -192,9 +208,11 @@ macro_rules! test {
|
||||
|
||||
let miner_tx = get_miner_tx_output(&rpc, &view).await;
|
||||
|
||||
let protocol = rpc.get_protocol().await.unwrap();
|
||||
|
||||
let builder = SignableTransactionBuilder::new(
|
||||
rpc.get_protocol().await.unwrap(),
|
||||
rpc.get_fee().await.unwrap(),
|
||||
protocol,
|
||||
rpc.get_fee(protocol, FeePriority::Low).await.unwrap(),
|
||||
Some(Change::new(
|
||||
&ViewPair::new(
|
||||
&random_scalar(&mut OsRng) * &ED25519_BASEPOINT_TABLE,
|
||||
@@ -205,13 +223,12 @@ macro_rules! test {
|
||||
);
|
||||
|
||||
let sign = |tx: SignableTransaction| {
|
||||
let rpc = rpc.clone();
|
||||
let spend = spend.clone();
|
||||
#[cfg(feature = "multisig")]
|
||||
let keys = keys.clone();
|
||||
async move {
|
||||
if !multisig {
|
||||
tx.sign(&mut OsRng, &rpc, &spend).await.unwrap()
|
||||
tx.sign(&mut OsRng, &spend).await.unwrap()
|
||||
} else {
|
||||
#[cfg(not(feature = "multisig"))]
|
||||
panic!("Multisig branch called without the multisig feature");
|
||||
@@ -224,12 +241,9 @@ macro_rules! test {
|
||||
tx
|
||||
.clone()
|
||||
.multisig(
|
||||
&rpc,
|
||||
keys[&i].clone(),
|
||||
RecommendedTranscript::new(b"Monero Serai Test Transaction"),
|
||||
rpc.get_height().await.unwrap() - 10,
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
@@ -245,13 +259,25 @@ macro_rules! test {
|
||||
|
||||
let temp = Box::new({
|
||||
let mut builder = builder.clone();
|
||||
builder.add_input(miner_tx);
|
||||
let (tx, state) = ($first_tx)(rpc.clone(), builder, next_addr).await;
|
||||
|
||||
let decoys = Decoys::select(
|
||||
&mut OsRng,
|
||||
&rpc,
|
||||
protocol.ring_len(),
|
||||
rpc.get_height().await.unwrap() - 1,
|
||||
&[miner_tx.clone()]
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
builder.add_input((miner_tx, decoys.first().unwrap().clone()));
|
||||
|
||||
let (tx, state) = ($first_tx)(rpc.clone(), builder, next_addr).await;
|
||||
let fee_rate = tx.fee_rate().clone();
|
||||
let signed = sign(tx).await;
|
||||
rpc.publish_transaction(&signed).await.unwrap();
|
||||
mine_until_unlocked(&rpc, &random_address().2.to_string(), signed.hash()).await;
|
||||
let tx = rpc.get_transaction(signed.hash()).await.unwrap();
|
||||
check_weight_and_fee(&tx, fee_rate);
|
||||
let scanner =
|
||||
Scanner::from_view(view.clone(), Some(HashSet::new()));
|
||||
($first_checks)(rpc.clone(), tx, scanner, state).await
|
||||
@@ -261,16 +287,18 @@ macro_rules! test {
|
||||
|
||||
$(
|
||||
let (tx, state) = ($tx)(
|
||||
protocol,
|
||||
rpc.clone(),
|
||||
builder.clone(),
|
||||
next_addr,
|
||||
*carried_state.downcast().unwrap()
|
||||
).await;
|
||||
|
||||
let fee_rate = tx.fee_rate().clone();
|
||||
let signed = sign(tx).await;
|
||||
rpc.publish_transaction(&signed).await.unwrap();
|
||||
mine_until_unlocked(&rpc, &random_address().2.to_string(), signed.hash()).await;
|
||||
let tx = rpc.get_transaction(signed.hash()).await.unwrap();
|
||||
check_weight_and_fee(&tx, fee_rate);
|
||||
#[allow(unused_assignments)]
|
||||
{
|
||||
let scanner =
|
||||
|
||||
@@ -1,11 +1,44 @@
|
||||
use rand_core::OsRng;
|
||||
|
||||
use monero_serai::{
|
||||
transaction::Transaction,
|
||||
wallet::{extra::Extra, address::SubaddressIndex, ReceivedOutput, SpendableOutput},
|
||||
rpc::Rpc,
|
||||
wallet::{
|
||||
extra::Extra, address::SubaddressIndex, ReceivedOutput, SpendableOutput, Decoys,
|
||||
SignableTransactionBuilder,
|
||||
},
|
||||
rpc::{Rpc, HttpRpc},
|
||||
Protocol,
|
||||
};
|
||||
|
||||
mod runner;
|
||||
|
||||
// Set up inputs, select decoys, then add them to the TX builder
|
||||
async fn add_inputs(
|
||||
protocol: Protocol,
|
||||
rpc: &Rpc<HttpRpc>,
|
||||
outputs: Vec<ReceivedOutput>,
|
||||
builder: &mut SignableTransactionBuilder,
|
||||
) {
|
||||
let mut spendable_outputs = Vec::with_capacity(outputs.len());
|
||||
for output in outputs {
|
||||
spendable_outputs.push(SpendableOutput::from(rpc, output).await.unwrap());
|
||||
}
|
||||
|
||||
let decoys = Decoys::select(
|
||||
&mut OsRng,
|
||||
rpc,
|
||||
protocol.ring_len(),
|
||||
rpc.get_height().await.unwrap() - 1,
|
||||
&spendable_outputs,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let inputs = spendable_outputs.into_iter().zip(decoys.into_iter()).collect::<Vec<_>>();
|
||||
|
||||
builder.add_inputs(&inputs);
|
||||
}
|
||||
|
||||
test!(
|
||||
spend_miner_output,
|
||||
(
|
||||
@@ -37,10 +70,8 @@ test!(
|
||||
},
|
||||
),
|
||||
(
|
||||
|rpc, mut builder: Builder, addr, outputs: Vec<ReceivedOutput>| async move {
|
||||
for output in outputs {
|
||||
builder.add_input(SpendableOutput::from(&rpc, output).await.unwrap());
|
||||
}
|
||||
|protocol: Protocol, rpc, mut builder: Builder, addr, outputs: Vec<ReceivedOutput>| async move {
|
||||
add_inputs(protocol, &rpc, outputs, &mut builder).await;
|
||||
builder.add_payment(addr, 6);
|
||||
(builder.build().unwrap(), ())
|
||||
},
|
||||
@@ -69,18 +100,20 @@ test!(
|
||||
},
|
||||
),
|
||||
(
|
||||
|rpc: Rpc<_>, _, _, mut outputs: Vec<ReceivedOutput>| async move {
|
||||
|protocol, rpc: Rpc<_>, _, _, outputs: Vec<ReceivedOutput>| async move {
|
||||
use monero_serai::wallet::FeePriority;
|
||||
|
||||
let change_view = ViewPair::new(
|
||||
&random_scalar(&mut OsRng) * &ED25519_BASEPOINT_TABLE,
|
||||
Zeroizing::new(random_scalar(&mut OsRng)),
|
||||
);
|
||||
|
||||
let mut builder = SignableTransactionBuilder::new(
|
||||
rpc.get_protocol().await.unwrap(),
|
||||
rpc.get_fee().await.unwrap(),
|
||||
protocol,
|
||||
rpc.get_fee(protocol, FeePriority::Low).await.unwrap(),
|
||||
Some(Change::new(&change_view, false)),
|
||||
);
|
||||
builder.add_input(SpendableOutput::from(&rpc, outputs.swap_remove(0)).await.unwrap());
|
||||
add_inputs(protocol, &rpc, vec![outputs.first().unwrap().clone()], &mut builder).await;
|
||||
|
||||
// Send to a subaddress
|
||||
let sub_view = ViewPair::new(
|
||||
@@ -116,3 +149,127 @@ test!(
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
test!(
|
||||
spend_one_input_to_one_output_plus_change,
|
||||
(
|
||||
|_, mut builder: Builder, addr| async move {
|
||||
builder.add_payment(addr, 2000000000000);
|
||||
(builder.build().unwrap(), ())
|
||||
},
|
||||
|_, tx: Transaction, mut scanner: Scanner, _| async move {
|
||||
let mut outputs = scanner.scan_transaction(&tx).not_locked();
|
||||
outputs.sort_by(|x, y| x.commitment().amount.cmp(&y.commitment().amount));
|
||||
assert_eq!(outputs[0].commitment().amount, 2000000000000);
|
||||
outputs
|
||||
},
|
||||
),
|
||||
(
|
||||
|protocol: Protocol, rpc, mut builder: Builder, addr, outputs: Vec<ReceivedOutput>| async move {
|
||||
add_inputs(protocol, &rpc, outputs, &mut builder).await;
|
||||
builder.add_payment(addr, 2);
|
||||
(builder.build().unwrap(), ())
|
||||
},
|
||||
|_, tx: Transaction, mut scanner: Scanner, _| async move {
|
||||
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
|
||||
assert_eq!(output.commitment().amount, 2);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
test!(
|
||||
spend_max_outputs,
|
||||
(
|
||||
|_, mut builder: Builder, addr| async move {
|
||||
builder.add_payment(addr, 1000000000000);
|
||||
(builder.build().unwrap(), ())
|
||||
},
|
||||
|_, tx: Transaction, mut scanner: Scanner, _| async move {
|
||||
let mut outputs = scanner.scan_transaction(&tx).not_locked();
|
||||
outputs.sort_by(|x, y| x.commitment().amount.cmp(&y.commitment().amount));
|
||||
assert_eq!(outputs[0].commitment().amount, 1000000000000);
|
||||
outputs
|
||||
},
|
||||
),
|
||||
(
|
||||
|protocol: Protocol, rpc, mut builder: Builder, addr, outputs: Vec<ReceivedOutput>| async move {
|
||||
add_inputs(protocol, &rpc, outputs, &mut builder).await;
|
||||
|
||||
for i in 0 .. 15 {
|
||||
builder.add_payment(addr, i + 1);
|
||||
}
|
||||
(builder.build().unwrap(), ())
|
||||
},
|
||||
|_, tx: Transaction, mut scanner: Scanner, _| async move {
|
||||
let mut scanned_tx = scanner.scan_transaction(&tx).not_locked();
|
||||
|
||||
let mut output_amounts = HashSet::new();
|
||||
for i in 0 .. 15 {
|
||||
output_amounts.insert((i + 1) as u64);
|
||||
}
|
||||
for _ in 0 .. 15 {
|
||||
let output = scanned_tx.swap_remove(0);
|
||||
let amount = output.commitment().amount;
|
||||
assert!(output_amounts.contains(&amount));
|
||||
output_amounts.remove(&amount);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
test!(
|
||||
spend_max_outputs_to_subaddresses,
|
||||
(
|
||||
|_, mut builder: Builder, addr| async move {
|
||||
builder.add_payment(addr, 1000000000000);
|
||||
(builder.build().unwrap(), ())
|
||||
},
|
||||
|_, tx: Transaction, mut scanner: Scanner, _| async move {
|
||||
let mut outputs = scanner.scan_transaction(&tx).not_locked();
|
||||
outputs.sort_by(|x, y| x.commitment().amount.cmp(&y.commitment().amount));
|
||||
assert_eq!(outputs[0].commitment().amount, 1000000000000);
|
||||
outputs
|
||||
},
|
||||
),
|
||||
(
|
||||
|protocol: Protocol, rpc, mut builder: Builder, _, outputs: Vec<ReceivedOutput>| async move {
|
||||
add_inputs(protocol, &rpc, outputs, &mut builder).await;
|
||||
|
||||
let view = runner::random_address().1;
|
||||
let mut scanner = Scanner::from_view(view.clone(), Some(HashSet::new()));
|
||||
|
||||
let mut subaddresses = vec![];
|
||||
for i in 0 .. 15 {
|
||||
let subaddress = SubaddressIndex::new(0, i + 1).unwrap();
|
||||
scanner.register_subaddress(subaddress);
|
||||
|
||||
builder.add_payment(
|
||||
view.address(Network::Mainnet, AddressSpec::Subaddress(subaddress)),
|
||||
(i + 1) as u64,
|
||||
);
|
||||
subaddresses.push(subaddress);
|
||||
}
|
||||
|
||||
(builder.build().unwrap(), (scanner, subaddresses))
|
||||
},
|
||||
|_, tx: Transaction, _, mut state: (Scanner, Vec<SubaddressIndex>)| async move {
|
||||
use std::collections::HashMap;
|
||||
|
||||
let mut scanned_tx = state.0.scan_transaction(&tx).not_locked();
|
||||
|
||||
let mut output_amounts_by_subaddress = HashMap::new();
|
||||
for i in 0 .. 15 {
|
||||
output_amounts_by_subaddress.insert((i + 1) as u64, state.1[i]);
|
||||
}
|
||||
for _ in 0 .. 15 {
|
||||
let output = scanned_tx.swap_remove(0);
|
||||
let amount = output.commitment().amount;
|
||||
|
||||
assert!(output_amounts_by_subaddress.contains_key(&amount));
|
||||
assert_eq!(output.metadata.subaddress, Some(output_amounts_by_subaddress[&amount]));
|
||||
|
||||
output_amounts_by_subaddress.remove(&amount);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -84,19 +84,30 @@ async fn from_wallet_rpc_to_self(spec: AddressSpec) {
|
||||
.unwrap();
|
||||
let tx_hash: [u8; 32] = tx.tx_hash.0.try_into().unwrap();
|
||||
|
||||
// TODO: Needs https://github.com/monero-project/monero/pull/8882
|
||||
// let fee_rate = daemon_rpc
|
||||
// // Uses `FeePriority::Low` instead of `FeePriority::Lowest` because wallet RPC
|
||||
// // adjusts `monero_rpc::TransferPriority::Default` up by 1
|
||||
// .get_fee(daemon_rpc.get_protocol().await.unwrap(), FeePriority::Low)
|
||||
// .await
|
||||
// .unwrap();
|
||||
|
||||
// unlock it
|
||||
runner::mine_until_unlocked(&daemon_rpc, &wallet_rpc_addr.to_string(), tx_hash).await;
|
||||
|
||||
// create the scanner
|
||||
// Create the scanner
|
||||
let mut scanner = Scanner::from_view(view_pair, Some(HashSet::new()));
|
||||
if let AddressSpec::Subaddress(index) = spec {
|
||||
scanner.register_subaddress(index);
|
||||
}
|
||||
|
||||
// retrieve it and confirm
|
||||
// Retrieve it and scan it
|
||||
let tx = daemon_rpc.get_transaction(tx_hash).await.unwrap();
|
||||
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
|
||||
|
||||
// TODO: Needs https://github.com/monero-project/monero/pull/8882
|
||||
// runner::check_weight_and_fee(&tx, fee_rate);
|
||||
|
||||
match spec {
|
||||
AddressSpec::Subaddress(index) => assert_eq!(output.metadata.subaddress, Some(index)),
|
||||
AddressSpec::Integrated(payment_id) => {
|
||||
|
||||
Reference in New Issue
Block a user