mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-24 10:05:29 -05:00
Name coin fields (#9381)
* name the fields we pull from the coin_records database * we don't need to ask for the coin_name in coin_record lookups, we never use it
This commit is contained in:
@@ -128,39 +128,51 @@ class CoinStore:
|
||||
cached = self.coin_record_cache.get(coin_name)
|
||||
if cached is not None:
|
||||
return cached
|
||||
cursor = await self.coin_record_db.execute("SELECT * from coin_record WHERE coin_name=?", (coin_name.hex(),))
|
||||
cursor = await self.coin_record_db.execute(
|
||||
"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
"coin_parent, amount, timestamp FROM coin_record WHERE coin_name=?",
|
||||
(coin_name.hex(),),
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
await cursor.close()
|
||||
if row is not None:
|
||||
coin = self.row_to_coin(row)
|
||||
record = CoinRecord(coin, row[1], row[2], row[3], row[4], row[8])
|
||||
record = CoinRecord(coin, row[0], row[1], row[2], row[3], row[7])
|
||||
self.coin_record_cache.put(record.coin.name(), record)
|
||||
return record
|
||||
return None
|
||||
|
||||
async def get_coins_added_at_height(self, height: uint32) -> List[CoinRecord]:
|
||||
cursor = await self.coin_record_db.execute("SELECT * from coin_record WHERE confirmed_index=?", (height,))
|
||||
cursor = await self.coin_record_db.execute(
|
||||
"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
"coin_parent, amount, timestamp FROM coin_record WHERE confirmed_index=?",
|
||||
(height,),
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
await cursor.close()
|
||||
coins = []
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
coins.append(CoinRecord(coin, row[1], row[2], row[3], row[4], row[8]))
|
||||
coins.append(CoinRecord(coin, row[0], row[1], row[2], row[3], row[7]))
|
||||
return coins
|
||||
|
||||
async def get_coins_removed_at_height(self, height: uint32) -> List[CoinRecord]:
|
||||
# Special case to avoid querying all unspent coins (spent_index=0)
|
||||
if height == 0:
|
||||
return []
|
||||
cursor = await self.coin_record_db.execute("SELECT * from coin_record WHERE spent_index=?", (height,))
|
||||
cursor = await self.coin_record_db.execute(
|
||||
"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
"coin_parent, amount, timestamp FROM coin_record WHERE spent_index=?",
|
||||
(height,),
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
await cursor.close()
|
||||
coins = []
|
||||
for row in rows:
|
||||
spent: bool = bool(row[3])
|
||||
spent: bool = bool(row[2])
|
||||
if spent:
|
||||
coin = self.row_to_coin(row)
|
||||
coin_record = CoinRecord(coin, row[1], row[2], spent, row[4], row[8])
|
||||
coin_record = CoinRecord(coin, row[0], row[1], spent, row[3], row[7])
|
||||
coins.append(coin_record)
|
||||
return coins
|
||||
|
||||
@@ -175,7 +187,8 @@ class CoinStore:
|
||||
|
||||
coins = set()
|
||||
cursor = await self.coin_record_db.execute(
|
||||
f"SELECT * from coin_record INDEXED BY coin_puzzle_hash WHERE puzzle_hash=? "
|
||||
f"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
f"coin_parent, amount, timestamp FROM coin_record INDEXED BY coin_puzzle_hash WHERE puzzle_hash=? "
|
||||
f"AND confirmed_index>=? AND confirmed_index<? "
|
||||
f"{'' if include_spent_coins else 'AND spent=0'}",
|
||||
(puzzle_hash.hex(), start_height, end_height),
|
||||
@@ -185,7 +198,7 @@ class CoinStore:
|
||||
await cursor.close()
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
coins.add(CoinRecord(coin, row[1], row[2], row[3], row[4], row[8]))
|
||||
coins.add(CoinRecord(coin, row[0], row[1], row[2], row[3], row[7]))
|
||||
return list(coins)
|
||||
|
||||
async def get_coin_records_by_puzzle_hashes(
|
||||
@@ -201,7 +214,8 @@ class CoinStore:
|
||||
coins = set()
|
||||
puzzle_hashes_db = tuple([ph.hex() for ph in puzzle_hashes])
|
||||
cursor = await self.coin_record_db.execute(
|
||||
f"SELECT * from coin_record INDEXED BY coin_puzzle_hash "
|
||||
f"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
f"coin_parent, amount, timestamp FROM coin_record INDEXED BY coin_puzzle_hash "
|
||||
f'WHERE puzzle_hash in ({"?," * (len(puzzle_hashes) - 1)}?) '
|
||||
f"AND confirmed_index>=? AND confirmed_index<? "
|
||||
f"{'' if include_spent_coins else 'AND spent=0'}",
|
||||
@@ -213,7 +227,7 @@ class CoinStore:
|
||||
await cursor.close()
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
coins.add(CoinRecord(coin, row[1], row[2], row[3], row[4], row[8]))
|
||||
coins.add(CoinRecord(coin, row[0], row[1], row[2], row[3], row[7]))
|
||||
return list(coins)
|
||||
|
||||
async def get_coin_records_by_names(
|
||||
@@ -229,7 +243,8 @@ class CoinStore:
|
||||
coins = set()
|
||||
names_db = tuple([name.hex() for name in names])
|
||||
cursor = await self.coin_record_db.execute(
|
||||
f'SELECT * from coin_record WHERE coin_name in ({"?," * (len(names) - 1)}?) '
|
||||
f"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
f'coin_parent, amount, timestamp FROM coin_record WHERE coin_name in ({"?," * (len(names) - 1)}?) '
|
||||
f"AND confirmed_index>=? AND confirmed_index<? "
|
||||
f"{'' if include_spent_coins else 'AND spent=0'}",
|
||||
names_db + (start_height, end_height),
|
||||
@@ -239,19 +254,19 @@ class CoinStore:
|
||||
await cursor.close()
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
coins.add(CoinRecord(coin, row[1], row[2], row[3], row[4], row[8]))
|
||||
coins.add(CoinRecord(coin, row[0], row[1], row[2], row[3], row[7]))
|
||||
|
||||
return list(coins)
|
||||
|
||||
def row_to_coin(self, row) -> Coin:
|
||||
return Coin(bytes32(bytes.fromhex(row[6])), bytes32(bytes.fromhex(row[5])), uint64.from_bytes(row[7]))
|
||||
return Coin(bytes32(bytes.fromhex(row[5])), bytes32(bytes.fromhex(row[4])), uint64.from_bytes(row[6]))
|
||||
|
||||
def row_to_coin_state(self, row):
|
||||
coin = self.row_to_coin(row)
|
||||
spent_h = None
|
||||
if row[3]:
|
||||
spent_h = row[2]
|
||||
return CoinState(coin, spent_h, row[1])
|
||||
if row[2]:
|
||||
spent_h = row[1]
|
||||
return CoinState(coin, spent_h, row[0])
|
||||
|
||||
async def get_coin_states_by_puzzle_hashes(
|
||||
self,
|
||||
@@ -266,7 +281,8 @@ class CoinStore:
|
||||
coins = set()
|
||||
puzzle_hashes_db = tuple([ph.hex() for ph in puzzle_hashes])
|
||||
cursor = await self.coin_record_db.execute(
|
||||
f"SELECT * from coin_record INDEXED BY coin_puzzle_hash "
|
||||
f"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
f"coin_parent, amount, timestamp FROM coin_record INDEXED BY coin_puzzle_hash "
|
||||
f'WHERE puzzle_hash in ({"?," * (len(puzzle_hashes) - 1)}?) '
|
||||
f"AND confirmed_index>=? AND confirmed_index<? "
|
||||
f"{'' if include_spent_coins else 'AND spent=0'}",
|
||||
@@ -294,7 +310,8 @@ class CoinStore:
|
||||
coins = set()
|
||||
parent_ids_db = tuple([pid.hex() for pid in parent_ids])
|
||||
cursor = await self.coin_record_db.execute(
|
||||
f'SELECT * from coin_record WHERE coin_parent in ({"?," * (len(parent_ids) - 1)}?) '
|
||||
f"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
f'coin_parent, amount, timestamp FROM coin_record WHERE coin_parent in ({"?," * (len(parent_ids) - 1)}?) '
|
||||
f"AND confirmed_index>=? AND confirmed_index<? "
|
||||
f"{'' if include_spent_coins else 'AND spent=0'}",
|
||||
parent_ids_db + (start_height, end_height),
|
||||
@@ -305,7 +322,7 @@ class CoinStore:
|
||||
await cursor.close()
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
coins.add(CoinRecord(coin, row[1], row[2], row[3], row[4], row[8]))
|
||||
coins.add(CoinRecord(coin, row[0], row[1], row[2], row[3], row[7]))
|
||||
return list(coins)
|
||||
|
||||
async def get_coin_state_by_ids(
|
||||
@@ -321,7 +338,8 @@ class CoinStore:
|
||||
coins = set()
|
||||
coin_ids_db = tuple([pid.hex() for pid in coin_ids])
|
||||
cursor = await self.coin_record_db.execute(
|
||||
f'SELECT * from coin_record WHERE coin_name in ({"?," * (len(coin_ids) - 1)}?) '
|
||||
f"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
f'coin_parent, amount, timestamp FROM coin_record WHERE coin_name in ({"?," * (len(coin_ids) - 1)}?) '
|
||||
f"AND confirmed_index>=? AND confirmed_index<? "
|
||||
f"{'' if include_spent_coins else 'AND spent=0'}",
|
||||
coin_ids_db + (start_height, end_height),
|
||||
@@ -360,12 +378,14 @@ class CoinStore:
|
||||
|
||||
coin_changes: Dict[bytes32, CoinRecord] = {}
|
||||
cursor_deleted = await self.coin_record_db.execute(
|
||||
"SELECT * FROM coin_record WHERE confirmed_index>?", (block_index,)
|
||||
"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
"coin_parent, amount, timestamp FROM coin_record WHERE confirmed_index>?",
|
||||
(block_index,),
|
||||
)
|
||||
rows = await cursor_deleted.fetchall()
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
record = CoinRecord(coin, uint32(0), row[2], row[3], row[4], uint64(0))
|
||||
record = CoinRecord(coin, uint32(0), row[1], row[2], row[3], uint64(0))
|
||||
coin_changes[record.name] = record
|
||||
await cursor_deleted.close()
|
||||
|
||||
@@ -374,19 +394,20 @@ class CoinStore:
|
||||
await c1.close()
|
||||
|
||||
cursor_unspent = await self.coin_record_db.execute(
|
||||
"SELECT * FROM coin_record WHERE confirmed_index>?", (block_index,)
|
||||
"SELECT confirmed_index, spent_index, spent, coinbase, puzzle_hash, "
|
||||
"coin_parent, amount, timestamp FROM coin_record WHERE confirmed_index>?",
|
||||
(block_index,),
|
||||
)
|
||||
rows = await cursor_unspent.fetchall()
|
||||
for row in rows:
|
||||
coin = self.row_to_coin(row)
|
||||
record = CoinRecord(coin, row[1], uint32(0), False, row[4], row[8])
|
||||
record = CoinRecord(coin, row[0], uint32(0), False, row[3], row[7])
|
||||
if record.name not in coin_changes:
|
||||
coin_changes[record.name] = record
|
||||
await cursor_unspent.close()
|
||||
|
||||
c2 = await self.coin_record_db.execute(
|
||||
"UPDATE coin_record SET spent_index = 0, spent = 0 WHERE spent_index>?",
|
||||
(block_index,),
|
||||
"UPDATE coin_record SET spent_index = 0, spent = 0 WHERE spent_index>?", (block_index,)
|
||||
)
|
||||
await c2.close()
|
||||
return list(coin_changes.values())
|
||||
|
||||
Reference in New Issue
Block a user