From ee02dafcc5f0209e39a056b5dffc9bbff055a3d1 Mon Sep 17 00:00:00 2001 From: yoduyodu Date: Fri, 9 Feb 2024 11:53:24 -0800 Subject: [PATCH] add result + test failure --- gateway-types/src/lib.rs | 7 +++++ messaging/src/lib.rs | 16 +++++++--- xps-gateway/src/rpc/api.rs | 4 +-- xps-gateway/src/rpc/methods.rs | 4 +-- xps-gateway/tests/integration_test.rs | 42 +++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-) diff --git a/gateway-types/src/lib.rs b/gateway-types/src/lib.rs index 18a82e6..fd114e5 100644 --- a/gateway-types/src/lib.rs +++ b/gateway-types/src/lib.rs @@ -48,6 +48,13 @@ pub struct GrantInstallationResult { pub transaction: String, } +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub struct SendMessageResult { + pub status: Status, + pub message: String, + pub transaction: String, +} + #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct KeyPackageResult { /// Status of the operation diff --git a/messaging/src/lib.rs b/messaging/src/lib.rs index a980ebe..e9d6e44 100644 --- a/messaging/src/lib.rs +++ b/messaging/src/lib.rs @@ -11,7 +11,7 @@ use ethers::{ types::H256, utils::keccak256, }; -use gateway_types::{error::ExtSignerError, Message}; +use gateway_types::{error::ExtSignerError, Message, SendMessageResult, Status}; abigen!( Conversation, @@ -32,8 +32,12 @@ where Self { contract } } - pub async fn send_message(&self, m: Message) -> Result<(), MessagingOperationError> { - self.contract + pub async fn send_message( + &self, + m: Message, + ) -> Result> { + let transaction_receipt = self + .contract .send_message_signed( m.conversation_id, m.payload, @@ -45,7 +49,11 @@ where .send() .await? .await?; - Ok(()) + Ok(SendMessageResult { + status: Status::Success, + message: "Message sent.".to_string(), + transaction: transaction_receipt.unwrap().transaction_hash.to_string(), + }) } } diff --git a/xps-gateway/src/rpc/api.rs b/xps-gateway/src/rpc/api.rs index fb2e193..8db35f3 100644 --- a/xps-gateway/src/rpc/api.rs +++ b/xps-gateway/src/rpc/api.rs @@ -4,8 +4,8 @@ use ethers::core::types::Signature; use ethers::prelude::*; use jsonrpsee::{proc_macros::rpc, types::ErrorObjectOwned}; -use gateway_types::Message; use gateway_types::{GrantInstallationResult, KeyPackageResult}; +use gateway_types::{Message, SendMessageResult}; use lib_didethresolver::types::XmtpAttribute; /// XPS JSON-RPC Interface Methods @@ -131,7 +131,7 @@ pub trait Xps { /// } /// ``` #[method(name = "sendMessage")] - async fn send_message(&self, _message: Message) -> Result<(), ErrorObjectOwned>; + async fn send_message(&self, _message: Message) -> Result; /// # Documentation for JSON RPC Endpoint: `grantInstallation` /// diff --git a/xps-gateway/src/rpc/methods.rs b/xps-gateway/src/rpc/methods.rs index 589d8ec..20b41c7 100644 --- a/xps-gateway/src/rpc/methods.rs +++ b/xps-gateway/src/rpc/methods.rs @@ -7,7 +7,7 @@ use super::api::*; use async_trait::async_trait; use ethers::prelude::*; use ethers::{core::types::Signature, providers::Middleware}; -use gateway_types::{GrantInstallationResult, KeyPackageResult}; +use gateway_types::{GrantInstallationResult, KeyPackageResult, SendMessageResult}; use jsonrpsee::types::ErrorObjectOwned; use lib_didethresolver::types::XmtpAttribute; use messaging::MessagingOperations; @@ -44,7 +44,7 @@ impl XpsMethods

{ #[async_trait] impl XpsServer for XpsMethods

{ - async fn send_message(&self, message: Message) -> Result<(), ErrorObjectOwned> { + async fn send_message(&self, message: Message) -> Result { let result = self .message_operations .send_message(message) diff --git a/xps-gateway/tests/integration_test.rs b/xps-gateway/tests/integration_test.rs index b38bf9b..aa5e578 100644 --- a/xps-gateway/tests/integration_test.rs +++ b/xps-gateway/tests/integration_test.rs @@ -62,7 +62,9 @@ async fn test_send_message() -> Result<(), Error> { let result = client.send_message(message).await; assert!(result.is_ok()); + assert!(result.unwrap().status == Status::Success); + // post-nonce should be same as pre-nonce + 1 let post_nonce = context.conversation.nonce(me.address()).call().await?; assert!(post_nonce == pre_nonce + 1); Ok(()) @@ -70,6 +72,46 @@ async fn test_send_message() -> Result<(), Error> { .await } +#[tokio::test] +async fn test_send_message_fail() -> Result<(), Error> { + with_xps_client(None, |client, context, _resolver, anvil| async move { + let wallet: LocalWallet = anvil.keys()[3].clone().into(); + let me = get_user(&anvil, 3).await; + + let conversation_id = keccak256(b"conversation_id"); + let payload = Bytes::from_static(b"payload"); + + let signature = wallet + .sign_xmtp_message( + &context.conversation, + keccak256(b"unmatched_conversation_id"), + payload.clone(), + me.address(), + ) + .await?; + + let message = Message { + conversation_id: conversation_id, + payload: payload, + identity: me.address(), + signature: signature, + }; + + let pre_nonce = context.conversation.nonce(me.address()).call().await?; + assert!(pre_nonce == U256::zero()); + + let result = client.send_message(message).await; + assert!(result.is_err()); + println!("{:?}", result.err()); + + // post-nonce should be same as pre-nonce + let post_nonce = context.conversation.nonce(me.address()).call().await?; + assert!(post_nonce == pre_nonce); + Ok(()) + }) + .await +} + #[tokio::test] async fn test_wallet_address() -> Result<(), Error> { with_xps_client(None, |client, _, _, _| async move {