Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICS 26 Routing module minimal implementation for MVP #250

Merged
merged 9 commits into from
Sep 28, 2020
11 changes: 5 additions & 6 deletions modules/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::ics07_tendermint;
use serde_derive::{Deserialize, Serialize};
use tendermint::block::Height;

#[cfg(test)]
use crate::ics02_client::client_def::AnyConsensusState;
use crate::ics07_tendermint;
#[cfg(test)]
use crate::mock_client::header::MockHeader;
#[cfg(test)]
use crate::mock_client::state::MockConsensusState;
use {
crate::ics02_client::client_def::AnyConsensusState, crate::mock_client::header::MockHeader,
crate::mock_client::state::MockConsensusState,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum SelfChainType {
Expand Down
17 changes: 17 additions & 0 deletions modules/src/ics02_client/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error::Error;
use crate::ics02_client::handler::ClientResult;
use crate::ics02_client::handler::ClientResult::{CreateResult, UpdateResult};
use crate::ics24_host::identifier::ClientId;
use tendermint::block::Height;

Expand All @@ -11,6 +13,21 @@ pub trait ClientReader {
}

pub trait ClientKeeper {
fn store_client_result(&mut self, handler_res: ClientResult) -> Result<(), Error> {
match handler_res {
CreateResult(res) => {
self.store_client_type(res.client_id.clone(), res.client_type)?;
self.store_client_state(res.client_id.clone(), res.client_state)?;
self.store_consensus_state(res.client_id, res.consensus_state)?;
}
UpdateResult(res) => {
self.store_client_state(res.client_id.clone(), res.client_state)?;
self.store_consensus_state(res.client_id, res.consensus_state)?;
}
}
Ok(())
}

fn store_client_type(
&mut self,
client_id: ClientId,
Expand Down
22 changes: 13 additions & 9 deletions modules/src/ics02_client/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::ics02_client::error::Error;
use crate::ics02_client::msgs::ClientMsg;
use crate::ics24_host::identifier::ClientId;

use crate::ics02_client::context::{ClientKeeper, ClientReader};
use crate::ics02_client::context::ClientReader;
use crate::ics02_client::handler::create_client::CreateClientResult;
use crate::ics02_client::handler::update_client::UpdateClientResult;
use crate::ics02_client::handler::ClientResult::{CreateResult, UpdateResult};

pub mod create_client;
pub mod update_client;
Expand All @@ -14,6 +17,11 @@ pub enum ClientEvent {
ClientUpdated(ClientId),
}

pub enum ClientResult {
CreateResult(CreateClientResult),
UpdateResult(UpdateClientResult),
}

impl From<ClientEvent> for Event {
fn from(ce: ClientEvent) -> Event {
match ce {
Expand All @@ -29,9 +37,9 @@ impl From<ClientEvent> for Event {
}
}

pub fn dispatch<Ctx>(ctx: &mut Ctx, msg: ClientMsg) -> Result<HandlerOutput<()>, Error>
pub fn dispatch<Ctx>(ctx: &mut Ctx, msg: ClientMsg) -> Result<HandlerOutput<ClientResult>, Error>
where
Ctx: ClientReader + ClientKeeper,
Ctx: ClientReader,
{
match msg {
ClientMsg::CreateClient(msg) => {
Expand All @@ -41,12 +49,10 @@ where
events,
} = create_client::process(ctx, msg)?;

create_client::keep(ctx, result)?;

Ok(HandlerOutput::builder()
.with_log(log)
.with_events(events)
.with_result(()))
.with_result(CreateResult(result)))
}
ClientMsg::UpdateClient(msg) => {
let HandlerOutput {
Expand All @@ -55,12 +61,10 @@ where
events,
} = update_client::process(ctx, msg)?;

update_client::keep(ctx, result)?;

Ok(HandlerOutput::builder()
.with_log(log)
.with_events(events)
.with_result(()))
.with_result(UpdateResult(result)))
}
}
}
18 changes: 5 additions & 13 deletions modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::handler::{HandlerOutput, HandlerResult};
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::context::{ClientKeeper, ClientReader};
use crate::ics02_client::context::ClientReader;
use crate::ics02_client::error::{Error, Kind};
use crate::ics02_client::handler::ClientEvent;
use crate::ics02_client::msgs::MsgCreateAnyClient;
use crate::ics24_host::identifier::ClientId;

#[derive(Debug)]
pub struct CreateClientResult {
client_id: ClientId,
client_type: ClientType,
client_state: AnyClientState,
consensus_state: AnyConsensusState,
pub client_id: ClientId,
pub client_type: ClientType,
pub client_state: AnyClientState,
pub consensus_state: AnyConsensusState,
}

pub fn process(
Expand Down Expand Up @@ -50,14 +50,6 @@ pub fn process(
}))
}

pub fn keep(keeper: &mut dyn ClientKeeper, result: CreateClientResult) -> Result<(), Error> {
keeper.store_client_type(result.client_id.clone(), result.client_type)?;
keeper.store_client_state(result.client_id.clone(), result.client_state)?;
keeper.store_consensus_state(result.client_id, result.consensus_state)?;

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
20 changes: 5 additions & 15 deletions modules/src/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![allow(unreachable_code, unused_variables)]

use crate::handler::{HandlerOutput, HandlerResult};
use crate::ics02_client::client_def::{AnyClient, AnyClientState, AnyConsensusState, ClientDef};
use crate::ics02_client::context::{ClientKeeper, ClientReader};
use crate::ics02_client::context::ClientReader;
use crate::ics02_client::error::{Error, Kind};
use crate::ics02_client::handler::ClientEvent;

Expand All @@ -12,9 +10,9 @@ use crate::ics24_host::identifier::ClientId;

#[derive(Debug)]
pub struct UpdateClientResult {
client_id: ClientId,
client_state: AnyClientState,
consensus_state: AnyConsensusState,
pub client_id: ClientId,
pub client_state: AnyClientState,
pub consensus_state: AnyConsensusState,
}

pub fn process(
Expand All @@ -36,8 +34,7 @@ pub fn process(
.ok_or_else(|| Kind::ClientNotFound(client_id.clone()))?;

let latest_height = client_state.latest_height();
let consensus_state = ctx
.consensus_state(&client_id, latest_height)
ctx.consensus_state(&client_id, latest_height)
.ok_or_else(|| Kind::ConsensusStateNotFound(client_id.clone(), latest_height))?;

// Use client_state to validate the new header against the latest consensus_state.
Expand All @@ -56,13 +53,6 @@ pub fn process(
}))
}

pub fn keep(keeper: &mut dyn ClientKeeper, result: UpdateClientResult) -> Result<(), Error> {
keeper.store_client_state(result.client_id.clone(), result.client_state)?;
keeper.store_consensus_state(result.client_id, result.consensus_state)?;

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions modules/src/ics02_client/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::ics02_client::client_type::ClientType;
use crate::ics24_host::identifier::ClientId;

#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug)]
pub enum ClientMsg {
CreateClient(MsgCreateAnyClient),
UpdateClient(MsgUpdateAnyClient),
Expand Down
21 changes: 20 additions & 1 deletion modules/src/ics03_connection/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::context::SelfChainType;
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics03_connection::connection::ConnectionEnd;
use crate::ics03_connection::connection::{ConnectionEnd, State};
use crate::ics03_connection::error::Error;
use crate::ics03_connection::handler::ConnectionResult;
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use tendermint::block::Height;
Expand Down Expand Up @@ -47,6 +48,24 @@ pub trait ConnectionReader {
/// A context supplying all the necessary write-only dependencies (i.e., storage functionalities)
/// for processing any `ICS3Msg`.
pub trait ConnectionKeeper {
fn store_connection_result(&mut self, result: ConnectionResult) -> Result<(), Error> {
match result.connection_end.state() {
State::Init | State::TryOpen => {
self.store_connection(&result.connection_id, &result.connection_end)?;
// If this is the first time the handler processed this connection, associate the
// connection end to its client identifier.
self.store_connection_to_client(
&result.connection_id,
&result.connection_end.client_id(),
)?;
}
_ => {
self.store_connection(&result.connection_id, &result.connection_end)?;
}
}
Ok(())
}

/// Stores the given connection_end at a path associated with the connection_id.
fn store_connection(
&mut self,
Expand Down
10 changes: 5 additions & 5 deletions modules/src/ics03_connection/context_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ics24_host::identifier::{ClientId, ConnectionId};
use std::collections::HashMap;
use tendermint::block::Height;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct MockConnectionContext {
chain_context: MockChainContext,
client_context: MockClientContext,
Expand Down Expand Up @@ -70,6 +70,10 @@ impl ConnectionReader for MockConnectionContext {
self.chain_context.max_size()
}

fn chain_type(&self) -> SelfChainType {
SelfChainType::Mock
}

fn commitment_prefix(&self) -> CommitmentPrefix {
CommitmentPrefix::from(vec![])
}
Expand All @@ -82,10 +86,6 @@ impl ConnectionReader for MockConnectionContext {
self.client_context.consensus_state(client_id, height)
}

fn chain_type(&self) -> SelfChainType {
SelfChainType::Mock
}

fn fetch_self_consensus_state(&self, height: Height) -> Option<AnyConsensusState> {
let hi = self.chain_context.self_historical_info(height)?.header;
match hi {
Expand Down
Loading