diff --git a/crates/dips/src/ipfs.rs b/crates/dips/src/ipfs.rs index 820901f12..0b9e38f14 100644 --- a/crates/dips/src/ipfs.rs +++ b/crates/dips/src/ipfs.rs @@ -100,6 +100,13 @@ impl TestIpfsClient { }, } } + pub fn no_network() -> Self { + Self { + manifest: GraphManifest { + data_sources: vec![], + }, + } + } } #[cfg(test)] diff --git a/crates/dips/src/lib.rs b/crates/dips/src/lib.rs index 6ad37c5db..9c2fcbb04 100644 --- a/crates/dips/src/lib.rs +++ b/crates/dips/src/lib.rs @@ -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}")] @@ -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 @@ -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), @@ -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> = vec![ - Err(DipsError::SubgraphChainIdMistmatch( - "mainnet2".to_string(), - "mainnet".to_string(), + Err(DipsError::InvalidSubgraphManifest( + voucher_ctx.deployment_id.clone(), )), Err(DipsError::PricePerBlockTooLow( "mainnet".to_string(), @@ -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()], diff --git a/crates/dips/src/server.rs b/crates/dips/src/server.rs index c613f9aaa..85ae1b3c8 100644 --- a/crates/dips/src/server.rs +++ b/crates/dips/src/server.rs @@ -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 { + 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)]