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

added support for validator-related queries in staking module #453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions cosmos-sdk-proto/src/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,61 @@ impl_name!(
"cosmos.staking.v1beta1",
"MsgDelegateResponse"
);
impl_name!(
cosmos::staking::v1beta1::CommissionRates,
"cosmos.staking.v1beta1",
"CommissionRates"
);
impl_name!(
cosmos::staking::v1beta1::Commission,
"cosmos.staking.v1beta1",
"Commission"
);
impl_name!(
cosmos::staking::v1beta1::Description,
"cosmos.staking.v1beta1",
"Description"
);
impl_name!(
cosmos::staking::v1beta1::HistoricalInfo,
"cosmos.staking.v1beta1",
"HistoricalInfo"
);
impl_name!(
cosmos::staking::v1beta1::QueryHistoricalInfoRequest,
"cosmos.staking.v1beta1",
"QueryHistoricalInfoRequest"
);
impl_name!(
cosmos::staking::v1beta1::QueryHistoricalInfoResponse,
"cosmos.staking.v1beta1",
"QueryHistoricalInfoResponse"
);
impl_name!(
cosmos::staking::v1beta1::QueryValidatorRequest,
"cosmos.staking.v1beta1",
"QueryValidatorRequest"
);
impl_name!(
cosmos::staking::v1beta1::QueryValidatorResponse,
"cosmos.staking.v1beta1",
"QueryValidatorResponse"
);
impl_name!(
cosmos::staking::v1beta1::QueryValidatorsRequest,
"cosmos.staking.v1beta1",
"QueryValidatorsRequest"
);
impl_name!(
cosmos::staking::v1beta1::QueryValidatorsResponse,
"cosmos.staking.v1beta1",
"QueryValidatorsResponse"
);
impl_name!(
cosmos::staking::v1beta1::Validator,
"cosmos.staking.v1beta1",
"Validator"
);

impl_name!(
cosmos::base::abci::v1beta1::MsgData,
Expand Down
22 changes: 20 additions & 2 deletions cosmrs/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@
//!
//! <https://docs.cosmos.network/v0.46/modules/staking/>

mod commission;
mod commission_rates;
mod description;
mod historical_info;
mod msg_begin_redelegate;
mod msg_delegate;
mod msg_undelegate;
mod query_historical_info_request;
mod query_historical_info_response;
mod query_validator_request;
mod query_validator_response;
mod query_validators_request;
mod query_validators_response;
mod validator;

pub use self::{
msg_begin_redelegate::MsgBeginRedelegate, msg_delegate::MsgDelegate,
msg_undelegate::MsgUndelegate,
commission::Commission, commission_rates::CommissionRates, description::Description,
historical_info::HistoricalInfo, msg_begin_redelegate::MsgBeginRedelegate,
msg_delegate::MsgDelegate, msg_undelegate::MsgUndelegate,
query_historical_info_request::QueryHistoricalInfoRequest,
query_historical_info_response::QueryHistoricalInfoResponse,
query_validator_request::QueryValidatorRequest,
query_validator_response::QueryValidatorResponse,
query_validators_request::QueryValidatorsRequest,
query_validators_response::QueryValidatorsResponse, validator::Validator,
};
49 changes: 49 additions & 0 deletions cosmrs/src/staking/commission.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::staking::CommissionRates;
use crate::{proto, ErrorReport, Result};
use cosmos_sdk_proto::Timestamp;
use tendermint::Time;

/// Commission defines commission parameters for a given validator.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Commission {
/// commission_rates defines the initial commission rates to be used for creating a validator.
pub commission_rates: Option<CommissionRates>,

/// update_time is the last time the commission rate was changed.
pub update_time: Option<Time>,
}

impl TryFrom<proto::cosmos::staking::v1beta1::Commission> for Commission {
type Error = ErrorReport;

fn try_from(proto: cosmos_sdk_proto::cosmos::staking::v1beta1::Commission) -> Result<Self> {
Ok(Commission {
commission_rates: proto.commission_rates.map(Into::into),
update_time: proto
.update_time
.map(|jailed_until| {
cosmos_sdk_proto::tendermint::google::protobuf::Timestamp {
seconds: jailed_until.seconds,
nanos: jailed_until.nanos,
}
.try_into()
})
.transpose()?,
})
}
}

