def check_unprocessed(top_height): for tx_hash in unprocessed_txs.members(): txid = hash_to_hex(tx_hash).decode() tx = Tx.tx_from_hex(txs[tx_hash].decode()) tx_blockchain = get_tx(txid) logging.info('Checking %s' % txid) if tx_blockchain.block_height == -1: continue if top_height - tx_blockchain.block_height + 1 >= REQUIRED_CONFIRMATIONS: # off by one error - if tx in top block that is 1 conf unprocessed_txs.remove(tx_hash) for out in tx.txs_out: address = out.bitcoin_address() if address not in all_addresses: continue account = Account(addr_to_uid[address]) satoshis = out.coin_value satoshis = int(satoshis / (1 + account.tip.get())) # scale for tip account.total_coins.incr(satoshis) node_minutes_d = calc_node_minutes(satoshis, exchange_rate=exchange_rate.get()) account.total_minutes.incr(node_minutes_d) total_nodeminutes.incr(node_minutes_d) nodes_recently_updated.append(account.uid) account.add_msg('Detected payment via txid: %s' % (txid,)) account.add_msg('Increased total paid by %.8f to %.8f (considering tip of %d %%)' % (satoshis / COIN, account.total_coins.get() / COIN, account.tip.get() * 100)) account.add_msg('Increased node life by %d minutes; expiring around %s' % (node_minutes_d, account.get_expiry().isoformat()))
#!/usr/bin/env python3 import time import requests from models import exchange_rate while True: res = requests.get("https://bitpay.com/api/rates/usd") response = res.json() if "code" in response: if response["code"] == "USD": exchange_rate.set(response["rate"]) print("Set rate to", response["rate"], "and retrieved", exchange_rate.get()) time.sleep(30)
def process_tx_initial(tx_obj: Tx): found_relevant_address = False for out in tx_obj.txs_out: address = out.bitcoin_address() if address in all_addresses: found_relevant_address = True break if not found_relevant_address: logging.info('Found irrelevant tx %s' % hash_to_hex(tx_obj.hash())) return tx_hash = tx_obj.hash() txid = hash_to_hex(tx_hash).decode() if tx_hash in known_txs: return known_txs.add(tx_hash) txs[tx_hash] = tx_obj.as_hex() for out in tx_obj.txs_out: address = out.bitcoin_address() if address in all_addresses and address is not None: unprocessed_txs.add(tx_hash) uid = addr_to_uid[address] account = Account(uid) account.txs.add(tx_hash) account.unconf_minutes.incr(calc_node_minutes(satoshi_amount=out.coin_value, exchange_rate=exchange_rate.get())) account.add_msg('Found tx for %.08f, %s' % (out.coin_value / COIN, txid)) nodes_recently_updated.append(account.uid)
time.sleep(10) # only do this at most once per block continue logging.info('Latest block: %s' % best_block_hash) for tx_hash in unprocessed_txs.members(): txid = hash_to_hex(tx_hash).decode() tx = Tx.tx_from_hex(txs[tx_hash].decode()) tx_blockchain = get_tx(txid) logging.info('Checking %s' % txid) if tx_blockchain.block_height == -1: continue if top_height - tx_blockchain.block_height + 1 >= REQUIRED_CONFIRMATIONS: # off by one error - if tx in top block that is 1 conf unprocessed_txs.remove(tx_hash) for out in tx.txs_out: address = out.bitcoin_address() if address not in all_addresses: continue account = Account(addr_to_uid[address]) satoshis = out.coin_value satoshis = int(satoshis / (1 + account.tip.get())) # scale for tip account.total_coins.incr(satoshis) node_minutes_d = calc_node_minutes(satoshis, exchange_rate=exchange_rate.get()) account.total_minutes.incr(node_minutes_d) total_nodeminutes.incr(node_minutes_d) nodes_recently_updated.append(account.uid) account.add_msg('Detected payment via txid: %s' % (txid,)) account.add_msg('Increased total paid by %.8f to %.8f (considering tip of %d %%)' % (satoshis / COIN, account.total_coins.get() / COIN, account.tip.get() * 100)) account.add_msg('Increased node life by %d minutes; expiring around %s' % (node_minutes_d, account.get_expiry().isoformat())) last_block_checked.set(best_block_hash) logging.info('Checked: %s' % best_block_hash)
def handle(method, **params): response = {} real_uid = params['uid'] uid = process_uid(params['uid']) # not user chosen any longer account = Account(uid) fieldMap = { 'emailNotify': account.email_notify, 'email': account.email, 'name': account.name, 'client': account.client, 'tip': account.tip, 'branch': account.branch, } if method == 'getPaymentDetails': client = params['client'] months = max(0, int(params['months'])) tip = account.tip.get() amount = actually_charge(months, tip, exchange_rate.get()) # note: this URI cannot contain spaces as some wallets cannot read it. response['uri'] = "bitcoin:%s?amount=%.8f&label=nodeup.xk.io_sponsored_node_funding_address" % (account.address, amount) response['status'] = 'Payment received.' if account.unconf_minutes.get() > 0 else 'Waiting for payment...' elif method == '': pass elif method == 'saveField': field = params['field'] value = params['value'] if field == 'name' and len(value) > 140: value = value[:140] fieldMap[field].set(value) elif method == 'loadField': field = params['field'] response['field'] = field response['value'] = fieldMap[field].get() elif method == 'getMsgs': if 'n' in params: n = params['n'] else: n = 10 response['msgs'] = account.get_msgs(n) elif method == 'getStats': response['nodeMinutes'] = int(total_nodeminutes.get()) response['totalMinutesPaid'] = account.total_minutes.get() response['totalCoinsPaid'] = account.total_coins.get() response['exchangeRate'] = exchange_rate.get() response['activeNodes'] = len(active_servers) response['nodeCreationIssues'] = node_creation_issues.get() elif method == 'recompile': if account.node_created.get(): droplets_to_configure.add(account.droplet_id.get(), 0) account.add_msg('Queued node for recompilation.') response['recompile_queued'] = True else: response['recompile_queued'] = False elif method == 'sendNodeDetails': account.email_user('Node Details', NODE_DETAILS_EMAIL.format(uid=real_uid), force=True) return response