From f7c5fabe4351bd5161f28f126ab11a370fdbb7eb Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Wed, 11 Nov 2020 00:30:23 -0500 Subject: [PATCH] Simplify squeak db table by using serialized squeak (#442) --- ...45_use_price_msat_in_sent_payment_table.py | 34 ------------------ ..._all.py => ccc641c2ce52_initialize_all.py} | 26 ++++++-------- squeaknode/db/models.py | 10 +----- squeaknode/db/squeak_db.py | 35 ++++--------------- 4 files changed, 19 insertions(+), 86 deletions(-) delete mode 100644 alembic/versions/434b1c53bc45_use_price_msat_in_sent_payment_table.py rename alembic/versions/{e771283d1959_initialize_all.py => ccc641c2ce52_initialize_all.py} (82%) diff --git a/alembic/versions/434b1c53bc45_use_price_msat_in_sent_payment_table.py b/alembic/versions/434b1c53bc45_use_price_msat_in_sent_payment_table.py deleted file mode 100644 index 82d1ef5f..00000000 --- a/alembic/versions/434b1c53bc45_use_price_msat_in_sent_payment_table.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Use price_msat in sent_payment table - -Revision ID: 434b1c53bc45 -Revises: e771283d1959 -Create Date: 2020-11-10 02:14:57.999127 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '434b1c53bc45' -down_revision = 'e771283d1959' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('sent_payment', schema=None) as batch_op: - batch_op.add_column(sa.Column('price_msat', sa.Integer(), nullable=False, server_default=sa.text("0"))) - batch_op.drop_column('amount') - - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('sent_payment', schema=None) as batch_op: - batch_op.add_column(sa.Column('amount', sa.INTEGER(), nullable=False, server_default=sa.text("0"))) - batch_op.drop_column('price_msat') - - # ### end Alembic commands ### diff --git a/alembic/versions/e771283d1959_initialize_all.py b/alembic/versions/ccc641c2ce52_initialize_all.py similarity index 82% rename from alembic/versions/e771283d1959_initialize_all.py rename to alembic/versions/ccc641c2ce52_initialize_all.py index 03e76a8e..18ead130 100644 --- a/alembic/versions/e771283d1959_initialize_all.py +++ b/alembic/versions/ccc641c2ce52_initialize_all.py @@ -1,8 +1,8 @@ """Initialize all -Revision ID: e771283d1959 +Revision ID: ccc641c2ce52 Revises: -Create Date: 2020-11-08 19:23:46.430021 +Create Date: 2020-11-10 23:45:44.333190 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'e771283d1959' +revision = 'ccc641c2ce52' down_revision = None branch_labels = None depends_on = None @@ -65,7 +65,7 @@ def upgrade(): sa.Column('squeak_hash', sa.String(length=64), nullable=False), sa.Column('preimage_hash', sa.String(length=64), nullable=False), sa.Column('preimage', sa.String(length=64), nullable=False), - sa.Column('amount', sa.Integer(), nullable=False), + sa.Column('price_msat', sa.Integer(), nullable=False), sa.Column('node_pubkey', sa.String(length=66), nullable=False), sa.Column('preimage_is_valid', sa.Boolean(), nullable=False), sa.PrimaryKeyConstraint('sent_payment_id') @@ -73,31 +73,27 @@ def upgrade(): op.create_table('squeak', sa.Column('hash', sa.String(length=64), nullable=False), sa.Column('created', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False), - sa.Column('n_version', sa.Integer(), nullable=False), - sa.Column('hash_enc_content', sa.String(length=64), nullable=False), + sa.Column('squeak', sa.Binary(), nullable=False), sa.Column('hash_reply_sqk', sa.String(length=64), nullable=False), sa.Column('hash_block', sa.String(length=64), nullable=False), sa.Column('n_block_height', sa.Integer(), nullable=False), - sa.Column('vch_script_pub_key', sa.Binary(), nullable=False), - sa.Column('vch_encryption_key', sa.Binary(), nullable=False), - sa.Column('enc_data_key', sa.String(), nullable=False), - sa.Column('iv', sa.String(length=64), nullable=False), sa.Column('n_time', sa.Integer(), nullable=False), - sa.Column('n_nonce', sa.BigInteger(), nullable=False), - sa.Column('enc_content', sa.String(length=2272), nullable=False), - sa.Column('vch_script_sig', sa.Binary(), nullable=False), sa.Column('author_address', sa.String(length=35), nullable=False), sa.Column('vch_decryption_key', sa.Binary(), nullable=True), sa.Column('block_header', sa.Binary(), nullable=True), sa.PrimaryKeyConstraint('hash') ) - op.create_index(op.f('ix_squeak_author_address'), 'squeak', ['author_address'], unique=False) + with op.batch_alter_table('squeak', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_squeak_author_address'), ['author_address'], unique=False) + # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_squeak_author_address'), table_name='squeak') + with op.batch_alter_table('squeak', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_squeak_author_address')) + op.drop_table('squeak') op.drop_table('sent_payment') op.drop_table('profile') diff --git a/squeaknode/db/models.py b/squeaknode/db/models.py index 053d0492..73200f75 100644 --- a/squeaknode/db/models.py +++ b/squeaknode/db/models.py @@ -41,19 +41,11 @@ class Models: self.metadata, Column("hash", String(64), primary_key=True), Column("created", DateTime, server_default=func.now(), nullable=False), - Column("n_version", Integer, nullable=False), - Column("hash_enc_content", String(64), nullable=False), + Column("squeak", Binary, nullable=False), Column("hash_reply_sqk", String(64), nullable=False), Column("hash_block", String(64), nullable=False), Column("n_block_height", Integer, nullable=False), - Column("vch_script_pub_key", Binary, nullable=False), - Column("vch_encryption_key", Binary, nullable=False), - Column("enc_data_key", String, nullable=False), - Column("iv", String(64), nullable=False), Column("n_time", Integer, nullable=False), - Column("n_nonce", BigInteger, nullable=False), - Column("enc_content", String(2272), nullable=False), - Column("vch_script_sig", Binary, nullable=False), Column("author_address", String(35), index=True, nullable=False), Column("vch_decryption_key", Binary, nullable=True), Column("block_header", Binary, nullable=True), diff --git a/squeaknode/db/squeak_db.py b/squeaknode/db/squeak_db.py index f31299a4..57428ff9 100644 --- a/squeaknode/db/squeak_db.py +++ b/squeaknode/db/squeak_db.py @@ -82,25 +82,17 @@ class SqueakDb: def insert_squeak(self, squeak): """ Insert a new squeak. """ + vch_decryption_key = squeak.GetDecryptionKey().get_bytes() if squeak.HasDecryptionKey() else None + squeak.ClearDecryptionKey() ins = self.squeaks.insert().values( hash=get_hash(squeak), - n_version=squeak.nVersion, - hash_enc_content=squeak.hashEncContent.hex(), + squeak=squeak.serialize(), hash_reply_sqk=squeak.hashReplySqk.hex(), hash_block=squeak.hashBlock.hex(), n_block_height=squeak.nBlockHeight, - vch_script_pub_key=squeak.vchScriptPubKey, - vch_encryption_key=squeak.vchEncryptionKey, - enc_data_key=squeak.encDatakey.hex(), - iv=squeak.iv.hex(), n_time=squeak.nTime, - n_nonce=squeak.nNonce, - enc_content=squeak.encContent.hex(), - vch_script_sig=squeak.vchScriptSig, author_address=str(squeak.GetAddress()), - vch_decryption_key=squeak.GetDecryptionKey().get_bytes() - if squeak.HasDecryptionKey() - else None, + vch_decryption_key=vch_decryption_key, ) with self.get_connection() as connection: try: @@ -942,22 +934,9 @@ class SqueakDb: vch_decryption_key = ( bytes(vch_decryption_key_column) if vch_decryption_key_column else b"" ) - squeak = CSqueak( - nVersion=row["n_version"], - hashEncContent=bytes.fromhex(row["hash_enc_content"]), - hashReplySqk=bytes.fromhex(row["hash_reply_sqk"]), - hashBlock=bytes.fromhex(row["hash_block"]), - nBlockHeight=row["n_block_height"], - vchScriptPubKey=bytes(row["vch_script_pub_key"]), - vchEncryptionKey=bytes(row["vch_encryption_key"]), - encDatakey=bytes.fromhex(row["enc_data_key"]), - iv=bytes.fromhex((row["iv"])), - nTime=row["n_time"], - nNonce=row["n_nonce"], - encContent=bytes.fromhex((row["enc_content"])), - vchScriptSig=bytes(row["vch_script_sig"]), - vchDecryptionKey=vch_decryption_key, - ) + squeak = CSqueak.deserialize(row["squeak"]) + if vch_decryption_key: + squeak.SetDecryptionKey(vch_decryption_key) block_header_column = row["block_header"] block_header_bytes = bytes(block_header_column) if block_header_column else None block_header = (