From ade852ce17e82e51a3e7f8de604903dcd2875c05 Mon Sep 17 00:00:00 2001 From: Adam Kelly <338792+aqk@users.noreply.github.com> Date: Wed, 20 Apr 2022 11:39:38 -0700 Subject: [PATCH] Print MempoolInclusionStatus as string when reporting mempool inclusion status (#11133) --- chia/cmds/cmds_util.py | 12 +++++++++++ chia/cmds/plotnft_funcs.py | 9 ++++---- chia/cmds/wallet_funcs.py | 5 +++-- chia/types/mempool_submission_status.py | 28 +++++++++++++++++++++++++ chia/wallet/trade_record.py | 2 +- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 chia/cmds/cmds_util.py create mode 100644 chia/types/mempool_submission_status.py diff --git a/chia/cmds/cmds_util.py b/chia/cmds/cmds_util.py new file mode 100644 index 000000000000..f2c764bfc9a1 --- /dev/null +++ b/chia/cmds/cmds_util.py @@ -0,0 +1,12 @@ +from chia.types.blockchain_format.sized_bytes import bytes32 +from chia.types.mempool_submission_status import MempoolSubmissionStatus +from chia.wallet.transaction_record import TransactionRecord + + +def transaction_submitted_msg(tx: TransactionRecord) -> str: + sent_to = [MempoolSubmissionStatus(s[0], s[1], s[2]).to_json_dict_convenience() for s in tx.sent_to] + return f"Transaction submitted to nodes: {sent_to}" + + +def transaction_status_msg(fingerprint: int, tx_id: bytes32) -> str: + return f"Run 'chia wallet get_transaction -f {fingerprint} -tx 0x{tx_id}' to get status" diff --git a/chia/cmds/plotnft_funcs.py b/chia/cmds/plotnft_funcs.py index b70f202a5dd8..e739c49f6bf0 100644 --- a/chia/cmds/plotnft_funcs.py +++ b/chia/cmds/plotnft_funcs.py @@ -26,6 +26,7 @@ from chia.util.config import load_config from chia.util.default_root import DEFAULT_ROOT_PATH from chia.util.ints import uint16, uint32, uint64 +from chia.cmds.cmds_util import transaction_submitted_msg, transaction_status_msg from chia.wallet.transaction_record import TransactionRecord from chia.wallet.util.wallet_types import WalletType @@ -100,8 +101,8 @@ async def create(args: dict, wallet_client: WalletRpcClient, fingerprint: int) - await asyncio.sleep(0.1) tx = await wallet_client.get_transaction(str(1), tx_record.name) if len(tx.sent_to) > 0: - print(f"Transaction submitted to nodes: {tx.sent_to}") - print(f"Do chia wallet get_transaction -f {fingerprint} -tx 0x{tx_record.name} to get status") + print(transaction_submitted_msg(tx)) + print(transaction_status_msg(fingerprint, tx_record.name)) return None except Exception as e: print(f"Error creating plot NFT: {e}\n Please start both farmer and wallet with: chia start -r farmer") @@ -286,8 +287,8 @@ async def submit_tx_with_confirmation( await asyncio.sleep(0.1) tx = await wallet_client.get_transaction(str(1), tx_record.name) if len(tx.sent_to) > 0: - print(f"Transaction submitted to nodes: {tx.sent_to}") - print(f"Do chia wallet get_transaction -f {fingerprint} -tx 0x{tx_record.name} to get status") + print(transaction_submitted_msg(tx)) + print(transaction_status_msg(fingerprint, tx_record.name)) return None except Exception as e: print(f"Error performing operation on Plot NFT -f {fingerprint} wallet id: {wallet_id}: {e}") diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index c6b074e4879b..1522d21c87c7 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -18,6 +18,7 @@ from chia.util.config import load_config from chia.util.default_root import DEFAULT_ROOT_PATH from chia.util.ints import uint16, uint32, uint64 +from chia.cmds.cmds_util import transaction_submitted_msg, transaction_status_msg from chia.wallet.trade_record import TradeRecord from chia.wallet.trading.offer import Offer from chia.wallet.trading.trade_status import TradeStatus @@ -228,8 +229,8 @@ async def send(args: dict, wallet_client: WalletRpcClient, fingerprint: int) -> await asyncio.sleep(0.1) tx = await wallet_client.get_transaction(str(wallet_id), tx_id) if len(tx.sent_to) > 0: - print(f"Transaction submitted to nodes: {tx.sent_to}") - print(f"Do chia wallet get_transaction -f {fingerprint} -tx 0x{tx_id} to get status") + print(transaction_submitted_msg(tx)) + print(transaction_status_msg(fingerprint, tx_id)) return None print("Transaction not yet submitted to nodes") diff --git a/chia/types/mempool_submission_status.py b/chia/types/mempool_submission_status.py new file mode 100644 index 000000000000..9ba1d5c70cca --- /dev/null +++ b/chia/types/mempool_submission_status.py @@ -0,0 +1,28 @@ +from dataclasses import dataclass +from typing import Dict, Optional, Union + +from chia.types.mempool_inclusion_status import MempoolInclusionStatus +from chia.util.ints import uint8 +from chia.util.streamable import Streamable, streamable + + +@streamable +@dataclass(frozen=True) +class MempoolSubmissionStatus(Streamable): + """ + :sent_to: in `TradeRecord` and `TransactionRecord` are a + Tuple of (peer_id: str, status: MempoolInclusionStatus, error: Optional[str]) + MempoolInclusionStatus is represented as a uint8 in those structs so they can be `Streamable` + """ + + peer_id: str + inclusion_status: uint8 # MempoolInclusionStatus + error_msg: Optional[str] + + def to_json_dict_convenience(self) -> Dict[str, Union[str, MempoolInclusionStatus, Optional[str]]]: + formatted = self.to_json_dict() + formatted["inclusion_status"] = MempoolInclusionStatus(self.inclusion_status).name + return formatted + + def __str__(self) -> str: + return f"{self.to_json_dict_convenience()}" diff --git a/chia/wallet/trade_record.py b/chia/wallet/trade_record.py index 08c56bb4bb60..1cf9a452df2b 100644 --- a/chia/wallet/trade_record.py +++ b/chia/wallet/trade_record.py @@ -26,7 +26,7 @@ class TradeRecord(Streamable): coins_of_interest: List[Coin] trade_id: bytes32 status: uint32 # TradeStatus, enum not streamable - sent_to: List[Tuple[str, uint8, Optional[str]]] + sent_to: List[Tuple[str, uint8, Optional[str]]] # MempoolSubmissionStatus.status enum not streamable def to_json_dict_convenience(self) -> Dict[str, Any]: formatted = self.to_json_dict()