Added blocks_won and last_time_farmed to get_farmed_amount Wall… (#15778)

* 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
This commit is contained in:
Izumi Hoshino
2023-07-17 10:23:20 -05:00
committed by GitHub
parent 494e6e7f22
commit fcde4bd619
2 changed files with 17 additions and 7 deletions
+12 -4
View File
@@ -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:
+5 -3
View File
@@ -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