Skip to content

Commit 148b813

Browse files
authored
Include plotNFT rewards in get_farmed_amount (#19245)
* Include plotNFT rewards in get_farmed_amount * Add flag to include pool rewards in farm summary * Add -i for short flag * Fix expected args * Include additional farming detail * Include additional farming detail * Format/organize output * Move block rewards * Update test * Format * Update test_farm_cmd.py * revert test bool * Passing tests * Added test for 'include_pool_rewards=True' * Address linting errors
1 parent beecb8a commit 148b813

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

chia/_tests/cmds/test_farm_cmd.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ async def receiver_available() -> bool:
4646
wallet_rpc_port = wallet_service.rpc_server.webserver.listen_port
4747
farmer_rpc_port = farmer_service.rpc_server.webserver.listen_port
4848

49-
await summary(full_node_rpc_port, wallet_rpc_port, None, farmer_rpc_port, bt.root_path)
49+
# Test with include_pool_rewards=False (original test)
50+
await summary(
51+
rpc_port=full_node_rpc_port,
52+
wallet_rpc_port=wallet_rpc_port,
53+
harvester_rpc_port=None,
54+
farmer_rpc_port=farmer_rpc_port,
55+
include_pool_rewards=False,
56+
root_path=bt.root_path,
57+
)
5058

5159
captured = capsys.readouterr()
5260
match = re.search(r"^.+(Farming status:.+)$", captured.out, re.DOTALL)
@@ -64,3 +72,47 @@ async def receiver_available() -> bool:
6472
assert "e (effective)" in lines[8]
6573
assert "Estimated network space:" in lines[9]
6674
assert "Expected time to win:" in lines[10]
75+
76+
# Test with include_pool_rewards=True
77+
await summary(
78+
rpc_port=full_node_rpc_port,
79+
wallet_rpc_port=wallet_rpc_port,
80+
harvester_rpc_port=None,
81+
farmer_rpc_port=farmer_rpc_port,
82+
include_pool_rewards=True,
83+
root_path=bt.root_path,
84+
)
85+
86+
captured = capsys.readouterr()
87+
match = re.search(r"Farming status:.*", captured.out, re.DOTALL)
88+
assert match, "no 'Farming status:' line"
89+
output = match.group(0).strip()
90+
lines = [line.strip() for line in output.splitlines()]
91+
92+
# always check these first six lines
93+
assert lines[0].startswith("Farming status:")
94+
assert lines[1].startswith("Total chia farmed:")
95+
assert lines[2].startswith("User transaction fees:")
96+
assert lines[3].startswith("Farmer rewards:")
97+
assert lines[4].startswith("Pool rewards:")
98+
assert lines[5].startswith("Total rewards:")
99+
100+
# decide where the harvester section starts
101+
if "Current/Last height farmed:" in output:
102+
# we saw the height-farmed block, so it occupies lines[6-8]
103+
assert lines[6].startswith("Current/Last height farmed:")
104+
assert lines[7].startswith("Blocks since last farmed:")
105+
assert lines[8].startswith("Time since last farmed:")
106+
harvester_idx = 9
107+
else:
108+
# no height block, so harvester begins at line 6
109+
harvester_idx = 6
110+
111+
# now the harvester lines
112+
assert lines[harvester_idx] == "Local Harvester"
113+
assert "plots of size" in lines[harvester_idx + 1]
114+
assert lines[harvester_idx + 2].startswith("Plot count for all harvesters:")
115+
assert lines[harvester_idx + 3].startswith("Total size of plots:")
116+
assert lines[harvester_idx + 4].startswith("Estimated network space:")
117+
assert lines[harvester_idx + 5].startswith("Expected time to win:")
118+
assert lines[harvester_idx + 6].startswith("Note:")

chia/cmds/farm.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,21 @@ def farm_cmd() -> None:
4949
default=None,
5050
show_default=True,
5151
)
52+
@click.option(
53+
"-i",
54+
"--include-pool-rewards",
55+
help="Include pool farming rewards in the total farmed amount",
56+
is_flag=True,
57+
default=False,
58+
)
5259
@click.pass_context
5360
def summary_cmd(
5461
ctx: click.Context,
5562
rpc_port: Optional[int],
5663
wallet_rpc_port: Optional[int],
5764
harvester_rpc_port: Optional[int],
5865
farmer_rpc_port: Optional[int],
66+
include_pool_rewards: bool,
5967
) -> None:
6068
import asyncio
6169

@@ -67,6 +75,7 @@ def summary_cmd(
6775
wallet_rpc_port,
6876
harvester_rpc_port,
6977
farmer_rpc_port,
78+
include_pool_rewards,
7079
root_path=ChiaCliContext.set_default(ctx).root_path,
7180
)
7281
)

chia/cmds/farm_funcs.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ async def get_average_block_time(rpc_port: Optional[int], root_path: Path) -> fl
4949
return (curr.timestamp - past_curr.timestamp) / (curr.height - past_curr.height)
5050

5151

