From 8e29bbed100fd21666e0cdb145d81bd4a671bf65 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 21 Jun 2023 19:27:32 +0200 Subject: [PATCH] Add commitment routes (#651) * Add Commitments routes * Fix no_std * Fix get_slot_commitment_by_index_raw name * Add GET * derive_more Display --- sdk/Cargo.toml | 2 +- sdk/src/client/node_api/core/routes.rs | 55 ++++++++++++++++++++++++-- sdk/src/types/block/slot/commitment.rs | 7 +++- sdk/src/types/block/slot/index.rs | 4 +- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 57062d128f..165f04693b 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -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 } diff --git a/sdk/src/client/node_api/core/routes.rs b/sdk/src/client/node_api/core/routes.rs index 20f0502b8f..ebc6b2e96a 100644 --- a/sdk/src/client/node_api/core/routes.rs +++ b/sdk/src/client/node_api/core/routes.rs @@ -21,6 +21,7 @@ use crate::{ block::{ output::{dto::OutputMetadataDto, Output, OutputId, OutputMetadata, OutputWithMetadata}, payload::transaction::TransactionId, + slot::{SlotCommitment, SlotCommitmentId, SlotIndex}, Block, BlockDto, BlockId, }, }, @@ -89,7 +90,7 @@ impl ClientInner { .await } - // Tangle routes. + // Blocks routes. /// Returns tips that are ideal for attaching a block. /// GET /api/core/v3/tips @@ -106,8 +107,6 @@ impl ClientInner { Ok(response.tips) } - // 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 { @@ -341,6 +340,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 { + let path = &format!("api/core/v3/commitments/{slot_commitment_id}"); + + self.node_manager + .read() + .await + .get_request::(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> { + 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 { + let path = &format!("api/core/v3/commitments/by-index/{slot_index}"); + + self.node_manager + .read() + .await + .get_request::(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> { + 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 diff --git a/sdk/src/types/block/slot/commitment.rs b/sdk/src/types/block/slot/commitment.rs index d4d9b65f98..5d10e56f5e 100644 --- a/sdk/src/types/block/slot/commitment.rs +++ b/sdk/src/types/block/slot/commitment.rs @@ -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, diff --git a/sdk/src/types/block/slot/index.rs b/sdk/src/types/block/slot/index.rs index 6a4b902c22..b78ed69ad1 100644 --- a/sdk/src/types/block/slot/index.rs +++ b/sdk/src/types/block/slot/index.rs @@ -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);