From fcde4bd619cf9cbac00926570b2dffcfd585f15a Mon Sep 17 00:00:00 2001 From: Izumi Hoshino Date: Tue, 18 Jul 2023 00:23:20 +0900 Subject: [PATCH] =?UTF-8?q?Added=20`blocks=5Fwon`=20and=20`last=5Ftime=5Ff?= =?UTF-8?q?armed`=20to=20`get=5Ffarmed=5Famount`=20Wall=E2=80=A6=20(#15778?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added `blocks_won` and `last_time_farmed` to `get_farmed_amount` Wallet RPC API * Updated code according to the review * Fixed performance degradation * Fixed lint error --- chia/rpc/wallet_rpc_api.py | 16 ++++++++++++---- tests/wallet/rpc/test_wallet_rpc.py | 8 +++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/chia/rpc/wallet_rpc_api.py b/chia/rpc/wallet_rpc_api.py index f21883e82d..567babac2b 100644 --- a/chia/rpc/wallet_rpc_api.py +++ b/chia/rpc/wallet_rpc_api.py @@ -2965,7 +2965,8 @@ class WalletRpcApi: pool_reward_amount = 0 farmer_reward_amount = 0 fee_amount = 0 - last_height_farmed = 0 + blocks_won = uint32(0) + last_height_farmed = uint32(0) for record in tx_records: if record.wallet_id not in self.service.wallet_state_manager.wallets: continue @@ -2978,15 +2979,20 @@ class WalletRpcApi: # .get_farming_rewards() above queries for only confirmed records. This # could be hinted by making TransactionRecord generic but streamable can't # handle that presently. Existing code would have raised an exception - # anyways if this were to fail and we already have an assert below. + # anyway if this were to fail and we already have an assert below. assert height is not None if record.type == TransactionType.FEE_REWARD: - fee_amount += record.amount - calculate_base_farmer_reward(height) - farmer_reward_amount += calculate_base_farmer_reward(height) + base_farmer_reward = calculate_base_farmer_reward(height) + fee_amount += record.amount - base_farmer_reward + farmer_reward_amount += base_farmer_reward + blocks_won += 1 if height > last_height_farmed: last_height_farmed = height amount += record.amount + last_time_farmed = uint32( + await self.service.get_timestamp_for_height(last_height_farmed) if last_height_farmed > 0 else 0 + ) assert amount == pool_reward_amount + farmer_reward_amount + fee_amount return { "farmed_amount": amount, @@ -2994,6 +3000,8 @@ class WalletRpcApi: "farmer_reward_amount": farmer_reward_amount, "fee_amount": fee_amount, "last_height_farmed": last_height_farmed, + "last_time_farmed": last_time_farmed, + "blocks_won": blocks_won, } async def create_signed_transaction(self, request, hold_lock=True) -> EndpointResult: diff --git a/tests/wallet/rpc/test_wallet_rpc.py b/tests/wallet/rpc/test_wallet_rpc.py index b6062c4683..7920f589f7 100644 --- a/tests/wallet/rpc/test_wallet_rpc.py +++ b/tests/wallet/rpc/test_wallet_rpc.py @@ -385,18 +385,20 @@ async def test_get_farmed_amount(wallet_rpc_environment: WalletRpcTestEnvironmen wallet_rpc_client = env.wallet_1.rpc_client await full_node_api.farm_blocks_to_wallet(2, wallet) - result = await wallet_rpc_client.get_farmed_amount() + get_farmed_amount_result = await wallet_rpc_client.get_farmed_amount() + get_timestamp_for_height_result = await wallet_rpc_client.get_timestamp_for_height(uint32(2)) expected_result = { + "blocks_won": 2, "farmed_amount": 4_000_000_000_000, "farmer_reward_amount": 500_000_000_000, "fee_amount": 0, "last_height_farmed": 2, + "last_time_farmed": get_timestamp_for_height_result, "pool_reward_amount": 3_500_000_000_000, "success": True, } - - assert result == expected_result + assert get_farmed_amount_result == expected_result @pytest.mark.asyncio