fedimint_dummy_server/
db.rsuse fedimint_core::db::{IDatabaseTransactionOpsCoreTyped, MigrationContext};
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::secp256k1::PublicKey;
use fedimint_core::{impl_db_lookup, impl_db_record, Amount, OutPoint};
use futures::StreamExt;
use serde::Serialize;
use strum_macros::EnumIter;
use crate::DummyOutputOutcome;
#[repr(u8)]
#[derive(Clone, EnumIter, Debug)]
pub enum DbKeyPrefix {
Funds = 0x01,
Outcome = 0x02,
}
impl std::fmt::Display for DbKeyPrefix {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
pub struct DummyFundsKeyV0(pub PublicKey);
#[derive(Debug, Encodable, Decodable)]
pub struct DummyFundsKeyPrefixV0;
impl_db_record!(
key = DummyFundsKeyV0,
value = (),
db_prefix = DbKeyPrefix::Funds,
);
impl_db_lookup!(key = DummyFundsKeyV0, query_prefix = DummyFundsKeyPrefixV0);
#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
pub struct DummyFundsKeyV1(pub PublicKey);
#[derive(Debug, Encodable, Decodable)]
pub struct DummyFundsPrefixV1;
impl_db_record!(
key = DummyFundsKeyV1,
value = Amount,
db_prefix = DbKeyPrefix::Funds,
);
impl_db_lookup!(key = DummyFundsKeyV1, query_prefix = DummyFundsPrefixV1);
pub async fn migrate_to_v1(mut ctx: MigrationContext<'_>) -> Result<(), anyhow::Error> {
let mut dbtx = ctx.dbtx();
let v0_entries = dbtx
.find_by_prefix(&DummyFundsKeyPrefixV0)
.await
.collect::<Vec<(DummyFundsKeyV0, ())>>()
.await;
dbtx.remove_by_prefix(&DummyFundsKeyPrefixV0).await;
for (v0_key, _v0_val) in v0_entries {
let v1_key = DummyFundsKeyV1(v0_key.0);
dbtx.insert_new_entry(&v1_key, &Amount::ZERO).await;
}
Ok(())
}
#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
pub struct DummyOutcomeKey(pub OutPoint);
#[derive(Debug, Encodable, Decodable)]
pub struct DummyOutcomePrefix;
impl_db_record!(
key = DummyOutcomeKey,
value = DummyOutputOutcome,
db_prefix = DbKeyPrefix::Outcome,
);
impl_db_lookup!(key = DummyOutcomeKey, query_prefix = DummyOutcomePrefix);