def start(self, ext_args=None, keep_datadir=False): if keep_datadir and self.datadir_path is not None: temp = self.datadir_path self.datadir_path = None self.shutdown() self.datadir_path = temp else: self.shutdown() # Create datadir and copy config into place self.datadir_path = tempfile.mkdtemp() shutil.copyfile(self.conf_path, self.datadir_path + '/' + self.daemon + '.conf') print("%s datadir: %s" % (self.name, self.datadir_path)) # Start process print("Starting %s" % self.name) if ext_args is None: ext_args = [] self.proc = subprocess.Popen( [self.path, "-datadir=" + self.datadir_path] + ext_args) self.rpc = AuthServiceProxy("http://" + self.config["rpcuser"] + ":" + self.config["rpcpassword"] + "@127.0.0.1:" + self.config["rpcport"]) # Give daemon a moment to start up time.sleep(1)
def __init__(self, node): threading.Thread.__init__(self) # query current longpollid templat = node.getblocktemplate() self.longpollid = templat['longpollid'] # create a new connection to the node, we can't use the same # connection from two threads self.node = AuthServiceProxy(node.url, timeout=600)
class LongpollThread(threading.Thread): def __init__(self, node): threading.Thread.__init__(self) # query current longpollid templat = node.getblocktemplate() self.longpollid = templat['longpollid'] # create a new connection to the node, we can't use the same # connection from two threads self.node = AuthServiceProxy(node.url, timeout=600) def run(self): self.node.getblocktemplate({'longpollid':self.longpollid})
def run_allowip_test(tmpdir, allow_ips, rpchost, rpcport): ''' Start a node with rpcwallow IP, and request getinfo at a non-localhost IP. ''' base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips] nodes = start_nodes(1, tmpdir, [base_args]) try: # connect to node through non-loopback interface url = "http://*****:*****@%s:%d" % (rpchost, rpcport,) node = AuthServiceProxy(url) node.getinfo() finally: node = None # make sure connection will be garbage collected and closed stop_nodes(nodes) wait_bitcoinds()
def startbitcoind(datadir, conf, args=""): subprocess.Popen( (BITCOINPATH + "/bitcoind -datadir=" + datadir + " " + args).split(), stdout=subprocess.PIPE) return AuthServiceProxy("http://" + conf["rpcuser"] + ":" + conf["rpcpassword"] + "@127.0.0.1:" + conf["rpcport"])
def startelementsd(datadir, conf, args=""): subprocess.Popen((ELEMENTSPATH + "/elementsd -datadir=" + datadir + " " + args).split(), stdout=subprocess.PIPE) return AuthServiceProxy("http://" + conf["rpcuser"] + ":" + conf["rpcpassword"] + "@127.0.0.1:" + conf["rpcport"])
def run_allowip_test(tmpdir, allow_ips, rpchost, rpcport): ''' Start a node with rpcwallow IP, and request getinfo at a non-localhost IP. ''' base_args = ['-disablewallet', '-nolisten'] + ['-rpcallowip='+x for x in allow_ips] nodes = start_nodes(1, tmpdir, [base_args]) try: # connect to node through non-loopback interface url = "http://*****:*****@%s:%d" % (rpchost, rpcport,) node = AuthServiceProxy(url) node.getinfo() finally: node = None # make sure connection will be garbage collected and closed stop_nodes(nodes) wait_bitcoinds()
def startbitcoind(datadir, conf, args=""): command = "bitcoind -datadir=" + datadir + " -conf=./bitcoin.conf" print("[Info] Initializing " + command) subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) # print("[Debug] startbitcoind - Initilizing bitcoind with command: ", command) # print("[DEBUG] startbitcoind - http://"+conf["rpcuser"]+": # "+conf["rpcpassword"]+"@127.0.0.1:"+conf["rpcport"]) return AuthServiceProxy("http://" + conf["rpcuser"] + ":" + conf["rpcpassword"] + "@127.0.0.1:" + conf["rpcport"])
def start_daemon(datadir, conf, args=""): if pegin: args += " -validatepegin" if regtest: args += " -regtest" subprocess.Popen( (SIDECHAIN_PATH + " -datadir=" + datadir + " " + args).split(), stdout=subprocess.PIPE) return AuthServiceProxy("http://" + conf["rpcuser"] + ":" + conf["rpcpassword"] + "@127.0.0.1:" + conf["rpcport"])
def start_mainchain(datadir, conf, args=""): if not pegin: return if regtest: args += " -regtest" subprocess.Popen( (MAINCHAIN_PATH + " -datadir=" + datadir + " " + args).split(), stdout=subprocess.PIPE) return AuthServiceProxy("http://" + conf["rpcuser"] + ":" + conf["rpcpassword"] + "@127.0.0.1:" + conf["rpcport"])
def startbitcoind(datadir, conf, args=""): print("datadir: " + datadir) subprocess.call("echo $HOME", shell=True) subprocess.call("bitcoind -daemon -datadir=" + datadir + "", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("waiting for bitcoind......") return AuthServiceProxy("http://" + conf["rpcuser"] + ":" + conf["rpcpassword"] + "@127.0.0.1:" + conf["rpcport"])
def __getattr__(self, name): assert not (name.startswith("__") and name.endswith("__")), "Python internals" with open(self.cookie_path) as fd: authpair = fd.read() service_url = ( f"http://{authpair}@localhost:{self.rpc_port}/wallet/{self.wallet_name}" ) proxy = AuthServiceProxy(service_url, name) def f(*args): return proxy.__call__(*args) # Make debuggers show <function bitcoin.rpc.name> rather than <function # bitcoin.rpc.<lambda>> f.__name__ = name return f
def _handle_request(self, r, path): """Handle a JSONRPC request {r} made to the HTTP endpoint {path} (to handle wallet paths)""" method = r["method"] # If we have set a mock for this method reply with that if method in self.mocks: return {"id": r["id"], "error": None, "result": self.mocks[method]} # Otherwise, just forward the request with open(self.bitcoind_cookie_path) as fd: authpair = fd.read() service_url = f"http://{authpair}@localhost:{self.bitcoind_rpc_port}/{path}" try: res = AuthServiceProxy(service_url, r["method"])(*r["params"]) return {"result": res, "id": r["id"]} except JSONRPCException as e: return {"error": e.error, "id": r["id"]}
subprocess.Popen(bitcoind2start.split(), stdout=subprocess.PIPE) sidechainstart = sidechain_bin_path + "/liquidd -datadir=" + sidechain_datadir + sidechain_args subprocess.Popen(sidechainstart.split(), stdout=subprocess.PIPE) # Start with invalid fedpegscript sidechain2start = sidechain_bin_path + "/liquidd -datadir=" + sidechain2_datadir + " -fedpegscript=" + bad_fedpeg_script subprocess.Popen(sidechain2start.split(), stdout=subprocess.PIPE) print("Daemons started") time.sleep(3) with open(bitcoin2_rpccookiefile, 'r') as f: bitcoin2_rpccookie = f.readline() bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(bitcoin_port)) bitcoin2 = AuthServiceProxy("http://" + bitcoin2_rpccookie + "@127.0.0.1:" + str(bitcoin2_port)) sidechain = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain_port)) sidechain2 = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain2_port)) try: sidechain2.getwalletinfo() raise Exception('Invalid fedpegscript should abort start') except ConnectionRefusedError: pass sidechain2start = sidechain_bin_path + "/liquidd -datadir=" + sidechain2_datadir + sidechain_args subprocess.Popen(sidechain2start.split(), stdout=subprocess.PIPE)
" and " + sidechain2_datadir) bitcoindstart = sys.argv[1] + "/bitcoind -datadir=" + bitcoin_datadir subprocess.Popen(bitcoindstart.split(), stdout=subprocess.PIPE) sidechainstart = sys.argv[ 2] + "/elementsd -datadir=" + sidechain_datadir + sidechain_args subprocess.Popen(sidechainstart.split(), stdout=subprocess.PIPE) sidechain2start = sys.argv[ 2] + "/elementsd -datadir=" + sidechain2_datadir + sidechain_args subprocess.Popen(sidechain2start.split(), stdout=subprocess.PIPE) print("Daemons started") time.sleep(3) bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(bitcoin_port)) sidechain = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain_port)) sidechain2 = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain2_port)) print("Daemons started, making blocks to get funds") bitcoin.generate(101) sidechain.generate(101) addr = bitcoin.getnewaddress() addrs = sidechain.getpeginaddress() txid1 = bitcoin.sendtoaddress(addrs["mainchain_address"], 24) # 10+2 confirms required to get into mempool and confirm bitcoin.generate(11)
def bstash_connect(): url = "http://%s:%s@%s:%s" % (RPC_USER, RPC_PASS, RPC_IP, RPC_PORT) return AuthServiceProxy(url)
def run_test(self): ''' The test creates a sc, send funds to it and then: ( 1) Switch ON the CZendooLowPrioThreadGuard that should prevent any mempool Cert to be included, because of ProofVerifier threads are on pause. ( 2) Switch OFF the CZendooLowPrioThreadGuard -> certificate must be applied to mempool. ( 3) Switch ON the CZendooLowPrioThreadGuard and try to generate the block with Certificate - must be successful ''' # forward transfer amounts creation_amount = Decimal("0.5") fwt_amount = Decimal("50") bwt_amount_bad = Decimal("100.0") bwt_amount = Decimal("50") self.nodes[0].getblockhash(0) mark_logs("Node generates {} block".format(MINIMAL_SC_HEIGHT), self.nodes, DEBUG_MODE) self.nodes[0].generate(MINIMAL_SC_HEIGHT) # SC creation # Generate wCertVk and constant mcTest = CertTestUtils(self.options.tmpdir, self.options.srcdir) vk = mcTest.generate_params("sc1") constant = generate_random_field_element_hex() cmdInput = { 'version': 0, 'withdrawalEpochLength': EPOCH_LENGTH, 'toaddress': "dada", 'amount': creation_amount, 'wCertVk': vk, 'constant': constant } ret = self.nodes[0].sc_create(cmdInput) creating_tx = ret['txid'] scid = ret['scid'] scid_swapped = str(swap_bytes(scid)) mark_logs("Node created the SC spending {} coins via tx {}.".format(creation_amount, creating_tx), self.nodes, DEBUG_MODE) decoded_tx = self.nodes[0].getrawtransaction(creating_tx, 1) assert_equal(scid, decoded_tx['vsc_ccout'][0]['scid']) mark_logs("created SC id: {}".format(scid), self.nodes, DEBUG_MODE) mark_logs("Node confirms Sc creation generating 1 block", self.nodes, DEBUG_MODE) self.nodes[0].generate(1) assert_equal(self.nodes[0].getscinfo(scid)['items'][0]['balance'], Decimal(0)) assert_equal(self.nodes[0].getscinfo(scid)['items'][0]['immatureAmounts'][0]['amount'], creation_amount) # Fwd Transfer to Sc bal_before_fwd_tx = self.nodes[0].getbalance("", 0) mark_logs("Node balance before fwd tx: {}".format(bal_before_fwd_tx), self.nodes, DEBUG_MODE) mc_return_address = self.nodes[0].getnewaddress() cmdInput = [{'toaddress': "abcd", 'amount': fwt_amount, "scid": scid, "mcReturnAddress": mc_return_address}] fwd_tx = self.nodes[0].sc_send(cmdInput) mark_logs("Node transfers {} coins to SC with tx {}...".format(fwt_amount, fwd_tx), self.nodes, DEBUG_MODE) mark_logs("Node confirms fwd transfer generating 1 block", self.nodes, DEBUG_MODE) self.nodes[0].generate(1) assert_equal(self.nodes[0].getscinfo(scid)['items'][0]['balance'], Decimal(0)) assert_equal(self.nodes[0].getscinfo(scid)['items'][0]['immatureAmounts'][0]['amount'], creation_amount) assert_equal(self.nodes[0].getscinfo(scid)['items'][0]['immatureAmounts'][1]['amount'], fwt_amount) nblocks = EPOCH_LENGTH - 2 mark_logs("Node0 generating {} more blocks to achieve end of withdrawal epoch".format(nblocks), self.nodes, DEBUG_MODE) self.nodes[0].generate(nblocks) assert_equal(self.nodes[0].getscinfo(scid)['items'][0]['balance'], creation_amount + fwt_amount) # Sc balance has matured assert_equal(len(self.nodes[0].getscinfo(scid)['items'][0]['immatureAmounts']), 0) epoch_number, epoch_cum_tree_hash = get_epoch_data(scid, self.nodes[0], EPOCH_LENGTH) mark_logs("epoch_number = {}, epoch_cum_tree_hash = {}".format(epoch_number, epoch_cum_tree_hash), self.nodes, DEBUG_MODE) addr_node0 = self.nodes[0].getnewaddress() #Create proof for WCert quality = 10 proof = mcTest.create_test_proof( "sc1", scid_swapped, epoch_number, quality, MBTR_SC_FEE, FT_SC_FEE, epoch_cum_tree_hash, constant, [addr_node0], [bwt_amount]) amount_cert_1 = [{"address": addr_node0, "amount": bwt_amount}] # Enable CZendooLowPrioThreadGuard mark_logs("Enable CZendooLowPrioThreadGuard...", self.nodes, DEBUG_MODE) res = self.nodes[0].setproofverifierlowpriorityguard(True) assert_equal(res["enabled"], True) # Try to send WCert - should fail because of the timeout: mempool proof verifier has low priority mark_logs("Node sends a certificate while CZendooLowPrioThreadGuard is enabled...", self.nodes, DEBUG_MODE) try: self.nodes[0].sc_send_certificate(scid, epoch_number, quality, epoch_cum_tree_hash, proof, amount_cert_1, FT_SC_FEE, MBTR_SC_FEE, CERT_FEE) assert(False) except Exception as e: errorString = e.message assert("timed out" == errorString) # Establish new AuthServiceProxy, because previous one is dead after the timeout error self.nodes[0] = AuthServiceProxy(self.nodes[0].get_service_url(), timeout=10) mark_logs("Send certificate failed with reason {}".format(errorString), self.nodes, DEBUG_MODE) # Disable CZendooLowPrioThreadGuard mark_logs("Disable CZendooLowPrioThreadGuard...", self.nodes, DEBUG_MODE) res = self.nodes[0].setproofverifierlowpriorityguard(False) assert_equal(res["enabled"], False) # Try to send WCert mark_logs("Node sends a certificate while CZendooLowPrioThreadGuard is enabled...", self.nodes, DEBUG_MODE) try: cert_epoch_0 = self.nodes[0].sc_send_certificate(scid, epoch_number, quality, epoch_cum_tree_hash, proof, amount_cert_1, FT_SC_FEE, MBTR_SC_FEE, CERT_FEE) assert (len(cert_epoch_0) > 0) mark_logs("Certificate is {}".format(cert_epoch_0), self.nodes, DEBUG_MODE) except JSONRPCException, e: errorString = e.error['message'] mark_logs("Send certificate failed with reason {}".format(errorString), self.nodes, DEBUG_MODE) assert (False)
" and " + sidechain2_datadir) bitcoindstart = sys.argv[1] + "/bitcoind -datadir=" + bitcoin_datadir subprocess.Popen(bitcoindstart.split(), stdout=subprocess.PIPE) sidechainstart = sys.argv[ 2] + "/elementsd -datadir=" + sidechain_datadir + sidechain_args subprocess.Popen(sidechainstart.split(), stdout=subprocess.PIPE) sidechain2start = sys.argv[ 2] + "/elementsd -datadir=" + sidechain2_datadir + sidechain_args subprocess.Popen(sidechain2start.split(), stdout=subprocess.PIPE) print("Daemons started") time.sleep(3) bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(bitcoin_port)) sidechain = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain_port)) sidechain2 = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain2_port)) print("Daemons started, making blocks to get funds") bitcoin.generate(101) sidechain.generate(101) addr = bitcoin.getnewaddress() # Lockup some funds to unlock later sidechain.sendtomainchain(addr, 50) # Tests withdrawlock tracking in database sidechain.generate(1)
class Daemon(): """ A class for representing a bitcoind or elementsd node. Wraps the process management, creation and deletion of datadirs, and RPC connectivity, into a simple object that will clean itself up on exit. The `cleanup_on_exit` parameter can be set to False to prevent the node from deleting its datadir on restart (and can be set by passing --no-cleanup to the main program). """ def __init__(self, name, daemon, path, conf_path, cleanup_on_exit=True): self.name = name self.daemon = daemon self.conf_path = path self.path = path self.cleanup_on_exit = cleanup_on_exit self.conf_path = conf_path self.datadir_path = None self.proc = None self.rpc = None # Parse config self.config = {} with open(self.conf_path, encoding="utf8") as f: for line in f: if len(line) == 0 or line[0] == "#" or len( line.split("=")) != 2: continue self.config[line.split("=")[0]] = line.split("=")[1].strip() def shutdown(self): if self.proc is not None: print("Shutting down %s" % self.name) self.proc.terminate() ## FIXME determine why we need 30+ seconds to shut down with a tiny regtest chain self.proc.wait(120) self.proc = None if self.datadir_path is not None: if self.cleanup_on_exit: shutil.rmtree(self.datadir_path) else: print("Leaving %s datadir at %s." % (self.name, self.datadir_path)) def start(self, ext_args=None, keep_datadir=False): if keep_datadir and self.datadir_path is not None: temp = self.datadir_path self.datadir_path = None self.shutdown() self.datadir_path = temp else: self.shutdown() # Create datadir and copy config into place self.datadir_path = tempfile.mkdtemp() shutil.copyfile(self.conf_path, self.datadir_path + '/' + self.daemon + '.conf') print("%s datadir: %s" % (self.name, self.datadir_path)) # Start process print("Starting %s" % self.name) if ext_args is None: ext_args = [] self.proc = subprocess.Popen( [self.path, "-datadir=" + self.datadir_path] + ext_args) self.rpc = AuthServiceProxy("http://" + self.config["rpcuser"] + ":" + self.config["rpcpassword"] + "@127.0.0.1:" + self.config["rpcport"]) # Give daemon a moment to start up time.sleep(1) def connect_to(self, other): self.addnode("localhost:%s" % other.config['port'], "onetry") def restart(self, ext_args=None, keep_datadir=False): self.start(ext_args, keep_datadir) def __del__(self): self.shutdown() def __getattr__(self, name): """Dispatches any unrecognised messages to the RPC connection or a CLI instance.""" return self.rpc.__getattr__(name) def __getitem__(self, key): """Dispatches any keys to the underlying config file""" return self.config[key]
bitcoind2start = bitcoin_bin_path+"/bitcoind -datadir="+bitcoin2_datadir subprocess.Popen(bitcoind2start.split(), stdout=subprocess.PIPE) sidechainstart = sidechain_bin_path+"/elementsd -datadir="+sidechain_datadir + sidechain_args subprocess.Popen(sidechainstart.split(), stdout=subprocess.PIPE) sidechain2start = sidechain_bin_path+"/elementsd -datadir="+sidechain2_datadir + sidechain_args subprocess.Popen(sidechain2start.split(), stdout=subprocess.PIPE) print("Daemons started") time.sleep(3) with open(bitcoin2_rpccookiefile, 'r') as f: bitcoin2_rpccookie = f.readline() bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:"+str(bitcoin_port)) bitcoin2 = AuthServiceProxy("http://"+ bitcoin2_rpccookie +"@127.0.0.1:"+str(bitcoin2_port)) sidechain = AuthServiceProxy("http://*****:*****@127.0.0.1:"+str(sidechain_port)) sidechain2 = AuthServiceProxy("http://*****:*****@127.0.0.1:"+str(sidechain2_port)) print("Daemons started, making blocks to get funds") bitcoin.generate(101) sidechain.generate(101) addr = bitcoin.getnewaddress() # First, blackhole all 21M bitcoin that already exist(and test subtractfrom) assert(sidechain.getwalletinfo()["balance"]["bitcoin"] == 21000000) sidechain.sendtomainchain(addr, 21000000, True) assert("bitcoin" not in sidechain.getwalletinfo()["balance"])
# Start daemons print("Starting daemons at " + bitcoin_datadir + ", " + sidechain_datadir + " and " + sidechain2_datadir) bitcoindstart = sys.argv[1] + "/bitcoind -datadir=" + bitcoin_datadir subprocess.Popen(bitcoindstart.split(), stdout=subprocess.PIPE) sidechainstart = sys.argv[2] + "/elementsd -datadir=" + sidechain_datadir subprocess.Popen(sidechainstart.split(), stdout=subprocess.PIPE) sidechain2start = sys.argv[2] + "/elementsd -datadir=" + sidechain2_datadir subprocess.Popen(sidechain2start.split(), stdout=subprocess.PIPE) print("Daemons started") time.sleep(2) bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(bitcoin_port)) sidechain = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain_port)) sidechain2 = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain2_port)) print("Daemons started, making blocks to get funds") bitcoin.generate(101) sidechain.generate(101) addr = bitcoin.getnewaddress() # Lockup some funds to unlock later sidechain.sendtomainchain(addr, 50) sidechain.generate(101)
f.write("mainchainrpcpassword="******"\n") f.write("validatepegin=1\n") f.write("validatepegout=0\n") # Start daemons print("Starting daemons at " + bitcoin_datadir + " and " + sidechain_datadir) bitcoindstart = sys.argv[1] + "/bitcoind -datadir=" + bitcoin_datadir subprocess.Popen(bitcoindstart.split(), stdout=subprocess.PIPE) sidechainstart = sys.argv[2] + "/elementsd -datadir=" + sidechain_datadir subprocess.Popen(sidechainstart.split(), stdout=subprocess.PIPE) print("Daemons started") time.sleep(2) bitcoin = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(bitcoin_port)) sidechain = AuthServiceProxy("http://*****:*****@127.0.0.1:" + str(sidechain_port)) print("Daemons started, making blocks to get funds") bitcoin.generate(101) sidechain.generate(101) addr = bitcoin.getnewaddress() # Lockup some funds to unlock later sidechain.sendtomainchain(addr, 50) sidechain.generate(101) addrs = sidechain.getpeginaddress() txid = bitcoin.sendtoaddress(addrs["mainchain_address"], 49)