Skip to content

Commit cd5c1db

Browse files
committed
Add RawRangeEntry type
1 parent f4f503d commit cd5c1db

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ pub use crate::query::{
9898
CustomQuery, DecCoin, Delegation, DelegationResponse, DelegationRewardsResponse,
9999
DelegationTotalRewardsResponse, DelegatorReward, DelegatorValidatorsResponse,
100100
DelegatorWithdrawAddressResponse, DenomMetadataResponse, DistributionQuery, FullDelegation,
101-
GrpcQuery, IbcQuery, PortIdResponse, QueryRequest, RawRangeResponse, StakingQuery,
102-
SupplyResponse, Validator, ValidatorResponse, WasmQuery,
101+
GrpcQuery, IbcQuery, PortIdResponse, QueryRequest, RawRangeEntry, RawRangeResponse,
102+
StakingQuery, SupplyResponse, Validator, ValidatorResponse, WasmQuery,
103103
};
104104

105105
#[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
@@ -2828,6 +2828,8 @@ mod tests {
28282828
limit,
28292829
order,
28302830
} => {
2831+
use crate::RawRangeEntry;
2832+
28312833
let Ok(addr) = api.addr_validate(contract_addr) else {
28322834
return SystemResult::Err(SystemError::NoSuchContract {
28332835
addr: contract_addr.clone(),
@@ -2841,12 +2843,12 @@ mod tests {
28412843
*order,
28422844
)
28432845
.take(*limit as usize + 1) // take one more entry than limit
2844-
.map(|(key, value)| (Binary::new(key), Binary::new(value)))
2846+
.map(|(key, value)| RawRangeEntry::new(key, value))
28452847
.collect();
28462848

28472849
// if we have more than limit, there are more entries to fetch
28482850
let next_key = if data.len() > *limit as usize {
2849-
data.pop().map(|(k, _)| k)
2851+
data.pop().map(|RawRangeEntry { key, .. }| key)
28502852
} else {
28512853
None
28522854
};
@@ -2946,7 +2948,7 @@ mod tests {
29462948
match result {
29472949
SystemResult::Ok(ContractResult::Ok(value)) => assert_eq!(
29482950
value.as_slice(),
2949-
br#"{"data":[["dGhlIGtleQ==","dGhlIHZhbHVl"]],"next_key":null}"#
2951+
br#"{"data":[{"k":"dGhlIGtleQ==","v":"dGhlIHZhbHVl"}],"next_key":null}"#
29502952
),
29512953
res => panic!("Unexpected result: {res:?}"),
29522954
}

0 commit comments

Comments
 (0)