Block store tests

This commit is contained in:
Mariano Sorgente 2020-11-03 19:55:30 +09:00 committed by Yostra
parent 8511f4d644
commit 42b6912829
2 changed files with 12 additions and 5 deletions

View File

@ -26,7 +26,7 @@ class BlockStore:
# Sub blocks
await self.db.execute(
"CREATE TABLE IF NOT EXISTS sub_blocks(header_hash "
"text PRIMARY KEY, prev_hash text, height bigint, weight bigint, total_iters text"
"text PRIMARY KEY, prev_hash text, height bigint, weight bigint, total_iters text,"
"sub_block blob, is_peak tinyint)"
)
@ -112,6 +112,6 @@ class BlockStore:
async def set_peak(self, header_hash: bytes32) -> None:
cursor_1 = await self.db.execute("UPDATE sub_blocks SET is_peak=0 WHERE is_peak=1")
await cursor_1.close()
cursor_2 = await self.db.execute("UPDATE sub_blocks SET is_peak=1 WHERE header_hash=?", (header_hash.hex()))
cursor_2 = await self.db.execute("UPDATE sub_blocks SET is_peak=1 WHERE header_hash=?", (header_hash.hex(),))
await cursor_2.close()
await self.db.commit()

View File

@ -29,8 +29,8 @@ class TestBlockStore:
blocks = [block_1, block_2, block_3]
db_filename = Path("blockchain_test.db")
db_filename_2 = Path("blockchain_test_2.db")
db_filename_3 = Path("blockchain_test_3.db")
db_filename_2 = Path("blockchain_test2.db")
db_filename_3 = Path("blockchain_test3.db")
if db_filename.exists():
db_filename.unlink()
@ -51,15 +51,22 @@ class TestBlockStore:
# Save/get block
for block in blocks:
sub_block = full_block_to_sub_block_record(block, 0, 0, 5)
sub_block_hh = sub_block.header_hash
await db.add_block(block, sub_block)
await db.add_block(block, sub_block)
assert block == await db.get_block(block.header_hash)
assert block == await db.get_block(block.header_hash)
assert sub_block == (await db.get_sub_block(sub_block_hh))
await db.set_peak(sub_block.header_hash)
await db.set_peak(sub_block.header_hash)
assert len(await db.get_blocks_at([1])) == 0
assert len(await db.get_blocks_at([0])) == 3
# Get sub blocks
assert len(await db.get_sub_blocks()) == len(blocks)
assert len((await db.get_sub_blocks())[0]) == len(blocks)
# TODO
# for block in blocks:
# await b.receive_block(block)
#