refactor: code style in localstore

This commit is contained in:
thesimplekid
2024-04-09 23:38:08 +01:00
parent ce0b309b66
commit 973dc89b01
4 changed files with 74 additions and 89 deletions

View File

@@ -47,24 +47,27 @@ impl RedbLocalStore {
let db_version = table.get("db_version")?;
let db_version = db_version.map(|v| v.value().to_owned());
if let Some(db_version) = db_version {
let current_file_version = u64::from_str(&db_version)?;
if current_file_version.ne(&DATABASE_VERSION) {
// Database needs to be upgraded
todo!()
match db_version {
Some(db_version) => {
let current_file_version = u64::from_str(&db_version)?;
if current_file_version.ne(&DATABASE_VERSION) {
// Database needs to be upgraded
todo!()
}
}
} else {
// Open all tables to init a new db
let _ = write_txn.open_table(ACTIVE_KEYSETS_TABLE)?;
let _ = write_txn.open_table(KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(PENDING_PROOFS_TABLE)?;
let _ = write_txn.open_table(SPENT_PROOFS_TABLE)?;
let _ = write_txn.open_table(BLINDED_SIGNATURES)?;
None => {
// Open all tables to init a new db
let _ = write_txn.open_table(ACTIVE_KEYSETS_TABLE)?;
let _ = write_txn.open_table(KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(PENDING_PROOFS_TABLE)?;
let _ = write_txn.open_table(SPENT_PROOFS_TABLE)?;
let _ = write_txn.open_table(BLINDED_SIGNATURES)?;
table.insert("db_version", "0")?;
};
table.insert("db_version", "0")?;
}
}
}
write_txn.commit()?;
@@ -165,12 +168,9 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(KEYSETS_TABLE)?;
let keyset = table.get(keyset_id.to_string().as_str())?;
if let Some(keyset) = keyset {
Ok(serde_json::from_str(keyset.value())?)
} else {
Ok(None)
match table.get(keyset_id.to_string().as_str())? {
Some(keyset) => Ok(serde_json::from_str(keyset.value())?),
None => Ok(None),
}
}
@@ -209,12 +209,9 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(MINT_QUOTES_TABLE)?;
let quote = table.get(quote_id)?;
if let Some(quote) = quote {
Ok(serde_json::from_str(quote.value())?)
} else {
Ok(None)
match table.get(quote_id)? {
Some(quote) => Ok(serde_json::from_str(quote.value())?),
None => Ok(None),
}
}
@@ -323,12 +320,9 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(SPENT_PROOFS_TABLE)?;
let proof = table.get(y.to_bytes())?;
if let Some(proof) = proof {
Ok(serde_json::from_str(proof.value())?)
} else {
Ok(None)
match table.get(y.to_bytes())? {
Some(proof) => Ok(serde_json::from_str(proof.value())?),
None => Ok(None),
}
}
@@ -339,14 +333,9 @@ impl LocalStore for RedbLocalStore {
let y: PublicKey = hash_to_curve(&secret.to_bytes())?;
let proof = table.get(y.to_bytes())?;
debug!("Checking secret: {}", secret.to_string());
if let Some(proof) = proof {
Ok(serde_json::from_str(proof.value())?)
} else {
Ok(None)
match table.get(y.to_bytes())? {
Some(proof) => Ok(serde_json::from_str(proof.value())?),
None => Ok(None),
}
}
@@ -372,12 +361,9 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(PENDING_PROOFS_TABLE)?;
let proof = table.get(y.to_bytes())?;
if let Some(proof) = proof {
Ok(serde_json::from_str(proof.value())?)
} else {
Ok(None)
match table.get(y.to_bytes())? {
Some(proof) => Ok(serde_json::from_str(proof.value())?),
None => Ok(None),
}
}
@@ -388,12 +374,9 @@ impl LocalStore for RedbLocalStore {
let secret_hash = hash_to_curve(&secret.to_bytes())?;
let proof = table.get(secret_hash.to_bytes())?;
if let Some(proof) = proof {
Ok(serde_json::from_str(proof.value())?)
} else {
Ok(None)
match table.get(secret_hash.to_bytes())? {
Some(proof) => Ok(serde_json::from_str(proof.value())?),
None => Ok(None),
}
}
@@ -441,11 +424,10 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(BLINDED_SIGNATURES)?;
if let Some(blinded_signature) = table.get(blinded_message.to_bytes())? {
return Ok(serde_json::from_str(blinded_signature.value())?);
match table.get(blinded_message.to_bytes())? {
Some(blind_signature) => Ok(serde_json::from_str(blind_signature.value())?),
None => Ok(None),
}
Ok(None)
}
async fn get_blinded_signatures(
@@ -459,10 +441,11 @@ impl LocalStore for RedbLocalStore {
let mut signatures = Vec::with_capacity(blinded_messages.len());
for blinded_message in blinded_messages {
if let Some(blinded_signature) = table.get(blinded_message.to_bytes())? {
signatures.push(Some(serde_json::from_str(blinded_signature.value())?))
} else {
signatures.push(None);
match table.get(blinded_message.to_bytes())? {
Some(blind_signature) => {
signatures.push(Some(serde_json::from_str(blind_signature.value())?))
}
None => signatures.push(None),
}
}

View File

@@ -42,29 +42,31 @@ impl RedbLocalStore {
let _ = write_txn.open_table(CONFIG_TABLE)?;
let mut table = write_txn.open_table(CONFIG_TABLE)?;
let db_version = table.get("db_version")?;
let db_version = db_version.map(|v| v.value().to_owned());
let db_version = table.get("db_version")?.map(|v| v.value().to_owned());
if let Some(db_version) = db_version {
let current_file_version = u64::from_str(&db_version)?;
if current_file_version.ne(&DATABASE_VERSION) {
// Database needs to be upgraded
todo!()
match db_version {
Some(db_version) => {
let current_file_version = u64::from_str(&db_version)?;
if current_file_version.ne(&DATABASE_VERSION) {
// Database needs to be upgraded
todo!()
}
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
}
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
} else {
// Open all tables to init a new db
let _ = write_txn.open_table(MINTS_TABLE)?;
let _ = write_txn.open_multimap_table(MINT_KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MINT_KEYS_TABLE)?;
let _ = write_txn.open_multimap_table(PROOFS_TABLE)?;
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
table.insert("db_version", "0")?;
};
None => {
// Open all tables to init a new db
let _ = write_txn.open_table(MINTS_TABLE)?;
let _ = write_txn.open_multimap_table(MINT_KEYSETS_TABLE)?;
let _ = write_txn.open_table(MINT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
let _ = write_txn.open_table(MINT_KEYS_TABLE)?;
let _ = write_txn.open_multimap_table(PROOFS_TABLE)?;
#[cfg(feature = "nut13")]
let _ = write_txn.open_table(KEYSET_COUNTER)?;
table.insert("db_version", "0")?;
}
}
}
write_txn.commit()?;
@@ -418,10 +420,10 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(KEYSET_COUNTER)?;
let counter = table.get(keyset_id.to_string().as_str())?;
current_counter = if let Some(counter) = counter {
counter.value()
} else {
0
current_counter = match counter {
Some(c) => c.value(),
None => 0,
};
}

View File

@@ -244,7 +244,7 @@ mod tests {
)
.unwrap();
let e = hash_e(vec![r1.into(), r2.into(), k.into(), c.into()]);
let e = hash_e(vec![r1, r2, k, c]);
let e_hex = hex::encode(e);
assert_eq!(

View File

@@ -166,7 +166,7 @@ mod tests {
let key_str = "026562efcfadc8e86d44da6a8adf80633d974302e62c850774db1fb36ff4cc7198";
let pubkey = PublicKey::from_str(key_str).unwrap();
let v_key: VerifyingKey = pubkey.clone().try_into().unwrap();
let v_key: VerifyingKey = pubkey.try_into().unwrap();
let p: PublicKey = v_key.to_normalized_public_key();