Skip to content

Commit

Permalink
[CHIA-1672] Add parsing for JSON formatted spend bundles in /push_tx (
Browse files Browse the repository at this point in the history
#18764)

Add parsing for JSON formatted spendbundles in `/push_tx`
  • Loading branch information
Quexington authored Oct 30, 2024
1 parent e1db6cb commit bea1447
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 3 additions & 3 deletions chia/_tests/wallet/cat_wallet/test_cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ async def test_cat_change_detection(wallet_environments: WalletTestFramework) ->
),
],
)
await env.rpc_client.push_tx(PushTX(bytes(eve_spend)))
await env.rpc_client.push_tx(PushTX(eve_spend))
await time_out_assert_not_none(5, full_node_api.full_node.mempool_manager.get_spendbundle, eve_spend.name())
await wallet_environments.process_pending_states(
[
Expand Down Expand Up @@ -1665,7 +1665,7 @@ async def test_cat_melt_balance(wallet_environments: WalletTestFramework) -> Non
)
],
)
await env.rpc_client.push_tx(PushTX(bytes(spend_to_wallet)))
await env.rpc_client.push_tx(PushTX(spend_to_wallet))
await time_out_assert(10, simulator.tx_id_in_mempool, True, spend_to_wallet.name())

await wallet_environments.process_pending_states(
Expand Down Expand Up @@ -1715,7 +1715,7 @@ async def test_cat_melt_balance(wallet_environments: WalletTestFramework) -> Non
],
)
signed_spend, _ = await env.wallet_state_manager.sign_bundle(new_spend.coin_spends)
await env.rpc_client.push_tx(PushTX(bytes(signed_spend)))
await env.rpc_client.push_tx(PushTX(signed_spend))
await time_out_assert(10, simulator.tx_id_in_mempool, True, signed_spend.name())

await wallet_environments.process_pending_states(
Expand Down
6 changes: 6 additions & 0 deletions chia/_tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
GetTimestampForHeight,
LogIn,
PushTransactions,
PushTX,
SetWalletResyncOnStartup,
SplitCoins,
SplitCoinsResponse,
Expand Down Expand Up @@ -432,6 +433,11 @@ async def test_push_transactions(wallet_rpc_environment: WalletRpcTestEnvironmen
for tx in resp_client.transactions:
assert (await client.get_transaction(transaction_id=tx.name)).confirmed

# Just testing NOT failure here really (parsing)
await client.push_tx(PushTX(spend_bundle))
resp = await client.fetch("push_tx", {"spend_bundle": bytes(spend_bundle).hex()})
assert resp["success"]


@pytest.mark.anyio
async def test_get_balance(wallet_rpc_environment: WalletRpcTestEnvironment):
Expand Down
13 changes: 12 additions & 1 deletion chia/rpc/wallet_request_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ class GetHeightInfoResponse(Streamable):
@streamable
@dataclass(frozen=True)
class PushTX(Streamable):
spend_bundle: bytes
spend_bundle: WalletSpendBundle

# We allow for flexibility in transaction parsing here so we need to override
@classmethod
def from_json_dict(cls, json_dict: dict[str, Any]) -> PushTX:
if isinstance(json_dict["spend_bundle"], str):
spend_bundle = WalletSpendBundle.from_bytes(hexstr_to_bytes(json_dict["spend_bundle"]))
else:
spend_bundle = WalletSpendBundle.from_json_dict(json_dict["spend_bundle"])

json_dict["spend_bundle"] = spend_bundle.to_json_dict()
return super().from_json_dict(json_dict)


@streamable
Expand Down
2 changes: 1 addition & 1 deletion chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ async def push_tx(self, request: PushTX) -> Empty:
nodes = self.service.server.get_connections(NodeType.FULL_NODE)
if len(nodes) == 0:
raise ValueError("Wallet is not currently connected to any full node peers")
await self.service.push_tx(WalletSpendBundle.from_bytes(request.spend_bundle))
await self.service.push_tx(request.spend_bundle)
return Empty()

@tx_endpoint(push=True)
Expand Down

0 comments on commit bea1447

Please sign in to comment.