Skip to content

Commit

Permalink
Keep backwards compatibility on coin record RPCs (Chia-Network#9938)
Browse files Browse the repository at this point in the history
* Keep backwards compatibility on coin record RPCs

* Abstract functionality to a function
  • Loading branch information
Quexington authored Jan 27, 2022
1 parent 5ebcd82 commit 70150e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
15 changes: 10 additions & 5 deletions chia/rpc/full_node_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from chia.util.ws_message import WsRpcMessage, create_payload_dict


def coin_record_dict_backwards_compat(coin_record: Dict[str, Any]):
coin_record["spent"] = coin_record["spent_block_index"] > 0
return coin_record


class FullNodeRpcApi:
def __init__(self, service: FullNode):
self.service = service
Expand Down Expand Up @@ -441,7 +446,7 @@ async def get_coin_records_by_puzzle_hash(self, request: Dict) -> Optional[Dict]

coin_records = await self.service.blockchain.coin_store.get_coin_records_by_puzzle_hash(**kwargs)

return {"coin_records": coin_records}
return {"coin_records": [coin_record_dict_backwards_compat(cr.to_json_dict()) for cr in coin_records]}

async def get_coin_records_by_puzzle_hashes(self, request: Dict) -> Optional[Dict]:
"""
Expand All @@ -463,7 +468,7 @@ async def get_coin_records_by_puzzle_hashes(self, request: Dict) -> Optional[Dic

coin_records = await self.service.blockchain.coin_store.get_coin_records_by_puzzle_hashes(**kwargs)

return {"coin_records": coin_records}
return {"coin_records": [coin_record_dict_backwards_compat(cr.to_json_dict()) for cr in coin_records]}

async def get_coin_record_by_name(self, request: Dict) -> Optional[Dict]:
"""
Expand All @@ -477,7 +482,7 @@ async def get_coin_record_by_name(self, request: Dict) -> Optional[Dict]:
if coin_record is None:
raise ValueError(f"Coin record 0x{name.hex()} not found")

return {"coin_record": coin_record}
return {"coin_record": coin_record_dict_backwards_compat(coin_record.to_json_dict())}

async def get_coin_records_by_names(self, request: Dict) -> Optional[Dict]:
"""
Expand All @@ -499,7 +504,7 @@ async def get_coin_records_by_names(self, request: Dict) -> Optional[Dict]:

coin_records = await self.service.blockchain.coin_store.get_coin_records_by_names(**kwargs)

return {"coin_records": coin_records}
return {"coin_records": [coin_record_dict_backwards_compat(cr.to_json_dict()) for cr in coin_records]}

async def get_coin_records_by_parent_ids(self, request: Dict) -> Optional[Dict]:
"""
Expand All @@ -521,7 +526,7 @@ async def get_coin_records_by_parent_ids(self, request: Dict) -> Optional[Dict]:

coin_records = await self.service.blockchain.coin_store.get_coin_records_by_parent_ids(**kwargs)

return {"coin_records": coin_records}
return {"coin_records": [coin_record_dict_backwards_compat(cr.to_json_dict()) for cr in coin_records]}

async def push_tx(self, request: Dict) -> Optional[Dict]:
if "spend_bundle" not in request:
Expand Down
36 changes: 19 additions & 17 deletions chia/rpc/full_node_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from chia.util.ints import uint32, uint64


def coin_record_dict_backwards_compat(coin_record: Dict[str, Any]):
del coin_record["spent"]
return coin_record


class FullNodeRpcClient(RpcClient):
"""
Client to Chia RPC, connects to a local full node. Uses HTTP/JSON, and converts back from
Expand Down Expand Up @@ -86,7 +91,8 @@ async def get_coin_record_by_name(self, coin_id: bytes32) -> Optional[CoinRecord
response = await self.fetch("get_coin_record_by_name", {"name": coin_id.hex()})
except Exception:
return None
return CoinRecord.from_json_dict(response["coin_record"])

return CoinRecord.from_json_dict(coin_record_dict_backwards_compat(response["coin_record"]))

async def get_coin_records_by_names(
self,
Expand All @@ -101,10 +107,9 @@ async def get_coin_records_by_names(
d["start_height"] = start_height
if end_height is not None:
d["end_height"] = end_height
return [
CoinRecord.from_json_dict(coin)
for coin in (await self.fetch("get_coin_records_by_names", d))["coin_records"]
]

response = await self.fetch("get_coin_records_by_names", d)
return [CoinRecord.from_json_dict(coin_record_dict_backwards_compat(coin)) for coin in response["coin_records"]]

async def get_coin_records_by_puzzle_hash(
self,
Expand All @@ -118,10 +123,9 @@ async def get_coin_records_by_puzzle_hash(
d["start_height"] = start_height
if end_height is not None:
d["end_height"] = end_height
return [
CoinRecord.from_json_dict(coin)
for coin in (await self.fetch("get_coin_records_by_puzzle_hash", d))["coin_records"]
]

response = await self.fetch("get_coin_records_by_puzzle_hash", d)
return [CoinRecord.from_json_dict(coin_record_dict_backwards_compat(coin)) for coin in response["coin_records"]]

async def get_coin_records_by_puzzle_hashes(
self,
Expand All @@ -136,10 +140,9 @@ async def get_coin_records_by_puzzle_hashes(
d["start_height"] = start_height
if end_height is not None:
d["end_height"] = end_height
return [
CoinRecord.from_json_dict(coin)
for coin in (await self.fetch("get_coin_records_by_puzzle_hashes", d))["coin_records"]
]

response = await self.fetch("get_coin_records_by_puzzle_hashes", d)
return [CoinRecord.from_json_dict(coin_record_dict_backwards_compat(coin)) for coin in response["coin_records"]]

async def get_coin_records_by_parent_ids(
self,
Expand All @@ -154,10 +157,9 @@ async def get_coin_records_by_parent_ids(
d["start_height"] = start_height
if end_height is not None:
d["end_height"] = end_height
return [
CoinRecord.from_json_dict(coin)
for coin in (await self.fetch("get_coin_records_by_parent_ids", d))["coin_records"]
]

response = await self.fetch("get_coin_records_by_parent_ids", d)
return [CoinRecord.from_json_dict(coin_record_dict_backwards_compat(coin)) for coin in response["coin_records"]]

async def get_additions_and_removals(self, header_hash: bytes32) -> Tuple[List[CoinRecord], List[CoinRecord]]:
try:
Expand Down

0 comments on commit 70150e9

Please sign in to comment.