本文整理汇总了Python中dht.network.Server.loadState方法的典型用法代码示例。如果您正苦于以下问题:Python Server.loadState方法的具体用法?Python Server.loadState怎么用?Python Server.loadState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dht.network.Server
的用法示例。
在下文中一共展示了Server.loadState方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def run(*args):
TESTNET = args[0]
LOGLEVEL = args[1]
PORT = args[2]
ALLOWIP = args[3]
SSL = args[4]
RESTPORT = args[5]
WSPORT = args[6]
# database
db = Database(TESTNET)
# key generation
keys = KeyChain(db)
# logging
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log", rotateLength=15000000, maxRotatedFiles=1)
log.addObserver(FileLogObserver(logFile, level=LOGLEVEL).emit)
log.addObserver(FileLogObserver(level=LOGLEVEL).emit)
logger = Logger(system="OpenBazaard")
# NAT traversal
p = PortMapper()
p.add_port_mapping(PORT, PORT, "UDP")
logger.info("Finding NAT Type...")
response = looping_retry(stun.get_ip_info, "0.0.0.0", PORT)
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
ip_address = response[1]
port = response[2]
if response[0] == "Full Cone":
nat_type = FULL_CONE
elif response[0] == "Restric NAT":
nat_type = RESTRICTED
else:
nat_type = SYMMETRIC
def on_bootstrap_complete(resp):
logger.info("bootstrap complete")
mserver.get_messages(mlistener)
task.LoopingCall(check_unfunded_for_payment, db, libbitcoin_client, nlistener, TESTNET).start(600)
protocol = OpenBazaarProtocol((ip_address, port), nat_type, testnet=TESTNET,
relaying=True if nat_type == FULL_CONE else False)
# kademlia
storage = ForgetfulStorage() if TESTNET else PersistentStorage(db.get_database_path())
relay_node = None
if nat_type != FULL_CONE:
for seed in SEEDS:
try:
relay_node = (socket.gethostbyname(seed[0].split(":")[0]),
28469 if TESTNET else 18469)
break
except socket.gaierror:
pass
try:
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, db,
nat_type, relay_node, on_bootstrap_complete, storage)
except Exception:
node = Node(keys.guid, ip_address, port, keys.verify_key.encode(),
relay_node, nat_type, Profile(db).get().vendor)
protocol.relay_node = node.relay_node
kserver = Server(node, db, keys.signing_key, KSIZE, ALPHA, storage=storage)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed(SEEDS)).addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
looping_retry(reactor.listenUDP, port, protocol)
interface = "0.0.0.0" if ALLOWIP not in ("127.0.0.1", "0.0.0.0") else ALLOWIP
# websockets api
ws_api = WSFactory(mserver, kserver, only_ip=ALLOWIP)
if SSL:
reactor.listenSSL(WSPORT, WebSocketFactory(ws_api),
ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(WSPORT, WebSocketFactory(ws_api), interface=interface)
# rest api
rest_api = RestAPI(mserver, kserver, protocol, only_ip=ALLOWIP)
if SSL:
reactor.listenSSL(RESTPORT, rest_api, ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(RESTPORT, rest_api, interface=interface)
# blockchain
if TESTNET:
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVER_TESTNET, log=Logger(service="LibbitcoinClient"))
else:
#.........这里部分代码省略.........
示例2: run
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def run(*args):
TESTNET = args[0]
# database
db = Database(TESTNET)
# key generation
keys = KeyChain(db)
# logging
# TODO: prune this log file and prevent it from getting too large?
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log")
log.addObserver(log.FileLogObserver(logFile).emit)
log.startLogging(sys.stdout)
# stun
# TODO: accept port from command line
port = 18467 if not TESTNET else 28467
print "Finding NAT Type.."
# TODO: maintain a list of backup STUN servers and try them if ours fails
response = stun.get_ip_info(stun_host="seed.openbazaar.org", source_port=port)
print "%s on %s:%s" % (response[0], response[1], response[2])
ip_address = response[1]
port = response[2]
# TODO: try UPnP if restricted NAT
# TODO: maintain open connection to seed node if STUN/UPnP fail
# TODO: use TURN if symmetric NAT
def on_bootstrap_complete(resp):
mlistener = MessageListenerImpl(ws_factory, db)
mserver.get_messages(mlistener)
mserver.protocol.add_listener(mlistener)
nlistener = NotificationListenerImpl(ws_factory, db)
mserver.protocol.add_listener(nlistener)
# TODO: after bootstrap run through any pending contracts and see if the bitcoin address
# has been funded, if not listen on the address and start the 10 minute delete timer.
protocol = OpenBazaarProtocol((ip_address, port), testnet=TESTNET)
# kademlia
node = Node(keys.guid, ip_address, port, signed_pubkey=keys.guid_signed_pubkey)
try:
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, db,
on_bootstrap_complete, storage=PersistentStorage(db.DATABASE))
except Exception:
kserver = Server(node, db, KSIZE, ALPHA, storage=PersistentStorage(db.DATABASE))
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(
kserver.querySeed("seed.openbazaar.org:8080",
"5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117"))\
.addCallback(on_bootstrap_complete)
# TODO: load seeds from config file
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
reactor.listenUDP(port, protocol)
# websockets api
ws_factory = WSFactory("ws://127.0.0.1:18466", mserver, kserver)
ws_factory.protocol = WSProtocol
ws_factory.setProtocolOptions(allowHixie76=True)
listenWS(ws_factory)
webdir = File(".")
web = Site(webdir)
reactor.listenTCP(9000, web, interface="127.0.0.1")
# rest api
api = OpenBazaarAPI(mserver, kserver, protocol)
site = Site(api, timeout=None)
reactor.listenTCP(18469, site, interface="127.0.0.1")
# TODO: add optional SSL on rest and websocket servers
# blockchain
# TODO: listen on the libbitcoin heartbeat port instead fetching height
def height_fetched(ec, height):
# TODO: re-broadcast any unconfirmed txs in the db using height to find confirmation status
print "Libbitcoin server online"
try:
timeout.cancel()
except Exception:
pass
def timeout(client):
print "Libbitcoin server offline"
client = None
if TESTNET:
libbitcoin_client = LibbitcoinClient("tcp://libbitcoin2.openbazaar.org:9091")
else:
#.........这里部分代码省略.........
示例3: start_server
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def start_server(keychain, first_startup=False):
# logging
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log", rotateLength=15000000, maxRotatedFiles=1)
log.addObserver(FileLogObserver(logFile, level="debug").emit)
log.addObserver(FileLogObserver(level="debug").emit)
logger = Logger(system="Httpseed")
if os.path.isfile(DATA_FOLDER + 'keys.pickle'):
keys = pickle.load(open(DATA_FOLDER + "keys.pickle", "r"))
signing_key_hex = keys["signing_privkey"]
signing_key = nacl.signing.SigningKey(signing_key_hex, encoder=nacl.encoding.HexEncoder)
else:
signing_key = nacl.signing.SigningKey.generate()
keys = {
'signing_privkey': signing_key.encode(encoder=nacl.encoding.HexEncoder),
'signing_pubkey': signing_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)
}
pickle.dump(keys, open(DATA_FOLDER + "keys.pickle", "wb"))
# Stun
port = 18467 if not TESTNET else 28467
logger.info("Finding NAT Type...")
response = stun.get_ip_info(stun_host="stun.l.google.com", source_port=port, stun_port=19302)
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
ip_address = response[1]
port = response[2]
# Start the kademlia server
this_node = Node(keychain.guid, ip_address, port,
keychain.verify_key.encode(), None, objects.FULL_CONE, False)
protocol = OpenBazaarProtocol(db, (ip_address, port), objects.FULL_CONE, testnet=TESTNET, relaying=True)
try:
kserver = Server.loadState('cache.pickle', ip_address, port, protocol, db, objects.FULL_CONE, None)
except Exception:
kserver = Server(this_node, db, keychain.signing_key)
kserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(kserver.protocol)
kserver.saveStateRegularly('cache.pickle', 10)
reactor.listenUDP(port, protocol)
class WebResource(resource.Resource):
def __init__(self, kserver_r):
resource.Resource.__init__(self)
self.kserver = kserver_r
self.nodes = {}
for bucket in self.kserver.protocol.router.buckets:
for node in bucket.getNodes():
self.nodes[(node.ip, node.port)] = node
self.nodes[(this_node.ip, this_node.port)] = this_node
loopingCall = task.LoopingCall(self.crawl)
loopingCall.start(900, True)
def crawl(self):
def gather_results(result):
for proto in result:
n = objects.Node()
try:
n.ParseFromString(proto)
node = Node(n.guid, n.nodeAddress.ip, n.nodeAddress.port, n.signedPublicKey,
None if not n.HasField("relayAddress") else
(n.relayAddress.ip, n.relayAddress.port),
n.natType,
n.vendor)
self.nodes[(node.ip, node.port)] = node
except Exception:
pass
def start_crawl(results):
for node, result in results.items():
if not result[0]:
del self.nodes[(node.ip, node.port)]
node = Node(digest(random.getrandbits(255)))
nearest = self.kserver.protocol.router.findNeighbors(node)
spider = NodeSpiderCrawl(self.kserver.protocol, node, nearest, 100, 4)
spider.find().addCallback(gather_results)
ds = {}
for bucket in self.kserver.protocol.router.buckets:
for node in bucket.getNodes():
self.nodes[(node.ip, node.port)] = node
for node in self.nodes.values():
if node.id != this_node.id:
ds[node] = self.kserver.protocol.callPing(node)
deferredDict(ds).addCallback(start_crawl)
def getChild(self, child, request):
return self
def render_GET(self, request):
nodes = self.nodes.values()
shuffle(nodes)
logger.info("Received a request for nodes, responding...")
if "format" in request.args:
if request.args["format"][0] == "json":
json_list = []
if "type" in request.args and request.args["type"][0] == "vendors":
for node in nodes:
#.........这里部分代码省略.........
示例4: run
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def run(*args):
TESTNET = args[0]
# Create the database
db = Database(testnet=TESTNET)
# logging
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log")
log.addObserver(log.FileLogObserver(logFile).emit)
log.startLogging(sys.stdout)
# Load the keys
keychain = KeyChain(db)
if os.path.isfile(DATA_FOLDER + 'keys.pickle'):
keys = pickle.load(open(DATA_FOLDER + "keys.pickle", "r"))
signing_key_hex = keys["signing_privkey"]
signing_key = nacl.signing.SigningKey(signing_key_hex, encoder=nacl.encoding.HexEncoder)
else:
signing_key = nacl.signing.SigningKey.generate()
keys = {
'signing_privkey': signing_key.encode(encoder=nacl.encoding.HexEncoder),
'signing_pubkey': signing_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)
}
pickle.dump(keys, open(DATA_FOLDER + "keys.pickle", "wb"))
# Stun
port = 18467 if not TESTNET else 28467
print "Finding NAT Type.."
response = stun.get_ip_info(stun_host="stun.l.google.com", source_port=port, stun_port=19302)
print "%s on %s:%s" % (response[0], response[1], response[2])
ip_address = response[1]
port = response[2]
# Start the kademlia server
this_node = Node(keychain.guid, ip_address, port, keychain.guid_signed_pubkey, vendor=Profile(db).get().vendor)
protocol = OpenBazaarProtocol((ip_address, port), testnet=TESTNET)
try:
kserver = Server.loadState('cache.pickle', ip_address, port, protocol, db)
except Exception:
kserver = Server(this_node, db)
kserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(kserver.protocol)
kserver.saveStateRegularly('cache.pickle', 10)
# start the market server
mserver = network.Server(kserver, keychain.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
reactor.listenUDP(port, protocol)
class WebResource(resource.Resource):
def __init__(self, kserver_r):
resource.Resource.__init__(self)
self.kserver = kserver_r
self.nodes = {}
for bucket in self.kserver.protocol.router.buckets:
for node in bucket.getNodes():
self.nodes[(node.ip, node.port)] = node
self.nodes[(this_node.ip, this_node.port)] = this_node
loopingCall = task.LoopingCall(self.crawl)
loopingCall.start(60, True)
def crawl(self):
def gather_results(result):
for proto in result:
n = objects.Node()
try:
n.ParseFromString(proto)
node = Node(n.guid, n.ip, n.port, n.signedPublicKey, n.vendor)
self.nodes[(node.ip, node.port)] = node
except Exception:
pass
def start_crawl(results):
for node, result in results.items():
if not result[0]:
del self.nodes[(node.ip, node.port)]
node = Node(digest(random.getrandbits(255)))
nearest = self.kserver.protocol.router.findNeighbors(node)
spider = NodeSpiderCrawl(self.kserver.protocol, node, nearest, 100, 4)
spider.find().addCallback(gather_results)
ds = {}
for bucket in self.kserver.protocol.router.buckets:
for node in bucket.getNodes():
self.nodes[(node.ip, node.port)] = node
for node in self.nodes.values():
if node.id != this_node.id:
ds[node] = self.kserver.protocol.callPing(node)
deferredDict(ds).addCallback(start_crawl)
def getChild(self, child, request):
return self
def render_GET(self, request):
nodes = self.nodes.values()
#.........这里部分代码省略.........
示例5: GUID
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
if os.path.isfile(DATA_FOLDER + 'keys.pickle'):
keys = pickle.load(open(DATA_FOLDER + "keys.pickle", "r"))
g = keys["guid"]
else:
print "Generating GUID, stand by..."
g = GUID()
keys = {'guid': g}
pickle.dump(keys, open(DATA_FOLDER + "keys.pickle", "wb"))
protocol = OpenBazaarProtocol((ip_address, port))
# kademlia
node = Node(g.guid, signed_pubkey=g.signed_pubkey)
if os.path.isfile(DATA_FOLDER + 'cache.pickle'):
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol)
else :
kserver = Server(node)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed("162.213.253.147:8080", "909b4f614ec4fc8c63aab83b91bc620d7a238600bf256472e968fdafce200128"))
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
reactor.listenUDP(18467, protocol)
示例6: run
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def run(*args):
TESTNET = args[0]
SSL = args[5]
# database
db = Database(TESTNET)
# key generation
keys = KeyChain(db)
# logging
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log", rotateLength=15000000, maxRotatedFiles=1)
log.addObserver(FileLogObserver(logFile, level=args[1]).emit)
log.addObserver(FileLogObserver(level=args[1]).emit)
logger = Logger(system="OpenBazaard")
# NAT traversal
port = args[2]
PortMapper().add_port_mapping(port, port, 'UDP')
logger.info("Finding NAT Type...")
while True:
try:
response = stun.get_ip_info(source_port=port)
break
except Exception:
pass
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
ip_address = response[1]
port = response[2]
# TODO: use TURN if symmetric NAT
def on_bootstrap_complete(resp):
logger.info("bootstrap complete, downloading outstanding messages...")
mserver.get_messages(mlistener)
# TODO: ping seed node to establish connection if not full cone NAT
# TODO: after bootstrap run through any pending contracts and see if the bitcoin address
# has been funded, if not listen on the address and start the 10 minute delete timer.
protocol = OpenBazaarProtocol((ip_address, port), response[0], testnet=TESTNET)
# kademlia
node = Node(keys.guid, ip_address, port, signed_pubkey=keys.guid_signed_pubkey, vendor=Profile(db).get().vendor)
storage = ForgetfulStorage() if TESTNET else PersistentStorage(db.DATABASE)
try:
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, db,
on_bootstrap_complete, storage=storage)
except Exception:
kserver = Server(node, db, KSIZE, ALPHA, storage=storage)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed(SEED)).addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
reactor.listenUDP(port, protocol)
class OnlyIP(Site):
def __init__(self, resource, ip, timeout=60 * 60 * 1):
self.ip = ip
Site.__init__(self, resource, timeout=timeout)
def buildProtocol(self, addr):
if addr.host == self.ip:
return Site.buildProtocol(self, addr)
return None
# websockets api
if SSL:
ws_factory = WSFactory("wss://127.0.0.1:18466", mserver, kserver)
contextFactory = ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT)
ws_factory.protocol = WSProtocol
ws_factory.setProtocolOptions(allowHixie76=True)
listenWS(ws_factory, contextFactory)
else:
ws_factory = WSFactory("ws://127.0.0.1:18466", mserver, kserver)
ws_factory.protocol = WSProtocol
ws_factory.setProtocolOptions(allowHixie76=True)
listenWS(ws_factory)
webdir = File(".")
if args[4] != "127.0.0.1" and args[4] != "0.0.0.0":
ws_interface = "0.0.0.0"
web = OnlyIP(webdir, args[4])
else:
ws_interface = args[4]
web = Site(webdir)
reactor.listenTCP(18465, web, interface=ws_interface)
# rest api
api = OpenBazaarAPI(mserver, kserver, protocol)
#.........这里部分代码省略.........
示例7: run
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def run(*args):
TESTNET = args[0]
LOGLEVEL = args[1]
PORT = args[2]
ALLOWIP = args[3]
SSL = args[4]
RESTPORT = args[5]
WSPORT = args[6]
# database
db = Database(TESTNET)
# key generation
keys = KeyChain(db)
# logging
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log", rotateLength=15000000, maxRotatedFiles=1)
log.addObserver(FileLogObserver(logFile, level=LOGLEVEL).emit)
log.addObserver(FileLogObserver(level=LOGLEVEL).emit)
logger = Logger(system="OpenBazaard")
# NAT traversal
p = PortMapper()
threading.Thread(target=p.add_port_mapping, args=(PORT, PORT, "UDP")).start()
logger.info("Finding NAT Type...")
while True:
# sometimes the stun server returns a code the client
# doesn't understand so we have to try again
try:
response = stun.get_ip_info(source_port=PORT)
break
except Exception:
pass
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
nat_type = response[0]
ip_address = response[1]
port = response[2]
# TODO: use TURN if symmetric NAT
def on_bootstrap_complete(resp):
logger.info("bootstrap complete")
mserver.get_messages(mlistener)
task.LoopingCall(check_unfunded_for_payment, db, libbitcoin_client, nlistener, TESTNET).start(600)
protocol = OpenBazaarProtocol((ip_address, port), response[0], testnet=TESTNET)
# kademlia
node = Node(keys.guid, ip_address, port, signed_pubkey=keys.guid_signed_pubkey, vendor=Profile(db).get().vendor)
storage = ForgetfulStorage() if TESTNET else PersistentStorage(db.DATABASE)
try:
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, db,
on_bootstrap_complete, storage=storage)
except Exception:
kserver = Server(node, db, KSIZE, ALPHA, storage=storage)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed(SEED)).addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
if nat_type != "Full Cone":
kserver.protocol.ping(SEED_NODE_TESTNET if TESTNET else SEED_NODE)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
reactor.listenUDP(port, protocol)
class OnlyIP(Site):
def __init__(self, resource, ip, timeout=60 * 60 * 1):
self.ip = ip
Site.__init__(self, resource, timeout=timeout)
def buildProtocol(self, addr):
if addr.host == self.ip:
return Site.buildProtocol(self, addr)
return None
# websockets api
if SSL:
ws_factory = WSFactory("wss://127.0.0.1:" + str(WSPORT), mserver, kserver, only_ip=ALLOWIP)
contextFactory = ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT)
ws_factory.protocol = WSProtocol
listenWS(ws_factory, contextFactory)
else:
ws_factory = WSFactory("ws://127.0.0.1:" + str(WSPORT), mserver, kserver, only_ip=ALLOWIP)
ws_factory.protocol = WSProtocol
listenWS(ws_factory)
if ALLOWIP != "127.0.0.1" and ALLOWIP != "0.0.0.0":
ws_interface = "0.0.0.0"
else:
ws_interface = ALLOWIP
webdir = File(".")
web = Site(webdir)
#.........这里部分代码省略.........
示例8: start_server
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def start_server(keys, first_startup=False):
# logging
logFile = logfile.LogFile.fromFullPath(
os.path.join(DATA_FOLDER, "debug.log"),
rotateLength=15000000,
maxRotatedFiles=1)
log.addObserver(FileLogObserver(logFile, level=LOGLEVEL).emit)
log.addObserver(FileLogObserver(level=LOGLEVEL).emit)
logger = Logger(system="OpenBazaard")
# NAT traversal
p = PortMapper()
p.add_port_mapping(PORT, PORT, "UDP")
logger.info("Finding NAT Type...")
response = looping_retry(stun.get_ip_info, "0.0.0.0", PORT)
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
ip_address = response[1]
port = response[2]
if response[0] == "Full Cone":
nat_type = FULL_CONE
elif response[0] == "Restric NAT":
nat_type = RESTRICTED
else:
nat_type = SYMMETRIC
def on_bootstrap_complete(resp):
logger.info("bootstrap complete")
task.LoopingCall(mserver.get_messages, mlistener).start(3600)
task.LoopingCall(check_unfunded_for_payment, db, libbitcoin_client, nlistener, TESTNET).start(600)
task.LoopingCall(rebroadcast_unconfirmed, db, libbitcoin_client, TESTNET).start(600)
protocol = OpenBazaarProtocol(db, (ip_address, port), nat_type, testnet=TESTNET,
relaying=True if nat_type == FULL_CONE else False)
# kademlia
SEED_URLS = SEEDS_TESTNET if TESTNET else SEEDS
relay_node = None
if nat_type != FULL_CONE:
for seed in SEED_URLS:
try:
relay_node = (socket.gethostbyname(seed[0].split(":")[0]),
28469 if TESTNET else 18469)
break
except socket.gaierror:
pass
try:
kserver = Server.loadState(os.path.join(DATA_FOLDER, 'cache.pickle'), ip_address, port, protocol, db,
nat_type, relay_node, on_bootstrap_complete, storage)
except Exception:
node = Node(keys.guid, ip_address, port, keys.verify_key.encode(),
relay_node, nat_type, Profile(db).get().vendor)
protocol.relay_node = node.relay_node
kserver = Server(node, db, keys.signing_key, KSIZE, ALPHA, storage=storage)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed(SEED_URLS)).addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(os.path.join(DATA_FOLDER, 'cache.pickle'), 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
looping_retry(reactor.listenUDP, port, protocol)
interface = "0.0.0.0" if ALLOWIP != ["127.0.0.1"] else "127.0.0.1"
# websockets api
authenticated_sessions = []
ws_api = WSFactory(mserver, kserver, only_ip=ALLOWIP)
ws_factory = AuthenticatedWebSocketFactory(ws_api)
ws_factory.authenticated_sessions = authenticated_sessions
ws_factory.protocol = AuthenticatedWebSocketProtocol
if SSL:
reactor.listenSSL(WSPORT, ws_factory,
ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(WSPORT, ws_factory, interface=interface)
# rest api
rest_api = RestAPI(mserver, kserver, protocol, username, password,
authenticated_sessions, only_ip=ALLOWIP)
if SSL:
reactor.listenSSL(RESTPORT, rest_api,
ChainedOpenSSLContextFactory(SSL_KEY, SSL_CERT), interface=interface)
else:
reactor.listenTCP(RESTPORT, rest_api, interface=interface)
# blockchain
if TESTNET:
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVERS_TESTNET, log=Logger(service="LibbitcoinClient"))
else:
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVERS, log=Logger(service="LibbitcoinClient"))
heartbeat_server.libbitcoin = libbitcoin_client
# listeners
#.........这里部分代码省略.........
示例9: open
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
'signing_privkey': signing_key.encode(encoder=nacl.encoding.HexEncoder),
'signing_pubkey': signing_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)
}
pickle.dump(keys, open("keys.pickle", "wb"))
# Stun
response = stun.get_ip_info(stun_host="stun.l.google.com", source_port=0, stun_port=19302)
ip_address = response[1]
port = 18467
# Start the kademlia server
this_node = Node(keychain.guid, ip_address, port, keychain.guid_signed_pubkey)
protocol = OpenBazaarProtocol((ip_address, port))
if os.path.isfile('cache.pickle'):
kserver = Server.loadState('cache.pickle', ip_address, port, protocol)
else:
kserver = Server(this_node)
kserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(kserver.protocol)
kserver.saveStateRegularly('cache.pickle', 10)
# start the market server
mserver = network.Server(kserver, keychain.signing_key)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
udpserver = internet.UDPServer(18467, protocol)
udpserver.setServiceParent(application)
示例10: KeyChain
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
keys = KeyChain()
def on_bootstrap_complete(resp):
mlistener = MessageListenerImpl(ws_factory)
mserver.get_messages(mlistener)
mserver.protocol.add_listener(mlistener)
nlistener = NotificationListenerImpl(ws_factory)
mserver.protocol.add_listener(nlistener)
protocol = OpenBazaarProtocol((ip_address, port))
# kademlia
node = Node(keys.guid, ip_address, port, signed_pubkey=keys.guid_signed_pubkey)
if os.path.isfile(DATA_FOLDER + 'cache.pickle'):
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol,
on_bootstrap_complete, storage=PersistentStorage(DATABASE))
else:
kserver = Server(node, dht.constants.KSIZE, dht.constants.ALPHA, storage=PersistentStorage(DATABASE))
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(
kserver.querySeed("162.213.253.147:8080",
"5b56c8daeb3b37c8a9b47be6102fa43b9f069f58dcb57475984041b26c99e389"))\
.addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
示例11: run
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
def run(*args):
TESTNET = args[0]
# database
db = Database(TESTNET)
# key generation
keys = KeyChain(db)
# logging
# TODO: prune this log file and prevent it from getting too large?
logFile = logfile.LogFile.fromFullPath(DATA_FOLDER + "debug.log")
log.addObserver(FileLogObserver(logFile, level=args[1]).emit)
log.addObserver(FileLogObserver(level=args[1]).emit)
logger = Logger(system="OpenBazaard")
# NAT traversal
port = args[2]
PortMapper().add_port_mapping(port, port, 'UDP')
logger.info("Finding NAT Type...")
while True:
try:
response = stun.get_ip_info(source_port=port)
break
except Exception:
pass
logger.info("%s on %s:%s" % (response[0], response[1], response[2]))
ip_address = response[1]
port = response[2]
# TODO: use TURN if symmetric NAT
def on_bootstrap_complete(resp):
logger.info("bootstrap complete, downloading outstanding messages...")
mlistener = MessageListenerImpl(ws_factory, db)
mserver.get_messages(mlistener)
mserver.protocol.add_listener(mlistener)
nlistener = NotificationListenerImpl(ws_factory, db)
mserver.protocol.add_listener(nlistener)
# TODO: ping seed node to establish connection if not full cone NAT
# TODO: after bootstrap run through any pending contracts and see if the bitcoin address
# has been funded, if not listen on the address and start the 10 minute delete timer.
protocol = OpenBazaarProtocol((ip_address, port), response[0], testnet=TESTNET)
# kademlia
node = Node(keys.guid, ip_address, port, signed_pubkey=keys.guid_signed_pubkey, vendor=Profile(db).get().vendor)
storage = ForgetfulStorage() if TESTNET else PersistentStorage(db.DATABASE)
try:
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, db,
on_bootstrap_complete, storage=storage)
except Exception:
kserver = Server(node, db, KSIZE, ALPHA, storage=storage)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(
kserver.querySeed("seed.openbazaar.org:8080",
"5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117"))\
.addCallback(on_bootstrap_complete)
# TODO: load seeds from config file
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key, db)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
reactor.listenUDP(port, protocol)
# websockets api
ws_factory = WSFactory("ws://127.0.0.1:18466", mserver, kserver)
ws_factory.protocol = WSProtocol
ws_factory.setProtocolOptions(allowHixie76=True)
listenWS(ws_factory)
webdir = File(".")
web = Site(webdir)
reactor.listenTCP(9000, web, interface=args[4])
# rest api
api = OpenBazaarAPI(mserver, kserver, protocol)
site = Site(api, timeout=None)
reactor.listenTCP(18469, site, interface=args[3])
# TODO: add optional SSL on rest and websocket servers
# blockchain
# TODO: listen on the libbitcoin heartbeat port instead fetching height
def height_fetched(ec, height):
# TODO: re-broadcast any unconfirmed txs in the db using height to find confirmation status
logger.info("Libbitcoin server online")
try:
timeout.cancel()
except Exception:
pass
#.........这里部分代码省略.........
示例12: KeyChain
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
keys = KeyChain()
def on_bootstrap_complete(resp):
mlistener = MessageListenerImpl(ws_factory)
mserver.get_messages(mlistener)
mserver.protocol.add_listener(mlistener)
nlistener = NotificationListenerImpl(ws_factory)
mserver.protocol.add_listener(nlistener)
protocol = OpenBazaarProtocol((ip_address, port))
# kademlia
node = Node(keys.guid, ip_address, port, signed_pubkey=keys.guid_signed_pubkey)
if os.path.isfile(DATA_FOLDER + 'cache.pickle'):
kserver = Server.loadState(DATA_FOLDER + 'cache.pickle', ip_address, port, protocol, on_bootstrap_complete)
else:
kserver = Server(node, dht.constants.KSIZE, dht.constants.ALPHA)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(
kserver.querySeed("162.213.253.147:8080",
"5b56c8daeb3b37c8a9b47be6102fa43b9f069f58dcb57475984041b26c99e389"))\
.addCallback(on_bootstrap_complete)
kserver.saveStateRegularly(DATA_FOLDER + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
# market
mserver = network.Server(kserver, keys.signing_key)
mserver.protocol.connect_multiplexer(protocol)
protocol.register_processor(mserver.protocol)
示例13: GUID
# 需要导入模块: from dht.network import Server [as 别名]
# 或者: from dht.network.Server import loadState [as 别名]
if os.path.isfile(datafolder + 'keys.pickle'):
keys = pickle.load(open(datafolder + "keys.pickle", "r"))
g = keys["guid"]
else:
print "Generating GUID, stand by..."
g = GUID()
keys = {'guid': g}
pickle.dump(keys, open(datafolder + "keys.pickle", "wb"))
protocol = OpenBazaarProtocol((ip_address, port))
# kademlia
node = Node(g.guid, signed_pubkey=g.signed_pubkey)
if os.path.isfile(datafolder + 'cache.pickle'):
kserver = Server.loadState(datafolder + 'cache.pickle', ip_address, port, protocol)
else :
kserver = Server(node)
kserver.protocol.connect_multiplexer(protocol)
kserver.bootstrap(kserver.querySeed("162.213.253.147:8080", "909b4f614ec4fc8c63aab83b91bc620d7a238600bf256472e968fdafce200128"))
kserver.saveStateRegularly(datafolder + 'cache.pickle', 10)
protocol.register_processor(kserver.protocol)
reactor.listenUDP(18467, protocol)
# RPC-Server
class RPCCalls(jsonrpc.JSONRPC):
def jsonrpc_getpubkey(self):
return hexlify(g.signed_pubkey)
def jsonrpc_getinfo(self):