Add black to make codeformat in makefile (#85)

This commit is contained in:
Jonathan Zernik 2020-07-18 19:40:28 -07:00 committed by GitHub
parent 87b2a3de7d
commit 4a2961de54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 306 additions and 322 deletions

View file

@ -11,10 +11,10 @@ test:
tox -e codechecks
codeformat:
# tox -e black
tox -e autoflake
tox -e autopep8
tox -e isort
tox -e black
itest:
./itests/run_itest.sh

View file

@ -12,14 +12,17 @@ from squeak.params import SelectParams
from proto import lnd_pb2 as ln
from proto import lnd_pb2_grpc as lnrpc
from proto import (squeak_admin_pb2, squeak_admin_pb2_grpc, squeak_server_pb2,
squeak_server_pb2_grpc)
from proto import (
squeak_admin_pb2,
squeak_admin_pb2_grpc,
squeak_server_pb2,
squeak_server_pb2_grpc,
)
def build_squeak_msg(squeak):
return squeak_server_pb2.Squeak(
hash=get_hash(squeak),
serialized_squeak=squeak.serialize(),
hash=get_hash(squeak), serialized_squeak=squeak.serialize(),
)
@ -56,15 +59,15 @@ def get_latest_block_info(lightning_client):
return block_hash, block_height
def make_squeak(signing_key: CSigningKey, content: str, block_height, block_hash, reply_to: bytes = b'\x00'*HASH_LENGTH):
def make_squeak(
signing_key: CSigningKey,
content: str,
block_height,
block_hash,
reply_to: bytes = b"\x00" * HASH_LENGTH,
):
timestamp = int(time.time())
return MakeSqueakFromStr(
signing_key,
content,
block_height,
block_hash,
timestamp,
)
return MakeSqueakFromStr(signing_key, content, block_height, block_hash, timestamp,)
def get_hash(squeak):
@ -73,16 +76,9 @@ def get_hash(squeak):
def load_lightning_client() -> LNDLightningClient:
tls_cert_path = '~/.lnd/tls.cert'
macaroon_path = '~/.lnd/data/chain/bitcoin/simnet/admin.macaroon'
return LNDLightningClient(
'lnd',
10009,
tls_cert_path,
macaroon_path,
ln,
lnrpc,
)
tls_cert_path = "~/.lnd/tls.cert"
macaroon_path = "~/.lnd/data/chain/bitcoin/simnet/admin.macaroon"
return LNDLightningClient("lnd", 10009, tls_cert_path, macaroon_path, ln, lnrpc,)
def bxor(b1, b2): # use xor for bytes
@ -103,8 +99,9 @@ def run():
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
# used in circumstances in which the with statement does not fit the needs
# of the code.
with grpc.insecure_channel('sqkserver:8774') as server_channel,\
grpc.insecure_channel('sqkserver:8994') as admin_channel:
with grpc.insecure_channel(
"sqkserver:8774"
) as server_channel, grpc.insecure_channel("sqkserver:8994") as admin_channel:
# load lnd client
lnd_lightning_client = load_lightning_client()
@ -119,16 +116,19 @@ def run():
# Post a squeak with a direct request to the server
signing_key = generate_signing_key()
block_height, block_hash = get_latest_block_info(lnd_lightning_client)
squeak = make_squeak(signing_key, 'hello from itest!', block_hash, block_height)
squeak = make_squeak(signing_key, "hello from itest!", block_hash, block_height)
squeak_hash = get_hash(squeak)
squeak_msg = build_squeak_msg(squeak)
post_response = server_stub.PostSqueak(
squeak_server_pb2.PostSqueakRequest(squeak=squeak_msg))
squeak_server_pb2.PostSqueakRequest(squeak=squeak_msg)
)
print("Direct server post response: " + str(post_response))
# Get the same squeak from the server
get_response = server_stub.GetSqueak(squeak_server_pb2.GetSqueakRequest(hash=squeak_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)
@ -141,11 +141,11 @@ def run():
get_address(generate_signing_key()),
get_address(generate_signing_key()),
]
lookup_response = server_stub.LookupSqueaks(squeak_server_pb2.LookupSqueaksRequest(
addresses=addresses,
min_block=0,
max_block=99999999,
))
lookup_response = server_stub.LookupSqueaks(
squeak_server_pb2.LookupSqueaksRequest(
addresses=addresses, min_block=0, max_block=99999999,
)
)
print("Lookup response: " + str(lookup_response))
assert get_hash(squeak) in set(lookup_response.hashes)
@ -155,11 +155,11 @@ def run():
get_address(generate_signing_key()),
get_address(generate_signing_key()),
]
lookup_response = server_stub.LookupSqueaks(squeak_server_pb2.LookupSqueaksRequest(
addresses=addresses,
min_block=0,
max_block=99999999,
))
lookup_response = server_stub.LookupSqueaks(
squeak_server_pb2.LookupSqueaksRequest(
addresses=addresses, min_block=0, max_block=99999999,
)
)
assert get_hash(squeak) not in set(lookup_response.hashes)
# Lookup again with a different block range
@ -168,11 +168,11 @@ def run():
get_address(generate_signing_key()),
get_address(generate_signing_key()),
]
lookup_response = server_stub.LookupSqueaks(squeak_server_pb2.LookupSqueaksRequest(
addresses=addresses,
min_block=600,
max_block=99999999,
))
lookup_response = server_stub.LookupSqueaks(
squeak_server_pb2.LookupSqueaksRequest(
addresses=addresses, min_block=600, max_block=99999999,
)
)
assert get_hash(squeak) not in set(lookup_response.hashes)
# Generate a challenge to verify the offer
@ -181,12 +181,11 @@ def run():
challenge = get_challenge(encryption_key, expected_proof)
# Buy the squeak data key
buy_response = server_stub.BuySqueak(squeak_server_pb2.BuySqueakRequest(
hash=squeak_hash,
challenge=challenge,
))
buy_response = server_stub.BuySqueak(
squeak_server_pb2.BuySqueakRequest(hash=squeak_hash, challenge=challenge,)
)
print("Server buy response: " + str(buy_response))
assert buy_response.offer.payment_request.startswith('ln')
assert buy_response.offer.payment_request.startswith("ln")
# Check the offer challenge proof
print("Server offer proof: " + str(buy_response.offer.proof))
@ -194,7 +193,8 @@ def run():
# Connect to the server lightning node
connect_peer_response = lnd_lightning_client.connect_peer(
buy_response.offer.pubkey, buy_response.offer.host)
buy_response.offer.pubkey, buy_response.offer.host
)
print("Server connect peer response: " + str(connect_peer_response))
# List peers
@ -206,7 +206,7 @@ def run():
open_channel_response = lnd_lightning_client.open_channel(pubkey_bytes, 1000000)
print("Opening channel...")
for update in open_channel_response:
if update.HasField('chan_open'):
if update.HasField("chan_open"):
channel_point = update.chan_open.channel_point
print("Channel now open: " + str(channel_point))
break
@ -216,7 +216,9 @@ def run():
print("Server list channels response: " + str(list_channels_response))
# Pay the invoice
payment = lnd_lightning_client.pay_invoice_sync(buy_response.offer.payment_request)
payment = lnd_lightning_client.pay_invoice_sync(
buy_response.offer.payment_request
)
print("Server pay invoice response: " + str(payment))
preimage = payment.payment_preimage
print("preimage: " + str(preimage))
@ -224,7 +226,9 @@ def run():
# Verify with the payment preimage and decryption key ciphertext
decryption_key_cipher_bytes = buy_response.offer.key_cipher
iv = buy_response.offer.iv
encrypted_decryption_key = CEncryptedDecryptionKey.from_bytes(decryption_key_cipher_bytes)
encrypted_decryption_key = CEncryptedDecryptionKey.from_bytes(
decryption_key_cipher_bytes
)
# Decrypt the decryption key
decryption_key = encrypted_decryption_key.get_decryption_key(preimage, iv)
@ -233,42 +237,49 @@ def run():
print("new decryption key: " + str(serialized_decryption_key))
get_response_squeak.SetDecryptionKey(serialized_decryption_key)
CheckSqueak(get_response_squeak)
assert get_response_squeak.GetDecryptedContentStr() == 'hello from itest!'
assert get_response_squeak.GetDecryptedContentStr() == "hello from itest!"
print("Finished checking squeak.")
# Check the server balance
get_balance_response = admin_stub.GetBalance(squeak_admin_pb2.GetBalanceRequest())
get_balance_response = admin_stub.GetBalance(
squeak_admin_pb2.GetBalanceRequest()
)
print("Get balance response: " + str(get_balance_response))
assert get_balance_response.wallet_balance_response.total_balance == 0
# Create a new signing profile
profile_name = 'bob'
create_signing_profile_response = admin_stub.CreateSigningProfile(squeak_admin_pb2.CreateSigningProfileRequest(
profile_name=profile_name,
))
print("Get create signing profile response: " + str(create_signing_profile_response))
profile_name = "bob"
create_signing_profile_response = admin_stub.CreateSigningProfile(
squeak_admin_pb2.CreateSigningProfileRequest(profile_name=profile_name,)
)
print(
"Get create signing profile response: "
+ str(create_signing_profile_response)
)
profile_id = create_signing_profile_response.profile_id
# Get the new squeak profile
get_squeak_profile_response = admin_stub.GetSqueakProfile(squeak_admin_pb2.GetSqueakProfileRequest(
profile_id=profile_id,
))
get_squeak_profile_response = admin_stub.GetSqueakProfile(
squeak_admin_pb2.GetSqueakProfileRequest(profile_id=profile_id,)
)
print("Get squeak profile response: " + str(get_squeak_profile_response))
assert get_squeak_profile_response.squeak_profile.profile_name == profile_name
# Create a new squeak using the new profile
make_squeak_content = 'Hello from the profile on the server!'
make_squeak_response = admin_stub.MakeSqueak(squeak_admin_pb2.MakeSqueakRequest(
profile_id=profile_id,
content=make_squeak_content,
))
make_squeak_content = "Hello from the profile on the server!"
make_squeak_response = admin_stub.MakeSqueak(
squeak_admin_pb2.MakeSqueakRequest(
profile_id=profile_id, content=make_squeak_content,
)
)
print("Get make squeak response: " + str(make_squeak_response))
make_squeak_hash = make_squeak_response.hash
assert len(make_squeak_hash) == 32
# Get the new squeak from the server
get_squeak_response = server_stub.GetSqueak(
squeak_server_pb2.GetSqueakRequest(hash=make_squeak_hash))
squeak_server_pb2.GetSqueakRequest(hash=make_squeak_hash)
)
print("Get squeak response: " + str(get_squeak_response))
get_squeak_response_squeak = squeak_from_msg(get_squeak_response.squeak)
CheckSqueak(get_response_squeak, skipDecryptionCheck=True)
@ -278,16 +289,18 @@ def run():
# Close the channel
time.sleep(10)
for update in lnd_lightning_client.close_channel(channel_point):
if update.HasField('chan_close'):
if update.HasField("chan_close"):
print("Channel closed.")
break
# Check the server balance
get_balance_response = admin_stub.GetBalance(squeak_admin_pb2.GetBalanceRequest())
get_balance_response = admin_stub.GetBalance(
squeak_admin_pb2.GetBalanceRequest()
)
print("Get balance response: " + str(get_balance_response))
assert get_balance_response.wallet_balance_response.total_balance == 1000
if __name__ == '__main__':
if __name__ == "__main__":
logging.basicConfig()
run()

