Skip to content

Commit

Permalink
refactor TryFromDto
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Nov 16, 2023
1 parent 5dba015 commit 09ea0ce
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 202 deletions.
2 changes: 1 addition & 1 deletion bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
client
.post_block(&SignedBlock::try_from_dto_with_params(
block,
client.get_protocol_parameters().await?,
&client.get_protocol_parameters().await?,
)?)
.await?,
),
Expand Down
4 changes: 2 additions & 2 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
.sign_and_submit_transaction(
PreparedTransactionData::try_from_dto_with_params(
prepared_transaction_data,
wallet.client().get_protocol_parameters().await?,
&wallet.client().get_protocol_parameters().await?,
)?,
None,
)
Expand All @@ -387,7 +387,7 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
} => {
let signed_transaction_data = SignedTransactionData::try_from_dto_with_params(
signed_transaction_data,
wallet.client().get_protocol_parameters().await?,
&wallet.client().get_protocol_parameters().await?,
)?;
let transaction = wallet
.submit_and_store_transaction(signed_transaction_data, None)
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/wallet/offline_signing/3_send_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn read_signed_transaction_from_file(client: &Client) -> Result<SignedTran

Ok(SignedTransactionData::try_from_dto_with_params(
dto,
client.get_protocol_parameters().await?,
&client.get_protocol_parameters().await?,
)?)
}

Expand Down
23 changes: 14 additions & 9 deletions sdk/src/client/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use crate::{
},
SignedTransactionPayload,
},
protocol::ProtocolParameters,
Error,
},
TryFromDto, ValidationParams,
TryFromDto,
},
utils::serde::bip44::option_bip44,
};
Expand Down Expand Up @@ -57,13 +58,15 @@ impl From<&PreparedTransactionData> for PreparedTransactionDataDto {
}
}

