synchronization – Detect when it’s protected to ‘cease’ the Bitcoin Core Daemon, bitcoind

0
51


Beforehand, I stored the Bitcoin Core daemon operating 24×7, nonetheless not too long ago I’ve change into much less interested by preserving it operating on a regular basis, and would really like as an alternative to run it for as a lot time because it takes to make sure the blockchain is not any various hours behind.

I am utilizing a Powershell script to hook up with the Bitcoin Core daemon operating on Home windows. The script gathers the output from bitcoin-cli.exe getblockchaininfo right into a json object to verify blockchain verification progress.

I’ve scheduled a job to begin the bitcoind course of, and my Powershell script checks the output from bitcoin-cli each 30 seconds, ready till verificationprogress is not less than 0.999999 earlier than shutting down the daemon.

As soon as verificationprogress is at 0.999999 or increased, the script shuts down the daemon, till the subsequent time it runs about 4 hours later. I’ve observed that the subsequent time bitcoind begins, it’s re-downloading blocks it downloaded the final time it ran.

Is there one thing else I ought to verify aside from the verificationprogress output from bitcoin-cli.exe getblockchaininfo ?

That is the Powershell script, in case anybody is :

Param
(
    [System.Boolean]$debug_mode = $false,
    [System.Boolean]$display_blockchain_info = $false
);
Set-Location "E:BitcoinCoreBin";
### if Bitcoin Daemon is not operating, begin it.
$Processes = (Get-Course of -Identify bitcoind -ErrorAction SilentlyContinue);
If ($Processes.Size -eq 0) 
{
    $ServiceState = (Get-Service -Identify BitcoinCoreDaemon -ErrorAction SilentlyContinue).Standing;
    If ($ServiceState -ne "Operating")
    {
        Write-Output "$((Get-Date).ToString('s')) - Beginning the Bitcoin Core Daemon Service.";
        Begin-Service -Identify BitcoinCoreDaemon -ErrorAction SilentlyContinue;
    }
    Else
    {
        ### the service standing is operating, however one thing has killed the bitcoin daemon course of, so we'll restart it.
        Write-Output "$((Get-Date).ToString('s')) - Beginning the Bitcoin Core Daemon Course of.";
        (Begin-Course of -FilePath ".bitcoind.exe" -ArgumentList "--conf=E:BitcoinCorebitcoin.conf" -ErrorAction SilentlyContinue);
    };
}
Else
{
    If ($debug_mode -eq $true)
    {
        $Processes;
    };
    Begin-Sleep -Seconds 3;
};
[System.Boolean]$is_complete = $false;
[System.Double]$progress = 0;
[System.Int32]$blocks_downloaded_at_start = 0;
[System.Int32]$blocks_downloaded = 0;
[System.Int32]$blocks_available = 0;
[System.DateTime]$start_time = (Get-Date);
Whereas ($is_complete -eq $false)
{
    If ($Processes.Size -eq 0)
    {
        Write-Output "$((Get-Date).ToString('s')) - Sleeping for 30 seconds.";
        Begin-Sleep -Seconds 30;
    }
    Else
    {
        $Processes = @();
    };
    [System.String]$block_chain_info = (.bitcoin-cli.exe --datadir=E:BitcoinCoreData getblockchaininfo)
    If ($block_chain_info -ne $null)
    {
        $bci = (ConvertFrom-Json $block_chain_info -ErrorAction SilentlyContinue);
        If ($display_blockchain_info -eq $true)
        {
            Write-Output "$($block_chain_info)";
        };
        $is_complete = -Not ($bci.initialblockdownload -eq $true);
        $blocks_available = $bci.headers;
        $blocks_downloaded = $bci.blocks;
        If ($debug_mode -eq $true)
        {
            Write-Output "$($blocks_downloaded) of $($blocks_available) blocks downloaded.";
        };
        IF ($blocks_downloaded_at_start -eq 0)
        {
            $blocks_downloaded_at_start = $blocks_downloaded;
        };
        $progress = $bci.verificationprogress;
        If (($blocks_downloaded -lt $blocks_available) -or ($progress -lt 0.999999))
        {
            If ($debug_mode -eq $true)
            {
                Write-Output "Setting `$is_complete to `$false.  `$progress is $($progress).  `$start_time + 180 seconds is $(($start_time.AddSeconds(180)).ToString('s')).  `$progress is $($progress).";
            };
            $is_complete = $false;
        };
        If ($start_time.AddSeconds(180) -gt (Get-Date))
        {
            Write-Output "Ready till not less than $($start_time.AddSeconds(180).ToString('s')) earlier than exiting.";
            $is_complete = $false;
        };
        If ($is_complete -eq $false)
        {
            Write-Output "Sync is $($progress) full.  $($blocks_downloaded - $blocks_downloaded_at_start) blocks downloaded...";
        }
        Else
        {
            Write-Output "Sync full (progess is $($progress)).  $($blocks_downloaded - $blocks_downloaded_at_start) blocks downloaded.";
        };
    };
};
### cease Bitcoin Core Daemon after which the Service
(.bitcoin-cli.exe --datadir=E:BitcoinCoreData cease);
[System.DateTime]$start_of_shutdown = (Get-Date);
Whereas (((Get-Course of -Identify bitcoind -ErrorAction SilentlyContinue).Size -gt 0) -and ($start_of_shutdown.AddSeconds(30) -lt (Get-Date)))
{
    Begin-Sleep -Seconds 1;
};
Cease-Service -Identify "BitcoinCoreDaemon" -ErrorAction SilentlyContinue;
Write-Output "$((Get-Date).ToString('s')) - Bitcoin Core Daemon and Service have been stopped.";

LEAVE A REPLY

Please enter your comment!
Please enter your name here