Use newer version of squeaklib with new inv struct (#1198)

* Use newer version of squeaklib with new inv struct

* Refactor peer message handler

* Use constants for inv types from new version of squeaklib
This commit is contained in:
Jonathan Zernik 2021-09-05 16:19:14 -07:00 committed by GitHub
parent c9b88ffd4a
commit 49ed12e84a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 50 deletions

View file

@ -4,4 +4,4 @@ grpcio
grpcio-tools
importlib_resources==1.4.0
pytest
squeakpy==0.7.1
squeakpy==0.7.3

View file

@ -15,5 +15,5 @@ psycopg2
pyzmq
requests
SQLAlchemy
squeakpy==0.7.1
squeakpy==0.7.3
typed-config

View file

@ -103,7 +103,7 @@ setup(
include_package_data=True,
zip_safe=False,
install_requires=[
'squeakpy>=0.7.1',
'squeakpy>=0.7.3',
'importlib_resources',
'argparse',
'googleapis-common-protos',

View file

@ -27,7 +27,9 @@ from squeak.messages import msg_inv
from squeak.messages import msg_notfound
from squeak.messages import msg_offer
from squeak.messages import msg_pong
from squeak.messages import MSG_SECRET_KEY
from squeak.messages import msg_secretkey
from squeak.messages import MSG_SQUEAK
from squeak.messages import msg_squeak
from squeak.net import CInv
@ -200,51 +202,34 @@ class PeerMessageHandler:
self.peer.send_msg(inv_msg)
def _get_inv_reply(self, inv):
if inv.type == 1:
squeak = self.squeak_controller.get_squeak(inv.hash)
if squeak is not None:
return msg_squeak(squeak=squeak)
if inv.type == 2:
offer_or_secret_key = self.squeak_controller.get_offer_or_secret_key(
inv.hash,
self.peer.remote_address,
)
if offer_or_secret_key is None:
return None
elif type(offer_or_secret_key) is bytes:
return msg_secretkey(
hashSqk=inv.hash,
secretKey=offer_or_secret_key,
)
elif type(offer_or_secret_key) is Offer:
return msg_offer(
hashSqk=inv.hash,
nonce=offer_or_secret_key.nonce,
strPaymentInfo=offer_or_secret_key.payment_request.encode(
'utf-8'),
host=offer_or_secret_key.host.encode('utf-8'),
port=offer_or_secret_key.port,
)
if inv.type == MSG_SQUEAK:
return self._get_inv_reply_for_squeak(inv)
if inv.type == MSG_SECRET_KEY:
return self._get_inv_reply_for_secret_key(inv)
# price = self.squeak_controller.get_price_for_squeak(inv.hash)
# if price == 0:
# secret_key = self.squeak_controller.get_squeak_secret_key(
# inv.hash)
# if secret_key is not None:
# return msg_secretkey(
# hashSqk=inv.hash,
# secretKey=secret_key,
# )
# else:
# offer = self.squeak_controller.get_buy_offer(
# squeak_hash=inv.hash,
# peer_address=self.peer.remote_address,
# )
# if offer is not None:
# return msg_offer(
# hashSqk=inv.hash,
# nonce=offer.nonce,
# strPaymentInfo=offer.payment_request.encode('utf-8'),
# host=offer.host.encode('utf-8'),
# port=offer.port,
# )
def _get_inv_reply_for_squeak(self, inv):
squeak = self.squeak_controller.get_squeak(inv.hash)
if squeak is not None:
return msg_squeak(squeak=squeak)
def _get_inv_reply_for_secret_key(self, inv):
resp = self.squeak_controller.get_offer_or_secret_key(
inv.hash,
self.peer.remote_address,
)
if resp is None:
return None
elif type(resp) is bytes:
return msg_secretkey(
hashSqk=inv.hash,
secretKey=resp,
)
elif type(resp) is Offer:
return msg_offer(
hashSqk=inv.hash,
nonce=resp.nonce,
strPaymentInfo=resp.payment_request.encode(
'utf-8'),
host=resp.host.encode('utf-8'),
port=resp.port,
)