Skip to content
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

WIP: Nut-17 #394

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
crodas committed Oct 9, 2024
commit ded8ec002297f1e2bd25c7ade800746736451ede
56 changes: 56 additions & 0 deletions crates/cdk/src/nuts/nut17.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl Indexable for SubscriptionResponse {
}

#[derive(Debug, Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]

/// Kind
pub enum Kind {
///
Expand Down Expand Up @@ -175,4 +177,58 @@ mod test {
assert!(subscriptions[0].try_recv().is_err());
assert!(subscriptions[1].try_recv().is_err());
}

#[test]
fn parsing_request() {
let json = r#"{"kind":"proof_state","filters":["x"],"subId":"uno"}"#;
let params: Params = serde_json::from_str(json).expect("valid json");
assert_eq!(params.kind, Kind::ProofState);
assert_eq!(params.filters, vec!["x"]);
assert_eq!(*params.id, "uno");
}

#[tokio::test]
async fn json_test() {
let manager = Manager::default();
let mut subscription = manager
.subscribe::<Params>(
serde_json::from_str(r#"{"kind":"proof_state","filters":["02194603ffa36356f4a56b7df9371fc3192472351453ec7398b8da8117e7c3e104"],"subId":"uno"}"#)
.expect("valid json"),
)
.await;

manager.broadcast(
ProofState {
y: PublicKey::from_hex(
"02194603ffa36356f4a56b7df9371fc3192472351453ec7398b8da8117e7c3e104",
)
.expect("valid pk"),
state: State::Pending,
witness: None,
}
.into(),
);

// no one is listening for this event
manager.broadcast(
ProofState {
y: PublicKey::from_hex(
"020000000000000000000000000000000000000000000000000000000000000001",
)
.expect("valid pk"),
state: State::Pending,
witness: None,
}
.into(),
);

sleep(Duration::from_millis(10)).await;
let (sub1, msg) = subscription.try_recv().expect("valid message");
assert_eq!("uno", *sub1);
assert_eq!(
r#"{"Y":"02194603ffa36356f4a56b7df9371fc3192472351453ec7398b8da8117e7c3e104","state":"PENDING","witness":null}"#,
serde_json::to_string(&msg).expect("valid json")
);
assert!(subscription.try_recv().is_err());
}
}
2 changes: 0 additions & 2 deletions crates/cdk/src/subscription/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ where
async fn broadcast_impl(storage: &IndexTree<T, I>, event: T) {
let index_storage = storage.read().await;
for index in event.to_indexes() {
println!("{:?}", index);
println!("{:?}", index_storage.keys().collect::<Vec<_>>());
for (key, sender) in index_storage.range(index.clone()..) {
if index.cmp_prefix(&key) != Ordering::Equal {
break;
Expand Down