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 slashing module #452

Merged
merged 1 commit into from
Dec 5, 2023
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
2 changes: 1 addition & 1 deletion cosmos-sdk-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod traits;
mod type_names;

pub use prost;
pub use prost_types::Any;
pub use prost_types::{Any, Timestamp};
pub use tendermint_proto as tendermint;

/// The version (commit hash) of the Cosmos SDK used when generating this library.
Expand Down
71 changes: 71 additions & 0 deletions cosmos-sdk-proto/src/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,77 @@ impl_name!(
"AllowedMsgAllowance"
);

impl_name!(
cosmos::slashing::v1beta1::GenesisState,
"cosmos.slashing.v1beta1",
"MissedBlocks"
);
impl_name!(
cosmos::slashing::v1beta1::MissedBlock,
"cosmos.slashing.v1beta1",
"MissedBlock"
);
impl_name!(
cosmos::slashing::v1beta1::MsgUnjail,
"cosmos.slashing.v1beta1",
"MsgUnjail"
);
impl_name!(
cosmos::slashing::v1beta1::MsgUnjailResponse,
"cosmos.slashing.v1beta1",
"MsgUnjailResponse"
);
impl_name!(
cosmos::slashing::v1beta1::Params,
"cosmos.slashing.v1beta1",
"Params"
);
impl_name!(
cosmos::slashing::v1beta1::QueryParamsRequest,
"cosmos.slashing.v1beta1",
"QueryParamsRequest"
);
impl_name!(
cosmos::slashing::v1beta1::QueryParamsResponse,
"cosmos.slashing.v1beta1",
"QueryParamsResponse"
);
impl_name!(
cosmos::slashing::v1beta1::QuerySigningInfoRequest,
"cosmos.slashing.v1beta1",
"QuerySigningInfoRequest"
);
impl_name!(
cosmos::slashing::v1beta1::QuerySigningInfoResponse,
"cosmos.slashing.v1beta1",
"QuerySigningInfoResponse"
);
impl_name!(
cosmos::slashing::v1beta1::QuerySigningInfosRequest,
"cosmos.slashing.v1beta1",
"QuerySigningInfosRequest"
);
impl_name!(
cosmos::slashing::v1beta1::QuerySigningInfosResponse,
"cosmos.slashing.v1beta1",
"QuerySigningInfosResponse"
);
impl_name!(
cosmos::slashing::v1beta1::SigningInfo,
"cosmos.slashing.v1beta1",
"SigningInfo"
);
impl_name!(
cosmos::slashing::v1beta1::ValidatorMissedBlocks,
"cosmos.slashing.v1beta1",
"ValidatorMissedBlocks"
);
impl_name!(
cosmos::slashing::v1beta1::ValidatorSigningInfo,
"cosmos.slashing.v1beta1",
"ValidatorSigningInfo"
);

impl_name!(
cosmos::staking::v1beta1::MsgEditValidatorResponse,
"cosmos.staking.v1beta1",
Expand Down
2 changes: 2 additions & 0 deletions cosmrs/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod account_id;
mod coin;
mod denom;

pub mod query;

pub use self::{account_id::AccountId, coin::Coin, denom::Denom};

/// Amounts.
Expand Down
3 changes: 3 additions & 0 deletions cosmrs/src/base/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod pagination;

pub use pagination::{PageRequest, PageResponse};
85 changes: 85 additions & 0 deletions cosmrs/src/base/query/pagination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::proto;

/// PageRequest is to be embedded in gRPC request messages for efficient
/// pagination.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PageRequest {
/// key is a value returned in PageResponse.next_key to begin
/// querying the next page most efficiently. Only one of offset or key
/// should be set.
pub key: Vec<u8>,

/// offset is a numeric offset that can be used when key is unavailable.
/// It is less efficient than using key. Only one of offset or key should
/// be set.
pub offset: u64,
/// limit is the total number of results to be returned in the result page.
/// If left empty it will default to a value to be set by each app.
pub limit: u64,
/// count_total is set to true to indicate that the result set should include
/// a count of the total number of items available for pagination in UIs.
/// count_total is only respected when offset is used. It is ignored when key
/// is set.
pub count_total: bool,

/// reverse is set to true if results are to be returned in the descending order.
///
/// Since: cosmos-sdk 0.43
pub reverse: bool,
}

