mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-01-12 22:43:42 +08:00
fix issue #3
This commit is contained in:
@@ -13,6 +13,10 @@ from opendig import ONS_SERVERS, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSW
|
||||
import json
|
||||
import hashlib
|
||||
from coinrpc.namecoind_server import NamecoindServer
|
||||
from multiprocessing.pool import ThreadPool
|
||||
from collections import Counter
|
||||
|
||||
SERVER_CONFIRMATION_PERCENTAGE = 60
|
||||
|
||||
#currently using namecoind for storing data (but ONS can use any blockchain)
|
||||
#---------------------------------------
|
||||
@@ -25,37 +29,37 @@ def error_reply(msg, code = -1):
|
||||
return reply
|
||||
|
||||
#-----------------------------------
|
||||
def ons_resolver(key):
|
||||
def ons_resolver(key):
|
||||
|
||||
counter = 0
|
||||
def check_server(server):
|
||||
|
||||
server = ONS_SERVERS[counter]
|
||||
try:
|
||||
namecoind = NamecoindServer(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD)
|
||||
return_data = namecoind.get_full_profile('u/' + key)
|
||||
except:
|
||||
return error_reply("Couldn't connect to namecoind")
|
||||
|
||||
data = json.dumps(return_data,sort_keys=True)
|
||||
data_hash = hashlib.md5(data).hexdigest()
|
||||
|
||||
while counter < len(ONS_SERVERS) - 1:
|
||||
counter += 1
|
||||
server = ONS_SERVERS[counter]
|
||||
try:
|
||||
namecoind = NamecoindServer(server, NAMECOIND_PORT, NAMECOIND_USER, NAMECOIND_PASSWD)
|
||||
check_data = namecoind.get_full_profile('u/' + key)
|
||||
return_data = namecoind.get_full_profile('u/' + key)
|
||||
return return_data
|
||||
except:
|
||||
return error_reply("Couldn't connect to namecoind")
|
||||
|
||||
check_data = json.dumps(check_data,sort_keys=True)
|
||||
|
||||
if data_hash != hashlib.md5(check_data).hexdigest():
|
||||
return error_reply("Data from different ONS servers doens't match")
|
||||
|
||||
return return_data
|
||||
#-----------------------------------
|
||||
pool = ThreadPool(len(ONS_SERVERS))
|
||||
|
||||
replies = pool.map(check_server, ONS_SERVERS)
|
||||
pool.close()
|
||||
pool.join()
|
||||
|
||||
data_hashes = []
|
||||
for reply in replies:
|
||||
data_hashes.append(hashlib.md5(json.dumps(reply)).hexdigest())
|
||||
|
||||
count = Counter(data_hashes)
|
||||
max_repeated_times = count.most_common()[0][1]
|
||||
|
||||
if max_repeated_times >= (SERVER_CONFIRMATION_PERCENTAGE/100.0) * len(ONS_SERVERS):
|
||||
return replies[0]
|
||||
else:
|
||||
return error_reply("Data from different ONS servers doens't match")
|
||||
|
||||
#------------------------------------
|
||||
if __name__ == "__main__":
|
||||
key = "ibrahim"
|
||||
print ons_resolver(key)
|
||||
|
||||
Reference in New Issue
Block a user