Skip to content
Snippets Groups Projects
Commit dfc8c67b authored by Tim's avatar Tim
Browse files

core indexer to use block hash FK, will explode if forks

parent 1d71d3d2
No related branches found
No related tags found
No related merge requests found
......@@ -12,11 +12,13 @@ metadata = sa.MetaData()
hive_blocks = sa.Table(
'hive_blocks', metadata,
sa.Column('num', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('prev', sa.Integer),
sa.Column('hash', CHAR(40, ascii=True), nullable=False),
sa.Column('prev', CHAR(40, ascii=True)),
sa.Column('txs', SMALLINT(unsigned=True), server_default='0', nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.UniqueConstraint('prev', name='hive_blocks_ux1'),
sa.ForeignKeyConstraint(['prev'], ['hive_blocks.num'], name='hive_blocks_fk1'),
sa.UniqueConstraint('hash', name='hive_blocks_ux1'),
sa.UniqueConstraint('prev', name='hive_blocks_ux2'),
sa.ForeignKeyConstraint(['prev'], ['hive_blocks.hash'], name='hive_blocks_fk1'),
mysql_engine='InnoDB',
mysql_default_charset='utf8mb4'
)
......@@ -203,7 +205,7 @@ def setup(connection_url=_url):
conn = engine.connect()
# Insert hive_blocks data
insert = hive_blocks.insert().values(num=0, prev=None, created_at='1970-01-01T00:00:00')
insert = hive_blocks.insert().values(num=0, hash='0000000000000000000000000000000000000000', prev=None, created_at='1970-01-01T00:00:00')
conn.execute(insert)
# Insert hive_accounts data
......
......@@ -311,14 +311,13 @@ def is_community(name: str) -> bool:
# -----------
def process_block(block):
date = parse_time(block['timestamp'])
block_num = int(block['previous'][:8], base=16) + 1
block_id = block['block_id']
prev = block['previous']
block_num = int(block_id[:8], base=16)
txs = block['transactions']
# NOTE: currently `prev` tracks the previous block number and this is enforced with a FK constraint.
# soon we will have access to prev block hash and current hash in the API return value, we should use this instead.
# the FK constraint will then fail if we somehow end up on the wrong side in a fork reorg.
query("INSERT INTO hive_blocks (num, prev, txs, created_at) "
"VALUES ('%d', '%d', '%d', '%s')" % (block_num, block_num - 1, len(txs), date))
query("INSERT INTO hive_blocks (num, hash, prev, txs, created_at) "
"VALUES (%d, '%s', '%s', %d, '%s')" % (block_num, block_id, prev, len(txs), date))
accounts = set()
comments = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment