From 0ecb73fac30e6822d735f040c278cb5814cea0d5 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sat, 25 Dec 2021 19:47:07 -0800 Subject: [PATCH] Update squeaklib and use new msg type (#1894) * Use updated squeaklib with offer msg removed * Bump squeaklib version and use methods to check if message has offer --- requirements-itest.txt | 2 +- requirements.txt | 2 +- squeaknode/network/connection.py | 49 ++++++++++++++++++----------- squeaknode/node/secret_key_reply.py | 9 ++++-- tests/node/test_secret_key_reply.py | 16 +++++----- 5 files changed, 48 insertions(+), 30 deletions(-) diff --git a/requirements-itest.txt b/requirements-itest.txt index 0ca3c5bc..ddce1449 100644 --- a/requirements-itest.txt +++ b/requirements-itest.txt @@ -3,4 +3,4 @@ grpcio==1.39.0 grpcio-tools==1.39.0 importlib_resources==1.4.0 pytest==6.2.5 -squeaklib==0.8.5 +squeaklib==0.9.1 diff --git a/requirements.txt b/requirements.txt index 1b943c62..480d04c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,5 +14,5 @@ python-bitcoinlib==0.11.0 pyzmq==22.3.0 requests==2.26.0 SQLAlchemy==1.4.25 -squeaklib==0.8.5 +squeaklib==0.9.1 typed-config==0.2.5 diff --git a/squeaknode/network/connection.py b/squeaknode/network/connection.py index fb09bad4..0f913a16 100644 --- a/squeaknode/network/connection.py +++ b/squeaknode/network/connection.py @@ -145,8 +145,8 @@ class Connection(object): self.handle_getdata(msg) elif msg.command == b'notfound': self.handle_notfound(msg) - elif msg.command == b'offer': - self.handle_offer(msg) + # elif msg.command == b'offer': + # self.handle_offer(msg) elif msg.command == b'secretkey': self.handle_secret_key(msg) elif msg.command == b'subscribe': @@ -202,24 +202,37 @@ class Connection(object): if saved_squeak_hash is not None: self.network_handler.request_offers(saved_squeak_hash) - def handle_offer(self, msg): - offer = Offer( - squeak_hash=msg.hashSqk, - nonce=msg.nonce, - payment_request=msg.strPaymentInfo.decode('utf-8'), - host=msg.host.decode('utf-8'), - port=msg.port, - ) - self.network_handler.save_received_offer( - offer, - self.peer.remote_address, - ) + # def handle_offer(self, msg): + # offer = Offer( + # squeak_hash=msg.hashSqk, + # nonce=msg.nonce, + # payment_request=msg.strPaymentInfo.decode('utf-8'), + # host=msg.host.decode('utf-8'), + # port=msg.port, + # ) + # self.network_handler.save_received_offer( + # offer, + # self.peer.remote_address, + # ) def handle_secret_key(self, msg): - self.network_handler.unlock_squeak( - msg.hashSqk, - msg.secretKey, - ) + if msg.has_secret_key(): + self.network_handler.unlock_squeak( + msg.hashSqk, + msg.secretKey, + ) + elif msg.has_offer(): + offer = Offer( + squeak_hash=msg.hashSqk, + nonce=msg.offer.nonce, + payment_request=msg.offer.strPaymentInfo.decode('utf-8'), + host=msg.offer.host.decode('utf-8'), + port=msg.offer.port, + ) + self.network_handler.save_received_offer( + offer, + self.peer.remote_address, + ) def handle_subscribe(self, msg): if msg.protover < 60003: diff --git a/squeaknode/node/secret_key_reply.py b/squeaknode/node/secret_key_reply.py index 569288fe..6a64d230 100644 --- a/squeaknode/node/secret_key_reply.py +++ b/squeaknode/node/secret_key_reply.py @@ -19,9 +19,9 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -from squeak.messages import msg_offer from squeak.messages import msg_secretkey from squeak.messages import MsgSerializable +from squeak.net import COffer from squeaknode.core.offer import Offer @@ -53,11 +53,14 @@ class OfferReply(SecretKeyReply): self.offer = offer def get_msg(self) -> MsgSerializable: - return msg_offer( - hashSqk=self.squeak_hash, + offer = COffer( nonce=self.offer.nonce, strPaymentInfo=self.offer.payment_request.encode( 'utf-8'), host=self.offer.host.encode('utf-8'), port=self.offer.port, ) + return msg_secretkey( + hashSqk=self.squeak_hash, + offer=offer, + ) diff --git a/tests/node/test_secret_key_reply.py b/tests/node/test_secret_key_reply.py index 9d2a5e37..6eb9fd53 100644 --- a/tests/node/test_secret_key_reply.py +++ b/tests/node/test_secret_key_reply.py @@ -20,8 +20,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import pytest -from squeak.messages import msg_offer from squeak.messages import msg_secretkey +from squeak.net import COffer from squeaknode.node.secret_key_reply import FreeSecretKeyReply from squeaknode.node.secret_key_reply import OfferReply @@ -55,11 +55,13 @@ def test_free_secret_key_reply_msg(squeak_hash, secret_key, free_secret_key_repl def test_offer_reply_msg(squeak_hash, offer, offer_reply): reply_msg = offer_reply.get_msg() - assert reply_msg == msg_offer( + assert reply_msg == msg_secretkey( hashSqk=squeak_hash, - nonce=offer.nonce, - strPaymentInfo=offer.payment_request.encode( - 'utf-8'), - host=offer.host.encode('utf-8'), - port=offer.port, + offer=COffer( + nonce=offer.nonce, + strPaymentInfo=offer.payment_request.encode( + 'utf-8'), + host=offer.host.encode('utf-8'), + port=offer.port, + ) )