Skip to content

Commit

Permalink
chore(db)!: direct dto conversion (#752)
Browse files Browse the repository at this point in the history
* Impl into dtos for db types

* Update bee to access remaining dtos. Fix clippy lints.

* Remove serde feature from bee-block

* Update src/types/stardust/block/payload/milestone/mod.rs

Co-authored-by: Jochen Görtler <grtlr@users.noreply.github.com>

Co-authored-by: Jochen Görtler <grtlr@users.noreply.github.com>
  • Loading branch information
DaughterOfMars and grtlr committed Sep 29, 2022
1 parent 4515374 commit ce584ac
Show file tree
Hide file tree
Showing 32 changed files with 593 additions and 149 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Expand Up @@ -76,13 +76,13 @@ tonic = { version = "0.8", default-features = false, optional = true }
metrics = { version = "0.20.0", default-features = false }
metrics-exporter-prometheus = { version = "0.11.0", default-features = false, features = [ "http-listener", "tokio" ] }
metrics-util = { version = "0.14.0", default-features = false }
opentelemetry = { version = "0.18", default-features = false, features = ["trace", "rt-tokio"], optional = true }
opentelemetry-jaeger = { version = "0.17", default-features = false, features = ["rt-tokio"], optional = true }
tracing-opentelemetry = { version = "0.18", default-features = false, features = ["tracing-log"], optional = true }
opentelemetry = { version = "0.18", default-features = false, features = [ "trace", "rt-tokio" ], optional = true }
opentelemetry-jaeger = { version = "0.17", default-features = false, features = [ "rt-tokio" ], optional = true }
tracing-opentelemetry = { version = "0.18", default-features = false, features = [ "tracing-log" ], optional = true }

# Stardust types
bee-api-types-stardust = { package = "bee-api-types", version = "1.0.0", default-features = false, features = ["axum"], optional = true }
bee-block-stardust = { package = "bee-block", version = "1.0.0", default-features = false, features = [ "dto", "std", "rand", "serde"], optional = true }
bee-api-types-stardust = { package = "bee-api-types", version = "1.0.1", default-features = false, features = [ "axum" ], optional = true }
bee-block-stardust = { package = "bee-block", version = "1.0.1", default-features = false, features = [ "dto", "std", "rand"], optional = true }

[dev-dependencies]
rand = { version = "0.8", default-features = false, features = [ "std" ] }
Expand Down
114 changes: 26 additions & 88 deletions src/bin/inx-chronicle/api/stardust/core/routes.rs
Expand Up @@ -17,11 +17,7 @@ use bee_api_types_stardust::{
StatusResponse, TreasuryResponse, UtxoChangesResponse,
},
};
use bee_block_stardust::{
output::dto::OutputDto,
payload::{dto::MilestonePayloadDto, milestone::option::dto::MilestoneOptionDto},
BlockDto,
};
use bee_block_stardust::payload::milestone::option::dto::MilestoneOptionDto;
use chronicle::{
db::{
collections::{
Expand All @@ -31,7 +27,7 @@ use chronicle::{
MongoDb,
},
types::{
context::{TryFromWithContext, TryIntoWithContext},
context::TryFromWithContext,
stardust::block::{
output::OutputId,
payload::{milestone::MilestoneId, transaction::TransactionId},
Expand Down Expand Up @@ -207,18 +203,7 @@ async fn block(
.await?
.ok_or(ApiError::NoResults)?;

let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_version(block.protocol_version)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

Ok(BlockResponse::Json(BlockDto::try_from_with_context(
&protocol_params,
block,
)?))
Ok(BlockResponse::Json(block.into()))
}

async fn block_metadata(
Expand All @@ -234,7 +219,7 @@ async fn block_metadata(

Ok(BlockMetadataResponse {
block_id: block_id_str,
parents: metadata.parents.iter().map(|id| id.to_hex()).collect(),
parents: metadata.parents.iter().map(BlockId::to_hex).collect(),
is_solid: metadata.is_solid,
referenced_by_milestone_index: Some(*metadata.referenced_by_milestone_index),
milestone_index: Some(*metadata.milestone_index),
Expand Down Expand Up @@ -288,17 +273,9 @@ async fn output(database: Extension<MongoDb>, Path(output_id): Path<String>) ->

let metadata = create_output_metadata_response(metadata, ledger_index);

let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(ledger_index)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

Ok(OutputResponse {
metadata,
output: OutputDto::try_from_with_context(&protocol_params, output)?,
output: output.into(),
})
}

Expand Down Expand Up @@ -332,36 +309,14 @@ async fn transaction_included_block(
.await?
.ok_or(ApiError::NoResults)?;

let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_version(block.protocol_version)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

Ok(BlockResponse::Json(BlockDto::try_from_with_context(
&protocol_params,
block,
)?))
Ok(BlockResponse::Json(block.into()))
}

async fn receipts(database: Extension<MongoDb>) -> ApiResult<ReceiptsResponse> {
let mut receipts_at = database.collection::<MilestoneCollection>().get_all_receipts().await?;
let mut receipts = Vec::new();
while let Some((receipt, at)) = receipts_at.try_next().await? {
let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(at)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;
let receipt: &bee_block_stardust::payload::milestone::MilestoneOption =
&receipt.try_into_with_context(&protocol_params)?;
let receipt: bee_block_stardust::payload::milestone::option::dto::MilestoneOptionDto = receipt.into();

if let MilestoneOptionDto::Receipt(receipt) = receipt {
if let MilestoneOptionDto::Receipt(receipt) = receipt.into() {
receipts.push(ReceiptDto {
receipt,
milestone_index: *at,
Expand All @@ -378,20 +333,9 @@ async fn receipts_migrated_at(database: Extension<MongoDb>, Path(index): Path<u3
.collection::<MilestoneCollection>()
.get_receipts_migrated_at(index.into())
.await?;
let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(index.into())
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;
let mut receipts = Vec::new();
while let Some((receipt, at)) = receipts_at.try_next().await? {
let receipt: &bee_block_stardust::payload::milestone::MilestoneOption =
&receipt.try_into_with_context(&protocol_params)?;
let receipt: bee_block_stardust::payload::milestone::option::dto::MilestoneOptionDto = receipt.into();

if let MilestoneOptionDto::Receipt(receipt) = receipt {
if let MilestoneOptionDto::Receipt(receipt) = receipt.into() {
receipts.push(ReceiptDto {
receipt,
milestone_index: *at,
Expand Down Expand Up @@ -427,15 +371,15 @@ async fn milestone(
.await?
.ok_or(ApiError::NoResults)?;

let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(milestone_payload.essence.index)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

if let Some(value) = headers.get(axum::http::header::ACCEPT) {
let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(milestone_payload.essence.index)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

if value.eq(&*BYTE_CONTENT_HEADER) {
let milestone_payload = bee_block_stardust::payload::MilestonePayload::try_from_with_context(
&protocol_params,
Expand All @@ -445,10 +389,7 @@ async fn milestone(
}
}

Ok(MilestoneResponse::Json(MilestonePayloadDto::try_from_with_context(
&protocol_params,
milestone_payload,
)?))
Ok(MilestoneResponse::Json(milestone_payload.into()))
}

async fn milestone_by_index(
Expand All @@ -462,16 +403,16 @@ async fn milestone_by_index(
.await?
.ok_or(ApiError::NoResults)?;

let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(milestone_payload.essence.index)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

if let Some(value) = headers.get(axum::http::header::ACCEPT) {
if value.eq(&*BYTE_CONTENT_HEADER) {
let protocol_params = database
.collection::<ProtocolUpdateCollection>()
.get_protocol_parameters_for_ledger_index(milestone_payload.essence.index)
.await?
.ok_or(ApiError::NoResults)?
.parameters
.try_into()?;

let milestone_payload = bee_block_stardust::payload::MilestonePayload::try_from_with_context(
&protocol_params,
milestone_payload,
Expand All @@ -480,10 +421,7 @@ async fn milestone_by_index(
}
}

Ok(MilestoneResponse::Json(MilestonePayloadDto::try_from_with_context(
&protocol_params,
milestone_payload,
)?))
Ok(MilestoneResponse::Json(milestone_payload.into()))
}

async fn utxo_changes(
Expand Down
6 changes: 6 additions & 0 deletions src/types/stardust/block/address/alias.rs
Expand Up @@ -25,6 +25,12 @@ impl From<AliasAddress> for bee::AliasAddress {
}
}

impl From<AliasAddress> for bee::dto::AliasAddressDto {
fn from(value: AliasAddress) -> Self {
Into::into(&bee::AliasAddress::from(value))
}
}

impl FromStr for AliasAddress {
type Err = bee_block_stardust::Error;

Expand Down
6 changes: 6 additions & 0 deletions src/types/stardust/block/address/ed25519.rs
Expand Up @@ -29,6 +29,12 @@ impl From<Ed25519Address> for bee::Ed25519Address {
}
}

impl From<Ed25519Address> for bee::dto::Ed25519AddressDto {
fn from(value: Ed25519Address) -> Self {
Into::into(&bee::Ed25519Address::from(value))
}
}

impl FromStr for Ed25519Address {
type Err = bee_block_stardust::Error;

Expand Down
10 changes: 10 additions & 0 deletions src/types/stardust/block/address/mod.rs
Expand Up @@ -51,6 +51,16 @@ impl From<Address> for bee::Address {
}
}

impl From<Address> for bee::dto::AddressDto {
fn from(value: Address) -> Self {
match value {
Address::Ed25519(a) => Self::Ed25519(a.into()),
Address::Alias(a) => Self::Alias(a.into()),
Address::Nft(a) => Self::Nft(a.into()),
}
}
}

impl FromStr for Address {
type Err = bee_block_stardust::Error;

Expand Down
6 changes: 6 additions & 0 deletions src/types/stardust/block/address/nft.rs
Expand Up @@ -25,6 +25,12 @@ impl From<NftAddress> for bee::NftAddress {
}
}

impl From<NftAddress> for bee::dto::NftAddressDto {
fn from(value: NftAddress) -> Self {
Into::into(&bee::NftAddress::from(value))
}
}

impl FromStr for NftAddress {
type Err = bee_block_stardust::Error;

Expand Down
16 changes: 16 additions & 0 deletions src/types/stardust/block/input.rs
Expand Up @@ -35,6 +35,22 @@ impl TryFrom<Input> for bee::Input {
}
}

impl From<Input> for bee::dto::InputDto {
fn from(value: Input) -> Self {
match value {
Input::Utxo(output_id) => Self::Utxo(bee::dto::UtxoInputDto {
kind: bee::UtxoInput::KIND,
transaction_id: output_id.transaction_id.to_hex(),
transaction_output_index: output_id.index,
}),
Input::Treasury { milestone_id } => Self::Treasury(bee::dto::TreasuryInputDto {
kind: bee::TreasuryInput::KIND,
milestone_id: milestone_id.to_hex(),
}),
}
}
}

#[cfg(feature = "rand")]
mod rand {

Expand Down
13 changes: 12 additions & 1 deletion src/types/stardust/block/mod.rs
Expand Up @@ -47,7 +47,7 @@ impl TryFromWithContext<Block> for bee::Block {

fn try_from_with_context(ctx: &ProtocolParameters, value: Block) -> Result<Self, Self::Error> {
let mut builder = bee::BlockBuilder::<u64>::new(bee::parent::Parents::new(
Vec::from(value.parents).into_iter().map(Into::into).collect::<Vec<_>>(),
value.parents.into_vec().into_iter().map(Into::into).collect::<Vec<_>>(),
)?)
.with_nonce_provider(value.nonce);
if let Some(payload) = value.payload {
Expand All @@ -66,6 +66,17 @@ impl TryFromWithContext<Block> for bee::BlockDto {
}
}

impl From<Block> for bee::BlockDto {
fn from(value: Block) -> Self {
Self {
protocol_version: value.protocol_version,
parents: value.parents.to_vec().iter().map(BlockId::to_hex).collect(),
payload: value.payload.map(Into::into),
nonce: value.nonce.to_string(),
}
}
}

#[cfg(feature = "rand")]
mod rand {
use bee::rand::number::rand_number;
Expand Down

0 comments on commit ce584ac

Please sign in to comment.