Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creation slot to TransactionId and rework some stuff #1414

Merged
merged 27 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions sdk/src/types/api/plugins/participation/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use getset::Getters;
use hashbrown::HashMap;
use packable::PackableExt;

#[cfg(feature = "serde")]
use crate::types::block::string_serde_impl;
use crate::types::{api::plugins::participation::error::Error, block::impl_id};

/// Participation tag.
Expand Down Expand Up @@ -53,9 +51,12 @@ pub struct ParticipationEvent {
pub data: ParticipationEventData,
}

impl_id!(pub ParticipationEventId, 32, "A participation event id.");
#[cfg(feature = "serde")]
string_serde_impl!(ParticipationEventId);
impl_id!(
/// A participation event id.
pub ParticipationEventId {
pub const LENGTH: usize = 32;
}
);

/// Information about a voting or staking event.
#[derive(Debug, Clone, Eq, PartialEq, Getters)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ mod dto {
}
}

impl_serde_typed_dto!(AccountAddress, AccountAddressDto, "account address");
crate::impl_serde_typed_dto!(AccountAddress, AccountAddressDto, "account address");
}
4 changes: 2 additions & 2 deletions sdk/src/types/block/address/bech32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl PartialEq<str> for Hrp {
}

#[cfg(feature = "serde")]
string_serde_impl!(Hrp);
crate::string_serde_impl!(Hrp);

impl<T: AsRef<str> + Send> ConvertTo<Hrp> for T {
fn convert(self) -> Result<Hrp, Error> {
Expand Down Expand Up @@ -217,7 +217,7 @@ impl<T: core::borrow::Borrow<Bech32Address>> From<T> for Address {
}

#[cfg(feature = "serde")]
string_serde_impl!(Bech32Address);
crate::string_serde_impl!(Bech32Address);

impl<T: AsRef<str> + Send> ConvertTo<Bech32Address> for T {
fn convert(self) -> Result<Bech32Address, Error> {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ pub(crate) mod dto {
}
}

impl_serde_typed_dto!(Ed25519Address, Ed25519AddressDto, "ed25519 address");
crate::impl_serde_typed_dto!(Ed25519Address, Ed25519AddressDto, "ed25519 address");
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/implicit_account_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) mod dto {
}
}

impl_serde_typed_dto!(
crate::impl_serde_typed_dto!(
ImplicitAccountCreationAddress,
ImplicitAccountCreationAddressDto,
"implicit account creation address"
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Address {
}
}

def_is_as_opt!(Address: Ed25519, Account, Nft, ImplicitAccountCreation, Restricted);
crate::def_is_as_opt!(Address: Ed25519, Account, Nft, ImplicitAccountCreation, Restricted);

/// Tries to create an [`Address`] from a bech32 encoded string.
pub fn try_from_bech32(address: impl AsRef<str>) -> Result<Self, Error> {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ pub(crate) mod dto {
}
}

impl_serde_typed_dto!(NftAddress, NftAddressDto, "nft address");
crate::impl_serde_typed_dto!(NftAddress, NftAddressDto, "nft address");
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/restricted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,5 @@ pub(crate) mod dto {
}
}

impl_serde_typed_dto!(RestrictedAddress, RestrictedAddressDto, "restricted address");
crate::impl_serde_typed_dto!(RestrictedAddress, RestrictedAddressDto, "restricted address");
}
122 changes: 8 additions & 114 deletions sdk/src/types/block/block_id.rs
Original file line number Diff line number Diff line change
@@ -1,117 +1,11 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use super::{slot::SlotIndex, ConvertTo};
use crate::types::block::Error;

impl_id!(pub BlockHash, 32, "The hash of a [`Block`].");

impl BlockHash {
#[cfg(target_endian = "little")]
pub fn with_slot_index(self, slot_index: SlotIndex) -> BlockId {
BlockId { hash: self, slot_index }
}

#[cfg(target_endian = "big")]
pub fn with_slot_index(self, slot_index: SlotIndex) -> BlockId {
BlockId {
hash: self,
slot_index: slot_index.to_le().into(),
}
}
}

/// A block identifier.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Debug, packable::Packable)]
#[packable(unpack_error = Error)]
#[repr(C)]
pub struct BlockId {
pub(crate) hash: BlockHash,
// IMPORTANT: On big-endian systems this value is misrepresented because it is transmuted directly
// from bytes, so the getter below handles that conversion. Do not access it directly.
slot_index: SlotIndex,
}

impl BlockId {
/// The length of a [`BlockId`]
pub const LENGTH: usize = 36;

pub fn new(bytes: [u8; Self::LENGTH]) -> Self {
unsafe { core::mem::transmute(bytes) }
}

/// Returns the [`BlockId`]'s hash part.
pub fn hash(&self) -> &BlockHash {
&self.hash
}

/// Returns the [`BlockId`]'s slot index part.
#[cfg(target_endian = "little")]
pub fn slot_index(&self) -> SlotIndex {
self.slot_index
}

/// Returns the [`BlockId`]'s slot index part.
#[cfg(target_endian = "big")]
pub fn slot_index(&self) -> SlotIndex {
self.slot_index.to_le().into()
}
}

impl AsRef<[u8]> for BlockId {
fn as_ref(&self) -> &[u8] {
unsafe { core::mem::transmute::<_, &[u8; Self::LENGTH]>(self) }
}
}

