Fix max squeaks per public key per block height config (#1896)

This commit is contained in:
Jonathan Zernik 2021-12-26 10:08:00 -08:00 committed by GitHub
parent 47ebb3c49f
commit 1a159a5f10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 20 deletions

View file

@ -9,7 +9,7 @@ config | type | valid values | has default | default value | environment variabl
node.network | string | ['mainnet', 'testnet', 'regtest', 'simnet'] | yes | "testnet" | SQUEAKNODE_NODE_NETWORK | Which network to use.
node.price_msat | int | [0,...] | yes | 10000 | SQUEAKNODE_NODE_PRICE_MSAT | The price to sell squeaks to other peers in millisatoshis.
node.max_squeaks | int | [0,...] | yes | 10000 | SQUEAKNODE_NODE_MAX_SQUEAKS | The absolute maximum number of squeaks allowed in the database.
node.max_squeaks_per_address_in_block_range | int | [0,...] | yes | 1000 | SQUEAKNODE_NODE_MAX_SQUEAKS_PER_ADDRESS_IN_BLOCK_RANGE | The maximum number of squeaks for an individual address in the recent block range.
node.max_squeaks_per_public_key_per_block | int | [0,...] | yes | 1000 | SQUEAKNODE_NODE_MAX_SQUEAKS_PER_PUBLIC_KEY_PER_BLOCK | The maximum number of squeaks for an any public key with any block height.
node.sqk_dir_path | string | | yes | "<USER_HOME>/.sqk" | SQUEAKNODE_NODE_SQK_DIR_PATH | The directory to store application data (only if using sqlite as database backend).
node.log_level | string | | yes | "INFO" | SQUEAKNODE_NODE_LOG_LEVEL | The log level to use.
node.sent_offer_retention_s | int | [0,...] | yes | 86400 | SQUEAKNODE_NODE_SENT_OFFER_RETENTION_S | The amount of time in seconds to keep a sent offer after expiry before deleting it.

View file

@ -1,7 +1,7 @@
[node]
network=simnet
price_msat=1000000
max_squeaks_per_address_in_block_range=50
max_squeaks_per_public_key_per_block=50
[lnd]
host=lnd

View file

@ -38,7 +38,7 @@ DEFAULT_NETWORK = "testnet"
DEFAULT_PRICE_MSAT = 10000
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_MAX_SQUEAKS = 10000
DEFAULT_MAX_SQUEAKS_PER_ADDRESS_IN_BLOCK_RANGE = 1000
DEFAULT_MAX_SQUEAKS_PER_PUBLIC_KEY_PER_BLOCK = 100
DEFAULT_SERVER_RPC_HOST = "0.0.0.0"
DEFAULT_SERVER_RPC_PORT = None
DEFAULT_ADMIN_RPC_HOST = "0.0.0.0"
@ -132,8 +132,8 @@ class NodeConfig(Config):
cast=int, required=False, default=DEFAULT_PRICE_MSAT)
max_squeaks = key(
cast=int, required=False, default=DEFAULT_MAX_SQUEAKS)
max_squeaks_per_address_in_block_range = key(
cast=int, required=False, default=DEFAULT_MAX_SQUEAKS_PER_ADDRESS_IN_BLOCK_RANGE)
max_squeaks_per_public_key_per_block = key(
cast=int, required=False, default=DEFAULT_MAX_SQUEAKS_PER_PUBLIC_KEY_PER_BLOCK)
sqk_dir_path = key(
cast=str, required=False, default=DEFAULT_SQK_DIR_PATH)
log_level = key(

View file

@ -666,21 +666,19 @@ class SqueakDb:
num_squeaks = row["num_squeaks"]
return num_squeaks
def number_of_squeaks_with_public_key_in_block_range(
def number_of_squeaks_with_public_key_with_block_height(
self,
public_key: SqueakPublicKey,
min_block: int,
max_block: int,
block_height: int,
) -> int:
""" Get number of squeaks with address in block range. """
""" Get number of squeaks with public key with block height. """
s = (
select([
func.count().label("num_squeaks"),
])
.select_from(self.squeaks)
.where(self.squeaks.c.author_public_key == public_key.to_bytes())
.where(self.squeaks.c.block_height >= min_block)
.where(self.squeaks.c.block_height <= max_block)
.where(self.squeaks.c.block_height == block_height)
)
with self.get_connection() as connection:
result = connection.execute(s)

View file

@ -173,7 +173,7 @@ class SqueakNode:
self.squeak_db,
self.squeak_core,
self.config.node.max_squeaks,
100, # TODO: update this with: max_squeaks_per_public_key_per_block
self.config.node.max_squeaks_per_public_key_per_block,
self.config.node.squeak_retention_s,
self.config.node.received_offer_retention_s,
self.config.node.sent_offer_retention_s,

View file

@ -77,7 +77,7 @@ class SqueakStore:
self.squeak_db = squeak_db
self.squeak_core = squeak_core
self.max_squeaks = max_squeaks
self.max_squeaks_per_public_key_per_block = max_squeaks_per_public_key_per_block,
self.max_squeaks_per_public_key_per_block = max_squeaks_per_public_key_per_block
self.squeak_retention_s = squeak_retention_s
self.received_offer_retention_s = received_offer_retention_s
self.sent_offer_retention_s = sent_offer_retention_s
@ -97,6 +97,12 @@ class SqueakStore:
if self.squeak_db.get_number_of_squeaks() >= self.max_squeaks:
raise Exception("Exceeded max number of squeaks.")
# TODO: Check if limit per public key per block is exceeded.
if self.squeak_db.number_of_squeaks_with_public_key_with_block_height(
squeak.GetPubKey(),
squeak.nBlockHeight,
) >= self.max_squeaks_per_public_key_per_block:
raise Exception(
"Exceeded max number of squeaks per public key per block.")
# Insert the squeak in db.
inserted_squeak_hash = self.squeak_db.insert_squeak(
squeak,

View file

@ -928,20 +928,18 @@ def test_get_number_of_squeaks(
assert num_squeaks == len(inserted_squeak_hashes)
def test_number_of_squeaks_with_public_key_in_block_range(
def test_number_of_squeaks_with_public_key_with_block_height(
squeak_db,
public_key,
inserted_squeak_hashes,
):
min_block = 43
max_block = 91
num_squeaks = squeak_db.number_of_squeaks_with_public_key_in_block_range(
block_height = 43
num_squeaks = squeak_db.number_of_squeaks_with_public_key_with_block_height(
public_key=public_key,
min_block=min_block,
max_block=max_block,
block_height=block_height,
)
assert num_squeaks == max_block - min_block + 1
assert num_squeaks == 1
def test_get_old_squeaks_to_delete(