2020-04-12 02:01:10 +02:00
|
|
|
# pylint: skip-file
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
from sqlalchemy import (
|
2020-07-14 03:00:24 +02:00
|
|
|
MetaData, Table, Column, ForeignKey,
|
2020-07-06 05:03:45 +02:00
|
|
|
LargeBinary, Text, SmallInteger, Integer, BigInteger, Boolean,
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
2020-07-13 18:38:28 +02:00
|
|
|
from .constants import TXO_TYPES, CLAIM_TYPE_CODES
|
2020-04-11 23:27:41 +02:00
|
|
|
|
|
|
|
|
2020-05-01 15:29:44 +02:00
|
|
|
SCHEMA_VERSION = '1.4'
|
|
|
|
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
metadata = MetaData()
|
|
|
|
|
|
|
|
|
|
|
|
Version = Table(
|
|
|
|
'version', metadata,
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('version', Text, primary_key=True),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-09 16:47:44 +02:00
|
|
|
Wallet = Table(
|
|
|
|
'wallet', metadata,
|
|
|
|
Column('wallet_id', Text, primary_key=True),
|
|
|
|
Column('data', Text),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
PubkeyAddress = Table(
|
|
|
|
'pubkey_address', metadata,
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('address', Text, primary_key=True),
|
|
|
|
Column('used_times', Integer, server_default='0'),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
AccountAddress = Table(
|
|
|
|
'account_address', metadata,
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('account', Text, primary_key=True),
|
|
|
|
Column('address', Text, ForeignKey(PubkeyAddress.columns.address), primary_key=True),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('chain', SmallInteger),
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('pubkey', LargeBinary),
|
|
|
|
Column('chain_code', LargeBinary),
|
|
|
|
Column('n', Integer),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('depth', SmallInteger),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-21 22:48:40 +02:00
|
|
|
pg_add_account_address_constraints_and_indexes = [
|
|
|
|
"CREATE UNIQUE INDEX account_address_idx ON account_address (account, address);"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
Block = Table(
|
|
|
|
'block', metadata,
|
2020-09-21 16:26:19 +02:00
|
|
|
Column('height', Integer, primary_key=True),
|
|
|
|
Column('block_hash', LargeBinary),
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('previous_hash', LargeBinary),
|
|
|
|
Column('file_number', SmallInteger),
|
2020-06-19 20:28:34 +02:00
|
|
|
Column('timestamp', Integer),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
|
|
|
|
2020-09-17 17:25:50 +02:00
|
|
|
pg_add_block_constraints_and_indexes = [
|
2020-09-21 16:26:19 +02:00
|
|
|
"ALTER TABLE block ADD PRIMARY KEY (height);",
|
2020-09-17 17:25:50 +02:00
|
|
|
]
|
|
|
|
|
2020-09-21 16:26:19 +02:00
|
|
|
|
2020-09-17 17:25:50 +02:00
|
|
|
BlockFilter = Table(
|
|
|
|
'block_filter', metadata,
|
2020-09-21 16:26:19 +02:00
|
|
|
Column('height', Integer, primary_key=True),
|
|
|
|
Column('address_filter', LargeBinary),
|
2020-09-17 17:25:50 +02:00
|
|
|
)
|
2020-09-21 16:26:19 +02:00
|
|
|
|
2020-09-17 17:25:50 +02:00
|
|
|
pg_add_block_filter_constraints_and_indexes = [
|
2020-09-21 16:26:19 +02:00
|
|
|
"ALTER TABLE block_filter ADD PRIMARY KEY (height);",
|
|
|
|
"ALTER TABLE block_filter ADD CONSTRAINT fk_block_filter"
|
|
|
|
" FOREIGN KEY (height) REFERENCES block (height) ON DELETE CASCADE;",
|
2020-09-17 17:25:50 +02:00
|
|
|
]
|
2020-04-11 23:27:41 +02:00
|
|
|
|
2020-09-21 16:26:19 +02:00
|
|
|
|
|
|
|
BlockGroupFilter = Table(
|
|
|
|
'block_group_filter', metadata,
|
|
|
|
Column('height', Integer),
|
|
|
|
Column('factor', SmallInteger),
|
|
|
|
Column('address_filter', LargeBinary),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
TX = Table(
|
|
|
|
'tx', metadata,
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('tx_hash', LargeBinary, primary_key=True),
|
|
|
|
Column('raw', LargeBinary),
|
|
|
|
Column('height', Integer),
|
|
|
|
Column('position', SmallInteger),
|
2020-06-19 20:28:34 +02:00
|
|
|
Column('timestamp', Integer, nullable=True),
|
|
|
|
Column('day', Integer, nullable=True),
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('is_verified', Boolean, server_default='FALSE'),
|
|
|
|
Column('purchased_claim_hash', LargeBinary, nullable=True),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
|
|
|
|
2020-07-14 03:00:24 +02:00
|
|
|
pg_add_tx_constraints_and_indexes = [
|
|
|
|
"ALTER TABLE tx ADD PRIMARY KEY (tx_hash);",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-21 16:26:19 +02:00
|
|
|
TXFilter = Table(
|
|
|
|
'tx_filter', metadata,
|
|
|
|
Column('tx_hash', LargeBinary, primary_key=True),
|
|
|
|
Column('height', Integer),
|
|
|
|
Column('address_filter', LargeBinary),
|
|
|
|
)
|
|
|
|
|
|
|
|
pg_add_tx_filter_constraints_and_indexes = [
|
|
|
|
"ALTER TABLE tx_filter ADD PRIMARY KEY (tx_hash);",
|
|
|
|
"ALTER TABLE tx_filter ADD CONSTRAINT fk_tx_filter"
|
|
|
|
" FOREIGN KEY (tx_hash) REFERENCES tx (tx_hash) ON DELETE CASCADE;"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
MempoolFilter = Table(
|
|
|
|
'mempool_filter', metadata,
|
|
|
|
Column('filter_number', Integer),
|
|
|
|
Column('mempool_filter', LargeBinary),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
TXO = Table(
|
|
|
|
'txo', metadata,
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('tx_hash', LargeBinary, ForeignKey(TX.columns.tx_hash)),
|
|
|
|
Column('txo_hash', LargeBinary, primary_key=True),
|
|
|
|
Column('address', Text),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('position', SmallInteger),
|
2020-04-12 17:59:00 +02:00
|
|
|
Column('amount', BigInteger),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('height', Integer),
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('spent_height', Integer, server_default='0'),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('script_offset', Integer),
|
|
|
|
Column('script_length', Integer),
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('is_reserved', Boolean, server_default='0'),
|
2020-06-22 01:51:09 +02:00
|
|
|
|
|
|
|
# claims
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('txo_type', SmallInteger, server_default='0'),
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('claim_id', Text, nullable=True),
|
|
|
|
Column('claim_hash', LargeBinary, nullable=True),
|
|
|
|
Column('claim_name', Text, nullable=True),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('channel_hash', LargeBinary, nullable=True), # claims in channel
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('signature', LargeBinary, nullable=True),
|
|
|
|
Column('signature_digest', LargeBinary, nullable=True),
|
2020-06-22 01:51:09 +02:00
|
|
|
|
2020-09-24 21:19:30 +02:00
|
|
|
# reposts
|
|
|
|
Column('reposted_claim_hash', LargeBinary, nullable=True),
|
|
|
|
|
2020-06-22 01:51:09 +02:00
|
|
|
# channels
|
|
|
|
Column('public_key', LargeBinary, nullable=True),
|
|
|
|
Column('public_key_hash', LargeBinary, nullable=True),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
|
|
|
|
2020-04-12 17:06:05 +02:00
|
|
|
txo_join_account = TXO.join(AccountAddress, TXO.columns.address == AccountAddress.columns.address)
|
|
|
|
|
2020-07-14 03:00:24 +02:00
|
|
|
pg_add_txo_constraints_and_indexes = [
|
|
|
|
"ALTER TABLE txo ADD PRIMARY KEY (txo_hash);",
|
2020-07-13 18:38:28 +02:00
|
|
|
# find appropriate channel public key for signing a content claim
|
2020-07-14 03:00:24 +02:00
|
|
|
f"CREATE INDEX txo_channel_hash_by_height_desc_w_pub_key "
|
|
|
|
f"ON txo (claim_hash, height desc) INCLUDE (public_key) "
|
|
|
|
f"WHERE txo_type={TXO_TYPES['channel']};",
|
|
|
|
# for calculating supports on a claim
|
|
|
|
f"CREATE INDEX txo_unspent_supports ON txo (claim_hash) INCLUDE (amount) "
|
|
|
|
f"WHERE spent_height = 0 AND txo_type={TXO_TYPES['support']};",
|
2020-10-21 19:53:37 +02:00
|
|
|
# for calculating balance
|
2020-10-21 22:48:40 +02:00
|
|
|
f"CREATE INDEX txo_unspent_by_address ON txo (address) INCLUDE (amount, txo_type, tx_hash) "
|
2020-10-21 19:53:37 +02:00
|
|
|
f"WHERE spent_height = 0;",
|
2020-07-14 03:00:24 +02:00
|
|
|
# for finding modified claims in a block range
|
|
|
|
f"CREATE INDEX txo_claim_changes "
|
2020-07-16 16:53:03 +02:00
|
|
|
f"ON txo (height DESC) INCLUDE (claim_hash, txo_hash) "
|
2020-07-14 03:00:24 +02:00
|
|
|
f"WHERE spent_height = 0 AND txo_type IN {tuple(CLAIM_TYPE_CODES)};",
|
|
|
|
# for finding claims which need support totals re-calculated in a block range
|
|
|
|
f"CREATE INDEX txo_added_supports_by_height ON txo (height DESC) "
|
|
|
|
f"INCLUDE (claim_hash) WHERE txo_type={TXO_TYPES['support']};",
|
|
|
|
f"CREATE INDEX txo_spent_supports_by_height ON txo (spent_height DESC) "
|
|
|
|
f"INCLUDE (claim_hash) WHERE txo_type={TXO_TYPES['support']};",
|
2020-09-24 21:19:30 +02:00
|
|
|
# for finding claims which need repost totals re-calculated in a block range
|
|
|
|
f"CREATE INDEX txo_added_reposts_by_height ON txo (height DESC) "
|
|
|
|
f"INCLUDE (reposted_claim_hash) WHERE txo_type={TXO_TYPES['repost']};",
|
|
|
|
f"CREATE INDEX txo_spent_reposts_by_height ON txo (spent_height DESC) "
|
|
|
|
f"INCLUDE (reposted_claim_hash) WHERE txo_type={TXO_TYPES['repost']};",
|
|
|
|
"CREATE INDEX txo_reposted_claim_hash ON txo (reposted_claim_hash)"
|
|
|
|
"WHERE reposted_claim_hash IS NOT NULL AND spent_height = 0;",
|
2020-09-21 16:26:19 +02:00
|
|
|
"CREATE INDEX txo_height ON txo (height);",
|
2020-11-11 22:56:32 +01:00
|
|
|
# used by sum_supports query (at least)
|
|
|
|
"CREATE INDEX txo_claim_hash ON txo (claim_hash)",
|
2020-07-14 03:00:24 +02:00
|
|
|
]
|
2020-07-06 05:03:45 +02:00
|
|
|
|
|
|
|
|
2020-04-11 23:27:41 +02:00
|
|
|
TXI = Table(
|
|
|
|
'txi', metadata,
|
2020-04-12 17:06:05 +02:00
|
|
|
Column('tx_hash', LargeBinary, ForeignKey(TX.columns.tx_hash)),
|
|
|
|
Column('txo_hash', LargeBinary, ForeignKey(TXO.columns.txo_hash), primary_key=True),
|
2020-05-01 15:29:44 +02:00
|
|
|
Column('address', Text, nullable=True),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('position', SmallInteger),
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('height', Integer),
|
2020-04-11 23:27:41 +02:00
|
|
|
)
|
2020-04-12 17:06:05 +02:00
|
|
|
|
|
|
|
txi_join_account = TXI.join(AccountAddress, TXI.columns.address == AccountAddress.columns.address)
|
2020-05-01 15:29:44 +02:00
|
|
|
|
2020-07-14 03:00:24 +02:00
|
|
|
pg_add_txi_constraints_and_indexes = [
|
|
|
|
"ALTER TABLE txi ADD PRIMARY KEY (txo_hash);",
|
2020-09-21 16:26:19 +02:00
|
|
|
"CREATE INDEX txi_height ON txi (height);",
|
2020-07-14 03:00:24 +02:00
|
|
|
]
|
2020-07-06 05:03:45 +02:00
|
|
|
|
|
|
|
|
2020-05-01 15:29:44 +02:00
|
|
|
Claim = Table(
|
|
|
|
'claim', metadata,
|
|
|
|
Column('claim_hash', LargeBinary, primary_key=True),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('claim_id', Text),
|
2020-05-01 15:29:44 +02:00
|
|
|
Column('claim_name', Text),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('normalized', Text),
|
|
|
|
Column('address', Text),
|
2020-05-01 15:29:44 +02:00
|
|
|
Column('txo_hash', LargeBinary, ForeignKey(TXO.columns.txo_hash)),
|
|
|
|
Column('amount', BigInteger),
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('staked_amount', BigInteger),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('timestamp', Integer), # last updated timestamp
|
|
|
|
Column('creation_timestamp', Integer),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('release_time', Integer, nullable=True),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('height', Integer), # last updated height
|
|
|
|
Column('creation_height', Integer),
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('activation_height', Integer),
|
|
|
|
Column('expiration_height', Integer),
|
2020-06-19 20:28:34 +02:00
|
|
|
Column('takeover_height', Integer, nullable=True),
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('is_controlling', Boolean),
|
2020-05-02 05:25:07 +02:00
|
|
|
|
2020-07-13 15:30:32 +02:00
|
|
|
# short_url: normalized#shortest-unique-claim_id
|
2020-07-06 05:03:45 +02:00
|
|
|
Column('short_url', Text),
|
2020-07-13 15:30:32 +02:00
|
|
|
# canonical_url: channel's-short_url/normalized#shortest-unique-claim_id-within-channel
|
|
|
|
# canonical_url is computed dynamically
|
2020-05-02 05:25:07 +02:00
|
|
|
|
|
|
|
Column('title', Text, nullable=True),
|
|
|
|
Column('author', Text, nullable=True),
|
|
|
|
Column('description', Text, nullable=True),
|
|
|
|
|
|
|
|
Column('claim_type', SmallInteger),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('staked_support_count', Integer, server_default='0'),
|
|
|
|
Column('staked_support_amount', BigInteger, server_default='0'),
|
2020-05-02 05:25:07 +02:00
|
|
|
|
|
|
|
# streams
|
2020-06-30 23:32:51 +02:00
|
|
|
Column('stream_type', SmallInteger, nullable=True),
|
2020-05-02 05:25:07 +02:00
|
|
|
Column('media_type', Text, nullable=True),
|
|
|
|
Column('fee_amount', BigInteger, server_default='0'),
|
|
|
|
Column('fee_currency', Text, nullable=True),
|
|
|
|
Column('duration', Integer, nullable=True),
|
|
|
|
|
|
|
|
# reposts
|
2020-09-24 21:19:30 +02:00
|
|
|
Column('reposted_claim_hash', LargeBinary, nullable=True), # on claim doing the repost
|
|
|
|
Column('reposted_count', Integer, server_default='0'), # on claim being reposted
|
2020-05-02 05:25:07 +02:00
|
|
|
|
|
|
|
# claims which are channels
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('signed_claim_count', Integer, server_default='0'),
|
|
|
|
Column('signed_support_count', Integer, server_default='0'),
|
2020-05-02 05:25:07 +02:00
|
|
|
|
|
|
|
# claims which are inside channels
|
2020-05-01 15:29:44 +02:00
|
|
|
Column('channel_hash', LargeBinary, nullable=True),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('is_signature_valid', Boolean, nullable=True),
|
2020-05-02 05:25:07 +02:00
|
|
|
|
|
|
|
Column('trending_group', BigInteger, server_default='0'),
|
|
|
|
Column('trending_mixed', BigInteger, server_default='0'),
|
|
|
|
Column('trending_local', BigInteger, server_default='0'),
|
|
|
|
Column('trending_global', BigInteger, server_default='0'),
|
2020-05-01 15:29:44 +02:00
|
|
|
)
|
|
|
|
|
2020-07-13 20:29:38 +02:00
|
|
|
Tag = Table(
|
|
|
|
'tag', metadata,
|
|
|
|
Column('claim_hash', LargeBinary),
|
|
|
|
Column('tag', Text),
|
|
|
|
)
|
|
|
|
|
2020-07-14 03:00:24 +02:00
|
|
|
pg_add_claim_and_tag_constraints_and_indexes = [
|
|
|
|
"ALTER TABLE claim ADD PRIMARY KEY (claim_hash);",
|
|
|
|
# for checking if claim is up-to-date
|
|
|
|
"CREATE UNIQUE INDEX claim_txo_hash ON claim (txo_hash);",
|
|
|
|
# used by takeover process to reset winning claims
|
|
|
|
"CREATE INDEX claim_normalized ON claim (normalized);",
|
2020-08-10 23:48:26 +02:00
|
|
|
# ordering and search by release_time
|
|
|
|
"CREATE INDEX claim_release_time ON claim (release_time DESC NULLs LAST);",
|
2020-07-14 03:00:24 +02:00
|
|
|
# used to count()/sum() claims signed by channel
|
|
|
|
"CREATE INDEX signed_content ON claim (channel_hash) "
|
|
|
|
"INCLUDE (amount) WHERE is_signature_valid;",
|
2020-09-24 21:19:30 +02:00
|
|
|
# used to count()/sum() reposted claims
|
|
|
|
"CREATE INDEX reposted_content ON claim (reposted_claim_hash);",
|
2020-07-14 03:00:24 +02:00
|
|
|
# basic tag indexes
|
|
|
|
"ALTER TABLE tag ADD PRIMARY KEY (claim_hash, tag);",
|
|
|
|
"CREATE INDEX tags ON tag (tag) INCLUDE (claim_hash);",
|
2020-11-11 22:56:32 +01:00
|
|
|
# used by sum_supports query (at least)
|
|
|
|
"CREATE INDEX claim_channel_hash ON claim (channel_hash)",
|
2020-07-14 03:00:24 +02:00
|
|
|
]
|
2020-07-13 06:55:30 +02:00
|
|
|
|
|
|
|
|
2020-06-05 06:35:22 +02:00
|
|
|
Support = Table(
|
|
|
|
'support', metadata,
|
2020-06-19 20:28:34 +02:00
|
|
|
|
|
|
|
Column('txo_hash', LargeBinary, ForeignKey(TXO.columns.txo_hash), primary_key=True),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('claim_hash', LargeBinary),
|
2020-06-19 20:28:34 +02:00
|
|
|
Column('address', Text),
|
|
|
|
Column('amount', BigInteger),
|
|
|
|
Column('height', Integer),
|
2020-07-12 22:27:51 +02:00
|
|
|
Column('timestamp', Integer),
|
2020-06-19 20:28:34 +02:00
|
|
|
|
|
|
|
# support metadata
|
|
|
|
Column('emoji', Text),
|
|
|
|
|
|
|
|
# signed supports
|
|
|
|
Column('channel_hash', LargeBinary, nullable=True),
|
|
|
|
Column('signature', LargeBinary, nullable=True),
|
|
|
|
Column('signature_digest', LargeBinary, nullable=True),
|
2020-06-22 01:51:09 +02:00
|
|
|
Column('is_signature_valid', Boolean, nullable=True),
|
2020-06-05 06:35:22 +02:00
|
|
|
)
|
2020-07-13 06:55:30 +02:00
|
|
|
|
2020-07-14 03:00:24 +02:00
|
|
|
pg_add_support_constraints_and_indexes = [
|
|
|
|
"ALTER TABLE support ADD PRIMARY KEY (txo_hash);",
|
|
|
|
# used to count()/sum() supports signed by channel
|
|
|
|
"CREATE INDEX signed_support ON support (channel_hash) "
|
|
|
|
"INCLUDE (amount) WHERE is_signature_valid;",
|
|
|
|
]
|
2020-08-10 23:48:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
Stake = Table(
|
|
|
|
'stake', metadata,
|
|
|
|
Column('claim_hash', LargeBinary),
|
|
|
|
Column('height', Integer),
|
|
|
|
Column('stake_min', BigInteger),
|
|
|
|
Column('stake_max', BigInteger),
|
|
|
|
Column('stake_sum', BigInteger),
|
|
|
|
Column('stake_count', Integer),
|
|
|
|
Column('stake_unique', Integer),
|
|
|
|
)
|