Giter Site home page Giter Site logo

Comments (4)

epiccurious avatar epiccurious commented on September 14, 2024

This will be more challenging than expected because a block's time is not always after the previous block.

Will need to run the binary search on mediantime. But that will give the wrong block. So then maybe jump back 15 or so blocks and do a linear search again.

https://bitcoin.stackexchange.com/questions/67618/difference-between-time-and-mediantime-in-getblock#67622

from born-on-bitcoin-block.

epiccurious avatar epiccurious commented on September 14, 2024
## Set the target time
target_time=1234567890

## The target time should always be greater than or equal to the lower bound's mediantime
lower_bound="0"
## Find the hash of the lower bound
lower_bound_hash=$(bitcoin-cli getblockhash $lower_bound)
## Find the mediantime of the lower bound
lower_bound_mediantime=$(bitcoin-cli getblockheader $lower_bound_hash | jq '.mediantime')

## The target time should always be less than the upper bound's mediantime
upper_bound=$block_count
## Find the hash of the upper bound
upper_bound_hash=$(bitcoin-cli getblockhash $upper_bound)
## Find the mediantime of the upper bound
upper_bound_mediantime=$(bitcoin-cli getblockheader $upper_bound_hash | jq '.mediantime')

## NEED TO ADD A CHECK HERE.
## IF THE TARGET_TIME IS OUTSIDE OF THE TWO BOUNDS PRINT A CUSTOM ERROR CODE AND EXIT 1.

while [ $(($upper_bound-$lower_bound)) -gt 1 ]
do
  ## The midpoint is the average of the two
  midpoint=$((($upper_bound+$lower_bound)/2))
  ## Need to figure out how to convert to an integer (i.e. drop the 0.5 from results). There are no fractional blocks.

  ## Find the hash of the midpoint block
  midpoint_hash=$(bitcoin-cli getblockhash $midpoint)

  ## Find the mediantime of the midpoint block
  midpoint_mediantime=$(bitcoin-cli getblockheader $midpoint_hash | jq '.mediantime')

  echo "Block $midpoint has a median time of $midpoint_mediantime."
  
  ## If the lower_bound rules apply (target time >= mediantime), then the midpoint is the new lower bound.
  ## If the upper_bound rules apply (target_time < mediantime), then the midpoint is the new upper bound.
  if [ $target_time -ge $midpoint_mediantime ]
  then
    echo "Setting the new lower bound."
    lower_bound=$midpoint
    lower_bound_hash=$midpoint_hash
    lower_bound_mediantime=$midpoint_mediantime
  else
    echo "Setting the new upper bound."
    upper_bound=$midpoint
    upper_bound_hash=$midpoint_hash
    upper_bound_mediantime=$midpoint_mediantime
  fi

done

from born-on-bitcoin-block.

epiccurious avatar epiccurious commented on September 14, 2024

This code hasn't been tested and is probably buggy. Need to test this code on a fully synced node before publishing.

from born-on-bitcoin-block.

epiccurious avatar epiccurious commented on September 14, 2024

A synced node will take me a few days. This issue is on hold until then.

from born-on-bitcoin-block.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.