Skip to content

Commit

Permalink
OutputMetadata 2.0 update (#655)
Browse files Browse the repository at this point in the history
* Update OutputMetadata

* Fix test

* Nit

* review comment

* Nits

* Add missing fields

* Fix test

* Update sdk/src/types/block/output/metadata.rs

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

---------

Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
thibault-martinez and Thoralf-M committed Jul 3, 2023
1 parent 8e29bbe commit 5d99c9d
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 132 deletions.
22 changes: 11 additions & 11 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl ClientInner {
Ok(response.block_id)
}

/// Finds a block by its BlockId. This method returns the given block object.
/// Finds a block by its ID and returns it as object.
/// GET /api/core/v3/blocks/{BlockId}
pub async fn get_block(&self, block_id: &BlockId) -> Result<Block> {
let path = &format!("api/core/v3/blocks/{block_id}");
Expand All @@ -229,7 +229,7 @@ impl ClientInner {
Ok(Block::try_from_dto(dto, &self.get_protocol_parameters().await?)?)
}

/// Finds a block by its BlockId. This method returns the given block raw data.
/// Finds a block by its ID and returns it as raw bytes.
/// GET /api/core/v3/blocks/{BlockId}
pub async fn get_block_raw(&self, block_id: &BlockId) -> Result<Vec<u8>> {
let path = &format!("api/core/v3/blocks/{block_id}");
Expand All @@ -255,7 +255,7 @@ impl ClientInner {

// UTXO routes.

/// Finds an output, as JSON, by its OutputId (TransactionId + output_index).
/// Finds an output by its ID and returns it as object.
/// GET /api/core/v3/outputs/{outputId}
pub async fn get_output(&self, output_id: &OutputId) -> Result<OutputWithMetadata> {
let path = &format!("api/core/v3/outputs/{output_id}");
Expand All @@ -274,7 +274,7 @@ impl ClientInner {
Ok(OutputWithMetadata::new(output, metadata))
}

/// Finds an output, as raw bytes, by its OutputId (TransactionId + output_index).
/// Finds an output by its ID and returns it as raw bytes.
/// GET /api/core/v3/outputs/{outputId}
pub async fn get_output_raw(&self, output_id: &OutputId) -> Result<Vec<u8>> {
let path = &format!("api/core/v3/outputs/{output_id}");
Expand All @@ -286,7 +286,7 @@ impl ClientInner {
.await
}

/// Get the metadata for a given `OutputId` (TransactionId + output_index).
/// Finds output metadata by output ID.
/// GET /api/core/v3/outputs/{outputId}/metadata
pub async fn get_output_metadata(&self, output_id: &OutputId) -> Result<OutputMetadata> {
let path = &format!("api/core/v3/outputs/{output_id}/metadata");
Expand All @@ -301,7 +301,7 @@ impl ClientInner {
Ok(OutputMetadata::try_from(metadata)?)
}

/// Returns the block, as object, that was included in the ledger for a given TransactionId.
/// Returns the block that was included in the ledger for a given transaction ID, as object.
/// GET /api/core/v3/transactions/{transactionId}/included-block
pub async fn get_included_block(&self, transaction_id: &TransactionId) -> Result<Block> {
let path = &format!("api/core/v3/transactions/{transaction_id}/included-block");
Expand All @@ -316,7 +316,7 @@ impl ClientInner {
Ok(Block::try_from_dto(dto, &self.get_protocol_parameters().await?)?)
}

/// Returns the block, as raw bytes, that was included in the ledger for a given TransactionId.
/// Returns the block that was included in the ledger for a given transaction ID, as object, as raw bytes.
/// GET /api/core/v3/transactions/{transactionId}/included-block
pub async fn get_included_block_raw(&self, transaction_id: &TransactionId) -> Result<Vec<u8>> {
let path = &format!("api/core/v3/transactions/{transaction_id}/included-block");
Expand All @@ -342,7 +342,7 @@ impl ClientInner {

// Commitments routes.

/// Gets the slot commitment by the given slot commitment id.
/// Finds a slot commitment by its ID and returns it as object.
/// 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}");
Expand All @@ -354,7 +354,7 @@ impl ClientInner {
.await
}

/// Gets the slot commitment, as raw bytes, by the given slot commitment id.
/// Finds a slot commitment by its ID and returns it as raw bytes.
/// 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}");
Expand All @@ -366,7 +366,7 @@ impl ClientInner {
.await
}

/// Gets the slot commitment by the given slot index.
/// Finds a slot commitment by slot index and returns it as object.
/// 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}");
Expand All @@ -378,7 +378,7 @@ impl ClientInner {
.await
}

/// Gets the slot commitment, as raw bytes, by the given slot index.
/// Finds a slot commitment by slot index and returns it as raw bytes.
/// 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}");
Expand Down
112 changes: 42 additions & 70 deletions sdk/src/types/block/output/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::types::block::{output::OutputId, payload::transaction::TransactionId, BlockId};
use crate::types::block::{output::OutputId, payload::transaction::TransactionId, slot::SlotCommitmentId, BlockId};

/// Metadata of an [`Output`](crate::types::block::output::Output).
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
Expand All @@ -11,27 +11,23 @@ use crate::types::block::{output::OutputId, payload::transaction::TransactionId,
serde(rename_all = "camelCase")
)]
pub struct OutputMetadata {
/// The identifier of the block in which the output was included.
/// The ID of the block in which the output was included.
block_id: BlockId,
/// The identifier of the output.
/// The ID of the output.
output_id: OutputId,
/// Whether the output is spent or not.
is_spent: bool,
/// If spent, the index of the milestone in which the output was spent.
// Commitment ID that includes the spent output.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
milestone_index_spent: Option<u32>,
/// If spent, the timestamp of the milestone in which the output was spent.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
milestone_timestamp_spent: Option<u32>,
/// If spent, the identifier of the transaction that spent the output.
commitment_id_spent: Option<SlotCommitmentId>,
// Transaction ID that spent the output.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
transaction_id_spent: Option<TransactionId>,
/// The index of the milestone that booked the output.
milestone_index_booked: u32,
/// The timestamp of the milestone that booked the output.
milestone_timestamp_booked: u32,
/// The index of ledger when the output was fetched.
ledger_index: u32,
/// Commitment ID that includes the output.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
included_commitment_id: Option<SlotCommitmentId>,
/// Latest commitment ID of the node.
latest_commitment_id: SlotCommitmentId,
}

impl OutputMetadata {
Expand All @@ -41,37 +37,33 @@ impl OutputMetadata {
block_id: BlockId,
output_id: OutputId,
is_spent: bool,
milestone_index_spent: Option<u32>,
milestone_timestamp_spent: Option<u32>,
commitment_id_spent: Option<SlotCommitmentId>,
transaction_id_spent: Option<TransactionId>,
milestone_index_booked: u32,
milestone_timestamp_booked: u32,
ledger_index: u32,
included_commitment_id: Option<SlotCommitmentId>,
latest_commitment_id: SlotCommitmentId,
) -> Self {
Self {
block_id,
output_id,
is_spent,
milestone_index_spent,
milestone_timestamp_spent,
commitment_id_spent,
transaction_id_spent,
milestone_index_booked,
milestone_timestamp_booked,
ledger_index,
included_commitment_id,
latest_commitment_id,
}
}

/// Returns the block id of the [`OutputMetadata`].
/// Returns the block ID of the [`OutputMetadata`].
pub fn block_id(&self) -> &BlockId {
&self.block_id
}

/// Returns the output id of the [`OutputMetadata`].
/// Returns the output ID of the [`OutputMetadata`].
pub fn output_id(&self) -> &OutputId {
&self.output_id
}

/// Returns the transaction id of the [`OutputMetadata`].
/// Returns the transaction ID of the [`OutputMetadata`].
pub fn transaction_id(&self) -> &TransactionId {
self.output_id.transaction_id()
}
Expand All @@ -91,34 +83,24 @@ impl OutputMetadata {
self.is_spent = spent;
}

/// Returns the milestone index spent of the [`OutputMetadata`].
pub fn milestone_index_spent(&self) -> Option<u32> {
self.milestone_index_spent
}

/// Returns the milestone timestamp spent of the [`OutputMetadata`].
pub fn milestone_timestamp_spent(&self) -> Option<u32> {
self.milestone_timestamp_spent
/// Returns the commitment ID spent of the [`OutputMetadata`].
pub fn commitment_id_spent(&self) -> Option<&SlotCommitmentId> {
self.commitment_id_spent.as_ref()
}

/// Returns the transaction id spent of the [`OutputMetadata`].
/// Returns the transaction ID spent of the [`OutputMetadata`].
pub fn transaction_id_spent(&self) -> Option<&TransactionId> {
self.transaction_id_spent.as_ref()
}

/// Returns the milestone index booked of the [`OutputMetadata`].
pub fn milestone_index_booked(&self) -> u32 {
self.milestone_index_booked
}

/// Returns the milestone timestamp booked of the [`OutputMetadata`].
pub fn milestone_timestamp_booked(&self) -> u32 {
self.milestone_timestamp_booked
/// Returns the included commitment ID of the [`OutputMetadata`].
pub fn included_commitment_id(&self) -> Option<&SlotCommitmentId> {
self.included_commitment_id.as_ref()
}

/// Returns the ledger index of the [`OutputMetadata`].
pub fn ledger_index(&self) -> u32 {
self.ledger_index
/// Returns the latest commitment ID of the [`OutputMetadata`].
pub fn latest_commitment_id(&self) -> &SlotCommitmentId {
&self.latest_commitment_id
}
}

Expand All @@ -141,14 +123,12 @@ pub mod dto {
pub output_index: u16,
pub is_spent: bool,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub milestone_index_spent: Option<u32>,
pub commitment_id_spent: Option<SlotCommitmentId>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub milestone_timestamp_spent: Option<u32>,
pub transaction_id_spent: Option<TransactionId>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub transaction_id_spent: Option<String>,
pub milestone_index_booked: u32,
pub milestone_timestamp_booked: u32,
pub ledger_index: u32,
pub included_commitment_id: Option<SlotCommitmentId>,
pub latest_commitment_id: SlotCommitmentId,
}

impl OutputMetadataDto {
Expand All @@ -169,16 +149,10 @@ pub mod dto {
response.output_index,
)?,
is_spent: response.is_spent,
milestone_index_spent: response.milestone_index_spent,
milestone_timestamp_spent: response.milestone_timestamp_spent,
transaction_id_spent: response
.transaction_id_spent
.as_ref()
.map(|s| TransactionId::from_str(s))
.transpose()?,
milestone_index_booked: response.milestone_index_booked,
milestone_timestamp_booked: response.milestone_timestamp_booked,
ledger_index: response.ledger_index,
commitment_id_spent: response.commitment_id_spent,
transaction_id_spent: response.transaction_id_spent,
included_commitment_id: response.included_commitment_id,
latest_commitment_id: response.latest_commitment_id,
})
}
}
Expand All @@ -190,12 +164,10 @@ pub mod dto {
transaction_id: output_metadata.transaction_id().to_string(),
output_index: output_metadata.output_index(),
is_spent: output_metadata.is_spent(),
milestone_index_spent: output_metadata.milestone_index_spent(),
milestone_timestamp_spent: output_metadata.milestone_timestamp_spent(),
transaction_id_spent: output_metadata.transaction_id_spent().map(|t| t.to_string()),
milestone_index_booked: output_metadata.milestone_index_booked(),
milestone_timestamp_booked: output_metadata.milestone_timestamp_booked(),
ledger_index: output_metadata.ledger_index(),
commitment_id_spent: output_metadata.commitment_id_spent().cloned(),
transaction_id_spent: output_metadata.transaction_id_spent().cloned(),
included_commitment_id: output_metadata.included_commitment_id().cloned(),
latest_commitment_id: *output_metadata.latest_commitment_id(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/types/block/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub mod output;
pub mod parents;
/// Module providing random payload generation utilities.
pub mod payload;
/// Module providing random slot generation utilities.
pub mod slot;
/// Module providing random string generation utilities.
pub mod string;
/// Module providing random transaction generation utilities.
Expand Down
12 changes: 5 additions & 7 deletions sdk/src/types/block/rand/output/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::types::block::{
output::OutputMetadata,
rand::{
block::rand_block_id, bool::rand_bool, number::rand_number, option::rand_option, output::rand_output_id,
transaction::rand_transaction_id,
block::rand_block_id, bool::rand_bool, option::rand_option, output::rand_output_id,
slot::rand_slot_commitment_id, transaction::rand_transaction_id,
},
};

Expand All @@ -15,11 +15,9 @@ pub fn rand_output_metadata() -> OutputMetadata {
rand_block_id(),
rand_output_id(),
rand_bool(),
rand_option(rand_number()),
rand_option(rand_number()),
rand_option(rand_slot_commitment_id()),
rand_option(rand_transaction_id()),
rand_number(),
rand_number(),
rand_number(),
rand_option(rand_slot_commitment_id()),
rand_slot_commitment_id(),
)
}
9 changes: 9 additions & 0 deletions sdk/src/types/block/rand/slot.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

use crate::types::block::{rand::bytes::rand_bytes_array, slot::SlotCommitmentId};

/// Generates a random slot commitment id.
pub fn rand_slot_commitment_id() -> SlotCommitmentId {
SlotCommitmentId::new(rand_bytes_array())
}
31 changes: 17 additions & 14 deletions sdk/src/wallet/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,17 @@ impl AccountInner {
_ => {}
}

if let Some(lower_bound_booked_timestamp) = filter.lower_bound_booked_timestamp {
if output.metadata.milestone_timestamp_booked() < lower_bound_booked_timestamp {
continue;
}
}
if let Some(upper_bound_booked_timestamp) = filter.upper_bound_booked_timestamp {
if output.metadata.milestone_timestamp_booked() > upper_bound_booked_timestamp {
continue;
}
}
// TODO check if we can still filter since milestone_timestamp_booked is gone
// if let Some(lower_bound_booked_timestamp) = filter.lower_bound_booked_timestamp {
// if output.metadata.milestone_timestamp_booked() < lower_bound_booked_timestamp {
// continue;
// }
// }
// if let Some(upper_bound_booked_timestamp) = filter.upper_bound_booked_timestamp {
// if output.metadata.milestone_timestamp_booked() > upper_bound_booked_timestamp {
// continue;
// }
// }

if let Some(output_types) = &filter.output_types {
if !output_types.contains(&output.output.kind()) {
Expand Down Expand Up @@ -451,10 +452,12 @@ pub(crate) fn build_transaction_from_payload_and_inputs(
.first()
.and_then(|i| BlockId::from_str(&i.metadata.block_id).ok()),
inclusion_state: InclusionState::Confirmed,
timestamp: inputs
.first()
.and_then(|i| i.metadata.milestone_timestamp_spent.map(|t| t as u128 * 1000))
.unwrap_or_else(|| crate::utils::unix_timestamp_now().as_millis()),
timestamp: 0,
// TODO check if we keep a timestamp in Transaction since milestone_timestamp_spent is gone
// inputs
// .first()
// .and_then(|i| i.metadata.milestone_timestamp_spent.map(|t| t as u128 * 1000))
// .unwrap_or_else(|| crate::utils::unix_timestamp_now().as_millis()),
transaction_id: tx_id,
network_id: tx_essence.network_id(),
incoming: true,
Expand Down
Loading

0 comments on commit 5d99c9d

Please sign in to comment.