Skip to content

Instantly share code, notes, and snippets.

@mutatrum
Last active September 14, 2022 20:52

Revisions

  1. mutatrum revised this gist Jun 10, 2022. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions blocktime.sh
    Original file line number Diff line number Diff line change
    @@ -10,10 +10,12 @@ function binarySearch {
    L=$1
    R=$2

    if ((R > L));
    if ((R >= L));
    then

    BLOCK=$((L + (R - L) / 2))
    BLOCKTIME=$(getBlockTime $BLOCK)

    if ((BLOCKTIME == TIME));
    then
    echo $BLOCK
    @@ -30,11 +32,15 @@ function binarySearch {

    BLOCKS=$(bitcoin-cli getblockchaininfo | jq -r .blocks)
    BLOCK=$(binarySearch 0 $BLOCKS)
    BLOCKTIME=$(getBlockTime $BLOCK)
    echo "Block $BLOCK: $(date -d @$BLOCKTIME -Iseconds) $((BLOCKTIME - TIME)) seconds"

    if (( BLOCKTIME != TIME )) && ((BLOCK > 0)) && ((BLOCK < BLOCKS));
    then
    if ((BLOCK >= 0));
    then
    BLOCKTIME=$(getBlockTime $BLOCK)
    echo "Block $BLOCK: $(date -d @$BLOCKTIME -Iseconds) $((BLOCKTIME - TIME)) seconds"
    fi

    if ((BLOCKTIME != TIME)) && ((BLOCK < BLOCKS));
    then
    NEXTBLOCK=$((BLOCK + 1))
    NEXTBLOCKTIME=$(getBlockTime $NEXTBLOCK)
    echo "Block $NEXTBLOCK: $(date -d @$NEXTBLOCKTIME -Iseconds) $((NEXTBLOCKTIME - TIME)) seconds"
  2. mutatrum revised this gist Jun 10, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion blocktime.sh
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ BLOCK=$(binarySearch 0 $BLOCKS)
    BLOCKTIME=$(getBlockTime $BLOCK)
    echo "Block $BLOCK: $(date -d @$BLOCKTIME -Iseconds) $((BLOCKTIME - TIME)) seconds"

    if ((BLOCKTIME < TIME)) && ((BLOCK < BLOCKS));
    if (( BLOCKTIME != TIME )) && ((BLOCK > 0)) && ((BLOCK < BLOCKS));
    then
    NEXTBLOCK=$((BLOCK + 1))
    NEXTBLOCKTIME=$(getBlockTime $NEXTBLOCK)
  3. mutatrum created this gist Jun 10, 2022.
    41 changes: 41 additions & 0 deletions blocktime.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/bin/bash

    TIME=$(date -d "$1" +"%s")

    function getBlockTime {
    echo $(bitcoin-cli getblockheader $(bitcoin-cli getblockhash $1) | jq -r .time)
    }

    function binarySearch {
    L=$1
    R=$2

    if ((R > L));
    then
    BLOCK=$((L + (R - L) / 2))
    BLOCKTIME=$(getBlockTime $BLOCK)
    if ((BLOCKTIME == TIME));
    then
    echo $BLOCK
    elif ((BLOCKTIME > TIME));
    then
    echo $(binarySearch $L $((BLOCK - 1)))
    else
    echo $(binarySearch $((BLOCK + 1)) $R)
    fi
    else
    echo $R
    fi
    }

    BLOCKS=$(bitcoin-cli getblockchaininfo | jq -r .blocks)
    BLOCK=$(binarySearch 0 $BLOCKS)
    BLOCKTIME=$(getBlockTime $BLOCK)
    echo "Block $BLOCK: $(date -d @$BLOCKTIME -Iseconds) $((BLOCKTIME - TIME)) seconds"

    if ((BLOCKTIME < TIME)) && ((BLOCK < BLOCKS));
    then
    NEXTBLOCK=$((BLOCK + 1))
    NEXTBLOCKTIME=$(getBlockTime $NEXTBLOCK)
    echo "Block $NEXTBLOCK: $(date -d @$NEXTBLOCKTIME -Iseconds) $((NEXTBLOCKTIME - TIME)) seconds"
    fi