Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /api/core/v3/blocks/{blockId}/full route #1707

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ pub enum ClientMethod {
/// Block ID
block_id: BlockId,
},
/// Get a block with its metadata
#[serde(rename_all = "camelCase")]
GetBlockWithMetadata {
/// Block ID
block_id: BlockId,
},
/// Get block raw
#[serde(rename_all = "camelCase")]
GetBlockRaw {
Expand Down
3 changes: 3 additions & 0 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
ClientMethod::GetBlockMetadata { block_id } => {
Response::BlockMetadata(client.get_block_metadata(&block_id).await?)
}
ClientMethod::GetBlockWithMetadata { block_id } => {
Response::BlockWithMetadata(client.get_block_with_metadata(&block_id).await?)
}
ClientMethod::GetBlockRaw { block_id } => Response::Raw(client.get_block_raw(&block_id).await?),
ClientMethod::GetOutput { output_id } => Response::OutputWithMetadataResponse(
client
Expand Down
7 changes: 5 additions & 2 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use iota_sdk::{
types::{
api::{
core::{
BlockMetadataResponse, InfoResponse as NodeInfo, IssuanceBlockHeaderResponse,
OutputWithMetadataResponse, PeerResponse,
BlockMetadataResponse, BlockWithMetadataResponse, InfoResponse as NodeInfo,
IssuanceBlockHeaderResponse, OutputWithMetadataResponse, PeerResponse,
},
plugins::indexer::OutputIdsResponse,
},
Expand Down Expand Up @@ -118,6 +118,9 @@ pub enum Response {
/// - [`GetBlockMetadata`](crate::method::ClientMethod::GetBlockMetadata)
BlockMetadata(BlockMetadataResponse),
/// Response for:
/// - [`GetBlockWithMetadata`](crate::method::ClientMethod::GetBlockWithMetadata)
BlockWithMetadata(BlockWithMetadataResponse),
/// Response for:
/// - [`GetBlockRaw`](crate::method::ClientMethod::GetBlockRaw)
Raw(Vec<u8>),
/// Response for:
Expand Down
18 changes: 18 additions & 0 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
u64,
TransactionId,
Bech32Address,
IBlockWithMetadata,
} from '../types';
import { OutputResponse, IOutputsResponse } from '../types/models/api';

Expand Down Expand Up @@ -194,6 +195,23 @@ export class Client {
return JSON.parse(response).payload;
}

/**
* Get a block with its metadata.
*
* @param blockId The corresponding block ID of the requested block.
* @returns The requested block with its metadata.
*/
async getBlockWithMetadata(blockId: BlockId): Promise<IBlockWithMetadata> {
const response = await this.methodHandler.callMethod({
name: 'getBlockWithMetadata',
data: {
blockId,
},
});

return JSON.parse(response).payload;
}

/**
* Find inputs from addresses for a given amount (useful for offline signing).
*
Expand Down
7 changes: 7 additions & 0 deletions bindings/nodejs/lib/types/client/bridge/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export interface __GetBlockMetadataMethod__ {
};
}

export interface __GetBlockWithMetadataMethod__ {
name: 'getBlockWithMetadata';
data: {
blockId: BlockId;
};
}

export interface __FindInputsMethod__ {
name: 'findInputs';
data: {
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/client/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
__GetNetworkInfoMethod__,
__GetBlockMethod__,
__GetBlockMetadataMethod__,
__GetBlockWithMetadataMethod__,
__FindInputsMethod__,
__SignTransactionMethod__,
__BuildBasicBlockMethod__,
Expand Down Expand Up @@ -65,6 +66,7 @@ export type __ClientMethods__ =
| __GetNetworkInfoMethod__
| __GetBlockMethod__
| __GetBlockMetadataMethod__
| __GetBlockWithMetadataMethod__
| __FindInputsMethod__
| __SignTransactionMethod__
| __SignatureUnlockMethod__
Expand Down
15 changes: 15 additions & 0 deletions bindings/nodejs/lib/types/models/block-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { TransactionFailureReason } from './transaction-failure-reason';
import type { HexEncodedString } from '../utils/hex-encoding';
import { BlockState, TransactionState } from './state';
import { BlockFailureReason } from './block-failure-reason';
import { Block } from '../block';

/**
* Response from the metadata endpoint.
Expand All @@ -31,3 +32,17 @@ export interface IBlockMetadata {
*/
transactionFailureReason?: TransactionFailureReason;
}

/**
* Response from the full endpoint.
*/
export interface IBlockWithMetadata {
/**
* The block.
*/
block: Block;
/**
* The block metadata.
*/
metadata: IBlockMetadata;
}
9 changes: 8 additions & 1 deletion bindings/python/iota_sdk/client/_node_core_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dacite import from_dict

from iota_sdk.types.block.signed_block import SignedBlock
from iota_sdk.types.block.metadata import BlockMetadata
from iota_sdk.types.block.metadata import BlockMetadata, BlockWithMetadata
from iota_sdk.types.common import HexStr
from iota_sdk.types.node_info import NodeInfo, NodeInfoWrapper
from iota_sdk.types.output_metadata import OutputWithMetadata, OutputMetadata
Expand Down Expand Up @@ -102,6 +102,13 @@ def get_block_metadata(self, block_id: HexStr) -> BlockMetadata:
'blockId': block_id
}))