impl From<proto::cosmos::base::query::v1beta1::PageRequest> for PageRequest {
fn from(proto: cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest) -> Self {
PageRequest {
key: proto.key,
offset: proto.offset,
limit: proto.limit,
count_total: proto.count_total,
reverse: proto.reverse,
}
}
}

impl From<PageRequest> for proto::cosmos::base::query::v1beta1::PageRequest {
fn from(page_request: PageRequest) -> Self {
proto::cosmos::base::query::v1beta1::PageRequest {
key: page_request.key,
offset: page_request.offset,
limit: page_request.limit,
count_total: page_request.count_total,
reverse: page_request.reverse,
}
}
}

/// PageResponse is to be embedded in gRPC response messages where the
/// corresponding request message has used PageRequest.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PageResponse {
/// next_key is the key to be passed to PageRequest.key to
/// query the next page most efficiently. It will be empty if
/// there are no more results.
pub next_key: Vec<u8>,

/// total is total number of results available if PageRequest.count_total
/// was set, its value is undefined otherwise
pub total: u64,
}

impl From<proto::cosmos::base::query::v1beta1::PageResponse> for PageResponse {
fn from(proto: cosmos_sdk_proto::cosmos::base::query::v1beta1::PageResponse) -> Self {
PageResponse {
next_key: proto.next_key,
total: proto.total,
}
}
}

impl From<PageResponse> for proto::cosmos::base::query::v1beta1::PageResponse {
fn from(page_response: PageResponse) -> Self {
proto::cosmos::base::query::v1beta1::PageResponse {
next_key: page_response.next_key,
total: page_response.total,
}
}
}
1 change: 1 addition & 0 deletions cosmrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod bank;
pub mod crypto;
pub mod distribution;
pub mod feegrant;
pub mod slashing;
pub mod staking;
pub mod tx;
pub mod vesting;
Expand Down
33 changes: 33 additions & 0 deletions cosmrs/src/slashing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Slashing module support
//!
//! <https://docs.cosmos.network/v0.46/modules/slashing/>

mod genesis_state;
mod missed_block;
mod msg_unjail;
mod params;
mod query_params_request;
mod query_params_response;
mod query_signing_info_request;
mod query_signing_info_response;
mod query_signing_infos_request;
mod query_signing_infos_response;
mod signing_info;
mod validator_missed_blocks;
mod validator_signing_info;

pub use self::{
genesis_state::GenesisState,
missed_block::MissedBlock,
msg_unjail::{MsgUnjail, MsgUnjailResponse},
params::Params,
query_params_request::QueryParamsRequest,
query_params_response::QueryParamsResponse,
query_signing_info_request::QuerySigningInfoRequest,
query_signing_info_response::QuerySigningInfoResponse,
query_signing_infos_request::QuerySigningInfosRequest,
query_signing_infos_response::QuerySigningInfosResponse,
signing_info::SigningInfo,
validator_missed_blocks::ValidatorMissedBlocks,
validator_signing_info::ValidatorSigningInfo,
};
51 changes: 51 additions & 0 deletions cosmrs/src/slashing/genesis_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::slashing::{Params, SigningInfo, ValidatorMissedBlocks};
use crate::{proto, ErrorReport, Result};

/// GenesisState defines the slashing module's genesis state.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GenesisState {
/// params defines all the paramaters of related to deposit.
pub params: Option<Params>,

/// signing_infos represents a map between validator addresses and their
/// signing infos.
pub signing_infos: Vec<SigningInfo>,