View file

@ -9,14 +9,10 @@ def generate_signing_key():
return CSigningKey.generate()
def make_squeak(signing_key: CSigningKey, content: str, reply_to: bytes = b'\x00'*HASH_LENGTH):
def make_squeak(
signing_key: CSigningKey, content: str, reply_to: bytes = b"\x00" * HASH_LENGTH
):
block_height = 0
block_hash = lx('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b')
block_hash = lx("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b")
timestamp = int(time.time())
return MakeSqueakFromStr(
signing_key,
content,
block_height,
block_hash,
timestamp,
)
return MakeSqueakFromStr(signing_key, content, block_height, block_hash, timestamp,)

View file

@ -11,9 +11,7 @@ class SqueakAdminServerHandler(object):
"""
def __init__(
self,
lightning_client: LNDLightningClient,
squeak_node: SqueakNode,
self, lightning_client: LNDLightningClient, squeak_node: SqueakNode,
):
self.lightning_client = lightning_client
self.squeak_node = squeak_node
@ -36,5 +34,7 @@ class SqueakAdminServerHandler(object):
def handle_make_squeak(self, profile_id, content_str, replyto_hash):
logger.info("Handle make squeak profile with id: {}".format(profile_id))
inserted_squeak_hash = self.squeak_node.make_squeak(profile_id, content_str, replyto_hash)
inserted_squeak_hash = self.squeak_node.make_squeak(
profile_id, content_str, replyto_hash
)
return inserted_squeak_hash

View file

@ -25,9 +25,7 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
def CreateSigningProfile(self, request, context):
profile_name = request.profile_name
profile_id = self.handler.handle_create_signing_profile(profile_name)
return squeak_admin_pb2.CreateSigningProfileReply(
profile_id=profile_id,
)
return squeak_admin_pb2.CreateSigningProfileReply(profile_id=profile_id,)
def GetSqueakProfile(self, request, context):
profile_id = request.profile_id
@ -47,15 +45,14 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
profile_id = request.profile_id
content_str = request.content
replyto_hash = request.replyto
squeak_hash = self.handler.handle_make_squeak(profile_id, content_str, replyto_hash)
return squeak_admin_pb2.MakeSqueakReply(
hash=squeak_hash,
squeak_hash = self.handler.handle_make_squeak(
profile_id, content_str, replyto_hash
)
return squeak_admin_pb2.MakeSqueakReply(hash=squeak_hash,)
def serve(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
squeak_admin_pb2_grpc.add_SqueakAdminServicer_to_server(
self, server)
server.add_insecure_port('{}:{}'.format(self.host, self.port))
squeak_admin_pb2_grpc.add_SqueakAdminServicer_to_server(self, server)
server.add_insecure_port("{}:{}".format(self.host, self.port))
server.start()
server.wait_for_termination()

View file

@ -7,15 +7,9 @@ import requests
class BitcoinBlockchainClient:
"""Access a bitcoin daemon using RPC."""
def __init__(
self,
host: str,
port: int,
rpc_user: str,
rpc_password: str,
) -> None:
self.url = f'https://{rpc_user}:{rpc_password}@{host}:{port}'
self.headers = {'content-type': 'application/json'}
def __init__(self, host: str, port: int, rpc_user: str, rpc_password: str,) -> None:
self.url = f"https://{rpc_user}:{rpc_password}@{host}:{port}"
self.headers = {"content-type": "application/json"}
def get_block_hash(self, block_height: int) -> Optional[bytes]:
# return self.access.getblockhash(block_height)
@ -26,9 +20,7 @@ class BitcoinBlockchainClient:
"id": 0,
}
response = requests.post(
self.url,
data=json.dumps(payload),
headers=self.headers,
self.url, data=json.dumps(payload), headers=self.headers,
).json()
result = response["result"]

View file

@ -17,5 +17,7 @@ class DummyBlockchainClient(BlockchainClient):
def get_block_hash(self, block_height: int) -> Optional[bytes]:
# return the genesis block hash
if block_height == 0:
return lx('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b')
return lx(
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
)
return None

View file

@ -1,4 +1,3 @@
def greet():
"""Return a greeting."""
return 'hello'
return "hello"

View file

@ -14,43 +14,45 @@ logger = logging.getLogger(__name__)
# Due to updated ECDSA generated tls.cert we need to let gprc know that
# we need to use that cipher suite otherwise there will be a handhsake
# error when we communicate with the lnd rpc server.
os.environ["GRPC_SSL_CIPHER_SUITES"] = 'HIGH+ECDSA'
os.environ["GRPC_VERBOSITY"] = 'DEBUG'
os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA"
os.environ["GRPC_VERBOSITY"] = "DEBUG"
class LNDLightningClient():
class LNDLightningClient:
"""Access a lightning deamon using RPC."""
def __init__(
self,
host: str,
port: int,
tls_cert_path: str,
macaroon_path: str,
ln_module,
lnrpc_module,
self,
host: str,
port: int,
tls_cert_path: str,
macaroon_path: str,
ln_module,
lnrpc_module,
) -> None:
self.ln_module = ln_module
self.lnrpc_module = lnrpc_module
url = '{}:{}'.format(host, port)
url = "{}:{}".format(host, port)
# Lnd cert is at ~/.lnd/tls.cert on Linux and
# ~/Library/Application Support/Lnd/tls.cert on Mac
cert = open(os.path.expanduser(tls_cert_path), 'rb').read()
cert = open(os.path.expanduser(tls_cert_path), "rb").read()
creds = grpc.ssl_channel_credentials(cert)
channel = grpc.secure_channel(url, creds)
self.stub = self.lnrpc_module.LightningStub(channel)
# Lnd admin macaroon is at ~/.lnd/data/chain/bitcoin/simnet/admin.macaroon on Linux and
# ~/Library/Application Support/Lnd/data/chain/bitcoin/simnet/admin.macaroon on Mac
with open(os.path.expanduser(macaroon_path), 'rb') as f:
with open(os.path.expanduser(macaroon_path), "rb") as f:
macaroon_bytes = f.read()
self.macaroon = codecs.encode(macaroon_bytes, 'hex'
)
self.macaroon = codecs.encode(macaroon_bytes, "hex")
def get_wallet_balance(self):
# Retrieve and display the wallet balance
return self.stub.WalletBalance(self.ln_module.WalletBalanceRequest(), metadata=[('macaroon', self.macaroon)])
return self.stub.WalletBalance(
self.ln_module.WalletBalanceRequest(),
metadata=[("macaroon", self.macaroon)],
)
def add_invoice(self, preimage, amount):
""" Create a new invoice with the given hash pre-image.
@ -59,11 +61,8 @@ class LNDLightningClient():
preimage -- the preimage bytes used to create the invoice
amount -- the value of the invoice
"""
invoice = self.ln_module.Invoice(
r_preimage=preimage,
value=amount,
)
return self.stub.AddInvoice(invoice, metadata=[('macaroon', self.macaroon)])
invoice = self.ln_module.Invoice(r_preimage=preimage, value=amount,)
return self.stub.AddInvoice(invoice, metadata=[("macaroon", self.macaroon)])
def pay_invoice_sync(self, payment_request):
""" Pay an invoice with a given payment_request
@ -74,7 +73,9 @@ class LNDLightningClient():
send_payment_request = self.ln_module.SendRequest(
payment_request=payment_request,
)
return self.stub.SendPaymentSync(send_payment_request, metadata=[('macaroon', self.macaroon)])
return self.stub.SendPaymentSync(
send_payment_request, metadata=[("macaroon", self.macaroon)]
)
def connect_peer(self, pubkey, host):
""" Connect to a lightning node peer.
@ -83,20 +84,21 @@ class LNDLightningClient():
pubkey -- The identity pubkey of the Lightning node
host -- The network location of the lightning node
"""
lightning_address = self.ln_module.LightningAddress(
pubkey=pubkey,
host=host,
)
lightning_address = self.ln_module.LightningAddress(pubkey=pubkey, host=host,)
connect_peer_request = self.ln_module.ConnectPeerRequest(
addr=lightning_address,
)
return self.stub.ConnectPeer(connect_peer_request, metadata=[('macaroon', self.macaroon)])
return self.stub.ConnectPeer(
connect_peer_request, metadata=[("macaroon", self.macaroon)]
)
def get_info(self):
""" Get info about the lightning network node.
"""
get_info_request = self.ln_module.GetInfoRequest()
return self.stub.GetInfo(get_info_request, metadata=[('macaroon', self.macaroon)])
return self.stub.GetInfo(
get_info_request, metadata=[("macaroon", self.macaroon)]
)
def open_channel_sync(self, pubkey_str, local_amount):
""" Open a channel with a remote lightning node.
@ -106,22 +108,27 @@ class LNDLightningClient():
local_amount -- The number of satoshis the wallet should commit to the channel
"""
open_channel_request = self.ln_module.OpenChannelRequest(
node_pubkey_string=pubkey_str,
local_funding_amount=local_amount,
node_pubkey_string=pubkey_str, local_funding_amount=local_amount,
)
return self.stub.OpenChannelSync(
open_channel_request, metadata=[("macaroon", self.macaroon)]
)
return self.stub.OpenChannelSync(open_channel_request, metadata=[('macaroon', self.macaroon)])
def list_channels(self):
""" List the channels
"""
list_channels_request = self.ln_module.ListChannelsRequest()
return self.stub.ListChannels(list_channels_request, metadata=[('macaroon', self.macaroon)])
return self.stub.ListChannels(
list_channels_request, metadata=[("macaroon", self.macaroon)]
)
def list_peers(self):
""" List the peers
"""
list_peers_request = self.ln_module.ListPeersRequest()
return self.stub.ListPeers(list_peers_request, metadata=[('macaroon', self.macaroon)])
return self.stub.ListPeers(
list_peers_request, metadata=[("macaroon", self.macaroon)]
)
def open_channel(self, pubkey, local_amount):
""" Open a channel
@ -131,10 +138,11 @@ class LNDLightningClient():
local_amount -- The number of satoshis the wallet should commit to the channel
"""
open_channel_request = self.ln_module.OpenChannelRequest(
node_pubkey=pubkey,
local_funding_amount=local_amount,
node_pubkey=pubkey, local_funding_amount=local_amount,
)
return self.stub.OpenChannel(
open_channel_request, metadata=[("macaroon", self.macaroon)]
)
return self.stub.OpenChannel(open_channel_request, metadata=[('macaroon', self.macaroon)])
def close_channel(self, channel_point):
""" Close a channel
@ -145,4 +153,6 @@ class LNDLightningClient():
close_channel_request = self.ln_module.CloseChannelRequest(
channel_point=channel_point,
)
return self.stub.CloseChannel(close_channel_request, metadata=[('macaroon', self.macaroon)])
return self.stub.CloseChannel(
close_channel_request, metadata=[("macaroon", self.macaroon)]
)

View file

@ -1,3 +1,3 @@
from collections import namedtuple
BlockInfo = namedtuple('BlockInfo', 'block_hash, block_height')
BlockInfo = namedtuple("BlockInfo", "block_hash, block_height")

View file

@ -8,7 +8,6 @@ VERIFY_UPDATE_INTERVAL_S = 10.0
class SqueakBlockPeriodicWorker:
def __init__(self, squeak_block_verifier):
self.squeak_block_verifier = squeak_block_verifier

View file

@ -5,7 +5,6 @@ logger = logging.getLogger(__name__)
class SqueakBlockQueueWorker:
def __init__(self, squeak_block_verifier):
self.squeak_block_verifier = squeak_block_verifier

View file

@ -5,19 +5,18 @@ logger = logging.getLogger(__name__)
class SqueakBlockVerifier:
def __init__(self, postgres_db, blockchain_client):
self.postgres_db = postgres_db
self.blockchain_client = blockchain_client
self.unverified_queue = queue.Queue()
def verify_squeak_block(self, squeak_hash):
logger.info('Verifying squeak hash: {}'.format(squeak_hash))
logger.info("Verifying squeak hash: {}".format(squeak_hash))
squeak = self._get_squeak(squeak_hash)
block_info = self._get_block_info(squeak)
def verify_all_unverified_squeaks(self):
logger.info('Calling verify_squeaks.')
logger.info("Calling verify_squeaks.")
squeaks_to_verify = self.postgres_db.get_unverified_block_squeaks()
for squeak_hash in squeaks_to_verify:
self.verify_squeak_block(squeak_hash)
@ -40,6 +39,6 @@ class SqueakBlockVerifier:
def _get_block_info(self, squeak):
block_height = squeak.nBlockHeight
block_hash = self.blockchain_client.get_block_hash(block_height)
logger.info('Got block hash from blockchain: {}'.format(block_hash))
logger.info('Got block hash from squeak: {}'.format(squeak.hashBlock))
logger.info('Is block hash correct: {}'.format(squeak.hashBlock == block_hash))
logger.info("Got block hash from blockchain: {}".format(block_hash))
logger.info("Got block hash from squeak: {}".format(squeak.hashBlock))
logger.info("Is block hash correct: {}".format(squeak.hashBlock == block_hash))

View file

@ -10,7 +10,6 @@ logger = logging.getLogger(__name__)
class SqueakMaker:
def __init__(self, lightning_client):
self.lightning_client = lightning_client
@ -27,11 +26,7 @@ class SqueakMaker:
logger.info("Creating squeak with block hash: {}".format(block_hash))
if replyto_hash is None or len(replyto_hash) == 0:
return MakeSqueakFromStr(
signing_key,
content_str,
block_height,
block_hash,
timestamp,
signing_key, content_str, block_height, block_hash, timestamp,
)
else:
return MakeSqueakFromStr(

View file

@ -1,9 +1,10 @@
from squeak.core.encryption import (CEncryptedDecryptionKey,
generate_initialization_vector)
from squeak.core.encryption import (
CEncryptedDecryptionKey,
generate_initialization_vector,
)
from squeak.core.signing import CSigningKey, CSqueakAddress
from squeakserver.node.squeak_block_periodic_worker import \
SqueakBlockPeriodicWorker
from squeakserver.node.squeak_block_periodic_worker import SqueakBlockPeriodicWorker
from squeakserver.node.squeak_block_queue_worker import SqueakBlockQueueWorker
from squeakserver.node.squeak_block_verifier import SqueakBlockVerifier
from squeakserver.node.squeak_maker import SqueakMaker
@ -13,16 +14,26 @@ from squeakserver.server.util import generate_offer_preimage
class SqueakNode:
def __init__(self, postgres_db, blockchain_client, lightning_client, lightning_host_port, price):
def __init__(
self,
postgres_db,
blockchain_client,
lightning_client,
lightning_host_port,
price,
):
self.postgres_db = postgres_db
self.blockchain_client = blockchain_client
self.lightning_client = lightning_client
self.lightning_host_port = lightning_host_port
self.price = price
self.squeak_block_verifier = SqueakBlockVerifier(postgres_db, blockchain_client)
self.squeak_block_periodic_worker = SqueakBlockPeriodicWorker(self.squeak_block_verifier)
self.squeak_block_queue_worker = SqueakBlockQueueWorker(self.squeak_block_verifier)
self.squeak_block_periodic_worker = SqueakBlockPeriodicWorker(
self.squeak_block_verifier
)
self.squeak_block_queue_worker = SqueakBlockQueueWorker(
self.squeak_block_verifier
)
def start_running(self):
# self.squeak_block_periodic_worker.start_running()
@ -57,7 +68,8 @@ class SqueakNode:
# Encrypt the decryption key
iv = generate_initialization_vector()
encrypted_decryption_key = CEncryptedDecryptionKey.from_decryption_key(
decryption_key, preimage, iv)
decryption_key, preimage, iv
)
# Get the offer price
amount = self.price
# Create the lightning invoice

View file

@ -1,8 +1,17 @@
class BuyOffer():
def __init__(self, squeak_hash, key_cipher, iv, amount, preimage_hash, payment_request, pubkey, host, port, proof):
class BuyOffer:
def __init__(
self,
squeak_hash,
key_cipher,
iv,
amount,
preimage_hash,
payment_request,
pubkey,
host,
port,
proof,
):
self.squeak_hash = squeak_hash
self.key_cipher = key_cipher
self.iv = iv

View file

@ -1,9 +1,7 @@
valid_params = {"host", "database", "user", "password"}
valid_params = {'host', 'database', 'user', 'password'}
def parse_db_params(config, section='postgresql'):
def parse_db_params(config, section="postgresql"):
# get section, default to postgresql
db = {}
if config.has_section(section):
@ -12,5 +10,7 @@ def parse_db_params(config, section='postgresql'):
if param[0] in valid_params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
raise Exception(
"Section {0} not found in the {1} file".format(section, filename)
)
return db

View file

@ -1,3 +1,3 @@
from collections import namedtuple
LightningAddressHostPort = namedtuple('LightningAddress', ['host', 'port'])
LightningAddressHostPort = namedtuple("LightningAddress", ["host", "port"])

View file

@ -8,12 +8,9 @@ from squeak.params import SelectParams
import proto.lnd_pb2 as ln
import proto.lnd_pb2_grpc as lnrpc
from squeakserver.admin.squeak_admin_server_handler import \
SqueakAdminServerHandler
from squeakserver.admin.squeak_admin_server_servicer import \
SqueakAdminServerServicer
from squeakserver.blockchain.bitcoin_blockchain_client import \
BitcoinBlockchainClient
from squeakserver.admin.squeak_admin_server_handler import SqueakAdminServerHandler
from squeakserver.admin.squeak_admin_server_servicer import SqueakAdminServerServicer
from squeakserver.blockchain.bitcoin_blockchain_client import BitcoinBlockchainClient
from squeakserver.common.lnd_lightning_client import LNDLightningClient
from squeakserver.node.squeak_node import SqueakNode
from squeakserver.server.db_params import parse_db_params
@ -26,49 +23,42 @@ logger = logging.getLogger(__name__)
def load_lightning_client(config) -> LNDLightningClient:
if int(config['server']['price']) == 0:
if int(config["server"]["price"]) == 0:
return None
return LNDLightningClient(
config['lnd']['host'],
config['lnd']['rpc_port'],
config['lnd']['tls_cert_path'],
config['lnd']['macaroon_path'],
config["lnd"]["host"],
config["lnd"]["rpc_port"],
config["lnd"]["tls_cert_path"],
config["lnd"]["macaroon_path"],
ln,
lnrpc,
)
def load_lightning_host_port(config) -> LNDLightningClient:
if int(config['server']['price']) == 0:
if int(config["server"]["price"]) == 0:
return None
lnd_host = config['lnd']['host']
if 'external_host' in config['lnd']:
lnd_host = config['lnd']['external_host']
lnd_port = int(config['lnd']['port'])
return LightningAddressHostPort(
lnd_host,
lnd_port,
)
lnd_host = config["lnd"]["host"]
if "external_host" in config["lnd"]:
lnd_host = config["lnd"]["external_host"]
lnd_port = int(config["lnd"]["port"])
return LightningAddressHostPort(lnd_host, lnd_port,)
def load_rpc_server(config, handler) -> SqueakServerServicer:
return SqueakServerServicer(
config['server']['rpc_host'],
config['server']['rpc_port'],
handler,
config["server"]["rpc_host"], config["server"]["rpc_port"], handler,
)
def load_admin_rpc_server(config, handler) -> SqueakAdminServerServicer:
return SqueakAdminServerServicer(
config['admin']['rpc_host'],
config['admin']['rpc_port'],
handler,
config["admin"]["rpc_host"], config["admin"]["rpc_port"], handler,
)
def load_price(config):
return int(config['server']['price'])
return int(config["server"]["price"])
def load_handler(squeak_node):
@ -76,10 +66,7 @@ def load_handler(squeak_node):
def load_admin_handler(lightning_client, squeak_node):
return SqueakAdminServerHandler(
lightning_client,
squeak_node,
)
return SqueakAdminServerHandler(lightning_client, squeak_node,)
def load_db_params(config):
@ -93,10 +80,10 @@ def load_postgres_db(config):
def load_blockchain_client(config):
return BitcoinBlockchainClient(
config['bitcoin']['rpc_host'],
config['bitcoin']['rpc_port'],
config['bitcoin']['rpc_user'],
config['bitcoin']['rpc_pass'],
config["bitcoin"]["rpc_host"],
config["bitcoin"]["rpc_port"],
config["bitcoin"]["rpc_user"],
config["bitcoin"]["rpc_pass"],
)
@ -106,11 +93,8 @@ def sigterm_handler(_signo, _stack_frame):
def start_admin_rpc_server(rpc_server):
logger.info('Calling start_admin_rpc_server...')
thread = threading.Thread(
target=rpc_server.serve,
args=(),
)
logger.info("Calling start_admin_rpc_server...")
thread = threading.Thread(target=rpc_server.serve, args=(),)
thread.daemon = True
thread.start()
@ -120,22 +104,15 @@ def parse_args():
description="squeakserver runs a node using squeak protocol. ",
)
parser.add_argument(
'--config',
dest='config',
type=str,
help='Path to the config file.',
"--config", dest="config", type=str, help="Path to the config file.",
)
parser.add_argument(
'--log-level',
dest='log_level',
type=str,
default='info',
help='Logging level',
"--log-level", dest="log_level", type=str, default="info", help="Logging level",
)
subparsers = parser.add_subparsers(help='sub-command help')
subparsers = parser.add_subparsers(help="sub-command help")
# create the parser for the "run-server" command
parser_run_server = subparsers.add_parser('run-server', help='run-server help')
parser_run_server = subparsers.add_parser("run-server", help="run-server help")
parser_run_server.set_defaults(func=run_server)
return parser.parse_args()
@ -159,17 +136,17 @@ def main():
def run_server(config):
logger.info('network: ' + config['DEFAULT']['network'])
logger.info("network: " + config["DEFAULT"]["network"])
# SelectParams(config['DEFAULT']['network'])
SelectParams("mainnet")
# load the db params
db_params = load_db_params(config)
logger.info('db params: ' + str(db_params))
logger.info("db params: " + str(db_params))
# load postgres db
postgres_db = load_postgres_db(config)
logger.info('postgres_db: ' + str(postgres_db))
logger.info("postgres_db: " + str(postgres_db))
postgres_db.get_version()
postgres_db.init()
@ -184,8 +161,9 @@ def run_server(config):
blockchain_client = load_blockchain_client(config)
# Create and start the squeak node
squeak_node = SqueakNode(postgres_db, blockchain_client,
lightning_client, lightning_host_port, price)
squeak_node = SqueakNode(
postgres_db, blockchain_client, lightning_client, lightning_host_port, price
)
squeak_node.start_running()
# start admin rpc server
@ -199,5 +177,5 @@ def run_server(config):
server.serve()
if __name__ == '__main__':
if __name__ == "__main__":
main()

View file

@ -10,8 +10,7 @@ from squeakserver.server.util import get_hash
logger = logging.getLogger(__name__)
class PostgresDb():
class PostgresDb:
def __init__(self, params):
self.connection_pool = pool.ThreadedConnectionPool(5, 20, **params)
@ -29,8 +28,8 @@ class PostgresDb():
""" Connect to the PostgreSQL database server """
with self.get_cursor() as curs:
# execute a statement
logger.info('PostgreSQL database version:')
curs.execute('SELECT version()')
logger.info("PostgreSQL database version:")
curs.execute("SELECT version()")
# display the PostgreSQL database server version
db_version = curs.fetchone()
@ -40,7 +39,7 @@ class PostgresDb():
""" Create the tables and indices in the database. """
with self.get_cursor() as curs:
# execute a statement
logger.info('Setting up database tables...')
logger.info("Setting up database tables...")
curs.execute(open("init.sql", "r").read())
def insert_squeak(self, squeak):
@ -52,24 +51,27 @@ class PostgresDb():
with self.get_cursor() as curs:
# execute the INSERT statement
curs.execute(sql, (
get_hash(squeak).hex(),
squeak.nVersion,
squeak.hashEncContent.hex(),
squeak.hashReplySqk.hex(),
squeak.hashBlock.hex(),
squeak.nBlockHeight,
squeak.vchScriptPubKey,
squeak.vchEncryptionKey,
squeak.encDatakey.hex(),
squeak.iv.hex(),
squeak.nTime,
squeak.nNonce,
squeak.encContent.hex(),
squeak.vchScriptSig,
str(squeak.GetAddress()),
squeak.vchDecryptionKey,
))
curs.execute(
sql,
(
get_hash(squeak).hex(),
squeak.nVersion,
squeak.hashEncContent.hex(),
squeak.hashReplySqk.hex(),
squeak.hashBlock.hex(),
squeak.nBlockHeight,
squeak.vchScriptPubKey,
squeak.vchEncryptionKey,
squeak.encDatakey.hex(),
squeak.iv.hex(),
squeak.nTime,
squeak.nNonce,
squeak.encContent.hex(),
squeak.vchScriptSig,
str(squeak.GetAddress()),
squeak.vchDecryptionKey,
),
)
# get the generated hash back
row = curs.fetchone()
return bytes.fromhex(row[0])
@ -120,10 +122,7 @@ class PostgresDb():
# logger.info(curs.mogrify(sql, (addresses_tuple, min_block, max_block)))
curs.execute(sql, (addresses_tuple, min_block, max_block))
rows = curs.fetchall()
hashes = [
bytes.fromhex(row[0])
for row in rows
]
hashes = [bytes.fromhex(row[0]) for row in rows]
return hashes
def insert_profile(self, squeak_profile):
@ -135,17 +134,20 @@ class PostgresDb():
"""
with self.get_cursor() as curs:
# execute the INSERT statement
curs.execute(sql, (
squeak_profile.profile_name,
squeak_profile.private_key,
squeak_profile.address,
squeak_profile.sharing,
squeak_profile.following,
))
logger.info('Inserted new profile')
curs.execute(
sql,
(
squeak_profile.profile_name,
squeak_profile.private_key,
squeak_profile.address,
squeak_profile.sharing,
squeak_profile.following,
),
)
logger.info("Inserted new profile")
# get the new profile id back
row = curs.fetchone()
logger.info('New profile id: {}'.format(row[0]))
logger.info("New profile id: {}".format(row[0]))
return row[0]
def get_profile(self, profile_id):
@ -176,10 +178,7 @@ class PostgresDb():
with self.get_cursor() as curs:
curs.execute(sql)
rows = curs.fetchall()
hashes = [
bytes.fromhex(row[0])
for row in rows
]
hashes = [bytes.fromhex(row[0]) for row in rows]
return hashes
def delete_squeak(self, squeak_hash):
@ -201,8 +200,5 @@ class PostgresDb():
squeak_hash_str = squeak_hash.hex()
with self.get_cursor() as curs:
# execute the UPDATE statement
curs.execute(sql, (
block_header,
squeak_hash,
))
logger.info('Updated squeak with block header')
curs.execute(sql, (block_header, squeak_hash,))
logger.info("Updated squeak with block header")

