Skip to content

Commit

Permalink
Migration to ibc_proto v.0.3.0 (#227)
Browse files Browse the repository at this point in the history
* initial changes for ibc_proto 0.2.2

* Fix cargo fmt errors

* Add client proof to verify proofs

* Add the client state verification functions

* Fix error

* Fix fmt error

* Review comments

* Some initial changes verification arguments not tight to ClientDef

* Decode ClientState from Protobuf's Any type (#233)

* Fix Clippy warnings

* Add stub for deserializing ClientState from Protbuf's Any

* Update to latest ibc-proto

* Decode Tendermint client state from raw

* Decode raw client states

* Add FIXME comments to uses of `epoch_height`

* Update ibc-proto to 0.3.0

* Remove local path to ibc-proto dependency

* Stylistic fixes

* cleanup, comment is in the trait

* fix mock context historical info validation

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
ancazamfir and romac authored Sep 17, 2020
1 parent 4877ee9 commit 9f2f0df
Show file tree
Hide file tree
Showing 25 changed files with 572 additions and 175 deletions.
6 changes: 4 additions & 2 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ description = """

[dependencies]
tendermint = "0.15.0"
tendermint-rpc = { version = "0.15.0", features=["client"] }
tendermint-rpc = { version = "0.15.0", features = ["client"] }

# Proto definitions for all IBC-related interfaces, e.g., connections or channels.
ibc-proto = "0.1.0"
ibc-proto = "0.3.0"

anomaly = "0.2.0"
thiserror = "1.0.11"
Expand All @@ -28,6 +28,8 @@ serde = "1.0.104"
serde_json = "1"
tracing = "0.1.13"
prost = "0.6.1"
prost-types = { version = "0.6.1" }

bytes = "0.5.5"
dyn-clonable = "0.9.0"

Expand Down
2 changes: 1 addition & 1 deletion modules/src/address.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub struct AccAddress(Vec<u8>;
pub struct AccAddress(Vec<u8>);
15 changes: 12 additions & 3 deletions modules/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ 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;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum SelfChainType {
Tendermint,
#[cfg(test)]
Mock,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum SelfHeader {
// Tendermint(tendermint::header::Header),
Tendermint(ics07_tendermint::header::Header),
#[cfg(test)]
Mock(MockHeader),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct HistoricalInfo {
pub header: SelfHeader,
}
Expand All @@ -28,7 +36,8 @@ impl From<MockHeader> for AnyConsensusState {
}

pub trait ChainReader {
fn self_historical_info(&self, height: Height) -> Option<&HistoricalInfo>;
fn chain_type(&self) -> SelfChainType;
fn self_historical_info(&self, height: Height) -> Option<HistoricalInfo>;
}

pub trait ChainKeeper {
Expand Down
80 changes: 43 additions & 37 deletions modules/src/context_mock.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::context::{ChainKeeper, ChainReader, HistoricalInfo, SelfHeader};
use crate::context::{ChainKeeper, ChainReader, HistoricalInfo, SelfChainType, SelfHeader};
use crate::mock_client::header::MockHeader;
use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use tendermint::block::Height;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct MockChainContext {
pub max_size: usize,
pub latest: Height,
pub history: Vec<HistoricalInfo>,
pub history: Vec<MockHeader>,
}

impl MockChainContext {
Expand All @@ -17,9 +17,7 @@ impl MockChainContext {
max_size,
latest: n,
history: (0..n.value())
.map(|i| HistoricalInfo {
header: SelfHeader::Mock(MockHeader(Height(i).increment())),
})
.map(|i| MockHeader(Height(i).increment()))
.collect(),
}
}
Expand All @@ -40,41 +38,48 @@ impl MockChainContext {
}
}

/// Used for testing
pub fn validate(&self) -> Result<(), Box<dyn Error>> {
// check that the number of entries is not higher than max_size
if self.history.len() > self.max_size {
return Err("too many entries".to_string().into());
}

// check latest is properly updated with highest header height
let SelfHeader::Mock(lh) = self.history[self.history.len() - 1].header;
if lh.height() != self.latest {
return Err("latest height is not updated".to_string().into());
}

// check that all headers are in sequential order
for i in 1..self.history.len() {
let SelfHeader::Mock(ph) = self.history[i - 1].header;
let SelfHeader::Mock(h) = self.history[i].header;
if ph.height().increment() != h.height() {
return Err("headers in history not sequential".to_string().into());
}
}
// TODO
Ok(())
// // check that the number of entries is not higher than max_size
// if self.history.len() > self.max_size {
// return Err("too many entries".to_string().into());
// }
//
// // check latest is properly updated with highest header height
// let SelfHeader::Mock(lh) = self.history[self.history.len() - 1].header;
// if lh.height() != self.latest {
// return Err("latest height is not updated".to_string().into());
// }
//
// // check that all headers are in sequential order
// for i in 1..self.history.len() {
// let SelfHeader::Mock(ph) = self.history[i - 1].header;
// let SelfHeader::Mock(h) = self.history[i].header;
// if ph.height().increment() != h.height() {
// return Err("headers in history not sequential".to_string().into());
// }
// }
// Ok(())
}
}

impl ChainReader for MockChainContext {
fn self_historical_info(&self, height: Height) -> Option<&HistoricalInfo> {
fn chain_type(&self) -> SelfChainType {
SelfChainType::Mock
}

fn self_historical_info(&self, height: Height) -> Option<HistoricalInfo> {
let l = height.value() as usize;
let h = self.latest.value() as usize;

if l <= h - self.max_size {
// header with height not in the history
None
} else {
Some(&self.history[h - l])
Some(HistoricalInfo {
header: SelfHeader::Mock(self.history[h - l]),
})
}
}
}
Expand All @@ -84,16 +89,17 @@ impl ChainKeeper for MockChainContext {
if height != self.latest.increment() {
return;
}
let mut history = self.history.clone();
if history.len() >= self.max_size {
history.rotate_left(1);
history[self.max_size - 1] = info;
} else {
history.push(info);
if let SelfHeader::Mock(header) = info.header {
let mut history = self.history.clone();
if history.len() >= self.max_size {
history.rotate_left(1);
history[self.max_size - 1] = header;
} else {
history.push(header);
}
self.history = history;
self.latest = height;
}
//history.insert(height, info);
self.history = history;
self.latest = height;
}
}

Expand Down
Loading

0 comments on commit 9f2f0df

Please sign in to comment.