impl core::str::FromStr for BlockId {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::new(prefix_hex::decode(s).map_err(Error::Hex)?))
}
}

impl core::fmt::Display for BlockId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
prefix_hex::encode(self.as_ref()).fmt(f)
}
}

impl TryFrom<&alloc::string::String> for BlockId {
type Error = Error;

fn try_from(s: &alloc::string::String) -> Result<Self, Self::Error> {
core::str::FromStr::from_str(s.as_str())
}
}

impl TryFrom<&str> for BlockId {
type Error = Error;

fn try_from(s: &str) -> Result<Self, Self::Error> {
core::str::FromStr::from_str(s)
}
}

impl ConvertTo<BlockId> for &alloc::string::String {
fn convert(self) -> Result<BlockId, Error> {
self.try_into()
}
}

impl ConvertTo<BlockId> for &str {
fn convert(self) -> Result<BlockId, Error> {
self.try_into()
}
}

impl core::ops::Deref for BlockId {
type Target = [u8; Self::LENGTH];

fn deref(&self) -> &Self::Target {
unsafe { core::mem::transmute::<_, &[u8; Self::LENGTH]>(self) }
}
}
#[cfg(feature = "serde")]
string_serde_impl!(BlockId);
crate::impl_id!(
/// The hash of a [`Block`](crate::types::block::Block).
pub BlockHash {
pub const LENGTH: usize = 32;
}
/// A [`Block`](crate::types::block::Block) identifier.
pub BlockId;
);
2 changes: 1 addition & 1 deletion sdk/src/types/block/context_input/block_issuance_credit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod dto {
}
}

impl_serde_typed_dto!(
crate::impl_serde_typed_dto!(
BlockIssuanceCreditContextInput,
BlockIssuanceCreditContextInputDto,
"block issuance credit context input"
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/context_input/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod dto {
Self::new(value.commitment_id)
}
}
impl_serde_typed_dto!(
crate::impl_serde_typed_dto!(
CommitmentContextInput,
CommitmentContextInputDto,
"commitment context input"
Expand Down
20 changes: 8 additions & 12 deletions sdk/src/types/block/context_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ContextInput {
}
}

def_is_as_opt!(ContextInput: Commitment, BlockIssuanceCredit, Reward);
crate::def_is_as_opt!(ContextInput: Commitment, BlockIssuanceCredit, Reward);
}

#[cfg(test)]
Expand All @@ -69,30 +69,26 @@ mod tests {

#[test]
fn test_commitment() {
let commitment: ContextInput = serde_json::from_str(
r#"
let commitment: ContextInput = serde_json::from_value(serde_json::json!(
{
"type": 0,
"commitmentId": "0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d82bf96689"
"commitmentId": "0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d8"
}
"#,
)
))
.unwrap();
assert!(commitment.is_commitment());
assert_eq!(
commitment.as_commitment().commitment_id().to_string(),
"0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d82bf96689"
"0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d8"
);

// Test wrong type returns error.
let commitment_deserialization_result: Result<ContextInput, _> = serde_json::from_str(
r#"
let commitment_deserialization_result: Result<ContextInput, _> = serde_json::from_value(serde_json::json!(
{
"type": 2,
"commitmentId": "0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d82bf96689"
"commitmentId": "0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d8"
}
"#,
);
));
assert!(commitment_deserialization_result.is_err());
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/context_input/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ mod dto {
}
}

impl_serde_typed_dto!(RewardContextInput, RewardContextInputDto, "reward context input");
crate::impl_serde_typed_dto!(RewardContextInput, RewardContextInputDto, "reward context input");
}
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 @@ -95,7 +95,7 @@ impl Block {
ValidationBlockBuilder::new(strong_parents, highest_supported_version, protocol_parameters_hash)
}

def_is_as_opt!(Block: Basic, Validation);
crate::def_is_as_opt!(Block: Basic, Validation);

pub(crate) fn hash(&self) -> [u8; 32] {
Blake2b256::digest(self.pack_to_vec()).into()
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/core/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl BlockWrapper {
]
.concat();
let block_hash = BlockHash::new(Blake2b256::digest(id).into());
block_hash.with_slot_index(protocol_params.slot_index(self.header.issuing_time() / 1000000000))
block_hash.into_block_id(protocol_params.slot_index(self.header.issuing_time() / 1000000000))
}

/// Unpacks a [`BlockWrapper`] from a sequence of bytes doing syntactical checks and verifying that
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ impl Input {
}
}

def_is_as_opt!(Input: Utxo);
crate::def_is_as_opt!(Input: Utxo);
}
2 changes: 1 addition & 1 deletion sdk/src/types/block/input/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ pub(crate) mod dto {
}
}

impl_serde_typed_dto!(UtxoInput, UtxoInputDto, "UTXO input");
crate::impl_serde_typed_dto!(UtxoInput, UtxoInputDto, "UTXO input");
}
12 changes: 5 additions & 7 deletions sdk/src/types/block/issuer_id.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

impl_id!(
pub IssuerId,
32,
"Identifier of a block issuer."
crate::impl_id!(
/// A unique identifier of a block issuer.
pub IssuerId {
pub const LENGTH: usize = 32;
}
);

#[cfg(feature = "serde")]
string_serde_impl!(IssuerId);
Loading