Previously, I kept the Bitcoin Core daemon running 24x7, however recently I've become less interested in keeping it running all the time, and would like instead to run it for as much time as it takes to ensure the blockchain is no more than a few hours behind.
I'm using a Powershell script to connect to the Bitcoin Core daemon running on Windows. The script gathers the output from bitcoin-cli.exe getblockchaininfo
into a json object to check blockchain verification progress.
I've scheduled a task to start the bitcoind process, and my Powershell script checks the output from bitcoin-cli every 30 seconds, waiting until verificationprogress
is at least 0.999999 before shutting down the daemon.
Once verificationprogress
is at 0.999999 or higher, the script shuts down the daemon, until the next time it runs about 4 hours later. I've noticed that the next time bitcoind
starts, it is re-downloading blocks it downloaded the last time it ran. My script checks the number of blocks downloaded vs the number of block headers available, as well as looking at the initialblockdownload
boolean indicator before exiting.
Is there something else I should check other than the verificationprogress
output from bitcoin-cli.exe getblockchaininfo
?
This is the Powershell script, in case anyone is interested:
Param
(
[System.Boolean]$debug_mode = $false,
[System.Boolean]$display_blockchain_info = $false
);
Set-Location "E:\BitcoinCore\Bin";
### if Bitcoin Daemon isn't running, start it.
$Processes = (Get-Process -Name bitcoind -ErrorAction SilentlyContinue);
If ($Processes.Length -eq 0)
{
$ServiceState = (Get-Service -Name BitcoinCoreDaemon -ErrorAction SilentlyContinue).Status;
If ($ServiceState -ne "Running")
{
Write-Output "$((Get-Date).ToString('s')) - Starting the Bitcoin Core Daemon Service.";
Start-Service -Name BitcoinCoreDaemon -ErrorAction SilentlyContinue;
}
Else
{
### the service status is running, but something has killed the bitcoin daemon process, so we'll restart it.
Write-Output "$((Get-Date).ToString('s')) - Starting the Bitcoin Core Daemon Process.";
(Start-Process -FilePath ".\bitcoind.exe" -ArgumentList "--conf=E:\BitcoinCore\bitcoin.conf" -ErrorAction SilentlyContinue);
};
}
Else
{
If ($debug_mode -eq $true)
{
$Processes;
};
Start-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);
While ($is_complete -eq $false)
{
If ($Processes.Length -eq 0)
{
Write-Output "$((Get-Date).ToString('s')) - Sleeping for 30 seconds.";
Start-Sleep -Seconds 30;
}
Else
{
$Processes = @();
};
[System.String]$block_chain_info = (.\bitcoin-cli.exe --datadir=E:\BitcoinCore\Data 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 "Waiting until at least $($start_time.AddSeconds(180).ToString('s')) before exiting.";
$is_complete = $false;
};
If ($is_complete -eq $false)
{
Write-Output "Sync is $($progress) complete. $($blocks_downloaded - $blocks_downloaded_at_start) blocks downloaded...";
}
Else
{
Write-Output "Sync complete (progess is $($progress)). $($blocks_downloaded - $blocks_downloaded_at_start) blocks downloaded.";
};
};
};
### stop Bitcoin Core Daemon and then the Service
(.\bitcoin-cli.exe --datadir=E:\BitcoinCore\Data stop);
[System.DateTime]$start_of_shutdown = (Get-Date);
While (((Get-Process -Name bitcoind -ErrorAction SilentlyContinue).Length -gt 0) -and ($start_of_shutdown.AddSeconds(30) -lt (Get-Date)))
{
Start-Sleep -Seconds 1;
};
Stop-Service -Name "BitcoinCoreDaemon" -ErrorAction SilentlyContinue;
Write-Output "$((Get-Date).ToString('s')) - Bitcoin Core Daemon and Service have been stopped.";
Here's the debug.log from the end of the prior session, and the start of the most recent session. The previous session ended with an UpdateTip value of 810877, however the most recent session starts with UpdateTip 810207, 670 blocks earlier, and indeed the new log shows [Chainstate [ibd] @ height 810206
.
2023-10-06T09:27:23Z UpdateTip: new best=000000000000000000026e31e1a0f6d0d65ac1ab7a765740eb0065176e38a209 height=810865 version=0x246fc000 log2_work=94.459262 tx=903462025 date='2023-10-06T08:01:13Z' progress=0.999979 cache=546.0MiB(3943178txo)
2023-10-06T09:27:25Z UpdateTip: new best=000000000000000000007e2d6aea7100d57133b5290eaba79891182a13f1a8f3 height=810866 version=0x2a4b4000 log2_work=94.459275 tx=903462385 date='2023-10-06T08:04:34Z' progress=0.999980 cache=546.5MiB(3947209txo)
2023-10-06T09:27:27Z UpdateTip: new best=00000000000000000004c7066de91e2532c5dbafd46a3f205da2d29e92511d30 height=810867 version=0x2000c000 log2_work=94.459288 tx=903463057 date='2023-10-06T08:17:29Z' progress=0.999983 cache=547.1MiB(3951752txo)
2023-10-06T09:27:28Z UpdateTip: new best=000000000000000000008b4aac5311e3ec65d3ed67a9f40b917a83a01aaca5f0 height=810868 version=0x2000e000 log2_work=94.459301 tx=903463474 date='2023-10-06T08:19:10Z' progress=0.999983 cache=547.7MiB(3956404txo)
2023-10-06T09:27:29Z UpdateTip: new best=0000000000000000000041af22296ba31c5a14bc35ea28e0f6de3d233b5596d9 height=810869 version=0x2109e004 log2_work=94.459314 tx=903464380 date='2023-10-06T08:37:32Z' progress=0.999988 cache=548.3MiB(3961301txo)
2023-10-06T09:27:31Z UpdateTip: new best=000000000000000000015d82f76da40ed61f772397e5bcc6a7486cf20312ef5f height=810870 version=0x31932000 log2_work=94.459327 tx=903464871 date='2023-10-06T08:43:04Z' progress=0.999989 cache=548.8MiB(3965122txo)
2023-10-06T09:27:32Z UpdateTip: new best=000000000000000000036fd5de966a90374af6793ef05a8cc38bc8b6573a03f8 height=810871 version=0x2fffe000 log2_work=94.459340 tx=903465378 date='2023-10-06T08:44:46Z' progress=0.999990 cache=549.2MiB(3969117txo)
2023-10-06T09:27:34Z UpdateTip: new best=00000000000000000004be6ac3f43df15ddcf1a7c62951e6e79eb5de57144edb height=810872 version=0x26000000 log2_work=94.459353 tx=903466243 date='2023-10-06T08:50:25Z' progress=0.999991 cache=549.7MiB(3972802txo)
2023-10-06T09:27:35Z UpdateTip: new best=000000000000000000044fa9f1776352615ac154b495874ce4e9a19e1afc5730 height=810873 version=0x20e00000 log2_work=94.459366 tx=903466739 date='2023-10-06T08:53:04Z' progress=0.999992 cache=550.2MiB(3976825txo)
2023-10-06T09:27:36Z UpdateTip: new best=000000000000000000035b75f248b8f6b910940c2c38a60bfe1814115f7737e0 height=810874 version=0x29960000 log2_work=94.459379 tx=903468832 date='2023-10-06T09:10:10Z' progress=0.999996 cache=550.8MiB(3981874txo)
2023-10-06T09:27:38Z UpdateTip: new best=000000000000000000009768742a1f0e94c67df18493ecab7aabe66499ae6936 height=810875 version=0x23572000 log2_work=94.459392 tx=903469658 date='2023-10-06T09:11:27Z' progress=0.999996 cache=551.3MiB(3985514txo)
2023-10-06T09:27:39Z UpdateTip: new best=000000000000000000002caf4e7450f713398fd4cf9a9cbf5a718ecf855f4c0c height=810876 version=0x2df92000 log2_work=94.459405 tx=903471258 date='2023-10-06T09:17:19Z' progress=0.999997 cache=551.9MiB(3990562txo)
2023-10-06T09:27:39Z Saw new header hash=000000000000000000024ff8dcb9fc8e05bd11652d93fa15ad8570a82c471e7c height=810877
2023-10-06T09:27:39Z New outbound peer connected: version: 70015, blocks=810876, peer=133 (block-relay-only)
2023-10-06T09:27:42Z UpdateTip: new best=000000000000000000024ff8dcb9fc8e05bd11652d93fa15ad8570a82c471e7c height=810877 version=0x22bc4000 log2_work=94.459418 tx=903473610 date='2023-10-06T09:28:09Z' progress=1.000000 cache=552.5MiB(3995018txo)
2023-10-06T09:28:09Z tor: Thread interrupt
2023-10-06T09:28:09Z addcon thread exit
2023-10-06T09:28:09Z opencon thread exit
2023-10-06T09:28:09Z Shutdown: In progress...
2023-10-06T09:28:09Z torcontrol thread exit
2023-10-06T09:28:09Z net thread exit
2023-10-06T13:00:03Z Bitcoin Core version v25.0.0 (release build)
2023-10-06T13:00:03Z InitParameterInteraction: parameter interaction: -externalip set -> setting -discover=0
2023-10-06T13:00:03Z Using the 'sse4(1way),sse41(4way)' SHA256 implementation
2023-10-06T13:00:03Z Default data directory C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Bitcoin
2023-10-06T13:00:03Z Using data directory E:\BitcoinCore\Data
2023-10-06T13:00:03Z Config file: E:\BitcoinCore\bitcoin.conf
2023-10-06T13:00:03Z Config file arg: chain="main"
2023-10-06T13:00:03Z Config file arg: datadir="E:\\BitcoinCore\\Data"
2023-10-06T13:00:03Z Config file arg: externalip="x.x.x.x"
2023-10-06T13:00:03Z Config file arg: listen="1"
2023-10-06T13:00:03Z Config file arg: port="8333"
2023-10-06T13:00:03Z Config file arg: rpcallowip="192.168.0.0/24"
2023-10-06T13:00:03Z Config file arg: rpcbind="0.0.0.0"
2023-10-06T13:00:03Z Config file arg: rpcport="8332"
2023-10-06T13:00:03Z Config file arg: walletdir="E:\\BitcoinCore\\Wallets"
2023-10-06T13:00:03Z Setting file arg: dbcache = "900"
2023-10-06T13:00:03Z Setting file arg: par = "2"
2023-10-06T13:00:03Z Command-line arg: conf="E:\\BitcoinCore\\bitcoin.conf"
2023-10-06T13:00:03Z Command-line arg: datadir="E:\\BitcoinCore\\Data"
2023-10-06T13:00:03Z Using at most 125 automatic connections (2048 file descriptors available)
2023-10-06T13:00:03Z Using 16 MiB out of 16 MiB requested for signature cache, able to store 524288 elements
2023-10-06T13:00:03Z Using 16 MiB out of 16 MiB requested for script execution cache, able to store 524288 elements
2023-10-06T13:00:03Z Script verification uses 1 additional threads
2023-10-06T13:00:03Z scheduler thread start
2023-10-06T13:00:03Z Binding RPC on address 0.0.0.0 port 8332
2023-10-06T13:00:03Z WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet
2023-10-06T13:00:03Z [http] creating work queue of depth 16
2023-10-06T13:00:03Z Using random cookie authentication.
2023-10-06T13:00:03Z Generated RPC authentication cookie E:\BitcoinCore\Data\.cookie
2023-10-06T13:00:03Z [http] starting 4 worker threads
2023-10-06T13:00:03Z Using wallet directory E:\BitcoinCore\Wallets
2023-10-06T13:00:03Z init message: Verifying wallet(s)…
2023-10-06T13:00:03Z Using /16 prefix for IP bucketing
2023-10-06T13:00:03Z init message: Loading P2P addresses…
2023-10-06T13:00:03Z Loaded 64337 addresses from peers.dat 406ms
2023-10-06T13:00:03Z init message: Loading banlist…
2023-10-06T13:00:03Z SetNetworkActive: true
2023-10-06T13:00:03Z AddLocal(x.x.x.x:8333,4)
2023-10-06T13:00:03Z Cache configuration:
2023-10-06T13:00:03Z * Using 2.0 MiB for block index database
2023-10-06T13:00:03Z * Using 8.0 MiB for chain state database
2023-10-06T13:00:03Z * Using 890.0 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space)
2023-10-06T13:00:03Z init message: Loading block index…
2023-10-06T13:00:03Z Assuming ancestors of block 000000000000000000035c3f0d31e71a5ee24c5aaf3354689f65bd7b07dee632 have valid signatures.
2023-10-06T13:00:03Z Setting nMinimumChainWork=000000000000000000000000000000000000000044a50fe819c39ad624021859
2023-10-06T13:00:03Z Opening LevelDB in E:\BitcoinCore\Data\blocks\index
2023-10-06T13:00:03Z Opened LevelDB successfully
2023-10-06T13:00:03Z Using obfuscation key for E:\BitcoinCore\Data\blocks\index: 0000000000000000
2023-10-06T13:00:08Z LoadBlockIndexDB: last block file = 3864
2023-10-06T13:00:08Z LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=6, size=9751382, heights=810807...810812, time=2023-10-05...2023-10-06)
2023-10-06T13:00:08Z Checking all blk files are present...
2023-10-06T13:00:11Z Initializing chainstate Chainstate [ibd] @ height -1 (null)
2023-10-06T13:00:11Z Opening LevelDB in E:\BitcoinCore\Data\chainstate
2023-10-06T13:00:11Z Opened LevelDB successfully
2023-10-06T13:00:11Z Using obfuscation key for E:\BitcoinCore\Data\chainstate: 4358e8c41e43c894
2023-10-06T13:00:11Z Loaded best chain: hashBestChain=0000000000000000000328e424cd9ff6317a8f256f7bc2b5d3aa487e295eb27d height=810206 date=2023-10-01T22:03:16Z progress=0.998377
2023-10-06T13:00:11Z [snapshot] allocating all cache to the IBD chainstate
2023-10-06T13:00:11Z Opening LevelDB in E:\BitcoinCore\Data\chainstate
2023-10-06T13:00:11Z Opened LevelDB successfully
2023-10-06T13:00:11Z Using obfuscation key for E:\BitcoinCore\Data\chainstate: 4358e8c41e43c894
2023-10-06T13:00:11Z [Chainstate [ibd] @ height 810206 (0000000000000000000328e424cd9ff6317a8f256f7bc2b5d3aa487e295eb27d)] resized coinsdb cache to 8.0 MiB
2023-10-06T13:00:11Z [Chainstate [ibd] @ height 810206 (0000000000000000000328e424cd9ff6317a8f256f7bc2b5d3aa487e295eb27d)] resized coinstip cache to 890.0 MiB
2023-10-06T13:00:11Z init message: Verifying blocks…
2023-10-06T13:00:11Z Verifying last 6 blocks at level 3
2023-10-06T13:00:11Z Verification progress: 0%
2023-10-06T13:00:14Z Verification progress: 16%
2023-10-06T13:00:17Z Verification progress: 33%
2023-10-06T13:00:19Z Verification progress: 50%
2023-10-06T13:00:22Z Verification progress: 66%
2023-10-06T13:00:25Z Verification progress: 83%
2023-10-06T13:00:27Z Verification progress: 99%
2023-10-06T13:00:27Z Verification: No coin database inconsistencies in last 6 blocks (14504 transactions)
2023-10-06T13:00:27Z block index 24335ms
2023-10-06T13:00:27Z Setting NODE_NETWORK on non-prune mode
2023-10-06T13:00:27Z block tree size = 810814
2023-10-06T13:00:27Z nBestHeight = 810206
2023-10-06T13:00:27Z loadblk thread start
2023-10-06T13:00:27Z Bound to 127.0.0.1:8334
2023-10-06T13:00:27Z torcontrol thread start
2023-10-06T13:00:27Z Bound to [::]:8333
2023-10-06T13:00:27Z Bound to 0.0.0.0:8333
2023-10-06T13:00:27Z 0 block-relay-only anchors will be tried for connections.
2023-10-06T13:00:27Z init message: Starting network threads…
2023-10-06T13:00:27Z net thread start
2023-10-06T13:00:27Z opencon thread start
2023-10-06T13:00:27Z dnsseed thread start
2023-10-06T13:00:27Z Waiting 300 seconds before querying DNS seeds.
2023-10-06T13:00:27Z addcon thread start
2023-10-06T13:00:27Z msghand thread start
2023-10-06T13:00:31Z UpdateTip: new best=000000000000000000005bf8f39f891a7ef192cd420aca62e6e4e712f9651345 height=810207 version=0x30b0c000 log2_work=94.450664 tx=902083876 date='2023-10-01T22:15:30Z' progress=0.998380 cache=1.9MiB(13862txo)
2023-10-06T13:00:38Z UpdateTip: new best=00000000000000000003d001eba5cebcc7f518100c565e9b81a8d43abe1a4647 height=810208 version=0x21b18000 log2_work=94.450677 tx=902085395 date='2023-10-01T22:16:33Z' progress=0.998380 cache=3.9MiB(27532txo)
2023-10-06T13:00:40Z UpdateTip: new best=000000000000000000023aa4cd17246fd163f70c76886d0a8a0c95de12064463 height=810209 version=0x31c16000 log2_work=94.450690 tx=902089477 date='2023-10-01T22:26:53Z' progress=0.998382 cache=5.5MiB(38614txo)
2023-10-06T13:00:44Z UpdateTip: new best=00000000000000000003bc9f4d0c3ee9506a98af5f998e76a26d0ce738857388 height=810210 version=0x20400000 log2_work=94.450703 tx=902091419 date='2023-10-01T22:34:18Z' progress=0.998384 cache=7.2MiB(49940txo)
2023-10-06T13:00:47Z UpdateTip: new best=000000000000000000015d7268a3392ee8a1d12df7487ab05a16850db6b55d27 height=810211 version=0x20000000 log2_work=94.450716 tx=902095202 date='2023-10-01T22:41:03Z' progress=0.998386 cache=8.5MiB(58801txo)
2023-10-06T13:00:49Z UpdateTip: new best=00000000000000000001e1f6c8ead46189e4be15a9d4ed23c9c3ab8173592fae height=810212 version=0x29274000 log2_work=94.450729 tx=902097477 date='2023-10-01T22:53:50Z' progress=0.998389 cache=9.8MiB(69269txo)
2023-10-06T13:00:49Z init message: Done loading
2023-10-06T13:00:50Z UpdateTip: new best=000000000000000000017157c555d2dbb157fab5fc745a16b08e6738e0c85936 height=810213 version=0x27e4e000 log2_work=94.450742 tx=902101740 date='2023-10-01T22:57:23Z' progress=0.998390 cache=11.0MiB(76813txo)
.
.
.
2023-10-06T13:25:12Z UpdateTip: new best=0000000000000000000079dafaab513cf75b6220c684509d181d5ec0a638679e height=810811 version=0x27306000 log2_work=94.458557 tx=903380823 date='2023-10-05T23:53:32Z' progress=0.999802 cache=513.6MiB(3686409txo)
2023-10-06T13:25:13Z UpdateTip: new best=000000000000000000033b8366f810946dbc4dcbb2554a0df47a065ace7f3276 height=810812 version=0x20400000 log2_work=94.458570 tx=903383323 date='2023-10-06T00:05:52Z' progress=0.999805 cache=514.2MiB(3690886txo)
2023-10-06T13:25:13Z Leaving InitialBlockDownload (latching to false)
2023-10-06T13:25:13Z Imported mempool transactions from disk: 2 succeeded, 32 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2023-10-06T13:25:13Z loadblk thread exit
2023-10-06T13:25:13Z Saw new header hash=0000000000000000000443ae77e6d49f6cfd76cac1eb641ac54b0c2ed014fa51 height=810813
2023-10-06T13:25:13Z Saw new header hash=0000000000000000000497d6381a7558a9d070a2941de4758bb1b69decf6ef5c height=810814
20
.
.
.
2023-10-06T13:28:24Z UpdateTip: new best=00000000000000000002d17d8f0633fcbfce76ba2a27a590d3330e7b6bb50a1f height=810905 version=0x22efc000 log2_work=94.459783 tx=903526119 date='2023-10-06T12:45:49Z' progress=0.999990 cache=564.7MiB(4092680txo)
2023-10-06T13:28:26Z UpdateTip: new best=00000000000000000004d144d05c5b9678748a76b21c50c55397c7db4e668376 height=810906 version=0x2000e000 log2_work=94.459796 tx=903528135 date='2023-10-06T12:50:15Z' progress=0.999991 cache=565.0MiB(4094669txo)
2023-10-06T13:29:36Z New outbound peer connected: version: 70015, blocks=810906, peer=162 (block-relay-only)
2023-10-06T13:31:54Z New outbound peer connected: version: 70016, blocks=810906, peer=173 (block-relay-only)
2023-10-06T13:35:13Z New outbound peer connected: version: 70016, blocks=810906, peer=184 (block-relay-only)
2023-10-06T13:37:13Z New outbound peer connected: version: 70016, blocks=810906, peer=189 (block-relay-only)
2023-10-06T13:37:21Z Saw new header hash=00000000000000000001fe1d6da07c23f61d49bd20e637f53a227f5b6cf428aa height=810907
2023-10-06T13:37:23Z UpdateTip: new best=00000000000000000001fe1d6da07c23f61d49bd20e637f53a227f5b6cf428aa height=810907 version=0x24e88000 log2_work=94.459809 tx=903531631 date='2023-10-06T13:37:00Z' progress=1.000000 cache=566.6MiB(4107740txo)
2023-10-06T13:37:23Z tor: Thread interrupt
2023-10-06T13:37:23Z opencon thread exit
2023-10-06T13:37:23Z addcon thread exit
2023-10-06T13:37:23Z Shutdown: In progress...
2023-10-06T13:37:23Z torcontrol thread exit
2023-10-06T13:37:23Z net thread exit
2023-10-06T13:37:23Z msghand thread exit