Skip to content

Commit

Permalink
Merge branch 'master' into andy/tx-sign
Browse files Browse the repository at this point in the history
  • Loading branch information
andynog committed Oct 16, 2020
2 parents 5fda69a + 8c6702b commit 9bbb31f
Show file tree
Hide file tree
Showing 90 changed files with 1,796 additions and 2,186 deletions.
12 changes: 6 additions & 6 deletions ci/build-chains.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Instead of blindly running this code, read it line by line and understand the dependecies and tasks.
## Prerequisites: Log into Docker Hub
set -eou pipefail
GAIA_BRANCH="master" # Requires a version with the `--keyring-backend` option. v2.1 and above.
GAIA_BRANCH="stargate-4" # Requires a version with the `--keyring-backend` option. v2.1 and above.

echo "*** Requirements"
which git && which go && which make && which sed && which jq && which docker
Expand All @@ -15,7 +15,7 @@ cd gaia
git checkout "${GAIA_BRANCH}" -q

echo "*** Build binary"
GOOS=linux make build
GOOS=linux CGO_ENABLED=0 make build

echo "*** Create config using the built binary"
docker run -it --rm -v $(pwd)/build:/root:z alpine /root/gaiad testnet -o /root/chain_a --v 1 --chain-id chain_A --keyring-backend test
Expand All @@ -34,10 +34,10 @@ docker build -t informaldev/chain_a -f chain_a.Dockerfile .
docker build -t informaldev/chain_b -f chain_b.Dockerfile .

# Get details from the config files
echo SECRET_A=$(jq -r .secret gaia/build/chain_a/node0/gaiacli/key_seed.json)
echo SECRET_B=$(jq -r .secret gaia/build/chain_b/node0/gaiacli/key_seed.json)
echo NODEID_A=$(jq -r .app_state.genutil.gentxs[0].value.memo gaia/build/chain_a/node0/gaiad/config/genesis.json)
echo NODEID_B=$(jq -r .app_state.genutil.gentxs[0].value.memo gaia/build/chain_b/node0/gaiad/config/genesis.json)
echo SECRET_A=$(jq -r .secret gaia/build/chain_a/node0/gaiad/key_seed.json)
echo SECRET_B=$(jq -r .secret gaia/build/chain_b/node0/gaiad/key_seed.json)
echo NODEID_A=$(jq -r .app_state.genutil.gen_txs[0].body.memo gaia/build/chain_a/node0/gaiad/config/genesis.json)
echo NODEID_B=$(jq -r .app_state.genutil.gen_txs[0].body.memo gaia/build/chain_b/node0/gaiad/config/genesis.json)

read -p "Press ENTER to push image to Docker Hub or CTRL-C to cancel. " dontcare
docker push informaldev/chain_a
Expand Down
4 changes: 2 additions & 2 deletions ci/chain_a.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ CMD ["start"]
VOLUME [ "/root" ]

COPY gaia/build/gaiad /usr/bin/gaiad
COPY gaia/build/chain_a/node0/gaiad /root/.gaiad
COPY gaia/build/chain_a/node0/gaiacli/key_seed.json /root/key_seed.json
COPY gaia/build/chain_a/node0/gaiad /root/.gaia
COPY gaia/build/chain_a/node0/gaiad/key_seed.json /root/key_seed.json
4 changes: 2 additions & 2 deletions ci/chain_b.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ CMD ["start"]
VOLUME [ "/root" ]

COPY gaia/build/gaiad /usr/bin/gaiad
COPY gaia/build/chain_b/node0/gaiad /root/.gaiad
COPY gaia/build/chain_b/node0/gaiacli/key_seed.json /root/key_seed.json
COPY gaia/build/chain_b/node0/gaiad /root/.gaia
COPY gaia/build/chain_b/node0/gaiad/key_seed.json /root/key_seed.json
7 changes: 3 additions & 4 deletions modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ prost = "0.6.1"
prost-types = "0.6.1"
bytes = "0.5.5"
dyn-clonable = "0.9.0"
regex = "1"

[dependencies.tendermint]
version = "0.16.0"
git = "https://github.com/informalsystems/tendermint-rs"
version = "0.17.0-rc1"

