def get_txnums( self, *, hashX: bytes, limit: Optional[int] = 1000, txnum_min: Optional[int] = None, txnum_max: Optional[int] = None, ): '''Generator that returns an unpruned, sorted list of tx_nums in the history of a hashX. Includes both spending and receiving transactions. By default yields at most 1000 entries. Set limit to None to get them all. txnum_min can be used to seek into the history and start there (>=) (instead of genesis). txnum_max can be used to stop early (<). ''' limit = util.resolve_limit(limit) prefix = b'H' + hashX it = self.db.iterator(prefix=prefix) if txnum_min is not None: it.seek(prefix + pack_txnum(txnum_min)) txnum_min = txnum_min if txnum_min is not None else 0 txnum_max = txnum_max if txnum_max is not None else float('inf') assert txnum_min <= txnum_max, f"txnum_min={txnum_min}, txnum_max={txnum_max}" for db_key, db_val in it: tx_numb = db_key[-TXNUM_LEN:] if limit == 0: return tx_num = unpack_txnum(tx_numb) if tx_num >= txnum_max: return assert txnum_min <= tx_num < txnum_max, (f"txnum_min={txnum_min}, tx_num={tx_num}, " f"txnum_max={txnum_max}") yield tx_num limit -= 1
def get_utxos(self, hashX, limit=1000): '''Generator that yields all UTXOs for an address sorted in no particular order. By default yields at most 1000 entries. Set limit to None to get them all. ''' limit = util.resolve_limit(limit) s_unpack = unpack # Key: b'u' + address_hashX + tx_idx + tx_num # Value: the UTXO value as a 64-bit unsigned integer prefix = b'u' + hashX for db_key, db_value in self.utxo_db.iterator(prefix=prefix): if limit == 0: return limit -= 1 tx_pos, tx_num = s_unpack('<HI', db_key[-6:]) tx_hash, height = self.fs_tx_hash(tx_num) if self.coin.EXTENDED_VOUT: asset = db_value[:32] value, = unpack('<Q', db_value[32:40]) yield UTXO_EXTENDED(tx_num, tx_pos, tx_hash, height, value, asset) else: value, = unpack('<Q', db_value) yield UTXO(tx_num, tx_pos, tx_hash, height, value)
def get_txnums(self, key, limit=1000): limit = util.resolve_limit(limit) for key, hist in self.db.iterator(prefix=key): a = array.array('I') a.frombytes(hist) # 把一维数据恢复成2*2维 for i in range(len(a) // 2): if limit == 0: return tx_num, log_index = a[2 * i:2 * i + 2] yield tx_num, log_index limit -= 1
def get_txnums(self, hashX, limit=1000): '''Generator that returns an unpruned, sorted list of tx_nums in the history of a hashX. Includes both spending and receiving transactions. By default yields at most 1000 entries. Set limit to None to get them all. ''' limit = util.resolve_limit(limit) chunks = util.chunks for _key, hist in self.db.iterator(prefix=hashX): for tx_numb in chunks(hist, 5): if limit == 0: return tx_num, = unpack_le_uint64(tx_numb + bytes(3)) yield tx_num limit -= 1
def get_txnums(self, hashX, limit=1000): '''Generator that returns an unpruned, sorted list of tx_nums in the history of a hashX. Includes both spending and receiving transactions. By default yields at most 1000 entries. Set limit to None to get them all. ''' limit = util.resolve_limit(limit) for key, hist in self.db.iterator(prefix=hashX): a = array.array('I') a.frombytes(hist) for tx_num in a: if limit == 0: return yield tx_num limit -= 1