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

feat: publish verify attestation #82

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async fn main() -> anyhow::Result<()> {
.publish(
topic.clone(),
message.clone(),
None,
1100,
Duration::from_secs(30),
false,
Expand Down
1 change: 1 addition & 0 deletions examples/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ async fn main() -> anyhow::Result<()> {
.publish(
topic.clone(),
message.clone(),
None,
1100,
Duration::from_secs(30),
false,
Expand Down
1 change: 1 addition & 0 deletions examples/websocket_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async fn main() -> anyhow::Result<()> {
.publish(
topic.clone(),
Arc::from("Hello WalletConnect!"),
None,
0,
Duration::from_secs(60),
false,
Expand Down
2 changes: 2 additions & 0 deletions relay_client/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl Client {
&self,
topic: Topic,
message: impl Into<Arc<str>>,
attestation: impl Into<Option<Arc<str>>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option<impl T> is generally better for a public API, as Into<Option<A>> for Option<B> is usually not implemented.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking is that if you already have an Arc<str>, then you'd be able to omit Some(), but in practice that's probably never gonna happen.

I think the conversation to have here is why are we still sticking to Arc<str>, even though they likely don't have any significant performance benefits, and only hurt the ergonomics of the APIs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the alternative to Arc<str>? I think it's good to have basically free cloning

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice we rarely clone these strings.

The downsides to using Arc<str> is double allocation on deserialization (it essentially first deserializes into String, and then converts that to Arc<str>, if I'm not mistaken) and the ergonomics of working with it (converting String to Arc<str> is another allocation).

An alternative is using regular String. The cloning cost is mostly mitigated by using a fast allocator like jemalloc or mimalloc.

It's hard to say definitively what would be more performant for the relay, but in some cases (a lot of highly multi-threaded cloning) using String is a lot faster, since allocators would use essentially thread-local memory arenas with minimal locking, while Arc<str> is using atomics under the hood for synchronization. And even though atomic usage is cheap, it's not free.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double allocation on deserialization (it essentially first deserializes into String, and then converts that to Arc, if I'm not mistaken)

Oh wow I didn't know about that. Yeah that looks accurate

Copy link
Contributor

@xDarksome xDarksome Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's the case, seems like it initially deserializes it into String, then converts it into Box<str> and then into Arc<str>

The reallocation can only happen if the underlying Vec shrinks (https://doc.rust-lang.org/std/vec/struct.Vec.html#method.shrink_to_fit) and I assume that's not the case, because the original String should have the exact size needed.

However, it may depend on the deserializer.

tag: u32,
ttl: Duration,
prompt: bool,
Expand All @@ -120,6 +121,7 @@ impl Client {
self.request(rpc::Publish {
topic,
message: message.into(),
attestation: attestation.into(),
ttl_secs,
tag,
prompt,
Expand Down
2 changes: 2 additions & 0 deletions relay_client/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@ impl Client {
&self,
topic: Topic,
message: impl Into<Arc<str>>,
attestation: impl Into<Option<Arc<str>>>,
tag: u32,
ttl: Duration,
prompt: bool,
) -> EmptyResponseFuture<Publish> {
let (request, response) = create_request(Publish {
topic,
message: message.into(),
attestation: attestation.into(),
ttl_secs: ttl.as_secs() as u32,
tag,
prompt,
Expand Down
7 changes: 7 additions & 0 deletions relay_rpc/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ pub struct Publish {
/// Message to publish.
pub message: Arc<str>,

#[serde(default, skip_serializing_if = "is_default")]
pub attestation: Option<Arc<str>>,

/// Duration for which the message should be kept in the mailbox if it can't
/// be delivered, in seconds.
#[serde(rename = "ttl")]
Expand Down Expand Up @@ -556,6 +559,7 @@ impl Publish {
data: SubscriptionData {
topic: self.topic.clone(),
message: self.message.clone(),
attestation: self.attestation.clone(),
published_at,
tag: self.tag,
},
Expand Down Expand Up @@ -728,6 +732,9 @@ pub struct SubscriptionData {
/// The message for the subscription.
pub message: Arc<str>,

#[serde(default, skip_serializing_if = "is_default")]
pub attestation: Option<Arc<str>>,

/// Message publish timestamp in UTC milliseconds.
pub published_at: i64,

Expand Down
14 changes: 11 additions & 3 deletions relay_rpc/src/rpc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn request() {
Params::Publish(Publish {
topic: "topic".into(),
message: "payload".into(),
attestation: Some(Arc::from("attestation_payload")),
ttl_secs: 12,
tag: 0,
prompt: false,
Expand All @@ -17,7 +18,7 @@ fn request() {

assert_eq!(
&serialized,
r#"{"id":1,"jsonrpc":"2.0","method":"irn_publish","params":{"topic":"topic","message":"payload","ttl":12,"tag":0}}"#
r#"{"id":1,"jsonrpc":"2.0","method":"irn_publish","params":{"topic":"topic","message":"payload","attestation":"attestation_payload","ttl":12,"tag":0}}"#
);

let deserialized: Payload = serde_json::from_str(&serialized).unwrap();
Expand Down Expand Up @@ -91,6 +92,7 @@ fn subscription() {
let data = SubscriptionData {
topic: "test_topic".into(),
message: "test_message".into(),
attestation: Some(Arc::from("test_attestation")),
published_at: 123,
tag: 1000,
};
Expand All @@ -104,7 +106,7 @@ fn subscription() {

assert_eq!(
&serialized,
r#"{"id":1,"jsonrpc":"2.0","method":"irn_subscription","params":{"id":"test_id","data":{"topic":"test_topic","message":"test_message","publishedAt":123,"tag":1000}}}"#
r#"{"id":1,"jsonrpc":"2.0","method":"irn_subscription","params":{"id":"test_id","data":{"topic":"test_topic","message":"test_message","attestation":"test_attestation","publishedAt":123,"tag":1000}}}"#
);

let deserialized: Payload = serde_json::from_str(&serialized).unwrap();
Expand All @@ -127,7 +129,6 @@ fn batch_receive() {
));

let serialized = serde_json::to_string(&payload).unwrap();
eprintln!("{serialized}");

assert_eq!(
&serialized,
Expand Down Expand Up @@ -289,6 +290,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: topic.clone(),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand All @@ -303,6 +305,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: topic.clone(),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand All @@ -317,6 +320,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: topic.clone(),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand All @@ -331,6 +335,7 @@ fn validation() {
params: Params::Publish(Publish {
topic: Topic::from("invalid"),
message: message.clone(),
attestation: None,
ttl_secs: 0,
tag: 0,
prompt: false,
Expand Down Expand Up @@ -407,6 +412,7 @@ fn validation() {
data: SubscriptionData {
topic: topic.clone(),
message: message.clone(),
attestation: None,
published_at: 123,
tag: 1000,
},
Expand All @@ -423,6 +429,7 @@ fn validation() {
data: SubscriptionData {
topic: topic.clone(),
message: message.clone(),
attestation: None,
published_at: 123,
tag: 1000,
},
Expand All @@ -439,6 +446,7 @@ fn validation() {
data: SubscriptionData {
topic: Topic::from("invalid"),
message,
attestation: None,
published_at: 123,
tag: 1000,
},
Expand Down
5 changes: 4 additions & 1 deletion relay_rpc/src/rpc/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub struct WatchEventPayload {
pub topic: Topic,
/// The published message.
pub message: Arc<str>,
/// The Verify attestation JWT.
pub attestation: Option<Arc<str>>,
/// Message publishing timestamp.
pub published_at: i64,
/// Message tag.
Expand Down Expand Up @@ -224,6 +226,7 @@ mod test {
status: WatchStatus::Accepted,
topic,
message: Arc::from("test message"),
attestation: Some(Arc::from("test attestation")),
published_at: iat.timestamp(),
tag: 1100,
},
Expand All @@ -233,7 +236,7 @@ mod test {
// lowercase.
assert_eq!(
serde_json::to_string(&claims).unwrap(),
r#"{"iss":"did:key:z6Mku3wsRZTAHjr6xrYWVUfyGeNSNz1GJRVfazp3N76AL9gE","aud":"wss://relay.walletconnect.com","sub":"https://example.com","iat":946684800,"exp":32503680000,"act":"irn_watchEvent","typ":"subscriber","whu":"https://example.com","evt":{"messageId":12345678,"status":"accepted","topic":"474e88153f4db893de42c35e1891dc0e37a02e11961385de0475460fb48b8639","message":"test message","publishedAt":946684800,"tag":1100}}"#
r#"{"iss":"did:key:z6Mku3wsRZTAHjr6xrYWVUfyGeNSNz1GJRVfazp3N76AL9gE","aud":"wss://relay.walletconnect.com","sub":"https://example.com","iat":946684800,"exp":32503680000,"act":"irn_watchEvent","typ":"subscriber","whu":"https://example.com","evt":{"messageId":12345678,"status":"accepted","topic":"474e88153f4db893de42c35e1891dc0e37a02e11961385de0475460fb48b8639","message":"test message","attestation":"test attestation","publishedAt":946684800,"tag":1100}}"#
);

// Verify that the claims can be encoded and decoded correctly.
Expand Down
Loading