2022-12-14 02:10:55 +03:00
|
|
|
from __future__ import annotations
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2023-12-05 22:31:13 +03:00
|
|
|
import contextlib
|
2023-02-11 00:13:45 +03:00
|
|
|
import json
|
2022-12-14 02:10:55 +03:00
|
|
|
import random
|
2023-02-01 04:22:06 +03:00
|
|
|
from contextlib import asynccontextmanager
|
2022-03-23 02:20:32 +03:00
|
|
|
from dataclasses import dataclass
|
2022-12-14 02:10:55 +03:00
|
|
|
from pathlib import Path
|
2023-02-01 04:22:06 +03:00
|
|
|
from typing import Any, AsyncIterator, Callable, Dict, List, Optional, Tuple, Type, TypeVar
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2023-12-05 22:31:13 +03:00
|
|
|
import anyio
|
|
|
|
|
2022-12-14 02:10:55 +03:00
|
|
|
from chia.consensus.block_rewards import calculate_base_farmer_reward, calculate_pool_reward
|
|
|
|
from chia.consensus.coinbase import create_farmer_coin, create_pool_coin
|
|
|
|
from chia.consensus.constants import ConsensusConstants
|
|
|
|
from chia.consensus.cost_calculator import NPCResult
|
|
|
|
from chia.consensus.default_constants import DEFAULT_CONSTANTS
|
|
|
|
from chia.full_node.bundle_tools import simple_solution_generator
|
|
|
|
from chia.full_node.coin_store import CoinStore
|
2023-02-22 21:26:04 +03:00
|
|
|
from chia.full_node.mempool import Mempool
|
2023-02-11 00:13:45 +03:00
|
|
|
from chia.full_node.mempool_check_conditions import get_name_puzzle_conditions, get_puzzle_and_solution_for_coin
|
2022-12-14 02:10:55 +03:00
|
|
|
from chia.full_node.mempool_manager import MempoolManager
|
2021-07-13 23:16:18 +03:00
|
|
|
from chia.types.blockchain_format.coin import Coin
|
2023-02-11 00:13:45 +03:00
|
|
|
from chia.types.blockchain_format.program import INFINITE_COST
|
2022-12-14 02:10:55 +03:00
|
|
|
from chia.types.blockchain_format.sized_bytes import bytes32
|
2021-07-13 23:16:18 +03:00
|
|
|
from chia.types.coin_record import CoinRecord
|
2022-12-14 02:10:55 +03:00
|
|
|
from chia.types.coin_spend import CoinSpend
|
2021-07-13 23:16:18 +03:00
|
|
|
from chia.types.generator_types import BlockGenerator
|
|
|
|
from chia.types.mempool_inclusion_status import MempoolInclusionStatus
|
2022-12-14 02:10:55 +03:00
|
|
|
from chia.types.mempool_item import MempoolItem
|
|
|
|
from chia.types.spend_bundle import SpendBundle
|
|
|
|
from chia.util.db_wrapper import DBWrapper2
|
|
|
|
from chia.util.errors import Err, ValidationError
|
|
|
|
from chia.util.hash import std_hash
|
|
|
|
from chia.util.ints import uint32, uint64
|
|
|
|
from chia.util.streamable import Streamable, streamable
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
The purpose of this file is to provide a lightweight simulator for the testing of Chialisp smart contracts.
|
|
|
|
|
|
|
|
The Node object uses actual MempoolManager, Mempool and CoinStore objects, while substituting FullBlock and
|
|
|
|
BlockRecord objects for trimmed down versions.
|
|
|
|
|
|
|
|
There is also a provided NodeClient object which implements many of the methods from chia.rpc.full_node_rpc_client
|
|
|
|
and is designed so that you could test with it and then swap in a real rpc client that uses the same code you tested.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-02-01 04:22:06 +03:00
|
|
|
@asynccontextmanager
|
|
|
|
async def sim_and_client(
|
|
|
|
db_path: Optional[Path] = None, defaults: ConsensusConstants = DEFAULT_CONSTANTS, pass_prefarm: bool = True
|
|
|
|
) -> AsyncIterator[Tuple[SpendSim, SimClient]]:
|
2023-12-05 22:31:13 +03:00
|
|
|
async with SpendSim.managed(db_path, defaults) as sim:
|
2023-02-01 04:22:06 +03:00
|
|
|
client: SimClient = SimClient(sim)
|
|
|
|
if pass_prefarm:
|
|
|
|
await sim.farm_block()
|
|
|
|
yield sim, client
|
|
|
|
|
|
|
|
|
2023-02-11 00:13:45 +03:00
|
|
|
class CostLogger:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.cost_dict: Dict[str, int] = {}
|
|
|
|
self.cost_dict_no_puzs: Dict[str, int] = {}
|
|
|
|
|
|
|
|
def add_cost(self, descriptor: str, spend_bundle: SpendBundle) -> SpendBundle:
|
|
|
|
program: BlockGenerator = simple_solution_generator(spend_bundle)
|
|
|
|
npc_result: NPCResult = get_name_puzzle_conditions(
|
2023-07-11 17:52:49 +03:00
|
|
|
program,
|
|
|
|
INFINITE_COST,
|
|
|
|
mempool_mode=True,
|
2023-11-22 02:06:17 +03:00
|
|
|
height=DEFAULT_CONSTANTS.HARD_FORK_HEIGHT,
|
2023-07-11 17:52:49 +03:00
|
|
|
constants=DEFAULT_CONSTANTS,
|
2023-02-11 00:13:45 +03:00
|
|
|
)
|
2024-01-02 20:45:23 +03:00
|
|
|
cost = uint64(0 if npc_result.conds is None else npc_result.conds.cost)
|
|
|
|
self.cost_dict[descriptor] = cost
|
2023-02-11 00:13:45 +03:00
|
|
|
cost_to_subtract: int = 0
|
|
|
|
for cs in spend_bundle.coin_spends:
|
|
|
|
cost_to_subtract += len(bytes(cs.puzzle_reveal)) * DEFAULT_CONSTANTS.COST_PER_BYTE
|
2024-01-02 20:45:23 +03:00
|
|
|
self.cost_dict_no_puzs[descriptor] = cost - cost_to_subtract
|
2023-02-11 00:13:45 +03:00
|
|
|
return spend_bundle
|
|
|
|
|
|
|
|
def log_cost_statistics(self) -> str:
|
|
|
|
merged_dict = {
|
|
|
|
"standard cost": self.cost_dict,
|
|
|
|
"no puzzle reveals": self.cost_dict_no_puzs,
|
|
|
|
}
|
|
|
|
return json.dumps(merged_dict, indent=4)
|
|
|
|
|
|
|
|
|
2022-03-23 02:20:32 +03:00
|
|
|
@streamable
|
2022-04-09 04:29:32 +03:00
|
|
|
@dataclass(frozen=True)
|
2022-03-23 02:20:32 +03:00
|
|
|
class SimFullBlock(Streamable):
|
|
|
|
transactions_generator: Optional[BlockGenerator]
|
|
|
|
height: uint32 # Note that height is not on a regular FullBlock
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
_T_SimBlockRecord = TypeVar("_T_SimBlockRecord", bound="SimBlockRecord")
|
|
|
|
|
|
|
|
|
2022-03-23 02:20:32 +03:00
|
|
|
@streamable
|
2022-04-09 04:29:32 +03:00
|
|
|
@dataclass(frozen=True)
|
2022-03-23 02:20:32 +03:00
|
|
|
class SimBlockRecord(Streamable):
|
|
|
|
reward_claims_incorporated: List[Coin]
|
|
|
|
height: uint32
|
|
|
|
prev_transaction_block_height: uint32
|
|
|
|
timestamp: uint64
|
|
|
|
is_transaction_block: bool
|
|
|
|
header_hash: bytes32
|
|
|
|
prev_transaction_block_hash: bytes32
|
|
|
|
|
|
|
|
@classmethod
|
2022-06-29 12:34:41 +03:00
|
|
|
def create(cls: Type[_T_SimBlockRecord], rci: List[Coin], height: uint32, timestamp: uint64) -> _T_SimBlockRecord:
|
2023-11-21 22:04:48 +03:00
|
|
|
prev_transaction_block_height = uint32(height - 1 if height > 0 else 0)
|
2022-03-23 02:20:32 +03:00
|
|
|
return cls(
|
|
|
|
rci,
|
|
|
|
height,
|
2023-11-21 22:04:48 +03:00
|
|
|
prev_transaction_block_height,
|
2022-03-23 02:20:32 +03:00
|
|
|
timestamp,
|
|
|
|
True,
|
2023-11-01 00:57:17 +03:00
|
|
|
std_hash(height.stream_to_bytes()),
|
2023-11-21 22:04:48 +03:00
|
|
|
std_hash(prev_transaction_block_height.stream_to_bytes()),
|
2022-03-23 02:20:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@streamable
|
2022-04-09 04:29:32 +03:00
|
|
|
@dataclass(frozen=True)
|
2022-03-23 02:20:32 +03:00
|
|
|
class SimStore(Streamable):
|
|
|
|
timestamp: uint64
|
|
|
|
block_height: uint32
|
|
|
|
block_records: List[SimBlockRecord]
|
|
|
|
blocks: List[SimFullBlock]
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
_T_SpendSim = TypeVar("_T_SpendSim", bound="SpendSim")
|
|
|
|
|
|
|
|
|
2021-07-13 23:16:18 +03:00
|
|
|
class SpendSim:
|
2022-03-28 21:58:00 +03:00
|
|
|
db_wrapper: DBWrapper2
|
2022-11-29 21:18:09 +03:00
|
|
|
coin_store: CoinStore
|
2021-07-13 23:16:18 +03:00
|
|
|
mempool_manager: MempoolManager
|
2021-07-16 16:40:07 +03:00
|
|
|
block_records: List[SimBlockRecord]
|
|
|
|
blocks: List[SimFullBlock]
|
2021-07-13 23:16:18 +03:00
|
|
|
timestamp: uint64
|
|
|
|
block_height: uint32
|
2021-07-16 16:40:07 +03:00
|
|
|
defaults: ConsensusConstants
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
@classmethod
|
2023-12-05 22:31:13 +03:00
|
|
|
@contextlib.asynccontextmanager
|
|
|
|
async def managed(
|
2022-06-29 12:34:41 +03:00
|
|
|
cls: Type[_T_SpendSim], db_path: Optional[Path] = None, defaults: ConsensusConstants = DEFAULT_CONSTANTS
|
2023-12-05 22:31:13 +03:00
|
|
|
) -> AsyncIterator[_T_SpendSim]:
|
2021-07-13 23:16:18 +03:00
|
|
|
self = cls()
|
2022-04-12 20:15:33 +03:00
|
|
|
if db_path is None:
|
|
|
|
uri = f"file:db_{random.randint(0, 99999999)}?mode=memory&cache=shared"
|
|
|
|
else:
|
|
|
|
uri = f"file:{db_path}"
|
2022-09-26 19:17:20 +03:00
|
|
|
|
2023-12-05 22:31:13 +03:00
|
|
|
async with DBWrapper2.managed(database=uri, uri=True, reader_count=1, db_version=2) as self.db_wrapper:
|
|
|
|
self.coin_store = await CoinStore.create(self.db_wrapper)
|
2023-12-23 02:24:41 +03:00
|
|
|
self.mempool_manager = MempoolManager(self.coin_store.get_coin_records, defaults)
|
2023-12-05 22:31:13 +03:00
|
|
|
self.defaults = defaults
|
|
|
|
|
|
|
|
# Load the next data if there is any
|
|
|
|
async with self.db_wrapper.writer_maybe_transaction() as conn:
|
|
|
|
await conn.execute("CREATE TABLE IF NOT EXISTS block_data(data blob PRIMARY_KEY)")
|
|
|
|
cursor = await conn.execute("SELECT * from block_data")
|
|
|
|
row = await cursor.fetchone()
|
|
|
|
await cursor.close()
|
|
|
|
if row is not None:
|
|
|
|
store_data = SimStore.from_bytes(row[0])
|
|
|
|
self.timestamp = store_data.timestamp
|
|
|
|
self.block_height = store_data.block_height
|
|
|
|
self.block_records = store_data.block_records
|
|
|
|
self.blocks = store_data.blocks
|
|
|
|
self.mempool_manager.peak = self.block_records[-1]
|
|
|
|
else:
|
|
|
|
self.timestamp = uint64(1)
|
|
|
|
self.block_height = uint32(0)
|
|
|
|
self.block_records = []
|
|
|
|
self.blocks = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self
|
|
|
|
finally:
|
|
|
|
with anyio.CancelScope(shield=True):
|
|
|
|
async with self.db_wrapper.writer_maybe_transaction() as conn:
|
|
|
|
c = await conn.execute("DELETE FROM block_data")
|
|
|
|
await c.close()
|
|
|
|
c = await conn.execute(
|
|
|
|
"INSERT INTO block_data VALUES(?)",
|
|
|
|
(bytes(SimStore(self.timestamp, self.block_height, self.block_records, self.blocks)),),
|
|
|
|
)
|
|
|
|
await c.close()
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2023-11-22 18:08:19 +03:00
|
|
|
async def new_peak(self, spent_coins_ids: Optional[List[bytes32]]) -> None:
|
|
|
|
await self.mempool_manager.new_peak(self.block_records[-1], spent_coins_ids)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
def new_coin_record(self, coin: Coin, coinbase: bool = False) -> CoinRecord:
|
2021-07-13 23:16:18 +03:00
|
|
|
return CoinRecord(
|
|
|
|
coin,
|
|
|
|
uint32(self.block_height + 1),
|
|
|
|
uint32(0),
|
|
|
|
coinbase,
|
|
|
|
self.timestamp,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def all_non_reward_coins(self) -> List[Coin]:
|
|
|
|
coins = set()
|
2022-11-29 21:18:09 +03:00
|
|
|
async with self.db_wrapper.reader_no_transaction() as conn:
|
2022-03-28 21:58:00 +03:00
|
|
|
cursor = await conn.execute(
|
2023-08-03 22:18:33 +03:00
|
|
|
"SELECT puzzle_hash,coin_parent,amount from coin_record WHERE coinbase=0 AND spent_index==0 ",
|
2022-03-28 21:58:00 +03:00
|
|
|
)
|
|
|
|
rows = await cursor.fetchall()
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2022-03-28 21:58:00 +03:00
|
|
|
await cursor.close()
|
2021-07-13 23:16:18 +03:00
|
|
|
for row in rows:
|
2023-08-03 22:18:33 +03:00
|
|
|
coin = Coin(bytes32(row[1]), bytes32(row[0]), uint64.from_bytes(row[2]))
|
2021-07-13 23:16:18 +03:00
|
|
|
coins.add(coin)
|
|
|
|
return list(coins)
|
|
|
|
|
|
|
|
async def generate_transaction_generator(self, bundle: Optional[SpendBundle]) -> Optional[BlockGenerator]:
|
|
|
|
if bundle is None:
|
|
|
|
return None
|
|
|
|
return simple_solution_generator(bundle)
|
|
|
|
|
2022-11-22 01:28:48 +03:00
|
|
|
async def farm_block(
|
|
|
|
self,
|
|
|
|
puzzle_hash: bytes32 = bytes32(b"0" * 32),
|
2023-02-27 21:57:32 +03:00
|
|
|
item_inclusion_filter: Optional[Callable[[bytes32], bool]] = None,
|
2022-11-22 01:28:48 +03:00
|
|
|
) -> Tuple[List[Coin], List[Coin]]:
|
2021-07-13 23:16:18 +03:00
|
|
|
# Fees get calculated
|
|
|
|
fees = uint64(0)
|
2023-04-24 21:45:37 +03:00
|
|
|
for item in self.mempool_manager.mempool.all_items():
|
2023-02-22 21:26:04 +03:00
|
|
|
fees = uint64(fees + item.fee)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
# Rewards get created
|
|
|
|
next_block_height: uint32 = uint32(self.block_height + 1) if len(self.block_records) > 0 else self.block_height
|
|
|
|
pool_coin: Coin = create_pool_coin(
|
|
|
|
next_block_height,
|
|
|
|
puzzle_hash,
|
|
|
|
calculate_pool_reward(next_block_height),
|
2021-07-16 16:40:07 +03:00
|
|
|
self.defaults.GENESIS_CHALLENGE,
|
2021-07-13 23:16:18 +03:00
|
|
|
)
|
|
|
|
farmer_coin: Coin = create_farmer_coin(
|
|
|
|
next_block_height,
|
|
|
|
puzzle_hash,
|
|
|
|
uint64(calculate_base_farmer_reward(next_block_height) + fees),
|
2021-07-16 16:40:07 +03:00
|
|
|
self.defaults.GENESIS_CHALLENGE,
|
2021-07-13 23:16:18 +03:00
|
|
|
)
|
2022-11-29 21:18:09 +03:00
|
|
|
await self.coin_store._add_coin_records(
|
2021-09-21 20:43:43 +03:00
|
|
|
[self.new_coin_record(pool_coin, True), self.new_coin_record(farmer_coin, True)]
|
|
|
|
)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
# Coin store gets updated
|
|
|
|
generator_bundle: Optional[SpendBundle] = None
|
|
|
|
return_additions: List[Coin] = []
|
|
|
|
return_removals: List[Coin] = []
|
2023-11-22 18:08:19 +03:00
|
|
|
spent_coins_ids = None
|
2023-02-22 21:26:04 +03:00
|
|
|
if (len(self.block_records) > 0) and (self.mempool_manager.mempool.size() > 0):
|
2021-07-13 23:16:18 +03:00
|
|
|
peak = self.mempool_manager.peak
|
|
|
|
if peak is not None:
|
2022-12-14 02:11:12 +03:00
|
|
|
result = self.mempool_manager.create_bundle_from_mempool(peak.header_hash, item_inclusion_filter)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
if result is not None:
|
2023-04-18 00:34:22 +03:00
|
|
|
bundle, additions = result
|
2021-07-13 23:16:18 +03:00
|
|
|
generator_bundle = bundle
|
|
|
|
return_additions = additions
|
2023-04-18 00:34:22 +03:00
|
|
|
return_removals = bundle.removals()
|
2023-11-22 18:08:19 +03:00
|
|
|
spent_coins_ids = [r.name() for r in return_removals]
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2022-11-29 21:18:09 +03:00
|
|
|
await self.coin_store._add_coin_records([self.new_coin_record(addition) for addition in additions])
|
2023-11-22 18:08:19 +03:00
|
|
|
await self.coin_store._set_spent(spent_coins_ids, uint32(self.block_height + 1))
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2021-07-16 16:40:07 +03:00
|
|
|
# SimBlockRecord is created
|
2021-07-13 23:16:18 +03:00
|
|
|
generator: Optional[BlockGenerator] = await self.generate_transaction_generator(generator_bundle)
|
|
|
|
self.block_records.append(
|
2022-03-23 02:20:32 +03:00
|
|
|
SimBlockRecord.create(
|
2021-07-13 23:16:18 +03:00
|
|
|
[pool_coin, farmer_coin],
|
|
|
|
next_block_height,
|
|
|
|
self.timestamp,
|
|
|
|
)
|
|
|
|
)
|
2021-07-16 16:40:07 +03:00
|
|
|
self.blocks.append(SimFullBlock(generator, next_block_height))
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
# block_height is incremented
|
|
|
|
self.block_height = next_block_height
|
|
|
|
|
|
|
|
# mempool is reset
|
2023-11-22 18:08:19 +03:00
|
|
|
await self.new_peak(spent_coins_ids)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
# return some debugging data
|
|
|
|
return return_additions, return_removals
|
|
|
|
|
|
|
|
def get_height(self) -> uint32:
|
|
|
|
return self.block_height
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
def pass_time(self, time: uint64) -> None:
|
2021-07-13 23:16:18 +03:00
|
|
|
self.timestamp = uint64(self.timestamp + time)
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
def pass_blocks(self, blocks: uint32) -> None:
|
2021-07-13 23:16:18 +03:00
|
|
|
self.block_height = uint32(self.block_height + blocks)
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
async def rewind(self, block_height: uint32) -> None:
|
2021-07-13 23:16:18 +03:00
|
|
|
new_br_list = list(filter(lambda br: br.height <= block_height, self.block_records))
|
|
|
|
new_block_list = list(filter(lambda block: block.height <= block_height, self.blocks))
|
|
|
|
self.block_records = new_br_list
|
|
|
|
self.blocks = new_block_list
|
2022-11-29 21:18:09 +03:00
|
|
|
await self.coin_store.rollback_to_block(block_height)
|
2023-02-22 21:26:04 +03:00
|
|
|
old_pool = self.mempool_manager.mempool
|
|
|
|
self.mempool_manager.mempool = Mempool(old_pool.mempool_info, old_pool.fee_estimator)
|
2021-07-13 23:16:18 +03:00
|
|
|
self.block_height = block_height
|
|
|
|
if new_br_list:
|
|
|
|
self.timestamp = new_br_list[-1].timestamp
|
|
|
|
else:
|
2021-08-03 20:21:37 +03:00
|
|
|
self.timestamp = uint64(1)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
class SimClient:
|
2022-06-29 12:34:41 +03:00
|
|
|
def __init__(self, service: SpendSim) -> None:
|
2021-07-13 23:16:18 +03:00
|
|
|
self.service = service
|
|
|
|
|
|
|
|
async def push_tx(self, spend_bundle: SpendBundle) -> Tuple[MempoolInclusionStatus, Optional[Err]]:
|
2021-11-04 19:29:05 +03:00
|
|
|
try:
|
2022-12-13 09:32:55 +03:00
|
|
|
spend_bundle_id = spend_bundle.name()
|
2021-11-04 19:29:05 +03:00
|
|
|
cost_result: NPCResult = await self.service.mempool_manager.pre_validate_spendbundle(
|
2022-12-13 09:32:55 +03:00
|
|
|
spend_bundle, None, spend_bundle_id
|
2021-11-04 19:29:05 +03:00
|
|
|
)
|
|
|
|
except ValidationError as e:
|
|
|
|
return MempoolInclusionStatus.FAILED, e.code
|
2022-11-22 01:28:48 +03:00
|
|
|
assert self.service.mempool_manager.peak
|
2022-09-06 20:32:03 +03:00
|
|
|
cost, status, error = await self.service.mempool_manager.add_spend_bundle(
|
2022-12-13 09:32:55 +03:00
|
|
|
spend_bundle, cost_result, spend_bundle_id, self.service.mempool_manager.peak.height
|
2021-07-13 23:16:18 +03:00
|
|
|
)
|
|
|
|
return status, error
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
async def get_coin_record_by_name(self, name: bytes32) -> Optional[CoinRecord]:
|
2022-11-29 21:18:09 +03:00
|
|
|
return await self.service.coin_store.get_coin_record(name)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2022-04-12 20:15:33 +03:00
|
|
|
async def get_coin_records_by_names(
|
|
|
|
self,
|
|
|
|
names: List[bytes32],
|
|
|
|
start_height: Optional[int] = None,
|
|
|
|
end_height: Optional[int] = None,
|
|
|
|
include_spent_coins: bool = False,
|
|
|
|
) -> List[CoinRecord]:
|
|
|
|
kwargs: Dict[str, Any] = {"include_spent_coins": include_spent_coins, "names": names}
|
|
|
|
if start_height is not None:
|
|
|
|
kwargs["start_height"] = start_height
|
|
|
|
if end_height is not None:
|
|
|
|
kwargs["end_height"] = end_height
|
2022-11-29 21:18:09 +03:00
|
|
|
return await self.service.coin_store.get_coin_records_by_names(**kwargs)
|
2022-04-12 20:15:33 +03:00
|
|
|
|
2021-12-07 20:56:37 +03:00
|
|
|
async def get_coin_records_by_parent_ids(
|
|
|
|
self,
|
|
|
|
parent_ids: List[bytes32],
|
|
|
|
start_height: Optional[int] = None,
|
|
|
|
end_height: Optional[int] = None,
|
|
|
|
include_spent_coins: bool = False,
|
|
|
|
) -> List[CoinRecord]:
|
|
|
|
kwargs: Dict[str, Any] = {"include_spent_coins": include_spent_coins, "parent_ids": parent_ids}
|
|
|
|
if start_height is not None:
|
|
|
|
kwargs["start_height"] = start_height
|
|
|
|
if end_height is not None:
|
|
|
|
kwargs["end_height"] = end_height
|
2022-11-29 21:18:09 +03:00
|
|
|
return await self.service.coin_store.get_coin_records_by_parent_ids(**kwargs)
|
2021-12-07 20:56:37 +03:00
|
|
|
|
2021-07-13 23:16:18 +03:00
|
|
|
async def get_coin_records_by_puzzle_hash(
|
|
|
|
self,
|
|
|
|
puzzle_hash: bytes32,
|
|
|
|
include_spent_coins: bool = True,
|
|
|
|
start_height: Optional[int] = None,
|
|
|
|
end_height: Optional[int] = None,
|
|
|
|
) -> List[CoinRecord]:
|
|
|
|
kwargs: Dict[str, Any] = {"include_spent_coins": include_spent_coins, "puzzle_hash": puzzle_hash}
|
|
|
|
if start_height is not None:
|
|
|
|
kwargs["start_height"] = start_height
|
|
|
|
if end_height is not None:
|
|
|
|
kwargs["end_height"] = end_height
|
2022-11-29 21:18:09 +03:00
|
|
|
return await self.service.coin_store.get_coin_records_by_puzzle_hash(**kwargs)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
async def get_coin_records_by_puzzle_hashes(
|
|
|
|
self,
|
|
|
|
puzzle_hashes: List[bytes32],
|
|
|
|
include_spent_coins: bool = True,
|
|
|
|
start_height: Optional[int] = None,
|
|
|
|
end_height: Optional[int] = None,
|
|
|
|
) -> List[CoinRecord]:
|
|
|
|
kwargs: Dict[str, Any] = {"include_spent_coins": include_spent_coins, "puzzle_hashes": puzzle_hashes}
|
|
|
|
if start_height is not None:
|
|
|
|
kwargs["start_height"] = start_height
|
|
|
|
if end_height is not None:
|
|
|
|
kwargs["end_height"] = end_height
|
2022-11-29 21:18:09 +03:00
|
|
|
return await self.service.coin_store.get_coin_records_by_puzzle_hashes(**kwargs)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2021-07-16 16:40:07 +03:00
|
|
|
async def get_block_record_by_height(self, height: uint32) -> SimBlockRecord:
|
2021-07-13 23:16:18 +03:00
|
|
|
return list(filter(lambda block: block.height == height, self.service.block_records))[0]
|
|
|
|
|
2021-07-16 16:40:07 +03:00
|
|
|
async def get_block_record(self, header_hash: bytes32) -> SimBlockRecord:
|
2021-07-13 23:16:18 +03:00
|
|
|
return list(filter(lambda block: block.header_hash == header_hash, self.service.block_records))[0]
|
|
|
|
|
2021-07-16 16:40:07 +03:00
|
|
|
async def get_block_records(self, start: uint32, end: uint32) -> List[SimBlockRecord]:
|
2021-07-13 23:16:18 +03:00
|
|
|
return list(filter(lambda block: (block.height >= start) and (block.height < end), self.service.block_records))
|
|
|
|
|
2021-07-16 16:40:07 +03:00
|
|
|
async def get_block(self, header_hash: bytes32) -> SimFullBlock:
|
|
|
|
selected_block: SimBlockRecord = list(
|
2021-07-13 23:16:18 +03:00
|
|
|
filter(lambda br: br.header_hash == header_hash, self.service.block_records)
|
|
|
|
)[0]
|
|
|
|
block_height: uint32 = selected_block.height
|
2021-07-16 16:40:07 +03:00
|
|
|
block: SimFullBlock = list(filter(lambda block: block.height == block_height, self.service.blocks))[0]
|
2021-07-13 23:16:18 +03:00
|
|
|
return block
|
|
|
|
|
2021-07-16 16:40:07 +03:00
|
|
|
async def get_all_block(self, start: uint32, end: uint32) -> List[SimFullBlock]:
|
2021-07-13 23:16:18 +03:00
|
|
|
return list(filter(lambda block: (block.height >= start) and (block.height < end), self.service.blocks))
|
|
|
|
|
|
|
|
async def get_additions_and_removals(self, header_hash: bytes32) -> Tuple[List[CoinRecord], List[CoinRecord]]:
|
2021-07-16 16:40:07 +03:00
|
|
|
selected_block: SimBlockRecord = list(
|
2021-07-13 23:16:18 +03:00
|
|
|
filter(lambda br: br.header_hash == header_hash, self.service.block_records)
|
|
|
|
)[0]
|
|
|
|
block_height: uint32 = selected_block.height
|
2022-11-29 21:18:09 +03:00
|
|
|
additions: List[CoinRecord] = await self.service.coin_store.get_coins_added_at_height(block_height)
|
|
|
|
removals: List[CoinRecord] = await self.service.coin_store.get_coins_removed_at_height(block_height)
|
2021-07-13 23:16:18 +03:00
|
|
|
return additions, removals
|
|
|
|
|
2023-05-18 01:34:33 +03:00
|
|
|
async def get_puzzle_and_solution(self, coin_id: bytes32, height: uint32) -> CoinSpend:
|
2022-06-29 12:34:41 +03:00
|
|
|
filtered_generators = list(filter(lambda block: block.height == height, self.service.blocks))
|
|
|
|
# real consideration should be made for the None cases instead of just hint ignoring
|
|
|
|
generator: BlockGenerator = filtered_generators[0].transactions_generator # type: ignore[assignment]
|
2022-11-29 21:18:09 +03:00
|
|
|
coin_record = await self.service.coin_store.get_coin_record(coin_id)
|
|
|
|
assert coin_record is not None
|
2023-10-10 18:14:21 +03:00
|
|
|
spend_info = get_puzzle_and_solution_for_coin(generator, coin_record.coin, height, self.service.defaults)
|
2023-05-18 01:34:33 +03:00
|
|
|
return CoinSpend(coin_record.coin, spend_info.puzzle, spend_info.solution)
|
2021-07-13 23:16:18 +03:00
|
|
|
|
|
|
|
async def get_all_mempool_tx_ids(self) -> List[bytes32]:
|
2023-04-24 21:45:37 +03:00
|
|
|
return self.service.mempool_manager.mempool.all_item_ids()
|
2021-07-13 23:16:18 +03:00
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
async def get_all_mempool_items(self) -> Dict[bytes32, MempoolItem]:
|
2021-07-13 23:16:18 +03:00
|
|
|
spends = {}
|
2023-04-24 21:45:37 +03:00
|
|
|
for item in self.service.mempool_manager.mempool.all_items():
|
2023-02-22 21:26:04 +03:00
|
|
|
spends[item.name] = item
|
2021-07-13 23:16:18 +03:00
|
|
|
return spends
|
|
|
|
|
2022-06-29 12:34:41 +03:00
|
|
|
async def get_mempool_item_by_tx_id(self, tx_id: bytes32) -> Optional[Dict[str, Any]]:
|
2021-07-13 23:16:18 +03:00
|
|
|
item = self.service.mempool_manager.get_mempool_item(tx_id)
|
|
|
|
if item is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return item.__dict__
|