save block timestamp

This commit is contained in:
Yostra 2020-09-17 01:58:33 -07:00 committed by Gene Hoffman
parent 6b92c75043
commit f1295bf82c
5 changed files with 32 additions and 4 deletions

View File

@ -23,3 +23,4 @@ class BlockRecord(Streamable):
removals: Optional[List[Coin]] # A block record without removals is not finished removals: Optional[List[Coin]] # A block record without removals is not finished
total_iters: Optional[uint64] total_iters: Optional[uint64]
new_challenge_hash: Optional[bytes32] new_challenge_hash: Optional[bytes32]
timestamp: uint64

View File

@ -599,6 +599,7 @@ class WalletNode:
[], [],
total_iters, total_iters,
None, None,
uint64(0),
) )
res = await self.wallet_state_manager.receive_block(block_record, None) res = await self.wallet_state_manager.receive_block(block_record, None)
assert ( assert (
@ -903,6 +904,7 @@ class WalletNode:
None, None,
response.header_block.header.data.total_iters, response.header_block.header.data.total_iters,
response.header_block.challenge.get_hash(), response.header_block.challenge.get_hash(),
response.header_block.header.data.timestamp,
) )
if self.wallet_state_manager.sync_mode: if self.wallet_state_manager.sync_mode:
@ -970,6 +972,7 @@ class WalletNode:
[], [],
block_record.total_iters, block_record.total_iters,
block_record.new_challenge_hash, block_record.new_challenge_hash,
block_record.timestamp,
) )
respond_header_msg: Optional[ respond_header_msg: Optional[
wallet_protocol.RespondHeader wallet_protocol.RespondHeader
@ -1074,6 +1077,7 @@ class WalletNode:
None, None,
block_record.total_iters, block_record.total_iters,
header_block.challenge.get_hash(), header_block.challenge.get_hash(),
header_block.header.data.timestamp,
) )
self.cached_blocks[response.header_hash] = ( self.cached_blocks[response.header_hash] = (
new_br, new_br,
@ -1128,6 +1132,7 @@ class WalletNode:
[], [],
new_br.total_iters, new_br.total_iters,
new_br.new_challenge_hash, new_br.new_challenge_hash,
new_br.timestamp,
) )
respond_header_msg: Optional[ respond_header_msg: Optional[
wallet_protocol.RespondHeader wallet_protocol.RespondHeader
@ -1210,6 +1215,7 @@ class WalletNode:
all_coins, all_coins,
block_record.total_iters, block_record.total_iters,
header_block.challenge.get_hash(), header_block.challenge.get_hash(),
header_block.header.data.timestamp,
) )
self.cached_blocks[response.header_hash] = ( self.cached_blocks[response.header_hash] = (

View File

@ -206,6 +206,7 @@ class WalletStateManager:
[], [],
genesis_hb.header.data.total_iters, genesis_hb.header.data.total_iters,
genesis_challenge.get_hash(), genesis_challenge.get_hash(),
genesis_hb.header.data.timestamp,
), ),
genesis_hb, genesis_hb,
) )

View File

@ -43,7 +43,7 @@ class WalletStore:
) )
await self.db_connection.execute( await self.db_connection.execute(
"CREATE TABLE IF NOT EXISTS block_records(header_hash text PRIMARY KEY, height int," "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 # Useful for reorg lookups
@ -71,6 +71,22 @@ class WalletStore:
"CREATE INDEX IF NOT EXISTS wallet_id on coin_record(wallet_id)" "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() await self.db_connection.commit()
self.coin_record_cache = dict() self.coin_record_cache = dict()
return self return self
@ -350,7 +366,7 @@ class WalletStore:
hash_to_br: Dict = {} hash_to_br: Dict = {}
max_height = -1 max_height = -1
for row in rows: for row in rows:
br = BlockRecord.from_bytes(row[3]) br = BlockRecord.from_bytes(row[4])
hash_to_br[bytes.fromhex(row[0])] = br hash_to_br[bytes.fromhex(row[0])] = br
assert row[0] == br.header_hash.hex() assert row[0] == br.header_hash.hex()
assert row[1] == br.height 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. to the chain, but it may or may not be in the LCA path.
""" """
cursor = await self.db_connection.execute( 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.header_hash.hex(),
block_record.height, block_record.height,
in_lca_path, in_lca_path,
block_record.timestamp,
bytes(block_record), bytes(block_record),
), ),
) )
@ -385,7 +402,7 @@ class WalletStore:
row = await cursor.fetchone() row = await cursor.fetchone()
await cursor.close() await cursor.close()
if row is not None: if row is not None:
return BlockRecord.from_bytes(row[3]) return BlockRecord.from_bytes(row[4])
else: else:
return None return None

View File

@ -150,6 +150,7 @@ class TestWalletStore:
None, None,
None, None,
None, None,
uint64(0),
) )
assert await store.get_block_record(br_1.header_hash) is None assert await store.get_block_record(br_1.header_hash) is None
await store.add_block_record(br_1, False) await store.add_block_record(br_1, False)
@ -171,6 +172,7 @@ class TestWalletStore:
None, None,
None, None,
None, None,
uint64(0),
) )
await store.add_block_record(br_2, False) await store.add_block_record(br_2, False)
assert len(await store.get_lca_path()) == 1 assert len(await store.get_lca_path()) == 1
@ -187,6 +189,7 @@ class TestWalletStore:
None, None,
None, None,
None, None,
uint64(0),
) )
await store.add_block_record(br_3, True) await store.add_block_record(br_3, True)
assert len(await store.get_lca_path()) == 3 assert len(await store.get_lca_path()) == 3