fix: update run_block.py (#10014)

* fix: update run_block.py

- add asset id
- add parent directory to load transactions_generator_ref_list from a directory

* fix: remove un used import

* fix: Call to function ref_list_to_args with too few arguments; should be no fewer than 2.

* core: remove unused function

* fix: missed a conflict, opps

* core: remove unused import

* fix: linting
This commit is contained in:
Laser Cat 2022-02-03 12:29:00 -06:00 committed by GitHub
parent 5b7f46fca7
commit ec92f4b680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 33 deletions

View File

@ -48,7 +48,7 @@ def test_block_no_generator():
with open(dirname / "300000.json") as f:
full_block = json.load(f)
cat_list = run_json_block(full_block, constants)
cat_list = run_json_block(full_block, dirname, constants)
assert not cat_list
@ -58,10 +58,10 @@ def test_block_retired_cat_with_memo():
with open(dirname / "396963.json") as f:
full_block = json.load(f)
cat_list = run_json_block(full_block, constants)
cat_list = run_json_block(full_block, dirname, constants)
assert cat_list
assert cat_list[0].tail_hash == "86bf9abe0600edf96b2e0fa928d19435b5aa756a9c9151c4b53c2c3da258502f"
assert cat_list[0].asset_id == "86bf9abe0600edf96b2e0fa928d19435b5aa756a9c9151c4b53c2c3da258502f"
assert cat_list[0].memo == "Hello, please find me, I'm a memo!"
assert cat_list[0].npc.coin_name.hex() == "244854a6fadf837b0fbb78d19b94b0de24fd2ffb440e7c0ec7866104b2aecd16"
assert cat_list[0].npc.puzzle_hash.hex() == "4aa945b657928602e59d37ad165ba12008d1dbee3a7be06c9bd19b4f00da456c"
@ -78,10 +78,10 @@ def test_block_retired_cat_no_memo():
with open(dirname / "392111.json") as f:
full_block = json.load(f)
cat_list = run_json_block(full_block, constants)
cat_list = run_json_block(full_block, dirname, constants)
assert cat_list
assert cat_list[0].tail_hash == "86bf9abe0600edf96b2e0fa928d19435b5aa756a9c9151c4b53c2c3da258502f"
assert cat_list[0].asset_id == "86bf9abe0600edf96b2e0fa928d19435b5aa756a9c9151c4b53c2c3da258502f"
assert not cat_list[0].memo
assert cat_list[0].npc.coin_name.hex() == "f419f6b77fa56b2cf0e93818d9214ec6023fb6335107dd6e6d82dfa5f4cbb4f6"
assert cat_list[0].npc.puzzle_hash.hex() == "714655375fc8e4e3545ecdc671ea53e497160682c82fe2c6dc44c4150dc845b4"
@ -99,10 +99,10 @@ def test_block_cat():
with open(dirname / "149988.json") as f:
full_block = json.load(f)
cat_list = run_json_block(full_block, constants)
cat_list = run_json_block(full_block, dirname, constants)
assert cat_list
assert cat_list[0].tail_hash == "8829a36776a15477a7f41f8fb6397752922374b60be7d3b2d7881c54b86b32a1"
assert cat_list[0].asset_id == "8829a36776a15477a7f41f8fb6397752922374b60be7d3b2d7881c54b86b32a1"
assert not cat_list[0].memo
assert cat_list[0].npc.coin_name.hex() == "4314b142cecfd6121474116e5a690d6d9b2e8c374e1ebef15235b0f3de4e2508"
assert cat_list[0].npc.puzzle_hash.hex() == "ddc37f3cbb49e3566b8638c5aaa93d5e10ee91dfd5d8ce37ad7175432d7209aa"

View File

@ -37,7 +37,8 @@ and in this way they control whether a spend is valid or not.
"""
import json
from dataclasses import dataclass
from typing import List, TextIO, Tuple, Dict
from pathlib import Path
from typing import List, Tuple, Dict
import click
@ -49,7 +50,6 @@ from chia.types.blockchain_format.program import SerializedProgram
from chia.types.blockchain_format.coin import Coin
from chia.types.condition_opcodes import ConditionOpcode
from chia.types.condition_with_args import ConditionWithArgs
from chia.types.full_block import FullBlock
from chia.types.generator_types import BlockGenerator
from chia.types.name_puzzle_condition import NPC
from chia.util.config import load_config
@ -61,12 +61,12 @@ from clvm.casts import int_from_bytes
@dataclass
class CAT:
tail_hash: str
asset_id: str
memo: str
npc: NPC
def cat_to_dict(self):
return {"tail_hash": self.tail_hash, "memo": self.memo, "npc": npc_to_dict(self.npc)}
return {"asset_id": self.asset_id, "memo": self.memo, "npc": npc_to_dict(self.npc)}
def condition_with_args_to_dict(condition_with_args: ConditionWithArgs):
@ -115,7 +115,7 @@ def run_generator(
if not matched:
continue
_, tail_hash, _ = curried_args
_, asset_id, _ = curried_args
memo = ""
result = puzzle.run(solution)
@ -158,7 +158,7 @@ def run_generator(
coin = Coin(parent.atom, puzzle_hash, int_from_bytes(amount.atom))
cat_list.append(
CAT(
tail_hash=bytes(tail_hash).hex()[2:],
asset_id=bytes(asset_id).hex()[2:],
memo=memo,
npc=NPC(coin.name(), puzzle_hash, [(op, cond) for op, cond in conds.items()]),
)
@ -167,25 +167,15 @@ def run_generator(
return cat_list
def ref_list_to_args(ref_list: List[uint32]) -> List[SerializedProgram]:
def ref_list_to_args(ref_list: List[uint32], root_path: Path) -> List[SerializedProgram]:
args = []
for height in ref_list:
with open(f"{height}.json", "r") as f:
with open(root_path / f"{height}.json", "rb") as f:
program_str = json.load(f)["block"]["transactions_generator"]
args.append(SerializedProgram.fromhex(program_str))
return args
def run_full_block(block: FullBlock, constants: ConsensusConstants) -> List[CAT]:
generator_args = ref_list_to_args(block.transactions_generator_ref_list)
if block.transactions_generator is None or block.transactions_info is None:
raise RuntimeError("transactions_generator of FullBlock is null")
block_generator = BlockGenerator(block.transactions_generator, generator_args, [])
return run_generator(
block_generator, constants, min(constants.MAX_BLOCK_COST_CLVM, block.transactions_info.cost), block.height
)
def run_generator_with_args(
generator_program_hex: str,
generator_args: List[SerializedProgram],
@ -201,13 +191,13 @@ def run_generator_with_args(
@click.command()
@click.argument("file", type=click.File("rb"))
def cmd_run_json_block_file(file):
@click.argument("filename", type=click.Path(exists=True), default="testnet10.396963.json")
def cmd_run_json_block_file(filename):
"""`file` is a file containing a FullBlock in JSON format"""
return run_json_block_file(file)
return run_json_block_file(Path(filename))
def run_json_block(full_block, constants: ConsensusConstants) -> List[CAT]:
def run_json_block(full_block, parent: Path, constants: ConsensusConstants) -> List[CAT]:
ref_list = full_block["block"]["transactions_generator_ref_list"]
tx_info: dict = full_block["block"]["transactions_info"]
generator_program_hex: str = full_block["block"]["transactions_generator"]
@ -215,18 +205,18 @@ def run_json_block(full_block, constants: ConsensusConstants) -> List[CAT]:
cat_list: List[CAT] = []
if tx_info and generator_program_hex:
cost = tx_info["cost"]
args = ref_list_to_args(ref_list)
args = ref_list_to_args(ref_list, parent)
cat_list = run_generator_with_args(generator_program_hex, args, constants, cost, height)
return cat_list
def run_json_block_file(file: TextIO):
full_block = json.load(file)
def run_json_block_file(filename: Path):
full_block = json.load(filename.open("rb"))
# pull in current constants from config.yaml
_, constants = get_config_and_constants()
cat_list = run_json_block(full_block, constants)
cat_list = run_json_block(full_block, filename.parent.absolute(), constants)
cat_list_json = json.dumps([cat.cat_to_dict() for cat in cat_list])
print(cat_list_json)