Skip to content

Commit

Permalink
add BlockHeaderDto
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Nov 16, 2023
1 parent b52e136 commit d8bd1d5
Show file tree
Hide file tree
Showing 25 changed files with 116 additions and 101 deletions.
16 changes: 8 additions & 8 deletions sdk/src/client/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ impl TryFromDto for PreparedTransactionData {
type Dto = PreparedTransactionDataDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(Self {
transaction: Transaction::try_from_dto_with_params(dto.transaction, &params)
transaction: Transaction::try_from_dto_with_params(dto.transaction, params)
.map_err(|_| Error::InvalidField("transaction"))?,
inputs_data: dto
.inputs_data
.into_iter()
.map(|i| InputSigningData::try_from_dto_with_params(i, &params))
.map(|i| InputSigningData::try_from_dto_with_params(i, params))
.collect::<crate::client::Result<Vec<InputSigningData>>>()
.map_err(|_| Error::InvalidField("input_data"))?,
remainder: match dto.remainder {
Some(remainder) => Some(
RemainderData::try_from_dto_with_params(remainder, &params)
RemainderData::try_from_dto_with_params(remainder, params)
.map_err(|_| Error::InvalidField("remainder"))?,
),
None => None,
Expand Down Expand Up @@ -114,14 +114,14 @@ impl TryFromDto for SignedTransactionData {
type Dto = SignedTransactionDataDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(Self {
payload: SignedTransactionPayload::try_from_dto_with_params(dto.payload, &params)
payload: SignedTransactionPayload::try_from_dto_with_params(dto.payload, params)
.map_err(|_| Error::InvalidField("transaction_payload"))?,
inputs_data: dto
.inputs_data
.into_iter()
.map(|i| InputSigningData::try_from_dto_with_params(i, &params))
.map(|i| InputSigningData::try_from_dto_with_params(i, params))
.collect::<crate::client::Result<Vec<InputSigningData>>>()
.map_err(|_| Error::InvalidField("inputs_data"))?,
})
Expand Down Expand Up @@ -155,7 +155,7 @@ impl TryFromDto for RemainderData {
type Dto = RemainderDataDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(Self {
output: Output::try_from_dto_with_params_inner(dto.output, params)?,
chain: dto.chain,
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(Block::try_from_dto_with_params(
dto,
self.get_protocol_parameters().await?,
&self.get_protocol_parameters().await?,
)?)
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/client/secret/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl TryFromDto for InputSigningData {
type Dto = InputSigningDataDto;
type Error = crate::client::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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(Self {
output: Output::try_from_dto_with_params_inner(dto.output, params)?,
output_metadata: dto.output_metadata,
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub(crate) mod dto {
type Dto = BasicBlockBodyDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
BasicBlockBodyBuilder::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
93 changes: 52 additions & 41 deletions sdk/src/types/block/core/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,7 @@ pub(crate) mod dto {
fn from(value: &Block) -> Self {
Self {
inner: UnsignedBlockDto {
protocol_version: value.protocol_version(),
network_id: value.network_id(),
issuing_time: value.issuing_time(),
slot_commitment_id: value.slot_commitment_id(),
latest_finalized_slot: value.latest_finalized_slot(),
issuer_id: value.issuer_id(),
header: BlockHeaderDto::from(&value.header),
body: BlockBodyDto::from(&value.body),
},
signature: value.signature,
Expand All @@ -382,32 +377,25 @@ pub(crate) mod dto {
type Dto = BlockDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params.protocol_parameters() {
if dto.inner.protocol_version != protocol_params.version() {
if dto.inner.header.protocol_version != protocol_params.version() {
return Err(Error::ProtocolVersionMismatch {
expected: protocol_params.version(),
actual: dto.inner.protocol_version,
actual: dto.inner.header.protocol_version,
});
}

if dto.inner.network_id != protocol_params.network_id() {
if dto.inner.header.network_id != protocol_params.network_id() {
return Err(Error::NetworkIdMismatch {
expected: protocol_params.network_id(),
actual: dto.inner.network_id,
actual: dto.inner.header.network_id,
});
}
}

Ok(Self::new(
BlockHeader::new(
dto.inner.protocol_version,
dto.inner.network_id,
dto.inner.issuing_time,
dto.inner.slot_commitment_id,
dto.inner.latest_finalized_slot,
dto.inner.issuer_id,
),
BlockHeader::try_from_dto_with_params_inner(dto.inner.header, params)?,
BlockBody::try_from_dto_with_params_inner(dto.inner.body, params)?,
dto.signature,
))
Expand All @@ -416,7 +404,7 @@ pub(crate) mod dto {

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnsignedBlockDto {
pub struct BlockHeaderDto {
pub protocol_version: u8,
#[serde(with = "string")]
pub network_id: u64,
Expand All @@ -425,28 +413,26 @@ pub(crate) mod dto {
pub slot_commitment_id: SlotCommitmentId,
pub latest_finalized_slot: SlotIndex,
pub issuer_id: IssuerId,
pub body: BlockBodyDto,
}

impl From<&UnsignedBlock> for UnsignedBlockDto {
fn from(value: &UnsignedBlock) -> Self {
impl From<&BlockHeader> for BlockHeaderDto {
fn from(value: &BlockHeader) -> Self {
Self {
protocol_version: value.header.protocol_version(),
network_id: value.header.network_id(),
issuing_time: value.header.issuing_time(),
slot_commitment_id: value.header.slot_commitment_id(),
latest_finalized_slot: value.header.latest_finalized_slot(),
issuer_id: value.header.issuer_id(),
body: BlockBodyDto::from(&value.body),
protocol_version: value.protocol_version(),
network_id: value.network_id(),
issuing_time: value.issuing_time(),
slot_commitment_id: value.slot_commitment_id(),
latest_finalized_slot: value.latest_finalized_slot(),
issuer_id: value.issuer_id(),
}
}
}

impl TryFromDto for UnsignedBlock {
type Dto = UnsignedBlockDto;
impl TryFromDto for BlockHeader {
type Dto = BlockHeaderDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params.protocol_parameters() {
if dto.protocol_version != protocol_params.version() {
return Err(Error::ProtocolVersionMismatch {
Expand All @@ -464,14 +450,39 @@ pub(crate) mod dto {
}

Ok(Self::new(
BlockHeader::new(
dto.protocol_version,
dto.network_id,
dto.issuing_time,
dto.slot_commitment_id,
dto.latest_finalized_slot,
dto.issuer_id,
),
dto.protocol_version,
dto.network_id,
dto.issuing_time,
dto.slot_commitment_id,
dto.latest_finalized_slot,
dto.issuer_id,
))
}
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnsignedBlockDto {
pub header: BlockHeaderDto,
pub body: BlockBodyDto,
}

impl From<&UnsignedBlock> for UnsignedBlockDto {
fn from(value: &UnsignedBlock) -> Self {
Self {
header: BlockHeaderDto::from(&value.header),
body: BlockBodyDto::from(&value.body),
}
}
}

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

fn try_from_dto_with_params_inner(dto: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(Self::new(
BlockHeader::try_from_dto_with_params_inner(dto.header, params)?,
BlockBody::try_from_dto_with_params_inner(dto.body, params)?,
))
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub(crate) mod dto {
type Dto = BlockBodyDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
match dto {
Self::Dto::Basic(basic_block_body_dto) => {
Ok(BasicBlockBody::try_from_dto_with_params_inner(basic_block_body_dto, params)?.into())
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/core/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub(crate) mod dto {
type Dto = ValidationBlockBodyDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
if let Some(protocol_params) = params.protocol_parameters() {
validate_protocol_params_hash(&dto.protocol_parameters_hash, protocol_params)?;
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/mana/allotment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub(super) mod dto {

fn try_from_dto_with_params_inner(
dto: Self::Dto,
params: crate::types::ValidationParams<'_>,
params: &crate::types::ValidationParams<'_>,
) -> Result<Self, Self::Error> {
Ok(if let Some(params) = params.protocol_parameters() {
Self::new(dto.account_id, dto.mana, params)?
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ pub(crate) mod dto {
type Dto = AccountOutputDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
let mut builder = AccountOutputBuilder::new_with_amount(dto.amount, dto.account_id)
.with_mana(dto.mana)
.with_foundry_counter(dto.foundry_counter)
Expand All @@ -685,7 +685,7 @@ pub(crate) mod dto {
.with_immutable_features(dto.immutable_features);

for u in dto.unlock_conditions {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, params)?);
}

builder.finish()
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ pub(crate) mod dto {
type Dto = AnchorOutputDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
let mut builder = AnchorOutputBuilder::new_with_amount(dto.amount, dto.anchor_id)
.with_mana(dto.mana)
.with_state_index(dto.state_index)
Expand All @@ -768,7 +768,7 @@ pub(crate) mod dto {
.with_state_metadata(dto.state_metadata);

for u in dto.unlock_conditions {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, params)?);
}

builder.finish()
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,14 @@ pub(crate) mod dto {
type Dto = BasicOutputDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
let mut builder = BasicOutputBuilder::new_with_amount(dto.amount)
.with_native_tokens(dto.native_tokens)
.with_mana(dto.mana)
.with_features(dto.features);

for u in dto.unlock_conditions {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, params)?);
}

builder.finish()
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub(crate) mod dto {

fn try_from_dto_with_params_inner(
dto: Self::Dto,
params: crate::types::ValidationParams<'_>,
params: &crate::types::ValidationParams<'_>,
) -> Result<Self, Self::Error> {
let mut builder = DelegationOutputBuilder::new_with_amount(
dto.amount,
Expand All @@ -493,7 +493,7 @@ pub(crate) mod dto {
.with_end_epoch(dto.end_epoch);

for u in dto.unlock_conditions {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, params)?);
}

builder.finish()
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ pub(crate) mod dto {
type Dto = FoundryOutputDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
let mut builder = FoundryOutputBuilder::new_with_amount(dto.amount, dto.serial_number, dto.token_scheme);

for t in dto.native_tokens {
Expand All @@ -734,7 +734,7 @@ pub(crate) mod dto {
}

for u in dto.unlock_conditions {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, params)?);
}

builder.finish()
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ pub mod dto {
type Dto = OutputDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(match dto {
OutputDto::Basic(o) => Self::Basic(BasicOutput::try_from_dto_with_params_inner(o, params)?),
OutputDto::Account(o) => Self::Account(AccountOutput::try_from_dto_with_params_inner(o, params)?),
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,15 +642,15 @@ pub(crate) mod dto {
type Dto = NftOutputDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
let mut builder = NftOutputBuilder::new_with_amount(dto.amount, dto.nft_id)
.with_mana(dto.mana)
.with_native_tokens(dto.native_tokens)
.with_features(dto.features)
.with_immutable_features(dto.immutable_features);

for u in dto.unlock_conditions {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, params)?);
}

builder.finish()
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/output/unlock_condition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ pub mod dto {
type Dto = UnlockConditionDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(match dto {
UnlockConditionDto::Address(v) => Self::Address(v),
UnlockConditionDto::StorageDepositReturn(v) => Self::StorageDepositReturn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub(crate) mod dto {
type Dto = StorageDepositReturnUnlockConditionDto;
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: Self::Dto, params: &ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(if let Some(token_supply) = params.token_supply() {
Self::new(dto.return_address, dto.amount, token_supply)?
} else {
Expand Down
Loading

0 comments on commit d8bd1d5

Please sign in to comment.