View file

@ -1,4 +1,6 @@
from collections import namedtuple
SqueakProfile = namedtuple(
'SqueakProfile', 'profile_id, profile_name, private_key, address, sharing, following')
"SqueakProfile",
"profile_id, profile_name, private_key, address, sharing, following",
)

View file

@ -23,8 +23,11 @@ class SqueakServerHandler(object):
return self.squeak_node.get_locked_squeak(squeak_hash)
def handle_lookup_squeaks(self, addresses, min_block, max_block):
logger.info("Handle lookup squeaks with addresses: {}, min_block: {}, max_block: {}".format(
str(addresses), min_block, max_block))
logger.info(
"Handle lookup squeaks with addresses: {}, min_block: {}, max_block: {}".format(
str(addresses), min_block, max_block
)
)
hashes = self.squeak_node.lookup_squeaks(addresses, min_block, max_block)
logger.info("Got number of hashes from db: {}".format(len(hashes)))
return hashes

View file

@ -26,16 +26,12 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
# Check is squeak deserialized correctly
if squeak == None:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return squeak_server_pb2.PostSqueakReply(
hash=None,
)
return squeak_server_pb2.PostSqueakReply(hash=None,)
# Check is squeak hash is correct
if get_hash(squeak) != squeak_hash:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return squeak_server_pb2.PostSqueakReply(
hash=None,
)
return squeak_server_pb2.PostSqueakReply(hash=None,)
# Insert the squeak in database.
self.handler.handle_posted_squeak(squeak)
@ -48,14 +44,11 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
squeak = self.handler.handle_get_squeak(squeak_hash)
if squeak == None:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return squeak_server_pb2.GetSqueakReply(
squeak=None,
)
return squeak_server_pb2.GetSqueakReply(squeak=None,)
return squeak_server_pb2.GetSqueakReply(
squeak=squeak_server_pb2.Squeak(
hash=get_hash(squeak),
serialized_squeak=squeak.serialize(),
hash=get_hash(squeak), serialized_squeak=squeak.serialize(),
)
)
@ -64,9 +57,7 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
min_block = request.min_block
max_block = request.max_block
hashes = self.handler.handle_lookup_squeaks(addresses, min_block, max_block)
return squeak_server_pb2.LookupSqueaksReply(
hashes=hashes,
)
return squeak_server_pb2.LookupSqueaksReply(hashes=hashes,)
def BuySqueak(self, request, context):
squeak_hash = request.hash
@ -77,18 +68,14 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
if buy_response == None:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return squeak_server_pb2.BuySqueakReply(
offer=None,
)
return squeak_server_pb2.BuySqueakReply(offer=None,)
offer_squeak_hash = buy_response.squeak_hash
amount = buy_response.amount
if offer_squeak_hash != squeak_hash:
context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
return squeak_server_pb2.BuySqueakReply(
offer=None,
)
return squeak_server_pb2.BuySqueakReply(offer=None,)
return squeak_server_pb2.BuySqueakReply(
offer=squeak_server_pb2.SqueakBuyOffer(
@ -107,9 +94,8 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
def serve(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
squeak_server_pb2_grpc.add_SqueakServerServicer_to_server(
self, server)
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))
server.add_insecure_port("{}:{}".format(self.host, self.port))
server.start()
server.wait_for_termination()

View file

@ -12,17 +12,13 @@ from squeakserver.server.squeak_validator import SqueakValidator
# _data_sql = f.read().decode("utf8")
def make_squeak(signing_key: CSigningKey, content: str, reply_to: bytes = b'\x00'*HASH_LENGTH):
def make_squeak(
signing_key: CSigningKey, content: str, reply_to: bytes = b"\x00" * HASH_LENGTH
):
block_height = 0
block_hash = lx('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b')
block_hash = lx("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b")
timestamp = int(time.time())
return MakeSqueakFromStr(
signing_key,
content,
block_height,
block_hash,
timestamp,
)
return MakeSqueakFromStr(signing_key, content, block_height, block_hash, timestamp,)
@pytest.fixture
@ -37,11 +33,11 @@ def validator():
@pytest.fixture
def example_squeak(signing_key):
return make_squeak(signing_key, 'hello!', )
return make_squeak(signing_key, "hello!",)
@pytest.fixture
def bad_squeak(signing_key):
squeak = make_squeak(signing_key, 'hello!', )
squeak = make_squeak(signing_key, "hello!",)
squeak.ClearDecryptionKey()
return squeak

View file

@ -1,5 +1,3 @@
def test_validate(validator, example_squeak):
assert validator.validate(example_squeak)

View file

@ -25,6 +25,7 @@ deps =
mypy_paths =
squeakserver
tests
itests
commands =
mypy --ignore-missing-imports {posargs:{[testenv:mypy]mypy_paths}}
@ -36,6 +37,7 @@ deps =
codechecks_paths =
squeakserver
tests
itests
commands =
flake8
reorder-python-imports
@ -47,8 +49,9 @@ deps =
black_paths =
squeakserver
tests
itests
commands =
black --check --diff {posargs:{[testenv:black]black_paths}}
black {posargs:{[testenv:black]black_paths}}
[testenv:autopep8]
basepython = python3.8