Skip to content

Commit

Permalink
fix: added doc block.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Sep 22, 2023
1 parent 0c52ccd commit f1d2a4f
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions crates/btc-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,14 +1271,34 @@ impl<T: Config> Pallet<T> {
Ok(())
}

/// The target value represents the mining difficulty required to mine a block with the specified hash.
///
/// # Parameters
///
/// * `block_hash`: A 256-bit block hash.
///
/// # Returns
///
/// * `Result<u32, DispatchError>`: A `Result` containing the retrieved target value as a `u32` if successful, or a
/// `DispatchError` if an error occurs during the retrieval.
fn get_target(block_hash: H256Le) -> Result<u32, DispatchError> {
let block_header = Self::get_block_header_from_hash(block_hash)?;
// Fixme: Should this be kept as a U256 type and other u32 type can be converted to U256?
// Currently taking low_u32/as_u32 loses precision
Ok(block_header.block_header.target.low_u32())
}

// Reference: https://github.com/spesmilo/electrum/blob/cee22abcb5544c5a6fa7f8a8108ccda9c32c2e29/electrum/blockchain.py#L582-L588
/// Calculates the chainwork of a block header at a given height based on the provided block hash.
/// Reference: https://github.com/spesmilo/electrum/blob/cee22abcb5544c5a6fa7f8a8108ccda9c32c2e29/electrum/blockchain.py#L582-L588
///
/// # Arguments
///
/// * `block_hash`: A 256-bit block hash.
///
/// # Returns
///
/// * `Result<u32, DispatchError>`: A `Result` containing the calculated chainwork as a `u32` if successful, or a
/// `DispatchError` if an error occurs during target retrieval.
fn chainwork_of_header_at_height(block_hash: H256Le) -> Result<u32, DispatchError> {
let target = Self::get_target(block_hash)?;
let work = (2_u32.saturating_pow(256).saturating_sub(target).saturating_less_one())
Expand All @@ -1287,8 +1307,17 @@ impl<T: Config> Pallet<T> {
Ok(work)
}

// Reference: https://github.com/spesmilo/electrum/blob/cee22abcb5544c5a6fa7f8a8108ccda9c32c2e29/electrum/blockchain.py#L589-L614
// Calculate the cumulative chainwork of a blockchain.
/// The chainwork is a measure of the total computational work done by the chain up to a certain point.
/// Reference: https://github.com/spesmilo/electrum/blob/cee22abcb5544c5a6fa7f8a8108ccda9c32c2e29/electrum/blockchain.py#L589-L614
///
/// # Parameters
///
/// * `chain`: new blockchain element
///
/// # Returns
///
/// * `Result<u32, DispatchError>`: A `Result` containing the calculated cumulative chainwork as a `u32` if
/// successful, or a `DispatchError` if an error occurs during the calculation.
fn get_chainwork(chain: BlockChain) -> Result<u32, DispatchError> {
// Calculate the height of the last retarget point.
let last_retarget = chain
Expand Down

0 comments on commit f1d2a4f

Please sign in to comment.