module CCL::Pow::Utils

Extended Modules

Defined in:

ccl-pow.cr

Constant Summary

MIN_NBITS = "20100000"
MIN_TARGET = BigInt.new("1000000000000000000000000000000000000000000000000000000000000000", 16)

Instance Method Summary

Instance Method Detail

def calculate_hash(nonce : UInt64, data : String) : BlockHash #

Returns the hash for nonce + data

CCL::Pow::Utils.calculate_hash(nonce: 20151213_u64, data: "cocol") # => "5906039dfa0262343155216f0d73135d30fd48a0d4543c61d27169db12736d3a"

[View source]
def calculate_nbits(from target : BigInt) : NBits #

Returns the target as nbits from a given numerical target

CCL::Pow::Utils.calculate_nbits(from: BigInt.new("26959535291011309493156476344723991336010898738574164086137773096960")) # => "1d00ffff"

[View source]
def calculate_target(from nbits : String) : BigInt #

Returns the numerical target threshold based on the given difficulty

Find out more about it here https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch10.asciidoc#target-representation

CCL::Pow::Utils.calculate_target(from: "1d00ffff") # => 26959535291011309493156476344723991336010898738574164086137773096960

[View source]
def retarget(start_time : Float64, end_time : Float64, wanted_timespan : Float64, current_target : BigInt, min_target = MIN_TARGET) : NBits #

Returns the retargeted difficulty based on the timestamp of a given block and the timespan in minutes.

Assuming you want retarget for the last minute in which you targeted 12 blocks you would pass the timestamp of the current_block - 12 and 60 seconds as wanted_timepan

CCL::Pow::Utils.retarget(
  start_time: 1559306286_f64,
  end_time: 1559306286_f64,
  wanted_timespan: 60_f64,
  current_target: CCL::Pow::Utils.calculate_target("1e38ae39")
) # => "1e00b560"

[View source]