Include block time in squeak entry response (#93)

This commit is contained in:
Jonathan Zernik 2020-07-19 04:02:31 -07:00 committed by GitHub
parent 4bbca79979
commit a32ae4f299
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 10 deletions

View file

@ -313,6 +313,25 @@ def run():
== "Hello from the profile on the server!"
)
# Make another squeak
admin_stub.MakeSqueak(
squeak_admin_pb2.MakeSqueakRequest(
profile_id=profile_id, content="Hello again!",
)
)
# Wait a few seconds for the squeak to be verified on the server.
time.sleep(5)
# Get all followed squeak display items
get_followed_squeak_display_response = admin_stub.GetFollowedSqueakDisplays(
squeak_admin_pb2.GetFollowedSqueakDisplaysRequest()
)
print("Get followed squeak displays response: " + str(get_followed_squeak_display_response))
assert (
len(get_followed_squeak_display_response.squeak_display_entries) == 2
)
if __name__ == "__main__":
logging.basicConfig()

View file

@ -32,6 +32,10 @@ service SqueakAdmin {
*/
rpc GetSqueakDisplay (GetSqueakDisplayRequest) returns (GetSqueakDisplayReply) {}
/** sqkadmin: `getfollowedsqueakdisplays`
*/
rpc GetFollowedSqueakDisplays (GetFollowedSqueakDisplaysRequest) returns (GetFollowedSqueakDisplaysReply) {}
}
message GetBalanceRequest {
@ -136,3 +140,11 @@ message SqueakDisplayEntry {
/// The author name
string author_name = 9;
}
message GetFollowedSqueakDisplaysRequest {
}
message GetFollowedSqueakDisplaysReply {
/// Multiple squeak display entries
repeated SqueakDisplayEntry squeak_display_entries = 1;
}

View file

@ -50,3 +50,13 @@ class SqueakAdminServerHandler(object):
)
)
return squeak_entry_with_profile
def handle_get_followed_squeak_display_entries(self):
logger.info("Handle get followed squeak display entries.")
squeak_entries_with_profile = self.squeak_node.get_followed_squeak_entries_with_profile()
logger.info(
"Got squeak entries with profile: {}".format(
squeak_entries_with_profile
)
)
return squeak_entries_with_profile

View file

@ -5,6 +5,8 @@ import grpc
from proto import squeak_admin_pb2, squeak_admin_pb2_grpc
from squeakserver.server.util import get_hash
logger = logging.getLogger(__name__)
@ -57,14 +59,37 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer):
)
squeak_entry = squeak_entry_with_profile.squeak_entry
squeak = squeak_entry.squeak
block_header = squeak_entry.block_header
logger.info("Got block_header: {}".format(block_header))
return squeak_admin_pb2.GetSqueakDisplayReply(
squeak_display_entry=squeak_admin_pb2.SqueakDisplayEntry(
squeak_hash=squeak_hash.hex(),
squeak_hash=get_hash(squeak).hex(),
is_unlocked=True,
content_str=squeak.GetDecryptedContentStr(),
block_height=squeak.nBlockHeight,
block_time=block_header.nTime,
)
)
def GetFollowedSqueakDisplays(self, request, context):
squeak_entries_with_profile = self.handler.handle_get_followed_squeak_display_entries()
ret = []
for entry in squeak_entries_with_profile:
squeak_entry = entry.squeak_entry
squeak = squeak_entry.squeak
block_header = squeak_entry.block_header
squeak_display_entry=squeak_admin_pb2.SqueakDisplayEntry(
squeak_hash=get_hash(squeak).hex(),
is_unlocked=True,
content_str=squeak.GetDecryptedContentStr(),
block_height=squeak.nBlockHeight,
block_time=block_header.nTime,
)
ret.append(squeak_display_entry)
return squeak_admin_pb2.GetFollowedSqueakDisplaysReply(
squeak_display_entries=ret
)
def serve(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
squeak_admin_pb2_grpc.add_SqueakAdminServicer_to_server(self, server)

View file

@ -54,7 +54,7 @@ class SqueakBlockVerifier:
block_hash_str = squeak.hashBlock.hex()
block_header = self.blockchain_client.get_block_header(block_hash_str, False)
logger.info("Got block header from blockchain: {}".format(block_header))
return block_header
return bytes.fromhex(block_header)
def _get_block_hash(self, block_height):
block_hash = self.blockchain_client.get_block_hash(block_height)

View file

@ -122,3 +122,6 @@ class SqueakNode:
def get_squeak_entry_with_profile(self, squeak_hash):
return self.postgres_db.get_squeak_entry_with_profile(squeak_hash)
def get_followed_squeak_entries_with_profile(self):
return self.postgres_db.get_followed_squeak_entries_with_profile()

View file

@ -9,6 +9,8 @@ from squeakserver.core.squeak_entry import SqueakEntry
from squeakserver.core.squeak_entry_with_profile import SqueakEntryWithProfile
from squeakserver.server.squeak_profile import SqueakProfile
from squeakserver.server.util import get_hash
from squeakserver.blockchain.util import parse_block_header
logger = logging.getLogger(__name__)
@ -105,11 +107,22 @@ class PostgresDb:
with self.get_cursor() as curs:
curs.execute(sql, (squeak_hash_str,))
row = curs.fetchone()
squeak_entry = self._parse_squeak_entry(row)
squeak_profile = self._parse_squeak_profile(row)
return SqueakEntryWithProfile(
squeak_entry=squeak_entry, squeak_profile=squeak_profile,
)
return self._parse_squeak_entry_with_profile(row)
def get_followed_squeak_entries_with_profile(self):
""" Get a squeak. """
sql = """
SELECT * FROM squeak
JOIN profile
ON squeak.address=profile.address
WHERE squeak.block_header IS NOT NULL
AND profile.following=False
ORDER BY n_block_height DESC, n_time DESC;
"""
with self.get_cursor() as curs:
curs.execute(sql)
rows = curs.fetchall()
return [self._parse_squeak_entry_with_profile(row) for row in rows]
def lookup_squeaks(self, addresses, min_block, max_block):
""" Lookup squeaks. """
@ -153,10 +166,8 @@ class PostgresDb:
squeak_profile.following,
),
)
logger.info("Inserted new profile")
# get the new profile id back
row = curs.fetchone()
logger.info("New profile id: {}".format(row["profile_id"]))
return row["profile_id"]
def get_profile(self, profile_id):
@ -224,7 +235,8 @@ class PostgresDb:
vchDecryptionKey=vch_decryption_key,
)
block_header_column = row["block_header"]
block_header = bytes(block_header_column) if block_header_column else None
block_header_bytes = bytes(block_header_column) if block_header_column else None
block_header = parse_block_header(block_header_bytes) if block_header_bytes else None
return SqueakEntry(squeak=squeak, block_header=block_header)
def _parse_squeak_profile(self, row):
@ -238,3 +250,10 @@ class PostgresDb:
sharing=row["sharing"],
following=row["following"],
)
def _parse_squeak_entry_with_profile(self, row):
squeak_entry = self._parse_squeak_entry(row)
squeak_profile = self._parse_squeak_profile(row)
return SqueakEntryWithProfile(
squeak_entry=squeak_entry, squeak_profile=squeak_profile,
)