-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [tests] move BCNode to separate module * [tests] move LitNode to separate module * [tests] Update copyright notice and python hashbang * [tests] Ensure we're running the minimum bitcoind version * [ci] download bitcoind from bitcoin.org * [tests] try to clean up bitcoind and lit processes on failure * [tests] remove global TMP_DIR * [tests] Improve failure logging * [tests] fixups * [ci] cache bitcoind and litecoind binaries
- Loading branch information
Showing
5 changed files
with
236 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2017 The lit developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
"""Classes representing bitcoind and litecoind nodes | ||
BCNode and LCNode represent a bitcoind and litecoind node respectively. | ||
They can be used to start/stop a bitcoin/litecoin node and communicate | ||
with it over RPC.""" | ||
import json | ||
import os | ||
import random | ||
import subprocess | ||
import sys | ||
import time | ||
|
||
import requests # `pip install requests` | ||
|
||
class BCNode(): | ||
"""A class representing a bitcoind node""" | ||
bin_name = "bitcoind" | ||
short_name = "bc" | ||
min_version = 140000 | ||
|
||
def __init__(self, i, tmd_dir): | ||
self.data_dir = tmd_dir + "/%snode%s" % (self.__class__.short_name, i) | ||
os.makedirs(self.data_dir) | ||
|
||
self.args = ["-regtest", "-datadir=%s" % self.data_dir, "-rpcuser=regtestuser", "-rpcpassword=regtestpass", "-rpcport=18332"] | ||
self.msg_id = random.randint(0, 9999) | ||
self.rpc_url = "http://regtestuser:[email protected]:18332" | ||
|
||
def start_node(self): | ||
try: | ||
self.process = subprocess.Popen([self.__class__.bin_name] + self.args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | ||
except FileNotFoundError: | ||
print("%s not found on path. Please install %s" % (self.__class__.bin_name, self.__class__.bin_name)) | ||
sys.exit(1) | ||
|
||
# Wait for process to start | ||
while True: | ||
if self.process.poll() is not None: | ||
raise Exception('%s exited with status %i during initialization' % (self.__class__.bin_name, self.process.returncode)) | ||
try: | ||
resp = self.getinfo() | ||
if resp.json()['error'] and resp.json()['error']['code'] == -28: | ||
# RPC is still in warmup. Sleep some more. | ||
continue | ||
# Check that we're running at least the minimum version | ||
assert resp.json()['result']['version'] > self.__class__.min_version | ||
break # break out of loop on success | ||
except requests.exceptions.ConnectionError as e: | ||
time.sleep(0.25) | ||
|
||
def send_message(self, method, params): | ||
self.msg_id += 1 | ||
rpcCmd = { | ||
"method": method, | ||
"params": params, | ||
"jsonrpc": "2.0", | ||
"id": str(self.msg_id) | ||
} | ||
payload = json.dumps(rpcCmd) | ||
|
||
return requests.post(self.rpc_url, headers={"Content-type": "application/json"}, data=payload) | ||
|
||
def __getattr__(self, name): | ||
"""Dispatches any unrecognised messages to the websocket connection""" | ||
def dispatcher(**kwargs): | ||
return self.send_message(name, kwargs) | ||
return dispatcher | ||
|
||
class LCNode(BCNode): | ||
"""A class representing a litecoind node""" | ||
bin_name = "litecoind" | ||
short_name = "lc" | ||
min_version = 130200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) 2017 The lit developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
"""Class representing a lit node | ||
LitNode represents a lit node. It can be used to start/stop a lit node | ||
and communicate with it over RPC.""" | ||
import os | ||
import subprocess | ||
|
||
from litpy import litrpc | ||
|
||
LIT_BIN = "%s/../lit" % os.path.abspath(os.path.dirname(__file__)) | ||
|
||
class LitNode(): | ||
"""A class representing a Lit node""" | ||
def __init__(self, i, tmp_dir): | ||
self.data_dir = tmp_dir + "/litnode%s" % i | ||
os.makedirs(self.data_dir) | ||
|
||
# Write a hexkey to the privkey file | ||
with open(self.data_dir + "/privkey.hex", 'w+') as f: | ||
f.write("1" * 63 + str(i) + "\n") | ||
|
||
self.args = ["-dir", self.data_dir] | ||
|
||
def start_node(self): | ||
self.process = subprocess.Popen([LIT_BIN] + self.args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | ||
|
||
def add_rpc_connection(self, ip, port): | ||
self.rpc = litrpc.LitConnection(ip, port) | ||
self.rpc.connect() | ||
|
||
def __getattr__(self, name): | ||
return self.rpc.__getattr__(name) |
Oops, something went wrong.