Skip to content

Commit

Permalink
Merge pull request #189 from semiotic-ai/aasseman/issue188
Browse files Browse the repository at this point in the history
refactor!: rename "gateway" to "sender" everywhere
  • Loading branch information
aasseman authored Nov 23, 2023
2 parents 3d247c0 + 309f41f commit 3cab533
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 170 deletions.
2 changes: 1 addition & 1 deletion tap_aggregator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Options:
--port <PORT>
Port to listen on for JSON-RPC requests [env: TAP_PORT=] [default: 8080]
--mnemonic <MNEMONIC>
Gateway mnemonic to be used to sign Receipt Aggregate Vouchers [env: TAP_MNEMONIC=]
Sender mnemonic to be used to sign Receipt Aggregate Vouchers [env: TAP_MNEMONIC=]
--max-request-body-size <MAX_REQUEST_BODY_SIZE>
Maximum request body size in bytes. Defaults to 10MB [env: TAP_MAX_REQUEST_BODY_SIZE=] [default: 10485760]
--max-response-body-size <MAX_RESPONSE_BODY_SIZE>
Expand Down
4 changes: 2 additions & 2 deletions tap_aggregator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ struct Args {
#[arg(long, default_value_t = 8080, env = "TAP_PORT")]
port: u16,

/// Gateway mnemonic to be used to generate key for signing Receipt Aggregate Vouchers.
/// Sender mnemonic to be used to generate key for signing Receipt Aggregate Vouchers.
#[arg(long, env = "TAP_MNEMONIC")]
mnemonic: String,

/// Gateway key derive path to be used to generate key for signing Receipt Aggregate Vouchers.
/// Sender key derive path to be used to generate key for signing Receipt Aggregate Vouchers.
#[arg(long, default_value = "m/44'/60'/0'/0/0", env = "TAP_KEY_DERIVE_PATH")]
key_derive_path: String,

Expand Down
16 changes: 8 additions & 8 deletions tap_core/src/adapters/escrow_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use async_trait::async_trait;
/// # Usage
///
/// The `get_available_escrow` method should be used to retrieve the local accounting
/// amount of available escrow for a specified gateway. Any errors during this operation
/// amount of available escrow for a specified sender. Any errors during this operation
/// should be captured and returned in the `AdapterError` format.
///
/// The `subtract_escrow` method is used to deduct a specified value from the local accounting
/// of available escrow of a specified gateway. Any errors during this operation should be captured
/// of available escrow of a specified sender. Any errors during this operation should be captured
/// and returned as an `AdapterError`.
///
/// This trait is utilized by [crate::tap_manager], which relies on these
Expand All @@ -36,21 +36,21 @@ pub trait EscrowAdapter {
/// Errors of this type are returned to the user when an operation fails.
type AdapterError: std::error::Error + std::fmt::Debug + Send + Sync + 'static;

/// Retrieves the local accounting amount of available escrow for a specified gateway.
/// Retrieves the local accounting amount of available escrow for a specified sender.
///
/// This method should be implemented to fetch the local accounting amount of available escrow for a
/// specified gateway from your system. Any errors that occur during this process should
/// specified sender from your system. Any errors that occur during this process should
/// be captured and returned as an `AdapterError`.
async fn get_available_escrow(&self, gateway_id: Address) -> Result<u128, Self::AdapterError>;
async fn get_available_escrow(&self, sender_id: Address) -> Result<u128, Self::AdapterError>;

/// Deducts a specified value from the local accounting of available escrow for a specified gateway.
/// Deducts a specified value from the local accounting of available escrow for a specified sender.
///
/// This method should be implemented to deduct a specified value from the local accounting of
/// available escrow of a specified gateway in your system. Any errors that occur during this
/// available escrow of a specified sender in your system. Any errors that occur during this
/// process should be captured and returned as an `AdapterError`.
async fn subtract_escrow(
&self,
gateway_id: Address,
sender_id: Address,
value: u128,
) -> Result<(), Self::AdapterError>;
}
12 changes: 6 additions & 6 deletions tap_core/src/adapters/receipt_checks_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use async_trait::async_trait;
///
/// This trait is designed to be implemented by users of this library who want to
/// customize the checks done on TAP receipts. This includes ensuring the receipt is unique,
/// verifying the allocation ID, the value and the gateway ID.
/// verifying the allocation ID, the value and the sender ID.
///
/// # Usage
///
Expand All @@ -19,7 +19,7 @@ use async_trait::async_trait;
///
/// The `is_valid_value` method should confirm the value of the receipt is valid for the given query ID.
///
/// The `is_valid_gateway_id` method should confirm the gateway ID is valid.
/// The `is_valid_sender_id` method should confirm the sender ID is valid.
///
/// This trait is utilized by [crate::tap_manager], which relies on these
/// operations for managing TAP receipts.
Expand Down Expand Up @@ -61,9 +61,9 @@ pub trait ReceiptChecksAdapter {
/// This method should be implemented to confirm the validity of the given value for a specific query ID.
async fn is_valid_value(&self, value: u128, query_id: u64) -> Result<bool, Self::AdapterError>;

/// Confirms the gateway ID is valid.
/// Confirms the sender ID is valid.
///
/// This method should be implemented to validate the given gateway ID is one associated with a gateway the indexer considers valid.
/// The provided gateway ID is the address of the gateway that is recovered from the signature of the receipt.
async fn is_valid_gateway_id(&self, gateway_id: Address) -> Result<bool, Self::AdapterError>;
/// This method should be implemented to validate the given sender ID is one associated with a sender the indexer considers valid.
/// The provided sender ID is the address of the sender that is recovered from the signature of the receipt.
async fn is_valid_sender_id(&self, sender_id: Address) -> Result<bool, Self::AdapterError>;
}
42 changes: 21 additions & 21 deletions tap_core/src/adapters/test/escrow_adapter_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::RwLock;
use crate::adapters::escrow_adapter::EscrowAdapter;

pub struct EscrowAdapterMock {
gateway_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>,
sender_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>,
}

use thiserror::Error;
Expand All @@ -21,43 +21,43 @@ pub enum AdpaterErrorMock {
}

impl EscrowAdapterMock {
pub fn new(gateway_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>) -> Self {
pub fn new(sender_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>) -> Self {
EscrowAdapterMock {
gateway_escrow_storage,
sender_escrow_storage,
}
}
pub async fn escrow(&self, gateway_id: Address) -> Result<u128, AdpaterErrorMock> {
let gateway_escrow_storage = self.gateway_escrow_storage.read().await;
if let Some(escrow) = gateway_escrow_storage.get(&gateway_id) {
pub async fn escrow(&self, sender_id: Address) -> Result<u128, AdpaterErrorMock> {
let sender_escrow_storage = self.sender_escrow_storage.read().await;
if let Some(escrow) = sender_escrow_storage.get(&sender_id) {
return Ok(*escrow);
}
Err(AdpaterErrorMock::AdapterError {
error: "No escrow exists for provided gateway ID.".to_owned(),
error: "No escrow exists for provided sender ID.".to_owned(),
})
}

pub async fn increase_escrow(&mut self, gateway_id: Address, value: u128) {
let mut gateway_escrow_storage = self.gateway_escrow_storage.write().await;
pub async fn increase_escrow(&mut self, sender_id: Address, value: u128) {
let mut sender_escrow_storage = self.sender_escrow_storage.write().await;

if let Some(current_value) = gateway_escrow_storage.get(&gateway_id) {
let mut gateway_escrow_storage = self.gateway_escrow_storage.write().await;
gateway_escrow_storage.insert(gateway_id, current_value + value);
if let Some(current_value) = sender_escrow_storage.get(&sender_id) {
let mut sender_escrow_storage = self.sender_escrow_storage.write().await;
sender_escrow_storage.insert(sender_id, current_value + value);
} else {
gateway_escrow_storage.insert(gateway_id, value);
sender_escrow_storage.insert(sender_id, value);
}
}

pub async fn reduce_escrow(
&self,
gateway_id: Address,
sender_id: Address,
value: u128,
) -> Result<(), AdpaterErrorMock> {
let mut gateway_escrow_storage = self.gateway_escrow_storage.write().await;
let mut sender_escrow_storage = self.sender_escrow_storage.write().await;

if let Some(current_value) = gateway_escrow_storage.get(&gateway_id) {
if let Some(current_value) = sender_escrow_storage.get(&sender_id) {
let checked_new_value = current_value.checked_sub(value);
if let Some(new_value) = checked_new_value {
gateway_escrow_storage.insert(gateway_id, new_value);
sender_escrow_storage.insert(sender_id, new_value);
return Ok(());
}
}
Expand All @@ -70,14 +70,14 @@ impl EscrowAdapterMock {
#[async_trait]
impl EscrowAdapter for EscrowAdapterMock {
type AdapterError = AdpaterErrorMock;
async fn get_available_escrow(&self, gateway_id: Address) -> Result<u128, Self::AdapterError> {
self.escrow(gateway_id).await
async fn get_available_escrow(&self, sender_id: Address) -> Result<u128, Self::AdapterError> {
self.escrow(sender_id).await
}
async fn subtract_escrow(
&self,
gateway_id: Address,
sender_id: Address,
value: u128,
) -> Result<(), Self::AdapterError> {
self.reduce_escrow(gateway_id, value).await
self.reduce_escrow(sender_id, value).await
}
}
36 changes: 15 additions & 21 deletions tap_core/src/adapters/test/escrow_adapter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,57 @@ mod escrow_adapter_unit_test {
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
.build()
.unwrap();
let gateway_id: [u8; 20] = wallet.address().into();
let gateway_id = gateway_id.into();
let sender_id: [u8; 20] = wallet.address().into();
let sender_id = sender_id.into();

let invalid_wallet: LocalWallet = MnemonicBuilder::<English>::default()
.phrase(
"wrong century settle satisfy market forest title connect ten push alley depend",
)
.build()
.unwrap();
let invalid_gateway_id: [u8; 20] = invalid_wallet.address().into();
let invalid_gateway_id = invalid_gateway_id.into();
let invalid_sender_id: [u8; 20] = invalid_wallet.address().into();
let invalid_sender_id = invalid_sender_id.into();

let initial_value = 500u128;

escrow_adapter
.increase_escrow(gateway_id, initial_value)
.increase_escrow(sender_id, initial_value)
.await;

// Check that gateway exists and has valid value through adapter
assert!(escrow_adapter
.get_available_escrow(gateway_id)
.await
.is_ok());
// Check that sender exists and has valid value through adapter
assert!(escrow_adapter.get_available_escrow(sender_id).await.is_ok());
assert_eq!(
escrow_adapter
.get_available_escrow(gateway_id)
.get_available_escrow(sender_id)
.await
.unwrap(),
initial_value
);

// Check that subtracting is valid for valid gateway, and results in expected value
assert!(escrow_adapter
.subtract_escrow(gateway_id, initial_value)
.await
.is_ok());
// Check that subtracting is valid for valid sender, and results in expected value
assert!(escrow_adapter
.get_available_escrow(gateway_id)
.subtract_escrow(sender_id, initial_value)
.await
.is_ok());
assert!(escrow_adapter.get_available_escrow(sender_id).await.is_ok());
assert_eq!(
escrow_adapter
.get_available_escrow(gateway_id)
.get_available_escrow(sender_id)
.await
.unwrap(),
0
);

// Check that subtracting to negative escrow results in err
assert!(escrow_adapter
.subtract_escrow(gateway_id, initial_value)
.subtract_escrow(sender_id, initial_value)
.await
.is_err());

// Check that accessing non initialized gateway results in err
// Check that accessing non initialized sender results in err
assert!(escrow_adapter
.get_available_escrow(invalid_gateway_id)
.get_available_escrow(invalid_sender_id)
.await
.is_err());
}
Expand Down
12 changes: 6 additions & 6 deletions tap_core/src/adapters/test/receipt_checks_adapter_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct ReceiptChecksAdapterMock {
receipt_storage: Arc<RwLock<HashMap<u64, ReceivedReceipt>>>,
query_appraisals: Arc<RwLock<HashMap<u64, u128>>>,
allocation_ids: Arc<RwLock<HashSet<Address>>>,
gateway_ids: Arc<RwLock<HashSet<Address>>>,
sender_ids: Arc<RwLock<HashSet<Address>>>,
}

#[derive(Debug, Error)]
Expand All @@ -43,13 +43,13 @@ impl ReceiptChecksAdapterMock {
receipt_storage: Arc<RwLock<HashMap<u64, ReceivedReceipt>>>,
query_appraisals: Arc<RwLock<HashMap<u64, u128>>>,
allocation_ids: Arc<RwLock<HashSet<Address>>>,
gateway_ids: Arc<RwLock<HashSet<Address>>>,
sender_ids: Arc<RwLock<HashSet<Address>>>,
) -> Self {
Self {
receipt_storage,
query_appraisals,
allocation_ids,
gateway_ids,
sender_ids,
}
}
}
Expand Down Expand Up @@ -90,8 +90,8 @@ impl ReceiptChecksAdapter for ReceiptChecksAdapterMock {
Ok(true)
}

async fn is_valid_gateway_id(&self, gateway_id: Address) -> Result<bool, Self::AdapterError> {
let gateway_ids = self.gateway_ids.read().await;
Ok(gateway_ids.contains(&gateway_id))
async fn is_valid_sender_id(&self, sender_id: Address) -> Result<bool, Self::AdapterError> {
let sender_ids = self.sender_ids.read().await;
Ok(sender_ids.contains(&sender_id))
}
}
10 changes: 5 additions & 5 deletions tap_core/src/adapters/test/receipt_checks_adapter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ mod receipt_checks_adapter_unit_test {
#[rstest]
#[tokio::test]
async fn receipt_checks_adapter_test(domain_separator: Eip712Domain) {
let gateway_ids = [
let sender_ids = [
Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(),
Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(),
Address::from_str("0xadadadadadadadadadadadadadadadadadadadad").unwrap(),
];
let gateway_ids_set = Arc::new(RwLock::new(HashSet::from(gateway_ids)));
let sender_ids_set = Arc::new(RwLock::new(HashSet::from(sender_ids)));

let allocation_ids = [
Address::from_str("0xabababababababababababababababababababab").unwrap(),
Expand Down Expand Up @@ -91,7 +91,7 @@ mod receipt_checks_adapter_unit_test {
Arc::clone(&receipt_storage),
query_appraisals_storage,
allocation_ids_set,
gateway_ids_set,
sender_ids_set,
);

let new_receipt = (
Expand Down Expand Up @@ -123,8 +123,8 @@ mod receipt_checks_adapter_unit_test {
.is_valid_allocation_id(new_receipt.1.signed_receipt.message.allocation_id)
.await
.unwrap());
// TODO: Add check when gateway_id is available from received receipt (issue: #56)
// assert!(receipt_checks_adapter.is_valid_gateway_id(gateway_id));
// TODO: Add check when sender_id is available from received receipt (issue: #56)
// assert!(receipt_checks_adapter.is_valid_sender_id(sender_id));
assert!(receipt_checks_adapter
.is_valid_value(
new_receipt.1.signed_receipt.message.value,
Expand Down
2 changes: 1 addition & 1 deletion tap_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum Error {
WalletError(#[from] WalletError),
#[error(transparent)]
SignatureError(#[from] SignatureError),
#[error("Recovered gateway address invalid{address}")]
#[error("Recovered sender address invalid{address}")]
InvalidRecoveredSigner { address: Address },
#[error("Received RAV does not match expexted RAV")]
InvalidReceivedRAV {
Expand Down
12 changes: 6 additions & 6 deletions tap_core/src/tap_manager/test/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod manager_unit_test {
}

#[fixture]
fn gateway_ids() -> Vec<Address> {
fn sender_ids() -> Vec<Address> {
vec![
Address::from_str("0xfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb").unwrap(),
Address::from_str("0xfafafafafafafafafafafafafafafafafafafafa").unwrap(),
Expand Down Expand Up @@ -81,9 +81,9 @@ mod manager_unit_test {

#[fixture]
fn escrow_adapters() -> (EscrowAdapterMock, Arc<RwLock<HashMap<Address, u128>>>) {
let gateway_escrow_storage = Arc::new(RwLock::new(HashMap::new()));
let escrow_adapter = EscrowAdapterMock::new(Arc::clone(&gateway_escrow_storage));
(escrow_adapter, gateway_escrow_storage)
let sender_escrow_storage = Arc::new(RwLock::new(HashMap::new()));
let escrow_adapter = EscrowAdapterMock::new(Arc::clone(&sender_escrow_storage));
(escrow_adapter, sender_escrow_storage)
}

#[fixture]
Expand All @@ -96,14 +96,14 @@ mod manager_unit_test {
let receipt_storage_adapter = ReceiptStorageAdapterMock::new(Arc::clone(&receipt_storage));

let allocation_ids_set = Arc::new(RwLock::new(HashSet::from_iter(allocation_ids())));
let gateway_ids_set = Arc::new(RwLock::new(HashSet::from_iter(gateway_ids())));
let sender_ids_set = Arc::new(RwLock::new(HashSet::from_iter(sender_ids())));
let query_appraisal_storage = Arc::new(RwLock::new(HashMap::new()));

let receipt_checks_adapter = ReceiptChecksAdapterMock::new(
Arc::clone(&receipt_storage),
Arc::clone(&query_appraisal_storage),
Arc::clone(&allocation_ids_set),
Arc::clone(&gateway_ids_set),
Arc::clone(&sender_ids_set),
);

(
Expand Down
Loading

0 comments on commit 3cab533

Please sign in to comment.