mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-22 02:34:55 +00:00
Misc tidying of serai-db calls
This commit is contained in:
parent
59fa49f750
commit
9b8c8f8231
6 changed files with 46 additions and 49 deletions
|
@ -47,9 +47,13 @@ macro_rules! create_db {
|
||||||
}) => {
|
}) => {
|
||||||
$(
|
$(
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) struct $field_name;
|
pub(crate) struct $field_name$(
|
||||||
impl $field_name {
|
<$($generic_name: $generic_type),+>
|
||||||
pub(crate) fn key$(<$($generic_name: $generic_type),+>)?($($arg: $arg_type),*) -> Vec<u8> {
|
)?$(
|
||||||
|
(core::marker::PhantomData<($($generic_name),+)>)
|
||||||
|
)?;
|
||||||
|
impl$(<$($generic_name: $generic_type),+>)? $field_name$(<$($generic_name),+>)? {
|
||||||
|
pub(crate) fn key($($arg: $arg_type),*) -> Vec<u8> {
|
||||||
use scale::Encode;
|
use scale::Encode;
|
||||||
$crate::serai_db_key(
|
$crate::serai_db_key(
|
||||||
stringify!($db_name).as_bytes(),
|
stringify!($db_name).as_bytes(),
|
||||||
|
@ -57,38 +61,38 @@ macro_rules! create_db {
|
||||||
($($arg),*).encode()
|
($($arg),*).encode()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
pub(crate) fn set$(<$($generic_name: $generic_type),+>)?(
|
pub(crate) fn set(
|
||||||
txn: &mut impl DbTxn
|
txn: &mut impl DbTxn
|
||||||
$(, $arg: $arg_type)*,
|
$(, $arg: $arg_type)*,
|
||||||
data: &$field_type
|
data: &$field_type
|
||||||
) {
|
) {
|
||||||
let key = $field_name::key$(::<$($generic_name),+>)?($($arg),*);
|
let key = Self::key($($arg),*);
|
||||||
txn.put(&key, borsh::to_vec(data).unwrap());
|
txn.put(&key, borsh::to_vec(data).unwrap());
|
||||||
}
|
}
|
||||||
pub(crate) fn get$(<$($generic_name: $generic_type),+>)?(
|
pub(crate) fn get(
|
||||||
getter: &impl Get,
|
getter: &impl Get,
|
||||||
$($arg: $arg_type),*
|
$($arg: $arg_type),*
|
||||||
) -> Option<$field_type> {
|
) -> Option<$field_type> {
|
||||||
getter.get($field_name::key$(::<$($generic_name),+>)?($($arg),*)).map(|data| {
|
getter.get(Self::key($($arg),*)).map(|data| {
|
||||||
borsh::from_slice(data.as_ref()).unwrap()
|
borsh::from_slice(data.as_ref()).unwrap()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Returns a PhantomData of all generic types so if the generic was only used in the value,
|
// Returns a PhantomData of all generic types so if the generic was only used in the value,
|
||||||
// not the keys, this doesn't have unused generic types
|
// not the keys, this doesn't have unused generic types
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub(crate) fn del$(<$($generic_name: $generic_type),+>)?(
|
pub(crate) fn del(
|
||||||
txn: &mut impl DbTxn
|
txn: &mut impl DbTxn
|
||||||
$(, $arg: $arg_type)*
|
$(, $arg: $arg_type)*
|
||||||
) -> core::marker::PhantomData<($($($generic_name),+)?)> {
|
) -> core::marker::PhantomData<($($($generic_name),+)?)> {
|
||||||
txn.del(&$field_name::key$(::<$($generic_name),+>)?($($arg),*));
|
txn.del(&Self::key($($arg),*));
|
||||||
core::marker::PhantomData
|
core::marker::PhantomData
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn take$(<$($generic_name: $generic_type),+>)?(
|
pub(crate) fn take(
|
||||||
txn: &mut impl DbTxn
|
txn: &mut impl DbTxn
|
||||||
$(, $arg: $arg_type)*
|
$(, $arg: $arg_type)*
|
||||||
) -> Option<$field_type> {
|
) -> Option<$field_type> {
|
||||||
let key = $field_name::key$(::<$($generic_name),+>)?($($arg),*);
|
let key = Self::key($($arg),*);
|
||||||
let res = txn.get(&key).map(|data| borsh::from_slice(data.as_ref()).unwrap());
|
let res = txn.get(&key).map(|data| borsh::from_slice(data.as_ref()).unwrap());
|
||||||
if res.is_some() {
|
if res.is_some() {
|
||||||
txn.del(key);
|
txn.del(key);
|
||||||
|
@ -119,14 +123,14 @@ macro_rules! db_channel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl $field_name {
|
impl$(<$($generic_name: $generic_type),+>)? $field_name$(<$($generic_name),+>)? {
|
||||||
pub(crate) fn send$(<$($generic_name: $generic_type),+>)?(
|
pub(crate) fn send(
|
||||||
txn: &mut impl DbTxn
|
txn: &mut impl DbTxn
|
||||||
$(, $arg: $arg_type)*
|
$(, $arg: $arg_type)*
|
||||||
, value: &$field_type
|
, value: &$field_type
|
||||||
) {
|
) {
|
||||||
// Use index 0 to store the amount of messages
|
// Use index 0 to store the amount of messages
|
||||||
let messages_sent_key = $field_name::key$(::<$($generic_name),+>)?($($arg,)* 0);
|
let messages_sent_key = Self::key($($arg,)* 0);
|
||||||
let messages_sent = txn.get(&messages_sent_key).map(|counter| {
|
let messages_sent = txn.get(&messages_sent_key).map(|counter| {
|
||||||
u32::from_le_bytes(counter.try_into().unwrap())
|
u32::from_le_bytes(counter.try_into().unwrap())
|
||||||
}).unwrap_or(0);
|
}).unwrap_or(0);
|
||||||
|
@ -137,22 +141,22 @@ macro_rules! db_channel {
|
||||||
// at the same time
|
// at the same time
|
||||||
let index_to_use = messages_sent + 2;
|
let index_to_use = messages_sent + 2;
|
||||||
|
|
||||||
$field_name::set$(::<$($generic_name),+>)?(txn, $($arg,)* index_to_use, value);
|
Self::set(txn, $($arg,)* index_to_use, value);
|
||||||
}
|
}
|
||||||
pub(crate) fn try_recv$(<$($generic_name: $generic_type),+>)?(
|
pub(crate) fn try_recv(
|
||||||
txn: &mut impl DbTxn
|
txn: &mut impl DbTxn
|
||||||
$(, $arg: $arg_type)*
|
$(, $arg: $arg_type)*
|
||||||
) -> Option<$field_type> {
|
) -> Option<$field_type> {
|
||||||
let messages_recvd_key = $field_name::key$(::<$($generic_name),+>)?($($arg,)* 1);
|
let messages_recvd_key = Self::key($($arg,)* 1);
|
||||||
let messages_recvd = txn.get(&messages_recvd_key).map(|counter| {
|
let messages_recvd = txn.get(&messages_recvd_key).map(|counter| {
|
||||||
u32::from_le_bytes(counter.try_into().unwrap())
|
u32::from_le_bytes(counter.try_into().unwrap())
|
||||||
}).unwrap_or(0);
|
}).unwrap_or(0);
|
||||||
|
|
||||||
let index_to_read = messages_recvd + 2;
|
let index_to_read = messages_recvd + 2;
|
||||||
|
|
||||||
let res = $field_name::get$(::<$($generic_name),+>)?(txn, $($arg,)* index_to_read);
|
let res = Self::get(txn, $($arg,)* index_to_read);
|
||||||
if res.is_some() {
|
if res.is_some() {
|
||||||
$field_name::del$(::<$($generic_name),+>)?(txn, $($arg,)* index_to_read);
|
Self::del(txn, $($arg,)* index_to_read);
|
||||||
txn.put(&messages_recvd_key, (messages_recvd + 1).to_le_bytes());
|
txn.put(&messages_recvd_key, (messages_recvd + 1).to_le_bytes());
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
|
|
|
@ -111,14 +111,14 @@ async fn coordinator_loop<D: Db>(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Queue the key to be activated upon the next Batch
|
// Queue the key to be activated upon the next Batch
|
||||||
db::KeyToActivate::send::<
|
db::KeyToActivate::<
|
||||||
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
||||||
>(txn, &key);
|
>::send(txn, &key);
|
||||||
|
|
||||||
// Set the external key, as needed by the signers
|
// Set the external key, as needed by the signers
|
||||||
db::ExternalKeyForSessionForSigners::set::<
|
db::ExternalKeyForSessionForSigners::<
|
||||||
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
||||||
>(txn, session, &key);
|
>::set(txn, session, &key);
|
||||||
|
|
||||||
// This isn't cheap yet only happens for the very first set of keys
|
// This isn't cheap yet only happens for the very first set of keys
|
||||||
if scanner.is_none() {
|
if scanner.is_none() {
|
||||||
|
@ -130,9 +130,9 @@ async fn coordinator_loop<D: Db>(
|
||||||
|
|
||||||
// Since this session had its slashes reported, it has finished all its signature
|
// Since this session had its slashes reported, it has finished all its signature
|
||||||
// protocols and has been fully retired. We retire it from the signers accordingly
|
// protocols and has been fully retired. We retire it from the signers accordingly
|
||||||
let key = db::ExternalKeyForSessionForSigners::take::<
|
let key = db::ExternalKeyForSessionForSigners::<
|
||||||
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
||||||
>(txn, session)
|
>::take(txn, session)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.0;
|
.0;
|
||||||
|
|
||||||
|
@ -147,9 +147,9 @@ async fn coordinator_loop<D: Db>(
|
||||||
} => {
|
} => {
|
||||||
let mut txn = txn.take().unwrap();
|
let mut txn = txn.take().unwrap();
|
||||||
let scanner = scanner.as_mut().unwrap();
|
let scanner = scanner.as_mut().unwrap();
|
||||||
let key_to_activate = db::KeyToActivate::try_recv::<
|
let key_to_activate = db::KeyToActivate::<
|
||||||
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
|
||||||
>(&mut txn)
|
>::try_recv(&mut txn)
|
||||||
.map(|key| key.0);
|
.map(|key| key.0);
|
||||||
// This is a cheap call as it internally just queues this to be done later
|
// This is a cheap call as it internally just queues this to be done later
|
||||||
scanner.acknowledge_batch(
|
scanner.acknowledge_batch(
|
||||||
|
|
|
@ -107,7 +107,7 @@ create_db!(
|
||||||
pub(crate) struct ScannerGlobalDb<S: ScannerFeed>(PhantomData<S>);
|
pub(crate) struct ScannerGlobalDb<S: ScannerFeed>(PhantomData<S>);
|
||||||
impl<S: ScannerFeed> ScannerGlobalDb<S> {
|
impl<S: ScannerFeed> ScannerGlobalDb<S> {
|
||||||
pub(crate) fn has_any_key_been_queued(getter: &impl Get) -> bool {
|
pub(crate) fn has_any_key_been_queued(getter: &impl Get) -> bool {
|
||||||
ActiveKeys::get::<EncodableG<KeyFor<S>>>(getter).is_some()
|
ActiveKeys::<EncodableG<KeyFor<S>>>::get(getter).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Queue a key.
|
/// Queue a key.
|
||||||
|
@ -315,7 +315,7 @@ pub(crate) struct ReceiverScanData<S: ScannerFeed> {
|
||||||
|
|
||||||
db_channel! {
|
db_channel! {
|
||||||
ScannerScanEventuality {
|
ScannerScanEventuality {
|
||||||
ScannedBlock: (empty_key: ()) -> Vec<u8>,
|
ScannedBlock: () -> Vec<u8>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,14 +364,14 @@ impl<S: ScannerFeed> ScanToEventualityDb<S> {
|
||||||
for output in &data.returns {
|
for output in &data.returns {
|
||||||
output.write(&mut buf).unwrap();
|
output.write(&mut buf).unwrap();
|
||||||
}
|
}
|
||||||
ScannedBlock::send(txn, (), &buf);
|
ScannedBlock::send(txn, &buf);
|
||||||
}
|
}
|
||||||
pub(crate) fn recv_scan_data(
|
pub(crate) fn recv_scan_data(
|
||||||
txn: &mut impl DbTxn,
|
txn: &mut impl DbTxn,
|
||||||
expected_block_number: u64,
|
expected_block_number: u64,
|
||||||
) -> ReceiverScanData<S> {
|
) -> ReceiverScanData<S> {
|
||||||
let data =
|
let data =
|
||||||
ScannedBlock::try_recv(txn, ()).expect("receiving data for a scanned block not yet sent");
|
ScannedBlock::try_recv(txn).expect("receiving data for a scanned block not yet sent");
|
||||||
let mut data = data.as_slice();
|
let mut data = data.as_slice();
|
||||||
|
|
||||||
let block_number = {
|
let block_number = {
|
||||||
|
@ -462,7 +462,7 @@ struct BlockBoundInInstructions {
|
||||||
|
|
||||||
db_channel! {
|
db_channel! {
|
||||||
ScannerScanReport {
|
ScannerScanReport {
|
||||||
InInstructions: (empty_key: ()) -> BlockBoundInInstructions,
|
InInstructions: () -> BlockBoundInInstructions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -484,7 +484,6 @@ impl<S: ScannerFeed> ScanToReportDb<S> {
|
||||||
}
|
}
|
||||||
InInstructions::send(
|
InInstructions::send(
|
||||||
txn,
|
txn,
|
||||||
(),
|
|
||||||
&BlockBoundInInstructions { block_number, returnable_in_instructions: buf },
|
&BlockBoundInInstructions { block_number, returnable_in_instructions: buf },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -493,7 +492,7 @@ impl<S: ScannerFeed> ScanToReportDb<S> {
|
||||||
txn: &mut impl DbTxn,
|
txn: &mut impl DbTxn,
|
||||||
block_number: u64,
|
block_number: u64,
|
||||||
) -> InInstructionData<S> {
|
) -> InInstructionData<S> {
|
||||||
let data = InInstructions::try_recv(txn, ())
|
let data = InInstructions::try_recv(txn)
|
||||||
.expect("receiving InInstructions for a scanned block not yet sent");
|
.expect("receiving InInstructions for a scanned block not yet sent");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
block_number, data.block_number,
|
block_number, data.block_number,
|
||||||
|
@ -556,7 +555,7 @@ mod _public_db {
|
||||||
|
|
||||||
db_channel! {
|
db_channel! {
|
||||||
ScannerPublic {
|
ScannerPublic {
|
||||||
Batches: (empty_key: ()) -> Batch,
|
Batches: () -> Batch,
|
||||||
BatchesToSign: (key: &[u8]) -> Batch,
|
BatchesToSign: (key: &[u8]) -> Batch,
|
||||||
AcknowledgedBatches: (key: &[u8]) -> u32,
|
AcknowledgedBatches: (key: &[u8]) -> u32,
|
||||||
CompletedEventualities: (key: &[u8]) -> [u8; 32],
|
CompletedEventualities: (key: &[u8]) -> [u8; 32],
|
||||||
|
@ -570,12 +569,12 @@ mod _public_db {
|
||||||
pub struct Batches;
|
pub struct Batches;
|
||||||
impl Batches {
|
impl Batches {
|
||||||
pub(crate) fn send(txn: &mut impl DbTxn, batch: &Batch) {
|
pub(crate) fn send(txn: &mut impl DbTxn, batch: &Batch) {
|
||||||
_public_db::Batches::send(txn, (), batch);
|
_public_db::Batches::send(txn, batch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Receive a batch to publish.
|
/// Receive a batch to publish.
|
||||||
pub fn try_recv(txn: &mut impl DbTxn) -> Option<Batch> {
|
pub fn try_recv(txn: &mut impl DbTxn) -> Option<Batch> {
|
||||||
_public_db::Batches::try_recv(txn, ())
|
_public_db::Batches::try_recv(txn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,9 +54,7 @@ impl<S: ScannerFeed> ReportDb<S> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn take_block_number_for_batch(txn: &mut impl DbTxn, id: u32) -> Option<u64> {
|
pub(crate) fn take_block_number_for_batch(txn: &mut impl DbTxn, id: u32) -> Option<u64> {
|
||||||
let block_number = BlockNumberForBatch::get(txn, id)?;
|
BlockNumberForBatch::take(txn, id)
|
||||||
BlockNumberForBatch::del(txn, id);
|
|
||||||
Some(block_number)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn save_external_key_for_session_to_sign_batch(
|
pub(crate) fn save_external_key_for_session_to_sign_batch(
|
||||||
|
@ -103,8 +101,7 @@ impl<S: ScannerFeed> ReportDb<S> {
|
||||||
txn: &mut impl DbTxn,
|
txn: &mut impl DbTxn,
|
||||||
id: u32,
|
id: u32,
|
||||||
) -> Option<Vec<Option<ReturnInformation<S>>>> {
|
) -> Option<Vec<Option<ReturnInformation<S>>>> {
|
||||||
let buf = SerializedReturnAddresses::get(txn, id)?;
|
let buf = SerializedReturnAddresses::take(txn, id)?;
|
||||||
SerializedReturnAddresses::del(txn, id);
|
|
||||||
let mut buf = buf.as_slice();
|
let mut buf = buf.as_slice();
|
||||||
|
|
||||||
let mut res = Vec::with_capacity(buf.len() / (32 + 1 + 8));
|
let mut res = Vec::with_capacity(buf.len() / (32 + 1 + 8));
|
||||||
|
|
|
@ -37,7 +37,7 @@ pub(crate) enum Action<S: ScannerFeed> {
|
||||||
|
|
||||||
db_channel!(
|
db_channel!(
|
||||||
ScannerSubstrate {
|
ScannerSubstrate {
|
||||||
Actions: (empty_key: ()) -> ActionEncodable,
|
Actions: () -> ActionEncodable,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -52,7 +52,6 @@ impl<S: ScannerFeed> SubstrateDb<S> {
|
||||||
) {
|
) {
|
||||||
Actions::send(
|
Actions::send(
|
||||||
txn,
|
txn,
|
||||||
(),
|
|
||||||
&ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable {
|
&ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable {
|
||||||
batch_id,
|
batch_id,
|
||||||
in_instruction_succeededs,
|
in_instruction_succeededs,
|
||||||
|
@ -62,11 +61,11 @@ impl<S: ScannerFeed> SubstrateDb<S> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pub(crate) fn queue_queue_burns(txn: &mut impl DbTxn, burns: Vec<OutInstructionWithBalance>) {
|
pub(crate) fn queue_queue_burns(txn: &mut impl DbTxn, burns: Vec<OutInstructionWithBalance>) {
|
||||||
Actions::send(txn, (), &ActionEncodable::QueueBurns(burns));
|
Actions::send(txn, &ActionEncodable::QueueBurns(burns));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn next_action(txn: &mut impl DbTxn) -> Option<Action<S>> {
|
pub(crate) fn next_action(txn: &mut impl DbTxn) -> Option<Action<S>> {
|
||||||
let action_encodable = Actions::try_recv(txn, ())?;
|
let action_encodable = Actions::try_recv(txn)?;
|
||||||
Some(match action_encodable {
|
Some(match action_encodable {
|
||||||
ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable {
|
ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable {
|
||||||
batch_id,
|
batch_id,
|
||||||
|
|
|
@ -69,9 +69,7 @@ impl<S: ScannerFeed> Db<S> {
|
||||||
txn: &mut impl DbTxn,
|
txn: &mut impl DbTxn,
|
||||||
output: &<OutputFor<S> as ReceivedOutput<KeyFor<S>, AddressFor<S>>>::Id,
|
output: &<OutputFor<S> as ReceivedOutput<KeyFor<S>, AddressFor<S>>>::Id,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let res = AlreadyAccumulatedOutput::get(txn, output.as_ref()).is_some();
|
AlreadyAccumulatedOutput::take(txn, output.as_ref()).is_some()
|
||||||
AlreadyAccumulatedOutput::del(txn, output.as_ref());
|
|
||||||
res
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn queued_payments(
|
pub(crate) fn queued_payments(
|
||||||
|
|
Loading…
Reference in a new issue