Skip to content

Commit

Permalink
more bytes32.from_hexstr() (#18446)
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky authored Aug 14, 2024
1 parent 6f72c9a commit 7d88089
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 40 deletions.
28 changes: 14 additions & 14 deletions chia/_tests/core/data_layer/test_data_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async def test_create_insert_get(
changelist: List[Dict[str, str]] = [{"action": "insert", "key": key.hex(), "value": value.hex()}]
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)
res = await data_rpc_api.batch_update({"id": store_id.hex(), "changelist": changelist})
update_tx_rec0 = res["tx_id"]
Expand Down Expand Up @@ -369,7 +369,7 @@ async def test_create_double_insert(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)
key1 = b"a"
value1 = b"\x01\x02"
Expand Down Expand Up @@ -407,7 +407,7 @@ async def test_keys_values_ancestors(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)
key1 = b"a"
value1 = b"\x01\x02"
Expand Down Expand Up @@ -482,12 +482,12 @@ async def test_get_roots(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id1 = bytes32(hexstr_to_bytes(res["id"]))
store_id1 = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id1, wallet=wallet_rpc_api.service)

res = await data_rpc_api.create_data_store({})
assert res is not None
store_id2 = bytes32(hexstr_to_bytes(res["id"]))
store_id2 = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id2, wallet=wallet_rpc_api.service)

key1 = b"a"
Expand Down Expand Up @@ -535,7 +535,7 @@ async def test_get_root_history(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id1 = bytes32(hexstr_to_bytes(res["id"]))
store_id1 = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id1, wallet=wallet_rpc_api.service)
key1 = b"a"
value1 = b"\x01\x02"
Expand Down Expand Up @@ -589,7 +589,7 @@ async def test_get_kv_diff(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id1 = bytes32(hexstr_to_bytes(res["id"]))
store_id1 = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id1, wallet=wallet_rpc_api.service)
key1 = b"a"
value1 = b"\x01\x02"
Expand Down Expand Up @@ -656,7 +656,7 @@ async def test_batch_update_matches_single_operations(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)

key = b"a"
Expand Down Expand Up @@ -2435,7 +2435,7 @@ async def test_mirrors(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)

urls = ["http://127.0.0.1/8000", "http://127.0.0.1/8001"]
Expand Down Expand Up @@ -2660,7 +2660,7 @@ async def test_dl_proof_errors(
fakeroot = bytes32([4] * 32)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)

with pytest.raises(ValueError, match="no root"):
Expand Down Expand Up @@ -2769,7 +2769,7 @@ async def test_pagination_rpcs(
data_rpc_api = DataLayerRpcApi(data_layer)
res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)
key1 = b"aa"
value1 = b"\x01\x02"
Expand Down Expand Up @@ -3035,7 +3035,7 @@ async def test_pagination_cmds(

res = await data_rpc_api.create_data_store({})
assert res is not None
store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)

key = b"aa"
Expand Down Expand Up @@ -3271,7 +3271,7 @@ async def test_unsubmitted_batch_update(
res = await data_rpc_api.create_data_store({})
assert res is not None

store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)

to_insert = [(b"a", b"\x00\x01"), (b"b", b"\x00\x02"), (b"c", b"\x00\x03")]
Expand Down Expand Up @@ -3725,7 +3725,7 @@ class ModifiedStatus(IntEnum):
res = await data_rpc_api.create_data_store({})
assert res is not None

store_id = bytes32(hexstr_to_bytes(res["id"]))
store_id = bytes32.from_hexstr(res["id"])
await farm_block_check_singleton(data_layer, full_node_api, ph, store_id, wallet=wallet_rpc_api.service)

