ibc_testkit/testapp/ibc/core/
core_ctx.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Implementation of a global context mock. Used in testing handlers of all IBC modules.

use core::fmt::Debug;
use core::time::Duration;

use basecoin_store::context::{ProvableStore, Store};
use basecoin_store::types::Height as StoreHeight;
use ibc::core::channel::types::channel::{ChannelEnd, IdentifiedChannelEnd};
use ibc::core::channel::types::commitment::{AcknowledgementCommitment, PacketCommitment};
use ibc::core::channel::types::packet::{PacketState, Receipt};
use ibc::core::client::context::consensus_state::ConsensusState;
use ibc::core::client::types::error::ClientError;
use ibc::core::client::types::Height;
use ibc::core::commitment_types::commitment::CommitmentPrefix;
use ibc::core::commitment_types::merkle::MerkleProof;
use ibc::core::connection::types::{ConnectionEnd, IdentifiedConnectionEnd};
use ibc::core::handler::types::events::IbcEvent;
use ibc::core::host::types::error::HostError;
use ibc::core::host::types::identifiers::{ClientId, ConnectionId, Sequence};
use ibc::core::host::types::path::{
    AckPath, ChannelEndPath, ClientConnectionPath, CommitmentPath, ConnectionPath,
    NextChannelSequencePath, NextClientSequencePath, NextConnectionSequencePath, Path, ReceiptPath,
    SeqAckPath, SeqRecvPath, SeqSendPath,
};
use ibc::core::host::{ClientStateRef, ConsensusStateRef, ExecutionContext, ValidationContext};
use ibc::core::primitives::prelude::*;
use ibc::core::primitives::{Signer, Timestamp};
use ibc::primitives::ToVec;
use ibc_proto::ibc::core::commitment::v1::MerkleProof as RawMerkleProof;
use ibc_query::core::context::{ProvableContext, QueryContext};

use super::types::{MockIbcStore, DEFAULT_BLOCK_TIME_SECS};
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};

