-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade bitcoin version and remove lightning #40
Open
jrawsthorne
wants to merge
4
commits into
rust-bitcoin:master
Choose a base branch
from
jrawsthorne:upgrade-bitcoin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,8 @@ use std::path::Path; | |
|
||
use bitcoin::{BitcoinHash, Network}; | ||
use bitcoin::blockdata::block::BlockHeader; | ||
use bitcoin::blockdata::constants::genesis_block; | ||
use bitcoin::{BlockHash, blockdata::constants::genesis_block}; | ||
|
||
use bitcoin_hashes::sha256d; | ||
use hammersbald::{BitcoinAdaptor, HammersbaldAPI, persistent, transient}; | ||
|
||
use crate::error::Error; | ||
|
@@ -80,7 +79,7 @@ impl ChainDB { | |
while let Some(stored) = self.fetch_header(&h)? { | ||
debug!("read stored header {}", &stored.bitcoin_hash()); | ||
self.headercache.add_header_unchecked(&h, &stored); | ||
if stored.header.prev_blockhash != sha256d::Hash::default() { | ||
if stored.header.prev_blockhash != BlockHash::default() { | ||
h = stored.header.prev_blockhash; | ||
} else { | ||
break; | ||
|
@@ -103,7 +102,7 @@ impl ChainDB { | |
let genesis = genesis_block(self.network).header; | ||
if let Some((cached, _, _)) = self.headercache.add_header(&genesis)? { | ||
info!("initialized with genesis header {}", genesis.bitcoin_hash()); | ||
self.db.put_hash_keyed(&cached.stored)?; | ||
self.db.put_hash_keyed(&cached.bitcoin_hash().as_hash(), &cached.stored)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we preserve the API here? |
||
self.db.batch()?; | ||
self.store_header_tip(&cached.bitcoin_hash())?; | ||
self.db.batch()?; | ||
|
@@ -115,9 +114,9 @@ impl ChainDB { | |
} | ||
|
||
/// Store a header | ||
pub fn add_header(&mut self, header: &BlockHeader) -> Result<Option<(StoredHeader, Option<Vec<sha256d::Hash>>, Option<Vec<sha256d::Hash>>)>, Error> { | ||
pub fn add_header(&mut self, header: &BlockHeader) -> Result<Option<(StoredHeader, Option<Vec<BlockHash>>, Option<Vec<BlockHash>>)>, Error> { | ||
if let Some((cached, unwinds, forward)) = self.headercache.add_header(header)? { | ||
self.db.put_hash_keyed(&cached.stored)?; | ||
self.db.put_hash_keyed(&cached.bitcoin_hash().as_hash(), &cached.stored)?; | ||
if let Some(forward) = forward.clone() { | ||
if forward.len() > 0 { | ||
self.store_header_tip(forward.last().unwrap())?; | ||
|
@@ -129,7 +128,7 @@ impl ChainDB { | |
} | ||
|
||
/// return position of hash on trunk if hash is on trunk | ||
pub fn pos_on_trunk(&self, hash: &sha256d::Hash) -> Option<u32> { | ||
pub fn pos_on_trunk(&self, hash: &BlockHash) -> Option<u32> { | ||
self.headercache.pos_on_trunk(hash) | ||
} | ||
|
||
|
@@ -149,7 +148,7 @@ impl ChainDB { | |
} | ||
|
||
/// Fetch a header by its id from cache | ||
pub fn get_header(&self, id: &sha256d::Hash) -> Option<CachedHeader> { | ||
pub fn get_header(&self, id: &BlockHash) -> Option<CachedHeader> { | ||
self.headercache.get_header(id) | ||
} | ||
|
||
|
@@ -159,24 +158,24 @@ impl ChainDB { | |
} | ||
|
||
/// locator for getheaders message | ||
pub fn header_locators(&self) -> Vec<sha256d::Hash> { | ||
pub fn header_locators(&self) -> Vec<BlockHash> { | ||
self.headercache.locator_hashes() | ||
} | ||
|
||
/// Store the header id with most work | ||
pub fn store_header_tip(&mut self, tip: &sha256d::Hash) -> Result<(), Error> { | ||
pub fn store_header_tip(&mut self, tip: &BlockHash) -> Result<(), Error> { | ||
self.db.put_keyed_encodable(HEADER_TIP_KEY, tip)?; | ||
Ok(()) | ||
} | ||
|
||
/// Find header id with most work | ||
pub fn fetch_header_tip(&self) -> Result<Option<sha256d::Hash>, Error> { | ||
Ok(self.db.get_keyed_decodable::<sha256d::Hash>(HEADER_TIP_KEY)?.map(|(_, h)| h.clone())) | ||
pub fn fetch_header_tip(&self) -> Result<Option<BlockHash>, Error> { | ||
Ok(self.db.get_keyed_decodable::<BlockHash>(HEADER_TIP_KEY)?.map(|(_, h)| h.clone())) | ||
} | ||
|
||
/// Read header from the DB | ||
pub fn fetch_header(&self, id: &sha256d::Hash) -> Result<Option<StoredHeader>, Error> { | ||
Ok(self.db.get_hash_keyed::<StoredHeader>(id)?.map(|(_, header)| header)) | ||
pub fn fetch_header(&self, id: &BlockHash) -> Result<Option<StoredHeader>, Error> { | ||
Ok(self.db.get_hash_keyed::<StoredHeader>(&id.as_hash())?.map(|(_, header)| header)) | ||
} | ||
|
||
/// Shutdown db | ||
|
@@ -198,8 +197,8 @@ pub struct StoredHeader { | |
} | ||
|
||
// need to implement if put_hash_keyed and get_hash_keyed should be used | ||
impl BitcoinHash for StoredHeader { | ||
fn bitcoin_hash(&self) -> sha256d::Hash { | ||
impl BitcoinHash<BlockHash> for StoredHeader { | ||
fn bitcoin_hash(&self) -> BlockHash { | ||
self.header.bitcoin_hash() | ||
} | ||
} | ||
|
@@ -209,7 +208,6 @@ const HEADER_TIP_KEY: &[u8] = &[0u8; 1]; | |
#[cfg(test)] | ||
mod test { | ||
use bitcoin::{Network, BitcoinHash}; | ||
use bitcoin_hashes::sha256d::Hash; | ||
use bitcoin::blockdata::constants::genesis_block; | ||
|
||
use crate::chaindb::ChainDB; | ||
|
@@ -234,7 +232,7 @@ mod test { | |
let genesis_header = genesis_block(network).header; | ||
|
||
let mut chaindb = ChainDB::mem(network).unwrap(); | ||
let missing_tip_header_hash: Hash = "6cfb35868c4465b7c289d7d5641563aa973db6a929655282a7bf95c8257f53ef".parse().unwrap(); | ||
let missing_tip_header_hash = "6cfb35868c4465b7c289d7d5641563aa973db6a929655282a7bf95c8257f53ef".parse().unwrap(); | ||
chaindb.store_header_tip(&missing_tip_header_hash).unwrap(); | ||
|
||
chaindb.init().unwrap(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should change once the
upgrade-bitcoin
branch is merged in the hammersbald repo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'll change it when that gets merged. Wanted to make sure tests passed