Skip to content

Commit fa33867

Browse files
authored
Merge pull request #2501 from CosmWasm/co/separate-validator-types
Split Validator type for better extensibility in the future
2 parents b0114f2 + 66f8001 commit fa33867

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ and this project adheres to
9494
- cosmwasm-vm: Enable partial reference-type support, enabling contracts
9595
compiled with Rust 1.82 or newer to be stored. ([#2473])
9696
- cosmwasm-std: Removed IBC fees ([#2479])
97+
- cosmwasm-std: Split up `Validator` type into `Validator` and
98+
`ValidatorMetadata` to allow adding more fields to `ValidatorResponse` in the
99+
future. ([#2501])
97100

98101
## Fixed
99102

@@ -141,6 +144,7 @@ and this project adheres to
141144
[#2479]: https://github.com/CosmWasm/cosmwasm/pull/2479
142145
[#2480]: https://github.com/CosmWasm/cosmwasm/pull/2480
143146
[#2484]: https://github.com/CosmWasm/cosmwasm/pull/2484
147+
[#2501]: https://github.com/CosmWasm/cosmwasm/pull/2501
144148

145149
## [2.2.0] - 2024-12-17
146150

packages/go-gen/tests/cosmwasm_std__AllValidatorsResponse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// AllValidatorsResponse is the expected response to AllValidatorsQuery
22
type AllValidatorsResponse struct {
3-
Validators Array[Validator] `json:"validators"`
3+
Validators Array[ValidatorMetadata] `json:"validators"`
44
}
55

6-
type Validator struct {
6+
type ValidatorMetadata struct {
77
Address string `json:"address"`
88
// decimal string, eg "0.02"
99
Commission string `json:"commission"`

packages/std/src/query/staking.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ impl FullDelegation {
152152
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
153153
#[non_exhaustive]
154154
pub struct AllValidatorsResponse {
155-
pub validators: Vec<Validator>,
155+
pub validators: Vec<ValidatorMetadata>,
156156
}
157157

158158
impl QueryResponseType for AllValidatorsResponse {}
159159

160-
impl_hidden_constructor!(AllValidatorsResponse, validators: Vec<Validator>);
160+
impl_hidden_constructor!(AllValidatorsResponse, validators: Vec<ValidatorMetadata>);
161161

162162
/// The data format returned from StakingRequest::Validator query
163163
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
@@ -173,7 +173,7 @@ impl_hidden_constructor!(ValidatorResponse, validator: Option<Validator>);
173173
/// Instances are created in the querier.
174174
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
175175
#[non_exhaustive]
176-
pub struct Validator {
176+
pub struct ValidatorMetadata {
177177
/// The operator address of the validator (e.g. cosmosvaloper1...).
178178
/// See https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L95-L96
179179
/// for more information.
@@ -188,13 +188,30 @@ pub struct Validator {
188188
}
189189

190190
impl_hidden_constructor!(
191-
Validator,
191+
ValidatorMetadata,
192192
address: String,
193193
commission: Decimal,
194194
max_commission: Decimal,
195195
max_change_rate: Decimal
196196
);
197197

198+
/// Instances are created in the querier.
199+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
200+
#[non_exhaustive]
201+
pub struct Validator {
202+
/// The operator address of the validator (e.g. cosmosvaloper1...).
203+
/// See https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L95-L96
204+
/// for more information.
205+
///
206+
/// This uses `String` instead of `Addr` since the bech32 address prefix is different from
207+
/// the ones that regular user accounts use.
208+
pub address: String,
209+
pub commission: Decimal,
210+
pub max_commission: Decimal,
211+
/// The maximum daily increase of the commission
212+
pub max_change_rate: Decimal,
213+
}
214+
198215
impl Validator {
199216
/// Creates a new validator.
200217
///
@@ -214,3 +231,24 @@ impl Validator {
214231
}
215232
}
216233
}
234+
235+
impl_hidden_constructor!(
236+
Validator,
237+
address: String,
238+
commission: Decimal,
239+
max_commission: Decimal,
240+
max_change_rate: Decimal
241+
);
242+
243+
// Validator should contain all data that ValidatorMetadata has + maybe some additional data
244+
// that is expensive to query, so we can convert ValidatorMetadata to Validator easily.
245+
impl From<Validator> for ValidatorMetadata {
246+
fn from(validator: Validator) -> Self {
247+
ValidatorMetadata {
248+
address: validator.address,
249+
commission: validator.commission,
250+
max_commission: validator.max_commission,
251+
max_change_rate: validator.max_change_rate,
252+
}
253+
}
254+
}

packages/std/src/testing/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ impl StakingQuerier {
12041204
}
12051205
StakingQuery::AllValidators {} => {
12061206
let res = AllValidatorsResponse {
1207-
validators: self.validators.clone(),
1207+
validators: self.validators.iter().cloned().map(Into::into).collect(),
12081208
};
12091209
to_json_binary(&res).into()
12101210
}
@@ -2508,7 +2508,7 @@ mod tests {
25082508
.unwrap()
25092509
.unwrap();
25102510
let vals: AllValidatorsResponse = from_json(raw).unwrap();
2511-
assert_eq!(vals.validators, vec![val1, val2]);
2511+
assert_eq!(vals.validators, vec![val1.into(), val2.into()]);
25122512
}
25132513

25142514
#[cfg(feature = "staking")]

packages/std/src/traits.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use crate::query::SupplyResponse;
1414
#[cfg(feature = "staking")]
1515
use crate::query::{
1616
AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation,
17-
DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse,
17+
DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorMetadata,
18+
ValidatorResponse,
1819
};
1920
#[cfg(feature = "cosmwasm_1_3")]
2021
use crate::query::{
@@ -593,7 +594,7 @@ impl<'a, C: CustomQuery> QuerierWrapper<'a, C> {
593594
}
594595

595596
#[cfg(feature = "staking")]
596-
pub fn query_all_validators(&self) -> StdResult<Vec<Validator>> {
597+
pub fn query_all_validators(&self) -> StdResult<Vec<ValidatorMetadata>> {
597598
let request = StakingQuery::AllValidators {}.into();
598599
let res: AllValidatorsResponse = self.query(&request)?;
599600
Ok(res.validators)

0 commit comments

Comments
 (0)