mirror of
https://github.com/Blockstream/satellite-api.git
synced 2025-02-22 13:42:33 +01:00
Define distinct maximum message sizes per channel
This commit is contained in:
parent
b2a7c7f394
commit
ef6074785e
4 changed files with 36 additions and 13 deletions
|
@ -48,7 +48,7 @@ LN_INVOICE_EXPIRY = 60 * 60 # one hour
|
|||
LN_INVOICE_DESCRIPTION = 'Blockstream Satellite Transmission' if os.getenv(
|
||||
'ENV') == 'production' else 'BSS Test'
|
||||
|
||||
MAX_MESSAGE_SIZE = 2**20
|
||||
DEFAULT_MAX_MESSAGE_SIZE = 2**20
|
||||
MAX_TEXT_MSG_LEN = 1024 # valid for message (not file)
|
||||
|
||||
MIN_BID = int(os.getenv('MIN_BID', 1000))
|
||||
|
@ -72,7 +72,7 @@ BTC_SRC_CHANNEL = 5
|
|||
|
||||
class ChannelInfo:
|
||||
|
||||
def __init__(self, name, user_permissions, tx_rate):
|
||||
def __init__(self, name, user_permissions, tx_rate, max_msg_size):
|
||||
"""Construct channel information
|
||||
|
||||
Args:
|
||||
|
@ -83,6 +83,7 @@ class ChannelInfo:
|
|||
but not post them, and only the admin can post messages.
|
||||
tx_rate (float): Transmit rate in bytes/sec. Used to handle the
|
||||
retransmission timeout intervals independently on each channel.
|
||||
max_msg_size (int): Maximum message size on this channel.
|
||||
"""
|
||||
assert isinstance(user_permissions, list)
|
||||
assert len(user_permissions) == 0 or \
|
||||
|
@ -95,14 +96,21 @@ class ChannelInfo:
|
|||
# the admin, and these messages are not paid.
|
||||
self.requires_payment = 'post' in user_permissions
|
||||
self.tx_rate = tx_rate
|
||||
self.max_msg_size = max_msg_size
|
||||
|
||||
|
||||
CHANNEL_INFO = {
|
||||
USER_CHANNEL: ChannelInfo('transmissions', ['get', 'post', 'delete'],
|
||||
1000),
|
||||
AUTH_CHANNEL: ChannelInfo('auth', [], 125),
|
||||
GOSSIP_CHANNEL: ChannelInfo('gossip', ['get'], 500),
|
||||
BTC_SRC_CHANNEL: ChannelInfo('btc-src', ['get'], 500),
|
||||
USER_CHANNEL:
|
||||
ChannelInfo('transmissions', ['get', 'post', 'delete'], 1000,
|
||||
DEFAULT_MAX_MESSAGE_SIZE),
|
||||
AUTH_CHANNEL:
|
||||
ChannelInfo('auth', [], 125, DEFAULT_MAX_MESSAGE_SIZE),
|
||||
GOSSIP_CHANNEL:
|
||||
ChannelInfo('gossip', ['get'], 500,
|
||||
1800000), # tx over 1h at 500 bytes/sec
|
||||
BTC_SRC_CHANNEL:
|
||||
ChannelInfo('btc-src', ['get'], 500,
|
||||
16200000), # tx over 9h at 500 bytes/sec
|
||||
}
|
||||
|
||||
CHANNELS = list(CHANNEL_INFO.keys())
|
||||
|
|
|
@ -26,7 +26,7 @@ errors = {
|
|||
"Minimum message size is {} byte",
|
||||
HTTPStatus.BAD_REQUEST),
|
||||
'MESSAGE_FILE_TOO_LARGE': (118, "Message too large",
|
||||
"Message size exceeds max size of {} MB",
|
||||
"Message size exceeds max size of {:.2f} MB",
|
||||
HTTPStatus.REQUEST_ENTITY_TOO_LARGE),
|
||||
'ORDER_CANCELLATION_ERROR': (120, "Cannot cancel order",
|
||||
"Order already {}", HTTPStatus.BAD_REQUEST),
|
||||
|
|
|
@ -124,10 +124,11 @@ class OrderUploadResource(Resource):
|
|||
return get_http_error_resp('MESSAGE_FILE_TOO_SMALL',
|
||||
constants.MIN_MESSAGE_SIZE)
|
||||
|
||||
if (msg_size > constants.MAX_MESSAGE_SIZE):
|
||||
if (msg_size > constants.CHANNEL_INFO[channel].max_msg_size):
|
||||
os.remove(filepath)
|
||||
return get_http_error_resp('MESSAGE_FILE_TOO_LARGE',
|
||||
constants.MAX_MESSAGE_SIZE / (2**20))
|
||||
return get_http_error_resp(
|
||||
'MESSAGE_FILE_TOO_LARGE',
|
||||
constants.CHANNEL_INFO[channel].max_msg_size / (2**20))
|
||||
|
||||
bid = int(args.get('bid')) if requires_payment else 0
|
||||
if (requires_payment and not bidding.validate_bid(msg_size, bid)):
|
||||
|
|
|
@ -75,7 +75,21 @@ def test_uploaded_file_too_small(client):
|
|||
|
||||
|
||||
def test_uploaded_file_too_large(client):
|
||||
n_bytes = constants.MAX_MESSAGE_SIZE + 1
|
||||
n_bytes = constants.DEFAULT_MAX_MESSAGE_SIZE + 1
|
||||
rv = place_order(client, n_bytes)
|
||||
assert_error(rv.get_json(), 'MESSAGE_FILE_TOO_LARGE')
|
||||
assert rv.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE
|
||||
|
||||
# The limit is different per channel. For instance, the default size would
|
||||
# work on the btc-src channel.
|
||||
rv = place_order(client,
|
||||
n_bytes,
|
||||
channel=constants.BTC_SRC_CHANNEL,
|
||||
admin=True)
|
||||
assert rv.status_code == HTTPStatus.OK
|
||||
|
||||
n_bytes = constants.CHANNEL_INFO[
|
||||
constants.BTC_SRC_CHANNEL].max_msg_size + 1
|
||||
rv = place_order(client, n_bytes)
|
||||
assert_error(rv.get_json(), 'MESSAGE_FILE_TOO_LARGE')
|
||||
assert rv.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE
|
||||
|
@ -83,7 +97,7 @@ def test_uploaded_file_too_large(client):
|
|||
|
||||
@patch('orders.new_invoice')
|
||||
def test_uploaded_file_max_size(mock_new_invoice, client):
|
||||
n_bytes = constants.MAX_MESSAGE_SIZE
|
||||
n_bytes = constants.DEFAULT_MAX_MESSAGE_SIZE
|
||||
mock_new_invoice.return_value = (True,
|
||||
new_invoice(1, InvoiceStatus.pending,
|
||||
bidding.get_min_bid(n_bytes)))
|
||||
|
|
Loading…
Add table
Reference in a new issue