-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add creation slot to
TransactionId
and rework some stuff (#1414)
* Add creation slot to transaction id and rework some stuff * fix tests * use a macro to define ids with a slot index * fix tests * debug prints ID * use size of OutputIndex * fix no_std * suggestion * rework macros * make the macro prettier * rename with_slot_index * rename output fn * fix doc links --------- Co-authored-by: Thibault Martinez <[email protected]>
- Loading branch information
1 parent
14e8a4a
commit 100b62a
Showing
75 changed files
with
440 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,5 +46,5 @@ impl Input { | |
} | ||
} | ||
|
||
def_is_as_opt!(Input: Utxo); | ||
crate::def_is_as_opt!(Input: Utxo); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.