Skip to content

Commit 1a4c74b

Browse files
committed
fix: fmt
1 parent da44d64 commit 1a4c74b

File tree

5 files changed

+118
-66
lines changed

5 files changed

+118
-66
lines changed

crates/dips/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::{
5-
str::FromStr,
6-
sync::Arc,
7-
};
4+
use std::{str::FromStr, sync::Arc};
85

96
use thegraph_core::alloy::{
107
core::primitives::Address,
@@ -83,7 +80,7 @@ sol! {
8380

8481
uint32 minEpochsPerCollection;
8582
uint32 maxEpochsPerCollection;
86-
83+
8784
// Deadline for the indexer to accept the agreement
8885
uint64 deadline;
8986
bytes metadata;
@@ -280,8 +277,9 @@ pub async fn validate_and_create_agreement(
280277
) -> Result<Uuid, DipsError> {
281278
let voucher = SignedIndexingAgreementVoucher::abi_decode(&mut voucher.as_ref(), true)
282279
.map_err(|e| DipsError::AbiDecoding(e.to_string()))?;
283-
let metadata = SubgraphIndexingVoucherMetadata::abi_decode(&mut voucher.voucher.metadata.as_ref(), true)
284-
.map_err(|e| DipsError::AbiDecoding(e.to_string()))?;
280+
let metadata =
281+
SubgraphIndexingVoucherMetadata::abi_decode(&mut voucher.voucher.metadata.as_ref(), true)
282+
.map_err(|e| DipsError::AbiDecoding(e.to_string()))?;
285283

286284
voucher.validate(domain, expected_payee, allowed_payers)?;
287285

@@ -298,7 +296,9 @@ pub async fn validate_and_cancel_agreement(
298296
let request = SignedCancellationRequest::abi_decode(&mut cancellation_request.as_ref(), true)
299297
.map_err(|e| DipsError::AbiDecoding(e.to_string()))?;
300298

301-
let result = store.get_by_id(Uuid::from_bytes(request.request.agreement_id.into())).await?;
299+
let result = store
300+
.get_by_id(Uuid::from_bytes(request.request.agreement_id.into()))
301+
.await?;
302302
let agreement_and_cancelled = result.ok_or(DipsError::AgreementNotFound)?;
303303
let agreement = agreement_and_cancelled.0;
304304
let cancelled = agreement_and_cancelled.1;
@@ -330,7 +330,8 @@ mod test {
330330

331331
pub use crate::store::{AgreementStore, InMemoryAgreementStore};
332332
use crate::{
333-
dips_agreement_eip712_domain, dips_cancellation_eip712_domain, CancellationRequest, DipsError, IndexingAgreementVoucher, SubgraphIndexingVoucherMetadata
333+
dips_agreement_eip712_domain, dips_cancellation_eip712_domain, CancellationRequest,
334+
DipsError, IndexingAgreementVoucher, SubgraphIndexingVoucherMetadata,
334335
};
335336

336337
#[tokio::test]
@@ -525,7 +526,7 @@ mod test {
525526
#[tokio::test]
526527
async fn test_create_and_cancel_agreement() -> anyhow::Result<()> {
527528
let deployment_id = "Qmbg1qF4YgHjiVfsVt6a13ddrVcRtWyJQfD4LA3CwHM29f".to_string();
528-
let payee = PrivateKeySigner::random();
529+
let payee = PrivateKeySigner::random();
529530
let payee_addr = payee.address();
530531
let payer = PrivateKeySigner::random();
531532
let payer_addr = payer.address();
@@ -559,7 +560,7 @@ mod test {
559560

560561
let domain = dips_agreement_eip712_domain();
561562
let signed_voucher = voucher.sign(&domain, payer.clone())?;
562-
563+
563564
// Create agreement
564565
let agreement_id = super::validate_and_create_agreement(
565566
store.clone(),

crates/dips/src/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct DipsServer {
2121
pub agreement_store: Arc<dyn AgreementStore>,
2222
pub expected_payee: Address,
2323
pub allowed_payers: Vec<Address>,
24-
pub domain: Eip712Domain
24+
pub domain: Eip712Domain,
2525
}
2626

2727
#[async_trait]
@@ -66,7 +66,7 @@ impl DipsService for DipsServer {
6666
) -> Result<Response<CancelAgreementResponse>, Status> {
6767
let CancelAgreementRequest {
6868
version,
69-
signed_cancellation
69+
signed_cancellation,
7070
} = request.into_inner();
7171

7272
if version != 1 {

crates/dips/src/store.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ use async_trait::async_trait;
77
use uuid::Uuid;
88

99
use crate::{
10-
DipsError, SignedCancellationRequest, SignedIndexingAgreementVoucher, SubgraphIndexingVoucherMetadata
10+
DipsError, SignedCancellationRequest, SignedIndexingAgreementVoucher,
11+
SubgraphIndexingVoucherMetadata,
1112
};
1213

1314
#[async_trait]
1415
pub trait AgreementStore: Sync + Send + std::fmt::Debug {
15-
async fn get_by_id(&self, id: Uuid) -> anyhow::Result<Option<(SignedIndexingAgreementVoucher, bool)>>;
16+
async fn get_by_id(
17+
&self,
18+
id: Uuid,
19+
) -> anyhow::Result<Option<(SignedIndexingAgreementVoucher, bool)>>;
1620
async fn create_agreement(
1721
&self,
1822
agreement: SignedIndexingAgreementVoucher,
@@ -31,17 +35,21 @@ pub struct InMemoryAgreementStore {
3135

3236
#[async_trait]
3337
impl AgreementStore for InMemoryAgreementStore {
34-
async fn get_by_id(&self, id: Uuid) -> anyhow::Result<Option<(SignedIndexingAgreementVoucher, bool)>> {
38+
async fn get_by_id(
39+
&self,
40+
id: Uuid,
41+
) -> anyhow::Result<Option<(SignedIndexingAgreementVoucher, bool)>> {
3542
Ok(self.data.try_read()?.get(&id).cloned())
3643
}
3744
async fn create_agreement(
3845
&self,
3946
agreement: SignedIndexingAgreementVoucher,
4047
_medatadata: SubgraphIndexingVoucherMetadata,
4148
) -> anyhow::Result<()> {
42-
self.data
43-
.try_write()?
44-
.insert(Uuid::from_bytes(agreement.voucher.agreement_id.into()), (agreement.clone(), false));
49+
self.data.try_write()?.insert(
50+
Uuid::from_bytes(agreement.voucher.agreement_id.into()),
51+
(agreement.clone(), false),
52+
);
4553

4654
Ok(())
4755
}
@@ -52,11 +60,20 @@ impl AgreementStore for InMemoryAgreementStore {
5260
let id = Uuid::from_bytes(signed_cancellation.request.agreement_id.into());
5361

5462
let agreement = {
55-
let read_lock = self.data.try_read().map_err(|e| DipsError::UnknownError(e.into()))?;
56-
read_lock.get(&id).cloned().ok_or(DipsError::AgreementNotFound)?
63+
let read_lock = self
64+
.data
65+
.try_read()
66+
.map_err(|e| DipsError::UnknownError(e.into()))?;
67+
read_lock
68+
.get(&id)
69+
.cloned()
70+
.ok_or(DipsError::AgreementNotFound)?
5771
};
5872

59-
let mut write_lock = self.data.try_write().map_err(|e| DipsError::UnknownError(e.into()))?;
73+
let mut write_lock = self
74+
.data
75+
.try_write()
76+
.map_err(|e| DipsError::UnknownError(e.into()))?;
6077
write_lock.insert(id, (agreement.0, true));
6178

6279
Ok(id)

crates/service/src/database/dips.rs

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ use anyhow::bail;
77
use axum::async_trait;
88
use build_info::chrono::{DateTime, Utc};
99
use indexer_dips::{
10-
store::AgreementStore, DipsError, SignedCancellationRequest, SignedIndexingAgreementVoucher, SubgraphIndexingVoucherMetadata
10+
store::AgreementStore, DipsError, SignedCancellationRequest, SignedIndexingAgreementVoucher,
11+
SubgraphIndexingVoucherMetadata,
1112
};
1213
use sqlx::{types::BigDecimal, PgPool};
13-
use thegraph_core::alloy::{
14-
core::primitives::U256 as uint256,
15-
sol_types::SolType
16-
};
14+
use thegraph_core::alloy::{core::primitives::U256 as uint256, hex::ToHexExt, sol_types::SolType};
1715
use uuid::Uuid;
18-
use thegraph_core::alloy::hex::ToHexExt;
1916

2017
#[derive(Debug)]
2118
pub struct PsqlAgreementStore {
@@ -31,10 +28,12 @@ fn uint32_to_i64(uint32: u32) -> i64 {
3128
uint32.try_into().unwrap()
3229
}
3330

34-
3531
#[async_trait]
3632
impl AgreementStore for PsqlAgreementStore {
37-
async fn get_by_id(&self, id: Uuid) -> anyhow::Result<Option<(SignedIndexingAgreementVoucher, bool)>> {
33+
async fn get_by_id(
34+
&self,
35+
id: Uuid,
36+
) -> anyhow::Result<Option<(SignedIndexingAgreementVoucher, bool)>> {
3837
let item = sqlx::query!("SELECT * FROM indexing_agreements WHERE id=$1", id,)
3938
.fetch_one(&self.pool)
4039
.await;
@@ -45,7 +44,8 @@ impl AgreementStore for PsqlAgreementStore {
4544
Err(err) => bail!(err),
4645
};
4746

48-
let signed = SignedIndexingAgreementVoucher::abi_decode(&mut item.signed_payload.as_ref(), true)?;
47+
let signed =
48+
SignedIndexingAgreementVoucher::abi_decode(&mut item.signed_payload.as_ref(), true)?;
4949
let cancelled = item.cancelled_at.is_some();
5050
Ok(Some((signed, cancelled)))
5151
}
@@ -57,12 +57,14 @@ impl AgreementStore for PsqlAgreementStore {
5757
let id = Uuid::from_bytes(agreement.voucher.agreement_id.into());
5858
let bs = agreement.encode_vec();
5959
let now = Utc::now();
60-
let deadline = DateTime::from_timestamp(agreement.voucher.deadline.try_into().unwrap(), 0).unwrap();
60+
let deadline =
61+
DateTime::from_timestamp(agreement.voucher.deadline.try_into().unwrap(), 0).unwrap();
6162
let base_price_per_epoch = uint256_to_bigdecimal(&metadata.basePricePerEpoch);
6263
let price_per_entity = uint256_to_bigdecimal(&metadata.pricePerEntity);
6364
let duration_epochs = uint32_to_i64(agreement.voucher.durationEpochs);
6465
let max_initial_amount = uint256_to_bigdecimal(&agreement.voucher.maxInitialAmount);
65-
let max_ongoing_amount_per_epoch = uint256_to_bigdecimal(&agreement.voucher.maxOngoingAmountPerEpoch);
66+
let max_ongoing_amount_per_epoch =
67+
uint256_to_bigdecimal(&agreement.voucher.maxOngoingAmountPerEpoch);
6668
let min_epochs_per_collection = uint32_to_i64(agreement.voucher.minEpochsPerCollection);
6769
let max_epochs_per_collection = uint32_to_i64(agreement.voucher.maxEpochsPerCollection);
6870
sqlx::query!(
@@ -117,14 +119,17 @@ impl AgreementStore for PsqlAgreementStore {
117119
#[cfg(test)]
118120
pub(crate) mod test {
119121
use std::sync::Arc;
120-
use super::*;
122+
121123
use build_info::chrono::Duration;
122124
use indexer_dips::{CancellationRequest, IndexingAgreementVoucher};
123125
use sqlx::PgPool;
124-
use thegraph_core::alloy::primitives::{ruint::aliases::U256, Address};
126+
use thegraph_core::alloy::{
127+
primitives::{ruint::aliases::U256, Address},
128+
sol_types::{SolType, SolValue},
129+
};
125130
use uuid::Uuid;
126-
use thegraph_core::alloy::sol_types::SolValue;
127-
use thegraph_core::alloy::sol_types::SolType;
131+
132+
use super::*;
128133

129134
#[sqlx::test(migrations = "../../migrations")]
130135
async fn test_store_agreement(pool: PgPool) {
@@ -149,26 +154,26 @@ pub(crate) mod test {
149154
payer: Address::from_str("1234567890123456789012345678901234567890").unwrap(),
150155
recipient: Address::from_str("2345678901234567890123456789012345678901").unwrap(),
151156
service: Address::from_str("3456789012345678901234567890123456789012").unwrap(),
152-
durationEpochs: 30, // 30 epochs duration
157+
durationEpochs: 30, // 30 epochs duration
153158
maxInitialAmount: U256::from(1000),
154159
maxOngoingAmountPerEpoch: U256::from(100),
155160
maxEpochsPerCollection: 5,
156161
minEpochsPerCollection: 1,
157-
metadata: metadata.abi_encode().into(), // Convert Vec<u8> to Bytes
162+
metadata: metadata.abi_encode().into(), // Convert Vec<u8> to Bytes
158163
},
159164
};
160165

161166
// Store agreement
162-
store.create_agreement(agreement.clone(), metadata).await.unwrap();
167+
store
168+
.create_agreement(agreement.clone(), metadata)
169+
.await
170+
.unwrap();
163171

164172
// Verify stored agreement
165-
let row = sqlx::query!(
166-
"SELECT * FROM indexing_agreements WHERE id = $1",
167-
id
168-
)
169-
.fetch_one(&store.pool)
170-
.await
171-
.unwrap();
173+
let row = sqlx::query!("SELECT * FROM indexing_agreements WHERE id = $1", id)
174+
.fetch_one(&store.pool)
175+
.await
176+
.unwrap();
172177

173178
assert_eq!(row.id, id);
174179
assert_eq!(row.signature, agreement.signature);
@@ -210,27 +215,53 @@ pub(crate) mod test {
210215
};
211216

212217
// Store agreement
213-
store.create_agreement(agreement.clone(), metadata.clone()).await.unwrap();
218+
store
219+
.create_agreement(agreement.clone(), metadata.clone())
220+
.await
221+
.unwrap();
214222

215223
// Retrieve agreement
216224
let retrieved = store.get_by_id(id).await.unwrap().unwrap();
217225

218226
let retrieved_voucher = &retrieved.0.voucher;
219227
let cancelled = retrieved.1;
220-
let retrieved_metadata = <indexer_dips::SubgraphIndexingVoucherMetadata as SolType>::abi_decode(&mut retrieved_voucher.metadata.as_ref(), true).unwrap();
228+
let retrieved_metadata =
229+
<indexer_dips::SubgraphIndexingVoucherMetadata as SolType>::abi_decode(
230+
&mut retrieved_voucher.metadata.as_ref(),
231+
true,
232+
)
233+
.unwrap();
221234
// Verify retrieved agreement matches original
222235
assert_eq!(retrieved.0.signature, agreement.signature);
223-
assert_eq!(retrieved_voucher.durationEpochs, agreement.voucher.durationEpochs);
236+
assert_eq!(
237+
retrieved_voucher.durationEpochs,
238+
agreement.voucher.durationEpochs
239+
);
224240
assert_eq!(retrieved_metadata.protocolNetwork, metadata.protocolNetwork);
225241
assert_eq!(retrieved_metadata.chainId, metadata.chainId);
226-
assert_eq!(retrieved_metadata.subgraphDeploymentId, metadata.subgraphDeploymentId);
242+
assert_eq!(
243+
retrieved_metadata.subgraphDeploymentId,
244+
metadata.subgraphDeploymentId
245+
);
227246
assert_eq!(retrieved_voucher.payer, agreement.voucher.payer);
228247
assert_eq!(retrieved_voucher.recipient, agreement.voucher.recipient);
229248
assert_eq!(retrieved_voucher.service, agreement.voucher.service);
230-
assert_eq!(retrieved_voucher.maxInitialAmount, agreement.voucher.maxInitialAmount);
231-
assert_eq!(retrieved_voucher.maxOngoingAmountPerEpoch, agreement.voucher.maxOngoingAmountPerEpoch);
232-
assert_eq!(retrieved_voucher.maxEpochsPerCollection, agreement.voucher.maxEpochsPerCollection);
233-
assert_eq!(retrieved_voucher.minEpochsPerCollection, agreement.voucher.minEpochsPerCollection);
249+
assert_eq!(
250+
retrieved_voucher.maxInitialAmount,
251+
agreement.voucher.maxInitialAmount
252+
);
253+
assert_eq!(
254+
retrieved_voucher.maxOngoingAmountPerEpoch,
255+
agreement.voucher.maxOngoingAmountPerEpoch
256+
);
257+
assert_eq!(
258+
retrieved_voucher.maxEpochsPerCollection,
259+
agreement.voucher.maxEpochsPerCollection
260+
);
261+
assert_eq!(
262+
retrieved_voucher.minEpochsPerCollection,
263+
agreement.voucher.minEpochsPerCollection
264+
);
234265
assert_eq!(cancelled, false);
235266
}
236267

@@ -267,8 +298,11 @@ pub(crate) mod test {
267298
};
268299

269300
// Store agreement
270-
store.create_agreement(agreement.clone(), metadata).await.unwrap();
271-
301+
store
302+
.create_agreement(agreement.clone(), metadata)
303+
.await
304+
.unwrap();
305+
272306
// Cancel agreement
273307
let cancellation = SignedCancellationRequest {
274308
signature: vec![1, 2, 3].into(),
@@ -279,15 +313,15 @@ pub(crate) mod test {
279313
store.cancel_agreement(cancellation.clone()).await.unwrap();
280314

281315
// Verify stored agreement
282-
let row = sqlx::query!(
283-
"SELECT * FROM indexing_agreements WHERE id = $1",
284-
id
285-
)
286-
.fetch_one(&store.pool)
287-
.await
288-
.unwrap();
316+
let row = sqlx::query!("SELECT * FROM indexing_agreements WHERE id = $1", id)
317+
.fetch_one(&store.pool)
318+
.await
319+
.unwrap();
289320

290321
assert!(row.cancelled_at.is_some());
291-
assert_eq!(row.signed_cancellation_payload, Some(cancellation.encode_vec()));
322+
assert_eq!(
323+
row.signed_cancellation_payload,
324+
Some(cancellation.encode_vec())
325+
);
292326
}
293327
}

crates/service/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub async fn run() -> anyhow::Result<()> {
137137
agreement_store: Arc::new(PsqlAgreementStore { pool: database }),
138138
expected_payee: indexer_address,
139139
allowed_payers: allowed_payers.clone(),
140-
domain: domain_separator
140+
domain: domain_separator,
141141
};
142142

143143
info!("starting dips grpc server on {}", addr);

0 commit comments

Comments
 (0)