Skip to content

Commit d0cfeb7

Browse files
authored
Depolama (#3919)
2 parents 7d2ce51 + 605169b commit d0cfeb7

File tree

23 files changed

+1488
-349
lines changed

23 files changed

+1488
-349
lines changed

Cargo.lock

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ members = [
194194
"cosmwasm/cw20-base",
195195
"lib/scroll-types",
196196
"lib/fork-schedules",
197+
"lib/depolama",
197198
]
198199

199200
[workspace.package]
@@ -227,6 +228,8 @@ beacon-api = { path = "lib/beacon-api", default-features = false }
227228
beacon-api-types = { path = "lib/beacon-api-types", default-features = false }
228229
fork-schedules = { path = "lib/fork-schedules", default-features = false }
229230

231+
depolama = { path = "lib/depolama", default-features = false }
232+
230233
state-lens-light-client-types = { path = "lib/state-lens-light-client-types", default-features = false }
231234

232235
arbitrum-light-client-types = { path = "lib/arbitrum-light-client-types", default-features = false }

cosmwasm/cosmwasm.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
private_key = "0xaa820fa947beb242032a41b6dc9a8b9c37d8f5fbcda0966b1ec80335b10a7d6f";
3636
gas_config = {
3737
gas_denom = "muno";
38-
gas_multiplier = "1.5";
39-
gas_price = "1.5";
40-
max_gas = 10000000;
38+
gas_multiplier = "1.1";
39+
gas_price = "1.0";
40+
max_gas = 200000000;
4141
};
4242
ucs03_type = "cw20";
4343
bech32_prefix = "union";
@@ -55,7 +55,7 @@
5555
gas_denom = "muno";
5656
gas_multiplier = "1.1";
5757
gas_price = "1.0";
58-
max_gas = 10000000;
58+
max_gas = 200000000;
5959
};
6060
apps = {
6161
ucs03 = ucs03-configs.cw20;

cosmwasm/ibc-union/core/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ library = []
1919

2020
[dependencies]
2121
alloy = { workspace = true, features = ["sol-types"] }
22-
bincode = { workspace = true, features = ["alloc", "derive"], optional = true }
22+
bincode = { workspace = true, features = ["alloc", "derive"] }
2323
cosmwasm-schema = { workspace = true }
24-
cosmwasm-std = { workspace = true, features = ["abort"] }
25-
cw-storage-plus = { workspace = true }
24+
cosmwasm-std = { workspace = true, features = ["abort", "iterator"] }
25+
depolama = { workspace = true }
2626
ethabi = { workspace = true }
2727
hex = { workspace = true }
2828
ibc-union-msg = { workspace = true }
29-
ibc-union-spec = { workspace = true, features = ["ethabi", "serde"] }
29+
ibc-union-spec = { workspace = true, features = ["ethabi", "serde", "bincode"] }
3030
serde = { workspace = true, features = ["derive"] }
31-
serde_json = { workspace = true }
31+
serde-json-wasm = "1.0"
3232
strum = { version = "0.26.3", features = ["derive"] }
3333
thiserror = { workspace = true }
34-
unionlabs = { workspace = true, features = ["ethabi"] }
34+
unionlabs = { workspace = true, features = ["ethabi", "bincode"] }
3535
unionlabs-cosmwasm-upgradable = { workspace = true }

cosmwasm/ibc-union/core/light-client-interface/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ workspace = true
1414
[dependencies]
1515
cosmwasm-schema = { workspace = true }
1616
cosmwasm-std = { workspace = true }
17-
cw-storage-plus = { workspace = true }
17+
depolama = { workspace = true }
18+
ibc-union = { workspace = true, features = ["library"] }
1819
ibc-union-msg = { workspace = true }
1920
macros = { workspace = true }
2021
schemars = { workspace = true }
@@ -27,4 +28,4 @@ unionlabs-cosmwasm-upgradable = { workspace = true }
2728
[features]
2829
default = ["dummy-instantiate"]
2930

30-
dummy-instantiate = [] # exports a panicking instantiate entrypoint
31+
dummy-instantiate = [] # exports a panicking instantiate entrypoint TODO: Remove? Unused

cosmwasm/ibc-union/core/light-client-interface/src/lib.rs

Lines changed: 47 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,27 @@
33
use core::fmt::Debug;
44

55
use cosmwasm_std::{
6-
from_json, to_json_binary, Addr, Binary, Deps, DepsMut, Env, QuerierWrapper, Response, StdError,
6+
to_json_binary, Addr, Binary, Deps, DepsMut, Env, QuerierWrapper, Response, StdError,
77
};
8-
use cw_storage_plus::{Item, Map};
8+
use depolama::{QuerierExt, StorageExt, Store};
9+
use ibc_union::state::{ClientConsensusStates, ClientImpls, ClientStates, ClientStore, QueryStore};
910
use ibc_union_msg::lightclient::{
1011
MisbehaviourResponse, QueryMsg, Status, StorageWrites, UpdateStateResponse,
1112
VerifyCreationResponse, VerifyCreationResponseEvent,
1213
};
1314
use msg::InitMsg;
14-
use state::IBC_HOST;
1515
use unionlabs::{
1616
encoding::{Decode, DecodeAs, DecodeErrorOf, Encode, EncodeAs, Encoding, EthAbi},
17-
primitives::{encoding::Base64, Bytes},
17+
primitives::Bytes,
1818
ErrorReporter,
1919
};
2020
use unionlabs_cosmwasm_upgradable::UpgradeError;
2121

22+
use crate::state::IbcHost;
23+
2224
pub mod msg;
2325
pub mod state;
2426

25-
pub const CLIENT_STORAGE_PREFIX: &str = "client/";
26-
27-
// These are only used for `key` calculation. We don't want this crate to depend on `ibc-union`.
28-
pub const CLIENT_STATES: Map<u32, Binary> = Map::new("client_states");
29-
pub const CLIENT_CONSENSUS_STATES: Map<(u32, u64), Binary> = Map::new("client_consensus_states");
30-
const CLIENT_IMPLS: Map<u32, Addr> = Map::new("client_impls");
31-
const QUERY_STORE: Item<Binary> = Item::new("query_store");
32-
3327
// TODO: Add #[source] to all variants
3428
#[derive(macros::Debug, thiserror::Error)]
3529
#[debug(bound())]
@@ -112,16 +106,11 @@ impl<'a, T: IbcClient> IbcClientCtx<'a, T> {
112106
)
113107
}
114108