[dependencies.tendermint-rpc]
version = "0.16.0"
version = "0.17.0-rc1"
features = ["http-client", "websocket-client"]
git = "https://github.com/informalsystems/tendermint-rs"

[dev-dependencies]
tokio = { version = "0.2", features = ["macros"] }
Expand Down
6 changes: 2 additions & 4 deletions modules/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::ics07_tendermint;
use serde_derive::{Deserialize, Serialize};
use tendermint::block::Height;

use crate::ics02_client::client_def::AnyConsensusState;
use crate::ics02_client::client_def::AnyHeader;
use crate::Height;

#[cfg(test)]
use {crate::mock_client::header::MockHeader, crate::mock_client::state::MockConsensusState};
Expand All @@ -17,7 +16,7 @@ pub enum SelfChainType {

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum SelfHeader {
Tendermint(ics07_tendermint::header::Header),
Tendermint(crate::ics07_tendermint::header::Header),
#[cfg(test)]
Mock(MockHeader),
}
Expand All @@ -35,7 +34,6 @@ impl From<MockHeader> for AnyConsensusState {
}

pub trait ChainReader {
fn chain_type(&self) -> SelfChainType;
fn self_historical_info(&self, height: Height) -> Option<HistoricalInfo>;
fn header(&self, height: Height) -> Option<AnyHeader>;
fn fetch_self_consensus_state(&self, height: Height) -> Option<AnyConsensusState>;
Expand Down
60 changes: 32 additions & 28 deletions modules/src/context_mock.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::context::{ChainKeeper, ChainReader, HistoricalInfo, SelfChainType, SelfHeader};
use crate::ics02_client::client_def::{AnyConsensusState, AnyHeader};
use crate::mock_client::header::MockHeader;

use std::cmp::min;
use std::convert::TryInto;
use std::error::Error;

use serde_derive::{Deserialize, Serialize};
use tendermint::block::Height;

use crate::context::{ChainKeeper, ChainReader, HistoricalInfo, SelfHeader};
use crate::ics02_client::client_def::{AnyConsensusState, AnyHeader};
use crate::ics02_client::height::{chain_version, Height};
use crate::mock_client::header::MockHeader;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub struct MockChainContext {
/// Chain identifier in the form <chain>-<version>
pub chain_id: String,
/// Maximum size of the history
pub max_size: usize,
/// Highest height of the headers in the history
Expand All @@ -21,17 +22,16 @@ pub struct MockChainContext {

impl MockChainContext {
/// Creates a new mock chain with max_size number of headers up to height h
pub fn new(max_size: usize, h: Height) -> Self {
pub fn new(chain_id: String, max_size: usize, h: Height) -> Self {
// number of headers to store, if h is 0 nothing is stored
let n = min(max_size as u64, h.value());

let n = min(max_size as u64, h.version_height);
Self {
chain_id,
max_size,
latest: h,
history: (0..n)
.rev()
// SAFETY: Call to `unwrap` is safe because we are decreasing the height
.map(|i| MockHeader((h.value() - i).try_into().unwrap()))
.map(|i| MockHeader(Height::new(h.version_number, h.version_height - i)))
.collect(),
}
}
Expand All @@ -44,23 +44,29 @@ impl MockChainContext {
pub fn populate(&mut self, hs: Vec<u64>) {
for h in hs {
self.store_historical_info(
h.try_into().unwrap(),
Height {
version_number: 0,
version_height: h,
},
HistoricalInfo {
header: SelfHeader::Mock(MockHeader(h.try_into().unwrap())),
header: SelfHeader::Mock(MockHeader(Height {
version_number: 0,
version_height: h,
})),
},
);
}
}

pub fn add_header(&mut self, h: u64) {
let mut new_h = h;
let mut new_h = Height::new(chain_version(self.chain_id.clone()), h);
if h == 0 {
new_h = u64::from(self.latest.increment());
new_h.version_height = self.latest.version_height + 1;
}
self.store_historical_info(
new_h.try_into().unwrap(),
new_h,
HistoricalInfo {
header: SelfHeader::Mock(MockHeader(new_h.try_into().unwrap())),
header: SelfHeader::Mock(MockHeader(new_h)),
},
);
}
Expand Down Expand Up @@ -91,13 +97,9 @@ impl MockChainContext {
}

impl ChainReader for MockChainContext {
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;
let l = height.version_height as usize;
let h = self.latest.version_height as usize;

if l <= h - self.max_size {
// header with height not in the history
Expand Down Expand Up @@ -150,10 +152,11 @@ impl ChainKeeper for MockChainContext {
#[cfg(test)]
mod tests {
use crate::context_mock::MockChainContext;
use std::convert::TryInto;
use crate::ics02_client::height::{chain_version, Height};

#[test]
fn test_store_historical_info() {
let chain_id = "testchain-0".to_string();
pub struct Test {
name: String,
ctx: MockChainContext,
Expand All @@ -166,25 +169,26 @@ mod tests {
}
}

let chain_version = chain_version(chain_id.clone());
let tests: Vec<Test> = vec![
Test {
name: "Add no prune".to_string(),
ctx: MockChainContext::new(3, 0_u64.try_into().unwrap()),
ctx: MockChainContext::new(chain_id.clone(), 3, Height::new(chain_version, 0)),
args: [1].to_vec(),
},
Test {
name: "Add with prune".to_string(),
ctx: MockChainContext::new(3, 2_u64.try_into().unwrap()),
ctx: MockChainContext::new(chain_id.clone(), 3, Height::new(chain_version, 2)),
args: [3, 4].to_vec(),
},
Test {
name: "Add with initial prune".to_string(),
ctx: MockChainContext::new(3, 10_u64.try_into().unwrap()),
ctx: MockChainContext::new(chain_id.clone(), 3, Height::new(chain_version, 10)),
args: [11].to_vec(),
},
Test {
name: "Attempt to add non sequential headers".to_string(),
ctx: MockChainContext::new(3, 2_u64.try_into().unwrap()),
ctx: MockChainContext::new(chain_id, 3, Height::new(chain_version, 2)),
args: [3, 5, 7].to_vec(),
},
];
Expand Down
27 changes: 18 additions & 9 deletions modules/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use crate::ics03_connection::events as ConnectionEvents;
use crate::ics04_channel::events as ChannelEvents;
use crate::ics20_fungible_token_transfer::events as TransferEvents;

use tendermint::block;
use tendermint_rpc::event::{Event as RpcEvent, EventData as RpcEventData};

use anomaly::BoxError;
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use tendermint::block::Height;

use tracing::warn;

/// Events created by the IBC component of a chain, destined for a relayer.
#[derive(Debug, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -52,15 +54,15 @@ impl IBCEvent {

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RawObject {
pub height: block::Height,
pub height: Height,
pub action: String,
pub idx: usize,
pub events: HashMap<String, Vec<String>>,
}

impl RawObject {
pub fn new(
height: block::Height,
height: Height,
action: String,
idx: usize,
events: HashMap<String, Vec<String>>,
Expand Down Expand Up @@ -131,19 +133,26 @@ pub fn get_all_events(result: RpcEvent) -> Result<Vec<IBCEvent>, String> {

RpcEventData::Tx { .. } => {
let events = &result.events.ok_or("missing events")?;
let height = events.get("tx.height").ok_or("tx.height")?[0]
let height_raw = events.get("tx.height").ok_or("tx.height")?[0]
.parse::<u64>()
.map_err(|e| e.to_string())?;
let height: Height = height_raw
.try_into()
.map_err(|_| "height parsing overflow")?;

let actions_and_indices = extract_helper(&events)?;
for action in actions_and_indices {
let ev = build_event(RawObject::new(
height.try_into().unwrap(), // TODO: Handle overflow
let event = build_event(RawObject::new(
height,
action.0,
action.1 as usize,
events.clone(),
))
.map_err(|e| e.to_string())?;
vals.push(ev);
));

match event {
Ok(event) => vals.push(event),
Err(e) => warn!("error while building event {}", e.to_string()),
}
}
}
_ => {}
Expand Down
Loading

0 comments on commit 9bbb31f

Please sign in to comment.