impl From<Commission> for proto::cosmos::staking::v1beta1::Commission {
fn from(commission: Commission) -> Self {
proto::cosmos::staking::v1beta1::Commission {
commission_rates: commission.commission_rates.map(Into::into),
update_time: commission
.update_time
.map(cosmos_sdk_proto::tendermint::google::protobuf::Timestamp::from)
.map(|t| Timestamp {
seconds: t.seconds,
nanos: t.nanos,
}),
}
}
}
35 changes: 35 additions & 0 deletions cosmrs/src/staking/commission_rates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::proto;

/// CommissionRates defines the initial commission rates to be used for creating
/// a validator.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct CommissionRates {
/// rate is the commission rate charged to delegators, as a fraction.
pub rate: String,

/// max_rate defines the maximum commission rate which validator can ever charge, as a fraction.
pub max_rate: String,

/// max_change_rate defines the maximum daily increase of the validator commission, as a fraction.
pub max_change_rate: String,
}

impl From<proto::cosmos::staking::v1beta1::CommissionRates> for CommissionRates {
fn from(proto: cosmos_sdk_proto::cosmos::staking::v1beta1::CommissionRates) -> Self {
CommissionRates {
rate: proto.rate,
max_rate: proto.max_rate,
max_change_rate: proto.max_change_rate,
}
}
}

impl From<CommissionRates> for proto::cosmos::staking::v1beta1::CommissionRates {
fn from(commission_rates: CommissionRates) -> Self {
proto::cosmos::staking::v1beta1::CommissionRates {
rate: commission_rates.rate,
max_rate: commission_rates.max_rate,
max_change_rate: commission_rates.max_change_rate,
}
}
}
44 changes: 44 additions & 0 deletions cosmrs/src/staking/description.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::proto;

/// Description defines a validator description.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Description {
/// moniker defines a human-readable name for the validator.
pub moniker: String,

/// identity defines an optional identity signature (ex. UPort or Keybase).
pub identity: String,

/// website defines an optional website link.
pub website: String,

/// security_contact defines an optional email for security contact.
pub security_contact: String,

/// details define other optional details.
pub details: String,
}

impl From<proto::cosmos::staking::v1beta1::Description> for Description {
fn from(proto: cosmos_sdk_proto::cosmos::staking::v1beta1::Description) -> Self {
Description {
moniker: proto.moniker,
identity: proto.identity,
website: proto.website,
security_contact: proto.security_contact,
details: proto.details,
}
}
}

impl From<Description> for proto::cosmos::staking::v1beta1::Description {
fn from(description: Description) -> Self {
proto::cosmos::staking::v1beta1::Description {
moniker: description.moniker,
identity: description.identity,
website: description.website,
security_contact: description.security_contact,
details: description.details,
}
}
}
40 changes: 40 additions & 0 deletions cosmrs/src/staking/historical_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::staking::Validator;
use crate::{proto, ErrorReport, Result};
use tendermint::block::Header;

/// HistoricalInfo contains header and validator information for a given block.
/// It is stored as part of staking module's state, which persists the `n` most
/// recent HistoricalInfo
/// (`n` is set by the staking module's `historical_entries` parameter).
#[derive(Clone, Debug, PartialEq)]
pub struct HistoricalInfo {
/// Header of the block
pub header: Option<Header>,

/// The validator set at the block
pub valset: Vec<Validator>,
}

impl TryFrom<proto::cosmos::staking::v1beta1::HistoricalInfo> for HistoricalInfo {
type Error = ErrorReport;

fn try_from(proto: cosmos_sdk_proto::cosmos::staking::v1beta1::HistoricalInfo) -> Result<Self> {
Ok(HistoricalInfo {
header: proto.header.map(TryInto::try_into).transpose()?,
valset: proto
.valset
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_>>()?,
})
}
}

impl From<HistoricalInfo> for proto::cosmos::staking::v1beta1::HistoricalInfo {
fn from(historical_info: HistoricalInfo) -> Self {
proto::cosmos::staking::v1beta1::HistoricalInfo {
header: historical_info.header.map(Into::into),
valset: historical_info.valset.into_iter().map(Into::into).collect(),
}
}
}
29 changes: 29 additions & 0 deletions cosmrs/src/staking/query_historical_info_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::proto;

/// QueryHistoricalInfoRequest is request type for the Query/HistoricalInfo RPC
/// method.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct QueryHistoricalInfoRequest {
/// height defines at which height to query the historical info.
pub height: i64,
}

