Skip to content

Commit 89f31f7

Browse files
committed
Add RawRangeEntry type
1 parent f71fbc9 commit 89f31f7

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
type RawRangeResponse struct {
22
// The key-value pairs
3-
Data Array[Array[[]byte]] `json:"data"`
3+
Data Array[RawRangeEntry] `json:"data"`
44
// `None` if there are no more key-value pairs within the given key range.
55
NextKey *[]byte `json:"next_key,omitempty"`
6+
}
7+
type RawRangeEntry struct {
8+
K []byte `json:"k"`
9+
V []byte `json:"v"`
610
}

packages/std/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ pub use crate::query::{
9999
DelegationTotalRewardsResponse, DelegatorReward, DelegatorValidatorsResponse,
100100
DelegatorWithdrawAddressResponse, DenomMetadataResponse, DistributionQuery,
101101
FeeEnabledChannelResponse, FullDelegation, GrpcQuery, IbcQuery, PortIdResponse, QueryRequest,
102-
RawRangeResponse, StakingQuery, SupplyResponse, Validator, ValidatorResponse, WasmQuery,
102+
RawRangeEntry, RawRangeResponse, StakingQuery, SupplyResponse, Validator, ValidatorResponse,
103+
WasmQuery,
103104
};
104105

105106
#[cfg(all(feature = "stargate", feature = "cosmwasm_1_2"))]

packages/std/src/query/wasm.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,35 @@ impl QueryResponseType for CodeInfoResponse {}
150150
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
151151
pub struct RawRangeResponse {
152152
/// The key-value pairs
153-
pub data: Vec<(Binary, Binary)>,
153+
pub data: Vec<RawRangeEntry>,
154154
/// `None` if there are no more key-value pairs within the given key range.
155155
pub next_key: Option<Binary>,
156156
}
157157

158158
impl_hidden_constructor!(
159159
RawRangeResponse,
160-
data: Vec<(Binary, Binary)>,
160+
data: Vec<RawRangeEntry>,
161161
next_key: Option<Binary>
162162
);
163163

164+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
165+
pub struct RawRangeEntry {
166+
// keys renamed to reduce JSON size overhead
167+
#[serde(rename = "k")]
168+
pub key: Binary,
169+
#[serde(rename = "v")]
170+
pub value: Binary,
171+
}
172+
173+
impl RawRangeEntry {
174+
pub fn new(key: impl Into<Binary>, value: impl Into<Binary>) -> Self {
175+
RawRangeEntry {
176+
key: key.into(),
177+
value: value.into(),
178+
}
179+
}
180+
}
181+
164182
#[cfg(test)]
165183
mod tests {
166184
use super::*;
@@ -266,15 +284,15 @@ mod tests {
266284
fn raw_range_response_serialization() {
267285
let response = RawRangeResponse {
268286
data: vec![
269-
(Binary::from(b"key"), Binary::from(b"value")),
270-
(Binary::from(b"foo"), Binary::from(b"bar")),
287+
RawRangeEntry::new(b"key", b"value"),
288+
RawRangeEntry::new(b"foo", b"bar"),
271289
],
272290
next_key: Some(Binary::from(b"next")),
273291
};
274292
let json = to_json_binary(&response).unwrap();
275293
assert_eq!(
276294
String::from_utf8_lossy(&json),
277-
r#"{"data":[["a2V5","dmFsdWU="],["Zm9v","YmFy"]],"next_key":"bmV4dA=="}"#,
295+
r#"{"data":[{"k":"a2V5","v":"dmFsdWU="},{"k":"Zm9v","v":"YmFy"}],"next_key":"bmV4dA=="}"#,
278296
);
279297
}
280298
}

packages/std/src/testing/mock.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,8 @@ mod tests {
28342834
limit,
28352835
order,
28362836
} => {
2837+
use crate::RawRangeEntry;
2838+
28372839
let Ok(addr) = api.addr_validate(contract_addr) else {
28382840
return SystemResult::Err(SystemError::NoSuchContract {
28392841
addr: contract_addr.clone(),
@@ -2847,12 +2849,12 @@ mod tests {
28472849
*order,
28482850
)
28492851
.take(*limit as usize + 1) // take one more entry than limit
2850-
.map(|(key, value)| (Binary::new(key), Binary::new(value)))
2852+
.map(|(key, value)| RawRangeEntry::new(key, value))
28512853
.collect();
28522854

28532855
// if we have more than limit, there are more entries to fetch
28542856
let next_key = if data.len() > *limit as usize {
2855-
data.pop().map(|(k, _)| k)
2857+
data.pop().map(|RawRangeEntry { key, .. }| key)
28562858
} else {
28572859
None
28582860
};
@@ -2952,7 +2954,7 @@ mod tests {
29522954
match result {
29532955
SystemResult::Ok(ContractResult::Ok(value)) => assert_eq!(
29542956
value.as_slice(),
2955-
br#"{"data":[["dGhlIGtleQ==","dGhlIHZhbHVl"]],"next_key":null}"#
2957+
br#"{"data":[{"k":"dGhlIGtleQ==","v":"dGhlIHZhbHVl"}],"next_key":null}"#
29562958
),
29572959
res => panic!("Unexpected result: {res:?}"),
29582960
}

0 commit comments

Comments
 (0)