Merge pull request #44 from tamasblummer/add_difficulty2

Add difficulty calculation
This commit is contained in:
Andrew Poelstra 2018-02-18 14:59:36 +00:00 committed by GitHub
commit 71bce7b067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 0 deletions

View File

@ -28,7 +28,9 @@ use util::hash::Sha256dHash;
use util::uint::Uint256; use util::uint::Uint256;
use network::encodable::VarInt; use network::encodable::VarInt;
use network::serialize::BitcoinHash; use network::serialize::BitcoinHash;
use network::constants::Network;
use blockdata::transaction::Transaction; use blockdata::transaction::Transaction;
use blockdata::constants::max_target;
/// A block header, which contains all the block's information except /// A block header, which contains all the block's information except
/// the actual transactions /// the actual transactions
@ -94,6 +96,11 @@ impl BlockHeader {
} }
} }
/// Compute the popular "difficulty" measure for mining
pub fn difficulty (&self, network: Network) -> u64 {
(max_target(network) / self.target()).low_u64()
}
/// Performs an SPV validation of a block, which confirms that the proof-of-work /// Performs an SPV validation of a block, which confirms that the proof-of-work
/// is correct, but does not verify that the transactions are valid or encoded /// is correct, but does not verify that the transactions are valid or encoded
/// correctly. /// correctly.