Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 81178c8

Browse files
author
Alexandcoats
authored
feat(db): separate ledger/protocol_param logic from collections (#677)
* Separate ledger/protocol param logic from collections. * ßßßßßßßß * Add helper fn for ledger index. Convert response milestone types. Remove ledger index from db results.
1 parent 6507ee7 commit 81178c8

File tree

9 files changed

+479
-519
lines changed

9 files changed

+479
-519
lines changed

src/bin/inx-chronicle/api/stardust/analytics/responses.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::ops::Range;
55

66
use bee_api_types_stardust::responses::RentStructureResponse;
7-
use chronicle::db::collections::DistributionStat;
7+
use chronicle::{db::collections::DistributionStat, types::tangle::MilestoneIndex};
88
use serde::{Deserialize, Serialize};
99

1010
use crate::api::responses::impl_success_response;
@@ -37,7 +37,7 @@ pub struct StorageDepositAnalyticsResponse {
3737
pub total_key_bytes: String,
3838
pub total_data_bytes: String,
3939
pub total_byte_cost: String,
40-
pub ledger_index: u32,
40+
pub ledger_index: MilestoneIndex,
4141
pub rent_structure: RentStructureResponse,
4242
}
4343

@@ -57,7 +57,7 @@ impl_success_response!(OutputDiffAnalyticsResponse);
5757
#[serde(rename_all = "camelCase")]
5858
pub struct RichestAddressesResponse {
5959
pub top: Vec<AddressStatDto>,
60-
pub ledger_index: u32,
60+
pub ledger_index: MilestoneIndex,
6161
}
6262

6363
impl_success_response!(RichestAddressesResponse);
@@ -72,7 +72,7 @@ pub struct AddressStatDto {
7272
#[serde(rename_all = "camelCase")]
7373
pub struct TokenDistributionResponse {
7474
pub distribution: Vec<DistributionStatDto>,
75-
pub ledger_index: u32,
75+
pub ledger_index: MilestoneIndex,
7676
}
7777

7878
impl_success_response!(TokenDistributionResponse);

src/bin/inx-chronicle/api/stardust/analytics/routes.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use chronicle::{
1313
types::{
1414
stardust::block::{
1515
output::{AliasOutput, BasicOutput, FoundryOutput, NftOutput},
16-
payload::milestone::MilestoneId,
16+
payload::MilestoneId,
1717
},
1818
tangle::MilestoneIndex,
1919
},
@@ -165,7 +165,7 @@ async fn unspent_output_ledger_analytics<O: OutputKind>(
165165
) -> ApiResult<OutputAnalyticsResponse> {
166166
let res = database
167167
.collection::<OutputCollection>()
168-
.get_unspent_output_analytics::<O>(ledger_index)
168+
.get_unspent_output_analytics::<O>(resolve_ledger_index(&database, ledger_index).await?)
169169
.await?
170170
.ok_or(ApiError::NoResults)?;
171171

@@ -179,11 +179,17 @@ async fn storage_deposit_ledger_analytics(
179179
database: Extension<MongoDb>,
180180
LedgerIndex { ledger_index }: LedgerIndex,
181181
) -> ApiResult<StorageDepositAnalyticsResponse> {
182+
let ledger_index = resolve_ledger_index(&database, ledger_index).await?;
183+
let protocol_params = database
184+
.collection::<ProtocolUpdateCollection>()
185+
.get_protocol_parameters_for_ledger_index(ledger_index)
186+
.await?
187+
.ok_or(InternalApiError::CorruptState("no protocol parameters"))?
188+
.parameters;
182189
let res = database
183190
.collection::<OutputCollection>()
184-
.get_storage_deposit_analytics(ledger_index)
185-
.await?
186-
.ok_or(ApiError::NoResults)?;
191+
.get_storage_deposit_analytics(ledger_index, protocol_params)
192+
.await?;
187193

188194
Ok(StorageDepositAnalyticsResponse {
189195
output_count: res.output_count.to_string(),
@@ -192,7 +198,7 @@ async fn storage_deposit_ledger_analytics(
192198
total_key_bytes: res.total_key_bytes,
193199
total_data_bytes: res.total_data_bytes,
194200
total_byte_cost: res.total_byte_cost,
195-
ledger_index: res.ledger_index.0,
201+
ledger_index,
196202
rent_structure: RentStructureResponse {
197203
v_byte_cost: res.rent_structure.v_byte_cost,
198204
v_byte_factor_key: res.rent_structure.v_byte_factor_key,
@@ -237,15 +243,15 @@ async fn richest_addresses_ledger_analytics(
237243
database: Extension<MongoDb>,
238244
RichestAddressesQuery { top, ledger_index }: RichestAddressesQuery,
239245
) -> ApiResult<RichestAddressesResponse> {
246+
let ledger_index = resolve_ledger_index(&database, ledger_index).await?;
240247
let res = database
241248
.collection::<OutputCollection>()
242249
.get_richest_addresses(ledger_index, top)
243-
.await?
244-
.ok_or(ApiError::NoResults)?;
250+
.await?;
245251

246252
let hrp = database
247253
.collection::<ProtocolUpdateCollection>()
248-
.get_protocol_parameters_for_ledger_index(res.ledger_index)
254+
.get_protocol_parameters_for_ledger_index(ledger_index)
249255
.await?
250256
.ok_or(InternalApiError::CorruptState("no protocol parameters"))?
251257
.parameters
@@ -260,22 +266,36 @@ async fn richest_addresses_ledger_analytics(
260266
balance: stat.balance,
261267
})
262268
.collect(),
263-
ledger_index: res.ledger_index.0,
269+
ledger_index,
264270
})
265271
}
266272

267273
async fn token_distribution_ledger_analytics(
268274
database: Extension<MongoDb>,
269275
LedgerIndex { ledger_index }: LedgerIndex,
270276
) -> ApiResult<TokenDistributionResponse> {
277+
let ledger_index = resolve_ledger_index(&database, ledger_index).await?;
271278
let res = database
272279
.collection::<OutputCollection>()
273280
.get_token_distribution(ledger_index)
274-
.await?
275-
.ok_or(ApiError::NoResults)?;
281+
.await?;
276282

277283
Ok(TokenDistributionResponse {
278284
distribution: res.distribution.into_iter().map(Into::into).collect(),
279-
ledger_index: res.ledger_index.0,
285+
ledger_index,
286+
})
287+
}
288+
289+
/// This is just a helper fn to either unwrap an optional ledger index param or fetch the latest
290+
/// index from the database.
291+
async fn resolve_ledger_index(database: &MongoDb, ledger_index: Option<MilestoneIndex>) -> ApiResult<MilestoneIndex> {
292+
Ok(if let Some(ledger_index) = ledger_index {
293+
ledger_index
294+
} else {
295+
database
296+
.collection::<MilestoneCollection>()
297+
.get_ledger_index()
298+
.await?
299+
.ok_or(ApiError::NoResults)?
280300
})
281301
}

src/bin/inx-chronicle/api/stardust/core/routes.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ async fn block_metadata(
230230
})
231231
}
232232

233-
fn create_output_metadata_response(metadata: OutputMetadataResult) -> OutputMetadataResponse {
233+
fn create_output_metadata_response(
234+
metadata: OutputMetadataResult,
235+
ledger_index: MilestoneIndex,
236+
) -> OutputMetadataResponse {
234237
OutputMetadataResponse {
235238
block_id: metadata.block_id.to_hex(),
236239
transaction_id: metadata.output_id.transaction_id.to_hex(),
@@ -250,19 +253,24 @@ fn create_output_metadata_response(metadata: OutputMetadataResult) -> OutputMeta
250253
.map(|spent_md| spent_md.transaction_id.to_hex()),
251254
milestone_index_booked: *metadata.booked.milestone_index,
252255
milestone_timestamp_booked: *metadata.booked.milestone_timestamp,
253-
ledger_index: metadata.ledger_index.0,
256+
ledger_index: ledger_index.0,
254257
}
255258
}
256259

257260
async fn output(database: Extension<MongoDb>, Path(output_id): Path<String>) -> ApiResult<OutputResponse> {
261+
let ledger_index = database
262+
.collection::<MilestoneCollection>()
263+
.get_ledger_index()
264+
.await?
265+
.ok_or(ApiError::NoResults)?;
258266
let output_id = OutputId::from_str(&output_id).map_err(ApiError::bad_parse)?;
259267
let OutputWithMetadataResult { output, metadata } = database
260268
.collection::<OutputCollection>()
261-
.get_output_with_metadata(&output_id)
269+
.get_output_with_metadata(&output_id, ledger_index)
262270
.await?
263271
.ok_or(ApiError::NoResults)?;
264272

265-
let metadata = create_output_metadata_response(metadata);
273+
let metadata = create_output_metadata_response(metadata, ledger_index);
266274

267275
Ok(OutputResponse {
268276
metadata,
@@ -274,14 +282,19 @@ async fn output_metadata(
274282
database: Extension<MongoDb>,
275283
Path(output_id): Path<String>,
276284
) -> ApiResult<OutputMetadataResponse> {
285+
let ledger_index = database
286+
.collection::<MilestoneCollection>()
287+
.get_ledger_index()
288+
.await?
289+
.ok_or(ApiError::NoResults)?;
277290
let output_id = OutputId::from_str(&output_id).map_err(ApiError::bad_parse)?;
278291
let metadata = database
279292
.collection::<OutputCollection>()
280-
.get_output_metadata(&output_id)
293+
.get_output_metadata(&output_id, ledger_index)
281294
.await?
282295
.ok_or(ApiError::NoResults)?;
283296

284-
Ok(create_output_metadata_response(metadata))
297+
Ok(create_output_metadata_response(metadata, ledger_index))
285298
}
286299

287300
async fn transaction_included_block(
@@ -424,12 +437,17 @@ async fn utxo_changes_by_index(
424437
}
425438

426439
async fn collect_utxo_changes(database: &MongoDb, milestone_index: MilestoneIndex) -> ApiResult<UtxoChangesResponse> {
440+
let ledger_index = database
441+
.collection::<MilestoneCollection>()
442+
.get_ledger_index()
443+
.await?
444+
.ok_or(ApiError::NoResults)?;
427445
let UtxoChangesResult {
428446
created_outputs,
429447
consumed_outputs,
430448
} = database
431449
.collection::<OutputCollection>()
432-
.get_utxo_changes(milestone_index)
450+
.get_utxo_changes(milestone_index, ledger_index)
433451
.await?
434452
.ok_or(ApiError::NoResults)?;
435453

src/bin/inx-chronicle/api/stardust/explorer/routes.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,22 @@ async fn ledger_updates_by_milestone(
128128
}
129129

130130
async fn balance(database: Extension<MongoDb>, Path(address): Path<String>) -> ApiResult<BalanceResponse> {
131+
let ledger_index = database
132+
.collection::<MilestoneCollection>()
133+
.get_ledger_index()
134+
.await?
135+
.ok_or(ApiError::NoResults)?;
131136
let address = Address::from_str(&address).map_err(ApiError::bad_parse)?;
132137
let res = database
133138
.collection::<OutputCollection>()
134-
.get_address_balance(address)
139+
.get_address_balance(address, ledger_index)
135140
.await?
136141
.ok_or(ApiError::NoResults)?;
137142

138143
Ok(BalanceResponse {
139144
total_balance: res.total_balance,
140145
sig_locked_balance: res.sig_locked_balance,
141-
ledger_index: res.ledger_index,
146+
ledger_index,
142147
})
143148
}
144149

src/bin/inx-chronicle/api/stardust/indexer/responses.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright 2022 IOTA Stiftung
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use chronicle::types::tangle::MilestoneIndex;
45
use serde::{Deserialize, Serialize};
56

67
use crate::api::responses::impl_success_response;
78

89
#[derive(Clone, Debug, Serialize, Deserialize)]
910
#[serde(rename_all = "camelCase")]
1011
pub struct IndexerOutputsResponse {
11-
pub ledger_index: u32,
12+
pub ledger_index: MilestoneIndex,
1213
pub items: Vec<String>,
1314
#[serde(skip_serializing_if = "Option::is_none")]
1415
pub cursor: Option<String>,

src/bin/inx-chronicle/api/stardust/indexer/routes.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use axum::{extract::Path, routing::get, Extension};
77
use chronicle::{
88
db::{
99
collections::{
10-
AliasOutputsQuery, BasicOutputsQuery, FoundryOutputsQuery, IndexedId, NftOutputsQuery, OutputCollection,
10+
AliasOutputsQuery, BasicOutputsQuery, FoundryOutputsQuery, IndexedId, MilestoneCollection, NftOutputsQuery,
11+
OutputCollection,
1112
},
1213
MongoDb,
1314
},
@@ -54,14 +55,19 @@ where
5455
ID: Into<IndexedId> + FromStr,
5556
ParseError: From<ID::Err>,
5657
{
58+
let ledger_index = database
59+
.collection::<MilestoneCollection>()
60+
.get_ledger_index()
61+
.await?
62+
.ok_or(ApiError::NoResults)?;
5763
let id = ID::from_str(&id).map_err(ApiError::bad_parse)?;
5864
let res = database
5965
.collection::<OutputCollection>()
60-
.get_indexed_output_by_id(id)
66+
.get_indexed_output_by_id(id, ledger_index)
6167
.await?
6268
.ok_or(ApiError::NoResults)?;
6369
Ok(IndexerOutputsResponse {
64-
ledger_index: res.ledger_index.0,
70+
ledger_index,
6571
items: vec![res.output_id.to_hex()],
6672
cursor: None,
6773
})
@@ -80,6 +86,11 @@ async fn indexed_outputs<Q>(
8086
where
8187
bson::Document: From<Q>,
8288
{
89+
let ledger_index = database
90+
.collection::<MilestoneCollection>()
91+
.get_ledger_index()
92+
.await?
93+
.ok_or(ApiError::NoResults)?;
8394
let res = database
8495
.collection::<OutputCollection>()
8596
.get_indexed_outputs(
@@ -89,9 +100,9 @@ where
89100
cursor,
90101
sort,
91102
include_spent,
103+
ledger_index,
92104
)
93-
.await?
94-
.ok_or(ApiError::NoResults)?;
105+
.await?;
95106

96107
let mut iter = res.outputs.iter();
97108

@@ -109,7 +120,7 @@ where
109120
});
110121

111122
Ok(IndexerOutputsResponse {
112-
ledger_index: res.ledger_index.0,
123+
ledger_index,
113124
items,
114125
cursor,
115126
})

0 commit comments

Comments
 (0)