[database] Add an in-memory database
This commit is contained in:
@@ -7,80 +7,19 @@ use bitcoin::hash_types::Txid;
|
||||
use bitcoin::util::bip32::{ChildNumber, DerivationPath};
|
||||
use bitcoin::{OutPoint, Script, Transaction};
|
||||
|
||||
use crate::database::memory::MapKey;
|
||||
use crate::database::{BatchDatabase, BatchOperations, Database};
|
||||
use crate::error::Error;
|
||||
use crate::types::*;
|
||||
|
||||
// path -> script p{i,e}<path> -> script
|
||||
// script -> path s<script> -> {i,e}<path>
|
||||
// outpoint u<outpoint> -> txout
|
||||
// rawtx r<txid> -> tx
|
||||
// transactions t<txid> -> tx details
|
||||
// deriv indexes c{i,e} -> u32
|
||||
// descriptor checksum d{i,e} -> vec<u8>
|
||||
|
||||
enum SledKey<'a> {
|
||||
Path((Option<ScriptType>, Option<&'a DerivationPath>)),
|
||||
Script(Option<&'a Script>),
|
||||
UTXO(Option<&'a OutPoint>),
|
||||
RawTx(Option<&'a Txid>),
|
||||
Transaction(Option<&'a Txid>),
|
||||
LastIndex(ScriptType),
|
||||
DescriptorChecksum(ScriptType),
|
||||
}
|
||||
|
||||
impl SledKey<'_> {
|
||||
pub fn as_prefix(&self) -> Vec<u8> {
|
||||
match self {
|
||||
SledKey::Path((st, _)) => {
|
||||
let mut v = b"p".to_vec();
|
||||
if let Some(st) = st {
|
||||
v.push(st.as_byte());
|
||||
}
|
||||
v
|
||||
}
|
||||
SledKey::Script(_) => b"s".to_vec(),
|
||||
SledKey::UTXO(_) => b"u".to_vec(),
|
||||
SledKey::RawTx(_) => b"r".to_vec(),
|
||||
SledKey::Transaction(_) => b"t".to_vec(),
|
||||
SledKey::LastIndex(st) => [b"c", st.as_ref()].concat(),
|
||||
SledKey::DescriptorChecksum(st) => [b"d", st.as_ref()].concat(),
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_content(&self) -> Vec<u8> {
|
||||
match self {
|
||||
SledKey::Path((_, Some(path))) => {
|
||||
let mut res = vec![];
|
||||
for val in *path {
|
||||
res.extend(&u32::from(*val).to_be_bytes());
|
||||
}
|
||||
res
|
||||
}
|
||||
SledKey::Script(Some(s)) => serialize(*s),
|
||||
SledKey::UTXO(Some(s)) => serialize(*s),
|
||||
SledKey::RawTx(Some(s)) => serialize(*s),
|
||||
SledKey::Transaction(Some(s)) => serialize(*s),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_sled_key(&self) -> Vec<u8> {
|
||||
let mut v = self.as_prefix();
|
||||
v.extend_from_slice(&self.serialize_content());
|
||||
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_batch_operations {
|
||||
( { $($after_insert:tt)* }, $process_delete:ident ) => {
|
||||
fn set_script_pubkey<P: AsRef<[ChildNumber]>>(&mut self, script: &Script, script_type: ScriptType, path: &P) -> Result<(), Error> {
|
||||
let deriv_path = DerivationPath::from(path.as_ref());
|
||||
let key = SledKey::Path((Some(script_type), Some(&deriv_path))).as_sled_key();
|
||||
let key = MapKey::Path((Some(script_type), Some(&deriv_path))).as_map_key();
|
||||
self.insert(key, serialize(script))$($after_insert)*;
|
||||
|
||||
let key = SledKey::Script(Some(script)).as_sled_key();
|
||||
let key = MapKey::Script(Some(script)).as_map_key();
|
||||
let value = json!({
|
||||
"t": script_type,
|
||||
"p": deriv_path,
|
||||
@@ -91,7 +30,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn set_utxo(&mut self, utxo: &UTXO) -> Result<(), Error> {
|
||||
let key = SledKey::UTXO(Some(&utxo.outpoint)).as_sled_key();
|
||||
let key = MapKey::UTXO(Some(&utxo.outpoint)).as_map_key();
|
||||
let value = serialize(&utxo.txout);
|
||||
self.insert(key, value)$($after_insert)*;
|
||||
|
||||
@@ -99,7 +38,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn set_raw_tx(&mut self, transaction: &Transaction) -> Result<(), Error> {
|
||||
let key = SledKey::RawTx(Some(&transaction.txid())).as_sled_key();
|
||||
let key = MapKey::RawTx(Some(&transaction.txid())).as_map_key();
|
||||
let value = serialize(transaction);
|
||||
self.insert(key, value)$($after_insert)*;
|
||||
|
||||
@@ -107,7 +46,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error> {
|
||||
let key = SledKey::Transaction(Some(&transaction.txid)).as_sled_key();
|
||||
let key = MapKey::Transaction(Some(&transaction.txid)).as_map_key();
|
||||
|
||||
// remove the raw tx from the serialized version
|
||||
let mut value = serde_json::to_value(transaction)?;
|
||||
@@ -125,7 +64,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error> {
|
||||
let key = SledKey::LastIndex(script_type).as_sled_key();
|
||||
let key = MapKey::LastIndex(script_type).as_map_key();
|
||||
self.insert(key, &value.to_be_bytes())$($after_insert)*;
|
||||
|
||||
Ok(())
|
||||
@@ -133,7 +72,7 @@ macro_rules! impl_batch_operations {
|
||||
|
||||
fn del_script_pubkey_from_path<P: AsRef<[ChildNumber]>>(&mut self, script_type: ScriptType, path: &P) -> Result<Option<Script>, Error> {
|
||||
let deriv_path = DerivationPath::from(path.as_ref());
|
||||
let key = SledKey::Path((Some(script_type), Some(&deriv_path))).as_sled_key();
|
||||
let key = MapKey::Path((Some(script_type), Some(&deriv_path))).as_map_key();
|
||||
let res = self.remove(key);
|
||||
let res = $process_delete!(res);
|
||||
|
||||
@@ -141,7 +80,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn del_path_from_script_pubkey(&mut self, script: &Script) -> Result<Option<(ScriptType, DerivationPath)>, Error> {
|
||||
let key = SledKey::Script(Some(script)).as_sled_key();
|
||||
let key = MapKey::Script(Some(script)).as_map_key();
|
||||
let res = self.remove(key);
|
||||
let res = $process_delete!(res);
|
||||
|
||||
@@ -158,7 +97,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn del_utxo(&mut self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
|
||||
let key = SledKey::UTXO(Some(outpoint)).as_sled_key();
|
||||
let key = MapKey::UTXO(Some(outpoint)).as_map_key();
|
||||
let res = self.remove(key);
|
||||
let res = $process_delete!(res);
|
||||
|
||||
@@ -172,7 +111,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn del_raw_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
||||
let key = SledKey::RawTx(Some(txid)).as_sled_key();
|
||||
let key = MapKey::RawTx(Some(txid)).as_map_key();
|
||||
let res = self.remove(key);
|
||||
let res = $process_delete!(res);
|
||||
|
||||
@@ -186,7 +125,7 @@ macro_rules! impl_batch_operations {
|
||||
None
|
||||
};
|
||||
|
||||
let key = SledKey::Transaction(Some(txid)).as_sled_key();
|
||||
let key = MapKey::Transaction(Some(txid)).as_map_key();
|
||||
let res = self.remove(key);
|
||||
let res = $process_delete!(res);
|
||||
|
||||
@@ -202,7 +141,7 @@ macro_rules! impl_batch_operations {
|
||||
}
|
||||
|
||||
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error> {
|
||||
let key = SledKey::LastIndex(script_type).as_sled_key();
|
||||
let key = MapKey::LastIndex(script_type).as_map_key();
|
||||
let res = self.remove(key);
|
||||
let res = $process_delete!(res);
|
||||
|
||||
@@ -243,7 +182,7 @@ impl Database for Tree {
|
||||
script_type: ScriptType,
|
||||
bytes: B,
|
||||
) -> Result<(), Error> {
|
||||
let key = SledKey::DescriptorChecksum(script_type).as_sled_key();
|
||||
let key = MapKey::DescriptorChecksum(script_type).as_map_key();
|
||||
|
||||
let prev = self.get(&key)?.map(|x| x.to_vec());
|
||||
if let Some(val) = prev {
|
||||
@@ -259,7 +198,7 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn iter_script_pubkeys(&self, script_type: Option<ScriptType>) -> Result<Vec<Script>, Error> {
|
||||
let key = SledKey::Path((script_type, None)).as_sled_key();
|
||||
let key = MapKey::Path((script_type, None)).as_map_key();
|
||||
self.scan_prefix(key)
|
||||
.map(|x| -> Result<_, Error> {
|
||||
let (_, v) = x?;
|
||||
@@ -269,7 +208,7 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn iter_utxos(&self) -> Result<Vec<UTXO>, Error> {
|
||||
let key = SledKey::UTXO(None).as_sled_key();
|
||||
let key = MapKey::UTXO(None).as_map_key();
|
||||
self.scan_prefix(key)
|
||||
.map(|x| -> Result<_, Error> {
|
||||
let (k, v) = x?;
|
||||
@@ -281,7 +220,7 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn iter_raw_txs(&self) -> Result<Vec<Transaction>, Error> {
|
||||
let key = SledKey::RawTx(None).as_sled_key();
|
||||
let key = MapKey::RawTx(None).as_map_key();
|
||||
self.scan_prefix(key)
|
||||
.map(|x| -> Result<_, Error> {
|
||||
let (_, v) = x?;
|
||||
@@ -291,7 +230,7 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn iter_txs(&self, include_raw: bool) -> Result<Vec<TransactionDetails>, Error> {
|
||||
let key = SledKey::Transaction(None).as_sled_key();
|
||||
let key = MapKey::Transaction(None).as_map_key();
|
||||
self.scan_prefix(key)
|
||||
.map(|x| -> Result<_, Error> {
|
||||
let (k, v) = x?;
|
||||
@@ -312,7 +251,7 @@ impl Database for Tree {
|
||||
path: &P,
|
||||
) -> Result<Option<Script>, Error> {
|
||||
let deriv_path = DerivationPath::from(path.as_ref());
|
||||
let key = SledKey::Path((Some(script_type), Some(&deriv_path))).as_sled_key();
|
||||
let key = MapKey::Path((Some(script_type), Some(&deriv_path))).as_map_key();
|
||||
Ok(self.get(key)?.map(|b| deserialize(&b)).transpose()?)
|
||||
}
|
||||
|
||||
@@ -320,7 +259,7 @@ impl Database for Tree {
|
||||
&self,
|
||||
script: &Script,
|
||||
) -> Result<Option<(ScriptType, DerivationPath)>, Error> {
|
||||
let key = SledKey::Script(Some(script)).as_sled_key();
|
||||
let key = MapKey::Script(Some(script)).as_map_key();
|
||||
self.get(key)?
|
||||
.map(|b| -> Result<_, Error> {
|
||||
let mut val: serde_json::Value = serde_json::from_slice(&b)?;
|
||||
@@ -333,7 +272,7 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn get_utxo(&self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
|
||||
let key = SledKey::UTXO(Some(outpoint)).as_sled_key();
|
||||
let key = MapKey::UTXO(Some(outpoint)).as_map_key();
|
||||
self.get(key)?
|
||||
.map(|b| -> Result<_, Error> {
|
||||
let txout = deserialize(&b)?;
|
||||
@@ -346,12 +285,12 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn get_raw_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
||||
let key = SledKey::RawTx(Some(txid)).as_sled_key();
|
||||
let key = MapKey::RawTx(Some(txid)).as_map_key();
|
||||
Ok(self.get(key)?.map(|b| deserialize(&b)).transpose()?)
|
||||
}
|
||||
|
||||
fn get_tx(&self, txid: &Txid, include_raw: bool) -> Result<Option<TransactionDetails>, Error> {
|
||||
let key = SledKey::Transaction(Some(txid)).as_sled_key();
|
||||
let key = MapKey::Transaction(Some(txid)).as_map_key();
|
||||
self.get(key)?
|
||||
.map(|b| -> Result<_, Error> {
|
||||
let mut txdetails: TransactionDetails = serde_json::from_slice(&b)?;
|
||||
@@ -365,7 +304,7 @@ impl Database for Tree {
|
||||
}
|
||||
|
||||
fn get_last_index(&self, script_type: ScriptType) -> Result<Option<u32>, Error> {
|
||||
let key = SledKey::LastIndex(script_type).as_sled_key();
|
||||
let key = MapKey::LastIndex(script_type).as_map_key();
|
||||
self.get(key)?
|
||||
.map(|b| -> Result<_, Error> {
|
||||
let array: [u8; 4] = b
|
||||
@@ -380,7 +319,7 @@ impl Database for Tree {
|
||||
|
||||
// inserts 0 if not present
|
||||
fn increment_last_index(&mut self, script_type: ScriptType) -> Result<u32, Error> {
|
||||
let key = SledKey::LastIndex(script_type).as_sled_key();
|
||||
let key = MapKey::LastIndex(script_type).as_map_key();
|
||||
self.update_and_fetch(key, |prev| {
|
||||
let new = match prev {
|
||||
Some(b) => {
|
||||
|
||||
Reference in New Issue
Block a user