Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ bitcoin_support=["bitcoin","serde","serde_cbor"]

[dependencies]
rand="0.7"
byteorder="1.2"
lru-cache = "0.1.1"
bitcoin_hashes = "0.7"
bitcoin = { version= "0.21", features=["serde"], optional = true }
byteorder="1.3"
lru-cache = "0.1.2"
bitcoin_hashes = "0.9"
bitcoin = { version = "0.25", features=["use-serde"], optional = true }
serde = { version ="1", optional = true }
serde_cbor = { version="0.10", optional = true }
serde_cbor = { version="0.11", optional = true }

[dev-dependencies]
hex = "0.3"
hex = "0.4"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get rid of this dependency like it was done for rust-bitcoin (rust-bitcoin/rust-bitcoin@07fb14b) and use Vec::from_hex instead

28 changes: 12 additions & 16 deletions src/bitcoin_adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ use PRef;
use HammersbaldAPI;
use HammersbaldIterator;

use bitcoin_hashes::sha256d;

use bitcoin::{
BitcoinHash
};
use bitcoin::hashes::sha256d;

use serde::Serialize;
use serde::de::DeserializeOwned;
Expand All @@ -45,15 +41,15 @@ impl BitcoinAdaptor {
}

/// Store some bitcoin object that has a bitcoin hash
pub fn put_hash_keyed<T>(&mut self, encodable: &T) -> Result<PRef, Box<dyn Error>>
where T: Serialize + BitcoinHash {
Ok(self.hammersbald.put_keyed(&encodable.bitcoin_hash()[..], serde_cbor::to_vec(encodable)?.as_slice())?)
pub fn put_hash_keyed<ID: Into<sha256d::Hash>, T>(&mut self, id: ID, encodable: &T) -> Result<PRef, Box<dyn Error>>
where T: Serialize {
Ok(self.hammersbald.put_keyed(&id.into()[..], serde_cbor::to_vec(encodable)?.as_slice())?)
}

/// Retrieve a bitcoin_object with its hash
pub fn get_hash_keyed<T>(&self, id: &sha256d::Hash) -> Result<Option<(PRef, T)>, Box<dyn Error>>
where T: DeserializeOwned + BitcoinHash{
if let Some((pref, data)) = self.hammersbald.get_keyed(&id[..])? {
pub fn get_hash_keyed<ID: Into<sha256d::Hash>, T>(&self, id: ID) -> Result<Option<(PRef, T)>, Box<dyn Error>>
where T: DeserializeOwned {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the BitcoinHash removed here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, this will be removed in the next version

if let Some((pref, data)) = self.hammersbald.get_keyed(&id.into()[..])? {
return Ok(Some((pref, serde_cbor::from_slice(data.as_slice())?)))
}
Ok(None)
Expand Down Expand Up @@ -170,7 +166,7 @@ mod test {
use bitcoin::network::constants::Network;
use transient;
use super::*;
use bitcoin::consensus::deserialize;
use bitcoin::{consensus::deserialize};

#[test]
pub fn bitcoin_test () {
Expand All @@ -189,9 +185,9 @@ mod test {
assert_eq!(tx, tx2);

// store the transaction with its hash as key
let txref2 = bdb.put_hash_keyed(&tx).unwrap();
let txref2 = bdb.put_hash_keyed(tx.txid(), &tx).unwrap();
// retrieve by hash
if let Some((pref, tx3)) = bdb.get_hash_keyed::<Transaction>(&tx.bitcoin_hash()).unwrap() {
if let Some((pref, tx3)) = bdb.get_hash_keyed::<_, Transaction>(tx.txid()).unwrap() {
assert_eq!(pref, txref2);
assert_eq!(tx3, tx);
}
Expand All @@ -201,9 +197,9 @@ mod test {

let genesis = genesis_block(Network::Bitcoin);
// store the genesist block
bdb.put_hash_keyed(&genesis).unwrap();
bdb.put_hash_keyed(genesis.block_hash(), &genesis).unwrap();
// find it
if let Some((_, block)) = bdb.get_hash_keyed::<Block>(&genesis.bitcoin_hash()).unwrap() {
if let Some((_, block)) = bdb.get_hash_keyed::<_, Block>(genesis.block_hash()).unwrap() {
assert_eq!(block, genesis);
}
else {
Expand Down