impl<S> ValidationContext for MockIbcStore<S>
where
    S: ProvableStore + Debug,
{
    type V = Self;
    type HostClientState = AnyClientState;
    type HostConsensusState = AnyConsensusState;

    fn host_height(&self) -> Result<Height, HostError> {
        Height::new(*self.revision_number.lock(), self.store.current_height())
            .map_err(HostError::invalid_state)
    }

    fn host_timestamp(&self) -> Result<Timestamp, HostError> {
        let host_height = self.host_height()?;
        let host_cons_state = self.host_consensus_state(&host_height)?;
        let timestamp = host_cons_state
            .timestamp()
            .map_err(HostError::invalid_state)?;
        Ok(timestamp)
    }

    fn client_counter(&self) -> Result<u64, HostError> {
        self.client_counter
            .get(StoreHeight::Pending, &NextClientSequencePath)
            .ok_or(HostError::missing_state("client counter"))
    }

    fn host_consensus_state(&self, height: &Height) -> Result<Self::HostConsensusState, HostError> {
        let consensus_states_binding = self.host_consensus_states.lock();

        consensus_states_binding
            .get(&height.revision_height())
            .cloned()
            .ok_or(HostError::missing_state(
                ClientError::MissingLocalConsensusState(*height),
            ))
    }

    fn validate_self_client(
        &self,
        client_state_of_host_on_counterparty: Self::HostClientState,
    ) -> Result<(), HostError> {
        if client_state_of_host_on_counterparty.is_frozen() {
            return Err(HostError::invalid_state("client unexpectedly frozen"));
        }

        let latest_height = self.host_height()?;

        let self_revision_number = latest_height.revision_number();
        if self_revision_number
            != client_state_of_host_on_counterparty
                .latest_height()
                .revision_number()
        {
            return Err(HostError::invalid_state(format!(
                "client is not in the same revision as the chain; expected `{}`, actual `{}`",
                self_revision_number,
                client_state_of_host_on_counterparty
                    .latest_height()
                    .revision_number()
            )));
        }

        let host_current_height = latest_height.increment();
        if client_state_of_host_on_counterparty.latest_height() >= host_current_height {
            return Err(HostError::invalid_state(
                format!(
                    "counterparty client state: client latest height `{}` should be less than chain height `{}`",
                    client_state_of_host_on_counterparty.latest_height(),
                    host_current_height
                ),
            ));
        }

        Ok(())
    }

    fn connection_end(&self, conn_id: &ConnectionId) -> Result<ConnectionEnd, HostError> {
        self.connection_end_store
            .get(StoreHeight::Pending, &ConnectionPath::new(conn_id))
            .ok_or(HostError::missing_state(format!(
                "connection end for connection `{}`",
                conn_id.clone()
            )))
    }

    fn commitment_prefix(&self) -> CommitmentPrefix {
        // this is prefix of ibc store
        // using a dummy prefix as in our mock context
        CommitmentPrefix::from(b"mock".to_vec())
    }

    fn connection_counter(&self) -> Result<u64, HostError> {
        self.conn_counter
            .get(StoreHeight::Pending, &NextConnectionSequencePath)
            .ok_or(HostError::missing_state("connection counter"))
    }

    fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result<ChannelEnd, HostError> {
        self.channel_end_store
            .get(StoreHeight::Pending, channel_end_path)
            .ok_or(HostError::missing_state(format!(
                "channel `{}` in port `{}`",
                channel_end_path.1.clone(),
                channel_end_path.0.clone()
            )))
    }

    fn get_next_sequence_send(&self, seq_send_path: &SeqSendPath) -> Result<Sequence, HostError> {
        self.send_sequence_store
            .get(StoreHeight::Pending, seq_send_path)
            .ok_or(HostError::failed_to_retrieve("send packet sequence"))
    }

    fn get_next_sequence_recv(&self, seq_recv_path: &SeqRecvPath) -> Result<Sequence, HostError> {
        self.recv_sequence_store
            .get(StoreHeight::Pending, seq_recv_path)
            .ok_or(HostError::failed_to_retrieve("recv packet sequence"))
    }

    fn get_next_sequence_ack(&self, seq_ack_path: &SeqAckPath) -> Result<Sequence, HostError> {
        self.ack_sequence_store
            .get(StoreHeight::Pending, seq_ack_path)
            .ok_or(HostError::failed_to_retrieve("ack packet sequence"))
    }

    fn get_packet_commitment(
        &self,
        commitment_path: &CommitmentPath,
    ) -> Result<PacketCommitment, HostError> {
        self.packet_commitment_store
            .get(StoreHeight::Pending, commitment_path)
            .ok_or(HostError::failed_to_retrieve("packet commitment"))
    }

    fn get_packet_receipt(&self, receipt_path: &ReceiptPath) -> Result<Receipt, HostError> {
        if self
            .packet_receipt_store
            .is_path_set(StoreHeight::Pending, receipt_path)
        {
            Ok(Receipt::Ok)
        } else {
            Ok(Receipt::None)
        }
    }

    fn get_packet_acknowledgement(
        &self,
        ack_path: &AckPath,
    ) -> Result<AcknowledgementCommitment, HostError> {
        self.packet_ack_store
            .get(StoreHeight::Pending, ack_path)
            .ok_or(HostError::failed_to_retrieve(format!(
                "packet acknowledgment `{}`",
                ack_path.sequence
            )))
    }

    /// Returns a counter of the number of channel ids that have been created thus far.
    /// The value of this counter should increase only via the
    /// `ChannelKeeper::increase_channel_counter` method.
    fn channel_counter(&self) -> Result<u64, HostError> {
        self.channel_counter
            .get(StoreHeight::Pending, &NextChannelSequencePath)
            .ok_or(HostError::failed_to_retrieve("channel counter"))
    }

    /// Returns the maximum expected time per block
    fn max_expected_time_per_block(&self) -> Duration {
        Duration::from_secs(DEFAULT_BLOCK_TIME_SECS)
    }

    fn validate_message_signer(&self, _signer: &Signer) -> Result<(), HostError> {
        Ok(())
    }

    fn get_client_validation_context(&self) -> &Self::V {
        self
    }
}

