Derive ToSchema for more types

This commit is contained in:
dynco-nym
2024-12-09 18:00:26 +01:00
parent b60f07730b
commit 901b88f98b
12 changed files with 81 additions and 20 deletions
@@ -23,4 +23,4 @@ serde_json = { workspace = true }
vergen = { workspace = true, features = ["build", "git", "gitcl", "rustc", "cargo"] }
[features]
naive_float = []
naive_float = []
@@ -6,6 +6,7 @@ use crate::nym_node::Role;
use crate::{error::MixnetContractError, Percent};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Decimal;
use utoipa::ToSchema;
pub type Performance = Percent;
pub type WorkFactor = Decimal;
@@ -20,32 +21,37 @@ pub type WorkFactor = Decimal;
)
)]
#[cw_serde]
#[derive(Copy)]
#[derive(Copy, ToSchema)]
pub struct IntervalRewardParams {
/// Current value of the rewarding pool.
/// It is expected to be constant throughout the interval.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub reward_pool: Decimal,
/// Current value of the staking supply.
/// It is expected to be constant throughout the interval.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub staking_supply: Decimal,
/// Defines the percentage of stake needed to reach saturation for all of the nodes in the rewarded set.
/// Also known as `beta`.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub staking_supply_scale_factor: Percent,
// computed values
/// Current value of the computed reward budget per epoch, per node.
/// It is expected to be constant throughout the interval.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub epoch_reward_budget: Decimal,
/// Current value of the stake saturation point.
/// It is expected to be constant throughout the interval.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub stake_saturation_point: Decimal,
// constants(-ish)
@@ -54,6 +60,7 @@ pub struct IntervalRewardParams {
/// It is not really expected to be changing very often.
/// As a matter of fact, unless there's a very specific reason, it should remain constant.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub sybil_resistance: Percent,
// default: 10
@@ -61,6 +68,7 @@ pub struct IntervalRewardParams {
/// It is not really expected to be changing very often.
/// As a matter of fact, unless there's a very specific reason, it should remain constant.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub active_set_work_factor: Decimal,
// default: 2%
@@ -70,6 +78,7 @@ pub struct IntervalRewardParams {
/// It is not really expected to be changing very often.
/// As a matter of fact, unless there's a very specific reason, it should remain constant.
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
#[schema(value_type = String)]
pub interval_pool_emission: Percent,
}
@@ -89,7 +98,7 @@ impl IntervalRewardParams {
)
)]
#[cw_serde]
#[derive(Copy)]
#[derive(Copy, ToSchema)]
pub struct RewardingParams {
/// Parameters that should remain unchanged throughout an interval.
pub interval: IntervalRewardParams,
@@ -253,7 +262,7 @@ impl RewardingParams {
)
)]
#[cw_serde]
#[derive(Copy)]
#[derive(Copy, ToSchema)]
pub struct RewardedSetParams {
/// The expected number of nodes assigned entry gateway role (i.e. [`Role::EntryGateway`])
pub entry_gateways: u32,
@@ -727,10 +727,10 @@ mod test {
let policy = AddressPolicy::parse_from_torrc(
r#"
ExitPolicy reject 1.2.3.4/32:*
ExitPolicy reject 1.2.3.5:*
ExitPolicy reject 1.2.3.5:*
ExitPolicy reject 1.2.3.6/16:*
ExitPolicy reject 1.2.3.6/16:123-456
ExitPolicy accept *:53
ExitPolicy reject 1.2.3.6/16:123-456
ExitPolicy accept *:53
ExitPolicy accept6 *6:119
ExitPolicy accept *4:120
ExitPolicy reject6 [FC00::]/7:*
+5 -1
View File
@@ -20,10 +20,12 @@ use time::Date;
pub type DepositId = u32;
pub type DKGEpochId = u64;
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, JsonSchema)]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct IssuedTicketbook {
#[schema(value_type = u32)]
pub deposit_id: DepositId,
#[schema(value_type = u32)]
pub epoch_id: DKGEpochId,
// 96 bytes serialised 'BlindedSignature'
@@ -38,9 +40,11 @@ pub struct IssuedTicketbook {
#[schemars(with = "String")]
#[serde(with = "nym_serde_helpers::date")]
#[schema(value_type = String)]
pub expiration_date: Date,
#[schemars(with = "String")]
#[schema(value_type = String)]
pub ticketbook_type: TicketType,
}
+22 -1
View File
@@ -27,10 +27,12 @@ use utoipa::ToSchema;
pub struct VerifyEcashTicketBody {
/// The cryptographic material required for spending the underlying credential.
#[schemars(with = "PlaceholderJsonSchemaImpl")]
#[schema(value_type = String)]
pub credential: CredentialSpendingData,
/// Cosmos address of the sender of the credential
#[schemars(with = "String")]
#[schema(value_type = String)]
pub gateway_cosmos_addr: AccountId,
}
@@ -62,8 +64,16 @@ impl VerifyEcashCredentialBody {
}
}
/// Used exclusively as part of OpenAPI docs
#[derive(ToSchema)]
pub enum EcashTicketVerificationResult {
Ok(()),
EcashTicketVerificationRejection
}
#[derive(Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
pub struct EcashTicketVerificationResponse {
#[schema(value_type = EcashTicketVerificationResult)]
pub verified: Result<(), EcashTicketVerificationRejection>,
}
@@ -81,12 +91,15 @@ pub enum EcashTicketVerificationRejection {
InvalidSpentDate {
#[schemars(with = "String")]
#[serde(with = "crate::helpers::date_serde")]
#[schema(value_type = String)]
today: Date,
#[schemars(with = "String")]
#[serde(with = "crate::helpers::date_serde")]
#[schema(value_type = String)]
yesterday: Date,
#[schemars(with = "String")]
#[serde(with = "crate::helpers::date_serde")]
#[schema(value_type = String)]
received: Date,
},
@@ -120,13 +133,16 @@ pub struct BlindSignRequestBody {
pub signature: identity::Signature,
#[schemars(with = "PlaceholderJsonSchemaImpl")]
#[schema(value_type = G1ProjectiveSchema)]
pub ecash_pubkey: PublicKeyUser,
#[schemars(with = "String")]
#[serde(with = "crate::helpers::date_serde")]
#[schema(value_type = String)]
pub expiration_date: Date,
#[schemars(with = "String")]
#[schema(value_type = String)]
pub ticketbook_type: TicketType,
}
@@ -403,6 +419,7 @@ pub type DepositId = u32;
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema, ToSchema, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CommitedDeposit {
#[schema(value_type = u32)]
pub deposit_id: DepositId,
pub merkle_index: usize,
}
@@ -455,7 +472,9 @@ impl IssuedTicketbooksForResponse {
pub struct IssuedTicketbooksChallengeRequest {
#[schemars(with = "String")]
#[serde(with = "crate::helpers::date_serde")]
#[schema(value_type = String)]
pub expiration_date: Date,
#[schema(value_type = Vec<u32>)]
pub deposits: Vec<DepositId>,
}
@@ -464,8 +483,10 @@ pub struct IssuedTicketbooksChallengeRequest {
pub struct IssuedTicketbooksChallengeResponseBody {
#[schemars(with = "String")]
#[serde(with = "crate::helpers::date_serde")]
#[schema(value_type = String)]
pub expiration_date: Date,
#[schema(value_type = BTreeMap<u32, IssuedTicketbook>)]
pub partial_ticketbooks: BTreeMap<DepositId, IssuedTicketbook>,
pub merkle_proof: IssuedTicketbooksFullMerkleProof,
}
@@ -499,7 +520,7 @@ impl IssuedTicketbooksChallengeResponseBody {
}
}
#[derive(Serialize, Deserialize, JsonSchema, ToSchema, Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct IssuedTicketbooksChallengeResponse {
pub body: IssuedTicketbooksChallengeResponseBody,
+13 -5
View File
@@ -143,7 +143,7 @@ pub struct NodePerformance {
pub last_24h: Performance,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
@@ -188,7 +188,7 @@ impl From<DisplayRole> for Role {
// imo for now there's no point in exposing more than that,
// nym-api shouldn't be calculating apy or stake saturation for you.
// it should just return its own metrics (performance) and then you can do with it as you wish
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
@@ -200,13 +200,14 @@ impl From<DisplayRole> for Role {
pub struct NodeAnnotation {
#[cfg_attr(feature = "generate-ts", ts(type = "string"))]
// legacy
#[schema(value_type = String)]
pub last_24h_performance: Performance,
pub current_role: Option<DisplayRole>,
pub detailed_performance: DetailedNodePerformance,
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
@@ -242,7 +243,7 @@ impl DetailedNodePerformance {
}
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
@@ -264,7 +265,7 @@ impl RoutingScore {
}
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
#[cfg_attr(
feature = "generate-ts",
@@ -823,6 +824,7 @@ pub struct CirculatingSupplyResponse {
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct HostInformation {
#[schema(value_type = Vec<String>)]
pub ip_address: Vec<IpAddr>,
pub hostname: Option<String>,
pub keys: HostKeys,
@@ -842,15 +844,18 @@ impl From<nym_node_requests::api::v1::node::models::HostInformation> for HostInf
pub struct HostKeys {
#[serde(with = "bs58_ed25519_pubkey")]
#[schemars(with = "String")]
#[schema(value_type = String)]
pub ed25519: ed25519::PublicKey,
#[serde(with = "bs58_x25519_pubkey")]
#[schemars(with = "String")]
#[schema(value_type = String)]
pub x25519: x25519::PublicKey,
#[serde(default)]
#[serde(with = "option_bs58_x25519_pubkey")]
#[schemars(with = "Option<String>")]
#[schema(value_type = String)]
pub x25519_noise: Option<x25519::PublicKey>,
}
@@ -894,6 +899,7 @@ pub struct OffsetDateTimeJsonSchemaWrapper(
default = "unix_epoch",
with = "crate::helpers::overengineered_offset_date_time_serde"
)]
#[schema(value_type = String)]
pub OffsetDateTime,
);
@@ -1283,12 +1289,14 @@ pub struct NoiseDetails {
pub struct NodeRefreshBody {
#[serde(with = "bs58_ed25519_pubkey")]
#[schemars(with = "String")]
#[schema(value_type = String)]
pub node_identity: ed25519::PublicKey,
// a poor man's nonce
pub request_timestamp: i64,
#[schemars(with = "PlaceholderJsonSchemaImpl")]
#[schema(value_type = String)]
pub signature: ed25519::Signature,
}
+1 -1
View File
@@ -35,7 +35,7 @@ impl<T> CachedNodesResponse<T> {
}
}
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema)]
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
pub struct PaginatedCachedNodesResponse<T> {
pub refreshed_at: OffsetDateTimeJsonSchemaWrapper,
pub nodes: PaginatedResponse<T>,
+17 -2
View File
@@ -47,12 +47,20 @@ pub(crate) struct ContractVersionSchemaResponse {
pub version: String,
}
#[allow(dead_code)] // not dead, used in OpenAPI docs
#[derive(ToSchema)]
pub struct ContractInformationContractVersion {
pub(crate) address: Option<String>,
pub(crate) details: Option<ContractVersionSchemaResponse>,
}
#[utoipa::path(
tag = "network",
get,
path = "/v1/network/nym-contracts",
responses(
(status = 200, body = HashMap<String, ContractInformation<ContractVersionSchemaResponse>>)
(status = 200, body = HashMap<String, ContractInformationContractVersion>)
)
)]
async fn nym_contracts(
@@ -73,12 +81,19 @@ async fn nym_contracts(
.into()
}
#[allow(dead_code)] // not dead, used in OpenAPI docs
#[derive(ToSchema)]
pub struct ContractInformationBuildInformation {
pub(crate) address: Option<String>,
pub(crate) details: Option<ContractBuildInformation>,
}
#[utoipa::path(
tag = "network",
get,
path = "/v1/network/nym-contracts-detailed",
responses(
(status = 200, body = HashMap<String, ContractInformation<ContractBuildInformation>>)
(status = 200, body = HashMap<String, ContractInformationBuildInformation>)
)
)]
async fn nym_contracts_detailed(
+3 -2
View File
@@ -4,8 +4,9 @@
use nym_config::defaults::NymNetworkDetails;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Clone, Serialize, Deserialize, JsonSchema, utoipa::ToSchema)]
#[derive(Clone, Serialize, Deserialize, JsonSchema, ToSchema)]
pub struct NetworkDetails {
pub(crate) connected_nyxd: String,
pub(crate) network: NymNetworkDetails,
@@ -20,7 +21,7 @@ impl NetworkDetails {
}
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, utoipa::ToSchema)]
#[derive(Clone, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "snake_case")]
pub struct ContractInformation<T> {
pub(crate) address: Option<String>,
+1
View File
@@ -319,6 +319,7 @@ pub(crate) type AxumResult<T> = Result<T, AxumErrorResponse>;
#[derive(ToSchema, ToResponse)]
#[schema(title = "ErrorResponse")]
#[schema(as = ErrorResponse)]
pub(crate) struct AxumErrorResponse {
message: RequestError,
#[schema(value_type = u16)]
+2
View File
@@ -14,6 +14,8 @@ pub struct PaginationRequest {
}
#[derive(Deserialize, IntoParams, ToSchema)]
#[schema(title = "NodeId")]
#[schema(as = NodeId)]
#[into_params(parameter_in = Path)]
pub(crate) struct NodeIdParam {
#[schema(value_type = u32)]
+1 -1
View File
@@ -17,8 +17,8 @@ use utoipauto::utoipauto;
#[utoipauto(paths = "./nym-api/src,
./nym-api/nym-api-requests/src from nym-api-requests,
./common/nym_offline_compact_ecash/src from nym-compact-ecash,
./common/config/src from nym-config,
./common/cosmwasm-smart-contracts/mixnet-contract/src from nym-mixnet-contract-common,
./common/ticketbooks-merkle/src from nym-ticketbooks-merkle,
./common/nym_offline_compact_ecash/src from nym_compact_ecash")]
#[derive(OpenApi)]