Skip to content

Commit

Permalink
Replace filter_map to parameterize RewardType filter expression
Browse files Browse the repository at this point in the history
  • Loading branch information
CriesofCarrots committed Jan 18, 2024
1 parent 766a416 commit a56e364
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,16 @@ impl JsonRpcRequestProcessor {
})
}

async fn get_reward_map(
async fn get_reward_map<F>(
&self,
slot: Slot,
addresses: &[String],
reward_type_filter: F,
config: &RpcEpochConfig,
) -> Result<HashMap<String, (Reward, Slot)>> {
) -> Result<HashMap<String, (Reward, Slot)>>
where
F: Fn(RewardType) -> bool,
{
let Ok(Some(block)) = self
.get_block(
slot,
Expand All @@ -541,12 +545,11 @@ impl JsonRpcRequestProcessor {
.rewards
.unwrap_or_default()
.into_iter()
.filter_map(|reward| match reward.reward_type? {
RewardType::Staking | RewardType::Voting => addresses
.contains(&reward.pubkey)
.then(|| (reward.clone().pubkey, (reward, slot))),
_ => None,
.filter(|reward| {
reward.reward_type.is_some_and(reward_type_filter)
&& addresses.contains(&reward.pubkey)
})
.map(|reward| (reward.clone().pubkey, (reward, slot)))
.collect())
}

Expand Down Expand Up @@ -588,6 +591,9 @@ impl JsonRpcRequestProcessor {
}

let bank = self.get_bank_with_config(slot_context)?;
let partitioned_epoch_reward_enabled = bank
.feature_set
.is_active(&feature_set::enable_partitioned_epoch_reward::id());

let first_confirmed_block_in_epoch = *self
.get_blocks_with_limit(first_slot_in_epoch, 1, config.commitment)
Expand All @@ -601,14 +607,19 @@ impl JsonRpcRequestProcessor {
let addresses: Vec<String> =
addresses.iter().map(|pubkey| pubkey.to_string()).collect();

self.get_reward_map(first_confirmed_block_in_epoch, &addresses, &config)
.await?
self.get_reward_map(
first_confirmed_block_in_epoch,
&addresses,
|reward_type| -> bool {
reward_type == RewardType::Voting
|| (!partitioned_epoch_reward_enabled && reward_type == RewardType::Staking)
},
&config,
)
.await?
};

if bank
.feature_set
.is_active(&feature_set::enable_partitioned_epoch_reward::id())
{
if partitioned_epoch_reward_enabled {
let partition_data_address = get_epoch_rewards_partition_data_address(rewards_epoch);
let partition_data_account =
bank.get_account(&partition_data_address)
Expand Down Expand Up @@ -654,7 +665,14 @@ impl JsonRpcRequestProcessor {
.get(partition_index.saturating_add(1))
.ok_or_else(Error::internal_error)?;

let index_reward_map = self.get_reward_map(slot, addresses, &config).await?;
let index_reward_map = self
.get_reward_map(
slot,
addresses,
|reward_type| -> bool { reward_type == RewardType::Staking },
&config,
)
.await?;
reward_map.extend(index_reward_map);
}
}
Expand Down

0 comments on commit a56e364

Please sign in to comment.