m.setattr("chia.data_layer.data_layer_util.Status", OldStatus)
Expand Down
2 changes: 1 addition & 1 deletion chia/cmds/plotnft_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async def join_pool(
func = functools.partial(
wallet_client.pw_join_pool,
wallet_id,
bytes32(hexstr_to_bytes(json_dict["target_puzzle_hash"])),
bytes32.from_hexstr(json_dict["target_puzzle_hash"]),
pool_url,
json_dict["relative_lock_height"],
fee,
Expand Down
3 changes: 1 addition & 2 deletions chia/pools/pool_wallet_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from chia.protocols.pool_protocol import POOL_PROTOCOL_VERSION
from chia.types.blockchain_format.coin import Coin
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint8, uint32
from chia.util.streamable import Streamable, streamable

Expand Down Expand Up @@ -75,7 +74,7 @@ def initial_pool_state_from_dict(
pool_url: str = ""
relative_lock_height = uint32(0)
elif singleton_state == FARMING_TO_POOL:
target_puzzle_hash = bytes32(hexstr_to_bytes(state_dict["target_puzzle_hash"]))
target_puzzle_hash = bytes32.from_hexstr(state_dict["target_puzzle_hash"])
pool_url = state_dict["pool_url"]
relative_lock_height = uint32(state_dict["relative_lock_height"])
else:
Expand Down
18 changes: 9 additions & 9 deletions chia/rpc/data_layer_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def process_change(change: Dict[str, Any]) -> Dict[str, Any]:
# have to deal with maybe-present attributes or Dict[str, Any] hints.
reference_node_hash = change.get("reference_node_hash")
if reference_node_hash is not None:
reference_node_hash = bytes32(hexstr_to_bytes(reference_node_hash))
reference_node_hash = bytes32.from_hexstr(reference_node_hash)

side = change.get("side")
if side is not None:
Expand Down Expand Up @@ -216,7 +216,7 @@ async def get_keys(self, request: Dict[str, Any]) -> EndpointResult:
return response

async def get_keys_values(self, request: Dict[str, Any]) -> EndpointResult:
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
# NOTE: being outside the rpc, this retains the none-means-unspecified semantics
root_hash: Optional[str] = request.get("root_hash")
page = request.get("page", None)
Expand Down Expand Up @@ -256,7 +256,7 @@ async def get_keys_values(self, request: Dict[str, Any]) -> EndpointResult:
return response

async def get_ancestors(self, request: Dict[str, Any]) -> EndpointResult:
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
node_hash = bytes32.from_hexstr(request["hash"])
if self.service is None:
raise Exception("Data layer not created")
Expand All @@ -270,7 +270,7 @@ async def batch_update(self, request: Dict[str, Any]) -> EndpointResult:
"""
fee = get_fee(self.service.config, request)
changelist = [process_change(change) for change in request["changelist"]]
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
submit_on_chain = request.get("submit_on_chain", True)
# todo input checks
if self.service is None:
Expand Down Expand Up @@ -302,7 +302,7 @@ async def multistore_batch_update(self, request: Dict[str, Any]) -> EndpointResu
return {}

async def submit_pending_root(self, request: Dict[str, Any]) -> EndpointResult:
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
fee = get_fee(self.service.config, request)
transaction_record = await self.service.submit_pending_root(store_id, uint64(fee))
return {"tx_id": transaction_record.name}
Expand All @@ -320,7 +320,7 @@ async def insert(self, request: Dict[str, Any]) -> EndpointResult:
fee = get_fee(self.service.config, request)
key = hexstr_to_bytes(request["key"])
value = hexstr_to_bytes(request["value"])
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
# todo input checks
if self.service is None:
raise Exception("Data layer not created")
Expand All @@ -336,7 +336,7 @@ async def delete_key(self, request: Dict[str, Any]) -> EndpointResult:
"""
fee = get_fee(self.service.config, request)
key = hexstr_to_bytes(request["key"])
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
# todo input checks
if self.service is None:
raise Exception("Data layer not created")
Expand All @@ -347,7 +347,7 @@ async def delete_key(self, request: Dict[str, Any]) -> EndpointResult:

async def get_root(self, request: Dict[str, Any]) -> EndpointResult:
"""get hash of latest tree root"""
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
# todo input checks
if self.service is None:
raise Exception("Data layer not created")
Expand All @@ -358,7 +358,7 @@ async def get_root(self, request: Dict[str, Any]) -> EndpointResult:

async def get_local_root(self, request: Dict[str, Any]) -> EndpointResult:
"""get hash of latest tree root saved in our local datastore"""
store_id = bytes32(hexstr_to_bytes(request["id"]))
store_id = bytes32.from_hexstr(request["id"])
# todo input checks
if self.service is None:
raise Exception("Data layer not created")
Expand Down
3 changes: 1 addition & 2 deletions chia/rpc/farmer_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from chia.protocols.harvester_protocol import Plot
from chia.rpc.rpc_server import Endpoint, EndpointResult
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint32
from chia.util.paginator import Paginator
from chia.util.streamable import Streamable, streamable
Expand Down Expand Up @@ -354,7 +353,7 @@ async def get_harvester_plots_duplicates(self, request_dict: Dict[str, object])
return self.paginated_plot_path_request(Receiver.duplicates, request_dict)

async def get_pool_login_link(self, request: Dict[str, Any]) -> EndpointResult:
launcher_id: bytes32 = bytes32(hexstr_to_bytes(request["launcher_id"]))
launcher_id: bytes32 = bytes32.from_hexstr(request["launcher_id"])
login_link: Optional[str] = await self.service.generate_login_link(launcher_id)
if login_link is None:
raise ValueError(f"Failed to generate login link for {launcher_id.hex()}")
Expand Down
5 changes: 2 additions & 3 deletions chia/rpc/full_node_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from chia.types.full_block import FullBlock
from chia.types.spend_bundle import SpendBundle
from chia.types.unfinished_header_block import UnfinishedHeaderBlock
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint32


Expand Down Expand Up @@ -235,13 +234,13 @@ async def get_puzzle_and_solution(self, coin_id: bytes32, height: uint32) -> Opt

async def get_all_mempool_tx_ids(self) -> List[bytes32]:
response = await self.fetch("get_all_mempool_tx_ids", {})
return [bytes32(hexstr_to_bytes(tx_id_hex)) for tx_id_hex in response["tx_ids"]]
return [bytes32.from_hexstr(tx_id_hex) for tx_id_hex in response["tx_ids"]]

async def get_all_mempool_items(self) -> Dict[bytes32, Dict[str, Any]]:
response = await self.fetch("get_all_mempool_items", {})
converted: Dict[bytes32, Dict[str, Any]] = {}
for tx_id_hex, item in response["mempool_items"].items():
converted[bytes32(hexstr_to_bytes(tx_id_hex))] = item
converted[bytes32.from_hexstr(tx_id_hex)] = item
return converted

async def get_mempool_item_by_tx_id(
Expand Down
10 changes: 5 additions & 5 deletions chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ async def get_wallet_balances(self, request: Dict[str, Any]) -> EndpointResult:
return {"wallet_balances": wallet_balances}

async def get_transaction(self, request: Dict[str, Any]) -> EndpointResult:
transaction_id: bytes32 = bytes32(hexstr_to_bytes(request["transaction_id"]))
transaction_id: bytes32 = bytes32.from_hexstr(request["transaction_id"])
tr: Optional[TransactionRecord] = await self.service.wallet_state_manager.get_transaction(transaction_id)
if tr is None:
raise ValueError(f"Transaction 0x{transaction_id.hex()} not found")
Expand All @@ -1058,7 +1058,7 @@ async def get_transaction(self, request: Dict[str, Any]) -> EndpointResult:
}

async def get_transaction_memo(self, request: Dict[str, Any]) -> EndpointResult:
transaction_id: bytes32 = bytes32(hexstr_to_bytes(request["transaction_id"]))
transaction_id: bytes32 = bytes32.from_hexstr(request["transaction_id"])
tr: Optional[TransactionRecord] = await self.service.wallet_state_manager.get_transaction(transaction_id)
if tr is None:
raise ValueError(f"Transaction 0x{transaction_id.hex()} not found")
Expand Down Expand Up @@ -3681,7 +3681,7 @@ async def nft_mint_bulk(
if xch_change_target[:2] == "xch":
xch_change_ph = decode_puzzle_hash(xch_change_target)
else:
xch_change_ph = bytes32(hexstr_to_bytes(xch_change_target))
xch_change_ph = bytes32.from_hexstr(xch_change_target)
else:
xch_change_ph = None
new_innerpuzhash = request.get("new_innerpuzhash", None)
Expand All @@ -3693,7 +3693,7 @@ async def nft_mint_bulk(
did_coin = None
did_lineage_parent_hex = request.get("did_lineage_parent", None)
if did_lineage_parent_hex:
did_lineage_parent = bytes32(hexstr_to_bytes(did_lineage_parent_hex))
did_lineage_parent = bytes32.from_hexstr(did_lineage_parent_hex)
else:
did_lineage_parent = None
mint_from_did = request.get("mint_from_did", False)
Expand Down Expand Up @@ -3982,7 +3982,7 @@ async def pw_join_pool(
raise ValueError("Wallet needs to be fully synced.")

if "target_puzzlehash" in request:
target_puzzlehash = bytes32(hexstr_to_bytes(request["target_puzzlehash"]))
target_puzzlehash = bytes32.from_hexstr(request["target_puzzlehash"])
assert target_puzzlehash is not None
new_target_state: PoolState = create_pool_state(
FARMING_TO_POOL,
Expand Down
3 changes: 1 addition & 2 deletions chia/simulator/simulator_full_node_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from chia.types.coin_record import CoinRecord
from chia.types.full_block import FullBlock
from chia.util.bech32m import encode_puzzle_hash
from chia.util.byte_types import hexstr_to_bytes
from chia.util.ints import uint128


Expand All @@ -33,7 +32,7 @@ async def get_auto_farming(self) -> bool:

async def get_farming_ph(self) -> bytes32:
result = await self.fetch("get_farming_ph", {})
return bytes32(hexstr_to_bytes(result["puzzle_hash"]))
return bytes32.from_hexstr(result["puzzle_hash"])

async def get_all_coins(self, include_spent_coins: bool = False) -> List[CoinRecord]:
json_result = await self.fetch("get_all_coins", {"include_spent_coins": include_spent_coins})
Expand Down
2 changes: 1 addition & 1 deletion chia/wallet/cat_wallet/cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async def get_or_create_wallet_for_cat(
elif name is None:
name = self.default_wallet_name_for_unknown_cat(limitations_program_hash_hex)

limitations_program_hash = bytes32(hexstr_to_bytes(limitations_program_hash_hex))
limitations_program_hash = bytes32.from_hexstr(limitations_program_hash_hex)
self.cat_info = CATInfo(limitations_program_hash, None)
info_as_string = bytes(self.cat_info).hex()
self.wallet_info = await wallet_state_manager.user_store.create_wallet(name, WalletType.CAT, info_as_string)
Expand Down
2 changes: 1 addition & 1 deletion chia/wallet/cat_wallet/dao_cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def get_or_create_wallet_for_cat(
if name is None:
name = CATWallet.default_wallet_name_for_unknown_cat(limitations_program_hash_hex)

limitations_program_hash = bytes32(hexstr_to_bytes(limitations_program_hash_hex))
limitations_program_hash = bytes32.from_hexstr(limitations_program_hash_hex)

self.dao_cat_info = DAOCATInfo(
dao_wallet_id,
Expand Down

0 comments on commit 7d88089

Please sign in to comment.