Skip to content

Commit

Permalink
how useful is this
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Jan 30, 2024
1 parent 8b0b2ec commit 5d68b62
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 101 deletions.
34 changes: 6 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ async-trait = "0.1"
jsonrpsee = { version = "0.21", features = ["macros", "server", "client-core"] }
anyhow = "1.0"
thiserror = "1.0"
ethers = { version = "2.0.11", features = ["abigen"] }
ethers = { path = "../../../../gakonst/ethers-rs/ethers", features = ["abigen"] }
ctor = "0.2"
lib-didethresolver = { git = "https://github.com/xmtp/didethresolver", package = "lib-didethresolver", branch = "main" }
gateway-types = { path = "./gateway-types" }
rustc-hex = "2.1"
hex = "0.4"

[patch.crates-io]
ethers = { path = "../../../../gakonst/ethers-rs/ethers", features = ["abigen"] }

24 changes: 12 additions & 12 deletions registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ where
String::from_utf8_lossy(&attribute)
);

self.registry
.revoke_attribute_signed(
address,
signature.v.try_into()?,
signature.r.into(),
signature.s.into(),
attribute,
value.into(),
)
.send()
.await?
.await?;
let res = self.registry.revoke_attribute_signed(
address,
signature.v.try_into()?,
signature.r.into(),
signature.s.into(),
attribute,
value.into(),
);

let res = res.send().await;
println!("{:?}", res);
res?.await?;

