Misc tidying of serai-db calls

This commit is contained in:
Luke Parker 2024-09-11 09:12:00 -04:00
parent 59fa49f750
commit 9b8c8f8231
6 changed files with 46 additions and 49 deletions

View file

@ -47,9 +47,13 @@ macro_rules! create_db {
}) => {
$(
#[derive(Clone, Debug)]
pub(crate) struct $field_name;
impl $field_name {
pub(crate) fn key$(<$($generic_name: $generic_type),+>)?($($arg: $arg_type),*) -> Vec<u8> {
pub(crate) struct $field_name$(
<$($generic_name: $generic_type),+>
)?$(
(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;
$crate::serai_db_key(
stringify!($db_name).as_bytes(),
@ -57,38 +61,38 @@ macro_rules! create_db {
($($arg),*).encode()
)
}
pub(crate) fn set$(<$($generic_name: $generic_type),+>)?(
pub(crate) fn set(
txn: &mut impl DbTxn
$(, $arg: $arg_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());
}
pub(crate) fn get$(<$($generic_name: $generic_type),+>)?(
pub(crate) fn get(
getter: &impl Get,
$($arg: $arg_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()
})
}
// 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
#[allow(dead_code)]
pub(crate) fn del$(<$($generic_name: $generic_type),+>)?(
pub(crate) fn del(
txn: &mut impl DbTxn
$(, $arg: $arg_type)*
) -> core::marker::PhantomData<($($($generic_name),+)?)> {
txn.del(&$field_name::key$(::<$($generic_name),+>)?($($arg),*));
txn.del(&Self::key($($arg),*));
core::marker::PhantomData
}
pub(crate) fn take$(<$($generic_name: $generic_type),+>)?(
pub(crate) fn take(
txn: &mut impl DbTxn
$(, $arg: $arg_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());
if res.is_some() {
txn.del(key);
@ -119,14 +123,14 @@ macro_rules! db_channel {
}
}
impl $field_name {
pub(crate) fn send$(<$($generic_name: $generic_type),+>)?(
impl$(<$($generic_name: $generic_type),+>)? $field_name$(<$($generic_name),+>)? {
pub(crate) fn send(
txn: &mut impl DbTxn
$(, $arg: $arg_type)*
, value: &$field_type
) {
// 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| {
u32::from_le_bytes(counter.try_into().unwrap())
}).unwrap_or(0);
@ -137,22 +141,22 @@ macro_rules! db_channel {
// at the same time
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
$(, $arg: $arg_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| {
u32::from_le_bytes(counter.try_into().unwrap())
}).unwrap_or(0);
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() {
$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());
}
res

View file

@ -111,14 +111,14 @@ async fn coordinator_loop<D: Db>(
);
// Queue the key to be activated upon the next Batch
db::KeyToActivate::send::<
db::KeyToActivate::<
<<KeyGenParams as ::key_gen::KeyGenParams>::ExternalNetworkCurve as Ciphersuite>::G,
>(txn, &key);
>::send(txn, &key);
// Set the external key, as needed by the signers
db::ExternalKeyForSessionForSigners::set::<
db::ExternalKeyForSessionForSigners::<
<<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
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
// 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,
>(txn, session)
>::take(txn, session)
.unwrap()
.0;
@ -147,9 +147,9 @@ async fn coordinator_loop<D: Db>(
} => {
let mut txn = txn.take().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,
>(&mut txn)
>::try_recv(&mut txn)
.map(|key| key.0);
// This is a cheap call as it internally just queues this to be done later
scanner.acknowledge_batch(

View file

@ -107,7 +107,7 @@ create_db!(
pub(crate) struct ScannerGlobalDb<S: ScannerFeed>(PhantomData<S>);
impl<S: ScannerFeed> ScannerGlobalDb<S> {
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.
@ -315,7 +315,7 @@ pub(crate) struct ReceiverScanData<S: ScannerFeed> {
db_channel! {
ScannerScanEventuality {
ScannedBlock: (empty_key: ()) -> Vec<u8>,
ScannedBlock: () -> Vec<u8>,
}
}
@ -364,14 +364,14 @@ impl<S: ScannerFeed> ScanToEventualityDb<S> {
for output in &data.returns {
output.write(&mut buf).unwrap();
}
ScannedBlock::send(txn, (), &buf);
ScannedBlock::send(txn, &buf);
}
pub(crate) fn recv_scan_data(
txn: &mut impl DbTxn,
expected_block_number: u64,
) -> ReceiverScanData<S> {
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 block_number = {
@ -462,7 +462,7 @@ struct BlockBoundInInstructions {
db_channel! {
ScannerScanReport {
InInstructions: (empty_key: ()) -> BlockBoundInInstructions,
InInstructions: () -> BlockBoundInInstructions,
}
}
@ -484,7 +484,6 @@ impl<S: ScannerFeed> ScanToReportDb<S> {
}
InInstructions::send(
txn,
(),
&BlockBoundInInstructions { block_number, returnable_in_instructions: buf },
);
}
@ -493,7 +492,7 @@ impl<S: ScannerFeed> ScanToReportDb<S> {
txn: &mut impl DbTxn,
block_number: u64,
) -> InInstructionData<S> {
let data = InInstructions::try_recv(txn, ())
let data = InInstructions::try_recv(txn)
.expect("receiving InInstructions for a scanned block not yet sent");
assert_eq!(
block_number, data.block_number,
@ -556,7 +555,7 @@ mod _public_db {
db_channel! {
ScannerPublic {
Batches: (empty_key: ()) -> Batch,
Batches: () -> Batch,
BatchesToSign: (key: &[u8]) -> Batch,
AcknowledgedBatches: (key: &[u8]) -> u32,
CompletedEventualities: (key: &[u8]) -> [u8; 32],
@ -570,12 +569,12 @@ mod _public_db {
pub struct Batches;
impl Batches {
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.
pub fn try_recv(txn: &mut impl DbTxn) -> Option<Batch> {
_public_db::Batches::try_recv(txn, ())
_public_db::Batches::try_recv(txn)
}
}

View file

@ -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> {
let block_number = BlockNumberForBatch::get(txn, id)?;
BlockNumberForBatch::del(txn, id);
Some(block_number)
BlockNumberForBatch::take(txn, id)
}
pub(crate) fn save_external_key_for_session_to_sign_batch(
@ -103,8 +101,7 @@ impl<S: ScannerFeed> ReportDb<S> {
txn: &mut impl DbTxn,
id: u32,
) -> Option<Vec<Option<ReturnInformation<S>>>> {
let buf = SerializedReturnAddresses::get(txn, id)?;
SerializedReturnAddresses::del(txn, id);
let buf = SerializedReturnAddresses::take(txn, id)?;
let mut buf = buf.as_slice();
let mut res = Vec::with_capacity(buf.len() / (32 + 1 + 8));

View file

@ -37,7 +37,7 @@ pub(crate) enum Action<S: ScannerFeed> {
db_channel!(
ScannerSubstrate {
Actions: (empty_key: ()) -> ActionEncodable,
Actions: () -> ActionEncodable,
}
);
@ -52,7 +52,6 @@ impl<S: ScannerFeed> SubstrateDb<S> {
) {
Actions::send(
txn,
(),
&ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable {
batch_id,
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>) {
Actions::send(txn, (), &ActionEncodable::QueueBurns(burns));
Actions::send(txn, &ActionEncodable::QueueBurns(burns));
}
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 {
ActionEncodable::AcknowledgeBatch(AcknowledgeBatchEncodable {
batch_id,

View file

@ -69,9 +69,7 @@ impl<S: ScannerFeed> Db<S> {
txn: &mut impl DbTxn,
output: &<OutputFor<S> as ReceivedOutput<KeyFor<S>, AddressFor<S>>>::Id,
) -> bool {
let res = AlreadyAccumulatedOutput::get(txn, output.as_ref()).is_some();
AlreadyAccumulatedOutput::del(txn, output.as_ref());
res
AlreadyAccumulatedOutput::take(txn, output.as_ref()).is_some()
}
pub(crate) fn queued_payments(