Skip to content

Commit

Permalink
implement network protocol version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pdxwebdev committed Jul 10, 2021
1 parent 2459c69 commit fbb2d3f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 114 deletions.
14 changes: 6 additions & 8 deletions yadacoin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self):
self.init_service_providers()
self.init_groups()
self.init_peer()
self.config.app_log.info("Node: {}:{}".format(self.config.peer_host, self.config.peer_port))
if 'pool' in self.config.modes:
self.init_pool()
if 'web' in self.config.modes:
Expand Down Expand Up @@ -289,20 +290,18 @@ async def background_cache_validator(self):
self.config.app_log.error(format_exc())

def configure_logging(self):
ch = logging.StreamHandler(stdout)
ch.setLevel(logging.INFO)
if options.debug:
ch.setLevel(logging.DEBUG)
# tornado.log.enable_pretty_logging()
self.config.app_log = logging.getLogger("tornado.application")
tornado.log.enable_pretty_logging(logger=self.config.app_log)
# app_log.addHandler(ch)
logfile = path.abspath("yada_app.log")
# Rotate log after reaching 512K, keep 5 old copies.
rotateHandler = RotatingFileHandler(logfile, "a", 512 * 1024, 5)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
rotateHandler.setFormatter(formatter)
self.config.app_log.addHandler(rotateHandler)
self.config.app_log.setLevel(logging.INFO)
if self.config.debug:
self.config.app_log.setLevel(logging.DEBUG)

self.access_log = logging.getLogger("tornado.access")
tornado.log.enable_pretty_logging()
Expand Down Expand Up @@ -442,8 +441,6 @@ def init_http(self):
self.config.app_log.info("API: http://{}:{}".format(self.config.serve_host, self.config.serve_port))
if 'web' in self.config.modes:
self.config.app_log.info("Wallet: http://{}:{}/app".format(self.config.serve_host, self.config.serve_port))
if 'node' in self.config.modes:
self.config.app_log.info("Node: {}:{}".format(self.config.peer_host, self.config.peer_port))
if os.path.exists(path.join(path.dirname(__file__), '..', 'templates')):
template_path = path.join(path.dirname(__file__), '..', 'templates')
else:
Expand Down Expand Up @@ -492,7 +489,8 @@ def init_peer(self):
'peer_type': self.config.peer_type,
'http_host': self.config.ssl['common_name'] if isinstance(self.config.ssl, dict) else self.config.peer_host,
'http_port': self.config.ssl['port'] if isinstance(self.config.ssl, dict) else self.config.serve_port,
'secure': isinstance(self.config.ssl, dict)
'secure': isinstance(self.config.ssl, dict),
'protocol_version': 2
}

if my_peer.get('peer_type') == 'seed':
Expand Down
2 changes: 1 addition & 1 deletion yadacoin/core/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ async def build_backward_from_block_to_fork(self, block, blocks, stream=None, de

retrace_consensus_block = [x async for x in self.get_previous_consensus_block(block, stream)]
if not retrace_consensus_block:
return blocks, True
return blocks, False

retrace_consensus_block = retrace_consensus_block[0]

Expand Down
38 changes: 30 additions & 8 deletions yadacoin/core/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(
http_host=None,
http_port=None,
secure=None,
protocol_version=1
protocol_version=2
):
self.host = host
self.port = port
Expand All @@ -50,7 +50,8 @@ def from_dict(cls, peer, is_me=False):
seed_gateway=peer.get('seed_gateway'),
http_host=peer.get('http_host'),
http_port=peer.get('http_port'),
secure=peer.get('secure')
secure=peer.get('secure'),
protocol_version=peer.get('protocol_version', 1)
)
return inst

Expand Down Expand Up @@ -165,7 +166,8 @@ def to_dict(self):
'seed_gateway': self.seed_gateway,
'http_host': self.http_host,
'http_port': self.http_port,
'secure': self.secure
'secure': self.secure,
'protocol_version': self.protocol_version
}

