def __init__(self, env): super().__init__() self.env = env self.coin = env.coin self.logger.info('switching current directory to {}'.format( env.db_dir)) os.chdir(env.db_dir) self.db_class = db_class(self.env.db_engine) self.logger.info('using {} for DB backend'.format(self.env.db_engine)) self.utxo_db = None self.open_dbs() self.clean_db() self.logger.info('reorg limit is {:,d} blocks'.format( self.env.reorg_limit)) self.headers_file = util.LogicalFile('meta/headers', 2, 16000000) self.tx_counts_file = util.LogicalFile('meta/txcounts', 2, 2000000) self.hashes_file = util.LogicalFile('meta/hashes', 4, 16000000) # tx_counts[N] has the cumulative number of txs at the end of # height N. So tx_counts[0] is 1 - the genesis coinbase size = (self.db_height + 1) * 4 tx_counts = self.tx_counts_file.read(0, size) assert len(tx_counts) == size self.tx_counts = array.array('I', tx_counts) if self.tx_counts: assert self.db_tx_count == self.tx_counts[-1] else: assert self.db_tx_count == 0
def db(tmpdir, request): cwd = os.getcwd() os.chdir(str(tmpdir)) db = db_class(request.param)("db", False) yield db os.chdir(cwd) db.close()
def db(tmpdir, request): cwd = os.getcwd() os.chdir(str(tmpdir)) db = db_class(request.param)("db", False) yield db os.chdir(cwd) # Make sure all the locks and handles are closed del db gc.collect()
def __init__(self, env): self.logger = logging.getLogger(self.__class__.__name__) self.env = env self.coin = env.coin # Setup block header size handlers if self.coin.STATIC_BLOCK_HEADERS: self.header_offset = self.coin.static_header_offset self.header_len = self.coin.static_header_len else: self.header_offset = self.dynamic_header_offset self.header_len = self.dynamic_header_len self.logger.info('switching current directory to {}'.format( env.db_dir)) os.chdir(env.db_dir) self.db_class = db_class(self.env.db_engine) self.logger.info('using {} for DB backend'.format(self.env.db_engine)) # For history compaction self.max_hist_row_entries = 12500 self.utxo_db = None self.hist_db = None self.eventlog_db = None # hashY+flush_id => [(tx_num, log_index), ] self.hashY_db = None # blockHash => [hashY, ] self.open_dbs() self.clean_db() self.logger.info('reorg limit is {:,d} blocks'.format( self.env.reorg_limit)) self.headers_file = util.LogicalFile('meta/headers', 2, 16000000) self.tx_counts_file = util.LogicalFile('meta/txcounts', 2, 2000000) self.hashes_file = util.LogicalFile('meta/hashes', 4, 16000000) if not self.coin.STATIC_BLOCK_HEADERS: self.headers_offsets_file = util.LogicalFile( 'meta/headers_offsets', 2, 16000000) # Write the offset of the genesis block if self.headers_offsets_file.read(0, 8) != b'\x00' * 8: self.headers_offsets_file.write(0, b'\x00' * 8) # tx_counts[N] has the cumulative number of txs at the end of # height N. So tx_counts[0] is 1 - the genesis coinbase size = (self.db_height + 1) * 4 tx_counts = self.tx_counts_file.read(0, size) assert len(tx_counts) == size self.tx_counts = array.array('I', tx_counts) if self.tx_counts: assert self.db_tx_count == self.tx_counts[-1] else: assert self.db_tx_count == 0
def __init__(self, env): super().__init__() self.env = env self.coin = env.coin # Setup block header size handlers if self.coin.STATIC_BLOCK_HEADERS: self.header_offset = self.coin.static_header_offset self.header_len = self.coin.static_header_len else: self.header_offset = self.dynamic_header_offset self.header_len = self.dynamic_header_len self.logger.info('switching current directory to {}'.format( env.db_dir)) os.chdir(env.db_dir) self.db_class = db_class(self.env.db_engine) self.logger.info('using {} for DB backend'.format(self.env.db_engine)) # For history compaction self.max_hist_row_entries = 12500 self.utxo_db = None self.open_dbs() self.clean_db() self.logger.info('reorg limit is {:,d} blocks'.format( self.env.reorg_limit)) #保存区块头信息,格式=header的前后相连 self.headers_file = util.LogicalFile('meta/headers', 2, 16000000) #记录区块的增量交易数量 self.tx_counts_file = util.LogicalFile('meta/txcounts', 2, 2000000) self.hashes_file = util.LogicalFile('meta/hashes', 4, 16000000) if not self.coin.STATIC_BLOCK_HEADERS: #保存区块头偏移信息(如果区块头长度不固定),结构为数据前后相连,可以通过height直接计算位置 self.headers_offsets_file = util.LogicalFile( 'meta/headers_offsets', 2, 16000000) # Write the offset of the genesis block if self.headers_offsets_file.read(0, 8) != b'\x00' * 8: self.headers_offsets_file.write(0, b'\x00' * 8) # tx_counts[N] has the cumulative number of txs at the end of # height N. So tx_counts[0] is 1 - the genesis coinbase size = (self.db_height + 1) * 4 tx_counts = self.tx_counts_file.read(0, size) assert len(tx_counts) == size self.tx_counts = array.array('I', tx_counts) if self.tx_counts: assert self.db_tx_count == self.tx_counts[-1] else: assert self.db_tx_count == 0
def test_close(db): db.put(b"a", b"b") db.close() db = db_class(db.__class__.__name__)("db", False) assert db.get(b"a") == b"b"