Skip to content

Commit

Permalink
feat(ibc): implement consensus_state, consensus_states, consensus_sta…
Browse files Browse the repository at this point in the history
…te_heights gRPC queries (#3531)

* implement consensus_state, consensus_states, consensus_state_heights for ibc grpc query server

* fix consensus_state by getting verified heights

* fix height returned from client_state

* revert height changes to prev

* re-add getting latest verified height for consensus_state
  • Loading branch information
noot authored Dec 30, 2023
1 parent d640c21 commit f9696cc
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 41 deletions.
20 changes: 10 additions & 10 deletions crates/core/component/ibc/src/component/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) trait Ics2ClientExt: StateWrite {
// if we have a stored consensus state for this height that conflicts, we need to freeze
// the client. if it doesn't conflict, we can return early
if let Ok(stored_cs_state) = self
.get_verified_consensus_state(verified_header.height(), client_id.clone())
.get_verified_consensus_state(&verified_header.height(), &client_id)
.await
{
if stored_cs_state == verified_consensus_state {
Expand All @@ -66,11 +66,11 @@ pub(crate) trait Ics2ClientExt: StateWrite {
// have. In that case, we need to verify that the timestamp is correct. if it isn't, freeze
// the client.
let next_consensus_state = self
.next_verified_consensus_state(&client_id, verified_header.height())
.next_verified_consensus_state(&client_id, &verified_header.height())
.await
.expect("able to get next verified consensus state");
let prev_consensus_state = self
.prev_verified_consensus_state(&client_id, verified_header.height())
.prev_verified_consensus_state(&client_id, &verified_header.height())
.await
.expect("able to get previous verified consensus state");

Expand Down Expand Up @@ -252,12 +252,12 @@ pub trait StateReadExt: StateRead {

async fn get_verified_consensus_state(
&self,
height: Height,
client_id: ClientId,
height: &Height,
client_id: &ClientId,
) -> Result<TendermintConsensusState> {
self.get(
&IBC_COMMITMENT_PREFIX
.apply_string(ClientConsensusStatePath::new(&client_id, &height).to_string()),
.apply_string(ClientConsensusStatePath::new(client_id, height).to_string()),
)
.await?
.ok_or_else(|| {
Expand Down Expand Up @@ -304,7 +304,7 @@ pub trait StateReadExt: StateRead {
async fn next_verified_consensus_state(
&self,
client_id: &ClientId,
height: Height,
height: &Height,
) -> Result<Option<TendermintConsensusState>> {
let mut verified_heights =
self.get_verified_heights(client_id)
Expand All @@ -322,7 +322,7 @@ pub trait StateReadExt: StateRead {
.find(|&verified_height| verified_height > &height)
{
let next_cons_state = self
.get_verified_consensus_state(*next_height, client_id.clone())
.get_verified_consensus_state(next_height, client_id)
.await?;
return Ok(Some(next_cons_state));
} else {
Expand All @@ -335,7 +335,7 @@ pub trait StateReadExt: StateRead {
async fn prev_verified_consensus_state(
&self,
client_id: &ClientId,
height: Height,
height: &Height,
) -> Result<Option<TendermintConsensusState>> {
let mut verified_heights =
self.get_verified_heights(client_id)
Expand All @@ -353,7 +353,7 @@ pub trait StateReadExt: StateRead {
.find(|&verified_height| verified_height < &height)
{
let prev_cons_state = self
.get_verified_consensus_state(*prev_height, client_id.clone())
.get_verified_consensus_state(prev_height, client_id)
.await?;
return Ok(Some(prev_cons_state));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl MsgHandler for MsgConnectionOpenAck {

// get the stored consensus state for the counterparty
let trusted_consensus_state = state
.get_verified_consensus_state(self.proofs_height_on_b, connection.client_id.clone())
.get_verified_consensus_state(&self.proofs_height_on_b, &connection.client_id)
.await?;

// PROOF VERIFICATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl MsgHandler for MsgConnectionOpenConfirm {

// get the stored consensus state for the counterparty
let trusted_consensus_state = state
.get_verified_consensus_state(self.proof_height_on_a, connection.client_id.clone())
.get_verified_consensus_state(&self.proof_height_on_a, &connection.client_id)
.await?;

// PROOF VERIFICATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl MsgHandler for MsgConnectionOpenTry {

// get the stored consensus state for the counterparty
let trusted_consensus_state = state
.get_verified_consensus_state(self.proofs_height_on_a, self.client_id_on_b.clone())
.get_verified_consensus_state(&self.proofs_height_on_a, &self.client_id_on_b)
.await?;

// PROOF VERIFICATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ impl MsgHandler for MsgSubmitMisbehaviour {

verify_misbehavior_header(
&state,
untrusted_misbehavior.client_id.clone(),
&untrusted_misbehavior.client_id,
&untrusted_misbehavior.header1,
&trusted_client_state,
)
.await?;
verify_misbehavior_header(
&state,
untrusted_misbehavior.client_id.clone(),
&untrusted_misbehavior.client_id,
&untrusted_misbehavior.header2,
&trusted_client_state,
)
Expand Down Expand Up @@ -116,13 +116,13 @@ fn client_is_not_frozen(client: &TendermintClientState) -> anyhow::Result<()> {

async fn verify_misbehavior_header<S: StateRead>(
state: S,
client_id: ClientId,
client_id: &ClientId,
mb_header: &TendermintHeader,
trusted_client_state: &TendermintClientState,
) -> Result<()> {
let trusted_height = mb_header.trusted_height;
let last_trusted_consensus_state = state
.get_verified_consensus_state(trusted_height, client_id)
.get_verified_consensus_state(&trusted_height, &client_id)
.await?;

let trusted_height = trusted_height
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl MsgHandler for MsgUpdateClient {
// We use the specified trusted height to query the trusted
// consensus state the update extends.
let last_trusted_consensus_state = state
.get_verified_consensus_state(trusted_height, self.client_id.clone())
.get_verified_consensus_state(&trusted_height, &self.client_id)
.await?;

// We also have to convert from an IBC height, which has two
Expand Down Expand Up @@ -174,9 +174,10 @@ async fn update_is_already_committed<S: StateRead>(

// check if we already have a consensus state for this height, if we do, check that it is
// the same as this update, if it is, return early.
let untrusted_consensus_state = TendermintConsensusState::from(untrusted_header.clone());
let height = untrusted_header.height();
let untrusted_consensus_state = TendermintConsensusState::from(untrusted_header);
if let Ok(stored_consensus_state) = state
.get_verified_consensus_state(untrusted_header.height(), client_id.clone())
.get_verified_consensus_state(&height, &client_id)
.await
{
let stored_tm_consensus_state = stored_consensus_state;
Expand All @@ -196,7 +197,7 @@ async fn client_is_not_expired<S: StateRead>(
client_state: &TendermintClientState,
) -> anyhow::Result<()> {
let latest_consensus_state = state
.get_verified_consensus_state(client_state.latest_height(), client_id.clone())
.get_verified_consensus_state(&client_state.latest_height(), client_id)
.await?;

// TODO(erwan): for now there is no casting that needs to happen because `get_verified_consensus_state` does not return an
Expand Down
9 changes: 4 additions & 5 deletions crates/core/component/ibc/src/component/proof_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub trait ClientUpgradeProofVerifier: StateReadExt {

// get the stored consensus state for the counterparty
let trusted_consensus_state = self
.get_verified_consensus_state(trusted_client_state.latest_height(), client_id.clone())
.get_verified_consensus_state(&trusted_client_state.latest_height(), client_id)
.await?;

verify_merkle_proof(
Expand Down Expand Up @@ -191,7 +191,7 @@ pub trait ChannelProofVerifier: StateReadExt {

// get the stored consensus state for the counterparty
let trusted_consensus_state = self
.get_verified_consensus_state(*proof_height, connection.client_id.clone())
.get_verified_consensus_state(proof_height, &connection.client_id)
.await?;

trusted_client_state.verify_height(*proof_height)?;
Expand Down Expand Up @@ -437,9 +437,8 @@ mod inner {
anyhow::bail!("client is frozen");
}

let trusted_consensus_state = self
.get_verified_consensus_state(*height, client_id.clone())
.await?;
let trusted_consensus_state =
self.get_verified_consensus_state(height, client_id).await?;

let tm_client_state = trusted_client_state;

Expand Down
Loading

0 comments on commit f9696cc

Please sign in to comment.