/// Trait to provide proofs in gRPC service blanket implementations.
impl<S> ProvableContext for MockIbcStore<S>
where
    S: ProvableStore + Debug,
{
    /// Returns the proof for the given [`Height`] and [`Path`]
    fn get_proof(&self, height: Height, path: &Path) -> Option<Vec<u8>> {
        self.store
            .get_proof(height.revision_height().into(), &path.to_string().into())
            .map(|path_proof| {
                let ibc_commitment_proof = self
                    .ibc_commiment_proofs
                    .lock()
                    .get(&height.revision_height())
                    .expect("proof exists")
                    .clone();

                RawMerkleProof::from(MerkleProof {
                    proofs: vec![path_proof, ibc_commitment_proof],
                })
            })
            .map(|p| p.to_vec())
    }
}

/// Trait to complete the gRPC service blanket implementations.
impl<S> QueryContext for MockIbcStore<S>
where
    S: ProvableStore + Debug,
{
    /// Returns the list of all client states.
    fn client_states(&self) -> Result<Vec<(ClientId, ClientStateRef<Self>)>, HostError> {
        let path = "clients".to_owned().into();

        self.client_state_store
            .get_keys(&path)
            .into_iter()
            .filter_map(|path| {
                if let Ok(Path::ClientState(client_path)) = path.try_into() {
                    Some(client_path)
                } else {
                    None
                }
            })
            .map(|client_state_path| {
                let client_state = self
                    .client_state_store
                    .get(StoreHeight::Pending, &client_state_path)
                    .ok_or_else(|| {
                        HostError::failed_to_retrieve(format!(
                            "client state from path `{}`",
                            client_state_path.0.clone()
                        ))
                    })?;
                Ok((client_state_path.0, client_state))
            })
            .collect()
    }

    /// Returns the list of all consensus states of the given client.
    fn consensus_states(
        &self,
        client_id: &ClientId,
    ) -> Result<Vec<(Height, ConsensusStateRef<Self>)>, HostError> {
        let path = format!("clients/{}/consensusStates", client_id).into();

        self.consensus_state_store
            .get_keys(&path)
            .into_iter()
            .filter_map(|path| {
                if let Ok(Path::ClientConsensusState(consensus_path)) = path.try_into() {
                    Some(consensus_path)
                } else {
                    None
                }
            })
            .map(|consensus_path| {
                let height = Height::new(
                    consensus_path.revision_number,
                    consensus_path.revision_height,
                )
                .map_err(HostError::invalid_state)?;
                let client_state = self
                    .consensus_state_store
                    .get(StoreHeight::Pending, &consensus_path)
                    .ok_or(HostError::failed_to_retrieve(format!(
                        "consensus state for client `{}` at height `{}`",
                        consensus_path.client_id, height,
                    )))?;
                Ok((height, client_state))
            })
            .collect()
    }

    /// Returns the list of heights at which the consensus state of the given client was updated.
    fn consensus_state_heights(&self, client_id: &ClientId) -> Result<Vec<Height>, HostError> {
        let path = format!("clients/{}/consensusStates", client_id).into();

        self.consensus_state_store
            .get_keys(&path)
            .into_iter()
            .filter_map(|path| {
                if let Ok(Path::ClientConsensusState(consensus_path)) = path.try_into() {
                    Some(consensus_path)
                } else {
                    None
                }
            })
            .map(|consensus_path| {
                Height::new(
                    consensus_path.revision_number,
                    consensus_path.revision_height,
                )
                .map_err(HostError::invalid_state)
            })
            .collect::<Result<Vec<_>, _>>()
    }

    /// Returns all the IBC connection ends of a chain.
    fn connection_ends(&self) -> Result<Vec<IdentifiedConnectionEnd>, HostError> {
        let path = "connections".to_owned().into();

        self.connection_end_store
            .get_keys(&path)
            .into_iter()
            .filter_map(|path| {
                if let Ok(Path::Connection(connection_path)) = path.try_into() {
                    Some(connection_path)
                } else {
                    None
                }
            })
            .map(|connection_path| {
                let connection_end = self
                    .connection_end_store
                    .get(StoreHeight::Pending, &connection_path)
                    .ok_or_else(|| {
                        HostError::failed_to_retrieve(format!(
                            "connection end `{}`",
                            connection_path.0.clone()
                        ))
                    })?;
                Ok(IdentifiedConnectionEnd {
                    connection_id: connection_path.0,
                    connection_end,
                })
            })
            .collect()
    }

    /// Returns all the IBC connection ends associated with a client.
    fn client_connection_ends(&self, client_id: &ClientId) -> Result<Vec<ConnectionId>, HostError> {
        let client_connection_path = ClientConnectionPath::new(client_id.clone());

        Ok(self
            .connection_ids_store
            .get(StoreHeight::Pending, &client_connection_path)
            .unwrap_or_default())
    }

    /// Returns all the IBC channel ends of a chain.
    fn channel_ends(&self) -> Result<Vec<IdentifiedChannelEnd>, HostError> {
        let path = "channelEnds".to_owned().into();

        self.channel_end_store
            .get_keys(&path)
            .into_iter()
            .filter_map(|path| {
                if let Ok(Path::ChannelEnd(channel_path)) = path.try_into() {
                    Some(channel_path)
                } else {
                    None
                }
            })
            .map(|channel_path| {
                let channel_end = self
                    .channel_end_store
                    .get(StoreHeight::Pending, &channel_path)
                    .ok_or_else(|| {
                        HostError::failed_to_retrieve(format!(
                            "channel `{}` with port `{}`",
                            channel_path.1.clone(),
                            channel_path.0.clone()
                        ))
                    })?;
                Ok(IdentifiedChannelEnd {
                    port_id: channel_path.0,
                    channel_id: channel_path.1,
                    channel_end,
                })
            })
            .collect()
    }

    /// Returns all the packet commitments associated with a channel.
    fn packet_commitments(
        &self,
        channel_end_path: &ChannelEndPath,
    ) -> Result<Vec<PacketState>, HostError> {
        let path = format!(
            "commitments/ports/{}/channels/{}/sequences",
            channel_end_path.0, channel_end_path.1
        )
        .into();

        self.packet_commitment_store
            .get_keys(&path)
            .into_iter()
            .filter_map(|path| {
                if let Ok(Path::Commitment(commitment_path)) = path.try_into() {
                    Some(commitment_path)
                } else {
                    None
                }
            })
            .filter(|commitment_path| {
                self.packet_commitment_store
                    .get(StoreHeight::Pending, commitment_path)
                    .is_some()
            })
            .map(|commitment_path| {
                self.get_packet_commitment(&commitment_path)
                    .map(|packet| PacketState {
                        seq: commitment_path.sequence,
                        port_id: commitment_path.port_id,
                        chan_id: commitment_path.channel_id,
                        data: packet.as_ref().into(),
                    })
            })
            .collect::<Result<Vec<_>, _>>()
    }

    /// Returns the acknowledged packets associated with a channel.
    ///
    /// Takes a sequence list as an argument.
    /// If the list set is empty, it returns all acknowledged packets.
    fn packet_acknowledgements(
        &self,
        channel_end_path: &ChannelEndPath,
        sequences: impl ExactSizeIterator<Item = Sequence>,
    ) -> Result<Vec<PacketState>, HostError> {
        let collected_paths: Vec<_> = if sequences.len() == 0 {
            // if sequences is empty, return all the acks
            let ack_path_prefix = format!(
                "acks/ports/{}/channels/{}/sequences",
                channel_end_path.0, channel_end_path.1
            )
            .into();

            self.packet_ack_store
                .get_keys(&ack_path_prefix)
                .into_iter()
                .filter_map(|path| {
                    if let Ok(Path::Ack(ack_path)) = path.try_into() {
                        Some(ack_path)
                    } else {
                        None
                    }
                })
                .collect()
        } else {
            sequences
                .into_iter()
                .map(|seq| AckPath::new(&channel_end_path.0, &channel_end_path.1, seq))
                .collect()
        };

        collected_paths
            .into_iter()
            .filter(|ack_path| {
                self.packet_ack_store
                    .get(StoreHeight::Pending, ack_path)
                    .is_some()
            })
            .map(|ack_path| {
                self.get_packet_acknowledgement(&ack_path)
                    .map(|packet| PacketState {
                        seq: ack_path.sequence,
                        port_id: ack_path.port_id,
                        chan_id: ack_path.channel_id,
                        data: packet.as_ref().into(),
                    })
            })
            .collect::<Result<Vec<_>, _>>()
    }

    /// Returns the unreceived IBC packets associated with a channel and sequences.
    ///
    /// Takes a sequence list as an argument.
    fn unreceived_packets(
        &self,
        channel_end_path: &ChannelEndPath,
        sequences: impl ExactSizeIterator<Item = Sequence>,
    ) -> Result<Vec<Sequence>, HostError> {
        // QUESTION. Currently only works for unordered channels; ordered channels
        // don't use receipts. However, ibc-go does it this way. Investigate if
        // this query only ever makes sense on unordered channels.

        Ok(sequences
            .into_iter()
            .map(|seq| ReceiptPath::new(&channel_end_path.0, &channel_end_path.1, seq))
            .filter(|receipt_path| {
                self.packet_receipt_store
                    .get(StoreHeight::Pending, receipt_path)
                    .is_none()
            })
            .map(|receipts_path| receipts_path.sequence)
            .collect())
    }

    /// Returns all the unreceived IBC acknowledgements associated with a channel and sequences.
    ///
    /// Takes a sequence list as an argument.
    /// If the list is empty, it Returns all the unreceived acks.
    fn unreceived_acks(
        &self,
        channel_end_path: &ChannelEndPath,
        sequences: impl ExactSizeIterator<Item = Sequence>,
    ) -> Result<Vec<Sequence>, HostError> {
        let collected_paths: Vec<_> = if sequences.len() == 0 {
            // if sequences is empty, return all the acks
            let commitment_path_prefix = format!(
                "commitments/ports/{}/channels/{}/sequences",
                channel_end_path.0, channel_end_path.1
            )
            .into();

            self.packet_commitment_store
                .get_keys(&commitment_path_prefix)
                .into_iter()
                .filter_map(|path| {
                    if let Ok(Path::Commitment(commitment_path)) = path.try_into() {
                        Some(commitment_path)
                    } else {
                        None
                    }
                })
                .collect()
        } else {
            sequences
                .into_iter()
                .map(|seq| CommitmentPath::new(&channel_end_path.0, &channel_end_path.1, seq))
                .collect()
        };

        Ok(collected_paths
            .into_iter()
            .filter(|commitment_path: &CommitmentPath| -> bool {
                self.packet_commitment_store
                    .get(StoreHeight::Pending, commitment_path)
                    .is_some()
            })
            .map(|commitment_path| commitment_path.sequence)
            .collect())
    }
}

