2020-02-05 11:59:02 +01:00
|
|
|
use bitcoin::hash_types::Txid;
|
2020-05-03 16:15:11 +02:00
|
|
|
use bitcoin::{OutPoint, Script, Transaction, TxOut};
|
2020-02-05 11:59:02 +01:00
|
|
|
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::types::*;
|
|
|
|
|
|
|
|
#[cfg(any(feature = "key-value-db", feature = "default"))]
|
|
|
|
pub mod keyvalue;
|
2020-04-29 18:06:09 +02:00
|
|
|
pub mod memory;
|
2020-02-05 11:59:02 +01:00
|
|
|
|
|
|
|
pub trait BatchOperations {
|
2020-06-30 15:21:14 +02:00
|
|
|
fn set_script_pubkey(
|
2020-02-05 11:59:02 +01:00
|
|
|
&mut self,
|
2020-02-06 12:17:28 +01:00
|
|
|
script: &Script,
|
2020-02-05 11:59:02 +01:00
|
|
|
script_type: ScriptType,
|
2020-06-30 15:21:14 +02:00
|
|
|
child: u32,
|
2020-02-05 11:59:02 +01:00
|
|
|
) -> Result<(), Error>;
|
2020-02-06 12:17:28 +01:00
|
|
|
fn set_utxo(&mut self, utxo: &UTXO) -> Result<(), Error>;
|
|
|
|
fn set_raw_tx(&mut self, transaction: &Transaction) -> Result<(), Error>;
|
|
|
|
fn set_tx(&mut self, transaction: &TransactionDetails) -> Result<(), Error>;
|
2020-02-05 11:59:02 +01:00
|
|
|
fn set_last_index(&mut self, script_type: ScriptType, value: u32) -> Result<(), Error>;
|
|
|
|
|
2020-06-30 15:21:14 +02:00
|
|
|
fn del_script_pubkey_from_path(
|
2020-02-05 11:59:02 +01:00
|
|
|
&mut self,
|
|
|
|
script_type: ScriptType,
|
2020-06-30 15:21:14 +02:00
|
|
|
child: u32,
|
2020-02-05 11:59:02 +01:00
|
|
|
) -> Result<Option<Script>, Error>;
|
|
|
|
fn del_path_from_script_pubkey(
|
|
|
|
&mut self,
|
|
|
|
script: &Script,
|
2020-06-30 15:21:14 +02:00
|
|
|
) -> Result<Option<(ScriptType, u32)>, Error>;
|
2020-02-05 11:59:02 +01:00
|
|
|
fn del_utxo(&mut self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error>;
|
|
|
|
fn del_raw_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error>;
|
|
|
|
fn del_tx(
|
|
|
|
&mut self,
|
|
|
|
txid: &Txid,
|
|
|
|
include_raw: bool,
|
|
|
|
) -> Result<Option<TransactionDetails>, Error>;
|
|
|
|
fn del_last_index(&mut self, script_type: ScriptType) -> Result<Option<u32>, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Database: BatchOperations {
|
2020-02-15 21:27:51 +01:00
|
|
|
fn check_descriptor_checksum<B: AsRef<[u8]>>(
|
|
|
|
&mut self,
|
|
|
|
script_type: ScriptType,
|
|
|
|
bytes: B,
|
|
|
|
) -> Result<(), Error>;
|
|
|
|
|
2020-02-07 23:22:28 +01:00
|
|
|
fn iter_script_pubkeys(&self, script_type: Option<ScriptType>) -> Result<Vec<Script>, Error>;
|
|
|
|
fn iter_utxos(&self) -> Result<Vec<UTXO>, Error>;
|
|
|
|
fn iter_raw_txs(&self) -> Result<Vec<Transaction>, Error>;
|
|
|
|
fn iter_txs(&self, include_raw: bool) -> Result<Vec<TransactionDetails>, Error>;
|
2020-02-05 11:59:02 +01:00
|
|
|
|
2020-06-30 15:21:14 +02:00
|
|
|
fn get_script_pubkey_from_path(
|
2020-02-05 11:59:02 +01:00
|
|
|
&self,
|
|
|
|
script_type: ScriptType,
|
2020-06-30 15:21:14 +02:00
|
|
|
child: u32,
|
2020-02-05 11:59:02 +01:00
|
|
|
) -> Result<Option<Script>, Error>;
|
|
|
|
fn get_path_from_script_pubkey(
|
|
|
|
&self,
|
|
|
|
script: &Script,
|
2020-06-30 15:21:14 +02:00
|
|
|
) -> Result<Option<(ScriptType, u32)>, Error>;
|
2020-02-05 11:59:02 +01:00
|
|
|
fn get_utxo(&self, outpoint: &OutPoint) -> Result<Option<UTXO>, Error>;
|
|
|
|
fn get_raw_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error>;
|
|
|
|
fn get_tx(&self, txid: &Txid, include_raw: bool) -> Result<Option<TransactionDetails>, Error>;
|
|
|
|
fn get_last_index(&self, script_type: ScriptType) -> Result<Option<u32>, Error>;
|
|
|
|
|
|
|
|
// inserts 0 if not present
|
2020-02-06 12:17:28 +01:00
|
|
|
fn increment_last_index(&mut self, script_type: ScriptType) -> Result<u32, Error>;
|
2020-02-05 11:59:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait BatchDatabase: Database {
|
|
|
|
type Batch: BatchOperations;
|
|
|
|
|
2020-02-06 12:17:28 +01:00
|
|
|
fn begin_batch(&self) -> Self::Batch;
|
|
|
|
fn commit_batch(&mut self, batch: Self::Batch) -> Result<(), Error>;
|
2020-02-05 11:59:02 +01:00
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
|
|
|
|
pub trait DatabaseUtils: Database {
|
|
|
|
fn is_mine(&self, script: &Script) -> Result<bool, Error> {
|
|
|
|
self.get_path_from_script_pubkey(script)
|
|
|
|
.map(|o| o.is_some())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_raw_tx_or<F>(&self, txid: &Txid, f: F) -> Result<Option<Transaction>, Error>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> Result<Option<Transaction>, Error>,
|
|
|
|
{
|
|
|
|
self.get_tx(txid, true)?
|
|
|
|
.map(|t| t.transaction)
|
|
|
|
.flatten()
|
|
|
|
.map_or_else(f, |t| Ok(Some(t)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_previous_output(&self, outpoint: &OutPoint) -> Result<Option<TxOut>, Error> {
|
|
|
|
self.get_raw_tx(&outpoint.txid)?
|
|
|
|
.and_then(|previous_tx| {
|
|
|
|
if outpoint.vout as usize >= previous_tx.output.len() {
|
|
|
|
Some(Err(Error::InvalidOutpoint(outpoint.clone())))
|
|
|
|
} else {
|
|
|
|
Some(Ok(previous_tx.output[outpoint.vout as usize].clone()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Database> DatabaseUtils for T {}
|