impl From<proto::cosmos::staking::v1beta1::QueryHistoricalInfoRequest>
for QueryHistoricalInfoRequest
{
fn from(proto: cosmos_sdk_proto::cosmos::staking::v1beta1::QueryHistoricalInfoRequest) -> Self {
QueryHistoricalInfoRequest {
height: proto.height,
}
}
}

impl From<QueryHistoricalInfoRequest>
for proto::cosmos::staking::v1beta1::QueryHistoricalInfoRequest
{
fn from(query_historical_info_request: QueryHistoricalInfoRequest) -> Self {
proto::cosmos::staking::v1beta1::QueryHistoricalInfoRequest {
height: query_historical_info_request.height,
}
}
}
34 changes: 34 additions & 0 deletions cosmrs/src/staking/query_historical_info_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::staking::HistoricalInfo;
use crate::{proto, ErrorReport, Result};

/// QueryHistoricalInfoResponse is response type for the Query/HistoricalInfo RPC
/// method.
#[derive(Clone, Debug, PartialEq)]
pub struct QueryHistoricalInfoResponse {
/// hist defines the historical info at the given height.
pub hist: Option<HistoricalInfo>,
}

impl TryFrom<proto::cosmos::staking::v1beta1::QueryHistoricalInfoResponse>
for QueryHistoricalInfoResponse
{
type Error = ErrorReport;

fn try_from(
proto: cosmos_sdk_proto::cosmos::staking::v1beta1::QueryHistoricalInfoResponse,
) -> Result<Self> {
Ok(QueryHistoricalInfoResponse {
hist: proto.hist.map(TryInto::try_into).transpose()?,
})
}
}

impl From<QueryHistoricalInfoResponse>
for proto::cosmos::staking::v1beta1::QueryHistoricalInfoResponse
{
fn from(query_historical_info_response: QueryHistoricalInfoResponse) -> Self {
proto::cosmos::staking::v1beta1::QueryHistoricalInfoResponse {
hist: query_historical_info_response.hist.map(Into::into),
}
}
}
28 changes: 28 additions & 0 deletions cosmrs/src/staking/query_validator_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::{proto, AccountId, ErrorReport, Result};

/// QueryValidatorRequest is response type for the Query/Validator RPC method
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct QueryValidatorRequest {
/// validator_addr defines the validator address to query for.
pub validator_addr: AccountId,
}

impl TryFrom<proto::cosmos::staking::v1beta1::QueryValidatorRequest> for QueryValidatorRequest {
type Error = ErrorReport;

fn try_from(
proto: cosmos_sdk_proto::cosmos::staking::v1beta1::QueryValidatorRequest,
) -> Result<Self> {
Ok(QueryValidatorRequest {
validator_addr: proto.validator_addr.parse()?,
})
}
}

impl From<QueryValidatorRequest> for proto::cosmos::staking::v1beta1::QueryValidatorRequest {
fn from(query_validator_request: QueryValidatorRequest) -> Self {
proto::cosmos::staking::v1beta1::QueryValidatorRequest {
validator_addr: query_validator_request.validator_addr.to_string(),
}
}
}
29 changes: 29 additions & 0 deletions cosmrs/src/staking/query_validator_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::staking::Validator;
use crate::{proto, ErrorReport, Result};

/// QueryValidatorResponse is response type for the Query/Validator RPC method
#[derive(Clone, Debug, PartialEq)]
pub struct QueryValidatorResponse {
/// validator defines the validator info.
pub validator: Option<Validator>,
}

impl TryFrom<proto::cosmos::staking::v1beta1::QueryValidatorResponse> for QueryValidatorResponse {
type Error = ErrorReport;

fn try_from(
proto: cosmos_sdk_proto::cosmos::staking::v1beta1::QueryValidatorResponse,
) -> Result<Self> {
Ok(QueryValidatorResponse {
validator: proto.validator.map(TryInto::try_into).transpose()?,
})
}
}

impl From<QueryValidatorResponse> for proto::cosmos::staking::v1beta1::QueryValidatorResponse {
fn from(query_validator_response: QueryValidatorResponse) -> Self {
proto::cosmos::staking::v1beta1::QueryValidatorResponse {
validator: query_validator_response.validator.map(Into::into),
}
}
}
Loading
Loading