/// missed_blocks represents a map between validator addresses and their
/// missed blocks.
pub missed_blocks: Vec<ValidatorMissedBlocks>,
}

impl TryFrom<proto::cosmos::slashing::v1beta1::GenesisState> for GenesisState {
type Error = ErrorReport;

fn try_from(proto: cosmos_sdk_proto::cosmos::slashing::v1beta1::GenesisState) -> Result<Self> {
Ok(GenesisState {
params: proto.params.map(TryInto::try_into).transpose()?,
signing_infos: proto
.signing_infos
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_>>()?,
missed_blocks: proto.missed_blocks.into_iter().map(Into::into).collect(),
})
}
}

impl From<GenesisState> for cosmos_sdk_proto::cosmos::slashing::v1beta1::GenesisState {
fn from(genesis_state: GenesisState) -> Self {
cosmos_sdk_proto::cosmos::slashing::v1beta1::GenesisState {
params: genesis_state.params.map(Into::into),
signing_infos: genesis_state
.signing_infos
.into_iter()
.map(Into::into)
.collect(),
missed_blocks: genesis_state
.missed_blocks
.into_iter()
.map(Into::into)
.collect(),
}
}
}
29 changes: 29 additions & 0 deletions cosmrs/src/slashing/missed_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::proto;

/// MissedBlock contains height and missed status as boolean.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MissedBlock {
/// index is the height at which the block was missed.
pub index: i64,

/// missed is the missed status.
pub missed: bool,
}

impl From<proto::cosmos::slashing::v1beta1::MissedBlock> for MissedBlock {
fn from(proto: cosmos_sdk_proto::cosmos::slashing::v1beta1::MissedBlock) -> MissedBlock {
MissedBlock {
index: proto.index,
missed: proto.missed,
}
}
}

impl From<MissedBlock> for cosmos_sdk_proto::cosmos::slashing::v1beta1::MissedBlock {
fn from(missed_block: MissedBlock) -> Self {
cosmos_sdk_proto::cosmos::slashing::v1beta1::MissedBlock {
index: missed_block.index,
missed: missed_block.missed,
}
}
}
54 changes: 54 additions & 0 deletions cosmrs/src/slashing/msg_unjail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::{proto, tx::Msg, AccountId, ErrorReport, Result};

/// MsgUnjail defines the Msg/Unjail request type
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct MsgUnjail {
/// Address of the validator to unjail.
pub validator_addr: AccountId,
}

impl Msg for MsgUnjail {
type Proto = proto::cosmos::slashing::v1beta1::MsgUnjail;
}

impl TryFrom<proto::cosmos::slashing::v1beta1::MsgUnjail> for MsgUnjail {
type Error = ErrorReport;

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

impl From<MsgUnjail> for cosmos_sdk_proto::cosmos::slashing::v1beta1::MsgUnjail {
fn from(msg_unjail: MsgUnjail) -> Self {
cosmos_sdk_proto::cosmos::slashing::v1beta1::MsgUnjail {
validator_addr: msg_unjail.validator_addr.to_string(),
}
}
}

/// MsgUnjailResponse defines the Msg/Unjail response type
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct MsgUnjailResponse {}

impl Msg for MsgUnjailResponse {
type Proto = proto::cosmos::slashing::v1beta1::MsgUnjailResponse;
}

impl TryFrom<proto::cosmos::slashing::v1beta1::MsgUnjailResponse> for MsgUnjailResponse {
type Error = ErrorReport;

fn try_from(
_proto: cosmos_sdk_proto::cosmos::slashing::v1beta1::MsgUnjailResponse,
) -> Result<Self> {
Ok(MsgUnjailResponse {})
}
}

impl From<MsgUnjailResponse> for cosmos_sdk_proto::cosmos::slashing::v1beta1::MsgUnjailResponse {
fn from(_: MsgUnjailResponse) -> Self {
cosmos_sdk_proto::cosmos::slashing::v1beta1::MsgUnjailResponse {}
}
}
Loading
Loading