mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Finish merging in the develop branch
This commit is contained in:
@@ -2,7 +2,7 @@ use core::marker::PhantomData;
|
||||
|
||||
use group::GroupEncoding;
|
||||
|
||||
use serai_primitives::{Coin, Amount, Balance};
|
||||
use serai_primitives::{ExternalCoin, Amount, ExternalBalance};
|
||||
|
||||
use borsh::BorshDeserialize;
|
||||
use serai_db::{Get, DbTxn, create_db, db_channel};
|
||||
@@ -13,31 +13,31 @@ use scanner::{ScannerFeed, KeyFor, AddressFor, OutputFor};
|
||||
|
||||
create_db! {
|
||||
UtxoScheduler {
|
||||
OperatingCosts: (coin: Coin) -> Amount,
|
||||
SerializedOutputs: (key: &[u8], coin: Coin) -> Vec<u8>,
|
||||
SerializedQueuedPayments: (key: &[u8], coin: Coin) -> Vec<u8>,
|
||||
OperatingCosts: (coin: ExternalCoin) -> Amount,
|
||||
SerializedOutputs: (key: &[u8], coin: ExternalCoin) -> Vec<u8>,
|
||||
SerializedQueuedPayments: (key: &[u8], coin: ExternalCoin) -> Vec<u8>,
|
||||
}
|
||||
}
|
||||
|
||||
db_channel! {
|
||||
UtxoScheduler {
|
||||
PendingBranch: (key: &[u8], balance: Balance) -> Vec<u8>,
|
||||
PendingBranch: (key: &[u8], balance: ExternalBalance) -> Vec<u8>,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Db<S: ScannerFeed>(PhantomData<S>);
|
||||
impl<S: ScannerFeed> Db<S> {
|
||||
pub(crate) fn operating_costs(getter: &impl Get, coin: Coin) -> Amount {
|
||||
pub(crate) fn operating_costs(getter: &impl Get, coin: ExternalCoin) -> Amount {
|
||||
OperatingCosts::get(getter, coin).unwrap_or(Amount(0))
|
||||
}
|
||||
pub(crate) fn set_operating_costs(txn: &mut impl DbTxn, coin: Coin, amount: Amount) {
|
||||
pub(crate) fn set_operating_costs(txn: &mut impl DbTxn, coin: ExternalCoin, amount: Amount) {
|
||||
OperatingCosts::set(txn, coin, &amount)
|
||||
}
|
||||
|
||||
pub(crate) fn outputs(
|
||||
getter: &impl Get,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
) -> Option<Vec<OutputFor<S>>> {
|
||||
let buf = SerializedOutputs::get(getter, key.to_bytes().as_ref(), coin)?;
|
||||
let mut buf = buf.as_slice();
|
||||
@@ -51,7 +51,7 @@ impl<S: ScannerFeed> Db<S> {
|
||||
pub(crate) fn set_outputs(
|
||||
txn: &mut impl DbTxn,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
outputs: &[OutputFor<S>],
|
||||
) {
|
||||
let mut buf = Vec::with_capacity(outputs.len() * 128);
|
||||
@@ -60,14 +60,14 @@ impl<S: ScannerFeed> Db<S> {
|
||||
}
|
||||
SerializedOutputs::set(txn, key.to_bytes().as_ref(), coin, &buf);
|
||||
}
|
||||
pub(crate) fn del_outputs(txn: &mut impl DbTxn, key: KeyFor<S>, coin: Coin) {
|
||||
pub(crate) fn del_outputs(txn: &mut impl DbTxn, key: KeyFor<S>, coin: ExternalCoin) {
|
||||
SerializedOutputs::del(txn, key.to_bytes().as_ref(), coin);
|
||||
}
|
||||
|
||||
pub(crate) fn queued_payments(
|
||||
getter: &impl Get,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
) -> Option<Vec<Payment<AddressFor<S>>>> {
|
||||
let buf = SerializedQueuedPayments::get(getter, key.to_bytes().as_ref(), coin)?;
|
||||
let mut buf = buf.as_slice();
|
||||
@@ -81,7 +81,7 @@ impl<S: ScannerFeed> Db<S> {
|
||||
pub(crate) fn set_queued_payments(
|
||||
txn: &mut impl DbTxn,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
queued: &[Payment<AddressFor<S>>],
|
||||
) {
|
||||
let mut buf = Vec::with_capacity(queued.len() * 128);
|
||||
@@ -90,14 +90,14 @@ impl<S: ScannerFeed> Db<S> {
|
||||
}
|
||||
SerializedQueuedPayments::set(txn, key.to_bytes().as_ref(), coin, &buf);
|
||||
}
|
||||
pub(crate) fn del_queued_payments(txn: &mut impl DbTxn, key: KeyFor<S>, coin: Coin) {
|
||||
pub(crate) fn del_queued_payments(txn: &mut impl DbTxn, key: KeyFor<S>, coin: ExternalCoin) {
|
||||
SerializedQueuedPayments::del(txn, key.to_bytes().as_ref(), coin);
|
||||
}
|
||||
|
||||
pub(crate) fn queue_pending_branch(
|
||||
txn: &mut impl DbTxn,
|
||||
key: KeyFor<S>,
|
||||
balance: Balance,
|
||||
balance: ExternalBalance,
|
||||
child: &TreeTransaction<AddressFor<S>>,
|
||||
) {
|
||||
PendingBranch::send(txn, key.to_bytes().as_ref(), balance, &borsh::to_vec(child).unwrap())
|
||||
@@ -105,7 +105,7 @@ impl<S: ScannerFeed> Db<S> {
|
||||
pub(crate) fn take_pending_branch(
|
||||
txn: &mut impl DbTxn,
|
||||
key: KeyFor<S>,
|
||||
balance: Balance,
|
||||
balance: ExternalBalance,
|
||||
) -> Option<TreeTransaction<AddressFor<S>>> {
|
||||
PendingBranch::try_recv(txn, key.to_bytes().as_ref(), balance)
|
||||
.map(|bytes| TreeTransaction::<AddressFor<S>>::deserialize(&mut bytes.as_slice()).unwrap())
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::collections::HashMap;
|
||||
|
||||
use group::GroupEncoding;
|
||||
|
||||
use serai_primitives::{Coin, Amount, Balance};
|
||||
use serai_primitives::{ExternalCoin, Amount, ExternalBalance};
|
||||
|
||||
use serai_db::DbTxn;
|
||||
|
||||
@@ -42,7 +42,7 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||
block: &BlockFor<S>,
|
||||
key_for_change: KeyFor<S>,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
) -> Result<Vec<EventualityFor<S>>, <Self as SchedulerTrait<S>>::EphemeralError> {
|
||||
let mut eventualities = vec![];
|
||||
|
||||
@@ -79,7 +79,7 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||
txn: &mut impl DbTxn,
|
||||
operating_costs: &mut u64,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
value_of_outputs: u64,
|
||||
) -> Vec<Payment<AddressFor<S>>> {
|
||||
// Fetch all payments for this key
|
||||
@@ -133,7 +133,7 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||
fn queue_branches(
|
||||
txn: &mut impl DbTxn,
|
||||
key: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
effected_payments: Vec<Amount>,
|
||||
tx: TreeTransaction<AddressFor<S>>,
|
||||
) {
|
||||
@@ -149,7 +149,7 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||
children thanks to our sort.
|
||||
*/
|
||||
for (amount, child) in effected_payments.into_iter().zip(children) {
|
||||
Db::<S>::queue_pending_branch(txn, key, Balance { coin, amount }, &child);
|
||||
Db::<S>::queue_pending_branch(txn, key, ExternalBalance { coin, amount }, &child);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,8 +216,6 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||
let branch_address = P::branch_address(key);
|
||||
|
||||
'coin: for coin in S::NETWORK.coins() {
|
||||
let coin = *coin;
|
||||
|
||||
// Perform any input aggregation we should
|
||||
eventualities
|
||||
.append(&mut self.aggregate_inputs(txn, block, key_for_change, key, coin).await?);
|
||||
@@ -308,7 +306,7 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> Scheduler<S, P> {
|
||||
block: &BlockFor<S>,
|
||||
from: KeyFor<S>,
|
||||
to: KeyFor<S>,
|
||||
coin: Coin,
|
||||
coin: ExternalCoin,
|
||||
) -> Result<(), <Self as SchedulerTrait<S>>::EphemeralError> {
|
||||
let from_bytes = from.to_bytes().as_ref().to_vec();
|
||||
// Ensure our inputs are aggregated
|
||||
@@ -349,10 +347,10 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Schedul
|
||||
|
||||
fn activate_key(txn: &mut impl DbTxn, key: KeyFor<S>) {
|
||||
for coin in S::NETWORK.coins() {
|
||||
assert!(Db::<S>::outputs(txn, key, *coin).is_none());
|
||||
Db::<S>::set_outputs(txn, key, *coin, &[]);
|
||||
assert!(Db::<S>::queued_payments(txn, key, *coin).is_none());
|
||||
Db::<S>::set_queued_payments(txn, key, *coin, &[]);
|
||||
assert!(Db::<S>::outputs(txn, key, coin).is_none());
|
||||
Db::<S>::set_outputs(txn, key, coin, &[]);
|
||||
assert!(Db::<S>::queued_payments(txn, key, coin).is_none());
|
||||
Db::<S>::set_queued_payments(txn, key, coin, &[]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,18 +366,18 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Schedul
|
||||
for coin in S::NETWORK.coins() {
|
||||
// Move the payments to the new key
|
||||
{
|
||||
let still_queued = Db::<S>::queued_payments(txn, retiring_key, *coin).unwrap();
|
||||
let mut new_queued = Db::<S>::queued_payments(txn, new_key, *coin).unwrap();
|
||||
let still_queued = Db::<S>::queued_payments(txn, retiring_key, coin).unwrap();
|
||||
let mut new_queued = Db::<S>::queued_payments(txn, new_key, coin).unwrap();
|
||||
|
||||
let mut queued = still_queued;
|
||||
queued.append(&mut new_queued);
|
||||
|
||||
Db::<S>::set_queued_payments(txn, retiring_key, *coin, &[]);
|
||||
Db::<S>::set_queued_payments(txn, new_key, *coin, &queued);
|
||||
Db::<S>::set_queued_payments(txn, retiring_key, coin, &[]);
|
||||
Db::<S>::set_queued_payments(txn, new_key, coin, &queued);
|
||||
}
|
||||
|
||||
// Move the outputs to the new key
|
||||
self.flush_outputs(txn, &mut eventualities, block, retiring_key, new_key, *coin).await?;
|
||||
self.flush_outputs(txn, &mut eventualities, block, retiring_key, new_key, coin).await?;
|
||||
}
|
||||
Ok(eventualities)
|
||||
}
|
||||
@@ -387,10 +385,10 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Schedul
|
||||
|
||||
fn retire_key(txn: &mut impl DbTxn, key: KeyFor<S>) {
|
||||
for coin in S::NETWORK.coins() {
|
||||
assert!(Db::<S>::outputs(txn, key, *coin).unwrap().is_empty());
|
||||
Db::<S>::del_outputs(txn, key, *coin);
|
||||
assert!(Db::<S>::queued_payments(txn, key, *coin).unwrap().is_empty());
|
||||
Db::<S>::del_queued_payments(txn, key, *coin);
|
||||
assert!(Db::<S>::outputs(txn, key, coin).unwrap().is_empty());
|
||||
Db::<S>::del_outputs(txn, key, coin);
|
||||
assert!(Db::<S>::queued_payments(txn, key, coin).unwrap().is_empty());
|
||||
Db::<S>::del_queued_payments(txn, key, coin);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -463,7 +461,7 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Schedul
|
||||
block,
|
||||
active_keys[0].0,
|
||||
active_keys[1].0,
|
||||
*coin,
|
||||
coin,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
@@ -552,10 +550,10 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, ()>> SchedulerTrait<S> for Schedul
|
||||
|
||||
// Queue the payments for this key
|
||||
for coin in S::NETWORK.coins() {
|
||||
let mut queued_payments = Db::<S>::queued_payments(txn, fulfillment_key, *coin).unwrap();
|
||||
let mut queued_payments = Db::<S>::queued_payments(txn, fulfillment_key, coin).unwrap();
|
||||
queued_payments
|
||||
.extend(payments.iter().filter(|payment| payment.balance().coin == *coin).cloned());
|
||||
Db::<S>::set_queued_payments(txn, fulfillment_key, *coin, &queued_payments);
|
||||
.extend(payments.iter().filter(|payment| payment.balance().coin == coin).cloned());
|
||||
Db::<S>::set_queued_payments(txn, fulfillment_key, coin, &queued_payments);
|
||||
}
|
||||
|
||||
// Handle the queued payments
|
||||
|
||||
Reference in New Issue
Block a user