52-
async def get_wallets_stats(wallet_rpc_port: Optional[int], root_path: Path) -> Optional[dict[str, Any]]:
52+
async def get_wallets_stats(
53+
wallet_rpc_port: Optional[int],
54+
root_path: Path,
55+
include_pool_rewards: bool,
56+
) -> Optional[dict[str, Any]]:
5357
async with get_any_service_client(WalletRpcClient, root_path, wallet_rpc_port) as (wallet_client, _):
54-
return await wallet_client.get_farmed_amount()
58+
return await wallet_client.get_farmed_amount(include_pool_rewards)
5559

5660

5761
async def get_challenges(root_path: Path, farmer_rpc_port: Optional[int]) -> Optional[list[dict[str, Any]]]:
@@ -80,6 +84,7 @@ async def summary(
8084
wallet_rpc_port: Optional[int],
8185
harvester_rpc_port: Optional[int],
8286
farmer_rpc_port: Optional[int],
87+
include_pool_rewards: bool,
8388
root_path: Path,
8489
) -> None:
8590
harvesters_summary = await get_harvesters_summary(farmer_rpc_port, root_path)
@@ -97,7 +102,7 @@ async def summary(
97102
wallet_not_ready: bool = False
98103
amounts = None
99104
try:
100-
amounts = await get_wallets_stats(wallet_rpc_port, root_path)
105+
amounts = await get_wallets_stats(wallet_rpc_port, root_path, include_pool_rewards)
101106
except CliRpcConnectionError:
102107
wallet_not_ready = True
103108
except Exception:
@@ -120,8 +125,21 @@ async def summary(
120125
if amounts is not None:
121126
print(f"Total chia farmed: {amounts['farmed_amount'] / units['chia']}")
122127
print(f"User transaction fees: {amounts['fee_amount'] / units['chia']}")
123-
print(f"Block rewards: {(amounts['farmer_reward_amount'] + amounts['pool_reward_amount']) / units['chia']}")
124-
print(f"Last height farmed: {amounts['last_height_farmed']}")
128+
if include_pool_rewards:
129+
print(f"Farmer rewards: {amounts['farmer_reward_amount'] / units['chia']}")
130+
print(f"Pool rewards: {amounts['pool_reward_amount'] / units['chia']}")
131+
print(f"Total rewards: {(amounts['farmer_reward_amount'] + amounts['pool_reward_amount']) / units['chia']}")
132+
if blockchain_state is not None and blockchain_state["peak"] is not None:
133+
peak_height = blockchain_state["peak"].height
134+
blocks_since_last_farm = peak_height - amounts["last_height_farmed"]
135+
print(f"Current/Last height farmed: {peak_height}/{amounts['last_height_farmed']}")
136+
print(f"Blocks since last farmed: {blocks_since_last_farm}")
137+
print(
138+
f"Time since last farmed: {format_minutes(int((blocks_since_last_farm * SECONDS_PER_BLOCK) / 60))}"
139+
)
140+
else:
141+
print(f"Block rewards: {(amounts['farmer_reward_amount'] + amounts['pool_reward_amount']) / units['chia']}")
142+
print(f"Last height farmed: {amounts['last_height_farmed']}")
125143

126144
class PlotStats:
127145
total_plot_size = 0

chia/wallet/wallet_rpc_api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,12 +3520,18 @@ async def get_farmed_amount(self, request: dict[str, Any]) -> EndpointResult:
35203520
fee_amount = 0
35213521
blocks_won = 0
35223522
last_height_farmed = uint32(0)
3523+
3524+
include_pool_rewards = request.get("include_pool_rewards", False)
3525+
35233526
for record in tx_records:
35243527
if record.wallet_id not in self.service.wallet_state_manager.wallets:
35253528
continue
35263529
if record.type == TransactionType.COINBASE_REWARD.value:
3527-
if self.service.wallet_state_manager.wallets[record.wallet_id].type() == WalletType.POOLING_WALLET:
3528-
# Don't add pool rewards for pool wallets.
3530+
if (
3531+
not include_pool_rewards
3532+
and self.service.wallet_state_manager.wallets[record.wallet_id].type() == WalletType.POOLING_WALLET
3533+
):
3534+
# Don't add pool rewards for pool wallets unless explicitly requested
35293535
continue
35303536
pool_reward_amount += record.amount
35313537
height = record.height_farmed(self.service.constants.GENESIS_CHALLENGE)

chia/wallet/wallet_rpc_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ async def extend_derivation_index(self, index: int) -> str:
416416
updated_index = response["index"]
417417
return str(updated_index)
418418

419-
async def get_farmed_amount(self) -> dict[str, Any]:
420-
return await self.fetch("get_farmed_amount", {})
419+
async def get_farmed_amount(self, include_pool_rewards: bool = False) -> dict[str, Any]:
420+
return await self.fetch("get_farmed_amount", {"include_pool_rewards": include_pool_rewards})
421421

422422
async def create_signed_transactions(
423423
self,

0 commit comments

Comments
 (0)