Skip to content

Commit

Permalink
Update core API in bindings core (#1771)
Browse files Browse the repository at this point in the history
* Update core API in bindings core

* Accept account id

* Update sdk/src/client/node_api/core/routes.rs

Co-authored-by: Thibault Martinez <[email protected]>

* Address review comments

* GetCommitments -> GetCommitment

---------

Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
3 people authored Dec 12, 2023
1 parent d8791f5 commit 1b97256
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 114 deletions.
76 changes: 74 additions & 2 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<SlotIndex>,
},
/// 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<u32>,
/// Starts the search from the cursor (requested slot index+start index).
cursor: Option<String>,
},
/// 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<EpochIndex>,
},
/// Get issuance
GetIssuance,
/// Post block (JSON)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
30 changes: 29 additions & 1 deletion bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?)
}
Expand Down
39 changes: 33 additions & 6 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -101,8 +102,20 @@ pub enum Response {
/// - [`GetInfo`](crate::method::ClientMethod::GetInfo)
Info(NodeInfoWrapper),
/// Response for:
/// - [`GetPeers`](crate::method::ClientMethod::GetPeers)
Peers(Vec<PeerResponse>),
/// - [`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),
Expand All @@ -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:
Expand All @@ -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<OutputWithMetadataResponse>),
Expand Down
5 changes: 0 additions & 5 deletions bindings/python/iota_sdk/client/_node_core_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
44 changes: 13 additions & 31 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<TransactionMetadataResponse> {
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.
Expand Down Expand Up @@ -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<Vec<PeerResponse>> {
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 {
Expand Down
61 changes: 0 additions & 61 deletions sdk/src/types/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,67 +448,6 @@ impl From<OutputWithMetadata> 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<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,
pub relation: Relation,
pub connected: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gossip: Option<Gossip>,
}

/// Response of GET /api/routes.
/// Returns the available API route groups of the node.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
8 changes: 0 additions & 8 deletions sdk/tests/client/node_api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1b97256

Please sign in to comment.