Skip to content

Commit

Permalink
tendermint: Add Vec<u8> for EventAttribute for 0.34 (informalsyst…
Browse files Browse the repository at this point in the history
…ems#1405)

0.34 doesn't enforce UTF8 for event attribute values. Adding a Vec<u8>
for those, and keeping String for later versions.
  • Loading branch information
penso committed Mar 30, 2024
1 parent f11a1be commit c3f20de
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- `[tendermint]` Change `EventAttribute` `value` from `String` to `Vec<u8>` for
TM34. `key`, `value` and `index` now have to be called through `key()`,
`value_str()` and `index()` to support both `Vec<u8>` and `String`.
([\#1400](https://github.com/informalsystems/tendermint-rs/issues/1400)).
14 changes: 7 additions & 7 deletions rpc/src/dialect/v0_34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pub struct EventAttribute {
/// The event value.
#[serde(
serialize_with = "base64string::serialize",
deserialize_with = "base64string::deserialize_to_string"
deserialize_with = "base64string::deserialize"
)]
pub value: String,
pub value: Vec<u8>,
/// Whether Tendermint's indexer should index this event.
///
/// **This field is nondeterministic**.
Expand All @@ -61,20 +61,20 @@ pub struct EventAttribute {

impl From<EventAttribute> for abci::EventAttribute {
fn from(msg: EventAttribute) -> Self {
Self {
Self::V034(abci::v0_34::EventAttribute {
key: msg.key,
value: msg.value,
index: msg.index,
}
})
}
}

impl From<abci::EventAttribute> for EventAttribute {
fn from(msg: abci::EventAttribute) -> Self {
Self {
key: msg.key,
value: msg.value,
index: msg.index,
key: msg.key().clone(),
value: msg.value_as_bytes().to_vec(),
index: msg.index(),
}
}
}
Expand Down
106 changes: 60 additions & 46 deletions rpc/tests/kvstore_fixtures/v0_34.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,21 +509,35 @@ fn incoming_fixtures() {
assert!(result.tx_result.data.is_empty());
assert_eq!(result.tx_result.events.len(), 1);
assert_eq!(result.tx_result.events[0].attributes.len(), 4);
assert_eq!(result.tx_result.events[0].attributes[0].key, "creator");
assert_eq!(result.tx_result.events[0].attributes[0].key(), "creator");
assert_eq!(
result.tx_result.events[0].attributes[0].value,
result.tx_result.events[0].attributes[0]
.value_str()
.unwrap(),
"Cosmoshi Netowoko"
);
assert_eq!(result.tx_result.events[0].attributes[1].key, "key");
assert_eq!(result.tx_result.events[0].attributes[1].value, "commit-key");
assert_eq!(result.tx_result.events[0].attributes[2].key, "index_key");
assert_eq!(result.tx_result.events[0].attributes[1].key(), "key");
assert_eq!(
result.tx_result.events[0].attributes[2].value,
result.tx_result.events[0].attributes[1]
.value_str()
.unwrap(),
"commit-key"
);
assert_eq!(result.tx_result.events[0].attributes[2].key(), "index_key");
assert_eq!(
result.tx_result.events[0].attributes[2]
.value_str()
.unwrap(),
"index is working"
);
assert_eq!(result.tx_result.events[0].attributes[3].key, "noindex_key");
assert_eq!(
result.tx_result.events[0].attributes[3].value,
result.tx_result.events[0].attributes[3].key(),
"noindex_key"
);
assert_eq!(
result.tx_result.events[0].attributes[3]
.value_str()
.unwrap(),
"index is working"
);
assert_eq!(result.tx_result.events[0].kind, "app");
Expand Down Expand Up @@ -908,23 +922,23 @@ fn incoming_fixtures() {
assert_eq!(rbb.events.len(), 2);
assert_eq!(rbb.events[0].kind, "transfer");
assert_eq!(rbb.events[0].attributes.len(), 2);
assert_eq!(rbb.events[0].attributes[0].key, "recipient");
assert_eq!(rbb.events[0].attributes[0].key(), "recipient");
assert_eq!(
rbb.events[0].attributes[0].value,
rbb.events[0].attributes[0].value_str().unwrap(),
"cosmos17xpfvakm2amg962yls6f84z3kell8c5lserqta"
);
assert!(rbb.events[0].attributes[0].index);
assert_eq!(rbb.events[0].attributes[1].key, "sender");
assert!(rbb.events[0].attributes[0].index());
assert_eq!(rbb.events[0].attributes[1].key(), "sender");
assert_eq!(
rbb.events[0].attributes[1].value,
rbb.events[0].attributes[1].value_str().unwrap(),
"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q"
);
assert!(!rbb.events[0].attributes[1].index);
assert!(!rbb.events[0].attributes[1].index());
assert_eq!(rbb.events[1].kind, "message");
assert_eq!(rbb.events[1].attributes.len(), 1);
assert_eq!(rbb.events[1].attributes[0].key, "sender");
assert_eq!(rbb.events[1].attributes[0].key(), "sender");
assert_eq!(
rbb.events[1].attributes[0].value,
rbb.events[1].attributes[0].value_str().unwrap(),
"cosmos1m3h30wlvsf8llruxtpukdvsy0km2kum8g38c8q"
);
let reb = result_end_block.unwrap();
Expand Down Expand Up @@ -1115,18 +1129,18 @@ fn incoming_fixtures() {
assert_eq!(tx_result.result.events.len(), 1);
assert_eq!(tx_result.result.events[0].kind, "app");
for attr in &tx_result.result.events[0].attributes {
match attr.key.as_str() {
match attr.key().as_str() {
"creator" => {
assert_eq!(attr.value, "Cosmoshi Netowoko")
assert_eq!(attr.value_str().unwrap(), "Cosmoshi Netowoko")
},
"key" => assert_eq!(attr.value, "tx0"),
"key" => assert_eq!(attr.value_str().unwrap(), "tx0"),
"index_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
"noindex_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
_ => panic!("unknown attribute found {}", attr.key),
_ => panic!("unknown attribute found {}", attr.key()),
}
}
assert_eq!(tx_result.tx, base64::decode("dHgwPXZhbHVl").unwrap());
Expand All @@ -1147,18 +1161,18 @@ fn incoming_fixtures() {
assert_eq!(tx_result.result.events.len(), 1);
assert_eq!(tx_result.result.events[0].kind, "app");
for attr in &tx_result.result.events[0].attributes {
match attr.key.as_str() {
match attr.key().as_str() {
"creator" => {
assert_eq!(attr.value, "Cosmoshi Netowoko")
assert_eq!(attr.value_str().unwrap(), "Cosmoshi Netowoko")
},
"key" => assert_eq!(attr.value, "tx1"),
"key" => assert_eq!(attr.value_str().unwrap(), "tx1"),
"index_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
"noindex_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
_ => panic!("unknown attribute found {}", attr.key),
_ => panic!("unknown attribute found {}", attr.key()),
}
}
assert_eq!(tx_result.tx, base64::decode("dHgxPXZhbHVl").unwrap());
Expand All @@ -1180,18 +1194,18 @@ fn incoming_fixtures() {
assert_eq!(tx_result.result.events.len(), 1);
assert_eq!(tx_result.result.events[0].kind, "app");
for attr in &tx_result.result.events[0].attributes {
match attr.key.as_str() {
match attr.key().as_str() {
"creator" => {
assert_eq!(attr.value, "Cosmoshi Netowoko")
assert_eq!(attr.value_str().unwrap(), "Cosmoshi Netowoko")
},
"key" => assert_eq!(attr.value, "tx2"),
"key" => assert_eq!(attr.value_str().unwrap(), "tx2"),
"index_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
"noindex_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
_ => panic!("unknown attribute found {}", attr.key),
_ => panic!("unknown attribute found {}", attr.key()),
}
}
assert_eq!(tx_result.tx, base64::decode("dHgyPXZhbHVl").unwrap());
Expand All @@ -1212,18 +1226,18 @@ fn incoming_fixtures() {
assert_eq!(tx_result.result.events.len(), 1);
assert_eq!(tx_result.result.events[0].kind, "app");
for attr in &tx_result.result.events[0].attributes {
match attr.key.as_str() {
match attr.key().as_str() {
"creator" => {
assert_eq!(attr.value, "Cosmoshi Netowoko")
assert_eq!(attr.value_str().unwrap(), "Cosmoshi Netowoko")
},
"key" => assert_eq!(attr.value, "tx3"),
"key" => assert_eq!(attr.value_str().unwrap(), "tx3"),
"index_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
"noindex_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
_ => panic!("unknown attribute found {}", attr.key),
_ => panic!("unknown attribute found {}", attr.key()),
}
}
assert_eq!(tx_result.tx, base64::decode("dHgzPXZhbHVl").unwrap());
Expand All @@ -1244,18 +1258,18 @@ fn incoming_fixtures() {
assert_eq!(tx_result.result.events.len(), 1);
assert_eq!(tx_result.result.events[0].kind, "app");
for attr in &tx_result.result.events[0].attributes {
match attr.key.as_str() {
match attr.key().as_str() {
"creator" => {
assert_eq!(attr.value, "Cosmoshi Netowoko")
assert_eq!(attr.value_str().unwrap(), "Cosmoshi Netowoko")
},
"key" => assert_eq!(attr.value, "tx4"),
"key" => assert_eq!(attr.value_str().unwrap(), "tx4"),
"index_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
"noindex_key" => {
assert_eq!(attr.value, "index is working")
assert_eq!(attr.value_str().unwrap(), "index is working")
},
_ => panic!("unknown attribute found {}", attr.key),
_ => panic!("unknown attribute found {}", attr.key()),
}
}
assert_eq!(tx_result.tx, base64::decode("dHg0PXZhbHVl").unwrap());
Expand Down
Loading

0 comments on commit c3f20de

Please sign in to comment.