Skip to content

Commit

Permalink
Merge #4770
Browse files Browse the repository at this point in the history
4770: Expose delegation rate in reward responses r=jacek-casper a=jacek-casper



Co-authored-by: Jacek Malec <[email protected]>
  • Loading branch information
casperlabs-bors-ng[bot] and jacek-casper authored Jul 4, 2024
2 parents 2018f2b + 970b027 commit 29697a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
29 changes: 24 additions & 5 deletions binary_port/src/type_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use datasize::DataSize;

use casper_types::{
bytesrepr::{self, Bytes, FromBytes, ToBytes},
system::auction::DelegationRate,
EraId, ExecutionInfo, Key, PublicKey, TimeDiff, Timestamp, Transaction, ValidatorChange, U512,
};
use serde::Serialize;
Expand Down Expand Up @@ -179,12 +180,17 @@ impl GetTrieFullResult {
pub struct RewardResponse {
amount: U512,
era_id: EraId,
delegation_rate: DelegationRate,
}

impl RewardResponse {
/// Constructs new reward response.
pub fn new(amount: U512, era_id: EraId) -> Self {
Self { amount, era_id }
pub fn new(amount: U512, era_id: EraId, delegation_rate: DelegationRate) -> Self {
Self {
amount,
era_id,
delegation_rate,
}
}

/// Returns the amount of the reward.
Expand All @@ -196,6 +202,11 @@ impl RewardResponse {
pub fn era_id(&self) -> EraId {
self.era_id
}

/// Returns the delegation rate of the validator.
pub fn delegation_rate(&self) -> DelegationRate {
self.delegation_rate
}
}

impl ToBytes for RewardResponse {
Expand All @@ -206,20 +217,27 @@ impl ToBytes for RewardResponse {
}

fn serialized_length(&self) -> usize {
self.amount.serialized_length() + self.era_id.serialized_length()
self.amount.serialized_length()
+ self.era_id.serialized_length()
+ self.delegation_rate.serialized_length()
}

fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
self.amount.write_bytes(writer)?;
self.era_id.write_bytes(writer)
self.era_id.write_bytes(writer)?;
self.delegation_rate.write_bytes(writer)
}
}

impl FromBytes for RewardResponse {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (amount, remainder) = FromBytes::from_bytes(bytes)?;
let (era_id, remainder) = FromBytes::from_bytes(remainder)?;
Ok((RewardResponse::new(amount, era_id), remainder))
let (delegation_rate, remainder) = FromBytes::from_bytes(remainder)?;
Ok((
RewardResponse::new(amount, era_id, delegation_rate),
remainder,
))
}
}

Expand Down Expand Up @@ -436,6 +454,7 @@ mod tests {
bytesrepr::test_serialization_roundtrip(&RewardResponse::new(
rng.gen(),
EraId::random(rng),
rng.gen(),
));
}

Expand Down
21 changes: 15 additions & 6 deletions node/src/components/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,22 +939,31 @@ where
let Some(validator_rewards) = block_rewards.get(&validator) else {
return BinaryResponse::new_empty(protocol_version);
};
match auction::reward(

let seigniorage_recipient = snapshot
.get(&header.era_id())
.and_then(|era| era.get(&validator));
let reward = auction::reward(
&validator,
delegator.as_deref(),
header.era_id(),
validator_rewards,
&snapshot,
) {
Ok(Some(reward)) => {
let response = RewardResponse::new(reward, header.era_id());
);
match (reward, seigniorage_recipient) {
(Ok(Some(reward)), Some(seigniorage_recipient)) => {
let response = RewardResponse::new(
reward,
header.era_id(),
*seigniorage_recipient.delegation_rate(),
);
BinaryResponse::from_value(response, protocol_version)
}
Ok(None) => BinaryResponse::new_empty(protocol_version),
Err(error) => {
(Err(error), _) => {
warn!(%error, "failed when calculating rewards");
BinaryResponse::new_error(ErrorCode::InternalError, protocol_version)
}
_ => BinaryResponse::new_empty(protocol_version),
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions node/src/reactor/main_reactor/tests/binary_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ async fn binary_port_component_handles_all_requests() {
None,
),
get_reward(
Some(EraIdentifier::Block(BlockIdentifier::Hash(
*highest_block.hash(),
))),
Some(EraIdentifier::Block(BlockIdentifier::Height(1))),
era_one_validator,
None,
),
Expand Down Expand Up @@ -932,7 +930,8 @@ fn get_reward(
}),
asserter: Box::new(move |response| {
assert_response::<RewardResponse, _>(response, Some(PayloadType::Reward), |reward| {
reward.amount() > U512::zero()
// test fixture sets delegation rate to 0
reward.amount() > U512::zero() && reward.delegation_rate() == 0
})
}),
}
Expand Down

0 comments on commit 29697a3

Please sign in to comment.