Skip to content

Commit

Permalink
Remove the concept of a "zero Height" (informalsystems#2346)
Browse files Browse the repository at this point in the history
Prior to this PR, the `Height` type used a special `Height::zero()` value to denote the absence of height value. This caused ad hoc checks of whether a height is zero in various places.

From now on, we instead use `Option<Height>` on places where the height value may be optional, and require non-zero value in the `Height` constructor.

- Optional height parameters should use `Option<Height>` instead of `Height`.
- Checking of `height == Height::zero()` is replaced with Option matching.

---

* From<Height> for String fix

* Use Option<Height> instead of Height::zero for consensus_height

* recv_packet still had Height::zero() in events

* TryFrom<RawHeight> for Height

* pre height parse changes

* Packet.timeout_height is now an Option<Height>

* rustdoc

* Remove Height::with_revision_height

* commit pre-modifying cli

* Finish Height::new() returns Result

* remove Height::is_zero()

* Remove `Height::zero()`

* Remove Height::default()

* Height fields accessors

* use revision_height accessor

* use revision_number accessor

* make Height fields private (WIP)

* compile error fixes

* FIXME in transfer.rs addressed

* Use existing constants instead of hardcoded strings

* changelog

* TimeoutHeight newtype

* Use TimeoutHeight everywhere

* Fixes after merging with master

* has_expired() fix

* doc url

* fix send_packet test

* Remove timeout_height == 0 && timestamp == 0 check

* Remove `has_timeout()`

* Change TimeoutHeight impl

* docstrings

* tests fix

* fix history manipulation test

* transfer test: no timeout

* msgtransfer test

* packet no timeout or timestamp test

* send_packet timeout height equal destination chain height

* send_packet test with timeout = height - 1

* test invalid timeout height

* fix docstring

* clippy

* Fix clippy warnings introduced in Rust 1.62

* Fix serialization of `TimeoutHeight` to match the format used by `Height`

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
plafer and romac committed Jul 4, 2022
1 parent 61cb63e commit 09c2eeb
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Remove the concept of a zero Height
([#1009](https://github.com/informalsystems/ibc-rs/issues/1009))
7 changes: 7 additions & 0 deletions e2e/e2e/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class Height:
revision_number: int


@dataclass
class TimeoutHeight:
revision_height: int
revision_number: int


@dataclass
class Duration:
nanos: int
Expand Down Expand Up @@ -39,6 +45,7 @@ class Ordering(Enum):
ClientType = NewType('ClientType', str)
BlockHeight = NewType('BlockHeight', str)


def split():
sleep(0.5)
print()
2 changes: 1 addition & 1 deletion e2e/e2e/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Packet:
destination_port: PortId
destination_channel: ChannelId
data: Hex
timeout_height: Height
timeout_height: TimeoutHeight
timeout_timestamp: Timestamp


Expand Down
3 changes: 2 additions & 1 deletion relayer-cli/src/commands/keys/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::collections::btree_map::BTreeMap as HashMap;
use core::fmt::Write;

use abscissa_core::clap::Parser;
use abscissa_core::{Command, Runnable};
Expand Down Expand Up @@ -52,7 +53,7 @@ impl Runnable for KeysListCmd {
Ok(keys) => {
let mut msg = String::new();
for (name, key) in keys {
msg.push_str(&format!("\n- {} ({})", name, key.account));
let _ = write!(msg, "\n- {} ({})", name, key.account);
}
Output::success_msg(msg).exit()
}
Expand Down
5 changes: 4 additions & 1 deletion relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ impl Runnable for QueryChannelEndCmd {
port_id: self.port_id.clone(),
channel_id: self.channel_id,
height: self.height.map_or(QueryHeight::Latest, |revision_height| {
QueryHeight::Specific(ibc::Height::new(chain.id().version(), revision_height))
QueryHeight::Specific(
ibc::Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
}),
},
IncludeProof::No,
Expand Down
6 changes: 4 additions & 2 deletions relayer-cli/src/commands/query/channel_ends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ibc_relayer::chain::requests::{
};
use ibc_relayer::registry::Registry;

use crate::conclude::Output;
use crate::conclude::{exit_with_unrecoverable_error, Output};
use crate::prelude::*;

#[derive(Clone, Command, Debug, Parser)]
Expand Down Expand Up @@ -96,7 +96,9 @@ fn do_run<Chain: ChainHandle>(cmd: &QueryChannelEndsCmd) -> Result<(), Box<dyn s
let chain = registry.get_or_spawn(&chain_id)?;

let chain_height = match cmd.height {
Some(height) => Height::new(chain.id().version(), height),
Some(height) => {
Height::new(chain.id().version(), height).unwrap_or_else(exit_with_unrecoverable_error)
}
None => chain.query_latest_height()?,
};

Expand Down
28 changes: 17 additions & 11 deletions relayer-cli/src/commands/query/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl Runnable for QueryClientStateCmd {
QueryClientStateRequest {
client_id: self.client_id.clone(),
height: self.height.map_or(QueryHeight::Latest, |revision_height| {
QueryHeight::Specific(ibc::Height::new(chain.id().version(), revision_height))
QueryHeight::Specific(
ibc::Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
}),
},
IncludeProof::No,
Expand Down Expand Up @@ -135,7 +138,8 @@ impl Runnable for QueryClientConsensusCmd {

match self.consensus_height {
Some(cs_height) => {
let consensus_height = ibc::Height::new(counterparty_chain.version(), cs_height);
let consensus_height = ibc::Height::new(counterparty_chain.version(), cs_height)
.unwrap_or_else(exit_with_unrecoverable_error);

let res = chain
.query_consensus_state(
Expand All @@ -145,10 +149,10 @@ impl Runnable for QueryClientConsensusCmd {
query_height: self.height.map_or(
QueryHeight::Latest,
|revision_height| {
QueryHeight::Specific(ibc::Height::new(
chain.id().version(),
revision_height,
))
QueryHeight::Specific(
ibc::Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
},
),
},
Expand Down Expand Up @@ -212,7 +216,7 @@ pub struct QueryClientHeaderCmd {
#[clap(
long = "height",
value_name = "HEIGHT",
help = "The chain height context for the query"
help = "The chain height context for the query. Leave unspecified for latest height."
)]
height: Option<u64>,
}
Expand Down Expand Up @@ -244,12 +248,14 @@ impl Runnable for QueryClientHeaderCmd {
};

let consensus_height =
ibc::Height::new(counterparty_chain.version(), self.consensus_height);
ibc::Height::new(counterparty_chain.version(), self.consensus_height)
.unwrap_or_else(exit_with_unrecoverable_error);

let query_height = match self.height {
Some(revision_height) => {
QueryHeight::Specific(Height::new(chain.id().version(), revision_height))
}
Some(revision_height) => QueryHeight::Specific(
Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
),
None => QueryHeight::Latest,
};

Expand Down
7 changes: 5 additions & 2 deletions relayer-cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct QueryConnectionEndCmd {
#[clap(
long = "height",
value_name = "HEIGHT",
help = "Height of the state to query"
help = "Height of the state to query. Leave unspecified for latest height."
)]
height: Option<u64>,
}
Expand All @@ -57,7 +57,10 @@ impl Runnable for QueryConnectionEndCmd {
QueryConnectionRequest {
connection_id: self.connection_id.clone(),
height: self.height.map_or(QueryHeight::Latest, |revision_height| {
QueryHeight::Specific(ibc::Height::new(chain.id().version(), revision_height))
QueryHeight::Specific(
ibc::Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
}),
},
IncludeProof::No,
Expand Down
12 changes: 6 additions & 6 deletions relayer-cli/src/commands/query/packet/ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ibc::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc_relayer::chain::handle::ChainHandle;

use crate::cli_utils::spawn_chain_runtime;
use crate::conclude::Output;
use crate::conclude::{exit_with_unrecoverable_error, Output};
use crate::error::Error;
use crate::prelude::*;

Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct QueryPacketAcknowledgmentCmd {
#[clap(
long = "height",
value_name = "HEIGHT",
help = "Height of the state to query"
help = "Height of the state to query. Leave unspecified for latest height."
)]
height: Option<u64>,
}
Expand All @@ -71,10 +71,10 @@ impl QueryPacketAcknowledgmentCmd {
channel_id: self.channel_id,
sequence: self.sequence,
height: self.height.map_or(QueryHeight::Latest, |revision_height| {
QueryHeight::Specific(ibc::Height::new(
chain.id().version(),
revision_height,
))
QueryHeight::Specific(
ibc::Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
}),
},
IncludeProof::No,
Expand Down
12 changes: 6 additions & 6 deletions relayer-cli/src/commands/query/packet/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;

use crate::cli_utils::spawn_chain_runtime;
use crate::conclude::Output;
use crate::conclude::{exit_with_unrecoverable_error, Output};
use crate::error::Error;
use crate::prelude::*;

Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct QueryPacketCommitmentCmd {
#[clap(
long = "height",
value_name = "HEIGHT",
help = "Height of the state to query"
help = "Height of the state to query. Leave unspecified for latest height."
)]
height: Option<u64>,
}
Expand All @@ -79,10 +79,10 @@ impl QueryPacketCommitmentCmd {
channel_id: self.channel_id,
sequence: self.sequence,
height: self.height.map_or(QueryHeight::Latest, |revision_height| {
QueryHeight::Specific(ibc::Height::new(
chain.id().version(),
revision_height,
))
QueryHeight::Specific(
ibc::Height::new(chain.id().version(), revision_height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
}),
},
IncludeProof::No,
Expand Down
16 changes: 10 additions & 6 deletions relayer-cli/src/commands/tx/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ pub struct TxUpdateClientCmd {
#[clap(
long = "height",
value_name = "REFERENCE_HEIGHT",
help = "The target height of the client update"
help = "The target height of the client update. Leave unspecified for latest height."
)]
target_height: Option<u64>,

#[clap(
long = "trusted-height",
value_name = "REFERENCE_TRUSTED_HEIGHT",
help = "The trusted height of the client update"
help = "The trusted height of the client update. Leave unspecified for latest height."
)]
trusted_height: Option<u64>,
}
Expand Down Expand Up @@ -166,12 +166,16 @@ impl Runnable for TxUpdateClientCmd {
};

let target_height = self.target_height.map_or(QueryHeight::Latest, |height| {
QueryHeight::Specific(Height::new(src_chain.id().version(), height))
QueryHeight::Specific(
Height::new(src_chain.id().version(), height)
.unwrap_or_else(exit_with_unrecoverable_error),
)
});

let trusted_height = self
.trusted_height
.map(|height| Height::new(src_chain.id().version(), height));
let trusted_height = self.trusted_height.map(|height| {
Height::new(src_chain.id().version(), height)
.unwrap_or_else(exit_with_unrecoverable_error)
});

let client = ForeignClient::find(src_chain, dst_chain, &self.dst_client_id)
.unwrap_or_else(exit_with_unrecoverable_error);
Expand Down
8 changes: 0 additions & 8 deletions relayer-cli/src/commands/tx/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ impl TxIcs20MsgTransferCmd {
return Err("number of messages should be greater than zero".into());
}

if self.timeout_height_offset == 0 && self.timeout_seconds == 0 {
return Err(
"packet timeout height and packet timeout timestamp cannot both be 0, \
please specify either --timeout-height-offset or --timeout-seconds"
.into(),
);
}

let opts = TransferOptions {
packet_src_port_id: self.src_port_id.clone(),
packet_src_channel_id: self.src_channel_id,
Expand Down
Loading

0 comments on commit 09c2eeb

Please sign in to comment.