fedimint_dummy_client/
db.rsuse std::io::Cursor;
use fedimint_core::core::OperationId;
use fedimint_core::db::{DatabaseTransaction, IDatabaseTransactionOpsCoreTyped};
use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::module::registry::ModuleDecoderRegistry;
use fedimint_core::{impl_db_record, Amount, TransactionId};
use strum_macros::EnumIter;
use tracing::warn;
use crate::states::DummyStateMachine;
#[repr(u8)]
#[derive(Clone, Debug, EnumIter)]
pub enum DbKeyPrefix {
ClientFunds = 0x04,
ClientName = 0x50,
}
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)]
pub struct DummyClientFundsKeyV0;
impl_db_record!(
key = DummyClientFundsKeyV0,
value = (),
db_prefix = DbKeyPrefix::ClientFunds,
);
#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash)]
pub struct DummyClientFundsKeyV1;
impl_db_record!(
key = DummyClientFundsKeyV1,
value = Amount,
db_prefix = DbKeyPrefix::ClientFunds,
);
#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash)]
pub struct DummyClientNameKey;
impl_db_record!(
key = DummyClientNameKey,
value = String,
db_prefix = DbKeyPrefix::ClientName,
);
pub async fn migrate_to_v1(
dbtx: &mut DatabaseTransaction<'_>,
) -> anyhow::Result<Option<(Vec<(Vec<u8>, OperationId)>, Vec<(Vec<u8>, OperationId)>)>> {
if dbtx.remove_entry(&DummyClientFundsKeyV0).await.is_some() {
dbtx.insert_new_entry(&DummyClientFundsKeyV1, &Amount::from_sats(1000))
.await;
} else {
warn!("Dummy client did not have client funds, skipping database migration");
}
Ok(None)
}
pub(crate) fn get_v1_migrated_state(
operation_id: OperationId,
cursor: &mut Cursor<&[u8]>,
) -> anyhow::Result<Option<(Vec<u8>, OperationId)>> {
let decoders = ModuleDecoderRegistry::default();
let dummy_sm_variant = u16::consensus_decode(cursor, &decoders)?;
if dummy_sm_variant != 5 {
return Ok(None);
}
let _unreachable_state_length = u16::consensus_decode(cursor, &decoders)?;
let unreachable = Unreachable::consensus_decode(cursor, &decoders)?;
let new_state = DummyStateMachine::OutputDone(
unreachable.amount,
unreachable.txid,
unreachable.operation_id,
);
let bytes = new_state.consensus_encode_to_vec();
Ok(Some((bytes, operation_id)))
}
#[derive(Debug)]
struct Unreachable {
operation_id: OperationId,
txid: TransactionId,
amount: Amount,
}
impl Decodable for Unreachable {
fn consensus_decode<R: std::io::Read>(
reader: &mut R,
modules: &ModuleDecoderRegistry,
) -> Result<Self, fedimint_core::encoding::DecodeError> {
let operation_id = OperationId::consensus_decode(reader, modules)?;
let txid = TransactionId::consensus_decode(reader, modules)?;
let amount = Amount::consensus_decode(reader, modules)?;
Ok(Unreachable {
operation_id,
txid,
amount,
})
}
}