Skip to content

Commit

Permalink
Adapt to upcoming changes in tendermint-rs regarding EventAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Apr 22, 2024
1 parent 1f2dcea commit ed430a3
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 61 deletions.
37 changes: 8 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ uuid = "1.8.0"
overflow-checks = true

[patch.crates-io]
# tendermint = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-rpc = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-proto = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-light-client = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-light-client-verifier = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-light-client-detector = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
# tendermint-testgen = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint-rpc = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint-proto = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint-light-client = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint-light-client-verifier = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint-light-client-detector = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
tendermint-testgen = { git = "https://github.com/informalsystems/tendermint-rs.git", branch = "main" }
6 changes: 5 additions & 1 deletion crates/relayer-types/src/applications/ics29_fee/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ define_error! {

EventAttributeNotFound
{ key: String }
| e | { format_args!("IBC event attribute not found for key: {}", e.key) },
| e | { format_args!("IBC event attribute not found for key `{}`", e.key) },

EventAttributeInvalidUtf8
{ key: String }
| e | { format_args!("IBC event attribute value for key `{}` is not a valid UTF-8 string", e.key) },
}
}
9 changes: 7 additions & 2 deletions crates/relayer-types/src/applications/ics29_fee/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ fn find_value<'a>(key: &str, entries: &'a [abci::EventAttribute]) -> Result<&'a
entries
.iter()
.find_map(|entry| {
if entry.key == key {
Some(entry.value.as_str())
if entry.key_bytes() == key.as_bytes() {
Some(
entry
.value_str()
.map_err(|_| Error::event_attribute_invalid_utf8(key.to_owned())),
)
} else {
None
}
})
.transpose()?
.ok_or_else(|| Error::event_attribute_not_found(key.to_owned()))
}

Expand Down
15 changes: 8 additions & 7 deletions crates/relayer-types/src/applications/ics31_icq/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,22 @@ fn find_value<'a>(key: &str, entries: &'a [abci::EventAttribute]) -> Result<&'a
entries
.iter()
.find_map(|entry| {
if entry.key == key {
Some(entry.value.as_str())
if entry.key_bytes() == key.as_bytes() {
Some(entry.value_str().map_err(|_| {
Error::event(format!(
"attribute value for key {key} is not a valid UTF-8 string"
))
}))
} else {
None
}
})
.transpose()?
.ok_or_else(|| Error::event(format!("attribute not found for key: {key}")))
}

fn new_attr(key: &str, value: &str) -> abci::EventAttribute {
abci::EventAttribute {
key: String::from(key),
value: String::from(value),
index: true,
}
abci::EventAttribute::from((key, value, true))
}

impl From<CrossChainQueryPacket> for abci::Event {
Expand Down
7 changes: 7 additions & 0 deletions crates/relayer-types/src/core/ics02_client/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,12 @@ define_error! {
ClientSpecific
{ description: String }
| e | { format_args!("client specific error: {0}", e.description) },

MalformedEventAttributeKey
| _ | { format_args!("event attribute key is not valid UTF-8") },

MalformedEventAttributeValue
{ key: String }
| e | { format_args!("event attribute value for key {} is not valid UTF-8", e.key) },
}
}
7 changes: 7 additions & 0 deletions crates/relayer-types/src/core/ics03_connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,12 @@ define_error! {

ImplementationSpecific
| _ | { "implementation specific error" },

MalformedEventAttributeKey
| _ | { format_args!("event attribute key is not valid UTF-8") },

MalformedEventAttributeValue
{ key: String }
| e | { format_args!("event attribute value for key {} is not valid UTF-8", e.key) },
}
}
9 changes: 8 additions & 1 deletion crates/relayer-types/src/core/ics04_channel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,14 @@ define_error! {

AbciConversionFailed
{ abci_event: String }
| e | { format_args!("Failed to convert abci event to IbcEvent: {}", e.abci_event)}
| e | { format_args!("Failed to convert abci event to IbcEvent: {}", e.abci_event)},

MalformedEventAttributeKey
| _ | { format_args!("event attribute key is not valid UTF-8") },

MalformedEventAttributeValue
{ key: String }
| e | { format_args!("event attribute value for key {} is not valid UTF-8", e.key) },
}
}

