From dfc8c67bb31fe8dfd6b3bf32b695d61cc611a43c Mon Sep 17 00:00:00 2001 From: Tim <roadscape@users.noreply.github.com> Date: Thu, 1 Jun 2017 16:13:17 -0400 Subject: [PATCH] core indexer to use block hash FK, will explode if forks --- hive/db/schema.py | 10 ++++++---- hive/indexer/core.py | 11 +++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/hive/db/schema.py b/hive/db/schema.py index 2e711e8d5..6b95de502 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -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 diff --git a/hive/indexer/core.py b/hive/indexer/core.py index 0ae9582ef..18959dbdb 100755 --- a/hive/indexer/core.py +++ b/hive/indexer/core.py @@ -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 = [] -- GitLab