impl TryFromDto for PreparedTransactionData {
type Dto = PreparedTransactionDataDto;
impl TryFromDto<PreparedTransactionDataDto> for PreparedTransactionData {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from_dto_with_params_inner(
dto: PreparedTransactionDataDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
Ok(Self {
transaction: Transaction::try_from_dto_with_params(dto.transaction, &params)
transaction: Transaction::try_from_dto_with_params_inner(dto.transaction, params)
.map_err(|_| Error::InvalidField("transaction"))?,
inputs_data: dto
.inputs_data
Expand Down Expand Up @@ -109,13 +112,15 @@ impl From<&SignedTransactionData> for SignedTransactionDataDto {
}
}

impl TryFromDto for SignedTransactionData {
type Dto = SignedTransactionDataDto;
impl TryFromDto<SignedTransactionDataDto> for SignedTransactionData {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from_dto_with_params_inner(
dto: SignedTransactionDataDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
Ok(Self {
payload: SignedTransactionPayload::try_from_dto_with_params(dto.payload, &params)
payload: SignedTransactionPayload::try_from_dto_with_params_inner(dto.payload, params)
.map_err(|_| Error::InvalidField("transaction_payload"))?,
inputs_data: dto
.inputs_data
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl ClientInner {

Ok(SignedBlock::try_from_dto_with_params(
dto,
self.get_protocol_parameters().await?,
&self.get_protocol_parameters().await?,
)?)
}

Expand Down Expand Up @@ -256,7 +256,7 @@ impl ClientInner {

Ok(SignedBlock::try_from_dto_with_params(
dto,
self.get_protocol_parameters().await?,
&self.get_protocol_parameters().await?,
)?)
}

Expand Down
10 changes: 6 additions & 4 deletions sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub(crate) mod dto {
use super::*;
use crate::types::{
block::{payload::dto::PayloadDto, BlockId, Error},
TryFromDto, ValidationParams,
TryFromDto,
};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -205,11 +205,13 @@ pub(crate) mod dto {
}
}

impl TryFromDto for BasicBlock {
type Dto = BasicBlockDto;
impl TryFromDto<BasicBlockDto> for BasicBlock {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from_dto_with_params_inner(
dto: BasicBlockDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
BasicBlockBuilder::new(StrongParents::from_set(dto.strong_parents)?, dto.max_burned_mana)
.with_weak_parents(WeakParents::from_set(dto.weak_parents)?)
.with_shallow_like_parents(ShallowLikeParents::from_set(dto.shallow_like_parents)?)
Expand Down
14 changes: 8 additions & 6 deletions sdk/src/types/block/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub(crate) mod dto {
pub use crate::types::block::core::signed_block::dto::{SignedBlockDto, UnsignedBlockDto};
use crate::types::{
block::core::{basic::dto::BasicBlockDto, validation::dto::ValidationBlockDto},
TryFromDto, ValidationParams,
TryFromDto,
};

#[derive(Clone, Debug, Eq, PartialEq, From)]
Expand Down Expand Up @@ -195,14 +195,16 @@ pub(crate) mod dto {
}
}

impl TryFromDto for Block {
type Dto = BlockDto;
impl TryFromDto<BlockDto> for Block {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from_dto_with_params_inner(
dto: BlockDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
match dto {
Self::Dto::Basic(basic) => Ok(BasicBlock::try_from_dto_with_params_inner(basic, params)?.into()),
Self::Dto::Validation(validation) => {
BlockDto::Basic(basic) => Ok(BasicBlock::try_from_dto_with_params_inner(basic, params)?.into()),
BlockDto::Validation(validation) => {
Ok(ValidationBlock::try_from_dto_with_params_inner(validation, params)?.into())
}
}
Expand Down
22 changes: 13 additions & 9 deletions sdk/src/types/block/core/signed_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub(crate) mod dto {

use super::*;
use crate::{
types::{block::core::dto::BlockDto, TryFromDto, ValidationParams},
types::{block::core::dto::BlockDto, TryFromDto},
utils::serde::string,
};

Expand Down Expand Up @@ -380,12 +380,14 @@ pub(crate) mod dto {
}
}

impl TryFromDto for SignedBlock {
type Dto = SignedBlockDto;
impl TryFromDto<SignedBlockDto> for SignedBlock {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params.protocol_parameters() {
fn try_from_dto_with_params_inner(
dto: SignedBlockDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params {
if dto.inner.protocol_version != protocol_params.version() {
return Err(Error::ProtocolVersionMismatch {
expected: protocol_params.version(),
Expand Down Expand Up @@ -444,12 +446,14 @@ pub(crate) mod dto {
}
}

impl TryFromDto for UnsignedBlock {
type Dto = UnsignedBlockDto;
impl TryFromDto<UnsignedBlockDto> for UnsignedBlock {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params.protocol_parameters() {
fn try_from_dto_with_params_inner(
dto: UnsignedBlockDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params {
if dto.protocol_version != protocol_params.version() {
return Err(Error::ProtocolVersionMismatch {
expected: protocol_params.version(),
Expand Down
12 changes: 7 additions & 5 deletions sdk/src/types/block/core/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub(crate) mod dto {
use super::*;
use crate::types::{
block::{BlockId, Error},
TryFromDto, ValidationParams,
TryFromDto,
};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -230,12 +230,14 @@ pub(crate) mod dto {
}
}

impl TryFromDto for ValidationBlock {
type Dto = ValidationBlockDto;
impl TryFromDto<ValidationBlockDto> for ValidationBlock {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params.protocol_parameters() {
fn try_from_dto_with_params_inner(
dto: ValidationBlockDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params {
verify_protocol_parameters_hash::<true>(&dto.protocol_parameters_hash, protocol_params)?;
}

Expand Down
9 changes: 4 additions & 5 deletions sdk/src/types/block/mana/allotment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ pub(super) mod dto {
}
}

impl TryFromDto for ManaAllotment {
type Dto = ManaAllotmentDto;
impl TryFromDto<ManaAllotmentDto> for ManaAllotment {
type Error = Error;

fn try_from_dto_with_params_inner(
dto: Self::Dto,
params: crate::types::ValidationParams<'_>,
dto: ManaAllotmentDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
Ok(if let Some(params) = params.protocol_parameters() {
Ok(if let Some(params) = params {
Self::new(dto.account_id, dto.mana, params)?
} else {
Self {
Expand Down
10 changes: 6 additions & 4 deletions sdk/src/types/block/payload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub mod dto {

pub use super::signed_transaction::dto::SignedTransactionPayloadDto;
use super::*;
use crate::types::{block::Error, TryFromDto, ValidationParams};
use crate::types::{block::Error, TryFromDto};

/// Describes all the different payload types.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -197,11 +197,13 @@ pub mod dto {
}
}

impl TryFromDto for Payload {
type Dto = PayloadDto;
impl TryFromDto<PayloadDto> for Payload {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from_dto_with_params_inner(
dto: PayloadDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
Ok(match dto {
PayloadDto::TaggedData(p) => Self::from(*p),
PayloadDto::SignedTransaction(p) => {
Expand Down
10 changes: 6 additions & 4 deletions sdk/src/types/block/payload/signed_transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub mod dto {
use super::*;
use crate::types::{
block::{unlock::Unlock, Error},
TryFromDto, ValidationParams,
TryFromDto,
};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand All @@ -112,11 +112,13 @@ pub mod dto {
}
}

impl TryFromDto for SignedTransactionPayload {
type Dto = SignedTransactionPayloadDto;
impl TryFromDto<SignedTransactionPayloadDto> for SignedTransactionPayload {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from_dto_with_params_inner(
dto: SignedTransactionPayloadDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
let transaction = Transaction::try_from_dto_with_params_inner(dto.transaction, params)?;
Self::new(transaction, Unlocks::new(dto.unlocks)?)
}
Expand Down
Loading

0 comments on commit 09ea0ce

Please sign in to comment.