def get_user_profile(username, refresh=False, namespace=DEFAULT_NAMESPACE): global MEMCACHED_ENABLED if refresh: MEMCACHED_ENABLED = False username = username.lower() blockstore_client = Proxy(BLOCKSTORED_SERVER, BLOCKSTORED_PORT) blockstore_resp = blockstore_client.lookup(username + "." + namespace) blockstore_resp = blockstore_resp[0] if blockstore_resp is None: abort(404) if MEMCACHED_ENABLED: cache_reply = mc.get("profile_" + str(username)) else: cache_reply = None if cache_reply is None: profile_hash = blockstore_resp['value_hash'] profile = get_profile_from_dht(profile_hash) data = format_profile(profile, username) if MEMCACHED_ENABLED or refresh: mc.set("profile_" + str(username), json.dumps(data), int(time() + MEMCACHED_TIMEOUT)) else: data = json.loads(cache_reply) return data
def refresh_dht_entries(): fin = open(DHT_STATE_FILE, 'r') data = fin.read() data = json.loads(data) counter = 0 for entry in data: print '-' * 5 print "Processing key: %s" % entry['key'] key = entry['key'] value = entry['value'] print "(%s, %s)" % (key, value) try: dht_client = Proxy(DEFAULT_SERVER, MIRROR_TCP_PORT, timeout=5) resp = dht_client.dht_set(key, value) print resp counter += 1 print counter print '-' * 5 except Exception as e: print e print "Problem with key: %s" % key
def get_dht_client(local_server=False): """ Get a new connection to DHT """ if local_server: return Proxy(DEFAULT_DHT_SERVERS[0], DHT_SERVER_PORT) else: return Proxy(DEFAULT_MIRROR, MIRROR_TCP_PORT)
def get_profile(username, refresh=False, namespace=DEFAULT_NAMESPACE): """ Given a fully-qualified username (username.namespace) get the data associated with that fqu. Return cached entries, if possible. """ global MEMCACHED_ENABLED global mc username = username.lower() if MEMCACHED_ENABLED and not refresh: log.debug("Memcache get DHT: %s" % username) dht_cache_reply = mc.get("dht_" + str(username)) else: log.debug("Memcache disabled: %s" % username) dht_cache_reply = None if dht_cache_reply is None: try: bs_client = Proxy(BLOCKSTACKD_IP, BLOCKSTACKD_PORT, timeout=10) bs_resp = bs_client.get_name_blockchain_record(username + "." + namespace) bs_resp = bs_resp[0] except: abort( 500, "Connection to blockstack-server %s:%s timed out" % (BLOCKSTACKD_IP, BLOCKSTACKD_PORT)) if bs_resp is None or 'error' in bs_resp: abort(404) if 'value_hash' in bs_resp: profile_hash = bs_resp['value_hash'] dht_response = fetch_from_dht(profile_hash) dht_data = {} dht_data['dht_response'] = dht_response dht_data['owner_address'] = bs_resp['address'] if MEMCACHED_ENABLED or refresh: log.debug("Memcache set DHT: %s" % username) mc.set("dht_" + str(username), json.dumps(dht_data), int(time() + MEMCACHED_TIMEOUT)) else: dht_data = {"error": "Not found"} else: dht_data = json.loads(dht_cache_reply) data = format_profile(dht_data['dht_response'], username, dht_data['owner_address']) return data
def get_profile_from_dht(profile_hash): dht_client = Proxy(DHT_MIRROR, DHT_MIRROR_PORT) dht_resp = dht_client.get(profile_hash) dht_resp = dht_resp[0] try: profile = json.loads(dht_resp['value']) except: profile = {} return profile
def dontUseServer(blockstackd_server): """ Return false if server fails any tests """ from registrar.config import CONSENSUS_SERVERS from basicrpc import Proxy servers_to_check = CONSENSUS_SERVERS servers_to_check.append(blockstackd_server) consensus_hashes = [] # initialize to a very large number last_block_everyone = 2000000000 for server in servers_to_check: bs_client = Proxy(server, BLOCKSTACKD_PORT) last_block_seen = bs_client.getinfo()[0]['bitcoind_blocks'] try: last_block_processed = bs_client.getinfo()[0]['last_block'] except: last_block_processed = bs_client.getinfo()[0]['blocks'] if (last_block_seen - last_block_processed) > 10: log.debug("Server %s, seems to be lagging: (%s, %s)" % (server, last_block_seen, last_block_processed)) return True if last_block_processed < last_block_everyone: last_block_everyone = last_block_processed for server in servers_to_check: bs_client = Proxy(server, BLOCKSTACKD_PORT) consensus_hash = bs_client.get_consensus_at(last_block_everyone)[0] print consensus_hash consensus_hashes.append(consensus_hash) check_hash = consensus_hashes[0] for stored_hash in consensus_hashes: if check_hash != stored_hash: log.debug('Mismatch in consensus hashes from %s' % servers_to_check) return True # can use server, if all tests pass return False
def fetch_from_dht(profile_hash): """ Given a @profile_hash fetch full profile JSON """ dht_client = Proxy(DHT_MIRROR_IP, DHT_MIRROR_PORT) dht_resp = dht_client.get(profile_hash) dht_resp = dht_resp[0] try: profile = json.loads(dht_resp['value']) except: profile = {} return profile
def dht_init(local_server=False): """ Establish our connection to the DHT, and give it the requisite state it needs (i.e. an API proxy to blockstack) """ global dht_server if local_server: dht_server = Proxy(DEFAULT_DHT_SERVERS[0], DHT_SERVER_PORT) else: dht_server = Proxy(DEFAULT_MIRROR, MIRROR_TCP_PORT) return True
def get_profile(username, refresh=False, namespace=DEFAULT_NAMESPACE): """ Given a fully-qualified username (username.namespace) get the data associated with that fqu. Return cached entries, if possible. """ global MEMCACHED_ENABLED global mc username = username.lower() if MEMCACHED_ENABLED and not refresh: log.debug("Memcache get: %s" % username) cache_reply = mc.get("profile_" + str(username)) else: log.debug("Memcache disabled: %s" % username) cache_reply = None if cache_reply is None: try: bs_client = Proxy(BLOCKSTACKD_IP, BLOCKSTACKD_PORT) bs_resp = bs_client.get_name_blockchain_record(username + "." + namespace) bs_resp = bs_resp[0] except: return {} if bs_resp is None: abort(404) if 'value_hash' in bs_resp: profile_hash = bs_resp['value_hash'] profile = fetch_from_dht(profile_hash) data = format_profile(profile, username) data['owner_address'] = bs_resp['address'] if MEMCACHED_ENABLED or refresh: log.debug("Memcache set: %s" % username) mc.set("profile_" + str(username), json.dumps(data), int(time() + MEMCACHED_TIMEOUT)) else: data = {"error": "Not found"} else: data = json.loads(cache_reply) return data
def get_profile(username, refresh=False, namespace=DEFAULT_NAMESPACE): global MEMCACHED_ENABLED if refresh: log.debug("Forcing refresh: %s" % username) # refresh is on, turning off memcache MEMCACHED_ENABLED = False username = username.lower() if MEMCACHED_ENABLED: log.debug("Memcache get: %s" % username) cache_reply = mc.get("profile_" + str(username)) else: cache_reply = None if cache_reply is None: try: blockstore_client = Proxy(BLOCKSTORED_IP, BLOCKSTORED_PORT) blockstore_resp = blockstore_client.get_name_blockchain_record(username + "." + namespace) blockstore_resp = blockstore_resp[0] except: return {} if blockstore_resp is None: abort(404) if 'value_hash' in blockstore_resp: profile_hash = blockstore_resp['value_hash'] profile = fetch_from_dht(profile_hash) data = format_profile(profile, username) data['owner_address'] = blockstore_resp['address'] print blockstore_resp['address'] if MEMCACHED_ENABLED or refresh: log.debug("Memcache set: %s" % username) mc.set("profile_" + str(username), json.dumps(data), int(time() + MEMCACHED_TIMEOUT)) else: data = {"error": "Not found"} else: data = json.loads(cache_reply) return data
def fetch_from_dht(profile_hash): """ Given a @profile_hash fetch full profile JSON """ dht_client = Proxy(DHT_MIRROR_IP, DHT_MIRROR_PORT) try: dht_resp = dht_client.get(profile_hash) except: #abort(500, "Connection to DHT timed out") return {"error": "Data not saved in DHT yet."} dht_resp = dht_resp[0] if dht_resp is None: return {"error": "Data not saved in DHT yet."} return dht_resp['value']
def dontUseServer(blockstackd_server): """ Return false if server fails any tests """ from registrar.config import CONSENSUS_SERVERS from blockstack_client.proxy import BlockstackRPCClient as Proxy servers_to_check = CONSENSUS_SERVERS servers_to_check.append(blockstackd_server) consensus_hashes = [] # initialize to a very large number last_block_everyone = 2000000000 for server in servers_to_check: bs_client = Proxy(server, BLOCKSTACKD_PORT) last_block_seen = bs_client.getinfo()[0]['bitcoind_blocks'] try: last_block_processed = bs_client.getinfo()[0]['last_block'] except: last_block_processed = bs_client.getinfo()[0]['blocks'] if (last_block_seen - last_block_processed) > 10: log.debug("Server %s, seems to be lagging: (%s, %s)" % (server, last_block_seen, last_block_processed)) return True if last_block_processed < last_block_everyone: last_block_everyone = last_block_processed for server in servers_to_check: bs_client = Proxy(server, BLOCKSTACKD_PORT) consensus_hash = bs_client.get_consensus_at(last_block_everyone)[0] print consensus_hash consensus_hashes.append(consensus_hash) check_hash = consensus_hashes[0] for stored_hash in consensus_hashes: if check_hash != stored_hash: log.debug('Mismatch in consensus hashes from %s' % servers_to_check) return True # can use server, if all tests pass return False
def get_address_names(addresses): resp = {} results = [] addresses = addresses.split(',') for address in addresses: data = {} names_owned = [] invalid_address = False try: is_b58check_address(str(address)) except: data['error'] = "Invalid address" invalid_address = True if not invalid_address: bs_client = Proxy(BLOCKSTORED_IP, BLOCKSTORED_PORT) try: resp = bs_client.get_names_owned_by_address(address) names_owned = resp[0] except: pass data['address'] = address data['names'] = names_owned results.append(data) resp = {'results': results} return jsonify(resp), 200
from config import DEFAULT_SERVER, DEFAULT_PORT, DEBUG def format_response(response): try: response = response[0] except: pass return json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')) # ------------------------------ if __name__ == '__main__': c = Proxy(DEFAULT_SERVER, DEFAULT_PORT) resp = c.ping() print format_response(resp) resp = c.stats() print format_response(resp) key = {"name": "Muneeb Ali"} #resp = c.dht_get(key) #print format_response(resp)
GNU General Public License for more details. You should have received a copy of the GNU General Public License along with DHT-Mirror. If not, see <http://www.gnu.org/licenses/>. """ import json from basicrpc import Proxy from .utils import pretty_print from .config import DEFAULT_SERVER, MIRROR_TCP_PORT, DEBUG from .config import DHT_STATE_FILE dht_client = Proxy(DEFAULT_SERVER, MIRROR_TCP_PORT, timeout=30) def refresh_dht_entries(): fin = open(DHT_STATE_FILE, 'r') data = fin.read() data = json.loads(data) counter = 0 for entry in data: print '-' * 5 print "Processing key: %s" % entry['key']
try: resp = c.set(key, value) pretty_print(resp) counter += 1 print counter print '-' * 5 except Exception as e: print e print "problem %s" % entry['username'] print key print value break #if resp is None: # print"trying set" # print c.dht_set(key, value) # ------------------------------ if __name__ == '__main__': test_hash = "3b04c220530154898d02463fba83a235de184936" c = Proxy(DEFAULT_SERVER, MIRROR_TCP_PORT, timeout=30) pretty_print(c.ping()) #pretty_print(c.stats()) result = c.dht_get(test_hash) print result #pretty_print(result) #warmup_mirror()
def get_dht_client(): return Proxy(DHT_MIRROR_IP, DHT_MIRROR_PORT)
from basicrpc import Proxy from blockstore_client import client as bs_client from .config import BLOCKSTORED_IP, BLOCKSTORED_PORT from .config import DHT_MIRROR_IP, DHT_MIRROR_PORT from .config import RESOLVER_URL, RESOLVER_USERS_ENDPOINT from .utils import get_hash, config_log from .utils import pretty_dump as pprint log = config_log(__name__) # direct client, using Proxy #bs_client = Proxy(BLOCKSTORED_IP, BLOCKSTORED_PORT) dht_client = Proxy(DHT_MIRROR_IP, DHT_MIRROR_PORT) # start session using blockstore_client bs_client.session(server_host=BLOCKSTORED_IP, server_port=BLOCKSTORED_PORT) def get_bs_client(): # return Proxy(BLOCKSTORED_IP, BLOCKSTORED_PORT) return bs_client def get_dht_client(): return Proxy(DHT_MIRROR_IP, DHT_MIRROR_PORT)
""" import json from basicrpc import Proxy from config import DEFAULT_SERVER, DEFAULT_PORT, DEBUG def format_response(response): try: response = response[0] except: pass return json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')) # ------------------------------ if __name__ == '__main__': c = Proxy(DEFAULT_SERVER, DEFAULT_PORT) resp = c.ping() print format_response(resp) resp = c.stats() print format_response(resp) key = {"name": "Muneeb Ali"} #resp = c.dht_get(key) #print format_response(resp)