Make squeak with admin rpc (#75)

* Make squeak admin rpc method.

* Get the squeak back after making it through admin rpc make squeak method
This commit is contained in:
Jonathan Zernik 2020-07-17 22:12:16 -07:00 committed by GitHub
parent 7d530b25f4
commit 513527cd27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 137 additions and 19 deletions

View file

@ -18,7 +18,7 @@ rpc_pass=
[server]
rpc_host=0.0.0.0
rpc_port=8774
price=100
price=1000
[admin]
rpc_host=0.0.0.0

View file

@ -246,6 +246,40 @@ def run():
print("Get balance response balance: " + str(get_balance_response.balance))
assert get_balance_response.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_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,
))
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,
))
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))
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)
assert get_hash(get_squeak_response_squeak) == make_squeak_hash
print("Squeak from make squeak request: " + str(get_squeak_response_squeak))
assert get_squeak_response_squeak.GetDecryptedContentStr() == 'Hello from the profile on the server!'
# Close the channel
time.sleep(10)
for update in lnd_lightning_client.close_channel(channel_point):
@ -256,23 +290,7 @@ def run():
# Check the server balance
get_balance_response = admin_stub.GetBalance(squeak_admin_pb2.GetBalanceRequest())
print("Get balance response balance: " + str(get_balance_response.balance))
# assert get_balance_response.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_id = create_signing_profile_response.profile_id
# assert profile_id >= 0
# Get the new squeak profile
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
assert get_balance_response.balance == 1000
if __name__ == '__main__':

View file

@ -22,6 +22,10 @@ service SqueakAdmin {
*/
rpc GetSqueakProfile (GetSqueakProfileRequest) returns (GetSqueakProfileReply) {}
/** sqkadmin: `makesqueak`
*/
rpc MakeSqueak (MakeSqueakRequest) returns (MakeSqueakReply) {}
}
message GetBalanceRequest {
@ -71,3 +75,19 @@ message SqueakProfile {
/// Following
bool following = 6;
}
message MakeSqueakRequest {
/// The profile id
int32 profile_id = 1;
/// The content
string content = 2;
/// The replyto hash
bytes replyto = 3;
}
message MakeSqueakReply {
/// Hash of the created squeak.
bytes hash = 1;
}

View file

@ -16,6 +16,8 @@ from squeakserver.server.util import get_hash
from squeakserver.server.squeak_profile import SqueakProfile
from squeakserver.node.squeak_maker import SqueakMaker
logger = logging.getLogger(__name__)
@ -41,12 +43,15 @@ class SqueakAdminServerHandler(object):
def handle_create_signing_profile(self, profile_name):
logger.info("Handle create signing profile with name: {}".format(profile_name))
signing_key = CSigningKey.generate()
logger.info("Created new signing key: {}".format(signing_key))
verifying_key = signing_key.get_verifying_key()
address = CSqueakAddress.from_verifying_key(verifying_key)
signing_key_str = str(signing_key)
signing_key_bytes = signing_key_str.encode()
squeak_profile = SqueakProfile(
profile_id=None,
profile_name=profile_name,
private_key=bytes(signing_key),
private_key=signing_key_bytes,
address=str(address),
sharing=False,
following=False,
@ -59,3 +64,11 @@ class SqueakAdminServerHandler(object):
logger.info("Handle get squeak profile with id: {}".format(profile_id))
squeak_profile = self.postgres_db.get_profile(profile_id)
return squeak_profile
def handle_make_squeak(self, profile_id, content_str, replyto_hash):
logger.info("Handle make squeak profile with id: {}".format(profile_id))
squeak_profile = self.postgres_db.get_profile(profile_id)
squeak_maker = SqueakMaker(self.lightning_client)
squeak = squeak_maker.make_squeak(squeak_profile, content_str, replyto_hash)
inserted_squeak_hash = self.postgres_db.insert_squeak(squeak)
return inserted_squeak_hash

View file

@ -48,6 +48,15 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
)
)
def MakeSqueak(self, request, context):
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,
)
def serve(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
squeak_admin_pb2_grpc.add_SqueakAdminServicer_to_server(

View file

View file

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

View file

View file

@ -0,0 +1,54 @@
import logging
import time
from squeak.core.signing import CSigningKey
from squeak.core import MakeSqueakFromStr
from squeakserver.node.block_info import BlockInfo
logger = logging.getLogger(__name__)
class SqueakMaker:
def __init__(self, lightning_client):
self.lightning_client = lightning_client
def make_squeak(self, signing_profile, content_str, replyto_hash=None):
signing_key_str = signing_profile.private_key.decode()
signing_key = CSigningKey(signing_key_str)
logger.info("Creating squeak with signing key: {}".format(signing_key))
logger.info("Creating squeak with replyto_hash: {}".format(replyto_hash))
latest_block = self._get_latest_block()
block_height = latest_block.block_height
block_hash = latest_block.block_hash
timestamp = self._get_current_time_s()
logger.info("Creating squeak with block height: {}".format(block_height))
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,
)
else:
return MakeSqueakFromStr(
signing_key,
content_str,
block_height,
block_hash,
timestamp,
replyto_hash,
)
def _get_latest_block(self):
get_info_response = self.lightning_client.get_info()
block_hash = bytes.fromhex(get_info_response.block_hash)
block_height = get_info_response.block_height
return BlockInfo(block_hash, block_height)
def _get_current_time_s(self):
return int(time.time())