115-
pub fn read_self_storage<V: Decode<T::Encoding>>(
116-
&self,
117-
key: &[u8],
118-
) -> Result<V, IbcClientError<T>> {
119-
read_storage(
120-
self.deps.querier.into_empty(),
121-
&self.ibc_host,
122-
self.client_id,
123-
key,
124-
)
109+
pub fn read_self_storage<S: Store>(&self, key: S::Key) -> Result<S::Value, IbcClientError<T>> {
110+
self.deps
111+
.querier
112+
.read::<ClientStore<S>>(&self.ibc_host, &(self.client_id, key))
113+
.map_err(Into::into)
125114
}
126115

127116
pub fn read_client_state<Client: IbcClient>(
@@ -173,15 +162,13 @@ fn client_impl<T: IbcClient>(
173162
ibc_host: &Addr,
174163
client_id: u32,
175164
) -> Result<Addr, IbcClientError<T>> {
176-
let addr = from_json::<Addr>(
177-
querier
178-
.query_wasm_raw(ibc_host.to_string(), CLIENT_IMPLS.key(client_id).to_vec())?
179-
.ok_or_else(|| {
180-
IbcClientError::Std(StdError::generic_err(format!(
181-
"unable to read client state of client {client_id}"
182-
)))
183-
})?,
184-
)?;
165+
let addr = querier
166+
.read::<ClientImpls>(ibc_host, &client_id)
167+
.map_err(|err| {
168+
IbcClientError::Std(StdError::generic_err(format!(
169+
"unable to read client state of client {client_id}: {err}"
170+
)))
171+
})?;
185172

186173
Ok(addr)
187174
}
@@ -208,8 +195,9 @@ impl<T: IbcClient> StateUpdate<T> {
208195
self
209196
}
210197

211-
pub fn add_storage_write<V: Encode<T::Encoding>>(mut self, key: Bytes, value: V) -> Self {
212-
self.storage_writes.insert(key, value.encode().into());
198+
pub fn add_storage_write<S: Store>(mut self, key: S::Key, value: S::Value) -> Self {
199+
self.storage_writes
200+
.insert(depolama::raw_key::<S>(&key), S::encode_value(&value));
213201
self
214202
}
215203
}
@@ -327,7 +315,11 @@ pub fn init<T: IbcClient>(
327315
deps: DepsMut<T::CustomQuery>,
328316
msg: InitMsg,
329317
) -> Result<Response, IbcClientError<T>> {
330-
IBC_HOST.save(deps.storage, &msg.ibc_host)?;
318+
// cosmwasm doesn't understand newtypes. the addr type in the message is not validated, so make sure we check it ourselves
319+
let ibc_host = deps.api.addr_validate(msg.ibc_host.as_ref())?;
320+
321+
deps.storage.write_item::<IbcHost>(&ibc_host);
322+
331323
Ok(Response::default())
332324
}
333325

@@ -338,19 +330,19 @@ pub fn query<T: IbcClient>(
338330
) -> Result<Binary, IbcClientError<T>> {
339331
match msg {
340332
QueryMsg::GetTimestamp { client_id, height } => {
341-
let ibc_host = IBC_HOST.load(deps.storage)?;
333+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
342334
let consensus_state =
343335
read_consensus_state::<T>(deps.querier.into_empty(), &ibc_host, client_id, height)?;
344336
to_json_binary(&T::get_timestamp(&consensus_state)).map_err(Into::into)
345337
}
346338
QueryMsg::GetLatestHeight { client_id } => {
347-
let ibc_host = IBC_HOST.load(deps.storage)?;
339+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
348340
let client_state =
349341
read_client_state::<T>(deps.querier.into_empty(), &ibc_host, client_id)?;
350342
to_json_binary(&T::get_latest_height(&client_state)).map_err(Into::into)
351343
}
352344
QueryMsg::GetStatus { client_id } => {
353-
let ibc_host = IBC_HOST.load(deps.storage)?;
345+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
354346
let client_state =
355347
read_client_state::<T>(deps.querier.into_empty(), &ibc_host, client_id)?;
356348
let status = T::status(
@@ -393,7 +385,7 @@ pub fn query<T: IbcClient>(
393385
path,
394386
value,
395387
} => {
396-
let ibc_host = IBC_HOST.load(deps.storage)?;
388+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
397389
let storage_proof = T::StorageProof::decode_as::<T::Encoding>(&proof)
398390
.map_err(DecodeError::StorageProof)?;
399391

@@ -413,7 +405,7 @@ pub fn query<T: IbcClient>(
413405
proof,
414406
path,
415407
} => {
416-
let ibc_host = IBC_HOST.load(deps.storage)?;
408+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
417409
let storage_proof = T::StorageProof::decode_as::<T::Encoding>(&proof)
418410
.map_err(DecodeError::StorageProof)?;
419411

@@ -427,8 +419,8 @@ pub fn query<T: IbcClient>(
427419
to_json_binary(&()).map_err(Into::into)
428420
}
429421
QueryMsg::UpdateState { client_id, caller } => {
430-
let ibc_host = IBC_HOST.load(deps.storage)?;
431-
let message = QUERY_STORE.query(&deps.querier, ibc_host.clone())?;
422+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
423+
let message = deps.querier.read_item::<QueryStore>(&ibc_host)?;
432424
let header =
433425
T::Header::decode_as::<T::Encoding>(&message).map_err(DecodeError::Header)?;
434426

@@ -457,7 +449,7 @@ pub fn query<T: IbcClient>(
457449
let misbehaviour = T::Misbehaviour::decode_as::<T::Encoding>(&message)
458450
.map_err(DecodeError::Misbehaviour)?;
459451

460-
let ibc_host = IBC_HOST.load(deps.storage)?;
452+
let ibc_host = deps.storage.read_item::<IbcHost>()?;
461453
let client_state = T::misbehaviour(
462454
IbcClientCtx::new(client_id, ibc_host, deps, env),
463455
misbehaviour,
@@ -478,15 +470,13 @@ pub fn read_client_state<T: IbcClient>(
478470
ibc_host: &Addr,
479471
client_id: u32,
480472
) -> Result<T::ClientState, IbcClientError<T>> {
481-
let client_state = from_json::<Bytes<Base64>>(
482-
querier
483-
.query_wasm_raw(ibc_host.to_string(), CLIENT_STATES.key(client_id).to_vec())?
484-
.ok_or_else(|| {
485-
IbcClientError::Std(StdError::generic_err(format!(
486-
"unable to read client state of client {client_id}"
487-
)))
488-
})?,
489-
)?;
473+
let client_state = querier
474+
.read::<ClientStates>(ibc_host, &client_id)
475+
.map_err(|err| {
476+
IbcClientError::Std(StdError::generic_err(format!(
477+
"unable to read client state of client {client_id}: {err}"
478+
)))
479+
})?;
490480

491481
T::ClientState::decode_as::<T::Encoding>(&client_state)
492482
.map_err(|e| IbcClientError::Decode(DecodeError::ClientState(e)))
@@ -498,49 +488,14 @@ pub fn read_consensus_state<T: IbcClient>(
498488
client_id: u32,
499489
height: u64,
500490
) -> Result<T::ConsensusState, IbcClientError<T>> {
501-
let consensus_state = from_json::<Bytes<Base64>>(
502-
querier
503-
.query_wasm_raw(
504-
ibc_host.to_string(),
505-
CLIENT_CONSENSUS_STATES.key((client_id, height)).to_vec()
506-
)?
507-
.ok_or_else(|| {
508-
IbcClientError::Std(
509-
StdError::generic_err(
510-
format!(
511-
"unable to read consensus state of client {client_id} at trusted height {height}"
512-
)
513-
)
514-
)
515-
})?
516-
)?;
517-
518-
T::ConsensusState::decode(&consensus_state)
519-
.map_err(|e| IbcClientError::Decode(DecodeError::ConsensusState(e)))
520-
}
521-
522-
fn read_storage<V: Decode<T::Encoding>, T: IbcClient>(
523-
querier: QuerierWrapper,
524-
ibc_host: &Addr,
525-
client_id: u32,
526-
key: &[u8],
527-
) -> Result<V, IbcClientError<T>> {
528-
let value = querier
529-
.query_wasm_raw(
530-
ibc_host,
531-
[
532-
CLIENT_STORAGE_PREFIX.as_bytes(),
533-
&client_id.to_le_bytes(),
534-
key,
535-
]
536-
.concat(),
537-
)?
538-
.ok_or_else(|| {
491+
let consensus_state = querier
492+
.read::<ClientConsensusStates>(ibc_host, &(client_id, height))
493+
.map_err(|err| {
539494
IbcClientError::Std(StdError::generic_err(format!(
540-
"unable to read the storage of client {client_id} with key {key:?}"
495+
"unable to read consensus state of client {client_id} at trusted height {height}: {err}"
541496
)))
542497
})?;
543498

544-
V::decode_as::<T::Encoding>(&value)
545-
.map_err(|_| IbcClientError::Decode(DecodeError::RawStorage(Bytes::new(value))))
499+
T::ConsensusState::decode(&consensus_state)
500+
.map_err(|e| IbcClientError::Decode(DecodeError::ConsensusState(e)))
546501
}

0 commit comments

Comments
 (0)