From c538491250bcddf0701369b04a724dc82b3d0b29 Mon Sep 17 00:00:00 2001 From: Spencer T Brody Date: Fri, 16 Aug 2024 13:36:03 -0400 Subject: [PATCH 1/2] fix: Fix bug in TimeEvent witness blocks parsing logic --- event/src/unvalidated/builder.rs | 6 +++--- event/src/unvalidated/event.rs | 24 +++++++++++++++--------- event/src/unvalidated/mod.rs | 4 +++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/event/src/unvalidated/builder.rs b/event/src/unvalidated/builder.rs index 4eff94f91..db3aa1f5f 100644 --- a/event/src/unvalidated/builder.rs +++ b/event/src/unvalidated/builder.rs @@ -422,7 +422,7 @@ mod tests { use crate::unvalidated::signed::JwkSigner; use crate::unvalidated::tests::{ DATA_EVENT_PAYLOAD, SIGNED_INIT_EVENT, SIGNED_INIT_EVENT_CAR, SIGNED_INIT_EVENT_CID, - SIGNED_INIT_EVENT_PAYLOAD, TIME_EVENT_CAR, + SIGNED_INIT_EVENT_PAYLOAD, TIME_EVENT_CAR_SINGLE_EVENT_BATCH, }; use crate::DidDocument; @@ -508,7 +508,7 @@ mod tests { assert_eq!(SIGNED_INIT_EVENT_CAR, event_car_str); } #[test(tokio::test)] - async fn build_time_event() { + async fn build_time_event_single_event_batch() { let id = Cid::from_str(SIGNED_INIT_EVENT_CID).unwrap(); let prev = Cid::from_str("bagcqcerae5oqoglzjjgz53enwsttl7mqglp5eoh2llzbbvfktmzxleeiffbq").unwrap(); @@ -532,6 +532,6 @@ mod tests { multibase::Base::Base64Url, event.encode_car().await.unwrap(), ); - assert_eq!(TIME_EVENT_CAR, event_car_str); + assert_eq!(TIME_EVENT_CAR_SINGLE_EVENT_BATCH, event_car_str); } } diff --git a/event/src/unvalidated/event.rs b/event/src/unvalidated/event.rs index 63d496d49..7ced4e462 100644 --- a/event/src/unvalidated/event.rs +++ b/event/src/unvalidated/event.rs @@ -50,12 +50,13 @@ where let block_bytes = car_blocks .get(&proof.root()) .ok_or_else(|| anyhow!("Time Event CAR data missing block for root",))?; - let mut block: Ipld = serde_ipld_dagcbor::from_slice(block_bytes)?; - blocks_in_path.push(block.clone()); + blocks_in_path.push(serde_ipld_dagcbor::from_slice(block_bytes)?); let parts: Vec<_> = event.path().split('/').collect(); // Add blocks for all parts but the last as it is the prev. for index in parts.iter().take(parts.len() - 1) { - let cid = block + let cid = blocks_in_path + .last() + .unwrap() .get(*index)? .ok_or_else(|| anyhow!("Time Event path indexes missing data"))?; let cid = match cid { @@ -65,8 +66,7 @@ where let block_bytes = car_blocks .get(cid) .ok_or_else(|| anyhow!("Time Event CAR data missing block for path index"))?; - blocks_in_path.push(block); - block = serde_ipld_dagcbor::from_slice(block_bytes)?; + blocks_in_path.push(serde_ipld_dagcbor::from_slice(block_bytes)?); } Ok(blocks_in_path) @@ -407,12 +407,14 @@ mod tests { use ipld_core::ipld::Ipld; use test_log::test; - use crate::unvalidated::tests::{SIGNED_INIT_EVENT_CID, UNSIGNED_INIT_NO_SEP_CAR}; + use crate::unvalidated::tests::{ + SIGNED_INIT_EVENT_CID, TIME_EVENT_CAR_MULTI_EVENT_BATCH, UNSIGNED_INIT_NO_SEP_CAR, + }; use crate::unvalidated::{ payload, tests::{ CACAO_SIGNED_DATA_EVENT_CAR, DATA_EVENT_CAR_UNSIGNED_INIT, SIGNED_DATA_EVENT_CAR, - SIGNED_INIT_EVENT_CAR, TIME_EVENT_CAR, UNSIGNED_INIT_EVENT_CAR, + SIGNED_INIT_EVENT_CAR, TIME_EVENT_CAR_SINGLE_EVENT_BATCH, UNSIGNED_INIT_EVENT_CAR, }, Builder, Event, }; @@ -448,8 +450,12 @@ mod tests { round_trip(DATA_EVENT_CAR_UNSIGNED_INIT).await; } #[test(tokio::test)] - async fn round_trip_time_event() { - round_trip(TIME_EVENT_CAR).await; + async fn round_trip_time_event_single_event_batch() { + round_trip(TIME_EVENT_CAR_SINGLE_EVENT_BATCH).await; + } + #[test(tokio::test)] + async fn round_trip_time_event_multi_event_batch() { + round_trip(TIME_EVENT_CAR_MULTI_EVENT_BATCH).await; } #[test(tokio::test)] async fn round_trip_init_payload_with_no_sep() { diff --git a/event/src/unvalidated/mod.rs b/event/src/unvalidated/mod.rs index 9e5dd94ba..32ca87f1a 100644 --- a/event/src/unvalidated/mod.rs +++ b/event/src/unvalidated/mod.rs @@ -49,7 +49,9 @@ mod tests { // Data event signed with a CACAO pub const CACAO_SIGNED_DATA_EVENT_CAR:&str="mO6Jlcm9vdHOB2CpYJgABhQESIN12WhnV8Y3aMRxZYqUSpaO4mSQnbGxEpeC8jMsldwv6Z3ZlcnNpb24BigQBcRIgtP3Gc62zs2I/pu98uctnwBAYUUrgyLjnPaxYwOnBytajYWihYXRnZWlwNDM2MWFwqWNhdWR4OGRpZDprZXk6ejZNa3I0YTNaM0ZGYUpGOFlxV25XblZIcWQ1ZURqWk45YkRTVDZ3VlpDMWhKODFQY2V4cHgYMjAyNC0wNi0xOVQyMDowNDo0Mi40NjRaY2lhdHgYMjAyNC0wNi0xMlQyMDowNDo0Mi40NjRaY2lzc3g7ZGlkOnBraDplaXAxNTU6MToweDM3OTRkNGYwNzdjMDhkOTI1ZmY4ZmY4MjAwMDZiNzM1MzI5OWIyMDBlbm9uY2Vqd1BpQ09jcGtsbGZkb21haW5kdGVzdGd2ZXJzaW9uYTFpcmVzb3VyY2VzgWtjZXJhbWljOi8vKmlzdGF0ZW1lbnR4PEdpdmUgdGhpcyBhcHBsaWNhdGlvbiBhY2Nlc3MgdG8gc29tZSBvZiB5b3VyIGRhdGEgb24gQ2VyYW1pY2FzomFzeIQweGIyNjY5OTkyNjM0NDZkZGI5YmY1ODg4MjVlOWFjMDhiNTQ1ZTY1NWY2MDc3ZThkODU3OWE4ZDY2MzljMTE2N2M1NmY3ZGFlN2FjNzBmN2ZhZWQ4YzE0MWFmOWUxMjRhN2ViNGY3NzQyM2E1NzJiMzYxNDRhZGE4ZWYyMjA2Y2RhMWNhdGZlaXAxOTHTAQFxEiB0pJYyJOf2PmUMzjHeHCaX9ETIA0AKnHWwb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IGNED_INIT_NO_SEP_CAR:&str="uOqJlcm9vdHOB2CpYJQABcRIgrY2L_wTWrzng7Mpf2kvh9Q9Uyz-Ei2CF7NfjzRD0illndmVyc2lvbgG5AwFxEiCtjYv_BNavOeDsyl_aS-H1D1TLP4SLYIXs1-PNEPSKWaJkZGF0YaZkZGF0YaNjdXJsYGVsYWJlbGdGYXN0aW5nbmNoaWxkcmVuSGlkZGVu9GR0eXBlbFF1ZXN0aW9uTm9kZWdjcmVhdGVkeBgyMDIzLTAyLTIwVDE1OjE5OjM2LjI3OVpocG9zaXRpb26iYXj7QKWDkiAAAABheftAtqeAAAAAAGlsYXRlcmFsSUR4JDY5YWFmMzdkLTU1OWItNGI5NS1hMTAwLTVlZTk4MThmYzVlZGlwcm9qZWN0SUR4P2tqemw2a2N5bTd3OHk1ZDY1Zjlva240cml5ZGF0OTJoM3M2dnZ6cHd3dTc1NDRpOTJmanlnYzU2N2x6aHJ2Y2ZoZWFkZXKjZW1vZGVsWCjOAQIBhQESIJbss9X3kzfag-eF6OFx0KDIOV6P5DxOi-cWAWnTN5FjZnVuaXF1ZUwq2_OJxYVaSoawmJtrY29udHJvbGxlcnOBeDtkaWQ6cGtoOmVpcDE1NToxOjB4Y2E1ZmY0YjM0NDJmY2FjMjdlMWFmNDQ1N2UwMmViNjI5YzcxMjk4Mw"; } From c14f28291ea841cd43e8cc2c7f91f327af8c3f24 Mon Sep 17 00:00:00 2001 From: Spencer T Brody Date: Fri, 16 Aug 2024 16:22:36 -0400 Subject: [PATCH 2/2] Add comment why unwrap is safe --- event/src/unvalidated/event.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/event/src/unvalidated/event.rs b/event/src/unvalidated/event.rs index 7ced4e462..d3fc5a643 100644 --- a/event/src/unvalidated/event.rs +++ b/event/src/unvalidated/event.rs @@ -54,6 +54,8 @@ where let parts: Vec<_> = event.path().split('/').collect(); // Add blocks for all parts but the last as it is the prev. for index in parts.iter().take(parts.len() - 1) { + // unwrap() is safe because .last() only returns None if the Vec is empty, and we know + // it has at least one element because we pushed a block before entering the loop. let cid = blocks_in_path .last() .unwrap()