Skip to content

Commit

Permalink
add result + test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
37ng committed Feb 9, 2024
1 parent 99ebdcf commit ee02daf
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
7 changes: 7 additions & 0 deletions gateway-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions messaging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check warning on line 17 in messaging/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

messaging/src/lib.rs#L17

Added line #L17 was not covered by tests
Expand All @@ -32,8 +32,12 @@ where
Self { contract }
}

pub async fn send_message(&self, m: Message) -> Result<(), MessagingOperationError<M>> {
self.contract
pub async fn send_message(
&self,
m: Message,
) -> Result<SendMessageResult, MessagingOperationError<M>> {
let transaction_receipt = self
.contract
.send_message_signed(
m.conversation_id,
m.payload,
Expand All @@ -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(),
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions xps-gateway/src/rpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<SendMessageResult, ErrorObjectOwned>;

/// # Documentation for JSON RPC Endpoint: `grantInstallation`
///
Expand Down
4 changes: 2 additions & 2 deletions xps-gateway/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +44,7 @@ impl<P: Middleware> XpsMethods<P> {

#[async_trait]
impl<P: Middleware + 'static> XpsServer for XpsMethods<P> {
async fn send_message(&self, message: Message) -> Result<(), ErrorObjectOwned> {
async fn send_message(&self, message: Message) -> Result<SendMessageResult, ErrorObjectOwned> {
let result = self
.message_operations
.send_message(message)
Expand Down
42 changes: 42 additions & 0 deletions xps-gateway/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,56 @@ 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(())
})
.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 {
Expand Down

0 comments on commit ee02daf

Please sign in to comment.