mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-19 13:18:26 +02:00
Redo peer protocol (#658)
* Add lookup squeaks to download peer rpc method * Added peer rpc method for lookup squeaks to upload * Got itest passing with new peer rpc * Fix upload lookup method in peer server servicer
This commit is contained in:
parent
a11174a69d
commit
1d4baaa5fa
7 changed files with 129 additions and 31 deletions
|
|
@ -114,8 +114,8 @@ def test_lookup_squeaks(server_stub, admin_stub, signing_profile_id, saved_squea
|
|||
|
||||
# Lookup squeaks for the given signing profile
|
||||
addresses = [squeak_profile_address]
|
||||
lookup_response = server_stub.LookupSqueaks(
|
||||
squeak_server_pb2.LookupSqueaksRequest(
|
||||
lookup_response = server_stub.LookupSqueaksToDownload(
|
||||
squeak_server_pb2.LookupSqueaksToDownloadRequest(
|
||||
addresses=addresses,
|
||||
min_block=0,
|
||||
max_block=99999999,
|
||||
|
|
@ -131,8 +131,8 @@ def test_lookup_squeaks_empty_result_addresses(server_stub, admin_stub):
|
|||
|
||||
# Lookup squeaks for the given address
|
||||
addresses = [address]
|
||||
lookup_response = server_stub.LookupSqueaks(
|
||||
squeak_server_pb2.LookupSqueaksRequest(
|
||||
lookup_response = server_stub.LookupSqueaksToDownload(
|
||||
squeak_server_pb2.LookupSqueaksToDownloadRequest(
|
||||
addresses=addresses,
|
||||
min_block=0,
|
||||
max_block=99999999,
|
||||
|
|
@ -154,8 +154,8 @@ def test_lookup_squeaks_empty_result_block_ranges(
|
|||
|
||||
# Lookup squeaks for the given signing profile
|
||||
addresses = [squeak_profile_address]
|
||||
lookup_response = server_stub.LookupSqueaks(
|
||||
squeak_server_pb2.LookupSqueaksRequest(
|
||||
lookup_response = server_stub.LookupSqueaksToDownload(
|
||||
squeak_server_pb2.LookupSqueaksToDownloadRequest(
|
||||
addresses=addresses,
|
||||
min_block=99999999,
|
||||
max_block=99999999,
|
||||
|
|
@ -164,6 +164,25 @@ def test_lookup_squeaks_empty_result_block_ranges(
|
|||
assert len(lookup_response.hashes) == 0
|
||||
|
||||
|
||||
def test_lookup_squeaks_to_upload(server_stub, admin_stub, signing_profile_id, saved_squeak_hash):
|
||||
# Get the squeak profile
|
||||
get_squeak_profile_response = admin_stub.GetSqueakProfile(
|
||||
squeak_admin_pb2.GetSqueakProfileRequest(
|
||||
profile_id=signing_profile_id,
|
||||
)
|
||||
)
|
||||
squeak_profile_address = get_squeak_profile_response.squeak_profile.address
|
||||
|
||||
# Lookup squeaks for the given signing profile
|
||||
addresses = [squeak_profile_address]
|
||||
lookup_response = server_stub.LookupSqueaksToUpload(
|
||||
squeak_server_pb2.LookupSqueaksToUploadRequest(
|
||||
addresses=addresses,
|
||||
)
|
||||
)
|
||||
assert bytes.fromhex(saved_squeak_hash) in set(lookup_response.hashes)
|
||||
|
||||
|
||||
def test_sell_squeak(server_stub, admin_stub, lightning_client, saved_squeak_hash):
|
||||
# Check the server balance
|
||||
get_balance_response = admin_stub.LndWalletBalance(
|
||||
|
|
|
|||
|
|
@ -18,9 +18,13 @@ service SqueakServer {
|
|||
*/
|
||||
rpc GetSqueak (GetSqueakRequest) returns (GetSqueakReply) {}
|
||||
|
||||
/** sqk: `lookupsqueaks`
|
||||
/** sqk: `lookupsqueakstodownload`
|
||||
*/
|
||||
rpc LookupSqueaks (LookupSqueaksRequest) returns (LookupSqueaksReply) {}
|
||||
rpc LookupSqueaksToDownload (LookupSqueaksToDownloadRequest) returns (LookupSqueaksToDownloadReply) {}
|
||||
|
||||
/** sqk: `lookupsqueakstoupload`
|
||||
*/
|
||||
rpc LookupSqueaksToUpload (LookupSqueaksToUploadRequest) returns (LookupSqueaksToUploadReply) {}
|
||||
|
||||
/** sqk: `getoffer`
|
||||
*/
|
||||
|
|
@ -38,7 +42,7 @@ message GetSqueakRequest {
|
|||
bytes hash = 1;
|
||||
}
|
||||
|
||||
message LookupSqueaksRequest {
|
||||
message LookupSqueaksToDownloadRequest {
|
||||
/// List of the author addresses
|
||||
repeated string addresses = 1;
|
||||
/// Minimum block to look for
|
||||
|
|
@ -47,6 +51,11 @@ message LookupSqueaksRequest {
|
|||
int32 max_block = 3;
|
||||
}
|
||||
|
||||
message LookupSqueaksToUploadRequest {
|
||||
/// List of the author addresses
|
||||
repeated string addresses = 1;
|
||||
}
|
||||
|
||||
message GetOfferRequest {
|
||||
/// Hash of the squeak to buy.
|
||||
bytes hash = 1;
|
||||
|
|
@ -60,15 +69,20 @@ message GetSqueakReply {
|
|||
Squeak squeak = 1;
|
||||
}
|
||||
|
||||
message LookupSqueaksReply {
|
||||
/// Height of the latest block.
|
||||
int32 latest_block_height = 1;
|
||||
|
||||
message LookupSqueaksToDownloadReply {
|
||||
/// Hash of the squeak to get.
|
||||
repeated bytes hashes = 2;
|
||||
repeated bytes hashes = 1;
|
||||
}
|
||||
|
||||
message LookupSqueaksToUploadReply {
|
||||
/// Hash of the squeak to get.
|
||||
repeated string allowed_addresses = 3;
|
||||
repeated bytes hashes = 1;
|
||||
/// List of the author addresses
|
||||
repeated string addresses = 2;
|
||||
/// Minimum block to look for
|
||||
int32 min_block = 3;
|
||||
/// Maximum block to look for
|
||||
int32 max_block = 4;
|
||||
}
|
||||
|
||||
message GetOfferReply {
|
||||
|
|
@ -100,3 +114,12 @@ message Offer {
|
|||
/// The port of the seller lightning node
|
||||
int32 port = 5;
|
||||
}
|
||||
|
||||
// message Interested {
|
||||
// /// List of the author addresses
|
||||
// repeated string addresses = 1;
|
||||
// /// Minimum block to look for
|
||||
// int32 min_block = 2;
|
||||
// /// Maximum block to look for
|
||||
// int32 max_block = 3;
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -300,6 +300,9 @@ class SqueakController:
|
|||
def get_best_block_height(self):
|
||||
return self.squeak_core.get_best_block_height()
|
||||
|
||||
def get_block_range(self):
|
||||
return self.config.sync.block_range
|
||||
|
||||
def get_network(self):
|
||||
return self.config.core.network
|
||||
|
||||
|
|
|
|||
|
|
@ -27,9 +27,9 @@ class PeerClient:
|
|||
yield self
|
||||
self.stub = None
|
||||
|
||||
def lookup_squeaks(self, addresses: List[str], min_block: int, max_block: int):
|
||||
lookup_response = self.stub.LookupSqueaks(
|
||||
squeak_server_pb2.LookupSqueaksRequest(
|
||||
def lookup_squeaks_to_download(self, addresses: List[str], min_block: int, max_block: int):
|
||||
lookup_response = self.stub.LookupSqueaksToDownload(
|
||||
squeak_server_pb2.LookupSqueaksToDownloadRequest(
|
||||
addresses=addresses,
|
||||
min_block=min_block,
|
||||
max_block=max_block,
|
||||
|
|
@ -37,6 +37,14 @@ class PeerClient:
|
|||
)
|
||||
return lookup_response
|
||||
|
||||
def lookup_squeaks_to_upload(self, addresses: List[str]):
|
||||
lookup_response = self.stub.LookupSqueaksToUpload(
|
||||
squeak_server_pb2.LookupSqueaksToUploadRequest(
|
||||
addresses=addresses,
|
||||
)
|
||||
)
|
||||
return lookup_response
|
||||
|
||||
def post_squeak(self, squeak: CSqueak):
|
||||
squeak_msg = self._build_squeak_msg(squeak)
|
||||
self.stub.PostSqueak(
|
||||
|
|
|
|||
|
|
@ -50,6 +50,53 @@ class SqueakServerHandler(object):
|
|||
allowed_addresses=allowed_addresses,
|
||||
)
|
||||
|
||||
def handle_lookup_squeaks_to_download(self, request):
|
||||
addresses = request.addresses
|
||||
min_block = request.min_block
|
||||
max_block = request.max_block
|
||||
logger.info(
|
||||
"Handle lookup squeaks to download with addresses: {}, min_block: {}, max_block: {}".format(
|
||||
str(addresses), min_block, max_block
|
||||
)
|
||||
)
|
||||
hashes = self.squeak_controller.lookup_squeaks(
|
||||
addresses, min_block, max_block)
|
||||
logger.info(
|
||||
"Got number of hashes to download from db: {}".format(len(hashes)))
|
||||
return squeak_server_pb2.LookupSqueaksToDownloadReply(
|
||||
hashes=hashes,
|
||||
)
|
||||
|
||||
def handle_lookup_squeaks_to_upload(self, request):
|
||||
addresses = request.addresses
|
||||
logger.info(
|
||||
"Handle lookup squeaks to upload with addresses: {}".format(
|
||||
str(addresses)
|
||||
)
|
||||
)
|
||||
allowed_addresses = self.squeak_controller.lookup_allowed_addresses(
|
||||
addresses)
|
||||
latest_block_height = self.squeak_controller.get_best_block_height()
|
||||
block_range = self.squeak_controller.get_block_range()
|
||||
max_block = latest_block_height
|
||||
min_block = latest_block_height - block_range
|
||||
hashes = self.squeak_controller.lookup_squeaks(
|
||||
addresses, min_block, max_block)
|
||||
logger.info(
|
||||
"Got number of hashes to already uploaded from db: {}, number of allowed addresses: {} with min_block: {} and max_block: {}".format(
|
||||
len(hashes),
|
||||
len(allowed_addresses),
|
||||
min_block,
|
||||
max_block,
|
||||
)
|
||||
)
|
||||
return squeak_server_pb2.LookupSqueaksToUploadReply(
|
||||
hashes=hashes,
|
||||
addresses=allowed_addresses,
|
||||
min_block=min_block,
|
||||
max_block=max_block,
|
||||
)
|
||||
|
||||
def handle_get_offer(self, squeak_hash: bytes, client_addr: str):
|
||||
logger.info(
|
||||
"Handle get offer by hash: {} from client_addr: {}".format(
|
||||
|
|
|
|||
|
|
@ -63,8 +63,11 @@ class SqueakServerServicer(squeak_server_pb2_grpc.SqueakServerServicer):
|
|||
)
|
||||
)
|
||||
|
||||
def LookupSqueaks(self, request, context):
|
||||
return self.handler.handle_lookup_squeaks(request)
|
||||
def LookupSqueaksToDownload(self, request, context):
|
||||
return self.handler.handle_lookup_squeaks_to_download(request)
|
||||
|
||||
def LookupSqueaksToUpload(self, request, context):
|
||||
return self.handler.handle_lookup_squeaks_to_upload(request)
|
||||
|
||||
def GetOffer(self, request, context):
|
||||
squeak_hash = request.hash
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class PeerSyncController:
|
|||
# Get list of followed addresses
|
||||
followed_addresses = self.squeak_controller.get_followed_addresses()
|
||||
# Get remote hashes
|
||||
lookup_result = self.peer_client.lookup_squeaks(
|
||||
lookup_result = self.peer_client.lookup_squeaks_to_download(
|
||||
followed_addresses,
|
||||
min_block,
|
||||
max_block,
|
||||
|
|
@ -81,21 +81,16 @@ class PeerSyncController:
|
|||
# Get list of sharing addresses.
|
||||
sharing_addresses = self.squeak_controller.get_sharing_addresses()
|
||||
# Get remote hashes
|
||||
lookup_result = self.peer_client.lookup_squeaks(
|
||||
lookup_result = self.peer_client.lookup_squeaks_to_upload(
|
||||
sharing_addresses,
|
||||
min_block,
|
||||
max_block,
|
||||
)
|
||||
remote_hashes = lookup_result.hashes
|
||||
allowed_addresses = lookup_result.allowed_addresses
|
||||
peer_latest_block = lookup_result.latest_block_height
|
||||
max_block = min(max_block, peer_latest_block)
|
||||
if max_block < min_block:
|
||||
return
|
||||
remote_addresses = lookup_result.addresses
|
||||
min_block = lookup_result.min_block
|
||||
max_block = lookup_result.max_block
|
||||
# Get local hashes
|
||||
addresses_to_search = set(allowed_addresses) & set(sharing_addresses)
|
||||
local_hashes = self.squeak_controller.lookup_squeaks(
|
||||
addresses_to_search,
|
||||
remote_addresses,
|
||||
min_block,
|
||||
max_block,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue