From 4f0da23b911db5b9426dd9ef1076681552de459f Mon Sep 17 00:00:00 2001 From: Scott Sadler Date: Mon, 19 Feb 2018 01:22:27 -0300 Subject: [PATCH] integration test for hoek and komodod with -ac_cc --- qa/cryptoconditions/README.md | 1 + qa/cryptoconditions/test_integration.py | 78 +++++++++++++++++++++++++ qa/cryptoconditions/testsupport.py | 47 +++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 qa/cryptoconditions/README.md create mode 100644 qa/cryptoconditions/test_integration.py create mode 100644 qa/cryptoconditions/testsupport.py diff --git a/qa/cryptoconditions/README.md b/qa/cryptoconditions/README.md new file mode 100644 index 000000000..6c8e01d60 --- /dev/null +++ b/qa/cryptoconditions/README.md @@ -0,0 +1 @@ +Integration tests for Crypto-Conditions diff --git a/qa/cryptoconditions/test_integration.py b/qa/cryptoconditions/test_integration.py new file mode 100644 index 000000000..a47b458e1 --- /dev/null +++ b/qa/cryptoconditions/test_integration.py @@ -0,0 +1,78 @@ +import sys +import time +import json +from testsupport import * + + +def wait_for_block(height): + for i in range(100): + try: + return rpc.getblock(str(height)) + except RPCError as e: + time.sleep(3) + raise Exception('Time out waiting for block at height %s' % height) + + +def sign_and_submit(tx): + signed = hoek.signTxBitcoin({'tx': tx, 'privateKeys': [notary_sk]}) + signed = hoek.signTxEd25519({'tx': signed['tx'], 'privateKeys': [alice_sk]}) + encoded = hoek.encodeTx(signed) + try: + rpc.getrawtransaction(encoded['txid']) + return encoded['txid'] + except RPCError: + pass + print >> sys.stderr, "submit transaction: %s:%s" % (encoded['txid'], json.dumps(signed)) + return rpc.sendrawtransaction(encoded['hex']) + + +def test_basic_spend(): + block = wait_for_block(3) + reward_txid = block['tx'][0] + reward_tx_raw = rpc.getrawtransaction(reward_txid) + reward_tx = hoek.decodeTx({'hex': reward_tx_raw}) + balance = reward_tx['tx']['outputs'][0]['amount'] + + spend0 = { + 'inputs': [ + {'txid': reward_txid, 'idx': 0, 'script': {'pubkey': notary_pk}} + ], + "outputs": [ + {"amount": 1000, "script": {"condition": cond_alice}}, + {"amount": balance - 1000, "script": {"address": notary_addr}} + ] + } + + spend0_txid = sign_and_submit(spend0) + + spend1 = { + 'inputs': [ + {'txid': spend0_txid, 'idx': 0, 'script': {"fulfillment": cond_alice}}, + {'txid': spend0_txid, 'idx': 1, 'script': {'address': notary_addr}} + ], + 'outputs': [ + {"amount": balance, "script": {"address": notary_addr}} + ] + } + + spend1_txid = sign_and_submit(spend1) + + assert rpc.getrawtransaction(spend1_txid) + + print("all done!") + + +notary_addr = 'RXSwmXKtDURwXP7sdqNfsJ6Ga8RaxTchxE' +notary_pk = '0205a8ad0c1dbc515f149af377981aab58b836af008d4d7ab21bd76faf80550b47' +notary_sk = 'UxFWWxsf1d7w7K5TvAWSkeX4H95XQKwdwGv49DXwWUTzPTTjHBbU' +alice_pk = '8ryTLBMnozUK4xUz7y49fjzZhxDDMK7c4mucLdbVY6jW' +alice_sk = 'E4ER7uYvaSTdpQFzTXNNSTkR6jNRJyqhZPJMGuU899nY' +cond_alice = {"type": "ed25519-sha-256", "publicKey": alice_pk} + + + +if __name__ == '__main__': + hoek = Hoek() + rpc = Komodod('/home/scott/.komodo/CCWAT/CCWAT.conf') + test_basic_spend() + diff --git a/qa/cryptoconditions/testsupport.py b/qa/cryptoconditions/testsupport.py new file mode 100644 index 000000000..166d98d14 --- /dev/null +++ b/qa/cryptoconditions/testsupport.py @@ -0,0 +1,47 @@ +import sys +import json +import subprocess + + +class RPCError(IOError): + pass + + +class JsonClient(object): + def __getattr__(self, method): + def inner(*args): + return self._exec(method, args) + return inner + + def load_response(self, data): + data = json.loads(data) + if data.get('error'): + raise RPCError(data['error']) + if 'result' in data: + return data['result'] + return data + + +def run_cmd(cmd): + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) + #print >>sys.stderr, "> %s" % repr(cmd)[1:-1] + assert proc.wait() == 0 + return proc.stdout.read() + + +class Hoek(JsonClient): + def _exec(self, method, args): + return self.load_response(run_cmd(['hoek', method, json.dumps(args[0])])) + + +class Komodod(JsonClient): + def __init__(self, conf_path): + urltpl = 'http://$rpcuser:$rpcpassword@${rpchost:-127.0.0.1}:$rpcport' + cmd = ['bash', '-c', '. "%s"; echo -n %s' % (conf_path, urltpl)] + self.url = run_cmd(cmd) + + def _exec(self, method, args): + req = {'method': method, 'params': args, 'id': 1} + cmd = ['curl', '-s', '-H', 'Content-Type: application/json', '-d', json.dumps(req), self.url] + return self.load_response(run_cmd(cmd)) +