impl<S> ExecutionContext for MockIbcStore<S>
where
    S: ProvableStore + Debug,
{
    type E = Self;

    fn get_client_execution_context(&mut self) -> &mut Self::E {
        self
    }

    /// Called upon client creation.
    /// Increases the counter, that keeps track of how many clients have been created.
    fn increase_client_counter(&mut self) -> Result<(), HostError> {
        let current_sequence = self
            .client_counter
            .get(StoreHeight::Pending, &NextClientSequencePath)
            .ok_or(HostError::failed_to_retrieve("client counter"))?;

        self.client_counter
            .set(NextClientSequencePath, current_sequence + 1)
            .map_err(|e| HostError::failed_to_store(format!("client counter: {e:?}")))?;

        Ok(())
    }

    /// Stores the given connection_end at path
    fn store_connection(
        &mut self,
        connection_path: &ConnectionPath,
        connection_end: ConnectionEnd,
    ) -> Result<(), HostError> {
        self.connection_end_store
            .set(connection_path.clone(), connection_end)
            .map_err(|e| HostError::failed_to_store(format!("connection end: {e:?}")))?;
        Ok(())
    }

    /// Stores the given connection_id at a path associated with the client_id.
    fn store_connection_to_client(
        &mut self,
        client_connection_path: &ClientConnectionPath,
        conn_id: ConnectionId,
    ) -> Result<(), HostError> {
        let mut conn_ids: Vec<ConnectionId> = self
            .connection_ids_store
            .get(StoreHeight::Pending, client_connection_path)
            .unwrap_or_default();
        conn_ids.push(conn_id);
        self.connection_ids_store
            .set(client_connection_path.clone(), conn_ids)
            .map_err(|e| HostError::failed_to_store(format!("connection IDs: {e:?}")))?;
        Ok(())
    }

    /// Called upon connection identifier creation (Init or Try process).
    /// Increases the counter, that keeps track of how many connections have been created.
    fn increase_connection_counter(&mut self) -> Result<(), HostError> {
        let current_sequence = self
            .conn_counter
            .get(StoreHeight::Pending, &NextConnectionSequencePath)
            .ok_or(HostError::failed_to_retrieve("connection counter"))?;

        self.conn_counter
            .set(NextConnectionSequencePath, current_sequence + 1)
            .map_err(|e| HostError::failed_to_store(format!("connection counter: {e:?}")))?;

        Ok(())
    }

    fn store_packet_commitment(
        &mut self,
        commitment_path: &CommitmentPath,
        commitment: PacketCommitment,
    ) -> Result<(), HostError> {
        self.packet_commitment_store
            .set(commitment_path.clone(), commitment)
            .map_err(|e| HostError::failed_to_store(format!("packet commitment: {e:?}")))?;
        Ok(())
    }

    fn delete_packet_commitment(
        &mut self,
        commitment_path: &CommitmentPath,
    ) -> Result<(), HostError> {
        self.packet_commitment_store.delete(commitment_path.clone());
        Ok(())
    }

    fn store_packet_receipt(
        &mut self,
        receipt_path: &ReceiptPath,
        _receipt: Receipt,
    ) -> Result<(), HostError> {
        self.packet_receipt_store
            .set_path(receipt_path.clone())
            .map_err(|e| HostError::failed_to_store(format!("packet receipt: {e:?}")))?;
        Ok(())
    }

    fn store_packet_acknowledgement(
        &mut self,
        ack_path: &AckPath,
        ack_commitment: AcknowledgementCommitment,
    ) -> Result<(), HostError> {
        self.packet_ack_store
            .set(ack_path.clone(), ack_commitment)
            .map_err(|e| HostError::failed_to_store(format!("packet acknowledgment: {e:?}")))?;
        Ok(())
    }

    fn delete_packet_acknowledgement(&mut self, ack_path: &AckPath) -> Result<(), HostError> {
        self.packet_ack_store.delete(ack_path.clone());
        Ok(())
    }

    fn store_channel(
        &mut self,
        channel_end_path: &ChannelEndPath,
        channel_end: ChannelEnd,
    ) -> Result<(), HostError> {
        self.channel_end_store
            .set(channel_end_path.clone(), channel_end)
            .map_err(|e| HostError::failed_to_store(format!("channel: {e:?}")))?;
        Ok(())
    }

    fn store_next_sequence_send(
        &mut self,
        seq_send_path: &SeqSendPath,
        seq: Sequence,
    ) -> Result<(), HostError> {
        self.send_sequence_store
            .set(seq_send_path.clone(), seq)
            .map_err(|e| HostError::failed_to_store(format!("next send sequence: {e:?}")))?;
        Ok(())
    }

    fn store_next_sequence_recv(
        &mut self,
        seq_recv_path: &SeqRecvPath,
        seq: Sequence,
    ) -> Result<(), HostError> {
        self.recv_sequence_store
            .set(seq_recv_path.clone(), seq)
            .map_err(|e| HostError::failed_to_store(format!("next recv sequence: {e:?}")))?;
        Ok(())
    }

    fn store_next_sequence_ack(
        &mut self,
        seq_ack_path: &SeqAckPath,
        seq: Sequence,
    ) -> Result<(), HostError> {
        self.ack_sequence_store
            .set(seq_ack_path.clone(), seq)
            .map_err(|e| {
                HostError::failed_to_store(format!("failed to store ack sequence: {e:?}"))
            })?;
        Ok(())
    }

    fn increase_channel_counter(&mut self) -> Result<(), HostError> {
        let current_sequence = self
            .channel_counter
            .get(StoreHeight::Pending, &NextChannelSequencePath)
            .ok_or(HostError::failed_to_retrieve("channel counter"))?;

        self.channel_counter
            .set(NextChannelSequencePath, current_sequence + 1)
            .map_err(|e| HostError::failed_to_store(format!("channel counter: {e:?}")))?;
        Ok(())
    }

    fn emit_ibc_event(&mut self, event: IbcEvent) -> Result<(), HostError> {
        self.events.lock().push(event);
        Ok(())
    }

    fn log_message(&mut self, message: String) -> Result<(), HostError> {
        self.logs.lock().push(message);
        Ok(())
    }
}