Skip to content

Commit

Permalink
Update encrypted output api response (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp authored Jul 6, 2023
1 parent edd3563 commit 2bcce85
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
27 changes: 3 additions & 24 deletions crates/relayer-handlers/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,9 @@ Returns encrypted outputs cached by the relayer.
```json
{
"encryptedOutputs": [
[
181, 159, 89, 66, 226, 23, 242, 191, 101, 204, 147, 17,
75, 136, 174, 91, 245, 154, 154, 244, 225, 196, 39, 53,
88, 220, 105, 105, 207, 215, 161, 254, 4, 196, 154, 209,
224, 228, 43, 182, 160, 111, 5, 32, 32, 35, 234, 12,
148, 174, 118, 96, 145, 0, 10, 40, 63, 175, 12, 140,
6, 173, 243, 183, 111, 46, 204, 140, 19, 228, 203, 201,
115, 131, 127, 25, 137, 133, 88, 203, 110, 22, 207, 95,
242, 145, 175, 225, 199, 75, 232, 103, 65, 35, 207, 134,
65, 177, 244, 142,
... 68 more items
],
[
40, 193, 119, 88, 204, 93, 235, 206, 18, 101, 57, 108,
242, 117, 213, 29, 73, 76, 255, 61, 167, 215, 255, 10,
117, 24, 24, 51, 73, 175, 233, 91, 136, 171, 141, 182,
21, 144, 230, 102, 207, 232, 134, 134, 30, 94, 113, 48,
190, 34, 175, 170, 211, 83, 219, 112, 6, 89, 164, 86,
17, 204, 253, 62, 25, 157, 204, 188, 134, 157, 216, 237,
116, 27, 105, 181, 240, 185, 222, 140, 223, 238, 220, 187,
20, 22, 123, 176, 199, 96, 161, 151, 208, 59, 8, 170,
16, 189, 22, 13,
... 68 more items
],
[
"5d7af1ca18064f45e05f4a74191603dc9e6547a848892ca424c0a14f796e33bcc4dbec9c312d50334be022afcdc48c3462e8f3897b18de4a4d9a32d58725c3b3c0aaedb34528ad3e4aa2e788801c798d72726ff8220a848c5e8b80d6d674f37930531a99a0375667ce9a04d0264a9ae32dd0079d6e37b1cefbb3c723787d58bdab1c53dfd868ff2387b7ff61e5ab23f7c99ea8f815feef2debb967fcfae8d23a240ee031d567c80f", "06ac71ded9713e44c9dce1562a280d286ef6acbfe64cb89046ffbc5ee26cf65de7f6c6b1064f94330481eb81ce0718a5d370433b52790281b0fb8c7ca1b09ea9efb9bcc5b06b6231c890e0d6aaa08d5a35e8a3a103bdac1bb6680ef0410ace5f1472955fbe9a87c2c909ef6b65e1da4e714d5c622b465781ea91a40cf3697f873c6e7b7dd7876daec511d34742843720473e6e67aac4f87fc31df1d918be9618cbeb5e124a3a4a6f"
]
],
"lastQueriedBlock": 37
}
Expand Down
2 changes: 1 addition & 1 deletion crates/relayer-handlers/src/routes/encrypted_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use super::OptionalRangeQuery;
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EncryptedOutputsCacheResponse {
encrypted_outputs: Vec<Vec<u8>>,
encrypted_outputs: Vec<String>,
last_queried_block: u64,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/relayer-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ pub trait LeafCacheStore: HistoryStore {
/// getting the encrypted output and insert them with a simple API.
pub trait EncryptedOutputCacheStore: HistoryStore {
/// The Output type which is the encrypted output.
type Output: IntoIterator<Item = Vec<u8>>;
type Output: IntoIterator<Item = String>;

/// Get all the encrypted output for the given key.
fn get_encrypted_output<K: Into<HistoryStoreKey> + Debug>(
Expand Down
6 changes: 4 additions & 2 deletions crates/relayer-store/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl LeafCacheStore for InMemoryStore {
}

impl EncryptedOutputCacheStore for InMemoryStore {
type Output = Vec<Vec<u8>>;
type Output = Vec<String>;

#[tracing::instrument(skip(self))]
fn get_encrypted_output<K: Into<HistoryStoreKey> + Debug>(
Expand All @@ -209,6 +209,7 @@ impl EncryptedOutputCacheStore for InMemoryStore {
) -> crate::Result<Self::Output> {
let guard = self.encrypted_output_store.read();
let val = guard.get(&key.into()).cloned().unwrap_or_default();
let val: Vec<String> = val.iter().map(hex::encode).collect();
Ok(val)
}

Expand All @@ -223,7 +224,8 @@ impl EncryptedOutputCacheStore for InMemoryStore {
let iter = val
.into_iter()
.skip(range.start as usize)
.take((range.end - range.start) as usize);
.take((range.end - range.start) as usize)
.map(hex::encode);
Ok(iter.collect())
}

Expand Down
56 changes: 52 additions & 4 deletions crates/relayer-store/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl LeafCacheStore for SledStore {
}

impl EncryptedOutputCacheStore for SledStore {
type Output = Vec<Vec<u8>>;
type Output = Vec<String>;

#[tracing::instrument(skip(self))]
fn get_encrypted_output<K: Into<HistoryStoreKey> + Debug>(
Expand All @@ -283,8 +283,12 @@ impl EncryptedOutputCacheStore for SledStore {
key.chain_id(),
key.address()
))?;
let encrypted_outputs: Vec<_> =
tree.iter().values().flatten().map(|v| v.to_vec()).collect();
let encrypted_outputs: Vec<_> = tree
.iter()
.values()
.flatten()
.map(|v| hex::encode(&v))
.collect();
Ok(encrypted_outputs)
}

Expand All @@ -306,7 +310,7 @@ impl EncryptedOutputCacheStore for SledStore {
.range(range_start..range_end)
.values()
.flatten()
.map(|v| v.to_vec())
.map(|v| hex::encode(&v))
.collect();
Ok(encrypted_outputs)
}
Expand Down Expand Up @@ -711,6 +715,7 @@ mod tests {

use super::*;
use webb::evm::ethers::core::types::transaction::eip2718::TypedTransaction;
use webb::evm::ethers::prelude::rand::Rng;
use webb::evm::ethers::types;
use webb::evm::ethers::types::transaction::request::TransactionRequest;
use webb::evm::{
Expand Down Expand Up @@ -977,6 +982,49 @@ mod tests {
);
}

#[test]
fn get_encrypted_outputs_should_work() {
let tmp = tempfile::tempdir().unwrap();
let store = SledStore::open(tmp.path()).unwrap();
let chain_id = 1u32;
let block_number = 20u64;
let contract =
types::H160::from_slice("11111111111111111111".as_bytes());
let history_store_key = (
TypedChainId::Evm(chain_id),
TargetSystem::new_contract_address(contract.to_fixed_bytes()),
);
let generated_encrypted_outputs = (0..20u32)
.map(|i| {
let mut encrypted_output = vec![0u8; 168];
for byte in encrypted_output.iter_mut() {
*byte = ethers::prelude::rand::thread_rng().gen();
}
(i, encrypted_output)
})
.collect::<Vec<_>>();
store
.insert_encrypted_output_and_last_deposit_block_number(
history_store_key,
&generated_encrypted_outputs,
block_number,
)
.unwrap();
let encrypted_outputs = store
.get_encrypted_output_with_range(history_store_key, 5..10)
.unwrap();
assert_eq!(encrypted_outputs.len(), 5);
assert_eq!(
encrypted_outputs,
generated_encrypted_outputs
.into_iter()
.skip(5)
.take(5)
.map(|(_, v)| hex::encode(v))
.collect::<Vec<_>>()
);
}

#[test]
fn update_item_should_work() {
let tmp = tempfile::tempdir().unwrap();
Expand Down

0 comments on commit 2bcce85

Please sign in to comment.