From 1b97256cf774ddf4a6e0ce7090dce0d7c483aa1c Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Tue, 12 Dec 2023 22:25:43 +0100 Subject: [PATCH] Update core API in bindings core (#1771) * Update core API in bindings core * Accept account id * Update sdk/src/client/node_api/core/routes.rs Co-authored-by: Thibault Martinez * Address review comments * GetCommitments -> GetCommitment --------- Co-authored-by: Thibault Martinez Co-authored-by: Thibault Martinez --- bindings/core/src/method/client.rs | 76 ++++++++++++++++++- bindings/core/src/method_handler/client.rs | 30 +++++++- bindings/core/src/response.rs | 39 ++++++++-- .../python/iota_sdk/client/_node_core_api.py | 5 -- sdk/src/client/node_api/core/routes.rs | 44 ++++------- sdk/src/types/api/core.rs | 61 --------------- sdk/tests/client/node_api/core.rs | 8 -- 7 files changed, 149 insertions(+), 114 deletions(-) diff --git a/bindings/core/src/method/client.rs b/bindings/core/src/method/client.rs index 7155d951f7..90c3b593c7 100644 --- a/bindings/core/src/method/client.rs +++ b/bindings/core/src/method/client.rs @@ -20,6 +20,7 @@ use iota_sdk::{ Output, OutputId, TokenScheme, }, payload::{dto::PayloadDto, signed_transaction::TransactionId}, + slot::{EpochIndex, SlotCommitmentId, SlotIndex}, BlockDto, BlockId, }, utils::serde::{option_string, string}, @@ -148,8 +149,45 @@ pub enum ClientMethod { }, /// Returns the node information together with the url of the used node GetInfo, - /// Get peers - GetPeers, + /// Check the readiness of the node to issue a new block, the reference mana cost based on the rate setter and + /// current network congestion, and the block issuance credits of the requested account. + #[serde(rename_all = "camelCase")] + GetAccountCongestion { + /// The Account ID of the account. + account_id: AccountId, + }, + /// Returns the totally available Mana rewards of an account or delegation output decayed up to endEpoch index + /// provided in the response. + #[serde(rename_all = "camelCase")] + GetRewards { + /// Output ID of an account or delegation output. + output_id: OutputId, + /// A client can specify a slot index explicitly, which should be equal to the slot it uses as the commitment + /// input for the claiming transaction. This parameter is only recommended to be provided when requesting + /// rewards for a Delegation Output in delegating state (i.e. when Delegation ID is zeroed). + slot_index: Option, + }, + /// Returns information of all registered validators and if they are active, ordered by their holding stake. + #[serde(rename_all = "camelCase")] + GetValidators { + /// The page number of validators. + page_size: Option, + /// Starts the search from the cursor (requested slot index+start index). + cursor: Option, + }, + /// Return information about a validator. + #[serde(rename_all = "camelCase")] + GetValidator { + /// The Account ID of the account. + account_id: AccountId, + }, + /// Return the information of committee members at the given epoch index. If epoch index is not provided, the + /// current committee members are returned. + #[serde(rename_all = "camelCase")] + GetCommittee { + /// The epoch index to query. + epoch_index: Option, + }, /// Get issuance GetIssuance, /// Post block (JSON) @@ -199,6 +237,12 @@ pub enum ClientMethod { /// Output ID output_id: OutputId, }, + /// Get output with its metadata + #[serde(rename_all = "camelCase")] + GetOutputWithMetadata { + /// Output ID + output_id: OutputId, + }, /// Returns the included block of the transaction. #[serde(rename_all = "camelCase")] GetIncludedBlock { @@ -211,6 +255,34 @@ pub enum ClientMethod { /// Transaction ID transaction_id: TransactionId, }, + /// Find the metadata of a transaction. + #[serde(rename_all = "camelCase")] + GetTransactionMetadata { + /// Transaction ID + transaction_id: TransactionId, + }, + /// Look up a commitment by a given commitment ID. + #[serde(rename_all = "camelCase")] + GetCommitment { + /// Commitment ID of the commitment to look up. + commitment_id: SlotCommitmentId, + }, + /// Get all UTXO changes of a given slot by Commitment ID. + #[serde(rename_all = "camelCase")] + GetUtxoChanges { + /// Commitment ID of the commitment to look up. + commitment_id: SlotCommitmentId, + }, + /// Look up a commitment by a given commitment index. + GetCommitmentByIndex { + /// Index of the commitment to look up. + index: SlotIndex, + }, + /// Get all UTXO changes of a given slot by commitment index. + GetUtxoChangesByIndex { + /// Index of the commitment to look up. + index: SlotIndex, + }, ////////////////////////////////////////////////////////////////////// // Node indexer API diff --git a/bindings/core/src/method_handler/client.rs b/bindings/core/src/method_handler/client.rs index 028bc06079..b2f6094c7d 100644 --- a/bindings/core/src/method_handler/client.rs +++ b/bindings/core/src/method_handler/client.rs @@ -173,7 +173,17 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM ClientMethod::GetHealth { url } => Response::Bool(client.get_health(&url).await?), ClientMethod::GetNodeInfo { url, auth } => Response::NodeInfo(Client::get_node_info(&url, auth).await?), ClientMethod::GetInfo => Response::Info(client.get_info().await?), - ClientMethod::GetPeers => Response::Peers(client.get_peers().await?), + ClientMethod::GetAccountCongestion { account_id } => { + Response::Congestion(client.get_account_congestion(&account_id).await?) + } + ClientMethod::GetRewards { output_id, slot_index } => { + Response::ManaRewards(client.get_output_mana_rewards(&output_id, slot_index).await?) + } + ClientMethod::GetValidators { page_size, cursor } => { + Response::Validators(client.get_validators(page_size, cursor).await?) + } + ClientMethod::GetValidator { account_id } => Response::Validator(client.get_validator(&account_id).await?), + ClientMethod::GetCommittee { epoch_index } => Response::Committee(client.get_committee(epoch_index).await?), ClientMethod::GetIssuance => Response::Issuance(client.get_issuance().await?), ClientMethod::PostBlockRaw { block_bytes } => Response::BlockId( client @@ -208,12 +218,30 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM ClientMethod::GetOutputMetadata { output_id } => { Response::OutputMetadata(client.get_output_metadata(&output_id).await?) } + ClientMethod::GetOutputWithMetadata { output_id } => { + Response::OutputWithMetadata(client.get_output_with_metadata(&output_id).await?) + } ClientMethod::GetIncludedBlock { transaction_id } => { Response::Block(BlockDto::from(&client.get_included_block(&transaction_id).await?)) } ClientMethod::GetIncludedBlockMetadata { transaction_id } => { Response::BlockMetadata(client.get_included_block_metadata(&transaction_id).await?) } + ClientMethod::GetTransactionMetadata { transaction_id } => { + Response::TransactionMetadata(client.get_transaction_metadata(&transaction_id).await?) + } + ClientMethod::GetCommitment { commitment_id } => { + Response::SlotCommitment(client.get_slot_commitment_by_id(&commitment_id).await?) + } + ClientMethod::GetUtxoChanges { commitment_id } => { + Response::UtxoChanges(client.get_utxo_changes_by_slot_commitment_id(&commitment_id).await?) + } + ClientMethod::GetCommitmentByIndex { index } => { + Response::SlotCommitment(client.get_slot_commitment_by_slot(index).await?) + } + ClientMethod::GetUtxoChangesByIndex { index } => { + Response::UtxoChanges(client.get_utxo_changes_by_slot(index).await?) + } ClientMethod::OutputIds { query_parameters } => { Response::OutputIdsResponse(client.output_ids(query_parameters).await?) } diff --git a/bindings/core/src/response.rs b/bindings/core/src/response.rs index 5fc5938060..8fe5a1761d 100644 --- a/bindings/core/src/response.rs +++ b/bindings/core/src/response.rs @@ -16,20 +16,21 @@ use iota_sdk::{ types::{ api::{ core::{ - BlockMetadataResponse, BlockWithMetadataResponse, InfoResponse as NodeInfo, - IssuanceBlockHeaderResponse, OutputWithMetadataResponse, PeerResponse, + BlockMetadataResponse, BlockWithMetadataResponse, CommitteeResponse, CongestionResponse, + InfoResponse as NodeInfo, IssuanceBlockHeaderResponse, ManaRewardsResponse, OutputWithMetadataResponse, + TransactionMetadataResponse, UtxoChangesResponse, ValidatorResponse, ValidatorsResponse, }, plugins::indexer::OutputIdsResponse, }, block::{ address::{Address, Bech32Address, Hrp}, input::UtxoInput, - output::{AccountId, FoundryId, NftId, Output, OutputId, OutputMetadata, TokenId}, + output::{AccountId, FoundryId, NftId, Output, OutputId, OutputMetadata, OutputWithMetadata, TokenId}, payload::{dto::SignedTransactionPayloadDto, signed_transaction::TransactionId}, protocol::ProtocolParameters, semantic::TransactionFailureReason, signature::Ed25519Signature, - slot::SlotCommitmentId, + slot::{SlotCommitment, SlotCommitmentId}, unlock::Unlock, BlockDto, BlockId, UnsignedBlockDto, }, @@ -101,8 +102,20 @@ pub enum Response { /// - [`GetInfo`](crate::method::ClientMethod::GetInfo) Info(NodeInfoWrapper), /// Response for: - /// - [`GetPeers`](crate::method::ClientMethod::GetPeers) - Peers(Vec), + /// - [`GetAccountCongestion`](crate::method::ClientMethod::GetAccountCongestion) + Congestion(CongestionResponse), + /// Response for: + /// - [`GetRewards`](crate::method::ClientMethod::GetRewards) + ManaRewards(ManaRewardsResponse), + /// Response for: + /// - [`GetValidators`](crate::method::ClientMethod::GetValidators) + Validators(ValidatorsResponse), + /// Response for: + /// - [`GetValidator`](crate::method::ClientMethod::GetValidator) + Validator(ValidatorResponse), + /// Response for: + /// - [`GetCommittee`](crate::method::ClientMethod::GetCommittee) + Committee(CommitteeResponse), /// Response for: /// - [`GetIssuance`](crate::method::ClientMethod::GetIssuance) Issuance(IssuanceBlockHeaderResponse), @@ -118,6 +131,17 @@ pub enum Response { /// - [`GetBlockMetadata`](crate::method::ClientMethod::GetBlockMetadata) BlockMetadata(BlockMetadataResponse), /// Response for: + /// - [`GetTransactionMetadata`](crate::method::ClientMethod::GetTransactionMetadata) + TransactionMetadata(TransactionMetadataResponse), + /// Response for: + /// - [`GetCommitment`](crate::method::ClientMethod::GetCommitment) + /// - [`GetCommitmentByIndex`](crate::method::ClientMethod::GetCommitmentByIndex) + SlotCommitment(SlotCommitment), + /// Response for: + /// - [`GetUtxoChanges`](crate::method::ClientMethod::GetUtxoChanges) + /// - [`GetUtxoChangesByIndex`](crate::method::ClientMethod::GetUtxoChangesByIndex) + UtxoChanges(UtxoChangesResponse), + /// Response for: /// - [`GetBlockWithMetadata`](crate::method::ClientMethod::GetBlockWithMetadata) BlockWithMetadata(BlockWithMetadataResponse), /// Response for: @@ -130,6 +154,9 @@ pub enum Response { /// - [`GetOutputMetadata`](crate::method::ClientMethod::GetOutputMetadata) OutputMetadata(OutputMetadata), /// Response for: + /// - [`GetOutputWithMetadata`](crate::method::ClientMethod::GetOutputWithMetadata) + OutputWithMetadata(OutputWithMetadata), + /// Response for: /// - [`GetOutputs`](crate::method::ClientMethod::GetOutputs) /// - [`GetOutputsIgnoreErrors`](crate::method::ClientMethod::GetOutputsIgnoreErrors) Outputs(Vec), diff --git a/bindings/python/iota_sdk/client/_node_core_api.py b/bindings/python/iota_sdk/client/_node_core_api.py index 23ed49bec5..dbbbfa5159 100644 --- a/bindings/python/iota_sdk/client/_node_core_api.py +++ b/bindings/python/iota_sdk/client/_node_core_api.py @@ -65,11 +65,6 @@ def get_info(self) -> NodeInfoWrapper: """ return from_dict(NodeInfoWrapper, self._call_method('getInfo')) - def get_peers(self): - """Get the peers of the node. - """ - return self._call_method('getPeers') - def get_tips(self) -> List[HexStr]: """Request tips from the node. """ diff --git a/sdk/src/client/node_api/core/routes.rs b/sdk/src/client/node_api/core/routes.rs index d0a705ba54..db4e6d591b 100644 --- a/sdk/src/client/node_api/core/routes.rs +++ b/sdk/src/client/node_api/core/routes.rs @@ -17,8 +17,8 @@ use crate::{ types::{ api::core::{ BlockMetadataResponse, BlockWithMetadataResponse, CommitteeResponse, CongestionResponse, InfoResponse, - IssuanceBlockHeaderResponse, ManaRewardsResponse, OutputResponse, PeerResponse, RoutesResponse, - SubmitBlockResponse, UtxoChangesResponse, ValidatorResponse, ValidatorsResponse, + IssuanceBlockHeaderResponse, ManaRewardsResponse, OutputResponse, RoutesResponse, SubmitBlockResponse, + TransactionMetadataResponse, UtxoChangesResponse, ValidatorResponse, ValidatorsResponse, }, block::{ address::ToBech32Ext, @@ -293,6 +293,17 @@ impl ClientInner { self.get_request(path, None, true, true).await } + /// Finds the metadata of a transaction. + /// GET /api/core/v3/transactions/{transactionId}/metadata + pub async fn get_transaction_metadata( + &self, + transaction_id: &TransactionId, + ) -> Result { + let path = &format!("api/core/v3/transactions/{transaction_id}/metadata"); + + self.get_request(path, None, true, true).await + } + // Commitments routes. /// Finds a slot commitment by its ID and returns it as object. @@ -345,35 +356,6 @@ impl ClientInner { self.get_request(path, None, false, false).await } - - // Peers routes. - - /// GET /api/core/v3/peers - pub async fn get_peers(&self) -> Result> { - const PATH: &str = "api/core/v3/peers"; - - self.get_request(PATH, None, false, false).await - } - - // // RoutePeer is the route for getting peers by their peerID. - // // GET returns the peer - // // DELETE deletes the peer. - // RoutePeer = "/peers/:" + restapipkg.ParameterPeerID - - // // RoutePeers is the route for getting all peers of the node. - // // GET returns a list of all peers. - // // POST adds a new peer. - // RoutePeers = "/peers" - - // Control routes. - - // // RouteControlDatabasePrune is the control route to manually prune the database. - // // POST prunes the database. - // RouteControlDatabasePrune = "/control/database/prune" - - // // RouteControlSnapshotsCreate is the control route to manually create a snapshot files. - // // POST creates a snapshot (full, delta or both). - // RouteControlSnapshotsCreate = "/control/snapshots/create" } impl Client { diff --git a/sdk/src/types/api/core.rs b/sdk/src/types/api/core.rs index 56548657f1..a944bc01ca 100644 --- a/sdk/src/types/api/core.rs +++ b/sdk/src/types/api/core.rs @@ -448,67 +448,6 @@ impl From for OutputWithMetadataResponse { } } -/// Describes the heartbeat of a node. -#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Heartbeat { - pub solid_milestone_index: u32, - pub pruned_milestone_index: u32, - pub latest_milestone_index: u32, - pub connected_peers: u8, - pub synced_peers: u8, -} - -/// Describes metrics of a gossip stream. -#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Metrics { - pub new_blocks: u64, - pub received_blocks: u64, - pub known_blocks: u64, - pub received_block_requests: u64, - pub received_milestone_requests: u64, - pub received_heartbeats: u64, - pub sent_blocks: u64, - pub sent_block_requests: u64, - pub sent_milestone_requests: u64, - pub sent_heartbeats: u64, - pub dropped_packets: u64, -} - -/// Returns all information about the gossip stream with the peer. -#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)] -pub struct Gossip { - pub heartbeat: Heartbeat, - pub metrics: Metrics, -} - -/// Describes the relation with the peer. -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub enum Relation { - Known, - Unknown, - Autopeered, -} - -/// Response of -/// - GET /api/core/v3/peer/{peer_id} -/// - POST /api/core/v3/peers -/// Returns information about a peer. -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PeerResponse { - pub id: String, - pub multi_addresses: Vec, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub alias: Option, - pub relation: Relation, - pub connected: bool, - #[serde(default, skip_serializing_if = "Option::is_none")] - pub gossip: Option, -} - /// Response of GET /api/routes. /// Returns the available API route groups of the node. #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] diff --git a/sdk/tests/client/node_api/core.rs b/sdk/tests/client/node_api/core.rs index 48da2a2cc3..e508c91263 100644 --- a/sdk/tests/client/node_api/core.rs +++ b/sdk/tests/client/node_api/core.rs @@ -166,14 +166,6 @@ async fn test_get_output_raw() { assert_eq!(output.output, output_raw); } -#[ignore] -#[tokio::test] -async fn test_get_peers() { - let r = setup_client_with_node_health_ignored().await.get_peers().await.unwrap(); - - println!("{r:#?}"); -} - #[ignore] #[tokio::test] async fn test_get_included_block() {