From f1295bf82c53a7a2e079fcb84af036243a893f80 Mon Sep 17 00:00:00 2001 From: Yostra Date: Thu, 17 Sep 2020 01:58:33 -0700 Subject: [PATCH] save block timestamp --- src/wallet/block_record.py | 1 + src/wallet/wallet_node.py | 6 ++++++ src/wallet/wallet_state_manager.py | 1 + src/wallet/wallet_store.py | 25 +++++++++++++++++++++---- tests/wallet/test_wallet_store.py | 3 +++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/wallet/block_record.py b/src/wallet/block_record.py index 94e0e7a0..463d063f 100644 --- a/src/wallet/block_record.py +++ b/src/wallet/block_record.py @@ -23,3 +23,4 @@ class BlockRecord(Streamable): removals: Optional[List[Coin]] # A block record without removals is not finished total_iters: Optional[uint64] new_challenge_hash: Optional[bytes32] + timestamp: uint64 diff --git a/src/wallet/wallet_node.py b/src/wallet/wallet_node.py index bbe1c0ef..1343fbcc 100644 --- a/src/wallet/wallet_node.py +++ b/src/wallet/wallet_node.py @@ -599,6 +599,7 @@ class WalletNode: [], total_iters, None, + uint64(0), ) res = await self.wallet_state_manager.receive_block(block_record, None) assert ( @@ -903,6 +904,7 @@ class WalletNode: None, response.header_block.header.data.total_iters, response.header_block.challenge.get_hash(), + response.header_block.header.data.timestamp, ) if self.wallet_state_manager.sync_mode: @@ -970,6 +972,7 @@ class WalletNode: [], block_record.total_iters, block_record.new_challenge_hash, + block_record.timestamp, ) respond_header_msg: Optional[ wallet_protocol.RespondHeader @@ -1074,6 +1077,7 @@ class WalletNode: None, block_record.total_iters, header_block.challenge.get_hash(), + header_block.header.data.timestamp, ) self.cached_blocks[response.header_hash] = ( new_br, @@ -1128,6 +1132,7 @@ class WalletNode: [], new_br.total_iters, new_br.new_challenge_hash, + new_br.timestamp, ) respond_header_msg: Optional[ wallet_protocol.RespondHeader @@ -1210,6 +1215,7 @@ class WalletNode: all_coins, block_record.total_iters, header_block.challenge.get_hash(), + header_block.header.data.timestamp, ) self.cached_blocks[response.header_hash] = ( diff --git a/src/wallet/wallet_state_manager.py b/src/wallet/wallet_state_manager.py index a642a0dc..ed6cfd62 100644 --- a/src/wallet/wallet_state_manager.py +++ b/src/wallet/wallet_state_manager.py @@ -206,6 +206,7 @@ class WalletStateManager: [], genesis_hb.header.data.total_iters, genesis_challenge.get_hash(), + genesis_hb.header.data.timestamp, ), genesis_hb, ) diff --git a/src/wallet/wallet_store.py b/src/wallet/wallet_store.py index 9a2e61da..e83b6480 100644 --- a/src/wallet/wallet_store.py +++ b/src/wallet/wallet_store.py @@ -43,7 +43,7 @@ class WalletStore: ) await self.db_connection.execute( "CREATE TABLE IF NOT EXISTS block_records(header_hash text PRIMARY KEY, height int," - " in_lca_path tinyint, block blob)" + " in_lca_path tinyint, timestamp int, block blob)" ) # Useful for reorg lookups @@ -71,6 +71,22 @@ class WalletStore: "CREATE INDEX IF NOT EXISTS wallet_id on coin_record(wallet_id)" ) + await self.db_connection.execute( + "CREATE INDEX IF NOT EXISTS header_hash on block_records(header_hash)" + ) + + await self.db_connection.execute( + "CREATE INDEX IF NOT EXISTS timestamp on block_records(timestamp)" + ) + + await self.db_connection.execute( + "CREATE INDEX IF NOT EXISTS height on block_records(height)" + ) + + await self.db_connection.execute( + "CREATE INDEX IF NOT EXISTS in_lca_path on block_records(in_lca_path)" + ) + await self.db_connection.commit() self.coin_record_cache = dict() return self @@ -350,7 +366,7 @@ class WalletStore: hash_to_br: Dict = {} max_height = -1 for row in rows: - br = BlockRecord.from_bytes(row[3]) + br = BlockRecord.from_bytes(row[4]) hash_to_br[bytes.fromhex(row[0])] = br assert row[0] == br.header_hash.hex() assert row[1] == br.height @@ -366,11 +382,12 @@ class WalletStore: to the chain, but it may or may not be in the LCA path. """ cursor = await self.db_connection.execute( - "INSERT OR REPLACE INTO block_records VALUES(?, ?, ?, ?)", + "INSERT OR REPLACE INTO block_records VALUES(?, ?, ?, ?, ?)", ( block_record.header_hash.hex(), block_record.height, in_lca_path, + block_record.timestamp, bytes(block_record), ), ) @@ -385,7 +402,7 @@ class WalletStore: row = await cursor.fetchone() await cursor.close() if row is not None: - return BlockRecord.from_bytes(row[3]) + return BlockRecord.from_bytes(row[4]) else: return None diff --git a/tests/wallet/test_wallet_store.py b/tests/wallet/test_wallet_store.py index 69ac3f61..543ee9da 100644 --- a/tests/wallet/test_wallet_store.py +++ b/tests/wallet/test_wallet_store.py @@ -150,6 +150,7 @@ class TestWalletStore: None, None, None, + uint64(0), ) assert await store.get_block_record(br_1.header_hash) is None await store.add_block_record(br_1, False) @@ -171,6 +172,7 @@ class TestWalletStore: None, None, None, + uint64(0), ) await store.add_block_record(br_2, False) assert len(await store.get_lca_path()) == 1 @@ -187,6 +189,7 @@ class TestWalletStore: None, None, None, + uint64(0), ) await store.add_block_record(br_3, True) assert len(await store.get_lca_path()) == 3