Skip to content

Commit fe5cb3b

Browse files
committed
autogenerate-rpc-examples.py: canned blocks.
Unless REGENERATE_BLOCKCHAIN is true, in which case we make them. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 59c789b commit fe5cb3b

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

tests/autogenerate-rpc-examples.py

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def update_example(node, method, params, response=None, description=None):
9797
return response
9898

9999

100-
def setup_test_nodes(node_factory, bitcoind):
100+
def setup_test_nodes(node_factory, bitcoind, regenerate_blockchain):
101101
"""Sets up six test nodes for various transaction scenarios:
102102
l1, l2, l3 for transactions and forwards
103103
l4 for complex transactions (sendpayment, keysend, renepay)
@@ -137,8 +137,7 @@ def setup_test_nodes(node_factory, bitcoind):
137137
# Write the data/p2sh_wallet_hsm_secret to the hsm_path, so node can spend funds at p2sh_wrapped_addr
138138
p2sh_wrapped_addr = '2N2V4ee2vMkiXe5FSkRqFjQhiS9hKqNytv3'
139139
update_example(node=l1, method='upgradewallet', params={})
140-
txid = bitcoind.rpc.sendtoaddress(p2sh_wrapped_addr, 20000000 / 10 ** 8)
141-
bitcoind.generate_block(1)
140+
txid = bitcoind.send_and_mine_block(p2sh_wrapped_addr, 20000000)
142141
l1.daemon.wait_for_log('Owning output .* txid {} CONFIRMED'.format(txid))
143142
# Doing it with 'reserved ok' should have 1. We use a big feerate so we can get over the RBF hump
144143
update_example(node=l1, method='upgradewallet', params={'feerate': 'urgent', 'reservedok': True})
@@ -671,7 +670,7 @@ def generate_utils_examples(l1, l2, l3, l4, l5, l6, c23_2, c34_2, inv_l11, inv_l
671670
raise
672671

673672

674-
def generate_splice_examples(node_factory, bitcoind):
673+
def generate_splice_examples(node_factory, bitcoind, regenerate_blockchain):
675674
"""Generates splice related examples"""
676675
try:
677676
logger.info('Splice Start...')
@@ -727,7 +726,7 @@ def generate_splice_examples(node_factory, bitcoind):
727726
raise
728727

729728

730-
def generate_channels_examples(node_factory, bitcoind, l1, l3, l4, l5):
729+
def generate_channels_examples(node_factory, bitcoind, l1, l3, l4, l5, regenerate_blockchain):
731730
"""Generates fundchannel and openchannel related examples"""
732731
try:
733732
logger.info('Channels Start...')
@@ -999,7 +998,7 @@ def generate_autoclean_delete_examples(l1, l2, l3, l4, l5, c12, c23):
999998
raise
1000999

10011000

1002-
def generate_backup_recovery_examples(node_factory, l4, l5, l6):
1001+
def generate_backup_recovery_examples(node_factory, l4, l5, l6, regenerate_blockchain):
10031002
"""Node backup and recovery examples"""
10041003
try:
10051004
logger.info('Backup and Recovery Start...')
@@ -1156,11 +1155,39 @@ def setup_logging():
11561155

11571156

11581157
@unittest.skipIf(not GENERATE_EXAMPLES, 'Generates examples for doc/schema/lightning-*.json files.')
1158+
@pytest.mark.parametrize('bitcoind', [False], indirect=True)
11591159
def test_generate_examples(node_factory, bitcoind, executor):
11601160
"""Re-generates examples for doc/schema/lightning-*.json files"""
1161+
1162+
# Change this to True to regenerate bitcoin block & wallet.
1163+
regenerate_blockchain = (os.environ.get("REGENERATE_BLOCKCHAIN") == "1")
1164+
wallet_exists = os.access("tests/data/autogenerate-bitcoind-wallet.dat", os.F_OK)
11611165
try:
11621166
global ALL_RPC_EXAMPLES, REGENERATING_RPCS
11631167

1168+
if regenerate_blockchain:
1169+
if wallet_exists:
1170+
bitcoind.start(wallet_file="tests/data/autogenerate-bitcoind-wallet.dat")
1171+
else:
1172+
bitcoind.start()
1173+
else:
1174+
# This was created by bitcoind.rpc.backupwallet. Probably unnecessary,
1175+
# but reduces gratuitous differences if we have to regenerate the blockchain.
1176+
bitcoind.start(wallet_file="tests/data/autogenerate-bitcoind-wallet.dat")
1177+
with open("tests/data/autogenerate-bitcoin-blocks.json", "r") as f:
1178+
canned_blocks = json.load(f)
1179+
bitcoind.set_canned_blocks(canned_blocks)
1180+
1181+
info = bitcoind.rpc.getblockchaininfo()
1182+
assert info['blocks'] == 0
1183+
print(bitcoind.rpc.listwallets())
1184+
# 102 is a funny story. When we *submitblock* the first 101 blocks,
1185+
# our wallet balance is 0. When we *generate* the frist 101 blocks,
1186+
# our wallet balance is 50.
1187+
if info['blocks'] < 102:
1188+
bitcoind.generate_block(102 - info['blocks'])
1189+
assert bitcoind.rpc.getbalance() > 0
1190+
11641191
def list_all_examples():
11651192
"""list all methods used in 'update_example' calls to ensure that all methods are covered"""
11661193
try:
@@ -1210,7 +1237,7 @@ def list_missing_examples():
12101237
# We make sure everyone is on predicable time
12111238
os.environ['CLN_DEV_SET_TIME'] = '1738000000'
12121239

1213-
l1, l2, l3, l4, l5, l6, c12, c23, c25 = setup_test_nodes(node_factory, bitcoind)
1240+
l1, l2, l3, l4, l5, l6, c12, c23, c25 = setup_test_nodes(node_factory, bitcoind, regenerate_blockchain)
12141241
c23_2, c23res2, c34_2, inv_l11, inv_l21, inv_l22, inv_l31, inv_l32, inv_l34 = generate_transactions_examples(l1, l2, l3, l4, l5, c25, bitcoind)
12151242
rune_l21 = generate_runes_examples(l1, l2, l3)
12161243
generate_datastore_examples(l2)
@@ -1220,13 +1247,23 @@ def list_missing_examples():
12201247
generate_askrene_examples(l1, l2, l3, c12, c23_2)
12211248
generate_wait_examples(l1, l2, bitcoind, executor)
12221249
address_l22 = generate_utils_examples(l1, l2, l3, l4, l5, l6, c23_2, c34_2, inv_l11, inv_l22, rune_l21, bitcoind)
1223-
generate_splice_examples(node_factory, bitcoind)
1224-
generate_channels_examples(node_factory, bitcoind, l1, l3, l4, l5)
1250+
generate_splice_examples(node_factory, bitcoind, regenerate_blockchain)
1251+
generate_channels_examples(node_factory, bitcoind, l1, l3, l4, l5, regenerate_blockchain)
12251252
generate_autoclean_delete_examples(l1, l2, l3, l4, l5, c12, c23)
1226-
generate_backup_recovery_examples(node_factory, l4, l5, l6)
1253+
generate_backup_recovery_examples(node_factory, l4, l5, l6, regenerate_blockchain)
12271254
generate_list_examples(l1, l2, l3, c12, c23_2, inv_l31, inv_l32, offer_l23, inv_req_l1_l22, address_l22)
12281255
update_examples_in_schema_files()
12291256
logger.info('All Done!!!')
12301257
except Exception as e:
12311258
logger.error(e, exc_info=True)
12321259
sys.exit(1)
1260+
1261+
if regenerate_blockchain:
1262+
with open("tests/data/autogenerate-bitcoin-blocks.json", "w") as blockfile:
1263+
print(json.dump(bitcoind.save_blocks(), blockfile))
1264+
logger.info('tests/data/autogenerate-bitcoin-blocks.json updated')
1265+
1266+
# Very first run, we can dump wallet too.
1267+
if not wallet_exists:
1268+
bitcoind.rpc.backupwallet("tests/data/autogenerate-bitcoind-wallet.dat")
1269+
logger.info('tests/data/autogenerate-bitcoind-wallet.dat regenerated')

0 commit comments

Comments
 (0)