Merge commit '749162d9fead35d2beb2d34bdc7d90df4d5ec6d5' into checkpoint/long_lived_atari_from_main_749162d9fead35d2beb2d34bdc7d90df4d5ec6d5

This commit is contained in:
Amine Khaldi 2022-07-27 16:43:39 +01:00
commit aed2c8e673
No known key found for this signature in database
GPG Key ID: B1C074FFC904E2D9
3 changed files with 41 additions and 24 deletions

View File

@ -1,12 +1,14 @@
import aiosqlite
import random
from pathlib import Path
from dataclasses import dataclass
from typing import Optional, List, Dict, Tuple, Any
from typing import Optional, List, Dict, Tuple, Any, Type, TypeVar
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.program import Program, SerializedProgram
from chia.types.mempool_item import MempoolItem
from chia.util.ints import uint64, uint32
from chia.util.hash import std_hash
from chia.util.errors import Err, ValidationError
@ -45,6 +47,9 @@ class SimFullBlock(Streamable):
height: uint32 # Note that height is not on a regular FullBlock
_T_SimBlockRecord = TypeVar("_T_SimBlockRecord", bound="SimBlockRecord")
@streamable
@dataclass(frozen=True)
class SimBlockRecord(Streamable):
@ -57,7 +62,7 @@ class SimBlockRecord(Streamable):
prev_transaction_block_hash: bytes32
@classmethod
def create(cls, rci: List[Coin], height: uint32, timestamp: uint64):
def create(cls: Type[_T_SimBlockRecord], rci: List[Coin], height: uint32, timestamp: uint64) -> _T_SimBlockRecord:
return cls(
rci,
height,
@ -78,6 +83,9 @@ class SimStore(Streamable):
blocks: List[SimFullBlock]
_T_SpendSim = TypeVar("_T_SpendSim", bound="SpendSim")
class SpendSim:
db_wrapper: DBWrapper2
@ -89,7 +97,9 @@ class SpendSim:
defaults: ConsensusConstants
@classmethod
async def create(cls, db_path=None, defaults=DEFAULT_CONSTANTS):
async def create(
cls: Type[_T_SpendSim], db_path: Optional[Path] = None, defaults: ConsensusConstants = DEFAULT_CONSTANTS
) -> _T_SpendSim:
self = cls()
if db_path is None:
uri = f"file:db_{random.randint(0, 99999999)}?mode=memory&cache=shared"
@ -114,15 +124,16 @@ class SpendSim:
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]
# Create a protocol to make BlockRecord and SimBlockRecord interchangeable.
self.mempool_manager.peak = self.block_records[-1] # type: ignore[assignment]
else:
self.timestamp = 1
self.block_height = 0
self.timestamp = uint64(1)
self.block_height = uint32(0)
self.block_records = []
self.blocks = []
return self
async def close(self):
async def close(self) -> None:
async with self.db_wrapper.write_db() as conn:
c = await conn.execute("DELETE FROM block_data")
await c.close()
@ -133,10 +144,11 @@ class SpendSim:
await c.close()
await self.db_wrapper.close()
async def new_peak(self):
await self.mempool_manager.new_peak(self.block_records[-1], None)
async def new_peak(self) -> None:
# Create a protocol to make BlockRecord and SimBlockRecord interchangeable.
await self.mempool_manager.new_peak(self.block_records[-1], None) # type: ignore[arg-type]
def new_coin_record(self, coin: Coin, coinbase=False) -> CoinRecord:
def new_coin_record(self, coin: Coin, coinbase: bool = False) -> CoinRecord:
return CoinRecord(
coin,
uint32(self.block_height + 1),
@ -164,7 +176,7 @@ class SpendSim:
return None
return simple_solution_generator(bundle)
async def farm_block(self, puzzle_hash: bytes32 = bytes32(b"0" * 32)):
async def farm_block(self, puzzle_hash: bytes32 = bytes32(b"0" * 32)) -> Tuple[List[Coin], List[Coin]]:
# Fees get calculated
fees = uint64(0)
if self.mempool_manager.mempool.spends:
@ -234,13 +246,13 @@ class SpendSim:
def get_height(self) -> uint32:
return self.block_height
def pass_time(self, time: uint64):
def pass_time(self, time: uint64) -> None:
self.timestamp = uint64(self.timestamp + time)
def pass_blocks(self, blocks: uint32):
def pass_blocks(self, blocks: uint32) -> None:
self.block_height = uint32(self.block_height + blocks)
async def rewind(self, block_height: uint32):
async def rewind(self, block_height: uint32) -> None:
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
@ -255,7 +267,7 @@ class SpendSim:
class SimClient:
def __init__(self, service):
def __init__(self, service: SpendSim) -> None:
self.service = service
async def push_tx(self, spend_bundle: SpendBundle) -> Tuple[MempoolInclusionStatus, Optional[Err]]:
@ -270,7 +282,7 @@ class SimClient:
)
return status, error
async def get_coin_record_by_name(self, name: bytes32) -> CoinRecord:
async def get_coin_record_by_name(self, name: bytes32) -> Optional[CoinRecord]:
return await self.service.mempool_manager.coin_store.get_coin_record(name)
async def get_coin_records_by_names(
@ -363,8 +375,13 @@ class SimClient:
return additions, removals
async def get_puzzle_and_solution(self, coin_id: bytes32, height: uint32) -> Optional[CoinSpend]:
generator = list(filter(lambda block: block.height == height, self.service.blocks))[0].transactions_generator
coin_record = await self.service.mempool_manager.coin_store.get_coin_record(coin_id)
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]
coin_record: CoinRecord
coin_record = await self.service.mempool_manager.coin_store.get_coin_record( # type: ignore[assignment]
coin_id,
)
error, puzzle, solution = get_puzzle_and_solution_for_coin(
generator,
coin_id,
@ -380,13 +397,13 @@ class SimClient:
async def get_all_mempool_tx_ids(self) -> List[bytes32]:
return list(self.service.mempool_manager.mempool.spends.keys())
async def get_all_mempool_items(self) -> Dict[bytes32, Dict]:
async def get_all_mempool_items(self) -> Dict[bytes32, MempoolItem]:
spends = {}
for tx_id, item in self.service.mempool_manager.mempool.spends.items():
spends[tx_id] = item
return spends
async def get_mempool_item_by_tx_id(self, tx_id: bytes32) -> Optional[Dict]:
async def get_mempool_item_by_tx_id(self, tx_id: bytes32) -> Optional[Dict[str, Any]]:
item = self.service.mempool_manager.get_mempool_item(tx_id)
if item is None:
return None

File diff suppressed because one or more lines are too long

View File

@ -131,7 +131,7 @@ async def test_state_layer(setup_sim: Tuple[SpendSim, SimClient], metadata_updat
await sim.farm_block()
state_layer_puzzle = create_nft_layer_puzzle_with_curry_params(metadata, METADATA_UPDATER_PUZZLE_HASH, ACS)
finally:
await sim.close() # type: ignore
await sim.close()
@pytest.mark.asyncio()
@ -238,7 +238,7 @@ async def test_ownership_layer(setup_sim: Tuple[SpendSim, SimClient]) -> None:
ACS,
).get_tree_hash()
finally:
await sim.close() # type: ignore
await sim.close()
@pytest.mark.asyncio()
@ -362,4 +362,4 @@ async def test_default_transfer_program(setup_sim: Tuple[SpendSim, SimClient]) -
assert result == (MempoolInclusionStatus.SUCCESS, None)
await sim.farm_block()
finally:
await sim.close() # type: ignore
await sim.close()