def get_block_with_metadata(self, block_id: HexStr) -> BlockWithMetadata:
"""Get a block with its metadata corresponding to the given block id.
"""
return BlockWithMetadata.from_dict(self._call_method('getBlockWithMetadata', {
'blockId': block_id
}))

def get_block_raw(self, block_id: HexStr) -> List[int]:
"""Get the raw bytes of the block corresponding to the given block id.
"""
Expand Down
16 changes: 16 additions & 0 deletions bindings/python/iota_sdk/types/block/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from dataclasses import dataclass
from typing import Optional
from iota_sdk.types.common import HexStr, json
# TODO rename change to Block
from iota_sdk.types.block.signed_block import SignedBlock


@json
Expand Down Expand Up @@ -182,3 +184,17 @@ def __str__(self):
26: "Destruction of nfts is not allowed in the transaction capabilities.",
255: "The semantic validation failed for a reason not covered by the previous variants."
}[self.value]


@json
@dataclass
class BlockWithMetadata:
"""Represents a block with its metadata.
Response of GET /api/core/v3/blocks/{blockId}/full.

Attributes:
block: The block.
metadata: The block metadata.
"""
block: SignedBlock
metadata: BlockMetadata
14 changes: 11 additions & 3 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use crate::{
},
types::{
api::core::{
BlockMetadataResponse, CommitteeResponse, CongestionResponse, InfoResponse, IssuanceBlockHeaderResponse,
ManaRewardsResponse, PeerResponse, RoutesResponse, SubmitBlockResponse, UtxoChangesResponse,
ValidatorResponse, ValidatorsResponse,
BlockMetadataResponse, BlockWithMetadataResponse, CommitteeResponse, CongestionResponse, InfoResponse,
IssuanceBlockHeaderResponse, ManaRewardsResponse, PeerResponse, RoutesResponse, SubmitBlockResponse,
UtxoChangesResponse, ValidatorResponse, ValidatorsResponse,
},
block::{
address::ToBech32Ext,
Expand Down Expand Up @@ -222,6 +222,14 @@ impl ClientInner {
self.get_request(path, None, true, true).await
}

/// Returns a block with its metadata.
/// GET /api/core/v3/blocks/{blockId}/full
pub async fn get_block_with_metadata(&self, block_id: &BlockId) -> Result<BlockWithMetadataResponse> {
let path = &format!("api/core/v3/blocks/{block_id}/full");

self.get_request(path, None, true, true).await
}

// UTXO routes.

/// Finds an output by its ID and returns it as object.
Expand Down
12 changes: 10 additions & 2 deletions sdk/src/types/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use crate::{
types::block::{
address::Bech32Address,
core::Parents,
output::{dto::OutputDto, AccountId, OutputId, OutputMetadata, OutputWithMetadata},
output::{dto::OutputDto, OutputId, OutputMetadata, OutputWithMetadata},
protocol::{ProtocolParameters, ProtocolParametersHash},
semantic::TransactionFailureReason,
slot::{EpochIndex, SlotCommitment, SlotCommitmentId, SlotIndex},
BlockId,
BlockDto, BlockId,
},
utils::serde::{option_string, string},
};
Expand Down Expand Up @@ -404,6 +404,14 @@ pub struct BlockMetadataResponse {
pub transaction_failure_reason: Option<TransactionFailureReason>,
}

/// Response of GET /api/core/v3/blocks/{blockId}/full.
/// Returns a block and its metadata.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct BlockWithMetadataResponse {
pub block: BlockDto,
pub metadata: BlockMetadataResponse,
}

/// Response of GET /api/core/v3/outputs/{output_id}.
/// Returns an output and its metadata.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
6 changes: 2 additions & 4 deletions sdk/src/wallet/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ use std::collections::HashMap;

use crate::{
client::secret::SecretManage,
types::{
api::core::OutputWithMetadataResponse,
block::output::{OutputId, OutputMetadata},
},
types::block::output::{OutputId, OutputMetadata},
wallet::{
types::{InclusionState, OutputData, TransactionWithMetadata},
Wallet,
},
};
#[cfg(feature = "events")]
use crate::{
types::api::core::OutputWithMetadataResponse,
types::block::payload::signed_transaction::dto::SignedTransactionPayloadDto,
wallet::{
events::types::{NewOutputEvent, SpentOutputEvent, TransactionInclusionEvent, WalletEvent},
Expand Down
Loading