Remove hash from post response (#57)

This commit is contained in:
Jonathan Zernik 2020-06-30 01:36:11 -07:00 committed by GitHub
parent 0037461cf4
commit 7c71a23448
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 64 deletions

View file

@ -10,16 +10,10 @@ fi
# Move this to squeakserver/common/lnd/rpc.proto instead.
cp rpc.proto squeakserver/common/rpc/lnd.proto
# cp rpc.proto lnd.proto
echo "Installing RPC protocol files"
# install lnd protocol
python3 -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. squeakserver/common/rpc/lnd.proto
# install lnd protocol at the top level
# python3 -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. lnd.proto
# install squeak server protocol
python3 -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. squeakserver/common/rpc/squeak_server.proto
# install squeak client protocol
# python3 -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. squeakserver/client/rpc/route_guide.proto

View file

@ -1,17 +1,3 @@
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the gRPC route guide client."""
from __future__ import print_function
import logging
@ -122,14 +108,14 @@ def run():
# Post a squeak with a direct request to the server
signing_key = generate_signing_key()
squeak = make_squeak(signing_key, 'hello from itest!')
squeak_hash = get_hash(squeak)
squeak_msg = build_squeak_msg(squeak)
post_response = server_stub.PostSqueak(squeak_server_pb2.PostSqueakRequest(squeak=squeak_msg))
print("Direct server post response: " + str(post_response))
assert post_response.hash == get_hash(squeak)
# Get the same squeak from the server
get_response = server_stub.GetSqueak(squeak_server_pb2.GetSqueakRequest(hash=post_response.hash))
get_response = server_stub.GetSqueak(squeak_server_pb2.GetSqueakRequest(hash=squeak_hash))
print("Direct server get response: " + str(get_response))
get_response_squeak = squeak_from_msg(get_response.squeak)
CheckSqueak(get_response_squeak, skipDecryptionCheck=True)
@ -177,7 +163,7 @@ def run():
assert get_hash(squeak) not in set(lookup_response.hashes)
# Buy the squeak data key
buy_response = server_stub.BuySqueak(squeak_server_pb2.BuySqueakRequest(hash=post_response.hash))
buy_response = server_stub.BuySqueak(squeak_server_pb2.BuySqueakRequest(hash=squeak_hash))
print("Server buy response: " + str(buy_response))
assert buy_response.offer.payment_request.startswith('ln')

View file

@ -67,8 +67,6 @@ message BuySqueakRequest {
}
message PostSqueakReply {
/// The squeak.
bytes hash = 1;
}
message GetSqueakReply {

View file

@ -35,18 +35,18 @@ class PostgresDb():
""" Connect to the PostgreSQL database server """
with self.get_cursor() as curs:
# execute a statement
print('PostgreSQL database version:')
logger.info('PostgreSQL database version:')
curs.execute('SELECT version()')
# display the PostgreSQL database server version
db_version = curs.fetchone()
print(db_version)
logger.info(db_version)
def init(self):
""" Create the tables and indices in the database. """
with self.get_cursor() as curs:
# execute a statement
print('Setting up database tables...')
logger.info('Setting up database tables...')
curs.execute(open("init.sql", "r").read())
def insert_squeak(self, squeak):
@ -116,7 +116,6 @@ class PostgresDb():
AND nBlockHeight >= %s
AND nBlockHeight <= %s"""
addresses_tuple = tuple(addresses)
logger.info("Lookup query with addresses tuple: " + str(addresses_tuple))
if not addresses:
return []

View file

@ -32,15 +32,14 @@ class SqueakServerHandler(object):
self.price = price
def handle_posted_squeak(self, squeak):
logger.info("Handler got posted squeak: " + str(squeak))
logger.info("Handle posted squeak: " + str(squeak))
# Insert the squeak in the database
inserted_squeak_hash = self.postgres_db.insert_squeak(squeak)
logger.info("Inserted squeak and got back hash: " + str(inserted_squeak_hash))
## Todo: return the squeak from the db.
return inserted_squeak_hash
logger.info("Inserted squeak with hash: " + str(inserted_squeak_hash))
return
def handle_get_squeak(self, squeak_hash):
logger.info("Handler get squeak by hash: " + str(squeak_hash))
logger.info("Handle get squeak by hash: " + str(squeak_hash))
squeak = self.postgres_db.get_squeak(squeak_hash)
logger.info("Got squeak from db: " + str(squeak))
# Remove the data key before sending squeak.
@ -48,14 +47,13 @@ class SqueakServerHandler(object):
return squeak
def handle_lookup_squeaks(self, addresses, min_block, max_block):
logger.info("Handler lookup squeaks with addresses: " + str(addresses))
logger.info("Handle lookup squeaks with addresses: " + str(addresses))
hashes = self.postgres_db.lookup_squeaks(addresses, min_block, max_block)
logger.info("Got hashes from db: " + str(hashes))
return hashes
def handle_buy_squeak(self, squeak_hash):
logger.info("Handler buy squeak by hash: " + str(squeak_hash))
logger.info("Handle buy squeak by hash: " + str(squeak_hash))
# Get the squeak from the database
squeak = self.postgres_db.get_squeak(squeak_hash)
# Get the datakey from the squeak
@ -63,22 +61,16 @@ class SqueakServerHandler(object):
# Generate a new random offer nonce
nonce = generate_offer_nonce()
# Get the invoice preimage from the nonce and the squeak data key
logger.info("Handling buy with nonce: " + str(nonce))
logger.info("Handling buy with data_key: " + str(data_key))
preimage = bxor(nonce, data_key)
# Get the offer price
amount = self.price
logger.info("Handling buy with preimage: " + str(preimage))
# Create the lightning invoice
add_invoice_response = self.lightning_client.add_invoice(preimage, amount)
preimage_hash = add_invoice_response.r_hash
invoice_payment_request = add_invoice_response.payment_request
# Get the lightning network node pubkey
get_info_response = self.lightning_client.get_info()
pubkey = get_info_response.identity_pubkey
# Return the buy offer
return BuyOffer(
squeak_hash,

View file

@ -1,17 +1,4 @@
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The Python implementation of the gRPC route guide server."""
import logging
import math
import time
from concurrent import futures
@ -25,6 +12,9 @@ from squeakserver.common.rpc import squeak_server_pb2_grpc
from squeakserver.server.util import get_hash
logger = logging.getLogger(__name__)
class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
"""Provides methods that implement functionality of squeak server."""
@ -53,10 +43,8 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
)
# Insert the squeak in database.
squeak_hash = self.handler.handle_posted_squeak(squeak)
return squeak_server_pb2.PostSqueakReply(
hash=squeak_hash,
)
self.handler.handle_posted_squeak(squeak)
return squeak_server_pb2.PostSqueakReply()
def GetSqueak(self, request, context):
squeak_hash = request.hash
@ -121,13 +109,10 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
)
def serve(self):
print('Calling serve...', flush=True)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
squeak_server_pb2_grpc.add_SqueakServerServicer_to_server(
self, server)
# server.add_insecure_port('0.0.0.0:50052')
server.add_insecure_port('{}:{}'.format(self.host, self.port))
print("Starting SqueakServerServicer rpc server...", flush=True)
server.start()
print("Started SqueakServerServicer rpc server...", flush=True)
server.wait_for_termination()