2020-02-14 18:19:55 +01:00
|
|
|
import os
|
|
|
|
import asyncio
|
2020-02-28 05:52:18 +01:00
|
|
|
from concurrent import futures
|
|
|
|
from collections import namedtuple, deque
|
2020-02-14 18:19:55 +01:00
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
import sqlite3
|
2020-02-14 18:19:55 +01:00
|
|
|
import apsw
|
|
|
|
|
|
|
|
|
|
|
|
DDL = """
|
|
|
|
pragma journal_mode=WAL;
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
create table if not exists block (
|
|
|
|
block_hash bytes not null primary key,
|
2020-02-14 18:19:55 +01:00
|
|
|
previous_hash bytes not null,
|
2020-02-28 05:52:18 +01:00
|
|
|
file_number integer not null,
|
2020-02-14 18:19:55 +01:00
|
|
|
height int
|
|
|
|
);
|
2020-02-28 05:52:18 +01:00
|
|
|
create table if not exists tx (
|
2020-02-14 18:19:55 +01:00
|
|
|
block_hash integer not null,
|
|
|
|
position integer not null,
|
|
|
|
tx_hash bytes not null
|
|
|
|
);
|
2020-02-28 05:52:18 +01:00
|
|
|
create table if not exists txi (
|
2020-02-14 18:19:55 +01:00
|
|
|
block_hash bytes not null,
|
|
|
|
tx_hash bytes not null,
|
|
|
|
txo_hash bytes not null
|
|
|
|
);
|
2020-02-28 05:52:18 +01:00
|
|
|
create table if not exists claim (
|
2020-02-14 18:19:55 +01:00
|
|
|
txo_hash bytes not null,
|
|
|
|
claim_hash bytes not null,
|
|
|
|
claim_name text not null,
|
|
|
|
amount integer not null,
|
|
|
|
height integer
|
|
|
|
);
|
2020-02-28 05:52:18 +01:00
|
|
|
create table if not exists claim_history (
|
2020-02-14 18:19:55 +01:00
|
|
|
block_hash bytes not null,
|
|
|
|
tx_hash bytes not null,
|
|
|
|
tx_position integer not null,
|
|
|
|
txo_hash bytes not null,
|
|
|
|
claim_hash bytes not null,
|
|
|
|
claim_name text not null,
|
|
|
|
action integer not null,
|
|
|
|
amount integer not null,
|
|
|
|
height integer,
|
|
|
|
is_spent bool
|
|
|
|
);
|
2020-02-28 05:52:18 +01:00
|
|
|
create table if not exists support (
|
2020-02-14 18:19:55 +01:00
|
|
|
block_hash bytes not null,
|
|
|
|
tx_hash bytes not null,
|
|
|
|
txo_hash bytes not null,
|
|
|
|
claim_hash bytes not null,
|
|
|
|
amount integer not null
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class BlockchainDB:
|
|
|
|
|
|
|
|
__slots__ = 'db', 'directory'
|
|
|
|
|
|
|
|
def __init__(self, path: str):
|
|
|
|
self.db = None
|
|
|
|
self.directory = path
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
@property
|
|
|
|
def db_file_path(self):
|
|
|
|
return os.path.join(self.directory, 'blockchain.db')
|
|
|
|
|
2020-02-14 18:19:55 +01:00
|
|
|
def open(self):
|
2020-02-28 05:52:18 +01:00
|
|
|
self.db = sqlite3.connect(self.db_file_path, isolation_level=None, uri=True, timeout=60.0 * 5)
|
|
|
|
self.db.executescript("""
|
|
|
|
pragma journal_mode=wal;
|
|
|
|
""")
|
|
|
|
# self.db = apsw.Connection(
|
|
|
|
# self.db_file_path,
|
|
|
|
# flags=(
|
|
|
|
# apsw.SQLITE_OPEN_READWRITE |
|
|
|
|
# apsw.SQLITE_OPEN_CREATE |
|
|
|
|
# apsw.SQLITE_OPEN_URI
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
self.execute_ddl(DDL)
|
|
|
|
self.execute(f"ATTACH ? AS block_index", ('file:'+os.path.join(self.directory, 'block_index.sqlite')+'?mode=ro',))
|
|
|
|
#def exec_factory(cursor, statement, bindings):
|
|
|
|
# tpl = namedtuple('row', (d[0] for d in cursor.getdescription()))
|
|
|
|
# cursor.setrowtrace(lambda cursor, row: tpl(*row))
|
|
|
|
# return True
|
|
|
|
#self.db.setexectrace(exec_factory)
|
|
|
|
def row_factory(cursor, row):
|
|
|
|
tpl = namedtuple('row', (d[0] for d in cursor.description))
|
|
|
|
return tpl(*row)
|
|
|
|
self.db.row_factory = row_factory
|
|
|
|
return self
|
2020-02-14 18:19:55 +01:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.db is not None:
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
def execute(self, *args):
|
|
|
|
return self.db.cursor().execute(*args)
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
def execute_many(self, *args):
|
2020-02-14 18:19:55 +01:00
|
|
|
return self.db.cursor().executemany(*args)
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
def execute_many_tx(self, *args):
|
|
|
|
cursor = self.db.cursor()
|
|
|
|
cursor.execute('begin;')
|
|
|
|
result = cursor.executemany(*args)
|
|
|
|
cursor.execute('commit;')
|
|
|
|
return result
|
|
|
|
|
|
|
|
def execute_ddl(self, *args):
|
|
|
|
self.db.executescript(*args)
|
|
|
|
#deque(self.execute(*args), maxlen=0)
|
|
|
|
|
2020-02-14 18:19:55 +01:00
|
|
|
def begin(self):
|
|
|
|
self.execute('begin;')
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.execute('commit;')
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
def get_block_file_path_from_number(self, block_file_number):
|
|
|
|
return os.path.join(self.directory, 'blocks', f'blk{block_file_number:05}.dat')
|
|
|
|
|
|
|
|
def get_block_files_not_synced(self):
|
|
|
|
return list(self.execute(
|
|
|
|
"""
|
|
|
|
SELECT file as file_number, COUNT(hash) as blocks, SUM(txcount) as txs
|
|
|
|
FROM block_index.block_info
|
|
|
|
WHERE hash NOT IN (SELECT block_hash FROM block)
|
|
|
|
GROUP BY file ORDER BY file ASC;
|
|
|
|
"""
|
|
|
|
))
|
|
|
|
|
|
|
|
def get_blocks_not_synced(self, block_file):
|
|
|
|
return self.execute(
|
|
|
|
"""
|
|
|
|
SELECT datapos as data_offset, height, hash as block_hash, txCount as txs
|
|
|
|
FROM block_index.block_info
|
|
|
|
WHERE file = ? AND hash NOT IN (SELECT block_hash FROM block)
|
|
|
|
ORDER BY datapos ASC;
|
|
|
|
""", (block_file,)
|
|
|
|
)
|
2020-02-14 18:19:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AsyncBlockchainDB:
|
|
|
|
|
|
|
|
def __init__(self, db: BlockchainDB):
|
2020-02-28 05:52:18 +01:00
|
|
|
self.sync_db = db
|
|
|
|
self.executor = futures.ThreadPoolExecutor(max_workers=1)
|
2020-02-14 18:19:55 +01:00
|
|
|
|
|
|
|
@classmethod
|
2020-02-28 05:52:18 +01:00
|
|
|
def from_path(cls, path: str) -> 'AsyncBlockchainDB':
|
2020-02-14 18:19:55 +01:00
|
|
|
return cls(BlockchainDB(path))
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
def get_block_file_path_from_number(self, block_file_number):
|
|
|
|
return self.sync_db.get_block_file_path_from_number(block_file_number)
|
|
|
|
|
|
|
|
async def run_in_executor(self, func, *args):
|
2020-02-14 18:19:55 +01:00
|
|
|
return await asyncio.get_running_loop().run_in_executor(
|
2020-02-28 05:52:18 +01:00
|
|
|
self.executor, func, *args
|
2020-02-14 18:19:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
async def open(self):
|
2020-02-28 05:52:18 +01:00
|
|
|
return await self.run_in_executor(self.sync_db.open)
|
2020-02-14 18:19:55 +01:00
|
|
|
|
|
|
|
async def close(self):
|
2020-02-28 05:52:18 +01:00
|
|
|
return await self.run_in_executor(self.sync_db.close)
|
2020-02-14 18:19:55 +01:00
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
async def get_block_files_not_synced(self):
|
|
|
|
return await self.run_in_executor(self.sync_db.get_block_files_not_synced)
|