Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/dips/src/ipfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ impl TestIpfsClient {
},
}
}
pub fn no_network() -> Self {
Self {
manifest: GraphManifest {
data_sources: vec![],
},
}
}
}

#[cfg(test)]
Expand Down
59 changes: 35 additions & 24 deletions crates/dips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ pub enum DipsError {
SubgraphManifestUnavailable(String),
#[error("invalid subgraph id {0}")]
InvalidSubgraphManifest(String),
#[error("voucher for chain id {0}, subgraph manifest has network {1}")]
SubgraphChainIdMistmatch(String, String),
#[error("chainId {0} is not supported")]
UnsupportedChainId(String),
#[error("price per block is below configured price for chain {0}, minimum: {1}, offered: {2}")]
Expand Down Expand Up @@ -319,31 +317,30 @@ pub async fn validate_and_create_agreement(

let manifest = ipfs_fetcher.fetch(&metadata.subgraphDeploymentId).await?;
match manifest.network() {
Some(chain_id) if chain_id == metadata.chainId => {}
Some(chain_id) => {
return Err(DipsError::SubgraphChainIdMistmatch(
metadata.chainId,
chain_id,
Some(network_name) => {
tracing::debug!("Subgraph manifest network: {}", network_name);
// TODO: Check if the network is supported
// This will require a mapping of network names to chain IDs
// by querying the supported networks from the EBO subgraph
}
None => {
return Err(DipsError::InvalidSubgraphManifest(
metadata.subgraphDeploymentId,
))
}
None => return Err(DipsError::UnsupportedChainId("".to_string())),
}

let chain_id = manifest
.network()
.ok_or_else(|| DipsError::UnsupportedChainId("".to_string()))?;

let offered_price = metadata.pricePerEntity;
match price_calculator.get_minimum_price(&chain_id) {
match price_calculator.get_minimum_price(&metadata.chainId) {
Some(price) if offered_price.lt(&Uint::from(price)) => {
return Err(DipsError::PricePerBlockTooLow(
chain_id,
metadata.chainId,
price,
offered_price.to_string(),
))
}
Some(_) => {}
None => return Err(DipsError::UnsupportedChainId(chain_id)),
None => return Err(DipsError::UnsupportedChainId(metadata.chainId)),
}

store
Expand Down Expand Up @@ -729,16 +726,25 @@ mod test {
)]),
))
.await;
let no_network_ctx =
DipsServerContext::for_testing_mocked_accounts_no_network(EscrowAccounts::new(
HashMap::default(),
HashMap::from_iter(vec![(
voucher_ctx.payer.address(),
vec![voucher_ctx.payer.address()],
)]),
))
.await;

let metadata = SubgraphIndexingVoucherMetadata {
basePricePerEpoch: U256::from(10000_u64),
pricePerEntity: U256::from(100_u64),
protocolNetwork: "eip155:42161".to_string(),
chainId: "mainnet2".to_string(),
chainId: "mainnet".to_string(),
subgraphDeploymentId: voucher_ctx.deployment_id.clone(),
};

let wrong_network_voucher = voucher_ctx.test_voucher(metadata);
// The voucher says mainnet, but the manifest has no network
let no_network_voucher = voucher_ctx.test_voucher(metadata);

let metadata = SubgraphIndexingVoucherMetadata {
basePricePerEpoch: U256::from(10000_u64),
Expand All @@ -763,10 +769,11 @@ mod test {
voucher_ctx.test_voucher_with_signer(metadata.clone(), signer.clone());
let valid_voucher = voucher_ctx.test_voucher(metadata);

let contexts = vec![no_network_ctx, ctx.clone(), ctx.clone(), ctx.clone()];

let expected_result: Vec<Result<[u8; 16], DipsError>> = vec![
Err(DipsError::SubgraphChainIdMistmatch(
"mainnet2".to_string(),
"mainnet".to_string(),
Err(DipsError::InvalidSubgraphManifest(
voucher_ctx.deployment_id.clone(),
)),
Err(DipsError::PricePerBlockTooLow(
"mainnet".to_string(),
Expand All @@ -782,14 +789,18 @@ mod test {
.unwrap()),
];
let cases = vec![
wrong_network_voucher,
no_network_voucher,
low_price_voucher,
valid_voucher_invalid_signer,
valid_voucher,
];
for (voucher, result) in cases.into_iter().zip(expected_result.into_iter()) {
for ((voucher, result), dips_ctx) in cases
.into_iter()
.zip(expected_result.into_iter())
.zip(contexts.into_iter())
{
let out = super::validate_and_create_agreement(
ctx.clone(),
dips_ctx.clone(),
&voucher_ctx.domain(),
&voucher_ctx.payee.address(),
vec![voucher_ctx.payer.address()],
Expand Down
12 changes: 12 additions & 0 deletions crates/dips/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ impl DipsServerContext {
signer_validator: Arc::new(signers::EscrowSignerValidator::mock(accounts).await),
})
}

#[cfg(test)]
pub async fn for_testing_mocked_accounts_no_network(accounts: EscrowAccounts) -> Arc<Self> {
use crate::{ipfs::TestIpfsClient, signers, test::InMemoryAgreementStore};

Arc::new(DipsServerContext {
store: Arc::new(InMemoryAgreementStore::default()),
ipfs_fetcher: Arc::new(TestIpfsClient::no_network()),
price_calculator: PriceCalculator::for_testing(),
signer_validator: Arc::new(signers::EscrowSignerValidator::mock(accounts).await),
})
}
}

#[derive(Debug)]
Expand Down
Loading