Ok(())
}
Expand Down
103 changes: 57 additions & 46 deletions xps-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,18 @@ pub async fn run(host: String, port: u16) -> Result<()> {
#[cfg(test)]
mod test {
use ethers::{
abi::AbiEncode,
prelude::{Transaction, TransactionReceipt},
providers::MockProvider,
types::{Block, FeeHistory, TxHash, U256, U64},
};
use serde::Serialize;
use std::borrow::Borrow;

fn setup_mock_tx(mock: &mut MockProvider) {
mock.push(TransactionReceipt::default()).unwrap(); // eth_getTransactionReceipt
mock.push(Transaction {
block_number: Some(1.into()),
..Transaction::default()
})
.unwrap(); // eth_getTransaction
mock.push(TxHash::default()).unwrap(); // eth_sendTransaction
mock.push(U64::from(0)).unwrap(); // eth_estimateGas
mock.push(U64::from(0)).unwrap(); // eth_GasPrice
}

pub trait MockProviderExt {
/// Set the response for a call to a contract
/// This must be called for each transaction that a function might send.
///
///
/// # Example
/// ```
/// use ethers::{
Expand All @@ -81,45 +70,69 @@ mod test {
/// let to = Address::from_str("0x7e575682a8e450e33eb0493f9972821ae333cd7f").unwrap();
/// let from = Address::from_str("0x0000000000000000000000000000000000000000").unwrap();
/// let tx = TransactionRequest::new().to(to).value(1000).from(from);
/// mock.set_transaction_response(None::<()>);
/// mock.set_transaction_response(TransactionReceipt::default());
/// let pending = provider.send_transaction(tx, None).await.unwrap().await.unwrap();
/// ```
fn set_transaction_response<T: Serialize + Send + Sync, R: Borrow<T>>(
&mut self,
response: Option<R>,
);
fn set_transaction_response(&mut self, response: TransactionReceipt);

/// Set the response for a transaction to a Contract
fn set_contract_response<T: Serialize + Send + Sync, R: Borrow<T>>(
&mut self,
response: Option<R>,
);
///
/// # Example
/// ```
///
/// let (context, mut mock) = GatewayContext::mocked().await;
/// let methods = XpsMethods::new(&context);
/// let attr = XmtpAttribute {
/// encoding: KeyEncoding::Hex,
/// purpose: XmtpKeyPurpose::Installation,
/// };
/// let value = vec![0x01, 0x02, 0x03];
/// mock.set_contract_response(Default::default());
/// let res = methods
/// .revoke_installation(
/// Address::default().to_string(),
/// attr,
/// value,
/// Signature {
/// r: [0x01; 32].into(),
/// s: [0x02; 32].into(),
/// v: 0x01,
/// },
/// )
/// .await;
/// ```
fn set_contract_response(&mut self, response: TransactionReceipt);

/// Set the response for a call to a contract
fn set_call_response<T: Serialize + Send + Sync, R: Borrow<T>>(
&mut self,
response: Option<R>,
) {
todo!()
}
///
/// # Example
///
/// ```
/// let (context, mut mock) = GatewayContext::mocked().await;
/// let registry = DIDRegistry::new(Address::default(), context.signer.clone());
/// mock.set_call_response(ChangedReturn(U256::zero()));
/// registry.changed(Address::default()).call().await.unwrap();
///
/// ```
///
fn set_call_response<T: Serialize + Send + Sync + AbiEncode>(&mut self, response: T);
}

impl MockProviderExt for MockProvider {
fn set_transaction_response<T: Serialize + Send + Sync, R: Borrow<T>>(
&mut self,
response: Option<R>,
) {
if let Some(r) = response {
self.push(r).unwrap();
}
setup_mock_tx(self);
fn set_transaction_response(&mut self, response: TransactionReceipt) {
self.push(response).unwrap();
self.push(Transaction {
block_number: Some(1.into()),
..Transaction::default()
})
.unwrap(); // eth_getTransaction
self.push(TxHash::default()).unwrap(); // eth_sendTransaction
self.push(U64::from(0)).unwrap(); // eth_estimateGas
self.push(U64::from(0)).unwrap(); // eth_GasPrice
}

fn set_contract_response<T: Serialize + Send + Sync, R: Borrow<T>>(
&mut self,
response: Option<R>,
) {
self.push(TransactionReceipt::default()).unwrap(); // eth_getTransactionReceipt
fn set_contract_response(&mut self, response: TransactionReceipt) {
self.push(response).unwrap();
self.push(Transaction {
block_number: Some(1.into()),
..Transaction::default()
Expand All @@ -138,11 +151,9 @@ mod test {
self.push(U64::from(0)).unwrap(); // transactionCount
}

fn set_call_response<T: Serialize + Send + Sync, R: Borrow<T>>(
&mut self,
response: Option<R>,
) {
todo!()
fn set_call_response<T: Serialize + Send + Sync + AbiEncode>(&mut self, response: T) {
self.push::<String, &String>(&response.encode_hex())
.unwrap();
}
}

Expand Down
38 changes: 37 additions & 1 deletion xps-gateway/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ethers::{core::types::Signature, providers::Middleware};
use gateway_types::GrantInstallationResult;
use jsonrpsee::types::ErrorObjectOwned;
use lib_didethresolver::types::XmtpAttribute;

use rand::{rngs::StdRng, SeedableRng};
use std::sync::Arc;
use thiserror::Error;
Expand Down Expand Up @@ -100,6 +101,7 @@ enum RpcError<M: Middleware> {

impl<M: Middleware> From<RpcError<M>> for ErrorObjectOwned {
fn from(error: RpcError<M>) -> Self {
println!("{:?}", error);
match error {
RpcError::ContactOperation(c) => {
ErrorObjectOwned::owned(-31999, c.to_string(), None::<()>)
Expand All @@ -111,6 +113,7 @@ impl<M: Middleware> From<RpcError<M>> for ErrorObjectOwned {
#[cfg(test)]
mod tests {
use crate::test::MockProviderExt;
use ethers::providers::MockResponse;
use lib_didethresolver::types::{KeyEncoding, XmtpKeyPurpose};

use super::*;
Expand Down Expand Up @@ -140,7 +143,40 @@ mod tests {
};
let value = vec![0x01, 0x02, 0x03];

mock.set_contract_response(None::<()>);
mock.set_contract_response(Default::default());

let res = methods
.revoke_installation(
"0x7e575682a8e450e33eb0493f9972821ae333cd7f".to_string(),
attr,
value,
Signature {
r: [0x01; 32].into(),
s: [0x02; 32].into(),
v: 0x01,
},
)
.await;
assert!(res.is_ok());
}

#[tokio::test]
async fn test_rpc_revoke_installation_error() {
let (context, mut mock) = GatewayContext::mocked().await;

let methods = XpsMethods::new(&context);

let attr = XmtpAttribute {
encoding: KeyEncoding::Hex,
purpose: XmtpKeyPurpose::Installation,
};
let value = vec![0x01, 0x02, 0x03];

mock.push_response(MockResponse::Error(JsonRpcError {
code: -32000,
message: "VM Exception while processing transaction: revert".to_string(),
data: None,
}));

let res = methods
.revoke_installation(
Expand Down
Loading

0 comments on commit 5d68b62

Please sign in to comment.