Skip to content

Commit

Permalink
fix: Handle data events with unsigned init events (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody authored and nathanielc committed May 9, 2024
1 parent 76a9c9a commit 8246494
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 35 deletions.
88 changes: 62 additions & 26 deletions api/src/server/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,25 @@ async fn get_init_event_payload(
let init_bytes = get_block(init_id, car_blocks, store).await?;
let init_event: unvalidated::Event<Ipld> =
serde_ipld_dagcbor::from_slice(&init_bytes).context("decoding init event")?;
if let unvalidated::Event::Signed(event) = init_event {
let link = event
.link()
.ok_or_else(|| anyhow!("init event should have a link"))?;
match init_event {
unvalidated::Event::Signed(event) => {
let link = event
.link()
.ok_or_else(|| anyhow!("init event should have a link"))?;

let payload_bytes = get_block(&link, car_blocks, store).await?;
let payload: unvalidated::Payload<Ipld> =
serde_ipld_dagcbor::from_slice(&payload_bytes).context("decoding init payload")?;
if let unvalidated::Payload::Init(payload) = payload {
Ok(payload)
} else {
bail!("init event payload is not well formed")
let payload_bytes = get_block(&link, car_blocks, store).await?;
let payload: unvalidated::Payload<Ipld> =
serde_ipld_dagcbor::from_slice(&payload_bytes).context("decoding init payload")?;
if let unvalidated::Payload::Init(payload) = payload {
Ok(payload)
} else {
bail!("init event payload is not well formed")
}
}
unvalidated::Event::Unsigned(event) => Ok(event),
unvalidated::Event::Time(_) => {
bail!("init event payload can't be a time event")
}
} else {
bail!("init event should be a signed event")
}
}

Expand All @@ -123,8 +127,9 @@ mod tests {
use super::*;

use crate::tests::{
decode_multibase_str, mock_get_init_event, MockAccessModelStoreTest, DATA_EVENT_CAR,
INIT_EVENT_CAR, TIME_EVENT_CAR,
decode_multibase_str, mock_get_init_event, mock_get_unsigned_init_event,
MockAccessModelStoreTest, DATA_EVENT_CAR, DATA_EVENT_CAR_UNSIGNED_INIT,
SIGNED_INIT_EVENT_CAR, TIME_EVENT_CAR, UNSIGNED_INIT_EVENT_CAR,
};
use async_trait::async_trait;
use expect_test::{expect, Expect};
Expand Down Expand Up @@ -172,7 +177,12 @@ mod tests {
)
"#]];
// Init events do not need to access the store
test_event_id_from_car(INIT_EVENT_CAR, expected, MockAccessModelStoreTest::new()).await
test_event_id_from_car(
SIGNED_INIT_EVENT_CAR,
expected,
MockAccessModelStoreTest::new(),
)
.await
}

#[tokio::test]
Expand All @@ -181,33 +191,28 @@ mod tests {
let expected = expect![[r#"
Ok(
EventId {
bytes: "ce0105004bf23f697fcdb44763a8eb5b47190f472416a164017112203130e432a07501b157851b7cc2e30ca2baed822009e02323daf04e4f2416a164",
bytes: "ce010500c703887c2b8374ed63a8eb5b47190f4706aabe66017112200a43060a07ecf21b7d3569c3c67a9e9dabb293e170a2905e1d379fbb06aabe66",
network_id: Some(
0,
),
separator: Some(
"4bf23f697fcdb447",
"c703887c2b8374ed",
),
controller: Some(
"63a8eb5b47190f47",
),
stream_id: Some(
"2416a164",
"06aabe66",
),
cid: Some(
"bafyreibrgdsdfidvagyvpbi3ptbogdfcxlwyeiaj4arshwxqjzhsifvbmq",
"bafyreiakimdaub7m6inx2nljypdhvhu5vozjhylqukif4hjxt65qnkv6my",
),
},
)
"#]];
test_event_id_from_car(
// Unsigned init payload event
"
uOqJlcm9vdHOB2CpYJQABcRIgMTDkMqB1AbFXhRt8wuMMorrtgiAJ4CMj2vBOTyQWoWRndmVyc2lvbgG
0AQFxEiAxMOQyoHUBsVeFG3zC4wyiuu2CIAngIyPa8E5PJBahZKJkZGF0YfZmaGVhZGVyo2NzZXBlbW9
kZWxlbW9kZWxYKM4BAgGFARIgV1HMaNjwORnyUJjowofErLoZ5HkFm-5dsy3v2onQm6NrY29udHJvbGx
lcnOBeDhkaWQ6a2V5Ono2TWt0Q0ZSY3dMUkZRQTlXYmVEUk03VzdrYkJkWlRIUTJ4blBneXhaTHExZ0N
wSw",
UNSIGNED_INIT_EVENT_CAR,
expected,
// Init events do not need to access the store
MockAccessModelStoreTest::new(),
Expand Down Expand Up @@ -244,6 +249,37 @@ mod tests {
mock_get_init_event(&mut mock_model);
test_event_id_from_car(DATA_EVENT_CAR, expected, mock_model).await
}

#[tokio::test]
#[traced_test]
async fn event_id_from_car_data_event_unsigned_init() {
let expected = expect![[r#"
Ok(
EventId {
bytes: "ce010500c703887c2b8374ed63a8eb5b47190f4706aabe6601850112200953f8c9dd5669e2f638b04ba24fb57b6f6006b5fb8b63aeea8b7ed33a071bd3",
network_id: Some(
0,
),
separator: Some(
"c703887c2b8374ed",
),
controller: Some(
"63a8eb5b47190f47",
),
stream_id: Some(
"06aabe66",
),
cid: Some(
"bagcqcerabfj7rso5kzu6f5rywbf2et5vpnxwabvv7ofwhlxkrn7ngoqhdpjq",
),
},
)
"#]];
let mut mock_model = MockAccessModelStoreTest::new();
mock_get_unsigned_init_event(&mut mock_model);
test_event_id_from_car(DATA_EVENT_CAR_UNSIGNED_INIT, expected, mock_model).await
}

#[tokio::test]
#[traced_test]
async fn event_id_from_car_time_event() {
Expand Down
44 changes: 35 additions & 9 deletions api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use tracing_test::traced_test;

struct Context;

pub const INIT_EVENT_CID: &str = "bagcqcerar2aga7747dm6fota3iipogz4q55gkaamcx2weebs6emvtvie2oha";
pub const INIT_EVENT_PAYLOAD_CID: &str =
pub const SIGNED_INIT_EVENT_CID: &str =
"bagcqcerar2aga7747dm6fota3iipogz4q55gkaamcx2weebs6emvtvie2oha";
pub const SIGNED_INIT_EVENT_PAYLOAD_CID: &str =
"bafyreiaroclcgqih242byss6pneufencrulmeex2ttfdzefst67agwq3im";
pub const INIT_EVENT_CAR: &str = "
pub const SIGNED_INIT_EVENT_CAR: &str = "
uO6Jlcm9vdHOB2CpYJgABhQESII6AYH_8-NniumDaEPcbPId6ZQAMFfViEDLxGVnVBNOOZ3ZlcnNpb24
B0QEBcRIgEXCWI0EH1zQcSl57SUKRoo0WwhL6nMo8kLKfvgNaG0OiZGRhdGGhZXN0ZXBoGQFNZmhlYWR
lcqRjc2VwZW1vZGVsZW1vZGVsWCjOAQIBhQESIKDoMqM144vTQLQ6DwKZvzxRWg_DPeTNeRCkPouTHo1
Expand All @@ -37,17 +38,26 @@ pub const INIT_EVENT_CAR: &str = "
WdGhiaXlTY21mdThuNVY3Ym9YZ3h5bzVxM1NaUlIifWlzaWduYXR1cmVYQCQDjlx8fT8rbTR4088HtOE
27LJMc38DSuf1_XtK14hDp1Q6vhHqnuiobqp5EqNOp0vNFCCzwgG-Dsjmes9jJww";

pub const INIT_EVENT: &str = "
pub const SIGNED_INIT_EVENT: &str = "
uomdwYXlsb2FkWCQBcRIgEXCWI0EH1zQcSl57SUKRoo0WwhL6nMo8kLKfvgNaG0Nqc2lnbmF0dXJlc4G
iaXByb3RlY3RlZFiBeyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDprZXk6ejZNa3RCeW5BUExyRXllUzd
wVnRoYml5U2NtZnU4bjVWN2JvWGd4eW81cTNTWlJSI3o2TWt0QnluQVBMckV5ZVM3cFZ0aGJpeVNjbWZ
1OG41Vjdib1hneHlvNXEzU1pSUiJ9aXNpZ25hdHVyZVhAJAOOXHx9PyttNHjTzwe04TbsskxzfwNK5_X
9e0rXiEOnVDq-Eeqe6KhuqnkSo06nS80UILPCAb4OyOZ6z2MnDA";
pub const INIT_EVENT_PAYLOAD: &str = "
pub const SIGNED_INIT_EVENT_PAYLOAD: &str = "
uomRkYXRhoWVzdGVwaBkBTWZoZWFkZXKkY3NlcGVtb2RlbGVtb2RlbFgozgECAYUBEiCg6DKjNeOL00C
0Og8Cmb88UVoPwz3kzXkQpD6Lkx6NWGZ1bmlxdWVMRKbxOrJBC7tqhWjea2NvbnRyb2xsZXJzgXg4ZGl
kOmtleTp6Nk1rdEJ5bkFQTHJFeWVTN3BWdGhiaXlTY21mdThuNVY3Ym9YZ3h5bzVxM1NaUlI";

pub const UNSIGNED_INIT_EVENT_CID: &str =
"bafyreiakimdaub7m6inx2nljypdhvhu5vozjhylqukif4hjxt65qnkv6my";

pub const UNSIGNED_INIT_EVENT_CAR: &str = "
uOqJlcm9vdHOB2CpYJQABcRIgCkMGCgfs8ht9NWnDxnqenauyk-FwopBeHTefuwaqvmZndmVyc2lvbgHDAQFxEiAKQwYKB-zyG301acPGep6dq7KT4XCikF4dN5-7Bqq-ZqJkZGF0YfZmaGVhZGVypGNzZXBlbW9kZWxlbW9kZWxYKM4BAgGFARIghHTHRYxxeQXgc9Q6LUJVelzW5bnrw9TWgoBJlBIOVtdmdW5pcXVlR2Zvb3xiYXJrY29udHJvbGxlcnOBeDhkaWQ6a2V5Ono2TWt0Q0ZSY3dMUkZRQTlXYmVEUk03VzdrYkJkWlRIUTJ4blBneXhaTHExZ0NwSw";

pub const UNSIGNED_INIT_EVENT_PAYLOAD: &str = "uomRkYXRh9mZoZWFkZXKkY3NlcGVtb2RlbGVtb2RlbFgozgECAYUBEiCEdMdFjHF5BeBz1DotQlV6XNbluevD1NaCgEmUEg5W12Z1bmlxdWVHZm9vfGJhcmtjb250cm9sbGVyc4F4OGRpZDprZXk6ejZNa3RDRlJjd0xSRlFBOVdiZURSTTdXN2tiQmRaVEhRMnhuUGd5eFpMcTFnQ3BL";

// Data Event for a stream with a signed init event
pub const DATA_EVENT_CAR: &str = "
uO6Jlcm9vdHOB2CpYJgABhQESICddBxl5Sk2e7I20pzX9kDLf0jj6WvIQ1KqbM3WQiClDZ3ZlcnNpb24
BqAEBcRIgdtssXEgR7sXQQQA1doBpxUpTn4pcAaVFZfQjyo-03SGjYmlk2CpYJgABhQESII6AYH_8-Nn
Expand All @@ -64,6 +74,10 @@ pub const DATA_EVENT_CAR: &str = "
pub const DATA_EVENT_ID: &str =
"ce010500aa5773c7d75777e1deb6cb4af0e69eebd504d38e0185011220275d0719794a4d9eec8db4a735fd9032dfd238fa5af210d4aa9b337590882943";

// Data Event for a stream with an unsigned init event
pub const DATA_EVENT_CAR_UNSIGNED_INIT: &str = "
uO6Jlcm9vdHOB2CpYJgABhQESIAlT-MndVmni9jiwS6JPtXtvYAa1-4tjruqLftM6BxvTZ3ZlcnNpb24B-gEBcRIguZ-ORAzcRLjL2LKcFJX2lC3Cv_4bywuG4Q8gEc5dbYajYmlk2CpYJQABcRIgCkMGCgfs8ht9NWnDxnqenauyk-FwopBeHTefuwaqvmZkZGF0YYSjYm9wY2FkZGRwYXRoZC9vbmVldmFsdWVjZm9vo2JvcGNhZGRkcGF0aGQvdHdvZXZhbHVlY2JhcqNib3BjYWRkZHBhdGhmL3RocmVlZXZhbHVlZmZvb2JhcqNib3BjYWRkZHBhdGhnL215RGF0YWV2YWx1ZQFkcHJldtgqWCUAAXESIApDBgoH7PIbfTVpw8Z6np2rspPhcKKQXh03n7sGqr5mugIBhQESIAlT-MndVmni9jiwS6JPtXtvYAa1-4tjruqLftM6BxvTomdwYXlsb2FkWCQBcRIguZ-ORAzcRLjL2LKcFJX2lC3Cv_4bywuG4Q8gEc5dbYZqc2lnbmF0dXJlc4GiaXByb3RlY3RlZFiBeyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDprZXk6ejZNa3RDRlJjd0xSRlFBOVdiZURSTTdXN2tiQmRaVEhRMnhuUGd5eFpMcTFnQ3BLI3o2TWt0Q0ZSY3dMUkZRQTlXYmVEUk03VzdrYkJkWlRIUTJ4blBneXhaTHExZ0NwSyJ9aXNpZ25hdHVyZVhAZSJEw5QkFrYhbLYdLgnBn5SIbGAgm5i2jHhntWwe8nDkyKcCu4OvLMvFyGpjPloYVOr0JKwXlQfbgccHtbJpDw";

pub const TIME_EVENT_CAR: &str = "
uOqJlcm9vdHOB2CpYJQABcRIgcmqgb7eHSgQ32hS1NGVKZruLJGcKDI1f4lqOyNYn3eVndmVyc2lvbgG
3AQFxEiByaqBvt4dKBDfaFLU0ZUpmu4skZwoMjV_iWo7I1ifd5aRiaWTYKlgmAAGFARIgjoBgf_z42eK
Expand Down Expand Up @@ -151,17 +165,29 @@ pub fn mock_get_init_event(mock_store: &mut MockAccessModelStoreTest) {
mock_store
.expect_get_block()
.once()
.with(predicate::eq(Cid::from_str(INIT_EVENT_CID).unwrap()))
.return_once(move |_| Ok(Some(decode_multibase_str(INIT_EVENT))));
.with(predicate::eq(Cid::from_str(SIGNED_INIT_EVENT_CID).unwrap()))
.return_once(move |_| Ok(Some(decode_multibase_str(SIGNED_INIT_EVENT))));

// Call to get the init event payload
mock_store
.expect_get_block()
.once()
.with(predicate::eq(
Cid::from_str(SIGNED_INIT_EVENT_PAYLOAD_CID).unwrap(),
))
.return_once(move |_| Ok(Some(decode_multibase_str(SIGNED_INIT_EVENT_PAYLOAD))));
}

/// Given a mock of the AccessModelStore, prepare it to expect calls to load the unsigned init event.
pub fn mock_get_unsigned_init_event(mock_store: &mut MockAccessModelStoreTest) {
// Call to get the init event payload
mock_store
.expect_get_block()
.once()
.with(predicate::eq(
Cid::from_str(INIT_EVENT_PAYLOAD_CID).unwrap(),
Cid::from_str(UNSIGNED_INIT_EVENT_CID).unwrap(),
))
.return_once(move |_| Ok(Some(decode_multibase_str(INIT_EVENT_PAYLOAD))));
.return_once(move |_| Ok(Some(decode_multibase_str(UNSIGNED_INIT_EVENT_PAYLOAD))));
}

#[tokio::test]
Expand Down

0 comments on commit 8246494

Please sign in to comment.