Expand Down
49 changes: 38 additions & 11 deletions crates/relayer/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,14 @@ fn client_extract_attributes_from_tx(event: &AbciEvent) -> Result<ClientAttribut
let mut attr = ClientAttributes::default();

for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
let key = tag
.key_str()
.map_err(|_| ClientError::malformed_event_attribute_key())?;

let value = tag
.value_str()
.map_err(|_| ClientError::malformed_event_attribute_value(key.to_owned()))?;

match key {
client_events::CLIENT_ID_ATTRIBUTE_KEY => {
attr.client_id = value
Expand All @@ -328,9 +334,13 @@ fn client_extract_attributes_from_tx(event: &AbciEvent) -> Result<ClientAttribut

pub fn extract_header_from_tx(event: &AbciEvent) -> Result<AnyHeader, ClientError> {
for tag in &event.attributes {
if tag.key == HEADER_ATTRIBUTE_KEY {
let header_bytes = hex::decode(tag.value.to_lowercase())
.map_err(|_| ClientError::malformed_header())?;
if tag.key_bytes() == HEADER_ATTRIBUTE_KEY.as_bytes() {
let header_bytes = hex::decode(
tag.value_str()
.map_err(|_| ClientError::malformed_header())?
.to_lowercase(),
)
.map_err(|_| ClientError::malformed_header())?;
return decode_header(&header_bytes);
}
}
Expand All @@ -344,8 +354,14 @@ fn connection_extract_attributes_from_tx(
let mut attr = ConnectionAttributes::default();

for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
let key = tag
.key_str()
.map_err(|_| ConnectionError::malformed_event_attribute_key())?;

let value = tag
.value_str()
.map_err(|_| ConnectionError::malformed_event_attribute_value(key.to_owned()))?;

match key {
connection_events::CONN_ID_ATTRIBUTE_KEY => {
attr.connection_id = value.parse().ok();
Expand Down Expand Up @@ -373,8 +389,14 @@ fn channel_extract_attributes_from_tx(
let mut attr = ChannelAttributes::default();

for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
let key = tag
.key_str()
.map_err(|_| ChannelError::malformed_event_attribute_key())?;

let value = tag
.value_str()
.map_err(|_| ChannelError::malformed_event_attribute_value(key.to_owned()))?;

match key {
channel_events::PORT_ID_ATTRIBUTE_KEY => {
attr.port_id = value.parse().map_err(ChannelError::identifier)?
Expand Down Expand Up @@ -405,8 +427,13 @@ pub fn extract_packet_and_write_ack_from_tx(
let mut write_ack: Vec<u8> = Vec::new();

for tag in &event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
let key = tag
.key_str()
.map_err(|_| ChannelError::malformed_event_attribute_key())?;

let value = tag
.value_str()
.map_err(|_| ChannelError::malformed_event_attribute_value(key.to_owned()))?;

match key {
channel_events::PKT_SRC_PORT_ATTRIBUTE_KEY => {
Expand Down
8 changes: 5 additions & 3 deletions crates/relayer/src/event/source/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ fn dedupe(events: Vec<abci::Event>) -> Vec<abci::Event> {
.attributes
.iter()
.zip(other.0.attributes.iter())
.all(|(a, b)| a.key == b.key && a.value == b.value)
.all(|(a, b)| {
a.key_bytes() == b.key_bytes() && a.value_bytes() == b.value_bytes()
})
}
}

Expand All @@ -304,8 +306,8 @@ fn dedupe(events: Vec<abci::Event>) -> Vec<abci::Event> {

for attr in &self.0.attributes {
// NOTE: We don't hash the index because it is not deterministic
attr.key.hash(state);
attr.value.hash(state);
attr.key_bytes().hash(state);
attr.value_bytes().hash(state);
}
}
}
Expand Down

0 comments on commit ed430a3

Please sign in to comment.