-
Notifications
You must be signed in to change notification settings - Fork 302
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
ibc: handle height in gRPC request metadata if specified #4905
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d8de569
get height from grpc metadata for relevant ibc queries
noot f3c6c9f
streamline the fix
SuperFluffy 998434f
provide trace information
SuperFluffy 1607f78
more tests
SuperFluffy b1a6e77
Merge branch 'main' of github.com:penumbra-zone/penumbra into noot/ib…
noot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
use std::str::FromStr; | ||
|
||
use anyhow::bail; | ||
use anyhow::Context as _; | ||
use cnidarium::Snapshot; | ||
use cnidarium::Storage; | ||
use ibc_proto::ibc::core::client::v1::Height; | ||
use tracing::debug; | ||
use tracing::instrument; | ||
|
||
type Type = tonic::metadata::MetadataMap; | ||
|
||
/// Determines which state snapshot to open given the height header in a [`MetadataMap`]. | ||
/// | ||
/// Returns the latest snapshot if the height header is 0, 0-0, or absent. | ||
#[instrument(skip_all, level = "debug")] | ||
pub(in crate::component::rpc) fn determine_snapshot_from_metadata( | ||
storage: Storage, | ||
metadata: &Type, | ||
) -> anyhow::Result<Snapshot> { | ||
let height = determine_height_from_metadata(metadata) | ||
.context("failed to determine height from metadata")?; | ||
if height.revision_height == 0 { | ||
Ok(storage.latest_snapshot()) | ||
} else { | ||
storage | ||
.snapshot(height.revision_height) | ||
.context("failed to create state snapshot from IBC height in height header") | ||
} | ||
} | ||
|
||
#[instrument(skip_all, level = "debug")] | ||
fn determine_height_from_metadata( | ||
metadata: &tonic::metadata::MetadataMap, | ||
) -> anyhow::Result<Height> { | ||
match metadata.get("height") { | ||
None => { | ||
debug!("height header was missing; assuming a height of 0"); | ||
Ok(TheHeight::zero().into_inner()) | ||
} | ||
Some(entry) => entry | ||
.to_str() | ||
.context("height header was present but its entry was not ASCII") | ||
.and_then(parse_as_ibc_height) | ||
.context("failed to parse height header as IBC height"), | ||
} | ||
} | ||
|
||
/// Newtype wrapper around [`Height`] to implement [`FromStr`]. | ||
#[derive(Debug)] | ||
struct TheHeight(Height); | ||
|
||
impl TheHeight { | ||
fn zero() -> Self { | ||
Self(Height { | ||
revision_number: 0, | ||
revision_height: 0, | ||
}) | ||
} | ||
fn into_inner(self) -> Height { | ||
self.0 | ||
} | ||
} | ||
|
||
impl FromStr for TheHeight { | ||
type Err = anyhow::Error; | ||
|
||
fn from_str(input: &str) -> Result<Self, Self::Err> { | ||
const FORM: &str = "input was not of the form '0' or '<number>-<height>'"; | ||
|
||
let mut parts = input.split('-'); | ||
|
||
let revision_number = parts | ||
.next() | ||
.context(FORM)? | ||
.parse::<u64>() | ||
.context("failed to parse revision number as u64")?; | ||
let revision_height = match parts.next() { | ||
None if revision_number == 0 => return Ok(Self::zero()), | ||
None => bail!(FORM), | ||
Some(rev_height) => rev_height | ||
.parse::<u64>() | ||
.context("failed to parse revision height as u64")?, | ||
}; | ||
|
||
Ok(TheHeight(Height { | ||
revision_number, | ||
revision_height, | ||
})) | ||
} | ||
} | ||
|
||
fn parse_as_ibc_height(input: &str) -> anyhow::Result<Height> { | ||
let height = input | ||
.trim() | ||
.parse::<TheHeight>() | ||
.context("failed to parse as IBC height")? | ||
.into_inner(); | ||
|
||
Ok(height) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use ibc_proto::ibc::core::client::v1::Height; | ||
use tonic::metadata::MetadataMap; | ||
|
||
use crate::component::rpc::utils::determine_height_from_metadata; | ||
|
||
use super::TheHeight; | ||
|
||
fn zero() -> Height { | ||
Height { | ||
revision_number: 0, | ||
revision_height: 0, | ||
} | ||
} | ||
|
||
fn height(revision_number: u64, revision_height: u64) -> Height { | ||
Height { | ||
revision_number, | ||
revision_height, | ||
} | ||
} | ||
|
||
#[track_caller] | ||
fn assert_ibc_height_is_parsed_correctly(input: &str, expected: Height) { | ||
let actual = input.parse::<TheHeight>().unwrap().into_inner(); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn parse_ibc_height() { | ||
assert_ibc_height_is_parsed_correctly("0", zero()); | ||
assert_ibc_height_is_parsed_correctly("0-0", zero()); | ||
assert_ibc_height_is_parsed_correctly("0-1", height(0, 1)); | ||
assert_ibc_height_is_parsed_correctly("1-0", height(1, 0)); | ||
assert_ibc_height_is_parsed_correctly("1-1", height(1, 1)); | ||
} | ||
|
||
#[track_caller] | ||
fn assert_ibc_height_is_determined_correctly(input: Option<&str>, expected: Height) { | ||
let mut metadata = MetadataMap::new(); | ||
if let Some(input) = input { | ||
metadata.insert("height", input.parse().unwrap()); | ||
} | ||
let actual = determine_height_from_metadata(&metadata).unwrap(); | ||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn determine_ibc_height_from_metadata() { | ||
assert_ibc_height_is_determined_correctly(None, zero()); | ||
assert_ibc_height_is_determined_correctly(Some("0"), zero()); | ||
assert_ibc_height_is_determined_correctly(Some("0-0"), zero()); | ||
assert_ibc_height_is_determined_correctly(Some("0-1"), height(0, 1)); | ||
assert_ibc_height_is_determined_correctly(Some("1-0"), height(1, 0)); | ||
assert_ibc_height_is_determined_correctly(Some("1-1"), height(1, 1)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use the
ibc-types-core-client::Height
type and avoid the newtyping. With that said, I think we should just merge this, and then favor using the new messages once those become available: cosmos/ibc-go#7303