def clean_wallet(server, clean_wallet): namecoind = NamecoindClient(server) reply = namecoind.listtransactions("", 10000) counter = 0 counter_total = 0 track_confirmations = 1000 for i in reply: counter_total += 1 if i['confirmations'] == 0: counter += 1 if clean_wallet: log.debug(namecoind.deletetransaction(i['txid'])) elif i['confirmations'] < track_confirmations: track_confirmations = i['confirmations'] log.debug("%s: %s pending tx, %s confirmations (last tx), %s total tx" % (server, counter, track_confirmations, counter_total))
def load_balance(current_server): counter = 0 for server in LOAD_SERVERS: if current_server == server: server_number = counter counter += 1 log.debug("current server: %s", LOAD_SERVERS[server_number]) while(1): if pending_transactions(LOAD_SERVERS[server_number]) > MAX_PENDING_TX: if server_number == len(LOAD_SERVERS) - 1: server_number = 0 else: server_number += 1 log.debug("load balancing: switching to %s", LOAD_SERVERS[server_number]) sleep(5) else: break return LOAD_SERVERS[server_number]
def name_show_mem(key): info = {} if MEMCACHED_ENABLED: cache_reply = mc.get("name_" + str(key)) else: cache_reply = None if cache_reply is None: try: info = namecoind.name_show(key) if 'status' in info['value'] and info['value']['status'] == -1: info['value'] = {} if MEMCACHED_ENABLED: mc.set("name_" + str(key), json.dumps(info['value']), int(time() + MEMCACHED_TIMEOUT)) log.debug("cache miss: " + str(key)) except: info = {} else: log.debug("cache hit: " + str(key)) info['value'] = json.loads(cache_reply) return info
def unlock_wallet(self, passphrase, timeout = 100): try: info = self.namecoind.walletpassphrase(passphrase, timeout) except JSONRPCException as e: if e.error['code'] == -17: return True else: log.debug(e.error) return False return True
def process_user(username, profile, server=NAMECOIND_SERVER, new_address=None): master_key = 'u/' + username if namecoind.check_registration(master_key): keys, values = slice_profile_update(username, profile) else: keys, values = slice_profile(username, profile) index = 0 key1 = keys[index] value1 = values[index] if namecoind.check_registration(key1): # if name is registered log.debug("name update: %s", key1) log.debug("size: %s", utf8len(json.dumps(value1))) update_name(key1, value1, new_address) else: # if not registered log.debug("name new: %s", key1) log.debug("size: %s", utf8len(json.dumps(value1))) register_name(key1, value1, server, username) process_additional_keys(keys, values, server, username, new_address)
def check_address_inner(server): try: namecoind = NamecoindClient(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD) info = namecoind.validate_address(address) except Exception as e: log.debug("Error in server %s", server) log.debug(e) return if info['ismine'] is True: reply['server'] = server reply['ismine'] = True
def get_namecoind(key): server = NAMECOIND_SERVER serverinfo = get_server(key, MAIN_SERVER, LOAD_SERVERS) if 'registered' in serverinfo and serverinfo['registered']: if serverinfo['server'] is not None: server = serverinfo['server'] log.debug(server) log.debug(key) namecoind = NamecoindClient(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD, NAMECOIND_USE_HTTPS, NAMECOIND_WALLET_PASSPHRASE) return namecoind
def warmup_cache(regrex,check_blocks=0): log.debug("processing namespace %s",regrex) reply = namecoind.name_filter(regrex,check_blocks) counter = 0 for i in reply: try: #set to no expiry i.e., 0 mc.set("name_" + str(i['name']),i['value'],0) log.debug("inserting %s in cache",i['name']) counter += 1 except: log.debug("not putting %s in cache",i['name']) log.debug("inserted %s entries in cache",counter) log.debug('-'*5)
def update_name(key, value, new_address=None): reply = {} cache_reply = mc.get("name_update_" + str(key)) if cache_reply is None: namecoind = get_namecoind(key) info = namecoind.name_update(key, json.dumps(value), new_address) if 'code' in info: reply = info else: reply['tx'] = info mc.set("name_update_" + str(key), "in_memory", int(time() + MEMCACHED_TIMEOUT)) else: reply['message'] = "ERROR: " + "recently sent name_update: " + str(key) log.debug(reply) log.debug('-' * 5)
def get_user_profile(username): username = username.lower() key = 'u/' + username if not namecoind.check_registration(key): abort(404) if MEMCACHED_ENABLED: log.debug('cache enabled') cache_reply = mc.get("profile_" + str(key)) else: cache_reply = None log.debug("cache off") if cache_reply is None: info = {} profile = full_profile_mem(key) if not profile: info['profile'] = None info['error'] = "Malformed profile data" info['verifications'] = [] else: info['profile'] = profile info['verifications'] = profile_to_proofs(profile, username) if MEMCACHED_ENABLED: mc.set("profile_" + str(key), json.dumps(info), int(time() + MEMCACHED_TIMEOUT)) log.debug("cache miss full_profile") else: log.debug("cache hit full_profile") info = json.loads(cache_reply) return info
def register_name(key, value, server=NAMECOIND_SERVER, username=None): reply = {} # check if already in register queue (name_new) check_queue = register_queue.find_one({"key": key}) if check_queue is not None: reply['message'] = "ERROR: " + "already in register queue: " + str(key) else: namecoind = NamecoindClient(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD, NAMECOIND_USE_HTTPS, NAMECOIND_WALLET_PASSPHRASE) try: info = namecoind.name_new(key, json.dumps(value)) reply['txid'] = info[0] reply['rand'] = info[1] except: log.debug(info) reply['message'] = info return reply reply['key'] = key reply['value'] = json.dumps(value) reply['tx_sent'] = False reply['server'] = server if username is not None: reply['username'] = username # save this data to Mongodb... register_queue.insert(reply) # reply[_id] is causing a json encode error del reply['_id'] log.debug(reply) log.debug('-' * 5) return reply
def process_additional_keys(keys, values, server, username, new_address): # register/update remaining keys size = len(keys) index = 1 while index < size: next_key = keys[index] next_value = values[index] log.debug(utf8len(json.dumps(next_value))) if namecoind.check_registration(next_key): log.debug("name update: " + next_key) update_name(next_key, next_value, new_address) else: log.debug("name new: " + next_key) register_name(next_key, next_value, server, username) index += 1
def sweep_btc(transfer_user, LIVE=False): user_id = transfer_user['user_id'] new_user = new_users.find_one({"_id": user_id}) if new_user is None: return old_user = old_users.find_one({'username': new_user['username']}) if old_user is None: return new_btc_address = new_user['bitcoin_address'] old_btc_address = json.loads(old_user['profile'])['bitcoin']['address'] wif_pk = bip38_decrypt(str(transfer_user['encrypted_private_key']), WALLET_SECRET) keypair = BitcoinKeypair.from_private_key(wif_pk) if old_btc_address == keypair.address(): balance = fetch_balance(old_btc_address) if balance == float(0): return False log.debug(new_user['username']) log.debug("old btc address: " + old_btc_address) bitcoind.importprivkey(keypair.wif_pk()) if LIVE: log.debug("sending " + str(balance) + " to " + new_btc_address) tx = bitcoind.sendtoaddress(new_btc_address, balance) log.debug(tx) else: log.debug("need to send " + str(balance) + " to " + new_btc_address) log.debug("final balance: %s", balance) log.debug('-' * 5) return True return False
from registrar.config import BITCOIND_SERVER, BITCOIND_PORT, BITCOIND_USER, BITCOIND_PASSWD, BITCOIND_USE_HTTPS, BITCOIND_WALLET_PASSPHRASE bitcoind = BitcoindServer(BITCOIND_SERVER, BITCOIND_PORT, BITCOIND_USER, BITCOIND_PASSWD, BITCOIND_USE_HTTPS, BITCOIND_WALLET_PASSPHRASE) from pymongo import MongoClient remote_db = MongoClient(MONGODB_URI).get_default_database() new_users = remote_db.user transfer = remote_db.name_transfer old_db = MongoClient(OLD_DB).get_default_database() old_users = old_db.user try: WALLET_SECRET = os.environ['WALLET_SECRET'] except: log.debug("ERROR: WALLET_SECRET not set (check web app env variables)") WALLET_SECRET = '' def sweep_btc(transfer_user, LIVE=False): user_id = transfer_user['user_id'] new_user = new_users.find_one({"_id": user_id}) if new_user is None: return old_user = old_users.find_one({'username': new_user['username']}) if old_user is None: return
def warmup_cache(regrex, check_blocks=0): log.debug("processing namespace %s", regrex) reply = namecoind.name_filter(regrex, check_blocks) counter = 0 for i in reply: try: # set to no expiry i.e., 0 mc.set("name_" + str(i['name']),i['value'],0) log.debug("inserting %s in cache",i['name']) counter += 1 except Exception as e: log.debug("not putting %s in cache",i['name']) log.debug(e) log.debug("inserted %s entries in cache",counter) log.debug('-'*5)
def do_name_firstupdate(): log.debug("Checking for new activations") log.debug('-' * 5) ignore_servers = [] counter = 0 counter_pending = 0 from coinrpc import namecoind blocks = namecoind.blocks() for entry in register_queue.find(): counter += 1 if counter % 10 == 0: for server in ignore_servers: if pending_transactions(server) > MAX_PENDING_TX: pass else: ignore_servers.remove(server) from coinrpc import namecoind if not namecoind.check_registration(entry['key']): counter_pending += 1 key = entry['key'] server = entry['server'] namecoind = NamecoindServer(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD, NAMECOIND_USE_HTTPS, NAMECOIND_WALLET_PASSPHRASE) if 'tx_sent' in entry and entry['tx_sent'] is True: log.debug('Already sent name_firstupdate: %s' % entry['key']) continue if 'wait_till_block' not in entry: reply = namecoind.gettransaction(entry['txid']) if 'code' in reply: register_queue.remove(entry) continue if reply['confirmations'] > 1: log.debug('Got confirmations on name_new: %s' % entry['key']) entry['wait_till_block'] = namecoind.blocks() + ( 12 - reply['confirmations']) register_queue.save(entry) else: log.debug('No confirmations on name_new: %s' % entry['key']) continue if entry['wait_till_block'] <= blocks: if server in ignore_servers: continue if pending_transactions(server) > MAX_PENDING_TX: log.debug("Pending tx on server, try again") ignore_servers.append(server) continue update_value = None if 'username' in entry: update_value = get_string(refresh_value(entry)) if update_value is None: update_value = get_string(entry['value']) log.debug("Activating entry: '%s' to point to '%s'" % (key, update_value)) output = namecoind.firstupdate(key, entry['rand'], update_value, entry['txid']) log.debug(output) if 'message' in output and output[ 'message'] == "this name is already active": register_queue.remove(entry) elif 'message' in output and output[ 'message'] == "previous transaction is not in the wallet": register_queue.remove(entry) elif 'code' in output: log.debug("Not activated. Try again.") else: entry['tx_sent'] = True register_queue.save(entry) log.debug('-' * 5) else: log.debug("wait: %s blocks for: %s" % ((entry['wait_till_block'] - blocks), entry['key'])) else: log.debug("key %s already active" % (entry['key'])) register_queue.remove(entry) print "Pending activations: %s" % counter_pending current_block = namecoind.blocks() while (1): new_block = namecoind.blocks() if current_block == new_block: log.debug('No new block. Sleeping ... ') sleep(15) else: break
BITCOIND_PASSWD, BITCOIND_USE_HTTPS, BITCOIND_WALLET_PASSPHRASE) from pymongo import MongoClient remote_db = MongoClient(MONGODB_URI).get_default_database() new_users = remote_db.user transfer = remote_db.name_transfer old_db = MongoClient(OLD_DB).get_default_database() old_users = old_db.user try: WALLET_SECRET = os.environ['WALLET_SECRET'] except: log.debug("ERROR: WALLET_SECRET not set (check web app env variables)") WALLET_SECRET = '' def sweep_btc(transfer_user, LIVE=False): user_id = transfer_user['user_id'] new_user = new_users.find_one({"_id": user_id}) if new_user is None: return old_user = old_users.find_one({'username': new_user['username']}) if old_user is None: return
MEMCACHED_PORT = 11211 MEMCACHED_SERVER = '127.0.0.1' MEMCACHED_TIMEOUT = 12 * 60 * 60 USERSTATS_TIMEOUT = 60 * 60 MEMCACHED_ENABLED = True RECENT_BLOCKS = 100 VALID_BLOCKS = 36000 REFRESH_BLOCKS = 25 try: from config_local import * except: log.debug('config_local.py not found, using default settings') try: MEMCACHED_USERNAME = os.environ['MEMCACHEDCLOUD_USERNAME'] MEMCACHED_PASSWORD = os.environ['MEMCACHEDCLOUD_PASSWORD'] except: try: MEMCACHED_USERNAME = os.environ['MEMCACHIER_USERNAME'] MEMCACHED_PASSWORD = os.environ['MEMCACHIER_PASSWORD'] except: MEMCACHED_USERNAME = None MEMCACHED_PASSWORD = None try: MEMCACHED_SERVERS = os.environ['MEMCACHEDCLOUD_SERVERS'].split(',') except:
def do_name_firstupdate(): log.debug("Checking for new activations") log.debug('-' * 5) ignore_servers = [] counter = 0 counter_pending = 0 from coinrpc import namecoind blocks = namecoind.blocks() for entry in register_queue.find(): counter += 1 if counter % 10 == 0: for server in ignore_servers: if pending_transactions(server) > MAX_PENDING_TX: pass else: ignore_servers.remove(server) from coinrpc import namecoind if not namecoind.check_registration(entry['key']): counter_pending += 1 key = entry['key'] server = entry['server'] namecoind = NamecoindServer(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD, NAMECOIND_USE_HTTPS, NAMECOIND_WALLET_PASSPHRASE) if 'tx_sent' in entry and entry['tx_sent'] is True: log.debug('Already sent name_firstupdate: %s' % entry['key']) continue if 'wait_till_block' not in entry: reply = namecoind.gettransaction(entry['txid']) if 'code' in reply: register_queue.remove(entry) continue if reply['confirmations'] > 1: log.debug('Got confirmations on name_new: %s' % entry['key']) entry['wait_till_block'] = namecoind.blocks() + (12 - reply['confirmations']) register_queue.save(entry) else: log.debug('No confirmations on name_new: %s' % entry['key']) continue if entry['wait_till_block'] <= blocks: if server in ignore_servers: continue if pending_transactions(server) > MAX_PENDING_TX: log.debug("Pending tx on server, try again") ignore_servers.append(server) continue update_value = None if 'username' in entry: update_value = get_string(refresh_value(entry)) if update_value is None: update_value = get_string(entry['value']) log.debug("Activating entry: '%s' to point to '%s'" % (key, update_value)) output = namecoind.firstupdate(key,entry['rand'],update_value,entry['txid']) log.debug(output) if 'message' in output and output['message'] == "this name is already active": register_queue.remove(entry) elif 'message' in output and output['message'] == "previous transaction is not in the wallet": register_queue.remove(entry) elif 'code' in output: log.debug("Not activated. Try again.") else: entry['tx_sent'] = True register_queue.save(entry) log.debug('-' * 5) else: log.debug("wait: %s blocks for: %s" % ((entry['wait_till_block'] - blocks), entry['key'])) else: log.debug("key %s already active" % (entry['key'])) register_queue.remove(entry) print "Pending activations: %s" % counter_pending current_block = namecoind.blocks() while(1): new_block = namecoind.blocks() if current_block == new_block: log.debug('No new block. Sleeping ... ') sleep(15) else: break