pytest: add helper to send node a gossip query and get response(s).

Note the use of sqrt, which makes a 13 second timeout under Travis
(180 second), or 7 seconds normally.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-09-30 11:05:19 +09:30
parent 38524d907b
commit 025b0e6a09

View File

@ -8,12 +8,14 @@ from lightning import LightningRpc
import json import json
import logging import logging
import lzma import lzma
import math
import os import os
import random import random
import re import re
import shutil import shutil
import sqlite3 import sqlite3
import string import string
import struct
import subprocess import subprocess
import threading import threading
import time import time
@ -750,6 +752,29 @@ class LightningNode(object):
wait_for(lambda: txid in self.bitcoin.rpc.getrawmempool()) wait_for(lambda: txid in self.bitcoin.rpc.getrawmempool())
def query_gossip(self, querytype, *args):
"""Generate a gossip query, feed it into this node and get responses
in hex"""
query = subprocess.run(['devtools/mkquery',
querytype] + [str(a) for a in args],
check=True,
timeout=TIMEOUT,
stdout=subprocess.PIPE).stdout.strip()
out = subprocess.run(['devtools/gossipwith',
'--timeout-after={}'.format(int(math.sqrt(TIMEOUT) * 1000)),
'{}@localhost:{}'.format(self.info['id'],
self.port),
query],
check=True,
timeout=TIMEOUT, stdout=subprocess.PIPE).stdout
msgs = []
while len(out):
length = struct.unpack('>H', out[0:2])[0]
msgs.append(out[2:2 + length].hex())
out = out[2 + length:]
return msgs
class NodeFactory(object): class NodeFactory(object):
"""A factory to setup and start `lightningd` daemons. """A factory to setup and start `lightningd` daemons.