Skip to content

Commit

Permalink
Add slot related types (#642)
Browse files Browse the repository at this point in the history
* Add SlotIndex

* Slot module

* Add SlotCommitmentId

* Add SlotCommitment::id

* Add RootsId

* Add SlotCommitment

* Add deref

* Added getters

* Use internal index

* Update sdk/src/types/block/slot/commitment.rs

Co-authored-by: Thoralf-M <[email protected]>

* Update sdk/src/types/block/slot/commitment.rs

Co-authored-by: Thoralf-M <[email protected]>

---------

Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
thibault-martinez and Thoralf-M committed Jun 23, 2023
1 parent 94ca205 commit 1bf3283
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sdk/src/types/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub mod rand;
pub mod semantic;
/// A module that provides types and syntactic validations of signatures.
pub mod signature;
/// A module that provides types and syntactic validations of slots.
pub mod slot;
/// A module that provides types and syntactic validations of unlocks.
pub mod unlock;

Expand Down
75 changes: 75 additions & 0 deletions sdk/src/types/block/slot/commitment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crypto::hashes::{blake2b::Blake2b256, Digest};
use packable::{packer::SlicePacker, Packable, PackableExt};

use crate::types::block::slot::{RootsId, SlotCommitmentId, SlotIndex};

/// Contains a summary of a slot.
/// It is linked to the commitment of the previous slot, which forms a commitment chain.
#[derive(Clone, Debug, Eq, PartialEq, Hash, derive_more::From, Packable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SlotCommitment {
/// The slot index of this commitment.
/// It is calculated based on genesis timestamp and the duration of a slot.
index: SlotIndex,
/// The commitment ID of the previous slot.
previous_slot_commitment_id: SlotCommitmentId,
/// A BLAKE2b-256 hash of concatenating multiple sparse merkle tree roots of a slot.
roots_id: RootsId,
/// The sum of previous slot commitment cumulative weight and weight of issuers of accepted blocks within this
/// slot. It is just an indication of "committed into" this slot, and can not strictly be used for evaluating
/// the switching of a chain.
cumulative_weight: u64,
}

impl SlotCommitment {
/// Creates a new [`SlotCommitment`].
pub fn new(
index: SlotIndex,
previous_slot_commitment_id: SlotCommitmentId,
roots_id: RootsId,
cumulative_weight: u64,
) -> Self {
Self {
index,
previous_slot_commitment_id,
roots_id,
cumulative_weight,
}
}

/// Returns the index of the [`SlotCommitment`].
pub fn index(&self) -> SlotIndex {
self.index
}

/// Returns the previous slot commitment ID of the [`SlotCommitment`].
pub fn previous_slot_commitment_id(&self) -> &SlotCommitmentId {
&self.previous_slot_commitment_id
}

/// Returns the [`RootsId`] of the [`SlotCommitment`].
pub fn roots_id(&self) -> &RootsId {
&self.roots_id
}

/// Returns the cumulative weight of the [`SlotCommitment`].
pub fn cumulative_weight(&self) -> u64 {
self.cumulative_weight
}

/// Derives the [`SlotCommitmentId`] of the [`SlotCommitment`].
pub fn id(&self) -> SlotCommitmentId {
let mut bytes = [0u8; SlotCommitmentId::LENGTH];
let mut packer = SlicePacker::new(&mut bytes);
let hash: [u8; 32] = Blake2b256::digest(self.pack_to_vec()).into();

// PANIC: packing to an array of bytes can't fail.
hash.pack(&mut packer).unwrap();
self.index.pack(&mut packer).unwrap();

SlotCommitmentId::from(bytes)
}
}
7 changes: 7 additions & 0 deletions sdk/src/types/block/slot/commitment_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

impl_id!(pub SlotCommitmentId, 40, "Identifier of a slot commitment.");

#[cfg(feature = "serde")]
string_serde_impl!(SlotCommitmentId);
18 changes: 18 additions & 0 deletions sdk/src/types/block/slot/index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use derive_more::{Deref, From};

/// Timeline is divided into slots, and each slot has a corresponding slot index.
/// To calculate the slot index of a timestamp, `genesisTimestamp` and the duration of a slot are needed.
/// The slot index of timestamp `ts` is `(ts - genesisTimestamp)/duration + 1`.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, From, Deref, packable::Packable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SlotIndex(u64);

impl SlotIndex {
/// Creates a new [`SlotIndex`].
pub fn new(index: u64) -> Self {
Self::from(index)
}
}
9 changes: 9 additions & 0 deletions sdk/src/types/block/slot/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod commitment;
mod commitment_id;
mod index;
mod roots_id;

pub use self::{commitment::SlotCommitment, commitment_id::SlotCommitmentId, index::SlotIndex, roots_id::RootsId};
7 changes: 7 additions & 0 deletions sdk/src/types/block/slot/roots_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

impl_id!(pub RootsId, 32, "A BLAKE2b-256 hash of concatenating multiple sparse merkle tree roots of a slot.");

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

0 comments on commit 1bf3283

Please sign in to comment.