Skip to content

Commit 9917337

Browse files
Separate handler methods from context APIs as free functions (#661)
* Separate handler methods from context API * Add unclog for val-exec-entry feature * Apply some improvements * Fix cargo doc for val-exec-entry feature * Separate CI's cargo doc for all-features & no-features * Fix indentation * Apply comments * changelog --------- Co-authored-by: Philippe Laferrière <[email protected]>
1 parent ee69fff commit 9917337

24 files changed

+1810
-2567
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Separate validation/execution handlers from context API
2+
([#539](https://github.com/cosmos/ibc-rs/issues/539))

.github/workflows/rust.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
command: fmt
4545
args: --all -- --check
4646

47-
doc:
47+
doc_all_features:
4848
runs-on: ubuntu-latest
4949
steps:
5050
- uses: actions/checkout@v2
@@ -56,6 +56,19 @@ jobs:
5656
with:
5757
command: doc
5858
args: --all-features --release
59+
60+
doc_no_default_features:
61+
runs-on: ubuntu-latest
62+
steps:
63+
- uses: actions/checkout@v2
64+
- uses: actions-rs/toolchain@v1
65+
with:
66+
toolchain: stable
67+
override: true
68+
- uses: actions-rs/cargo@v1
69+
with:
70+
command: doc
71+
args: --no-default-features --release
5972

6073
clippy_all_features:
6174
runs-on: ubuntu-latest

crates/ibc/src/core/context.rs

Lines changed: 8 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,35 @@
1-
mod chan_close_confirm;
2-
mod chan_close_init;
3-
mod chan_open_ack;
4-
mod chan_open_confirm;
5-
mod chan_open_init;
6-
mod chan_open_try;
7-
8-
mod acknowledgement;
9-
mod recv_packet;
10-
mod timeout;
1+
use core::time::Duration;
2+
use displaydoc::Display;
3+
use ibc_proto::google::protobuf::Any;
114

125
use crate::prelude::*;
136
use crate::signer::Signer;
147

15-
use self::chan_close_confirm::{chan_close_confirm_execute, chan_close_confirm_validate};
16-
use self::chan_close_init::{chan_close_init_execute, chan_close_init_validate};
17-
use self::chan_open_ack::{chan_open_ack_execute, chan_open_ack_validate};
18-
use self::chan_open_confirm::{chan_open_confirm_execute, chan_open_confirm_validate};
19-
use self::chan_open_init::{chan_open_init_execute, chan_open_init_validate};
20-
use self::chan_open_try::{chan_open_try_execute, chan_open_try_validate};
21-
22-
use self::acknowledgement::{acknowledgement_packet_execute, acknowledgement_packet_validate};
23-
use self::recv_packet::{recv_packet_execute, recv_packet_validate};
24-
use self::timeout::{timeout_packet_execute, timeout_packet_validate, TimeoutMsgType};
25-
26-
use super::ics02_client::msgs::MsgUpdateOrMisbehaviour;
27-
use super::router::Router;
28-
use super::{
29-
ics02_client::error::ClientError,
30-
ics03_connection::error::ConnectionError,
31-
ics04_channel::error::{ChannelError, PacketError},
32-
};
33-
use core::time::Duration;
34-
35-
use ibc_proto::google::protobuf::Any;
36-
378
use crate::core::events::IbcEvent;
389
use crate::core::ics02_client::client_state::ClientState;
3910
use crate::core::ics02_client::consensus_state::ConsensusState;
11+
use crate::core::ics02_client::error::ClientError;
4012
use crate::core::ics03_connection::connection::ConnectionEnd;
13+
use crate::core::ics03_connection::error::ConnectionError;
4114
use crate::core::ics03_connection::version::{
4215
get_compatible_versions, pick_version, Version as ConnectionVersion,
4316
};
4417
use crate::core::ics04_channel::channel::ChannelEnd;
4518
use crate::core::ics04_channel::commitment::{AcknowledgementCommitment, PacketCommitment};
4619
use crate::core::ics04_channel::context::calculate_block_delay;
47-
use crate::core::ics04_channel::msgs::{ChannelMsg, PacketMsg};
20+
use crate::core::ics04_channel::error::{ChannelError, PacketError};
4821
use crate::core::ics04_channel::packet::{Receipt, Sequence};
4922
use crate::core::ics23_commitment::commitment::CommitmentPrefix;
23+
use crate::core::ics24_host::identifier::ClientId;
5024
use crate::core::ics24_host::identifier::ConnectionId;
5125
use crate::core::ics24_host::path::{
5226
AckPath, ChannelEndPath, ClientConnectionPath, ClientConsensusStatePath, ClientStatePath,
5327
CommitmentPath, ConnectionPath, ReceiptPath, SeqAckPath, SeqRecvPath, SeqSendPath,
5428
};
29+
use crate::core::router::Router;
5530
use crate::core::timestamp::Timestamp;
56-
use crate::core::{
57-
ics02_client::{
58-
handler::{create_client, update_client, upgrade_client},
59-
msgs::ClientMsg,
60-
},
61-
ics03_connection::{
62-
handler::{conn_open_ack, conn_open_confirm, conn_open_init, conn_open_try},
63-
msgs::ConnectionMsg,
64-
},
65-
ics24_host::identifier::ClientId,
66-
msgs::MsgEnvelope,
67-
};
6831
use crate::Height;
6932

70-
use displaydoc::Display;
71-
7233
/// Top-level error
7334
#[derive(Debug, Display)]
7435
pub enum ContextError {
@@ -151,81 +112,6 @@ impl std::error::Error for RouterError {
151112
///
152113
/// Trait used for the top-level [`validate`](crate::core::validate)
153114
pub trait ValidationContext: Router {
154-
/// Validation entrypoint.
155-
fn validate(&self, msg: MsgEnvelope) -> Result<(), RouterError>
156-
where
157-
Self: Sized,
158-
{
159-
match msg {
160-
MsgEnvelope::Client(msg) => match msg {
161-
ClientMsg::CreateClient(msg) => create_client::validate(self, msg),
162-
ClientMsg::UpdateClient(msg) => {
163-
update_client::validate(self, MsgUpdateOrMisbehaviour::UpdateClient(msg))
164-
}
165-
ClientMsg::Misbehaviour(msg) => {
166-
update_client::validate(self, MsgUpdateOrMisbehaviour::Misbehaviour(msg))
167-
}
168-
ClientMsg::UpgradeClient(msg) => upgrade_client::validate(self, msg),
169-
}
170-
.map_err(RouterError::ContextError),
171-
MsgEnvelope::Connection(msg) => match msg {
172-
ConnectionMsg::OpenInit(msg) => conn_open_init::validate(self, msg),
173-
ConnectionMsg::OpenTry(msg) => conn_open_try::validate(self, msg),
174-
ConnectionMsg::OpenAck(msg) => conn_open_ack::validate(self, msg),
175-
ConnectionMsg::OpenConfirm(ref msg) => conn_open_confirm::validate(self, msg),
176-
}
177-
.map_err(RouterError::ContextError),
178-
MsgEnvelope::Channel(msg) => {
179-
let module_id = self
180-
.lookup_module_channel(&msg)
181-
.map_err(ContextError::from)?;
182-
if !self.has_route(&module_id) {
183-
return Err(ChannelError::RouteNotFound)
184-
.map_err(ContextError::ChannelError)
185-
.map_err(RouterError::ContextError);
186-
}
187-
188-
match msg {
189-
ChannelMsg::OpenInit(msg) => chan_open_init_validate(self, module_id, msg),
190-
ChannelMsg::OpenTry(msg) => chan_open_try_validate(self, module_id, msg),
191-
ChannelMsg::OpenAck(msg) => chan_open_ack_validate(self, module_id, msg),
192-
ChannelMsg::OpenConfirm(msg) => {
193-
chan_open_confirm_validate(self, module_id, msg)
194-
}
195-
ChannelMsg::CloseInit(msg) => chan_close_init_validate(self, module_id, msg),
196-
ChannelMsg::CloseConfirm(msg) => {
197-
chan_close_confirm_validate(self, module_id, msg)
198-
}
199-
}
200-
.map_err(RouterError::ContextError)
201-
}
202-
MsgEnvelope::Packet(msg) => {
203-
let module_id = self
204-
.lookup_module_packet(&msg)
205-
.map_err(ContextError::from)?;
206-
if !self.has_route(&module_id) {
207-
return Err(ChannelError::RouteNotFound)
208-
.map_err(ContextError::ChannelError)
209-
.map_err(RouterError::ContextError);
210-
}
211-
212-
match msg {
213-
PacketMsg::Recv(msg) => recv_packet_validate(self, msg),
214-
PacketMsg::Ack(msg) => acknowledgement_packet_validate(self, module_id, msg),
215-
PacketMsg::Timeout(msg) => {
216-
timeout_packet_validate(self, module_id, TimeoutMsgType::Timeout(msg))
217-
}
218-
PacketMsg::TimeoutOnClose(msg) => timeout_packet_validate(
219-
self,
220-
module_id,
221-
TimeoutMsgType::TimeoutOnClose(msg),
222-
),
223-
}
224-
.map_err(RouterError::ContextError)
225-
}
226-
}
227-
}
228-
229115
/// Returns the ClientState for the given identifier `client_id`.
230116
fn client_state(&self, client_id: &ClientId) -> Result<Box<dyn ClientState>, ContextError>;
231117

@@ -379,77 +265,6 @@ pub trait ValidationContext: Router {
379265
///
380266
/// Trait used for the top-level [`execute`](crate::core::execute) and [`dispatch`](crate::core::dispatch)
381267
pub trait ExecutionContext: ValidationContext {
382-
/// Execution entrypoint
383-
fn execute(&mut self, msg: MsgEnvelope) -> Result<(), RouterError>
384-
where
385-
Self: Sized,
386-
{
387-
match msg {
388-
MsgEnvelope::Client(msg) => match msg {
389-
ClientMsg::CreateClient(msg) => create_client::execute(self, msg),
390-
ClientMsg::UpdateClient(msg) => {
391-
update_client::execute(self, MsgUpdateOrMisbehaviour::UpdateClient(msg))
392-
}
393-
ClientMsg::Misbehaviour(msg) => {
394-
update_client::execute(self, MsgUpdateOrMisbehaviour::Misbehaviour(msg))
395-
}
396-
ClientMsg::UpgradeClient(msg) => upgrade_client::execute(self, msg),
397-
}
398-
.map_err(RouterError::ContextError),
399-
MsgEnvelope::Connection(msg) => match msg {
400-
ConnectionMsg::OpenInit(msg) => conn_open_init::execute(self, msg),
401-
ConnectionMsg::OpenTry(msg) => conn_open_try::execute(self, msg),
402-
ConnectionMsg::OpenAck(msg) => conn_open_ack::execute(self, msg),
403-
ConnectionMsg::OpenConfirm(ref msg) => conn_open_confirm::execute(self, msg),
404-
}
405-
.map_err(RouterError::ContextError),
406-
MsgEnvelope::Channel(msg) => {
407-
let module_id = self
408-
.lookup_module_channel(&msg)
409-
.map_err(ContextError::from)?;
410-
if !self.has_route(&module_id) {
411-
return Err(ChannelError::RouteNotFound)
412-
.map_err(ContextError::ChannelError)
413-
.map_err(RouterError::ContextError);
414-
}
415-
416-
match msg {
417-
ChannelMsg::OpenInit(msg) => chan_open_init_execute(self, module_id, msg),
418-
ChannelMsg::OpenTry(msg) => chan_open_try_execute(self, module_id, msg),
419-
ChannelMsg::OpenAck(msg) => chan_open_ack_execute(self, module_id, msg),
420-
ChannelMsg::OpenConfirm(msg) => chan_open_confirm_execute(self, module_id, msg),
421-
ChannelMsg::CloseInit(msg) => chan_close_init_execute(self, module_id, msg),
422-
ChannelMsg::CloseConfirm(msg) => {
423-
chan_close_confirm_execute(self, module_id, msg)
424-
}
425-
}
426-
.map_err(RouterError::ContextError)
427-
}
428-
MsgEnvelope::Packet(msg) => {
429-
let module_id = self
430-
.lookup_module_packet(&msg)
431-
.map_err(ContextError::from)?;
432-
if !self.has_route(&module_id) {
433-
return Err(ChannelError::RouteNotFound)
434-
.map_err(ContextError::ChannelError)
435-
.map_err(RouterError::ContextError);
436-
}
437-
438-
match msg {
439-
PacketMsg::Recv(msg) => recv_packet_execute(self, module_id, msg),
440-
PacketMsg::Ack(msg) => acknowledgement_packet_execute(self, module_id, msg),
441-
PacketMsg::Timeout(msg) => {
442-
timeout_packet_execute(self, module_id, TimeoutMsgType::Timeout(msg))
443-
}
444-
PacketMsg::TimeoutOnClose(msg) => {
445-
timeout_packet_execute(self, module_id, TimeoutMsgType::TimeoutOnClose(msg))
446-
}
447-
}
448-
.map_err(RouterError::ContextError)
449-
}
450-
}
451-
}
452-
453268
/// Called upon successful client creation and update
454269
fn store_client_state(
455270
&mut self,

0 commit comments

Comments
 (0)