Skip to content

Commit

Permalink
Add commitment routes (#651)
Browse files Browse the repository at this point in the history
* Add Commitments routes

* Fix no_std

* Fix get_slot_commitment_by_index_raw name

* Add GET

* derive_more Display
  • Loading branch information
thibault-martinez committed Jun 22, 2023
1 parent 63ed7bf commit 7dd3c2e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ rustdoc-args = [ "--cfg", "docsrs" ]
# Mandatory dependencies
bech32 = { version = "0.9.1", default-features = false }
bitflags = { version = "2.3.1", default-features = false }
derive_more = { version = "0.99.17", default-features = false, features = [ "from", "as_ref", "deref", "deref_mut" ] }
derive_more = { version = "0.99.17", default-features = false, features = [ "from", "as_ref", "deref", "deref_mut", "display" ] }
getset = { version = "0.1.2", default-features = false }
hashbrown = { version = "0.13.2", default-features = false, features = [ "ahash", "inline-more" ] }
hex = { version = "0.4.3", default-features = false }
Expand Down
55 changes: 52 additions & 3 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{
block::{
output::{dto::OutputMetadataDto, Output, OutputId, OutputMetadata, OutputWithMetadata},
payload::transaction::TransactionId,
slot::{SlotCommitment, SlotCommitmentId, SlotIndex},
Block, BlockDto, BlockId,
},
},
Expand Down Expand Up @@ -91,7 +92,7 @@ impl ClientInner {
.await
}

// Tangle routes.
// Blocks routes.

/// Returns tips that are ideal for attaching a block.
/// GET /api/core/v3/tips
Expand All @@ -111,8 +112,6 @@ impl ClientInner {
.collect::<Result<Vec<_>>>()
}

// Blocks routes.

/// Returns the BlockId of the submitted block.
/// POST JSON to /api/core/v3/blocks
pub async fn post_block(&self, block: &Block) -> Result<BlockId> {
Expand Down Expand Up @@ -349,6 +348,56 @@ impl ClientInner {
.await
}

// Commitments routes.

/// Gets the slot commitment by the given slot commitment id.
/// GET /api/core/v3/commitments/{commitmentId}
pub async fn get_slot_commitment_by_id(&self, slot_commitment_id: &SlotCommitmentId) -> Result<SlotCommitment> {
let path = &format!("api/core/v3/commitments/{slot_commitment_id}");

self.node_manager
.read()
.await
.get_request::<SlotCommitment>(path, None, self.get_timeout().await, false, true)
.await
}

/// Gets the slot commitment, as raw bytes, by the given slot commitment id.
/// GET /api/core/v3/commitments/{commitmentId}
pub async fn get_slot_commitment_by_id_raw(&self, slot_commitment_id: &SlotCommitmentId) -> Result<Vec<u8>> {
let path = &format!("api/core/v3/commitments/{slot_commitment_id}");

self.node_manager
.read()
.await
.get_request_bytes(path, None, self.get_timeout().await)
.await
}

/// Gets the slot commitment by the given slot index.
/// GET /api/core/v3/commitments/by-index/{index}
pub async fn get_slot_commitment_by_index(&self, slot_index: &SlotIndex) -> Result<SlotCommitment> {
let path = &format!("api/core/v3/commitments/by-index/{slot_index}");

self.node_manager
.read()
.await
.get_request::<SlotCommitment>(path, None, self.get_timeout().await, false, true)
.await
}

/// Gets the slot commitment, as raw bytes, by the given slot index.
/// GET /api/core/v3/commitments/by-index/{index}
pub async fn get_slot_commitment_by_index_raw(&self, slot_index: &SlotIndex) -> Result<Vec<u8>> {
let path = &format!("api/core/v3/commitments/by-index/{slot_index}");

self.node_manager
.read()
.await
.get_request_bytes(path, None, self.get_timeout().await)
.await
}

// Peers routes.

/// GET /api/core/v3/peers
Expand Down
7 changes: 6 additions & 1 deletion sdk/src/types/block/slot/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ 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))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
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.
#[cfg_attr(feature = "serde", serde(rename = "prevId"))]
previous_slot_commitment_id: SlotCommitmentId,
/// A BLAKE2b-256 hash of concatenating multiple sparse merkle tree roots of a slot.
roots_id: RootsId,
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/types/block/slot/index.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use derive_more::{Deref, From};
use derive_more::{Deref, Display, 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)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, From, Deref, Display, packable::Packable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SlotIndex(u64);

Expand Down

0 comments on commit 7dd3c2e

Please sign in to comment.