Rename ScriptType to KeychainKind

This avoids confusion with the "type of script".
This commit is contained in:
Alekos Filini
2020-12-14 17:14:24 +01:00
parent 7adaaf227c
commit 1713d621d4
19 changed files with 322 additions and 333 deletions

View File

@@ -121,7 +121,7 @@ impl BatchOperations for AnyDatabase {
fn set_script_pubkey(
&mut self,
script: &Script,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<(), Error> {
impl_inner_method!(
@@ -129,7 +129,7 @@ impl BatchOperations for AnyDatabase {
self,
set_script_pubkey,
script,
script_type,
keychain,
child
)
}
@@ -142,27 +142,27 @@ impl BatchOperations for AnyDatabase {
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error> {
impl_inner_method!(AnyDatabase, self, set_tx, transaction)
}
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error> {
impl_inner_method!(AnyDatabase, self, set_last_index, script_type, value)
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error> {
impl_inner_method!(AnyDatabase, self, set_last_index, keychain, value)
}
fn del_script_pubkey_from_path(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<Option<Script>, Error> {
impl_inner_method!(
AnyDatabase,
self,
del_script_pubkey_from_path,
script_type,
keychain,
child
)
}
fn del_path_from_script_pubkey(
&mut self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error> {
) -> Result<Option<(KeychainKind, u32)>, Error> {
impl_inner_method!(AnyDatabase, self, del_path_from_script_pubkey, script)
}
fn del_utxo(&mut self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
@@ -178,28 +178,28 @@ impl BatchOperations for AnyDatabase {
) -> Result<Option<TransactionDetails>, Error> {
impl_inner_method!(AnyDatabase, self, del_tx, txid, include_raw)
}
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error> {
impl_inner_method!(AnyDatabase, self, del_last_index, script_type)
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
impl_inner_method!(AnyDatabase, self, del_last_index, keychain)
}
}
impl Database for AnyDatabase {
fn check_descriptor_checksum<B: AsRef<[u8]>>(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
bytes: B,
) -> Result<(), Error> {
impl_inner_method!(
AnyDatabase,
self,
check_descriptor_checksum,
script_type,
keychain,
bytes
)
}
fn iter_script_pubkeys(&self, script_type: Option<ScriptType>) -> Result<Vec<Script>, Error> {
impl_inner_method!(AnyDatabase, self, iter_script_pubkeys, script_type)
fn iter_script_pubkeys(&self, keychain: Option<KeychainKind>) -> Result<Vec<Script>, Error> {
impl_inner_method!(AnyDatabase, self, iter_script_pubkeys, keychain)
}
fn iter_utxos(&self) -> Result<Vec<UTXO>, Error> {
impl_inner_method!(AnyDatabase, self, iter_utxos)
@@ -213,21 +213,21 @@ impl Database for AnyDatabase {
fn get_script_pubkey_from_path(
&self,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<Option<Script>, Error> {
impl_inner_method!(
AnyDatabase,
self,
get_script_pubkey_from_path,
script_type,
keychain,
child
)
}
fn get_path_from_script_pubkey(
&self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error> {
) -> Result<Option<(KeychainKind, u32)>, Error> {
impl_inner_method!(AnyDatabase, self, get_path_from_script_pubkey, script)
}
fn get_utxo(&self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
@@ -239,12 +239,12 @@ impl Database for AnyDatabase {
fn get_tx(&self, txid: &Txid, include_raw: bool) -> Result<Option<TransactionDetails>, Error> {
impl_inner_method!(AnyDatabase, self, get_tx, txid, include_raw)
}
fn get_last_index(&self, script_type: ScriptType) -> Result<Option<u32>, Error> {
impl_inner_method!(AnyDatabase, self, get_last_index, script_type)
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
impl_inner_method!(AnyDatabase, self, get_last_index, keychain)
}
fn increment_last_index(&mut self, script_type: ScriptType) -> Result<u32, Error> {
impl_inner_method!(AnyDatabase, self, increment_last_index, script_type)
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
impl_inner_method!(AnyDatabase, self, increment_last_index, keychain)
}
}
@@ -252,17 +252,10 @@ impl BatchOperations for AnyBatch {
fn set_script_pubkey(
&mut self,
script: &Script,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<(), Error> {
impl_inner_method!(
AnyBatch,
self,
set_script_pubkey,
script,
script_type,
child
)
impl_inner_method!(AnyBatch, self, set_script_pubkey, script, keychain, child)
}
fn set_utxo(&mut self, utxo: &UTXO) -> Result<(), Error> {
impl_inner_method!(AnyBatch, self, set_utxo, utxo)
@@ -273,27 +266,21 @@ impl BatchOperations for AnyBatch {
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error> {
impl_inner_method!(AnyBatch, self, set_tx, transaction)
}
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error> {
impl_inner_method!(AnyBatch, self, set_last_index, script_type, value)
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error> {
impl_inner_method!(AnyBatch, self, set_last_index, keychain, value)
}
fn del_script_pubkey_from_path(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<Option<Script>, Error> {
impl_inner_method!(
AnyBatch,
self,
del_script_pubkey_from_path,
script_type,
child
)
impl_inner_method!(AnyBatch, self, del_script_pubkey_from_path, keychain, child)
}
fn del_path_from_script_pubkey(
&mut self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error> {
) -> Result<Option<(KeychainKind, u32)>, Error> {
impl_inner_method!(AnyBatch, self, del_path_from_script_pubkey, script)
}
fn del_utxo(&mut self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
@@ -309,8 +296,8 @@ impl BatchOperations for AnyBatch {
) -> Result<Option<TransactionDetails>, Error> {
impl_inner_method!(AnyBatch, self, del_tx, txid, include_raw)
}
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error> {
impl_inner_method!(AnyBatch, self, del_last_index, script_type)
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
impl_inner_method!(AnyBatch, self, del_last_index, keychain)
}
}

View File

@@ -37,13 +37,13 @@ use crate::types::*;
macro_rules! impl_batch_operations {
( { $($after_insert:tt)* }, $process_delete:ident ) => {
fn set_script_pubkey(&mut self, script: &Script, script_type: ScriptType, path: u32) -> Result<(), Error> {
let key = MapKey::Path((Some(script_type), Some(path))).as_map_key();
fn set_script_pubkey(&mut self, script: &Script, keychain: KeychainKind, path: u32) -> Result<(), Error> {
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
self.insert(key, serialize(script))$($after_insert)*;
let key = MapKey::Script(Some(script)).as_map_key();
let value = json!({
"t": script_type,
"t": keychain,
"p": path,
});
self.insert(key, serde_json::to_vec(&value)?)$($after_insert)*;
@@ -55,7 +55,7 @@ macro_rules! impl_batch_operations {
let key = MapKey::UTXO(Some(&utxo.outpoint)).as_map_key();
let value = json!({
"t": utxo.txout,
"i": utxo.script_type,
"i": utxo.keychain,
});
self.insert(key, serde_json::to_vec(&value)?)$($after_insert)*;
@@ -88,22 +88,22 @@ macro_rules! impl_batch_operations {
Ok(())
}
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
self.insert(key, &value.to_be_bytes())$($after_insert)*;
Ok(())
}
fn del_script_pubkey_from_path(&mut self, script_type: ScriptType, path: u32) -> Result<Option<Script>, Error> {
let key = MapKey::Path((Some(script_type), Some(path))).as_map_key();
fn del_script_pubkey_from_path(&mut self, keychain: KeychainKind, path: u32) -> Result<Option<Script>, Error> {
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
let res = self.remove(key);
let res = $process_delete!(res);
Ok(res.map_or(Ok(None), |x| Some(deserialize(&x)).transpose())?)
}
fn del_path_from_script_pubkey(&mut self, script: &Script) -> Result<Option<(ScriptType, u32)>, Error> {
fn del_path_from_script_pubkey(&mut self, script: &Script) -> Result<Option<(KeychainKind, u32)>, Error> {
let key = MapKey::Script(Some(script)).as_map_key();
let res = self.remove(key);
let res = $process_delete!(res);
@@ -130,9 +130,9 @@ macro_rules! impl_batch_operations {
Some(b) => {
let mut val: serde_json::Value = serde_json::from_slice(&b)?;
let txout = serde_json::from_value(val["t"].take())?;
let script_type = serde_json::from_value(val["i"].take())?;
let keychain = serde_json::from_value(val["i"].take())?;
Ok(Some(UTXO { outpoint: outpoint.clone(), txout, script_type }))
Ok(Some(UTXO { outpoint: outpoint.clone(), txout, keychain }))
}
}
}
@@ -167,8 +167,8 @@ macro_rules! impl_batch_operations {
}
}
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
let res = self.remove(key);
let res = $process_delete!(res);
@@ -206,10 +206,10 @@ impl BatchOperations for Batch {
impl Database for Tree {
fn check_descriptor_checksum<B: AsRef<[u8]>>(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
bytes: B,
) -> Result<(), Error> {
let key = MapKey::DescriptorChecksum(script_type).as_map_key();
let key = MapKey::DescriptorChecksum(keychain).as_map_key();
let prev = self.get(&key)?.map(|x| x.to_vec());
if let Some(val) = prev {
@@ -224,8 +224,8 @@ impl Database for Tree {
}
}
fn iter_script_pubkeys(&self, script_type: Option<ScriptType>) -> Result<Vec<Script>, Error> {
let key = MapKey::Path((script_type, None)).as_map_key();
fn iter_script_pubkeys(&self, keychain: Option<KeychainKind>) -> Result<Vec<Script>, Error> {
let key = MapKey::Path((keychain, None)).as_map_key();
self.scan_prefix(key)
.map(|x| -> Result<_, Error> {
let (_, v) = x?;
@@ -243,12 +243,12 @@ impl Database for Tree {
let mut val: serde_json::Value = serde_json::from_slice(&v)?;
let txout = serde_json::from_value(val["t"].take())?;
let script_type = serde_json::from_value(val["i"].take())?;
let keychain = serde_json::from_value(val["i"].take())?;
Ok(UTXO {
outpoint,
txout,
script_type,
keychain,
})
})
.collect()
@@ -282,17 +282,17 @@ impl Database for Tree {
fn get_script_pubkey_from_path(
&self,
script_type: ScriptType,
keychain: KeychainKind,
path: u32,
) -> Result<Option<Script>, Error> {
let key = MapKey::Path((Some(script_type), Some(path))).as_map_key();
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
Ok(self.get(key)?.map(|b| deserialize(&b)).transpose()?)
}
fn get_path_from_script_pubkey(
&self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error> {
) -> Result<Option<(KeychainKind, u32)>, Error> {
let key = MapKey::Script(Some(script)).as_map_key();
self.get(key)?
.map(|b| -> Result<_, Error> {
@@ -311,12 +311,12 @@ impl Database for Tree {
.map(|b| -> Result<_, Error> {
let mut val: serde_json::Value = serde_json::from_slice(&b)?;
let txout = serde_json::from_value(val["t"].take())?;
let script_type = serde_json::from_value(val["i"].take())?;
let keychain = serde_json::from_value(val["i"].take())?;
Ok(UTXO {
outpoint: *outpoint,
txout,
script_type,
keychain,
})
})
.transpose()
@@ -341,8 +341,8 @@ impl Database for Tree {
.transpose()
}
fn get_last_index(&self, script_type: ScriptType) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
self.get(key)?
.map(|b| -> Result<_, Error> {
let array: [u8; 4] = b
@@ -356,8 +356,8 @@ impl Database for Tree {
}
// inserts 0 if not present
fn increment_last_index(&mut self, script_type: ScriptType) -> Result<u32, Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
self.update_and_fetch(key, |prev| {
let new = match prev {
Some(b) => {

View File

@@ -47,13 +47,13 @@ use crate::types::*;
// descriptor checksum d{i,e} -> vec<u8>
pub(crate) enum MapKey<'a> {
Path((Option<ScriptType>, Option<u32>)),
Path((Option<KeychainKind>, Option<u32>)),
Script(Option<&'a Script>),
UTXO(Option<&'a OutPoint>),
RawTx(Option<&'a Txid>),
Transaction(Option<&'a Txid>),
LastIndex(ScriptType),
DescriptorChecksum(ScriptType),
LastIndex(KeychainKind),
DescriptorChecksum(KeychainKind),
}
impl MapKey<'_> {
@@ -141,15 +141,15 @@ impl BatchOperations for MemoryDatabase {
fn set_script_pubkey(
&mut self,
script: &Script,
script_type: ScriptType,
keychain: KeychainKind,
path: u32,
) -> Result<(), Error> {
let key = MapKey::Path((Some(script_type), Some(path))).as_map_key();
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
self.map.insert(key, Box::new(script.clone()));
let key = MapKey::Script(Some(script)).as_map_key();
let value = json!({
"t": script_type,
"t": keychain,
"p": path,
});
self.map.insert(key, Box::new(value));
@@ -160,7 +160,7 @@ impl BatchOperations for MemoryDatabase {
fn set_utxo(&mut self, utxo: &UTXO) -> Result<(), Error> {
let key = MapKey::UTXO(Some(&utxo.outpoint)).as_map_key();
self.map
.insert(key, Box::new((utxo.txout.clone(), utxo.script_type)));
.insert(key, Box::new((utxo.txout.clone(), utxo.keychain)));
Ok(())
}
@@ -186,8 +186,8 @@ impl BatchOperations for MemoryDatabase {
Ok(())
}
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
self.map.insert(key, Box::new(value));
Ok(())
@@ -195,10 +195,10 @@ impl BatchOperations for MemoryDatabase {
fn del_script_pubkey_from_path(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
path: u32,
) -> Result<Option<Script>, Error> {
let key = MapKey::Path((Some(script_type), Some(path))).as_map_key();
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
let res = self.map.remove(&key);
self.deleted_keys.push(key);
@@ -207,7 +207,7 @@ impl BatchOperations for MemoryDatabase {
fn del_path_from_script_pubkey(
&mut self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error> {
) -> Result<Option<(KeychainKind, u32)>, Error> {
let key = MapKey::Script(Some(script)).as_map_key();
let res = self.map.remove(&key);
self.deleted_keys.push(key);
@@ -231,11 +231,11 @@ impl BatchOperations for MemoryDatabase {
match res {
None => Ok(None),
Some(b) => {
let (txout, script_type) = b.downcast_ref().cloned().unwrap();
let (txout, keychain) = b.downcast_ref().cloned().unwrap();
Ok(Some(UTXO {
outpoint: *outpoint,
txout,
script_type,
keychain,
}))
}
}
@@ -272,8 +272,8 @@ impl BatchOperations for MemoryDatabase {
}
}
}
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
let res = self.map.remove(&key);
self.deleted_keys.push(key);
@@ -287,10 +287,10 @@ impl BatchOperations for MemoryDatabase {
impl Database for MemoryDatabase {
fn check_descriptor_checksum<B: AsRef<[u8]>>(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
bytes: B,
) -> Result<(), Error> {
let key = MapKey::DescriptorChecksum(script_type).as_map_key();
let key = MapKey::DescriptorChecksum(keychain).as_map_key();
let prev = self
.map
@@ -308,8 +308,8 @@ impl Database for MemoryDatabase {
}
}
fn iter_script_pubkeys(&self, script_type: Option<ScriptType>) -> Result<Vec<Script>, Error> {
let key = MapKey::Path((script_type, None)).as_map_key();
fn iter_script_pubkeys(&self, keychain: Option<KeychainKind>) -> Result<Vec<Script>, Error> {
let key = MapKey::Path((keychain, None)).as_map_key();
self.map
.range::<Vec<u8>, _>((Included(&key), Excluded(&after(&key))))
.map(|(_, v)| Ok(v.downcast_ref().cloned().unwrap()))
@@ -322,11 +322,11 @@ impl Database for MemoryDatabase {
.range::<Vec<u8>, _>((Included(&key), Excluded(&after(&key))))
.map(|(k, v)| {
let outpoint = deserialize(&k[1..]).unwrap();
let (txout, script_type) = v.downcast_ref().cloned().unwrap();
let (txout, keychain) = v.downcast_ref().cloned().unwrap();
Ok(UTXO {
outpoint,
txout,
script_type,
keychain,
})
})
.collect()
@@ -358,10 +358,10 @@ impl Database for MemoryDatabase {
fn get_script_pubkey_from_path(
&self,
script_type: ScriptType,
keychain: KeychainKind,
path: u32,
) -> Result<Option<Script>, Error> {
let key = MapKey::Path((Some(script_type), Some(path))).as_map_key();
let key = MapKey::Path((Some(keychain), Some(path))).as_map_key();
Ok(self
.map
.get(&key)
@@ -371,7 +371,7 @@ impl Database for MemoryDatabase {
fn get_path_from_script_pubkey(
&self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error> {
) -> Result<Option<(KeychainKind, u32)>, Error> {
let key = MapKey::Script(Some(script)).as_map_key();
Ok(self.map.get(&key).map(|b| {
let mut val: serde_json::Value = b.downcast_ref().cloned().unwrap();
@@ -385,11 +385,11 @@ impl Database for MemoryDatabase {
fn get_utxo(&self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error> {
let key = MapKey::UTXO(Some(outpoint)).as_map_key();
Ok(self.map.get(&key).map(|b| {
let (txout, script_type) = b.downcast_ref().cloned().unwrap();
let (txout, keychain) = b.downcast_ref().cloned().unwrap();
UTXO {
outpoint: *outpoint,
txout,
script_type,
keychain,
}
}))
}
@@ -414,14 +414,14 @@ impl Database for MemoryDatabase {
}))
}
fn get_last_index(&self, script_type: ScriptType) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
Ok(self.map.get(&key).map(|b| *b.downcast_ref().unwrap()))
}
// inserts 0 if not present
fn increment_last_index(&mut self, script_type: ScriptType) -> Result<u32, Error> {
let key = MapKey::LastIndex(script_type).as_map_key();
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error> {
let key = MapKey::LastIndex(keychain).as_map_key();
let value = self
.map
.entry(key)
@@ -507,7 +507,7 @@ impl MemoryDatabase {
txid,
vout: vout as u32,
},
script_type: ScriptType::External,
keychain: KeychainKind::External,
})
.unwrap();
}

View File

@@ -61,7 +61,7 @@ pub trait BatchOperations {
fn set_script_pubkey(
&mut self,
script: &Script,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<(), Error>;
/// Store a [`UTXO`]
@@ -71,12 +71,12 @@ pub trait BatchOperations {
/// Store the metadata of a transaction
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error>;
/// Store the last derivation index for a given script type
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error>;
fn set_last_index(&mut self, keychain: KeychainKind, value: u32) -> Result<(), Error>;
/// Delete a script_pubkey given the script type and its child number
fn del_script_pubkey_from_path(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<Option<Script>, Error>;
/// Delete the data related to a specific script_pubkey, meaning the script type and the child
@@ -84,7 +84,7 @@ pub trait BatchOperations {
fn del_path_from_script_pubkey(
&mut self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error>;
) -> Result<Option<(KeychainKind, u32)>, Error>;
/// Delete a [`UTXO`] given its [`OutPoint`]
fn del_utxo(&mut self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error>;
/// Delete a raw transaction given its [`Txid`]
@@ -96,7 +96,7 @@ pub trait BatchOperations {
include_raw: bool,
) -> Result<Option<TransactionDetails>, Error>;
/// Delete the last derivation index for a script type
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error>;
fn del_last_index(&mut self, keychain: KeychainKind) -> Result<Option<u32>, Error>;
}
/// Trait for reading data from a database
@@ -110,12 +110,12 @@ pub trait Database: BatchOperations {
/// next time.
fn check_descriptor_checksum<B: AsRef<[u8]>>(
&mut self,
script_type: ScriptType,
keychain: KeychainKind,
bytes: B,
) -> Result<(), Error>;
/// Return the list of script_pubkeys
fn iter_script_pubkeys(&self, script_type: Option<ScriptType>) -> Result<Vec<Script>, Error>;
fn iter_script_pubkeys(&self, keychain: Option<KeychainKind>) -> Result<Vec<Script>, Error>;
/// Return the list of [`UTXO`]s
fn iter_utxos(&self) -> Result<Vec<UTXO>, Error>;
/// Return the list of raw transactions
@@ -126,14 +126,14 @@ pub trait Database: BatchOperations {
/// Fetch a script_pubkey given the script type and child number
fn get_script_pubkey_from_path(
&self,
script_type: ScriptType,
keychain: KeychainKind,
child: u32,
) -> Result<Option<Script>, Error>;
/// Fetch the script type and child number of a given script_pubkey
fn get_path_from_script_pubkey(
&self,
script: &Script,
) -> Result<Option<(ScriptType, u32)>, Error>;
) -> Result<Option<(KeychainKind, u32)>, Error>;
/// Fetch a [`UTXO`] given its [`OutPoint`]
fn get_utxo(&self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error>;
/// Fetch a raw transaction given its [`Txid`]
@@ -141,12 +141,12 @@ pub trait Database: BatchOperations {
/// Fetch the transaction metadata and optionally also the raw transaction
fn get_tx(&self, txid: &Txid, include_raw: bool) -> Result<Option<TransactionDetails>, Error>;
/// Return the last defivation index for a script type
fn get_last_index(&self, script_type: ScriptType) -> Result<Option<u32>, Error>;
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error>;
/// Increment the last derivation index for a script type and returns it
///
/// It should insert and return `0` if not present in the database
fn increment_last_index(&mut self, script_type: ScriptType) -> Result<u32, Error>;
fn increment_last_index(&mut self, keychain: KeychainKind) -> Result<u32, Error>;
}
/// Trait for a database that supports batch operations
@@ -217,17 +217,17 @@ pub mod test {
Vec::<u8>::from_hex("76a91402306a7c23f3e8010de41e9e591348bb83f11daa88ac").unwrap(),
);
let path = 42;
let script_type = ScriptType::External;
let keychain = KeychainKind::External;
tree.set_script_pubkey(&script, script_type, path).unwrap();
tree.set_script_pubkey(&script, keychain, path).unwrap();
assert_eq!(
tree.get_script_pubkey_from_path(script_type, path).unwrap(),
tree.get_script_pubkey_from_path(keychain, path).unwrap(),
Some(script.clone())
);
assert_eq!(
tree.get_path_from_script_pubkey(&script).unwrap(),
Some((script_type, path.clone()))
Some((keychain, path.clone()))
);
}
@@ -238,12 +238,12 @@ pub mod test {
Vec::<u8>::from_hex("76a91402306a7c23f3e8010de41e9e591348bb83f11daa88ac").unwrap(),
);
let path = 42;
let script_type = ScriptType::External;
let keychain = KeychainKind::External;
batch.set_script_pubkey(&script, script_type, path).unwrap();
batch.set_script_pubkey(&script, keychain, path).unwrap();
assert_eq!(
tree.get_script_pubkey_from_path(script_type, path).unwrap(),
tree.get_script_pubkey_from_path(keychain, path).unwrap(),
None
);
assert_eq!(tree.get_path_from_script_pubkey(&script).unwrap(), None);
@@ -251,12 +251,12 @@ pub mod test {
tree.commit_batch(batch).unwrap();
assert_eq!(
tree.get_script_pubkey_from_path(script_type, path).unwrap(),
tree.get_script_pubkey_from_path(keychain, path).unwrap(),
Some(script.clone())
);
assert_eq!(
tree.get_path_from_script_pubkey(&script).unwrap(),
Some((script_type, path.clone()))
Some((keychain, path.clone()))
);
}
@@ -265,9 +265,9 @@ pub mod test {
Vec::<u8>::from_hex("76a91402306a7c23f3e8010de41e9e591348bb83f11daa88ac").unwrap(),
);
let path = 42;
let script_type = ScriptType::External;
let keychain = KeychainKind::External;
tree.set_script_pubkey(&script, script_type, path).unwrap();
tree.set_script_pubkey(&script, keychain, path).unwrap();
assert_eq!(tree.iter_script_pubkeys(None).unwrap().len(), 1);
}
@@ -277,12 +277,12 @@ pub mod test {
Vec::<u8>::from_hex("76a91402306a7c23f3e8010de41e9e591348bb83f11daa88ac").unwrap(),
);
let path = 42;
let script_type = ScriptType::External;
let keychain = KeychainKind::External;
tree.set_script_pubkey(&script, script_type, path).unwrap();
tree.set_script_pubkey(&script, keychain, path).unwrap();
assert_eq!(tree.iter_script_pubkeys(None).unwrap().len(), 1);
tree.del_script_pubkey_from_path(script_type, path).unwrap();
tree.del_script_pubkey_from_path(keychain, path).unwrap();
assert_eq!(tree.iter_script_pubkeys(None).unwrap().len(), 0);
}
@@ -301,7 +301,7 @@ pub mod test {
let utxo = UTXO {
txout,
outpoint,
script_type: ScriptType::External,
keychain: KeychainKind::External,
};
tree.set_utxo(&utxo).unwrap();
@@ -356,24 +356,27 @@ pub mod test {
}
pub fn test_last_index<D: Database>(mut tree: D) {
tree.set_last_index(ScriptType::External, 1337).unwrap();
tree.set_last_index(KeychainKind::External, 1337).unwrap();
assert_eq!(
tree.get_last_index(ScriptType::External).unwrap(),
tree.get_last_index(KeychainKind::External).unwrap(),
Some(1337)
);
assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), None);
assert_eq!(tree.get_last_index(KeychainKind::Internal).unwrap(), None);
let res = tree.increment_last_index(ScriptType::External).unwrap();
let res = tree.increment_last_index(KeychainKind::External).unwrap();
assert_eq!(res, 1338);
let res = tree.increment_last_index(ScriptType::Internal).unwrap();
let res = tree.increment_last_index(KeychainKind::Internal).unwrap();
assert_eq!(res, 0);
assert_eq!(
tree.get_last_index(ScriptType::External).unwrap(),
tree.get_last_index(KeychainKind::External).unwrap(),
Some(1338)
);
assert_eq!(tree.get_last_index(ScriptType::Internal).unwrap(), Some(0));
assert_eq!(
tree.get_last_index(KeychainKind::Internal).unwrap(),
Some(0)
);
}
// TODO: more tests...