From 1a159a5f10e8b113a2e45f07bc7885f9475ec2b3 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sun, 26 Dec 2021 10:08:00 -0800 Subject: [PATCH] Fix max squeaks per public key per block height config (#1896) --- docs/configuration.md | 2 +- itests/config.ini | 2 +- squeaknode/config/config.py | 6 +++--- squeaknode/db/squeak_db.py | 10 ++++------ squeaknode/node/squeak_node.py | 2 +- squeaknode/node/squeak_store.py | 8 +++++++- tests/db/test_squeak_db.py | 12 +++++------- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index ea933311..df5d78a1 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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 | "/.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. diff --git a/itests/config.ini b/itests/config.ini index 8d2fb80a..3ab842b6 100644 --- a/itests/config.ini +++ b/itests/config.ini @@ -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 diff --git a/squeaknode/config/config.py b/squeaknode/config/config.py index d06c9a3e..50c3122b 100644 --- a/squeaknode/config/config.py +++ b/squeaknode/config/config.py @@ -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( diff --git a/squeaknode/db/squeak_db.py b/squeaknode/db/squeak_db.py index 1858f9ec..f7a46fc6 100644 --- a/squeaknode/db/squeak_db.py +++ b/squeaknode/db/squeak_db.py @@ -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) diff --git a/squeaknode/node/squeak_node.py b/squeaknode/node/squeak_node.py index 3ec25328..3d9be3fc 100644 --- a/squeaknode/node/squeak_node.py +++ b/squeaknode/node/squeak_node.py @@ -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, diff --git a/squeaknode/node/squeak_store.py b/squeaknode/node/squeak_store.py index f8e7149b..16a2ebf5 100644 --- a/squeaknode/node/squeak_store.py +++ b/squeaknode/node/squeak_store.py @@ -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, diff --git a/tests/db/test_squeak_db.py b/tests/db/test_squeak_db.py index a67d218b..abcc0e5c 100644 --- a/tests/db/test_squeak_db.py +++ b/tests/db/test_squeak_db.py @@ -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(