From 2f6e5f1fc2df75c0e1970ab357b4335d9133ad1d Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 2 Jun 2020 13:00:18 +0000 Subject: [PATCH] test.py: add test for halving calculator --- examples/halving-calculator.py | 23 ++++++++++++++++++----- test/test_py_d/ts_regtest.py | 7 +++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/halving-calculator.py b/examples/halving-calculator.py index 149c18b8..c66e13f9 100755 --- a/examples/halving-calculator.py +++ b/examples/halving-calculator.py @@ -2,15 +2,28 @@ # Demonstrates use of the MMGen asyncio/aiohttp JSON-RPC interface # https://github.com/mmgen/mmgen -# Requires a running Bitcoin Core node -# If necessary, invoke with --rpc-host/--rpc-port/--rpc-user/--rpc-password -# Specify aiohttp backend with --rpc-backend=aiohttp (Linux only) import time from decimal import Decimal from mmgen.common import * -opts.init() +opts.init({ + 'text': { + 'desc': 'Estimate date of next Bitcoin halving', + 'usage':'[opts]', + 'options': """ +-h, --help Print this help message +--, --longhelp Print help message for long options (common options) +-s, --sample-size=N Specify sample block range for block discovery time + estimate +""", + 'notes': """ +Requires a running Bitcoin Core node +If necessary, invoke with --rpc-host/--rpc-port/--rpc-user/--rpc-password +Specify aiohttp backend with --rpc-backend=aiohttp (Linux only) +""" + } +}) HalvingInterval = 210000 # src/chainparams.cpp @@ -36,7 +49,7 @@ async def main(): tip = await c.call('getblockcount') remaining = HalvingInterval - tip % HalvingInterval - sample_size = max(remaining,144) + sample_size = int(opt.sample_size) if opt.sample_size else max(remaining,144) # aiohttp backend will perform these two calls concurrently: cur,old = await c.gathered_call('getblockstats',((tip,),(tip - sample_size,))) diff --git a/test/test_py_d/ts_regtest.py b/test/test_py_d/ts_regtest.py index ad615922..4eef53a5 100755 --- a/test/test_py_d/ts_regtest.py +++ b/test/test_py_d/ts_regtest.py @@ -143,6 +143,7 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): cmd_group = ( ('setup', 'regtest (Bob and Alice) mode setup'), ('current_user', 'current user'), + ('halving_calculator_bob', 'halving calculator (Bob)'), ('walletgen_bob', 'wallet generation (Bob)'), ('walletgen_alice', 'wallet generation (Alice)'), ('addrgen_bob', 'address generation (Bob)'), @@ -283,6 +284,12 @@ class TestSuiteRegtest(TestSuiteBase,TestSuiteShared): t.expect('Bob') return t + def halving_calculator_bob(self): + t = self.spawn('halving-calculator.py',['--bob','--sample-size=144'],cmd_dir='examples') + t.expect('time until halving') + t.read() + return t + def walletgen(self,user): t = self.spawn('mmgen-walletgen',['-q','-r0','-p1','--'+user]) t.passphrase_new('new '+dfl_wcls.desc,rt_pw)