#!/usr/bin/env perl
# Copyright 2019-2020 The Hush developers
# Released under the GPLv3
use warnings;
use strict;

# Simulate the total supply on Hush v3 mainnet
# Todo: track FR
# Todo: verify FR off-by-one

my $supply   = 0.0;
my $block    = 0; # Block 0 in Hush Smart chains is the BTC genesis block
my $puposhis = 100_000_000;
my $reward0  = 1_250_000_000;
my $halvings = 0;
my $initial  = 6178674 * $puposhis;
my $interval = 1_640_000; # 4 years of 75s blocks
my $stop     = shift || -1;
my $totalfr  = 0; # total paid out to FR address
my $reward   = $reward0;

# Usage: ./hush_supply  &> supply.csv
#        ./hush_supply HEIGHT &> supply.csv # stop at HEIGHT

printf "# block, supply, reward, fr, totalfr, halvings\n";

# We know BR will go to zero between 7 and 8th halvings
while ($halvings <= 10) {
    $block++;
    my $fr = 0;
    # blocks 2-127 of Hush v3 had BR=0
    if ($block == 1) {
        $reward = $initial; # airdropped funds from Hush v2 mainnet
    } elsif ($block > 1 && $block < 128) {
        $reward = 0; # blocks 2-127 have BR=0
    } else {
        $fr = 125_000_000;
        if ($block < 340_000) {
            $reward = $reward0;
        } else {
            my $shifted = $block - 340_000;
            # Past the first halving
            $halvings = 1 + int ($shifted / $interval);
            if ($shifted % 840_000 == 0) {
                $reward >>= 2;
                $fr     >>= 2;
            }
        }
    }
    $supply  += $reward;
    $totalfr += $fr;

    # block, current supply, block reward amount, number of halvings, all amounts are in puposhis
    printf "%d,%d,%d,%d,%d,%d\n", $block, $supply, $reward, $fr, $totalfr, $halvings;
    exit(0) if $block == $stop;
}