def to_string(self):
Expand Down Expand Up @@ -520,15 +522,25 @@ class Peers:
@classmethod
def get_seeds(cls):
return OrderedDict({x.identity.username_signature: x for x in [
# Seed.from_dict({
# 'host': '34.237.46.10',
# 'port': 8000,
# 'identity': {
# "username": "",
# "username_signature": "MEUCIQCP+rF5R4sZ7pHJCBAWHxARLg9GN4dRw+/pobJ0MPmX3gIgX0RD4OxhSS9KPJTUonYI1Tr+ZI2N9uuoToZo1RGOs2M=",
# "public_key": "02fa9550f57055c96c7ce4c6c9cd1411856beba5c7d5a07417e980a39aa03da3dc"
# },
# "seed_gateway": "MEQCIHONdT7i8K+ZTzv3PHyPAhYkaksoh6FxEJUmPLmXZqFPAiBHOnt1CjgMtNzCGdBk/0S/oikPzJVys32bgThxXtAbgQ=="
# }),
Seed.from_dict({
'host': '34.237.46.10',
'host': '0.0.0.0',
'port': 8000,
'identity': {
"username": "",
"username_signature": "MEUCIQCP+rF5R4sZ7pHJCBAWHxARLg9GN4dRw+/pobJ0MPmX3gIgX0RD4OxhSS9KPJTUonYI1Tr+ZI2N9uuoToZo1RGOs2M=",
"public_key": "02fa9550f57055c96c7ce4c6c9cd1411856beba5c7d5a07417e980a39aa03da3dc"
"username": "seed_A",
"username_signature": "MEUCIQC3slOHQ0AgPSyFeas/mxMrmJuF5+itfpxSFAERAjyr4wIgCBMuSOEJnisJ7//Y019vYhIWCWvzvCnfXZRxfbrt2SM=",
"public_key": "0286707b29746a434ead4ab94af2d7758d4ae8aaa12fdad9ab42ce3952a8ef798f"
},
"seed_gateway": "MEQCIHONdT7i8K+ZTzv3PHyPAhYkaksoh6FxEJUmPLmXZqFPAiBHOnt1CjgMtNzCGdBk/0S/oikPzJVys32bgThxXtAbgQ=="
"seed_gateway": "MEQCIEvShxHewQt9u/4+WlcjSubCfsjOmvq8bRoU6t/LGmdLAiAQyr5op3AZj58NzRDthvq7bEouwHhEzis5ZYKlE6D0HA=="
}),
]})

Expand All @@ -545,6 +557,16 @@ def get_seed_gateways(cls):
},
"seed": "MEUCIQCP+rF5R4sZ7pHJCBAWHxARLg9GN4dRw+/pobJ0MPmX3gIgX0RD4OxhSS9KPJTUonYI1Tr+ZI2N9uuoToZo1RGOs2M="
}),
# SeedGateway.from_dict({
# 'host': '0.0.0.0',
# 'port': 8002,
# 'identity': {
# "username": "seed_gateway_A",
# "username_signature": "MEQCIEvShxHewQt9u/4+WlcjSubCfsjOmvq8bRoU6t/LGmdLAiAQyr5op3AZj58NzRDthvq7bEouwHhEzis5ZYKlE6D0HA==",
# "public_key": "03e8b4651a1e794998c265545facbab520131cdddaea3da304a36279b1d334dfb1"
# },
# "seed": "MEUCIQC3slOHQ0AgPSyFeas/mxMrmJuF5+itfpxSFAERAjyr4wIgCBMuSOEJnisJ7//Y019vYhIWCWvzvCnfXZRxfbrt2SM="
# }),
]})

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions yadacoin/tcpsocket/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def handle_stream(self, stream, address):
data = await stream.read_until(b"\n")
body = json.loads(data)
method = body.get('method')
if 'result' in body and stream.peer.protocol_version > 1:
if 'result' in body:
if method in REQUEST_RESPONSE_MAP:
if body['id'] in stream.message_queue.get(REQUEST_RESPONSE_MAP[method], {}):
del stream.message_queue[REQUEST_RESPONSE_MAP[method]][body['id']]
Expand Down Expand Up @@ -163,7 +163,7 @@ async def wait_for_data(self, stream):
while True:
try:
body = json.loads(await stream.read_until(b"\n"))
if 'result' in body and stream.peer.protocol_version > 1:
if 'result' in body:
if body['method'] in REQUEST_RESPONSE_MAP:
if body['id'] in stream.message_queue.get(REQUEST_RESPONSE_MAP[body['method']], {}):
del stream.message_queue[REQUEST_RESPONSE_MAP[body['method']]][body['id']]
Expand Down

0 comments on commit fbb2d3f

Please sign in to comment.