From 9d7d429010d4b569f0e2739168fa5b34f0763015 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Fri, 22 Aug 2014 12:01:36 -0400 Subject: [PATCH 01/17] Update wif prefixes for changes in pycoin interface --- lib/bitcoin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index 63b2a296f0..ff5cbd1814 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -384,9 +384,10 @@ def sort_unspent_txouts(unspent, allow_unconfirmed_inputs): return unspent def private_key_to_public_key (private_key_wif): - # allowable_wif_prefixes = [ + allowable_wif_prefixes = [b'x80', b'\xef'] # Bitcoin mainnet, testnet3 try: - secret_exponent, compressed = wif_to_tuple_of_secret_exponent_compressed(private_key_wif, is_test=config.TESTNET) + secret_exponent, compressed = wif_to_tuple_of_secret_exponent_compressed( + private_key_wif, allowable_wif_prefixes) except EncodingError: raise exceptions.AltcoinSupportError('pycoin: unsupported WIF prefix') public_pair = public_pair_for_secret_exponent(generator_secp256k1, secret_exponent) From 4d95ec0dc03630796cf9f7edbcfa332e88a59ab9 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Sun, 17 Aug 2014 17:37:12 -0400 Subject: [PATCH 02/17] Initial asyncio implementation for talking to bitcoind --- counterpartyd.py | 14 ++++++---- lib/bitcoin.py | 72 +++++++++++++++++++++++++----------------------- lib/blocks.py | 36 ++++++++++++++---------- 3 files changed, 67 insertions(+), 55 deletions(-) diff --git a/counterpartyd.py b/counterpartyd.py index 5e12b32da6..73233efaa1 100755 --- a/counterpartyd.py +++ b/counterpartyd.py @@ -14,7 +14,7 @@ import binascii from fractions import Fraction -import requests +import asyncio, aiohttp import appdirs from prettytable import PrettyTable @@ -97,7 +97,7 @@ def market (give_asset, get_asset): # Your Pending Orders Matches. addresses = [] - for bunch in bitcoin.get_wallet(): + for bunch in loop.run_until_complete(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), @@ -521,6 +521,7 @@ def generate_move_random_hash(move): #patch up cmd.exe's "challenged" (i.e. broken/non-existent) UTF-8 logging util_windows.fix_win32_unicode() + loop = asyncio.get_event_loop() # Parse command-line arguments. parser = argparse.ArgumentParser(prog=config.XCP_CLIENT, description='the reference implementation of the {} protocol'.format(config.XCP_NAME)) parser.add_argument('-V', '--version', action='version', version="{} v{}".format(config.XCP_CLIENT, config.VERSION_STRING)) @@ -1029,7 +1030,7 @@ def generate_move_random_hash(move): totals = {} print() - for bunch in bitcoin.get_wallet(): + for bunch in loop.run_until_complete(bitcoin.get_wallet()): address, btc_balance = bunch[:2] address_data = get_address(db, address=address) balances = address_data['balances'] @@ -1064,7 +1065,7 @@ def generate_move_random_hash(move): elif args.action == 'pending': addresses = [] - for bunch in bitcoin.get_wallet(): + for bunch in loop.run_until_complete(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), @@ -1113,7 +1114,10 @@ def generate_move_random_hash(move): raise Exception("Blockchain backend (%s) not initialized! Aborting startup after %i tries." % ( config.BLOCKCHAIN_SERVICE_NAME, num_tries)) - blocks.follow(db) + asyncio.async(asyncio.Task(blocks.follow(db))) + loop.run_forever() # Go do everything + + else: parser.print_help() diff --git a/lib/bitcoin.py b/lib/bitcoin.py index ff5cbd1814..34ececed68 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -14,7 +14,7 @@ import decimal import logging -import requests +import asyncio, aiohttp from pycoin.ecdsa import generator_secp256k1, public_pair_for_secret_exponent from pycoin.encoding import wif_to_tuple_of_secret_exponent_compressed, public_pair_to_sec, is_sec_compressed, EncodingError from Crypto.Cipher import ARC4 @@ -41,84 +41,85 @@ def print_coin(coin): return 'amount: {}; txid: {}; vout: {}; confirmations: {}'.format(coin['amount'], coin['txid'], coin['vout'], coin.get('confirmations', '?')) # simplify and make deterministic def get_block_count(): - return int(rpc('getblockcount', [])) + count= yield from rpc('getblockcount', []) + return int(count) def get_block_hash(block_index): - return rpc('getblockhash', [block_index]) + return(yield from rpc('getblockhash', [block_index])) def is_valid (address): - return rpc('validateaddress', [address])['isvalid'] + return((yield from rpc('validateaddress', [address]))['isvalid']) def is_mine (address): - return rpc('validateaddress', [address])['ismine'] + return(yield from rpc('validateaddress', [address])['ismine']) def send_raw_transaction (tx_hex): - return rpc('sendrawtransaction', [tx_hex]) + return(yield from rpc('sendrawtransaction', [tx_hex])) def get_raw_transaction (tx_hash, json=True): if json: - return rpc('getrawtransaction', [tx_hash, 1]) + return(yield from rpc('getrawtransaction', [tx_hash, 1])) else: - return rpc('getrawtransaction', [tx_hash]) + return(yield from rpc('getrawtransaction', [tx_hash])) def get_block (block_hash): - return rpc('getblock', [block_hash]) + return(yield from rpc('getblock', [block_hash])) def get_block_hash (block_index): - return rpc('getblockhash', [block_index]) + return(yield from rpc('getblockhash', [block_index])) def decode_raw_transaction (unsigned_tx_hex): - return rpc('decoderawtransaction', [unsigned_tx_hex]) + return(yield from rpc('decoderawtransaction', [unsigned_tx_hex])) def get_wallet (): - for group in rpc('listaddressgroupings', []): + addressgroupings = yield from rpc('listaddressgroupings', []) + for group in addressgroupings: for bunch in group: yield bunch def get_mempool (): - return rpc('getrawmempool', []) + return(yield from rpc('getrawmempool', [])) def get_info(): - return rpc('getinfo', []) + return(yield from rpc('getinfo', [])) def bitcoind_check (db): """Checks blocktime of last block to see if {} Core is running behind.""".format(config.BTC_NAME) - block_count = rpc('getblockcount', []) - block_hash = rpc('getblockhash', [block_count]) - block = rpc('getblock', [block_hash]) + block_count = yield from rpc('getblockcount', []) + block_hash = yield from rpc('getblockhash', [block_count]) + block = yield from rpc('getblock', [block_hash]) time_behind = time.time() - block['time'] # How reliable is the block time?! if time_behind > 60 * 60 * 2: # Two hours. raise exceptions.BitcoindError('Bitcoind is running about {} seconds behind.'.format(round(time_behind))) -def connect (host, payload, headers): - global bitcoin_rpc_session - if not bitcoin_rpc_session: bitcoin_rpc_session = requests.Session() +def connect (url, payload, headers): TRIES = 12 for i in range(TRIES): try: - response = bitcoin_rpc_session.post(host, data=json.dumps(payload), headers=headers, verify=config.BACKEND_RPC_SSL_VERIFY) + response = yield from asyncio.Task(aiohttp.request('POST', url, data=json.dumps(payload), + headers=headers)) if i > 0: print('Successfully connected.', file=sys.stderr) return response - except requests.exceptions.SSLError as e: - raise e - except requests.exceptions.ConnectionError: - logging.debug('Could not connect to Bitcoind. (Try {}/{})'.format(i+1, TRIES)) + except aiohttp.ConnectionError: + print('Could not connect to Bitcoind. Sleeping for five seconds. (Try {}/{})'.format(i+1, TRIES), file=sys.stderr) time.sleep(5) return None def wallet_unlock (): - getinfo = get_info() + getinfo = yield from get_info() if 'unlocked_until' in getinfo: if getinfo['unlocked_until'] >= 60: return True # Wallet is unlocked for at least the next 60 seconds. else: passphrase = getpass.getpass('Enter your Bitcoind[‐Qt] wallet passhrase: ') print('Unlocking wallet for 60 (more) seconds.') - rpc('walletpassphrase', [passphrase, 60]) + yield from rpc('walletpassphrase', [passphrase, 60]) else: return True # Wallet is unencrypted. +@asyncio.coroutine def rpc (method, params): + starttime = time.time() headers = {'content-type': 'application/json'} payload = { "method": method, @@ -135,13 +136,13 @@ def rpc (method, params): f.write(payload) ''' - response = connect(config.BACKEND_RPC, payload, headers) + response = yield from connect(config.BACKEND_RPC, payload, headers) if response == None: if config.TESTNET: network = 'testnet' else: network = 'mainnet' raise exceptions.BitcoindRPCError('Cannot communicate with {} Core. ({} is set to run on {}, is {} Core?)'.format(config.BTC_NAME, config.XCP_CLIENT, network, config.BTC_NAME)) - elif response.status_code not in (200, 500): - raise exceptions.BitcoindRPCError(str(response.status_code) + ' ' + response.reason) + elif response.status not in (200, 500): + raise exceptions.BitcoindRPCError(str(response.status) + ' ' + response.reason) ''' if config.UNITTEST: @@ -150,7 +151,7 @@ def rpc (method, params): ''' # Return result, with error handling. - response_json = response.json() + response_json = yield from response.json() if 'error' not in response_json.keys() or response_json['error'] == None: return response_json['result'] elif response_json['error']['code'] == -5: # RPC_INVALID_ADDRESS_OR_KEY @@ -158,7 +159,7 @@ def rpc (method, params): elif response_json['error']['code'] == -4: # Unknown private key (locked wallet?) # If address in wallet, attempt to unlock. address = params[0] - validate_address = rpc('validateaddress', [address]) + validate_address = yield from rpc('validateaddress', [address]) if validate_address['isvalid']: if validate_address['ismine']: raise exceptions.BitcoindError('Wallet is locked.') @@ -168,7 +169,7 @@ def rpc (method, params): raise exceptions.AddressError('Invalid address.') elif response_json['error']['code'] == -1 and response_json['message'] == 'Block number out of range.': time.sleep(10) - return rpc('getblockhash', [block_index]) + return(yield from rpc('getblockhash', [block_index])) # elif config.UNITTEST: # print(method) @@ -432,7 +433,7 @@ def transaction (tx_info, encoding='auto', fee_per_kb=config.DEFAULT_FEE_PER_KB, if config.UNITTEST: private_key_wif = config.UNITTEST_PRIVKEY[source] else: - private_key_wif = rpc('dumpprivkey', [source]) + private_key_wif = yield from rpc('dumpprivkey', [source]) # Derive public key. public_key_hex = private_key_to_public_key(private_key_wif) @@ -460,7 +461,8 @@ def transaction (tx_info, encoding='auto', fee_per_kb=config.DEFAULT_FEE_PER_KB, # Check that the source is in wallet. if not config.UNITTEST and encoding in ('multisig') and not public_key: - if not rpc('validateaddress', [source])['ismine']: + ismine = yield from rpc('validateaddress', [source]) + if not ismine['ismine']: raise exceptions.AddressError('Not one of your Bitcoin addresses:', source) # Check that the destination output isn't a dust output. diff --git a/lib/blocks.py b/lib/blocks.py index 5a5c8f0d2c..10a7f53afb 100644 --- a/lib/blocks.py +++ b/lib/blocks.py @@ -15,6 +15,7 @@ from Crypto.Cipher import ARC4 import apsw +import asyncio from . import (config, exceptions, util, bitcoin) from . import (send, order, btcpay, issuance, broadcast, bet, dividend, burn, cancel, callback, rps, rpsresolve) @@ -911,7 +912,7 @@ def get_tx_info (tx, block_index): source_list = [] for vin in tx['vin']: # Loop through input transactions. if 'coinbase' in vin: return b'', None, None, None, None - vin_tx = bitcoin.get_raw_transaction(vin['txid']) # Get the full transaction data for this input transaction. + vin_tx = yield from bitcoin.get_raw_transaction(vin['txid']) # Get the full transaction data for this input transaction. vout = vin_tx['vout'][vin['vout']] fee += vout['value'] * config.UNIT @@ -967,12 +968,13 @@ def reparse (db, block_index=None, quiet=False): cursor.close() return +@asyncio.coroutine def list_tx (db, block_hash, block_index, block_time, tx_hash, tx_index): cursor = db.cursor() # Get the important details about each transaction. - tx = bitcoin.get_raw_transaction(tx_hash) - logging.debug('Status: Examining transaction {}.'.format(tx_hash)) - source, destination, btc_amount, fee, data = get_tx_info(tx, block_index) + tx = yield from bitcoin.get_raw_transaction(tx_hash) + logging.debug('Status: examining transaction {}.'.format(tx_hash)) + source, destination, btc_amount, fee, data = yield from get_tx_info(tx, block_index) if source and (data or destination == config.UNSPENDABLE): cursor.execute('''INSERT INTO transactions( tx_index, @@ -999,6 +1001,7 @@ def list_tx (db, block_hash, block_index, block_time, tx_hash, tx_index): cursor.close() return +@asyncio.coroutine def follow (db): # TODO: This is not thread-safe! cursor = db.cursor() @@ -1039,12 +1042,12 @@ def follow (db): # matter, with the block count decreasing. This should only delay # processing of the new blocks a bit. while True: - + starttime = time.time() # Get new blocks. - block_count = bitcoin.get_block_count() + block_count = yield from bitcoin.get_block_count() if block_index <= block_count: - logging.info('Block: {}'.format(str(block_index))) + #logging.info('Block: {}'.format(str(block_index))) # Backwards check for incorrect blocks due to chain reorganisation, and stop when a common parent is found. c = block_index @@ -1053,8 +1056,8 @@ def follow (db): if c == config.BLOCK_FIRST: break # Bitcoind parent hash. - c_hash = bitcoin.get_block_hash(c) - c_block = bitcoin.get_block(c_hash) + c_hash = yield from bitcoin.get_block_hash(c) + c_block = yield from bitcoin.get_block(c_hash) bitcoind_parent = c_block['previousblockhash'] # DB parent hash. @@ -1082,8 +1085,8 @@ def follow (db): continue # Get and parse transactions in this block (atomically). - block_hash = bitcoin.get_block_hash(block_index) - block = bitcoin.get_block(block_hash) + block_hash = yield from bitcoin.get_block_hash(block_index) + block = yield from bitcoin.get_block(block_hash) block_time = block['time'] tx_hash_list = block['tx'] with db: @@ -1099,7 +1102,7 @@ def follow (db): # List the transactions in the block. for tx_hash in tx_hash_list: - list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index) + yield from list_tx(db, block_hash, block_index, block_time, tx_hash, tx_index) tx_index += 1 # Parse the transactions in the block. @@ -1115,7 +1118,7 @@ def follow (db): del not_supported[tx_h] # Increment block index. - block_count = bitcoin.get_block_count() + block_count = yield from bitcoin.get_block_count() block_index +=1 else: @@ -1138,7 +1141,8 @@ def follow (db): # and then save those messages. # Every transaction in mempool is parsed independently. (DB is rolled back after each one.) mempool = [] - for tx_hash in bitcoin.get_mempool(): + bitcoinmempool = yield from bitcoin.get_mempool() + for tx_hash in bitcoinmempool: # If already in counterpartyd mempool, copy to new one. if tx_hash in old_mempool_hashes: @@ -1164,7 +1168,7 @@ def follow (db): # List transaction. try: # Sometimes the transactions can’t be found: `{'code': -5, 'message': 'No information available about transaction'} Is txindex enabled in Bitcoind?` - list_tx(db, config.MEMPOOL_BLOCK_HASH, config.MEMPOOL_BLOCK_INDEX, curr_time, tx_hash, mempool_tx_index) + yield from list_tx(db, config.MEMPOOL_BLOCK_HASH, config.MEMPOOL_BLOCK_INDEX, curr_time, tx_hash, mempool_tx_index) mempool_tx_index += 1 except exceptions.BitcoindError: assert False @@ -1211,6 +1215,8 @@ def follow (db): mempool_initialised = True time.sleep(2) + logging.info('Block: %s took %ss'%(str(block_index), time.time()-starttime)) + cursor.close() # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From eb1eb18118796540651f7566593b240e36705875 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Fri, 22 Aug 2014 11:27:12 -0400 Subject: [PATCH 03/17] yield from in all the right places --- test/test_.py | 76 +++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/test/test_.py b/test/test_.py index eda35b42c7..4f3096d33d 100644 --- a/test/test_.py +++ b/test/test_.py @@ -47,7 +47,7 @@ except: pass logging.basicConfig(filename=CURR_DIR + '/log.new', level=logging.DEBUG, format='%(message)s') requests_log = logging.getLogger("requests") -requests_log.setLevel(logging.WARNING) +requests_log.setLevel(logging.DEBUG) # Output. output_new = {} @@ -85,7 +85,7 @@ def parse_hex (unsigned_tx_hex): block_hash = hashlib.sha512(chr(block_index).encode('utf-8')).hexdigest() block_time = block_index * 10000000 - source, destination, btc_amount, fee, data = blocks.get_tx_info(tx, block_index) + source, destination, btc_amount, fee, data = yield from blocks.get_tx_info(tx, block_index) cursor.execute('''INSERT INTO blocks( block_index, @@ -231,28 +231,28 @@ def test_initialise (): def test_burn (): unsigned_tx_hex = bitcoin.transaction(burn.compose(db, source_default, int(.62 * quantity)), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send (): unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, config.XCP, small), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_buy_xcp (): unsigned_tx_hex = bitcoin.transaction(order.compose(db, source_default, config.BTC, small, config.XCP, small * 2, expiration, 0), encoding='multisig', fee_provided=fee_provided) - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_sell_xcp (): unsigned_tx_hex = bitcoin.transaction(order.compose(db, source_default, config.XCP, round(small * 2.1), config.BTC, small, expiration, fee_required), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex @@ -260,168 +260,168 @@ def test_btcpay (): order_match_id = 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986084fed08b978af4d7d196a7446a86b58009e636b611db16211b65a9aadff29c5' unsigned_tx_hex = bitcoin.transaction(btcpay.compose(db, source_default, order_match_id), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_divisible (): unsigned_tx_hex = bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBB', quantity * 10, True, False, 0, 0.0, ''), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_indivisible_callable (): unsigned_tx_hex = bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBC', round(quantity / 1000), False, True, 17, 0.015, 'foobar'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_divisible (): unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBB', round(quantity / 25)), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_indivisible (): unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', round(quantity / 190000)), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_divisible (): unsigned_tx_hex = bitcoin.transaction(dividend.compose(db, source_default, 600, 'BBBB', config.XCP), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_indivisible (): unsigned_tx_hex = bitcoin.transaction(dividend.compose(db, source_default, 800, 'BBBC', config.XCP), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_initial (): unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000000, 100, fee_multiplier_default, 'Unit Test'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_liquidated (): unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small, round(small / 2), 0.0, 15120, expiration), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_liquidated (): unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, round(small / 2), round(small * .83), 0.0, 15120, expiration), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_settled (): unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small * 3, small * 7, 0.0, 5040, expiration), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_settled (): unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, small * 7, small * 3, 0.0, 5040, expiration), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_equal (): unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 2, 1388000200, small * 15, small * 13, 1, 5040, expiration), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_notequal (): unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 3, 1388000200, small * 13, small * 15, 1, 5040, expiration), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_liquidate (): unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000050, round(100 - (.415/3) - .00001, 5), fee_multiplier_default, 'Unit Test'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_settle (): unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000101, 100.343, fee_multiplier_default, 'Unit Test'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_equal (): unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000201, 2, fee_multiplier_default, 'Unit Test'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_to_be_cancelled (): unsigned_tx_hex = bitcoin.transaction(order.compose(db, source_default, 'BBBB', small, config.XCP, small, expiration, 0), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_cancel (): unsigned_tx_hex = bitcoin.transaction(cancel.compose(db, source_default, '2f0fd1e89b8de1d57292742ec380ea47066e307ad645f5bc3adad8a06ff58608'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_overburn (): unsigned_tx_hex = bitcoin.transaction(burn.compose(db, source_default, (1 * config.UNIT), overburn=True), encoding='multisig') # Try to burn a whole 'nother BTC. - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_callable (): unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', 10000), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_callback (): unsigned_tx_hex = bitcoin.transaction(callback.compose(db, source_default, .3, 'BBBC'), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps (): unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 100), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_counter_rps (): unsigned_tx_hex = bitcoin.transaction(rps.compose(db, destination_default, 5, 11021663, '6e8bf66cbd6636aca1802459b730a99548624e48e243b840e0b34a12bede17ec', 100), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex @@ -429,7 +429,7 @@ def test_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' unsigned_tx_hex = bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex @@ -437,13 +437,13 @@ def test_counter_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' unsigned_tx_hex = bitcoin.transaction(rpsresolve.compose(db, destination_default, 5, 'fa765e80203cba24a298e4458f63ff6b', rps_match_id), encoding='multisig') - parse_hex(unsigned_tx_hex) + yield from parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps_expiration (): unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 10), encoding='multisig') - tx_rps = parse_hex(unsigned_tx_hex) + tx_rps = yield from parse_hex(unsigned_tx_hex) check_movment(db, 'debit', tx_rps['block_index'], source_default, 'XCP', 11021663, tx_rps['tx_hash']) block_progress(15) @@ -454,11 +454,11 @@ def test_rps_expiration (): def test_pending_rps_match_expiration (): unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig') - rps1 = parse_hex(unsigned_tx_hex) + rps1 = yield from parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021664, rps1['tx_hash']) unsigned_tx_hex = bitcoin.transaction(rps.compose(db, destination_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig') - rps2 = parse_hex(unsigned_tx_hex) + rps2 = yield from parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps2['block_index'], destination_default, 'XCP', 11021664, rps2['tx_hash']) block_progress(25) @@ -470,16 +470,16 @@ def test_pending_rps_match_expiration (): def test_pending_and_resolved_rps_match_expiration (): unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig') - rps1 = parse_hex(unsigned_tx_hex) + rps1 = yield from parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021665, rps1['tx_hash']) unsigned_tx_hex = bitcoin.transaction(rps.compose(db, destination_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig') - rps2 = parse_hex(unsigned_tx_hex) + rps2 = yield from parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps2['block_index'], destination_default, 'XCP', 11021665, rps2['tx_hash']) rps_match_id = rps1['tx_hash'] + rps2['tx_hash'] unsigned_tx_hex = bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig') - rps_match = parse_hex(unsigned_tx_hex) + rps_match = yield from parse_hex(unsigned_tx_hex) block_progress(25) expiration_block = rps_match['block_index']+20 From c415180fa2cc95c04f9b23175c30ac35a01a4be6 Mon Sep 17 00:00:00 2001 From: xnova Date: Sun, 24 Aug 2014 21:03:24 +0000 Subject: [PATCH 04/17] updated API docs --- docs/API.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/API.rst b/docs/API.rst index 18a32c30b7..51b991e69b 100644 --- a/docs/API.rst +++ b/docs/API.rst @@ -751,7 +751,11 @@ Issue a dividend on a specific user defined asset. create_issuance ^^^^^^^^^^^^^^^^^ **create_issuance(source, asset, quantity, divisible, description, callable_=false, call_date=null, call_price=null, +<<<<<<< HEAD transfer_destination=null, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** +======= +transfer_destination=null, lock=false, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** +>>>>>>> updated API docs Issue a new asset, issue more of an existing asset, lock an asset, or transfer the ownership of an asset (note that you can only do one of these operations in a given create_issuance call). From 6cae8a5e2cefa951a125d51b7335b6237394783b Mon Sep 17 00:00:00 2001 From: xnova Date: Sun, 24 Aug 2014 21:53:38 +0000 Subject: [PATCH 05/17] more doc tweaks with lock param for create_issuance --- docs/API.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/API.rst b/docs/API.rst index 51b991e69b..7b7b11ba98 100644 --- a/docs/API.rst +++ b/docs/API.rst @@ -752,10 +752,14 @@ create_issuance ^^^^^^^^^^^^^^^^^ **create_issuance(source, asset, quantity, divisible, description, callable_=false, call_date=null, call_price=null, <<<<<<< HEAD +<<<<<<< HEAD transfer_destination=null, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** ======= transfer_destination=null, lock=false, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** >>>>>>> updated API docs +======= +transfer_destination=null, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** +>>>>>>> more doc tweaks with lock param for create_issuance Issue a new asset, issue more of an existing asset, lock an asset, or transfer the ownership of an asset (note that you can only do one of these operations in a given create_issuance call). From de1fe521ae8522075c2ea630d876ced563822f8c Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Mon, 25 Aug 2014 15:45:41 -0400 Subject: [PATCH 06/17] Missed a couple rpc calls and wip fix --- lib/bitcoin.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index 34ececed68..da2e4967aa 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -385,10 +385,10 @@ def sort_unspent_txouts(unspent, allow_unconfirmed_inputs): return unspent def private_key_to_public_key (private_key_wif): - allowable_wif_prefixes = [b'x80', b'\xef'] # Bitcoin mainnet, testnet3 + allowable_wif_prefixes = [ b'\x80', b'\xef' ] try: secret_exponent, compressed = wif_to_tuple_of_secret_exponent_compressed( - private_key_wif, allowable_wif_prefixes) + private_key_wif, allowable_wif_prefixes=allowable_wif_prefixes) except EncodingError: raise exceptions.AltcoinSupportError('pycoin: unsupported WIF prefix') public_pair = public_pair_for_secret_exponent(generator_secp256k1, secret_exponent) @@ -575,7 +575,7 @@ def sign_tx (unsigned_tx_hex, private_key_wif=None): raise exceptions.TransactionError('Could not sign transaction with pybtctool.') else: # Assume source is in wallet and wallet is unlocked. - result = rpc('signrawtransaction', [unsigned_tx_hex]) + result = yield from rpc('signrawtransaction', [unsigned_tx_hex]) if result['complete']: signed_tx_hex = result['hex'] else: @@ -616,9 +616,9 @@ def get_unspent_txouts(address, normalize=False): with open(CURR_DIR + '/../test/listunspent.test.json', 'r') as listunspent_test_file: # HACK wallet_unspent = json.load(listunspent_test_file) return [output for output in wallet_unspent if output['address'] == address] - - if rpc('validateaddress', [address])['ismine']: - wallet_unspent = rpc('listunspent', [0, 999999]) + rpcv = yield from rpc('validateaddress', [address]) + if rpcv['ismine']: + wallet_unspent = yield from rpc('listunspent', [0, 999999]) return [output for output in wallet_unspent if output['address'] == address] else: return blockchain.listunspent(address) From e5e58786400bf8b68efcd73eee4a015ca9af3ef8 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Mon, 25 Aug 2014 15:46:02 -0400 Subject: [PATCH 07/17] aiohttp instead of requests, with aiorun() to emulate synchronicity --- lib/api.py | 32 +++++++++++++++++++------------- lib/util.py | 33 +++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/lib/api.py b/lib/api.py index 361bd93c62..7d891f0e72 100644 --- a/lib/api.py +++ b/lib/api.py @@ -7,7 +7,7 @@ import time import json import re -import requests +import asyncio, aiohttp import collections import logging from logging import handlers as logging_handlers @@ -202,7 +202,7 @@ def compose_transaction(db, name, params, fee=None, fee_provided=0): tx_info = sys.modules['lib.{}'.format(name)].compose(db, **params) - return bitcoin.transaction(tx_info, encoding=encoding, + return util.aiorun(bitcoin.transaction(tx_info, encoding=encoding, fee_per_kb=fee_per_kb, regular_dust_size=regular_dust_size, multisig_dust_size=multisig_dust_size, @@ -210,24 +210,26 @@ def compose_transaction(db, name, params, public_key_hex=pubkey, allow_unconfirmed_inputs=allow_unconfirmed_inputs, exact_fee=fee, - fee_provided=fee_provided) + fee_provided=fee_provided)) def sign_transaction(unsigned_tx_hex, private_key_wif=None): - return bitcoin.sign_tx(unsigned_tx_hex, private_key_wif=private_key_wif) + return util.aiorun(bitcoin.sign_tx(unsigned_tx_hex, + private_key_wif=private_key_wif)) def broadcast_transaction(signed_tx_hex): if not config.TESTNET and config.BROADCAST_TX_MAINNET in ['bci', 'bci-failover']: url = "https://blockchain.info/pushtx" params = {'tx': signed_tx_hex} - response = requests.post(url, data=params) - if response.text.lower() != 'transaction submitted' or response.status_code != 200: + response = util.aiorun(aiohttp.request('POST', url, data=params)) + data = util.aiorun(response.read()) + if data.lower() != 'transaction submitted' or response.status != 200: if config.BROADCAST_TX_MAINNET == 'bci-failover': - return bitcoin.broadcast_tx(signed_tx_hex) + return util.aiorun(bitcoin.broadcast_tx(signed_tx_hex)) else: raise Exception(response.text) - return response.text + return data else: - return bitcoin.broadcast_tx(signed_tx_hex) + return util.aiorun(bitcoin.broadcast_tx(signed_tx_hex)) def do_transaction(db, name, params, private_key_wif=None, **kwargs): unsigned_tx = compose_transaction(db, name, params, **kwargs) @@ -244,6 +246,8 @@ def __init__(self): def run(self): global current_api_status_code, current_api_status_response_json db = util.connect_to_db(flags='SQLITE_OPEN_READONLY') + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) while True: try: @@ -256,9 +260,9 @@ def run(self): # Check that the database has caught up with bitcoind. if time.time() - self.last_database_check > 10 * 60: # Ten minutes since last check. code = 11 - bitcoin.bitcoind_check(db) + util.aiorun(bitcoin.bitcoind_check(db)) code = 12 - util.database_check(db, bitcoin.get_block_count()) # TODO: If not reparse or rollback, once those use API. + util.database_check(db, util.aiorun(bitcoin.get_block_count())) # TODO: If not reparse or rollback, once those use API. self.last_database_check = time.time() except Exception as e: exception_name = e.__class__.__name__ @@ -279,6 +283,8 @@ def run(self): db = util.connect_to_db(flags='SQLITE_OPEN_READONLY') app = flask.Flask(__name__) auth = HTTPBasicAuth() + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) @auth.get_password def get_pw(username): @@ -393,7 +399,7 @@ def get_asset_info(assets): # BTC and XCP. if asset in [config.BTC, config.XCP]: if asset == config.BTC: - supply = bitcoin.get_btc_supply(normalize=False) + supply = util.aiorun(bitcoin.get_btc_supply(normalize=False)) else: supply = util.xcp_supply(db) @@ -480,7 +486,7 @@ def get_blocks(block_indexes): @dispatcher.add_method def get_running_info(): - latestBlockIndex = bitcoin.get_block_count() + latestBlockIndex = util.aiorun(bitcoin.get_block_count()) try: util.database_check(db, latestBlockIndex) diff --git a/lib/util.py b/lib/util.py index afe0304dfc..790da45053 100644 --- a/lib/util.py +++ b/lib/util.py @@ -6,7 +6,7 @@ import apsw import collections import inspect -import requests +import asyncio, aiohttp from datetime import datetime from dateutil.tz import tzlocal from operator import itemgetter @@ -22,6 +22,11 @@ BET_TYPE_NAME = {0: 'BullCFD', 1: 'BearCFD', 2: 'Equal', 3: 'NotEqual'} BET_TYPE_ID = {'BullCFD': 0, 'BearCFD': 1, 'Equal': 2, 'NotEqual': 3} +def aiorun(x, timeout=5): + """ Run an asynchronous generator as though it was synchronous. (i.e. + wait for it to complete), with an optional timeout """ + return asyncio.get_event_loop().run_until_complete( + asyncio.wait_for(x, timeout=timeout)) # TODO: This doesn’t timeout properly. (If server hangs, then unhangs, no result.) def api (method, params): @@ -32,16 +37,17 @@ def api (method, params): "jsonrpc": "2.0", "id": 0, } - response = requests.post(config.RPC, data=json.dumps(payload), headers=headers) + response = aiorun(aiohttp.request('POST', config.RPC, + data=json.dumps(payload), headers=headers)) + response_json = aiorun(response.json()) if response == None: raise exceptions.RPCError('Cannot communicate with {} server.'.format(config.XCP_CLIENT)) - elif response.status_code != 200: - if response.status_code == 500: + elif response.status != 200: + if response.status == 500: raise exceptions.RPCError('Malformed API call.') else: - raise exceptions.RPCError(str(response.status_code) + ' ' + response.reason) + raise exceptions.RPCError(str(response.status) + ' ' + response.reason) - response_json = response.json() if 'error' not in response_json.keys() or response_json['error'] == None: try: return response_json['result'] @@ -353,8 +359,9 @@ def connect_to_db(flags=None): def version_check (db): try: host = 'https://raw2.github.com/CounterpartyXCP/counterpartyd/master/version.json' - response = requests.get(host, headers={'cache-control': 'no-cache'}) - versions = json.loads(response.text) + response = aiorun(aiohttp.request('GET', host, headers={'cache-control': + 'no-cache'})) + versions = aiorun(response.json()) except Exception as e: raise exceptions.VersionError('Unable to check version. How’s your Internet access?') @@ -710,13 +717,15 @@ def supplies (db): def get_url(url, abort_on_error=False, is_json=True, fetch_timeout=5): try: - r = requests.get(url, timeout=fetch_timeout) + r = aiorun(aiohttp.request('GET', url), timeout=fetch_timeout) + if is_json: result = aiorun(r.json()) + else: result = aiorun(r.read()) except Exception as e: raise Exception("Got get_url request error: %s" % e) else: - if r.status_code != 200 and abort_on_error: - raise Exception("Bad status code returned: '%s'. result body: '%s'." % (r.status_code, r.text)) - result = json.loads(r.text) if is_json else r.text + if r.status != 200 and abort_on_error: + raise Exception("Bad status code returned: '%s'. result body: '%s'."%( + r.status, result)) return result # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From f328aa6de68b705b5ce67778c7b9cb891ac21858 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Mon, 25 Aug 2014 16:01:50 -0400 Subject: [PATCH 08/17] Update requirements requests->aiohttp, python 3.4 --- README.md | 4 ++-- pip-requirements.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7573d2e451..45f7eb437f 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ The Counterparty protocol specification may be found at . # Dependencies -* [Python 3](http://python.org) -* Python 3 packages: apsw, requests, appdirs, prettytable, python-dateutil, json-rpc, tornado, flask, Flask-HTTPAuth, pycoin, pyzmq(v2.2+), pycrypto (see [this link](https://github.com/CounterpartyXCP/counterpartyd/blob/master/pip-requirements.txt) for exact working versions) +* [Python 3.4](http://python.org) +* Python 3 packages: apsw, aiohttp, appdirs, prettytable, python-dateutil, json-rpc, tornado, flask, Flask-HTTPAuth, pycoin, pyzmq(v2.2+), pycrypto (see [this link](https://github.com/CounterpartyXCP/counterpartyd/blob/master/pip-requirements.txt) for exact working versions) * Bitcoind # Installation diff --git a/pip-requirements.txt b/pip-requirements.txt index ebe04b2bfa..4383dacad4 100644 --- a/pip-requirements.txt +++ b/pip-requirements.txt @@ -18,7 +18,7 @@ pytest==2.5.1 pycoin==0.26 -requests==2.3.0 +aiohttp>=0.9.0 Flask-HTTPAuth==2.2.1 From c8afa25ce02cf689a3aca63ad4e32b885c310b6c Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Mon, 25 Aug 2014 16:44:42 -0400 Subject: [PATCH 09/17] Yield from functions in bitcoin.py --- lib/bitcoin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index da2e4967aa..f966e0c96a 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -511,7 +511,7 @@ def chunks(l, n): outputs_size = ((25 + 9) * len(destination_outputs)) + (len(data_array) * data_output_size) # Get inputs. - unspent = get_unspent_txouts(source, normalize=True) + unspent = yield from get_unspent_txouts(source, normalize=True) unspent = sort_unspent_txouts(unspent, allow_unconfirmed_inputs) logging.debug('Sorted UTXOs: {}'.format([print_coin(coin) for coin in unspent])) From 89371c0681b61372a33ee0cab59ad50e537dbf92 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Mon, 25 Aug 2014 16:47:42 -0400 Subject: [PATCH 10/17] Tests pass (excluding test_log and test_json_rpc) --- test/test_.py | 223 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 143 insertions(+), 80 deletions(-) diff --git a/test/test_.py b/test/test_.py index 4f3096d33d..e9f0f205b1 100644 --- a/test/test_.py +++ b/test/test_.py @@ -11,8 +11,7 @@ import difflib import json import inspect -import requests -from requests.auth import HTTPBasicAuth +import asyncio, aiohttp import logging import tempfile import shutil @@ -76,7 +75,7 @@ def parse_hex (unsigned_tx_hex): - tx = bitcoin.decode_raw_transaction(unsigned_tx_hex) + tx = util.aiorun(bitcoin.decode_raw_transaction(unsigned_tx_hex)) cursor = db.cursor() tx_hash = hashlib.sha256(chr(tx_index).encode('utf-8')).hexdigest() @@ -85,7 +84,8 @@ def parse_hex (unsigned_tx_hex): block_hash = hashlib.sha512(chr(block_index).encode('utf-8')).hexdigest() block_time = block_index * 10000000 - source, destination, btc_amount, fee, data = yield from blocks.get_tx_info(tx, block_index) + source, destination, btc_amount, fee, data = util.aiorun(blocks.get_tx_info(tx, + block_index)) cursor.execute('''INSERT INTO blocks( block_index, @@ -198,7 +198,6 @@ def compare(filename): def summarise (ebit): return (ebit['block_index'], ebit['address'], ebit['asset'], ebit['quantity']) - def setup_function(function): global db global cursor @@ -207,7 +206,6 @@ def setup_function(function): def teardown_function(function): cursor.execute('''END''') - # TODO: replace inspect.stack()[0][3] with inspect.currentframe().f_code.co_name? def test_start (): @@ -229,221 +227,276 @@ def test_initialise (): cursor.close() def test_burn (): - unsigned_tx_hex = bitcoin.transaction(burn.compose(db, source_default, int(.62 * quantity)), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(burn.compose(db, + source_default, int(.62 * quantity)), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send (): - unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, config.XCP, small), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + source_default, destination_default, config.XCP, small), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_buy_xcp (): - unsigned_tx_hex = bitcoin.transaction(order.compose(db, source_default, config.BTC, small, config.XCP, small * 2, expiration, 0), encoding='multisig', fee_provided=fee_provided) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(order.compose(db, + source_default, config.BTC, small, config.XCP, small * 2, expiration, + 0), encoding='multisig', fee_provided=fee_provided)) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_sell_xcp (): - unsigned_tx_hex = bitcoin.transaction(order.compose(db, source_default, config.XCP, round(small * 2.1), config.BTC, small, expiration, fee_required), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(order.compose(db, + source_default, config.XCP, round(small * 2.1), config.BTC, small, + expiration, fee_required), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_btcpay (): order_match_id = 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986084fed08b978af4d7d196a7446a86b58009e636b611db16211b65a9aadff29c5' - unsigned_tx_hex = bitcoin.transaction(btcpay.compose(db, source_default, order_match_id), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(btcpay.compose(db, + source_default, order_match_id), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_divisible (): - unsigned_tx_hex = bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBB', quantity * 10, True, False, 0, 0.0, ''), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(issuance.compose(db, + source_default, None, 'BBBB', quantity * 10, True, False, 0, 0.0, ''), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_indivisible_callable (): - unsigned_tx_hex = bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBC', round(quantity / 1000), False, True, 17, 0.015, 'foobar'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(issuance.compose(db, + source_default, None, 'BBBC', round(quantity / 1000), False, True, 17, + 0.015, 'foobar'), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_divisible (): - unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBB', round(quantity / 25)), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + source_default, destination_default, 'BBBB', round(quantity / 25)), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_indivisible (): - unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', round(quantity / 190000)), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + source_default, destination_default, 'BBBC', round(quantity / 190000)), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_divisible (): - unsigned_tx_hex = bitcoin.transaction(dividend.compose(db, source_default, 600, 'BBBB', config.XCP), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(dividend.compose(db, + source_default, 600, 'BBBB', config.XCP), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_indivisible (): - unsigned_tx_hex = bitcoin.transaction(dividend.compose(db, source_default, 800, 'BBBC', config.XCP), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(dividend.compose(db, + source_default, 800, 'BBBC', config.XCP), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_initial (): - unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000000, 100, fee_multiplier_default, 'Unit Test'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + source_default, 1388000000, 100, fee_multiplier_default, 'Unit Test'), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_liquidated (): - unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small, round(small / 2), 0.0, 15120, expiration), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + source_default, 0, 1388000100, small, round(small / 2), 0.0, 15120, + expiration), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_liquidated (): - unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, round(small / 2), round(small * .83), 0.0, 15120, expiration), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + source_default, 1, 1388000100, round(small / 2), round(small * .83), + 0.0, 15120, expiration), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_settled (): - unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small * 3, small * 7, 0.0, 5040, expiration), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + source_default, 0, 1388000100, small * 3, small * 7, 0.0, 5040, + expiration), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_settled (): - unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, small * 7, small * 3, 0.0, 5040, expiration), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + source_default, 1, 1388000100, small * 7, small * 3, 0.0, 5040, + expiration), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_equal (): - unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 2, 1388000200, small * 15, small * 13, 1, 5040, expiration), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + source_default, 2, 1388000200, small * 15, small * 13, 1, 5040, + expiration), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_notequal (): - unsigned_tx_hex = bitcoin.transaction(bet.compose(db, source_default, source_default, 3, 1388000200, small * 13, small * 15, 1, 5040, expiration), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + source_default, 3, 1388000200, small * 13, small * 15, 1, 5040, + expiration), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_liquidate (): - unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000050, round(100 - (.415/3) - .00001, 5), fee_multiplier_default, 'Unit Test'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + source_default, 1388000050, round(100 - (.415/3) - .00001, 5), + fee_multiplier_default, 'Unit Test'), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_settle (): - unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000101, 100.343, fee_multiplier_default, 'Unit Test'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + source_default, 1388000101, 100.343, fee_multiplier_default, 'Unit Test'), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_equal (): - unsigned_tx_hex = bitcoin.transaction(broadcast.compose(db, source_default, 1388000201, 2, fee_multiplier_default, 'Unit Test'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + source_default, 1388000201, 2, fee_multiplier_default, 'Unit Test'), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_to_be_cancelled (): - unsigned_tx_hex = bitcoin.transaction(order.compose(db, source_default, 'BBBB', small, config.XCP, small, expiration, 0), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(order.compose(db, + source_default, 'BBBB', small, config.XCP, small, expiration, 0), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_cancel (): - unsigned_tx_hex = bitcoin.transaction(cancel.compose(db, source_default, '2f0fd1e89b8de1d57292742ec380ea47066e307ad645f5bc3adad8a06ff58608'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(cancel.compose(db, + source_default, + '2f0fd1e89b8de1d57292742ec380ea47066e307ad645f5bc3adad8a06ff58608'), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_overburn (): - unsigned_tx_hex = bitcoin.transaction(burn.compose(db, source_default, (1 * config.UNIT), overburn=True), encoding='multisig') # Try to burn a whole 'nother BTC. + unsigned_tx_hex = util.aiorun(bitcoin.transaction(burn.compose(db, + source_default, (1 * config.UNIT), overburn=True), encoding='multisig')) # Try to burn a whole 'nother BTC. - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_callable (): - unsigned_tx_hex = bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', 10000), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + source_default, destination_default, 'BBBC', 10000), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_callback (): - unsigned_tx_hex = bitcoin.transaction(callback.compose(db, source_default, .3, 'BBBC'), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(callback.compose(db, + source_default, .3, 'BBBC'), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps (): - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 100), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + 5, 11021663, move_random_hash_default, 100), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_counter_rps (): - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, destination_default, 5, 11021663, '6e8bf66cbd6636aca1802459b730a99548624e48e243b840e0b34a12bede17ec', 100), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, + destination_default, 5, 11021663, + '6e8bf66cbd6636aca1802459b730a99548624e48e243b840e0b34a12bede17ec', + 100), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' - unsigned_tx_hex = bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rpsresolve.compose(db, + source_default, 3, rps_random_default, rps_match_id), + encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_counter_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' - unsigned_tx_hex = bitcoin.transaction(rpsresolve.compose(db, destination_default, 5, 'fa765e80203cba24a298e4458f63ff6b', rps_match_id), encoding='multisig') + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rpsresolve.compose(db, + destination_default, 5, 'fa765e80203cba24a298e4458f63ff6b', + rps_match_id), encoding='multisig')) - yield from parse_hex(unsigned_tx_hex) + parse_hex(unsigned_tx_hex) output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps_expiration (): - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 10), encoding='multisig') - tx_rps = yield from parse_hex(unsigned_tx_hex) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + 5, 11021663, move_random_hash_default, 10), encoding='multisig')) + tx_rps = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', tx_rps['block_index'], source_default, 'XCP', 11021663, tx_rps['tx_hash']) block_progress(15) @@ -453,12 +506,15 @@ def test_rps_expiration (): check_movment(db, 'credit', expiration_block, source_default, 'XCP', 11021663, tx_rps['tx_hash']) def test_pending_rps_match_expiration (): - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig') - rps1 = yield from parse_hex(unsigned_tx_hex) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + 5, 11021664, move_random_hash_default, 10), encoding='multisig')) + rps1 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021664, rps1['tx_hash']) - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, destination_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig') - rps2 = yield from parse_hex(unsigned_tx_hex) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, + destination_default, 5, 11021664, move_random_hash_default, 10), + encoding='multisig')) + rps2 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps2['block_index'], destination_default, 'XCP', 11021664, rps2['tx_hash']) block_progress(25) @@ -469,17 +525,22 @@ def test_pending_rps_match_expiration (): check_movment(db, 'credit', expiration_block, destination_default, 'XCP', 11021664, rps1['tx_hash'] + rps2['tx_hash']) def test_pending_and_resolved_rps_match_expiration (): - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, source_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig') - rps1 = yield from parse_hex(unsigned_tx_hex) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + 5, 11021665, move_random_hash_default, 10), encoding='multisig')) + rps1 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021665, rps1['tx_hash']) - unsigned_tx_hex = bitcoin.transaction(rps.compose(db, destination_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig') - rps2 = yield from parse_hex(unsigned_tx_hex) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, + destination_default, 5, 11021665, move_random_hash_default, 10), + encoding='multisig')) + rps2 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps2['block_index'], destination_default, 'XCP', 11021665, rps2['tx_hash']) rps_match_id = rps1['tx_hash'] + rps2['tx_hash'] - unsigned_tx_hex = bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig') - rps_match = yield from parse_hex(unsigned_tx_hex) + unsigned_tx_hex = util.aiorun(bitcoin.transaction(rpsresolve.compose(db, + source_default, 3, rps_random_default, rps_match_id), + encoding='multisig')) + rps_match = parse_hex(unsigned_tx_hex) block_progress(25) expiration_block = rps_match['block_index']+20 @@ -517,7 +578,9 @@ def test_json_rpc(): for payload in payloads: for attempt in range(100): # Try until server is ready. try: - response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False).json() + response = util.aiorun(aiohttp.request('POST', url, + data=json.dumps(payload), headers=headers)) + response = util.aiorun(response.json()) print(response) # print('\npayload', payload) # print('response', response, '\n') @@ -531,7 +594,7 @@ def test_json_rpc(): except KeyError as e: print(response, file=sys.stderr) exit(1) - except requests.exceptions.ConnectionError: + except aiohttp.errors.ConnectionError: time.sleep(.05) if attempt == 99: exit(1) # Fail From 015410af9e4ef16c0d17f41e2ed41880e7166d7f Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Tue, 26 Aug 2014 09:19:18 -0400 Subject: [PATCH 11/17] Use aiorun() --- counterpartyd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/counterpartyd.py b/counterpartyd.py index 73233efaa1..ccd63ea70c 100755 --- a/counterpartyd.py +++ b/counterpartyd.py @@ -97,7 +97,7 @@ def market (give_asset, get_asset): # Your Pending Orders Matches. addresses = [] - for bunch in loop.run_until_complete(bitcoin.get_wallet()): + for bunch in util.aiorun(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), @@ -1030,7 +1030,7 @@ def generate_move_random_hash(move): totals = {} print() - for bunch in loop.run_until_complete(bitcoin.get_wallet()): + for bunch in util.aiorun(bitcoin.get_wallet()): address, btc_balance = bunch[:2] address_data = get_address(db, address=address) balances = address_data['balances'] @@ -1065,7 +1065,7 @@ def generate_move_random_hash(move): elif args.action == 'pending': addresses = [] - for bunch in loop.run_until_complete(bitcoin.get_wallet()): + for bunch in util.aiorun(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), From c7483fe974b367a90c550c320cdd4051a1c66eed Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Tue, 26 Aug 2014 09:19:28 -0400 Subject: [PATCH 12/17] Print block timing information --- lib/blocks.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/blocks.py b/lib/blocks.py index 10a7f53afb..48e3d22712 100644 --- a/lib/blocks.py +++ b/lib/blocks.py @@ -1047,8 +1047,6 @@ def follow (db): block_count = yield from bitcoin.get_block_count() if block_index <= block_count: - #logging.info('Block: {}'.format(str(block_index))) - # Backwards check for incorrect blocks due to chain reorganisation, and stop when a common parent is found. c = block_index requires_rollback = False @@ -1117,6 +1115,7 @@ def follow (db): (i, tx_h) = not_supported_sorted.popleft() del not_supported[tx_h] + logging.info('Block: %s took %ss'%(str(block_index), time.time()-starttime)) # Increment block index. block_count = yield from bitcoin.get_block_count() block_index +=1 @@ -1215,7 +1214,6 @@ def follow (db): mempool_initialised = True time.sleep(2) - logging.info('Block: %s took %ss'%(str(block_index), time.time()-starttime)) cursor.close() From d3507a9e0fa7ab77840a75cf0d8aa1d31fd049cf Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Tue, 26 Aug 2014 12:42:36 -0400 Subject: [PATCH 13/17] Fix merge conflict --- docs/API.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/docs/API.rst b/docs/API.rst index 7b7b11ba98..18a32c30b7 100644 --- a/docs/API.rst +++ b/docs/API.rst @@ -751,15 +751,7 @@ Issue a dividend on a specific user defined asset. create_issuance ^^^^^^^^^^^^^^^^^ **create_issuance(source, asset, quantity, divisible, description, callable_=false, call_date=null, call_price=null, -<<<<<<< HEAD -<<<<<<< HEAD transfer_destination=null, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** -======= -transfer_destination=null, lock=false, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** ->>>>>>> updated API docs -======= -transfer_destination=null, encoding='multisig', pubkey=null, allow_unconfirmed_inputs=false, fee=null, fee_per_kb=10000)** ->>>>>>> more doc tweaks with lock param for create_issuance Issue a new asset, issue more of an existing asset, lock an asset, or transfer the ownership of an asset (note that you can only do one of these operations in a given create_issuance call). From 22171028cff710b5c8c5de898b6fc351e03e8bdd Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Fri, 29 Aug 2014 09:55:50 -0400 Subject: [PATCH 14/17] Rename aiorun to aio_wait_for --- counterpartyd.py | 6 ++-- lib/api.py | 20 ++++++------ lib/util.py | 16 +++++----- test/test_.py | 80 ++++++++++++++++++++++++------------------------ 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/counterpartyd.py b/counterpartyd.py index ccd63ea70c..e7320d9347 100755 --- a/counterpartyd.py +++ b/counterpartyd.py @@ -97,7 +97,7 @@ def market (give_asset, get_asset): # Your Pending Orders Matches. addresses = [] - for bunch in util.aiorun(bitcoin.get_wallet()): + for bunch in util.aio_wait_for(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), @@ -1030,7 +1030,7 @@ def generate_move_random_hash(move): totals = {} print() - for bunch in util.aiorun(bitcoin.get_wallet()): + for bunch in util.aio_wait_for(bitcoin.get_wallet()): address, btc_balance = bunch[:2] address_data = get_address(db, address=address) balances = address_data['balances'] @@ -1065,7 +1065,7 @@ def generate_move_random_hash(move): elif args.action == 'pending': addresses = [] - for bunch in util.aiorun(bitcoin.get_wallet()): + for bunch in util.aio_wait_for(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), diff --git a/lib/api.py b/lib/api.py index 7d891f0e72..9935e4dfa3 100644 --- a/lib/api.py +++ b/lib/api.py @@ -202,7 +202,7 @@ def compose_transaction(db, name, params, fee=None, fee_provided=0): tx_info = sys.modules['lib.{}'.format(name)].compose(db, **params) - return util.aiorun(bitcoin.transaction(tx_info, encoding=encoding, + return util.aio_wait_for(bitcoin.transaction(tx_info, encoding=encoding, fee_per_kb=fee_per_kb, regular_dust_size=regular_dust_size, multisig_dust_size=multisig_dust_size, @@ -213,23 +213,23 @@ def compose_transaction(db, name, params, fee_provided=fee_provided)) def sign_transaction(unsigned_tx_hex, private_key_wif=None): - return util.aiorun(bitcoin.sign_tx(unsigned_tx_hex, + return util.aio_wait_for(bitcoin.sign_tx(unsigned_tx_hex, private_key_wif=private_key_wif)) def broadcast_transaction(signed_tx_hex): if not config.TESTNET and config.BROADCAST_TX_MAINNET in ['bci', 'bci-failover']: url = "https://blockchain.info/pushtx" params = {'tx': signed_tx_hex} - response = util.aiorun(aiohttp.request('POST', url, data=params)) - data = util.aiorun(response.read()) + response = util.aio_wait_for(aiohttp.request('POST', url, data=params)) + data = util.aio_wait_for(response.read()) if data.lower() != 'transaction submitted' or response.status != 200: if config.BROADCAST_TX_MAINNET == 'bci-failover': - return util.aiorun(bitcoin.broadcast_tx(signed_tx_hex)) + return util.aio_wait_for(bitcoin.broadcast_tx(signed_tx_hex)) else: raise Exception(response.text) return data else: - return util.aiorun(bitcoin.broadcast_tx(signed_tx_hex)) + return util.aio_wait_for(bitcoin.broadcast_tx(signed_tx_hex)) def do_transaction(db, name, params, private_key_wif=None, **kwargs): unsigned_tx = compose_transaction(db, name, params, **kwargs) @@ -260,9 +260,9 @@ def run(self): # Check that the database has caught up with bitcoind. if time.time() - self.last_database_check > 10 * 60: # Ten minutes since last check. code = 11 - util.aiorun(bitcoin.bitcoind_check(db)) + util.aio_wait_for(bitcoin.bitcoind_check(db)) code = 12 - util.database_check(db, util.aiorun(bitcoin.get_block_count())) # TODO: If not reparse or rollback, once those use API. + util.database_check(db, util.aio_wait_for(bitcoin.get_block_count())) # TODO: If not reparse or rollback, once those use API. self.last_database_check = time.time() except Exception as e: exception_name = e.__class__.__name__ @@ -399,7 +399,7 @@ def get_asset_info(assets): # BTC and XCP. if asset in [config.BTC, config.XCP]: if asset == config.BTC: - supply = util.aiorun(bitcoin.get_btc_supply(normalize=False)) + supply = util.aio_wait_for(bitcoin.get_btc_supply(normalize=False)) else: supply = util.xcp_supply(db) @@ -486,7 +486,7 @@ def get_blocks(block_indexes): @dispatcher.add_method def get_running_info(): - latestBlockIndex = util.aiorun(bitcoin.get_block_count()) + latestBlockIndex = util.aio_wait_for(bitcoin.get_block_count()) try: util.database_check(db, latestBlockIndex) diff --git a/lib/util.py b/lib/util.py index 790da45053..e8343537e0 100644 --- a/lib/util.py +++ b/lib/util.py @@ -22,7 +22,7 @@ BET_TYPE_NAME = {0: 'BullCFD', 1: 'BearCFD', 2: 'Equal', 3: 'NotEqual'} BET_TYPE_ID = {'BullCFD': 0, 'BearCFD': 1, 'Equal': 2, 'NotEqual': 3} -def aiorun(x, timeout=5): +def aio_wait_for(x, timeout=5): """ Run an asynchronous generator as though it was synchronous. (i.e. wait for it to complete), with an optional timeout """ return asyncio.get_event_loop().run_until_complete( @@ -37,9 +37,9 @@ def api (method, params): "jsonrpc": "2.0", "id": 0, } - response = aiorun(aiohttp.request('POST', config.RPC, + response = aio_wait_for(aiohttp.request('POST', config.RPC, data=json.dumps(payload), headers=headers)) - response_json = aiorun(response.json()) + response_json = aio_wait_for(response.json()) if response == None: raise exceptions.RPCError('Cannot communicate with {} server.'.format(config.XCP_CLIENT)) elif response.status != 200: @@ -359,9 +359,9 @@ def connect_to_db(flags=None): def version_check (db): try: host = 'https://raw2.github.com/CounterpartyXCP/counterpartyd/master/version.json' - response = aiorun(aiohttp.request('GET', host, headers={'cache-control': + response = aio_wait_for(aiohttp.request('GET', host, headers={'cache-control': 'no-cache'})) - versions = aiorun(response.json()) + versions = aio_wait_for(response.json()) except Exception as e: raise exceptions.VersionError('Unable to check version. How’s your Internet access?') @@ -717,9 +717,9 @@ def supplies (db): def get_url(url, abort_on_error=False, is_json=True, fetch_timeout=5): try: - r = aiorun(aiohttp.request('GET', url), timeout=fetch_timeout) - if is_json: result = aiorun(r.json()) - else: result = aiorun(r.read()) + r = aio_wait_for(aiohttp.request('GET', url), timeout=fetch_timeout) + if is_json: result = aio_wait_for(r.json()) + else: result = aio_wait_for(r.read()) except Exception as e: raise Exception("Got get_url request error: %s" % e) else: diff --git a/test/test_.py b/test/test_.py index e9f0f205b1..7fa655bbd9 100644 --- a/test/test_.py +++ b/test/test_.py @@ -75,7 +75,7 @@ def parse_hex (unsigned_tx_hex): - tx = util.aiorun(bitcoin.decode_raw_transaction(unsigned_tx_hex)) + tx = util.aio_wait_for(bitcoin.decode_raw_transaction(unsigned_tx_hex)) cursor = db.cursor() tx_hash = hashlib.sha256(chr(tx_index).encode('utf-8')).hexdigest() @@ -84,7 +84,7 @@ def parse_hex (unsigned_tx_hex): block_hash = hashlib.sha512(chr(block_index).encode('utf-8')).hexdigest() block_time = block_index * 10000000 - source, destination, btc_amount, fee, data = util.aiorun(blocks.get_tx_info(tx, + source, destination, btc_amount, fee, data = util.aio_wait_for(blocks.get_tx_info(tx, block_index)) cursor.execute('''INSERT INTO blocks( @@ -227,7 +227,7 @@ def test_initialise (): cursor.close() def test_burn (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(burn.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(burn.compose(db, source_default, int(.62 * quantity)), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -235,7 +235,7 @@ def test_burn (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, source_default, destination_default, config.XCP, small), encoding='multisig')) @@ -244,7 +244,7 @@ def test_send (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_buy_xcp (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(order.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(order.compose(db, source_default, config.BTC, small, config.XCP, small * 2, expiration, 0), encoding='multisig', fee_provided=fee_provided)) @@ -253,7 +253,7 @@ def test_order_buy_xcp (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_sell_xcp (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(order.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(order.compose(db, source_default, config.XCP, round(small * 2.1), config.BTC, small, expiration, fee_required), encoding='multisig')) @@ -263,7 +263,7 @@ def test_order_sell_xcp (): def test_btcpay (): order_match_id = 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986084fed08b978af4d7d196a7446a86b58009e636b611db16211b65a9aadff29c5' - unsigned_tx_hex = util.aiorun(bitcoin.transaction(btcpay.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(btcpay.compose(db, source_default, order_match_id), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -271,7 +271,7 @@ def test_btcpay (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_divisible (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(issuance.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBB', quantity * 10, True, False, 0, 0.0, ''), encoding='multisig')) @@ -280,7 +280,7 @@ def test_issuance_divisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_indivisible_callable (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(issuance.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBC', round(quantity / 1000), False, True, 17, 0.015, 'foobar'), encoding='multisig')) @@ -289,7 +289,7 @@ def test_issuance_indivisible_callable (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_divisible (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBB', round(quantity / 25)), encoding='multisig')) @@ -298,7 +298,7 @@ def test_send_divisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_indivisible (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', round(quantity / 190000)), encoding='multisig')) @@ -307,7 +307,7 @@ def test_send_indivisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_divisible (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(dividend.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(dividend.compose(db, source_default, 600, 'BBBB', config.XCP), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -315,7 +315,7 @@ def test_dividend_divisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_indivisible (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(dividend.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(dividend.compose(db, source_default, 800, 'BBBC', config.XCP), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -323,7 +323,7 @@ def test_dividend_indivisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_initial (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, source_default, 1388000000, 100, fee_multiplier_default, 'Unit Test'), encoding='multisig')) @@ -332,7 +332,7 @@ def test_broadcast_initial (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_liquidated (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small, round(small / 2), 0.0, 15120, expiration), encoding='multisig')) @@ -341,7 +341,7 @@ def test_bet_bullcfd_to_be_liquidated (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_liquidated (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, round(small / 2), round(small * .83), 0.0, 15120, expiration), encoding='multisig')) @@ -350,7 +350,7 @@ def test_bet_bearcfd_to_be_liquidated (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_settled (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small * 3, small * 7, 0.0, 5040, expiration), encoding='multisig')) @@ -359,7 +359,7 @@ def test_bet_bullcfd_to_be_settled (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_settled (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, small * 7, small * 3, 0.0, 5040, expiration), encoding='multisig')) @@ -368,7 +368,7 @@ def test_bet_bearcfd_to_be_settled (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_equal (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, source_default, 2, 1388000200, small * 15, small * 13, 1, 5040, expiration), encoding='multisig')) @@ -377,7 +377,7 @@ def test_bet_equal (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_notequal (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, source_default, 3, 1388000200, small * 13, small * 15, 1, 5040, expiration), encoding='multisig')) @@ -386,7 +386,7 @@ def test_bet_notequal (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_liquidate (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, source_default, 1388000050, round(100 - (.415/3) - .00001, 5), fee_multiplier_default, 'Unit Test'), encoding='multisig')) @@ -395,7 +395,7 @@ def test_broadcast_liquidate (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_settle (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, source_default, 1388000101, 100.343, fee_multiplier_default, 'Unit Test'), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -403,7 +403,7 @@ def test_broadcast_settle (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_equal (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, source_default, 1388000201, 2, fee_multiplier_default, 'Unit Test'), encoding='multisig')) @@ -412,7 +412,7 @@ def test_broadcast_equal (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_to_be_cancelled (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(order.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(order.compose(db, source_default, 'BBBB', small, config.XCP, small, expiration, 0), encoding='multisig')) @@ -421,7 +421,7 @@ def test_order_to_be_cancelled (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_cancel (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(cancel.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(cancel.compose(db, source_default, '2f0fd1e89b8de1d57292742ec380ea47066e307ad645f5bc3adad8a06ff58608'), encoding='multisig')) @@ -431,7 +431,7 @@ def test_cancel (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_overburn (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(burn.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(burn.compose(db, source_default, (1 * config.UNIT), overburn=True), encoding='multisig')) # Try to burn a whole 'nother BTC. parse_hex(unsigned_tx_hex) @@ -439,7 +439,7 @@ def test_overburn (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_callable (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', 10000), encoding='multisig')) @@ -448,7 +448,7 @@ def test_send_callable (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_callback (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(callback.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(callback.compose(db, source_default, .3, 'BBBC'), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -456,7 +456,7 @@ def test_callback (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 100), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -464,7 +464,7 @@ def test_rps (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_counter_rps (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, destination_default, 5, 11021663, '6e8bf66cbd6636aca1802459b730a99548624e48e243b840e0b34a12bede17ec', 100), encoding='multisig')) @@ -475,7 +475,7 @@ def test_counter_rps (): def test_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rpsresolve.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig')) @@ -485,7 +485,7 @@ def test_rpsresolve (): def test_counter_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rpsresolve.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rpsresolve.compose(db, destination_default, 5, 'fa765e80203cba24a298e4458f63ff6b', rps_match_id), encoding='multisig')) @@ -494,7 +494,7 @@ def test_counter_rpsresolve (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps_expiration (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 10), encoding='multisig')) tx_rps = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', tx_rps['block_index'], source_default, 'XCP', 11021663, tx_rps['tx_hash']) @@ -506,12 +506,12 @@ def test_rps_expiration (): check_movment(db, 'credit', expiration_block, source_default, 'XCP', 11021663, tx_rps['tx_hash']) def test_pending_rps_match_expiration (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig')) rps1 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021664, rps1['tx_hash']) - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, destination_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig')) rps2 = parse_hex(unsigned_tx_hex) @@ -525,19 +525,19 @@ def test_pending_rps_match_expiration (): check_movment(db, 'credit', expiration_block, destination_default, 'XCP', 11021664, rps1['tx_hash'] + rps2['tx_hash']) def test_pending_and_resolved_rps_match_expiration (): - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig')) rps1 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021665, rps1['tx_hash']) - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rps.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, destination_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig')) rps2 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps2['block_index'], destination_default, 'XCP', 11021665, rps2['tx_hash']) rps_match_id = rps1['tx_hash'] + rps2['tx_hash'] - unsigned_tx_hex = util.aiorun(bitcoin.transaction(rpsresolve.compose(db, + unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig')) rps_match = parse_hex(unsigned_tx_hex) @@ -578,9 +578,9 @@ def test_json_rpc(): for payload in payloads: for attempt in range(100): # Try until server is ready. try: - response = util.aiorun(aiohttp.request('POST', url, + response = util.aio_wait_for(aiohttp.request('POST', url, data=json.dumps(payload), headers=headers)) - response = util.aiorun(response.json()) + response = util.aio_wait_for(response.json()) print(response) # print('\npayload', payload) # print('response', response, '\n') From c405127986ac1824517eb92ddabade139130d9f1 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Fri, 29 Aug 2014 10:31:11 -0400 Subject: [PATCH 15/17] Rename aio_wait_for to aio_run_synch --- counterpartyd.py | 6 ++-- lib/api.py | 20 ++++++------ lib/util.py | 16 +++++----- test/test_.py | 80 ++++++++++++++++++++++++------------------------ 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/counterpartyd.py b/counterpartyd.py index e7320d9347..af77189cb2 100755 --- a/counterpartyd.py +++ b/counterpartyd.py @@ -97,7 +97,7 @@ def market (give_asset, get_asset): # Your Pending Orders Matches. addresses = [] - for bunch in util.aio_wait_for(bitcoin.get_wallet()): + for bunch in util.aio_run_synch(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), @@ -1030,7 +1030,7 @@ def generate_move_random_hash(move): totals = {} print() - for bunch in util.aio_wait_for(bitcoin.get_wallet()): + for bunch in util.aio_run_synch(bitcoin.get_wallet()): address, btc_balance = bunch[:2] address_data = get_address(db, address=address) balances = address_data['balances'] @@ -1065,7 +1065,7 @@ def generate_move_random_hash(move): elif args.action == 'pending': addresses = [] - for bunch in util.aio_wait_for(bitcoin.get_wallet()): + for bunch in util.aio_run_synch(bitcoin.get_wallet()): addresses.append(bunch[:2][0]) filters = [ ('tx0_address', 'IN', addresses), diff --git a/lib/api.py b/lib/api.py index 9935e4dfa3..a90eaa0eea 100644 --- a/lib/api.py +++ b/lib/api.py @@ -202,7 +202,7 @@ def compose_transaction(db, name, params, fee=None, fee_provided=0): tx_info = sys.modules['lib.{}'.format(name)].compose(db, **params) - return util.aio_wait_for(bitcoin.transaction(tx_info, encoding=encoding, + return util.aio_run_synch(bitcoin.transaction(tx_info, encoding=encoding, fee_per_kb=fee_per_kb, regular_dust_size=regular_dust_size, multisig_dust_size=multisig_dust_size, @@ -213,23 +213,23 @@ def compose_transaction(db, name, params, fee_provided=fee_provided)) def sign_transaction(unsigned_tx_hex, private_key_wif=None): - return util.aio_wait_for(bitcoin.sign_tx(unsigned_tx_hex, + return util.aio_run_synch(bitcoin.sign_tx(unsigned_tx_hex, private_key_wif=private_key_wif)) def broadcast_transaction(signed_tx_hex): if not config.TESTNET and config.BROADCAST_TX_MAINNET in ['bci', 'bci-failover']: url = "https://blockchain.info/pushtx" params = {'tx': signed_tx_hex} - response = util.aio_wait_for(aiohttp.request('POST', url, data=params)) - data = util.aio_wait_for(response.read()) + response = util.aio_run_synch(aiohttp.request('POST', url, data=params)) + data = util.aio_run_synch(response.read()) if data.lower() != 'transaction submitted' or response.status != 200: if config.BROADCAST_TX_MAINNET == 'bci-failover': - return util.aio_wait_for(bitcoin.broadcast_tx(signed_tx_hex)) + return util.aio_run_synch(bitcoin.broadcast_tx(signed_tx_hex)) else: raise Exception(response.text) return data else: - return util.aio_wait_for(bitcoin.broadcast_tx(signed_tx_hex)) + return util.aio_run_synch(bitcoin.broadcast_tx(signed_tx_hex)) def do_transaction(db, name, params, private_key_wif=None, **kwargs): unsigned_tx = compose_transaction(db, name, params, **kwargs) @@ -260,9 +260,9 @@ def run(self): # Check that the database has caught up with bitcoind. if time.time() - self.last_database_check > 10 * 60: # Ten minutes since last check. code = 11 - util.aio_wait_for(bitcoin.bitcoind_check(db)) + util.aio_run_synch(bitcoin.bitcoind_check(db)) code = 12 - util.database_check(db, util.aio_wait_for(bitcoin.get_block_count())) # TODO: If not reparse or rollback, once those use API. + util.database_check(db, util.aio_run_synch(bitcoin.get_block_count())) # TODO: If not reparse or rollback, once those use API. self.last_database_check = time.time() except Exception as e: exception_name = e.__class__.__name__ @@ -399,7 +399,7 @@ def get_asset_info(assets): # BTC and XCP. if asset in [config.BTC, config.XCP]: if asset == config.BTC: - supply = util.aio_wait_for(bitcoin.get_btc_supply(normalize=False)) + supply = util.aio_run_synch(bitcoin.get_btc_supply(normalize=False)) else: supply = util.xcp_supply(db) @@ -486,7 +486,7 @@ def get_blocks(block_indexes): @dispatcher.add_method def get_running_info(): - latestBlockIndex = util.aio_wait_for(bitcoin.get_block_count()) + latestBlockIndex = util.aio_run_synch(bitcoin.get_block_count()) try: util.database_check(db, latestBlockIndex) diff --git a/lib/util.py b/lib/util.py index e8343537e0..1adcd59277 100644 --- a/lib/util.py +++ b/lib/util.py @@ -22,7 +22,7 @@ BET_TYPE_NAME = {0: 'BullCFD', 1: 'BearCFD', 2: 'Equal', 3: 'NotEqual'} BET_TYPE_ID = {'BullCFD': 0, 'BearCFD': 1, 'Equal': 2, 'NotEqual': 3} -def aio_wait_for(x, timeout=5): +def aio_run_synch(x, timeout=5): """ Run an asynchronous generator as though it was synchronous. (i.e. wait for it to complete), with an optional timeout """ return asyncio.get_event_loop().run_until_complete( @@ -37,9 +37,9 @@ def api (method, params): "jsonrpc": "2.0", "id": 0, } - response = aio_wait_for(aiohttp.request('POST', config.RPC, + response = aio_run_synch(aiohttp.request('POST', config.RPC, data=json.dumps(payload), headers=headers)) - response_json = aio_wait_for(response.json()) + response_json = aio_run_synch(response.json()) if response == None: raise exceptions.RPCError('Cannot communicate with {} server.'.format(config.XCP_CLIENT)) elif response.status != 200: @@ -359,9 +359,9 @@ def connect_to_db(flags=None): def version_check (db): try: host = 'https://raw2.github.com/CounterpartyXCP/counterpartyd/master/version.json' - response = aio_wait_for(aiohttp.request('GET', host, headers={'cache-control': + response = aio_run_synch(aiohttp.request('GET', host, headers={'cache-control': 'no-cache'})) - versions = aio_wait_for(response.json()) + versions = aio_run_synch(response.json()) except Exception as e: raise exceptions.VersionError('Unable to check version. How’s your Internet access?') @@ -717,9 +717,9 @@ def supplies (db): def get_url(url, abort_on_error=False, is_json=True, fetch_timeout=5): try: - r = aio_wait_for(aiohttp.request('GET', url), timeout=fetch_timeout) - if is_json: result = aio_wait_for(r.json()) - else: result = aio_wait_for(r.read()) + r = aio_run_synch(aiohttp.request('GET', url), timeout=fetch_timeout) + if is_json: result = aio_run_synch(r.json()) + else: result = aio_run_synch(r.read()) except Exception as e: raise Exception("Got get_url request error: %s" % e) else: diff --git a/test/test_.py b/test/test_.py index 7fa655bbd9..48ec60a71b 100644 --- a/test/test_.py +++ b/test/test_.py @@ -75,7 +75,7 @@ def parse_hex (unsigned_tx_hex): - tx = util.aio_wait_for(bitcoin.decode_raw_transaction(unsigned_tx_hex)) + tx = util.aio_run_synch(bitcoin.decode_raw_transaction(unsigned_tx_hex)) cursor = db.cursor() tx_hash = hashlib.sha256(chr(tx_index).encode('utf-8')).hexdigest() @@ -84,7 +84,7 @@ def parse_hex (unsigned_tx_hex): block_hash = hashlib.sha512(chr(block_index).encode('utf-8')).hexdigest() block_time = block_index * 10000000 - source, destination, btc_amount, fee, data = util.aio_wait_for(blocks.get_tx_info(tx, + source, destination, btc_amount, fee, data = util.aio_run_synch(blocks.get_tx_info(tx, block_index)) cursor.execute('''INSERT INTO blocks( @@ -227,7 +227,7 @@ def test_initialise (): cursor.close() def test_burn (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(burn.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(burn.compose(db, source_default, int(.62 * quantity)), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -235,7 +235,7 @@ def test_burn (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(send.compose(db, source_default, destination_default, config.XCP, small), encoding='multisig')) @@ -244,7 +244,7 @@ def test_send (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_buy_xcp (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(order.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(order.compose(db, source_default, config.BTC, small, config.XCP, small * 2, expiration, 0), encoding='multisig', fee_provided=fee_provided)) @@ -253,7 +253,7 @@ def test_order_buy_xcp (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_sell_xcp (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(order.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(order.compose(db, source_default, config.XCP, round(small * 2.1), config.BTC, small, expiration, fee_required), encoding='multisig')) @@ -263,7 +263,7 @@ def test_order_sell_xcp (): def test_btcpay (): order_match_id = 'dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986084fed08b978af4d7d196a7446a86b58009e636b611db16211b65a9aadff29c5' - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(btcpay.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(btcpay.compose(db, source_default, order_match_id), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -271,7 +271,7 @@ def test_btcpay (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_divisible (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(issuance.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBB', quantity * 10, True, False, 0, 0.0, ''), encoding='multisig')) @@ -280,7 +280,7 @@ def test_issuance_divisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_issuance_indivisible_callable (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(issuance.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(issuance.compose(db, source_default, None, 'BBBC', round(quantity / 1000), False, True, 17, 0.015, 'foobar'), encoding='multisig')) @@ -289,7 +289,7 @@ def test_issuance_indivisible_callable (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_divisible (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBB', round(quantity / 25)), encoding='multisig')) @@ -298,7 +298,7 @@ def test_send_divisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_indivisible (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', round(quantity / 190000)), encoding='multisig')) @@ -307,7 +307,7 @@ def test_send_indivisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_divisible (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(dividend.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(dividend.compose(db, source_default, 600, 'BBBB', config.XCP), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -315,7 +315,7 @@ def test_dividend_divisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_dividend_indivisible (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(dividend.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(dividend.compose(db, source_default, 800, 'BBBC', config.XCP), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -323,7 +323,7 @@ def test_dividend_indivisible (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_initial (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(broadcast.compose(db, source_default, 1388000000, 100, fee_multiplier_default, 'Unit Test'), encoding='multisig')) @@ -332,7 +332,7 @@ def test_broadcast_initial (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_liquidated (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small, round(small / 2), 0.0, 15120, expiration), encoding='multisig')) @@ -341,7 +341,7 @@ def test_bet_bullcfd_to_be_liquidated (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_liquidated (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, round(small / 2), round(small * .83), 0.0, 15120, expiration), encoding='multisig')) @@ -350,7 +350,7 @@ def test_bet_bearcfd_to_be_liquidated (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bullcfd_to_be_settled (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(bet.compose(db, source_default, source_default, 0, 1388000100, small * 3, small * 7, 0.0, 5040, expiration), encoding='multisig')) @@ -359,7 +359,7 @@ def test_bet_bullcfd_to_be_settled (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_bearcfd_to_be_settled (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(bet.compose(db, source_default, source_default, 1, 1388000100, small * 7, small * 3, 0.0, 5040, expiration), encoding='multisig')) @@ -368,7 +368,7 @@ def test_bet_bearcfd_to_be_settled (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_equal (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(bet.compose(db, source_default, source_default, 2, 1388000200, small * 15, small * 13, 1, 5040, expiration), encoding='multisig')) @@ -377,7 +377,7 @@ def test_bet_equal (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_bet_notequal (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(bet.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(bet.compose(db, source_default, source_default, 3, 1388000200, small * 13, small * 15, 1, 5040, expiration), encoding='multisig')) @@ -386,7 +386,7 @@ def test_bet_notequal (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_liquidate (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(broadcast.compose(db, source_default, 1388000050, round(100 - (.415/3) - .00001, 5), fee_multiplier_default, 'Unit Test'), encoding='multisig')) @@ -395,7 +395,7 @@ def test_broadcast_liquidate (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_settle (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(broadcast.compose(db, source_default, 1388000101, 100.343, fee_multiplier_default, 'Unit Test'), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -403,7 +403,7 @@ def test_broadcast_settle (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_broadcast_equal (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(broadcast.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(broadcast.compose(db, source_default, 1388000201, 2, fee_multiplier_default, 'Unit Test'), encoding='multisig')) @@ -412,7 +412,7 @@ def test_broadcast_equal (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_order_to_be_cancelled (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(order.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(order.compose(db, source_default, 'BBBB', small, config.XCP, small, expiration, 0), encoding='multisig')) @@ -421,7 +421,7 @@ def test_order_to_be_cancelled (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_cancel (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(cancel.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(cancel.compose(db, source_default, '2f0fd1e89b8de1d57292742ec380ea47066e307ad645f5bc3adad8a06ff58608'), encoding='multisig')) @@ -431,7 +431,7 @@ def test_cancel (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_overburn (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(burn.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(burn.compose(db, source_default, (1 * config.UNIT), overburn=True), encoding='multisig')) # Try to burn a whole 'nother BTC. parse_hex(unsigned_tx_hex) @@ -439,7 +439,7 @@ def test_overburn (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_send_callable (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(send.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(send.compose(db, source_default, destination_default, 'BBBC', 10000), encoding='multisig')) @@ -448,7 +448,7 @@ def test_send_callable (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_callback (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(callback.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(callback.compose(db, source_default, .3, 'BBBC'), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -456,7 +456,7 @@ def test_callback (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 100), encoding='multisig')) parse_hex(unsigned_tx_hex) @@ -464,7 +464,7 @@ def test_rps (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_counter_rps (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, destination_default, 5, 11021663, '6e8bf66cbd6636aca1802459b730a99548624e48e243b840e0b34a12bede17ec', 100), encoding='multisig')) @@ -475,7 +475,7 @@ def test_counter_rps (): def test_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rpsresolve.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig')) @@ -485,7 +485,7 @@ def test_rpsresolve (): def test_counter_rpsresolve (): rps_match_id = '58f7b0780592032e4d8602a3e8690fb2c701b2e1dd546e703445aabd6469734d77adfc95029e73b173f60e556f915b0cd8850848111358b1c370fb7c154e61fd' - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rpsresolve.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rpsresolve.compose(db, destination_default, 5, 'fa765e80203cba24a298e4458f63ff6b', rps_match_id), encoding='multisig')) @@ -494,7 +494,7 @@ def test_counter_rpsresolve (): output_new[inspect.stack()[0][3]] = unsigned_tx_hex def test_rps_expiration (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, source_default, 5, 11021663, move_random_hash_default, 10), encoding='multisig')) tx_rps = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', tx_rps['block_index'], source_default, 'XCP', 11021663, tx_rps['tx_hash']) @@ -506,12 +506,12 @@ def test_rps_expiration (): check_movment(db, 'credit', expiration_block, source_default, 'XCP', 11021663, tx_rps['tx_hash']) def test_pending_rps_match_expiration (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, source_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig')) rps1 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021664, rps1['tx_hash']) - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, destination_default, 5, 11021664, move_random_hash_default, 10), encoding='multisig')) rps2 = parse_hex(unsigned_tx_hex) @@ -525,19 +525,19 @@ def test_pending_rps_match_expiration (): check_movment(db, 'credit', expiration_block, destination_default, 'XCP', 11021664, rps1['tx_hash'] + rps2['tx_hash']) def test_pending_and_resolved_rps_match_expiration (): - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, source_default, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, source_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig')) rps1 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps1['block_index'], source_default, 'XCP', 11021665, rps1['tx_hash']) - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rps.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rps.compose(db, destination_default, 5, 11021665, move_random_hash_default, 10), encoding='multisig')) rps2 = parse_hex(unsigned_tx_hex) check_movment(db, 'debit', rps2['block_index'], destination_default, 'XCP', 11021665, rps2['tx_hash']) rps_match_id = rps1['tx_hash'] + rps2['tx_hash'] - unsigned_tx_hex = util.aio_wait_for(bitcoin.transaction(rpsresolve.compose(db, + unsigned_tx_hex = util.aio_run_synch(bitcoin.transaction(rpsresolve.compose(db, source_default, 3, rps_random_default, rps_match_id), encoding='multisig')) rps_match = parse_hex(unsigned_tx_hex) @@ -578,9 +578,9 @@ def test_json_rpc(): for payload in payloads: for attempt in range(100): # Try until server is ready. try: - response = util.aio_wait_for(aiohttp.request('POST', url, + response = util.aio_run_synch(aiohttp.request('POST', url, data=json.dumps(payload), headers=headers)) - response = util.aio_wait_for(response.json()) + response = util.aio_run_synch(response.json()) print(response) # print('\npayload', payload) # print('response', response, '\n') From adc3300ee22d42f4d06b78a31fddf153d443a272 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Fri, 29 Aug 2014 11:07:04 -0400 Subject: [PATCH 16/17] Put wif prefixes in config --- lib/bitcoin.py | 5 ++++- lib/config.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index f966e0c96a..ccafdc5d8e 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -385,7 +385,10 @@ def sort_unspent_txouts(unspent, allow_unconfirmed_inputs): return unspent def private_key_to_public_key (private_key_wif): - allowable_wif_prefixes = [ b'\x80', b'\xef' ] + if config.TESTNET: + allowable_wif_prefixes = [config.PRIVATEKEY_VERSION_TESTNET] + else: + allowable_wif_prefixes = [config.PRIVATEKEY_VERSION_MAINNET] try: secret_exponent, compressed = wif_to_tuple_of_secret_exponent_compressed( private_key_wif, allowable_wif_prefixes=allowable_wif_prefixes) diff --git a/lib/config.py b/lib/config.py index dbb4eb11b6..72f77ddffc 100644 --- a/lib/config.py +++ b/lib/config.py @@ -51,9 +51,9 @@ UNSPENDABLE_MAINNET = '1CounterpartyXXXXXXXXXXXXXXXUWLpVr' ADDRESSVERSION_TESTNET = b'\x6f' -# PRIVATEKEY_VERSION_TESTNET = +PRIVATEKEY_VERSION_TESTNET = '\x6f' ADDRESSVERSION_MAINNET = b'\x00' -# PRIVATEKEY_VERSION_MAINNET = +PRIVATEKEY_VERSION_MAINNET = '\x80' MAGIC_BYTES_TESTNET = b'\xfa\xbf\xb5\xda' # For bip-0010 MAGIC_BYTES_MAINNET = b'\xf9\xbe\xb4\xd9' # For bip-0010 From 27406df9d40b4032db101cd403d68f17fb1b0b01 Mon Sep 17 00:00:00 2001 From: Bob McElrath Date: Thu, 4 Sep 2014 09:28:23 -0400 Subject: [PATCH 17/17] Update pycoin version to 0.51 --- pip-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pip-requirements.txt b/pip-requirements.txt index 4383dacad4..af32e0c6f4 100644 --- a/pip-requirements.txt +++ b/pip-requirements.txt @@ -16,7 +16,7 @@ pytest==2.5.1 ############# #counterpartyd specific deps -pycoin==0.26 +pycoin==0.51 aiohttp>=0.9.0