Document the RPC

This commit is contained in:
Luke Parker
2024-06-16 19:59:25 -04:00
parent d740bd2924
commit 08b95abdd8
20 changed files with 289 additions and 197 deletions

View File

@@ -10,7 +10,7 @@ use rand_distr::num_traits::Float;
use curve25519_dalek::edwards::EdwardsPoint;
use monero_serai::{DEFAULT_LOCK_WINDOW, COINBASE_LOCK_WINDOW, BLOCK_TIME};
use monero_rpc::{RpcError, RpcConnection, Rpc};
use monero_rpc::{RpcError, Rpc};
use crate::SpendableOutput;
const RECENT_WINDOW: usize = 15;
@@ -19,9 +19,9 @@ const BLOCKS_PER_YEAR: usize = 365 * 24 * 60 * 60 / BLOCK_TIME;
const TIP_APPLICATION: f64 = (DEFAULT_LOCK_WINDOW * BLOCK_TIME) as f64;
#[allow(clippy::too_many_arguments)]
async fn select_n<'a, R: RngCore + CryptoRng, RPC: RpcConnection>(
async fn select_n<'a, R: RngCore + CryptoRng>(
rng: &mut R,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
distribution: &[u64],
height: usize,
high: u64,
@@ -129,9 +129,9 @@ fn offset(ring: &[u64]) -> Vec<u64> {
res
}
async fn select_decoys<R: RngCore + CryptoRng, RPC: RpcConnection>(
async fn select_decoys<R: RngCore + CryptoRng>(
rng: &mut R,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
ring_len: usize,
height: usize,
inputs: &[SpendableOutput],
@@ -278,20 +278,17 @@ pub use monero_serai::primitives::Decoys;
#[cfg(feature = "std")]
#[async_trait::async_trait]
pub trait DecoySelection {
async fn select<R: Send + Sync + RngCore + CryptoRng, RPC: Send + Sync + RpcConnection>(
async fn select<R: Send + Sync + RngCore + CryptoRng>(
rng: &mut R,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
ring_len: usize,
height: usize,
inputs: &[SpendableOutput],
) -> Result<Vec<Decoys>, RpcError>;
async fn fingerprintable_canonical_select<
R: Send + Sync + RngCore + CryptoRng,
RPC: Send + Sync + RpcConnection,
>(
async fn fingerprintable_canonical_select<R: Send + Sync + RngCore + CryptoRng>(
rng: &mut R,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
ring_len: usize,
height: usize,
inputs: &[SpendableOutput],
@@ -303,9 +300,9 @@ pub trait DecoySelection {
impl DecoySelection for Decoys {
/// Select decoys using the same distribution as Monero. Relies on the monerod RPC
/// response for an output's unlocked status, minimizing trips to the daemon.
async fn select<R: Send + Sync + RngCore + CryptoRng, RPC: Send + Sync + RpcConnection>(
async fn select<R: Send + Sync + RngCore + CryptoRng>(
rng: &mut R,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
ring_len: usize,
height: usize,
inputs: &[SpendableOutput],
@@ -321,12 +318,9 @@ impl DecoySelection for Decoys {
///
/// TODO: upstream change to monerod get_outs RPC to accept a height param for checking
/// output's unlocked status and remove all usage of fingerprintable_canonical
async fn fingerprintable_canonical_select<
R: Send + Sync + RngCore + CryptoRng,
RPC: Send + Sync + RpcConnection,
>(
async fn fingerprintable_canonical_select<R: Send + Sync + RngCore + CryptoRng>(
rng: &mut R,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
ring_len: usize,
height: usize,
inputs: &[SpendableOutput],

View File

@@ -47,7 +47,7 @@ pub mod decoys {
pub use decoys::{DecoySelection, Decoys};
mod send;
pub use send::{FeePriority, Fee, TransactionError, Change, SignableTransaction, Eventuality};
pub use send::{FeePriority, FeeRate, TransactionError, Change, SignableTransaction, Eventuality};
#[cfg(feature = "std")]
pub use send::SignableTransactionBuilder;
#[cfg(feature = "multisig")]

View File

@@ -9,7 +9,7 @@ use zeroize::{Zeroize, ZeroizeOnDrop};
use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar, edwards::EdwardsPoint};
use monero_rpc::{RpcError, RpcConnection, Rpc};
use monero_rpc::{RpcError, Rpc};
use monero_serai::{
io::*,
primitives::Commitment,
@@ -240,10 +240,7 @@ pub struct SpendableOutput {
impl SpendableOutput {
/// Update the spendable output's global index. This is intended to be called if a
/// re-organization occurred.
pub async fn refresh_global_index<RPC: RpcConnection>(
&mut self,
rpc: &Rpc<RPC>,
) -> Result<(), RpcError> {
pub async fn refresh_global_index(&mut self, rpc: &impl Rpc) -> Result<(), RpcError> {
self.global_index = *rpc
.get_o_indexes(self.output.absolute.tx)
.await?
@@ -254,10 +251,7 @@ impl SpendableOutput {
Ok(())
}
pub async fn from<RPC: RpcConnection>(
rpc: &Rpc<RPC>,
output: ReceivedOutput,
) -> Result<SpendableOutput, RpcError> {
pub async fn from(rpc: &impl Rpc, output: ReceivedOutput) -> Result<SpendableOutput, RpcError> {
let mut output = SpendableOutput { output, global_index: 0 };
output.refresh_global_index(rpc).await?;
Ok(output)
@@ -466,9 +460,9 @@ impl Scanner {
/// transactions is a dead giveaway for which transactions you successfully scanned. This
/// function obtains the output indexes for the miner transaction, incrementing from there
/// instead.
pub async fn scan<RPC: RpcConnection>(
pub async fn scan(
&mut self,
rpc: &Rpc<RPC>,
rpc: &impl Rpc,
block: &Block,
) -> Result<Vec<Timelocked<SpendableOutput>>, RpcError> {
let mut index = rpc.get_o_indexes(block.miner_tx.hash()).await?[0];

View File

@@ -4,14 +4,14 @@ use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
use monero_serai::Protocol;
use crate::{
address::MoneroAddress, Fee, SpendableOutput, Change, Decoys, SignableTransaction,
address::MoneroAddress, FeeRate, SpendableOutput, Change, Decoys, SignableTransaction,
TransactionError, extra::MAX_ARBITRARY_DATA_SIZE,
};
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, ZeroizeOnDrop)]
struct SignableTransactionBuilderInternal {
protocol: Protocol,
fee_rate: Fee,
fee_rate: FeeRate,
r_seed: Option<Zeroizing<[u8; 32]>>,
inputs: Vec<(SpendableOutput, Decoys)>,
@@ -23,7 +23,7 @@ struct SignableTransactionBuilderInternal {
impl SignableTransactionBuilderInternal {
// Takes in the change address so users don't miss that they have to manually set one
// If they don't, all leftover funds will become part of the fee
fn new(protocol: Protocol, fee_rate: Fee, change_address: Change) -> Self {
fn new(protocol: Protocol, fee_rate: FeeRate, change_address: Change) -> Self {
Self {
protocol,
fee_rate,
@@ -88,7 +88,7 @@ impl SignableTransactionBuilder {
Self(self.0.clone())
}
pub fn new(protocol: Protocol, fee_rate: Fee, change_address: Change) -> Self {
pub fn new(protocol: Protocol, fee_rate: FeeRate, change_address: Change) -> Self {
Self(Arc::new(RwLock::new(SignableTransactionBuilderInternal::new(
protocol,
fee_rate,

View File

@@ -22,7 +22,7 @@ use curve25519_dalek::{
use frost::FrostError;
use monero_rpc::RpcError;
pub use monero_rpc::{Fee, FeePriority};
pub use monero_rpc::{FeePriority, FeeRate};
use monero_serai::{
io::*,
primitives::{Commitment, keccak256},
@@ -216,7 +216,7 @@ fn calculate_weight_and_fee(
decoy_weights: &[usize],
n_outputs: usize,
extra: usize,
fee_rate: Fee,
fee_rate: FeeRate,
) -> (usize, u64) {
// Starting the fee at 0 here is different than core Monero's wallet2.cpp, which starts its fee
// calculation with an estimate.
@@ -291,7 +291,7 @@ pub struct SignableTransaction {
payments: Vec<InternalPayment>,
data: Vec<Vec<u8>>,
fee: u64,
fee_rate: Fee,
fee_rate: FeeRate,
}
/// Specification for a change output.
@@ -398,7 +398,7 @@ impl SignableTransaction {
payments: Vec<(MoneroAddress, u64)>,
change: &Change,
data: Vec<Vec<u8>>,
fee_rate: Fee,
fee_rate: FeeRate,
) -> Result<SignableTransaction, TransactionError> {
// Make sure there's only one payment ID
let mut has_payment_id = {
@@ -559,7 +559,7 @@ impl SignableTransaction {
self.fee
}
pub fn fee_rate(&self) -> Fee {
pub fn fee_rate(&self) -> FeeRate {
self.fee_rate
}

View File

@@ -1,5 +1,5 @@
use monero_serai::transaction::Transaction;
use monero_wallet::{TransactionError, extra::MAX_ARBITRARY_DATA_SIZE};
use monero_wallet::{rpc::Rpc, TransactionError, extra::MAX_ARBITRARY_DATA_SIZE};
mod runner;

View File

@@ -1,6 +1,9 @@
use monero_rpc::{Rpc, OutputResponse};
use monero_serai::{transaction::Transaction, Protocol, DEFAULT_LOCK_WINDOW};
use monero_wallet::SpendableOutput;
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{
monero::{transaction::Transaction, Protocol, DEFAULT_LOCK_WINDOW},
rpc::{OutputResponse, Rpc},
SpendableOutput,
};
mod runner;
@@ -12,7 +15,7 @@ test!(
builder.add_payment(addr, 2000000000000);
(builder.build().unwrap(), ())
},
|rpc: Rpc<_>, tx: Transaction, mut scanner: Scanner, ()| async move {
|rpc: SimpleRequestRpc, tx: Transaction, mut scanner: Scanner, ()| async move {
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
assert_eq!(output.commitment().amount, 2000000000000);
SpendableOutput::from(&rpc, output).await.unwrap()
@@ -20,7 +23,7 @@ test!(
),
(
// Then make a second tx1
|protocol: Protocol, rpc: Rpc<_>, mut builder: Builder, addr, state: _| async move {
|protocol: Protocol, rpc: SimpleRequestRpc, mut builder: Builder, addr, state: _| async move {
let output_tx0: SpendableOutput = state;
let decoys = Decoys::fingerprintable_canonical_select(
&mut OsRng,
@@ -39,7 +42,7 @@ test!(
(builder.build().unwrap(), (protocol, output_tx0))
},
// Then make sure DSA selects freshly unlocked output from tx1 as a decoy
|rpc: Rpc<_>, tx: Transaction, mut scanner: Scanner, state: (_, _)| async move {
|rpc: SimpleRequestRpc, tx: Transaction, mut scanner: Scanner, state: (_, _)| async move {
use rand_core::OsRng;
let height = rpc.get_height().await.unwrap();
@@ -89,7 +92,7 @@ test!(
builder.add_payment(addr, 2000000000000);
(builder.build().unwrap(), ())
},
|rpc: Rpc<_>, tx: Transaction, mut scanner: Scanner, ()| async move {
|rpc: SimpleRequestRpc, tx: Transaction, mut scanner: Scanner, ()| async move {
let output = scanner.scan_transaction(&tx).not_locked().swap_remove(0);
assert_eq!(output.commitment().amount, 2000000000000);
SpendableOutput::from(&rpc, output).await.unwrap()
@@ -97,7 +100,7 @@ test!(
),
(
// Then make a second tx1
|protocol: Protocol, rpc: Rpc<_>, mut builder: Builder, addr, state: _| async move {
|protocol: Protocol, rpc: SimpleRequestRpc, mut builder: Builder, addr, state: _| async move {
let output_tx0: SpendableOutput = state;
let decoys = Decoys::select(
&mut OsRng,
@@ -116,7 +119,7 @@ test!(
(builder.build().unwrap(), (protocol, output_tx0))
},
// Then make sure DSA selects freshly unlocked output from tx1 as a decoy
|rpc: Rpc<_>, tx: Transaction, mut scanner: Scanner, state: (_, _)| async move {
|rpc: SimpleRequestRpc, tx: Transaction, mut scanner: Scanner, state: (_, _)| async move {
use rand_core::OsRng;
let height = rpc.get_height().await.unwrap();

View File

@@ -2,6 +2,7 @@ use curve25519_dalek::constants::ED25519_BASEPOINT_POINT;
use monero_serai::transaction::Transaction;
use monero_wallet::{
rpc::Rpc,
Eventuality,
address::{AddressType, AddressMeta, MoneroAddress},
};

View File

@@ -8,13 +8,13 @@ use curve25519_dalek::{constants::ED25519_BASEPOINT_TABLE, scalar::Scalar};
use tokio::sync::Mutex;
use monero_rpc::Rpc;
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_serai::{transaction::Transaction, DEFAULT_LOCK_WINDOW};
use monero_wallet::{
monero::transaction::Transaction,
rpc::Rpc,
ViewPair, Scanner,
address::{Network, AddressType, AddressSpec, AddressMeta, MoneroAddress},
SpendableOutput, Fee,
SpendableOutput, FeeRate,
};
pub fn random_address() -> (Scalar, ViewPair, MoneroAddress) {
@@ -34,7 +34,7 @@ pub fn random_address() -> (Scalar, ViewPair, MoneroAddress) {
// TODO: Support transactions already on-chain
// TODO: Don't have a side effect of mining blocks more blocks than needed under race conditions
pub async fn mine_until_unlocked(rpc: &Rpc<SimpleRequestRpc>, addr: &str, tx_hash: [u8; 32]) {
pub async fn mine_until_unlocked(rpc: &SimpleRequestRpc, addr: &str, tx_hash: [u8; 32]) {
// mine until tx is in a block
let mut height = rpc.get_height().await.unwrap();
let mut found = false;
@@ -52,11 +52,11 @@ pub async fn mine_until_unlocked(rpc: &Rpc<SimpleRequestRpc>, addr: &str, tx_has
// Mine until tx's outputs are unlocked
let o_indexes: Vec<u64> = rpc.get_o_indexes(tx_hash).await.unwrap();
while rpc
.get_outs(&o_indexes)
.get_unlocked_outputs(&o_indexes, height, false)
.await
.unwrap()
.into_iter()
.all(|o| (!(o.unlocked && height >= (o.height + DEFAULT_LOCK_WINDOW))))
.all(|output| output.is_some())
{
height = rpc.generate_blocks(addr, 1).await.unwrap().1 + 1;
}
@@ -64,7 +64,7 @@ pub async fn mine_until_unlocked(rpc: &Rpc<SimpleRequestRpc>, addr: &str, tx_has
// Mines 60 blocks and returns an unlocked miner TX output.
#[allow(dead_code)]
pub async fn get_miner_tx_output(rpc: &Rpc<SimpleRequestRpc>, view: &ViewPair) -> SpendableOutput {
pub async fn get_miner_tx_output(rpc: &SimpleRequestRpc, view: &ViewPair) -> SpendableOutput {
let mut scanner = Scanner::from_view(view.clone(), Some(HashSet::new()));
// Mine 60 blocks to unlock a miner TX
@@ -79,7 +79,7 @@ pub async fn get_miner_tx_output(rpc: &Rpc<SimpleRequestRpc>, view: &ViewPair) -
}
/// Make sure the weight and fee match the expected calculation.
pub fn check_weight_and_fee(tx: &Transaction, fee_rate: Fee) {
pub fn check_weight_and_fee(tx: &Transaction, fee_rate: FeeRate) {
let fee = tx.rct_signatures.base.fee;
let weight = tx.weight();
@@ -90,7 +90,7 @@ pub fn check_weight_and_fee(tx: &Transaction, fee_rate: Fee) {
assert_eq!(fee, expected_fee);
}
pub async fn rpc() -> Rpc<SimpleRequestRpc> {
pub async fn rpc() -> SimpleRequestRpc {
let rpc =
SimpleRequestRpc::new("http://serai:seraidex@127.0.0.1:18081".to_string()).await.unwrap();
@@ -216,7 +216,7 @@ macro_rules! test {
let builder = SignableTransactionBuilder::new(
protocol,
rpc.get_fee(protocol, FeePriority::Unimportant).await.unwrap(),
rpc.get_fee_rate(protocol, FeePriority::Unimportant).await.unwrap(),
Change::new(
&ViewPair::new(
&Scalar::random(&mut OsRng) * ED25519_BASEPOINT_TABLE,

View File

@@ -1,7 +1,7 @@
use rand_core::RngCore;
use monero_serai::transaction::Transaction;
use monero_wallet::{address::SubaddressIndex, extra::PaymentId};
use monero_wallet::{rpc::Rpc, address::SubaddressIndex, extra::PaymentId};
mod runner;

View File

@@ -1,11 +1,12 @@
use rand_core::OsRng;
use monero_serai::{transaction::Transaction, Protocol};
use monero_rpc::Rpc;
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{
extra::Extra, address::SubaddressIndex, ReceivedOutput, SpendableOutput, DecoySelection, Decoys,
SignableTransactionBuilder,
monero::{transaction::Transaction, Protocol},
rpc::Rpc,
extra::Extra,
address::SubaddressIndex,
ReceivedOutput, SpendableOutput, DecoySelection, Decoys, SignableTransactionBuilder,
};
mod runner;
@@ -13,7 +14,7 @@ mod runner;
// Set up inputs, select decoys, then add them to the TX builder
async fn add_inputs(
protocol: Protocol,
rpc: &Rpc<SimpleRequestRpc>,
rpc: &SimpleRequestRpc,
outputs: Vec<ReceivedOutput>,
builder: &mut SignableTransactionBuilder,
) {
@@ -98,7 +99,7 @@ test!(
},
),
(
|protocol, rpc: Rpc<_>, _, _, outputs: Vec<ReceivedOutput>| async move {
|protocol, rpc: SimpleRequestRpc, _, _, outputs: Vec<ReceivedOutput>| async move {
use monero_wallet::FeePriority;
let change_view = ViewPair::new(
@@ -108,7 +109,7 @@ test!(
let mut builder = SignableTransactionBuilder::new(
protocol,
rpc.get_fee(protocol, FeePriority::Unimportant).await.unwrap(),
rpc.get_fee_rate(protocol, FeePriority::Unimportant).await.unwrap(),
Change::new(&change_view, false),
);
add_inputs(protocol, &rpc, vec![outputs.first().unwrap().clone()], &mut builder).await;
@@ -287,12 +288,12 @@ test!(
},
),
(
|protocol, rpc: Rpc<_>, _, addr, outputs: Vec<ReceivedOutput>| async move {
|protocol, rpc: SimpleRequestRpc, _, addr, outputs: Vec<ReceivedOutput>| async move {
use monero_wallet::FeePriority;
let mut builder = SignableTransactionBuilder::new(
protocol,
rpc.get_fee(protocol, FeePriority::Unimportant).await.unwrap(),
rpc.get_fee_rate(protocol, FeePriority::Unimportant).await.unwrap(),
Change::fingerprintable(None),
);
add_inputs(protocol, &rpc, vec![outputs.first().unwrap().clone()], &mut builder).await;

View File

@@ -5,10 +5,10 @@ use rand_core::{OsRng, RngCore};
use serde::Deserialize;
use serde_json::json;
use monero_serai::transaction::Transaction;
use monero_rpc::{EmptyResponse, Rpc};
use monero_simple_request_rpc::SimpleRequestRpc;
use monero_wallet::{
monero::transaction::Transaction,
rpc::Rpc,
address::{Network, AddressSpec, SubaddressIndex, MoneroAddress},
extra::{MAX_TX_EXTRA_NONCE_SIZE, Extra, PaymentId},
Scanner,
@@ -16,7 +16,10 @@ use monero_wallet::{
mod runner;
async fn make_integrated_address(rpc: &Rpc<SimpleRequestRpc>, payment_id: [u8; 8]) -> String {
#[derive(Deserialize, Debug)]
struct EmptyResponse {}
async fn make_integrated_address(rpc: &SimpleRequestRpc, payment_id: [u8; 8]) -> String {
#[derive(Debug, Deserialize)]
struct IntegratedAddressResponse {
integrated_address: String,
@@ -33,7 +36,7 @@ async fn make_integrated_address(rpc: &Rpc<SimpleRequestRpc>, payment_id: [u8; 8
res.integrated_address
}
async fn initialize_rpcs() -> (Rpc<SimpleRequestRpc>, Rpc<SimpleRequestRpc>, String) {
async fn initialize_rpcs() -> (SimpleRequestRpc, SimpleRequestRpc, String) {
let wallet_rpc = SimpleRequestRpc::new("http://127.0.0.1:18082".to_string()).await.unwrap();
let daemon_rpc = runner::rpc().await;
@@ -89,7 +92,7 @@ async fn from_wallet_rpc_to_self(spec: AddressSpec) {
// TODO: Needs https://github.com/monero-project/monero/pull/9260
// let fee_rate = daemon_rpc
// .get_fee(daemon_rpc.get_protocol().await.unwrap(), FeePriority::Unimportant)
// .get_fee_rate(daemon_rpc.get_protocol().await.unwrap(), FeePriority::Unimportant)
// .await
// .unwrap();
@@ -173,7 +176,7 @@ test!(
.add_payment(MoneroAddress::from_str(Network::Mainnet, &wallet_rpc_addr).unwrap(), 1000000);
(builder.build().unwrap(), wallet_rpc)
},
|_, tx: Transaction, _, data: Rpc<SimpleRequestRpc>| async move {
|_, tx: Transaction, _, data: SimpleRequestRpc| async move {
// confirm receipt
let _: EmptyResponse = data.json_rpc_call("refresh", None).await.unwrap();
let transfer: TransfersResponse = data
@@ -207,7 +210,7 @@ test!(
.add_payment(MoneroAddress::from_str(Network::Mainnet, &addr.address).unwrap(), 1000000);
(builder.build().unwrap(), (wallet_rpc, addr.account_index))
},
|_, tx: Transaction, _, data: (Rpc<SimpleRequestRpc>, u32)| async move {
|_, tx: Transaction, _, data: (SimpleRequestRpc, u32)| async move {
// confirm receipt
let _: EmptyResponse = data.0.json_rpc_call("refresh", None).await.unwrap();
let transfer: TransfersResponse = data
@@ -259,7 +262,7 @@ test!(
]);
(builder.build().unwrap(), (wallet_rpc, daemon_rpc, addrs.address_index))
},
|_, tx: Transaction, _, data: (Rpc<SimpleRequestRpc>, Rpc<SimpleRequestRpc>, u32)| async move {
|_, tx: Transaction, _, data: (SimpleRequestRpc, SimpleRequestRpc, u32)| async move {
// confirm receipt
let _: EmptyResponse = data.0.json_rpc_call("refresh", None).await.unwrap();
let transfer: TransfersResponse = data
@@ -304,7 +307,7 @@ test!(
builder.add_payment(MoneroAddress::from_str(Network::Mainnet, &addr).unwrap(), 1000000);
(builder.build().unwrap(), (wallet_rpc, payment_id))
},
|_, tx: Transaction, _, data: (Rpc<SimpleRequestRpc>, [u8; 8])| async move {
|_, tx: Transaction, _, data: (SimpleRequestRpc, [u8; 8])| async move {
// confirm receipt
let _: EmptyResponse = data.0.json_rpc_call("refresh", None).await.unwrap();
let transfer: TransfersResponse = data
@@ -339,7 +342,7 @@ test!(
(builder.build().unwrap(), wallet_rpc)
},
|_, tx: Transaction, _, data: Rpc<SimpleRequestRpc>| async move {
|_, tx: Transaction, _, data: SimpleRequestRpc| async move {
// confirm receipt
let _: EmptyResponse = data.json_rpc_call("refresh", None).await.unwrap();
let transfer: TransfersResponse = data