Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bakers reward period #111

Merged
merged 11 commits into from
Aug 29, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Add a `Cis4Contract` for interacting with Cis4 contracts.
- Add a new `web3id` module that contains types and functionality for
construcing Web3ID credentials and verifying Web3ID proofs.
- Add support for `GetBakersRewardPeriod` endpoint (only available when querying a node
with at least version 6.1)
MilkywayPirate marked this conversation as resolved.
Show resolved Hide resolved

### Breaking changes in types
- `ConsensusInfo`
Expand Down
37 changes: 37 additions & 0 deletions examples/v2_get_bakers_reward_period.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Test the `GetBakersRewardPeriod` endpoint.
use anyhow::Context;
use clap::AppSettings;
use concordium_rust_sdk::v2;
use futures::StreamExt;
use structopt::StructOpt;

#[derive(StructOpt)]
struct App {
#[structopt(
long = "node",
help = "GRPC interface of the node.",
default_value = "http://localhost:20000"
)]
endpoint: v2::Endpoint,
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
let app = {
let app = App::clap().global_setting(AppSettings::ColoredHelp);
let matches = app.get_matches();
App::from_clap(&matches)
};

let mut client = v2::Client::new(app.endpoint)
.await
.context("Cannot connect.")?;
let mut res = client
.get_bakers_reward_period(&v2::BlockIdentifier::LastFinal)
.await?;
println!("Blockhash: {}", res.block_hash);
while let Some(a) = res.response.next().await {
println!("{:?}", a?);
}
Ok(())
}
26 changes: 26 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,32 @@ pub struct NodeInfo {
pub details: NodeDetails,
}

/// Information of a baker for a certain reward period.
#[derive(Debug)]
pub struct BakerRewardPeriodInfo {
/// Metadata for the baker.
MilkywayPirate marked this conversation as resolved.
Show resolved Hide resolved
pub baker: BakerInfo,
/// The stake of the baker that the
/// consensus protocol uses to determine lottery weight.
/// I.e. this is the stake after applying leverage bound and caps.
MilkywayPirate marked this conversation as resolved.
Show resolved Hide resolved
/// If the baker is also a finalizer then the effective stake is
/// also used to calculate the weight that the baker votes with as part of
/// the finalization committee.
pub effective_stake: Amount,
/// The effective commission rates for the baker that applies
/// in the reward period.
pub commission_rates: CommissionRates,
/// The amount that the baker staked itself in the
/// reward period.
pub equity_capital: Amount,
/// The amount that was delegated to the baker in the
/// reward period.
pub delegated_capital: Amount,
/// Whether the baker is part of the finalization committee
/// in the reward period.
pub is_finalizer: bool,
}

#[derive(Debug, SerdeDeserialize)]
#[serde(try_from = "wallet_account_json::VersionedWalletAccount")]
/// An account imported from one of the supported export formats.
Expand Down
15 changes: 15 additions & 0 deletions src/v2/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3134,6 +3134,21 @@ impl TryFrom<NextUpdateSequenceNumbers> for super::types::queries::NextUpdateSeq
}
}

impl TryFrom<BakerRewardPeriodInfo> for super::types::BakerRewardPeriodInfo {
type Error = tonic::Status;

fn try_from(message: BakerRewardPeriodInfo) -> Result<Self, Self::Error> {
Ok(Self {
baker: message.baker.require()?.try_into()?,
effective_stake: message.effective_stake.require()?.into(),
commission_rates: message.commission_rates.require()?.try_into()?,
equity_capital: message.equity_capital.require()?.into(),
delegated_capital: message.delegated_capital.require()?.into(),
is_finalizer: message.is_finalizer,
})
}
}

#[cfg(test)]
mod test {

Expand Down
51 changes: 51 additions & 0 deletions src/v2/generated/concordium.v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4655,6 +4655,31 @@ pub mod block_item {
UpdateInstruction(super::UpdateInstruction),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BakerRewardPeriodInfo {
/// The baker id and public keys for the baker.
#[prost(message, optional, tag = "1")]
pub baker: ::core::option::Option<BakerInfo>,
/// The effective stake of the baker for the consensus protocol.
/// The returned amount accounts for delegation, capital bounds and leverage
/// bounds.
#[prost(message, optional, tag = "2")]
pub effective_stake: ::core::option::Option<Amount>,
/// The effective commission rate for the baker that applies for the reward
/// period.
#[prost(message, optional, tag = "3")]
pub commission_rates: ::core::option::Option<CommissionRates>,
/// The amount staked by the baker itself.
#[prost(message, optional, tag = "4")]
pub equity_capital: ::core::option::Option<Amount>,
/// The total amount of capital delegated to this baker pool.
#[prost(message, optional, tag = "5")]
pub delegated_capital: ::core::option::Option<Amount>,
/// Whether the baker is a finalizer or not.
#[prost(bool, tag = "6")]
pub is_finalizer: bool,
}
/// Information about how open the pool is to new delegators.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
Expand Down Expand Up @@ -6050,5 +6075,31 @@ pub mod queries_client {
.server_streaming(request.into_request(), path, codec)
.await
}

/// Get all bakers in the reward period of a block.
/// This endpoint is only supported for protocol version 6 and onwards.
/// If the protocol does not support the endpoint then an
/// 'IllegalArgument' error is returned.
pub async fn get_bakers_reward_period(
&mut self,
request: impl tonic::IntoRequest<super::BlockHashInput>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::BakerRewardPeriodInfo>>,
tonic::Status,
> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/concordium.v2.Queries/GetBakersRewardPeriod",
);
self.inner
.server_streaming(request.into_request(), path, codec)
.await
}
}
}
22 changes: 22 additions & 0 deletions src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,28 @@ impl Client {
}
last_found.ok_or(QueryError::NotFound)
}

/// Get the bakers of a reward period.
MilkywayPirate marked this conversation as resolved.
Show resolved Hide resolved
pub async fn get_bakers_reward_period(
&mut self,
bi: impl IntoBlockIdentifier,
) -> endpoints::QueryResult<
QueryResponse<impl Stream<Item = Result<types::BakerRewardPeriodInfo, tonic::Status>>>,
> {
let response = self
.client
.get_bakers_reward_period(&bi.into_block_identifier())
.await?;
let block_hash = extract_metadata(&response)?;
let stream = response.into_inner().map(|result| match result {
Ok(baker) => baker.try_into(),
Err(err) => Err(err),
});
Ok(QueryResponse {
block_hash,
response: stream,
})
}
}

/// A stream of finalized blocks. This contains a background task that polls
Expand Down
2 changes: 1 addition & 1 deletion src/v2/proto_schema_version.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub const PROTO_SCHEMA_VERSION: &str = "e67aa923ef5f0bd31138a5c87a6fefe87101873c";
pub const PROTO_SCHEMA_VERSION: &str = "bc0b3fcb12cbfb6336051a0bf40ae36474587790";
Loading