Remove Output::amount and move Payment from Amount to Balance

This code is still largely designed around the idea a payment for a network is
fungible with any other, which isn't true. This starts moving past that.

Asserts are added to ensure the integrity of coin to the scheduler (which is
now per key per coin, not per key alone) and in Bitcoin/Monero prepare_send.
This commit is contained in:
Luke Parker
2023-11-08 23:33:25 -05:00
parent ffedba7a05
commit 7d72e224f0
10 changed files with 201 additions and 87 deletions

View File

@@ -8,6 +8,8 @@ use tokio::time::timeout;
use serai_db::{DbTxn, Db, MemDb};
use serai_client::primitives::{NetworkId, Coin, Amount, Balance};
use crate::{
Payment, Plan,
networks::{Output, Transaction, Block, Network},
@@ -64,12 +66,33 @@ pub async fn test_wallet<N: Network>(network: N) {
txn.commit();
let mut txn = db.txn();
let mut scheduler = Scheduler::new::<MemDb>(&mut txn, key);
let mut scheduler = Scheduler::new::<MemDb>(
&mut txn,
key,
match N::NETWORK {
NetworkId::Serai => panic!("test_wallet called with Serai"),
NetworkId::Bitcoin => Coin::Bitcoin,
NetworkId::Ethereum => Coin::Ether,
NetworkId::Monero => Coin::Monero,
},
);
let amount = 2 * N::DUST;
let plans = scheduler.schedule::<MemDb>(
&mut txn,
outputs.clone(),
vec![Payment { address: N::address(key), data: None, amount }],
vec![Payment {
address: N::address(key),
data: None,
balance: Balance {
coin: match N::NETWORK {
NetworkId::Serai => panic!("test_wallet called with Serai"),
NetworkId::Bitcoin => Coin::Bitcoin,
NetworkId::Ethereum => Coin::Ether,
NetworkId::Monero => Coin::Monero,
},
amount: Amount(amount),
},
}],
key,
false,
);
@@ -79,7 +102,19 @@ pub async fn test_wallet<N: Network>(network: N) {
vec![Plan {
key,
inputs: outputs.clone(),
payments: vec![Payment { address: N::address(key), data: None, amount }],
payments: vec![Payment {
address: N::address(key),
data: None,
balance: Balance {
coin: match N::NETWORK {
NetworkId::Serai => panic!("test_wallet called with Serai"),
NetworkId::Bitcoin => Coin::Bitcoin,
NetworkId::Ethereum => Coin::Ether,
NetworkId::Monero => Coin::Monero,
},
amount: Amount(amount),
}
}],
change: Some(N::change_address(key)),
}]
);
@@ -113,7 +148,7 @@ pub async fn test_wallet<N: Network>(network: N) {
let outputs = network.get_outputs(&block, key).await;
assert_eq!(outputs.len(), 2);
let amount = amount - tx.fee(&network).await;
assert!((outputs[0].amount() == amount) || (outputs[1].amount() == amount));
assert!((outputs[0].balance().amount.0 == amount) || (outputs[1].balance().amount.0 == amount));
for eventuality in eventualities {
assert!(network.confirm_completion(&eventuality, &tx));