Skip to content

Commit c2d2671

Browse files
committed
Add RawRangeEntry type
1 parent a0253e1 commit c2d2671

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
@@ -95,7 +95,8 @@ pub use crate::query::{
9595
DelegationTotalRewardsResponse, DelegatorReward, DelegatorValidatorsResponse,
9696
DelegatorWithdrawAddressResponse, DenomMetadataResponse, DistributionQuery,
9797
FeeEnabledChannelResponse, FullDelegation, GrpcQuery, IbcQuery, PortIdResponse, QueryRequest,
98-
RawRangeResponse, StakingQuery, SupplyResponse, Validator, ValidatorResponse, WasmQuery,
98+
RawRangeEntry, RawRangeResponse, StakingQuery, SupplyResponse, Validator, ValidatorResponse,
99+
WasmQuery,
99100
};
100101

101102
#[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
@@ -148,17 +148,35 @@ impl QueryResponseType for CodeInfoResponse {}
148148
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
149149
pub struct RawRangeResponse {
150150
/// The key-value pairs
151-
pub data: Vec<(Binary, Binary)>,
151+
pub data: Vec<RawRangeEntry>,
152152
/// `None` if there are no more key-value pairs within the given key range.
153153
pub next_key: Option<Binary>,
154154
}
155155

156156
impl_response_constructor!(
157157
RawRangeResponse,
158-
data: Vec<(Binary, Binary)>,
158+
data: Vec<RawRangeEntry>,
159159
next_key: Option<Binary>
160160
);
161161

162+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
163+
pub struct RawRangeEntry {
164+
// keys renamed to reduce JSON size overhead
165+
#[serde(rename = "k")]
166+
pub key: Binary,
167+
#[serde(rename = "v")]
168+
pub value: Binary,
169+
}
170+
171+
impl RawRangeEntry {
172+
pub fn new(key: impl Into<Binary>, value: impl Into<Binary>) -> Self {
173+
RawRangeEntry {
174+
key: key.into(),
175+
value: value.into(),
176+
}
177+
}
178+
}
179+
162180
#[cfg(test)]
163181
mod tests {
164182
use super::*;
@@ -264,15 +282,15 @@ mod tests {
264282
fn raw_range_response_serialization() {
265283
let response = RawRangeResponse {
266284
data: vec![
267-
(Binary::from(b"key"), Binary::from(b"value")),
268-
(Binary::from(b"foo"), Binary::from(b"bar")),
285+
RawRangeEntry::new(b"key", b"value"),
286+
RawRangeEntry::new(b"foo", b"bar"),
269287
],
270288
next_key: Some(Binary::from(b"next")),
271289
};
272290
let json = to_json_binary(&response).unwrap();
273291
assert_eq!(
274292
String::from_utf8_lossy(&json),
275-
r#"{"data":[["a2V5","dmFsdWU="],["Zm9v","YmFy"]],"next_key":"bmV4dA=="}"#,
293+
r#"{"data":[{"k":"a2V5","v":"dmFsdWU="},{"k":"Zm9v","v":"YmFy"}],"next_key":"bmV4dA=="}"#,
276294
);
277295
}
278296
}

packages/std/src/testing/mock.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,8 @@ mod tests {
27822782
limit,
27832783
order,
27842784
} => {
2785+
use crate::RawRangeEntry;
2786+
27852787
let Ok(addr) = api.addr_validate(contract_addr) else {
27862788
return SystemResult::Err(SystemError::NoSuchContract {
27872789
addr: contract_addr.clone(),
@@ -2795,12 +2797,12 @@ mod tests {
27952797
*order,
27962798
)
27972799
.take(*limit as usize + 1) // take one more entry than limit
2798-
.map(|(key, value)| (Binary::new(key), Binary::new(value)))
2800+
.map(|(key, value)| RawRangeEntry::new(key, value))
27992801
.collect();
28002802

28012803
// if we have more than limit, there are more entries to fetch
28022804
let next_key = if data.len() > *limit as usize {
2803-
data.pop().map(|(k, _)| k)
2805+
data.pop().map(|RawRangeEntry { key, .. }| key)
28042806
} else {
28052807
None
28062808
};
@@ -2900,7 +2902,7 @@ mod tests {
29002902
match result {
29012903
SystemResult::Ok(ContractResult::Ok(value)) => assert_eq!(
29022904
value.as_slice(),
2903-
br#"{"data":[["dGhlIGtleQ==","dGhlIHZhbHVl"]],"next_key":null}"#
2905+
br#"{"data":[{"k":"dGhlIGtleQ==","v":"dGhlIHZhbHVl"}],"next_key":null}"#
29042906
),
29052907
res => panic!("Unexpected result: {res:?}"),
29062908
}

0 commit comments

Comments
 (0)