Skip to content

Commit

Permalink
[NPG-3427, NPG-3367] Snapshot endpoints: "/api/v0/snapshot/voter/{tag…
Browse files Browse the repository at this point in the history
…}/{voting_key}:", " /api/v0/snapshot/delegator/{tag}/{stake_public_key}:" (#280)

* update snapshot endpoints

* update

* add new get_delegator_info endpoint

* add voting_power_saturation field to the VoterInfo

* fix, update test

* update test

* update docs

* fix

* fix clippy

* update
  • Loading branch information
Mr-Leshiy committed Sep 30, 2022
1 parent 7f68560 commit fa2faa4
Show file tree
Hide file tree
Showing 11 changed files with 489 additions and 248 deletions.
118 changes: 68 additions & 50 deletions doc/api/v0.yaml
Expand Up @@ -10,8 +10,6 @@ info:
tags:
- name: fund
description: Information on treasury fund campaigns.
- name: user
description: Information about the user
- name: challenge
description: Information on challenges, structuring proposals within a fund.
- name: proposal
Expand Down Expand Up @@ -92,32 +90,6 @@ paths:
"404":
description: The requested fund was not found

/api/v0/user/{id}:
description: Manages Cardano user information by the provided user id
get:
operationId: getUserById
tags:
- user
description: Retrieves user info
summary: get user info by id
parameters:
- name: id
in: path
required: true
deprecated: false
schema:
type: string
description: user's id
responses:
"200":
description: user's info
content:
application/json:
schema:
$ref: '#/components/schemas/UserInfo'
"404":
description: The requested user was not found

/api/v0/proposals:
post:
summary: Get proposals by chain id
Expand Down Expand Up @@ -303,13 +275,13 @@ paths:
"400":
description: Invalid combination of table/column (e.g. using funds column on challenges table)

/api/v0/snapshot/{tag}/{voting_key}:
/api/v0/snapshot/voter/{tag}/{voting_key}:
get:
operationId: getVotingPower
summary: Get voting power by voting key
operationId: getVoterInfo
summary: Get voter's info by voting key
tags: [snapshot]
description: |
Get voting power by voting key
Get voter's info by voting key
parameters:
- in: path
name: tag
Expand All @@ -327,7 +299,35 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/VotingPower"
$ref: "#/components/schemas/VotersInfo"
"400":
description: Not found

/api/v0/snapshot/delegator/{tag}/{stake_public_key}:
get:
operationId: getDelegatorInfo
summary: Get delegator's info by stake public key
tags: [snapshot]
description: |
Get delegator's info by stake public key
parameters:
- in: path
name: tag
schema:
type: string
required: true
- in: path
name: stake_public_key
schema:
type: string
required: true
responses:
"200":
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/VotersInfo"
"400":
description: Not found

Expand Down Expand Up @@ -958,38 +958,55 @@ components:
type: string
enum: [title, type, desc, author, funds]

VotingPower:
VotersInfo:
properties:
voting_info:
voter_info:
type: array
items:
$ref: "#/components/schemas/VotingInfo"
last_update:
$ref: "#/components/schemas/VoterInfo"
last_updated:
type: string
format: date-time
description: Date and time for the latest update to this snapshot information.

VotingInfo:
VoterInfo:
type: object
description: voter's info
required:
- voting_power
- voting_group
- delegations_power
- delegations_count
- voting_power_saturation
properties:
voting_power:
description: voter's voting power
type: integer
format: u64
voting_group:
description: voter's voting group on which he is assigned
type: string
delegations_power:
description: voter's delegation's power, which represents total voting power which was delegated to this voter
type: integer
format: u64
delegations_count:
description: amount of delegators, who was delegated to this voter
type: integer
format: u64
voting_power_saturation:
description: voting power's share of the total voting power corresponds to the current voting group
type: number
minimum: 0
maximum: 1
example:
[
{
"voting_power": 1000,
"voting_group": "representative",
"delegations_power": 400,
"delegations_count": 200,
},
]
"voting_power_saturation": 0.5,
}

NextFundInfo:
properties:
Expand Down Expand Up @@ -1093,17 +1110,15 @@ components:
type: string
format: date-time
description: Date and time for the latest update to this snapshot information.
UserInfo:

DelegatorInfo:
type: object
description: User info
description: delegator's info
required:
- is_voter
- delegators
- voting_groups
- last_updated
properties:
is_voter:
type: boolean
description: Is this user an active voter (representative) or delegator
dreps:
type: array
description: List of delegated representatives for the current user
Expand All @@ -1117,11 +1132,14 @@ components:
items:
description: Voting group id (the same as voting token id)
type: string
pattern: '[0-9a-f]{56}.[0-9a-f]+'
last_updated:
description: Date and time for the latest update to this snapshot information
type: string
format: date-time
example:
{
is_voter: true,
delegators: ["f5285eeead8b5885a1420800de14b0d1960db1a990a6c2f7b517125bedc000db", "7ef044ba437057d6d944ace679b7f811335639a689064cd969dffc8b55a7cc19"],
voting_groups: ["00000000000000000000000000000000000000000000000000000000.6c1e8abc"]
voting_groups: ["group1", "group2"],
last_updated: "2021-02-11T10:10:27+00:00"
}

Expand Up @@ -13,4 +13,4 @@ DROP TABLE IF EXISTS groups;
DROP TABLE IF EXISTS votes;
DROP TABLE IF EXISTS snapshots;
DROP TABLE IF EXISTS voters;
DROP TABLE IF EXISTS contributors;
DROP TABLE IF EXISTS contributions;
Expand Up @@ -159,7 +159,7 @@ create table voters (
FOREIGN KEY(snapshot_tag) REFERENCES snapshots(tag) ON DELETE CASCADE
);

create table contributors (
create table contributions (
stake_public_key TEXT NOT NULL,
reward_address TEXT NOT NULL,
value BIGINT NOT NULL,
Expand Down
93 changes: 37 additions & 56 deletions vit-servicing-station-lib/src/db/models/snapshot.rs
@@ -1,8 +1,9 @@
use crate::db::schema::{contributors, snapshots, voters};
use diesel::{ExpressionMethods, Insertable, Queryable};
use crate::db::schema::{contributions, snapshots, voters};
use diesel::{Insertable, Queryable};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Queryable)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Queryable, Insertable)]
#[diesel(table_name = "snapshots")]
#[serde(rename_all = "camelCase")]
pub struct Snapshot {
/// Tag - a unique identifier of the current snapshot
Expand All @@ -13,21 +14,22 @@ pub struct Snapshot {
pub last_updated: i64,
}

impl Insertable<snapshots::table> for Snapshot {
type Values = (
diesel::dsl::Eq<snapshots::tag, String>,
diesel::dsl::Eq<snapshots::last_updated, i64>,
);
// impl Insertable<snapshots::table> for Snapshot {
// type Values = (
// diesel::dsl::Eq<snapshots::tag, String>,
// diesel::dsl::Eq<snapshots::last_updated, i64>,
// );

fn values(self) -> Self::Values {
(
snapshots::tag.eq(self.tag),
snapshots::last_updated.eq(self.last_updated),
)
}
}
// fn values(self) -> Self::Values {
// (
// snapshots::tag.eq(self.tag),
// snapshots::last_updated.eq(self.last_updated),
// )
// }
// }

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Queryable)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Queryable, Insertable)]
#[diesel(table_name = "voters")]
#[serde(rename_all = "camelCase")]
pub struct Voter {
pub voting_key: String,
Expand All @@ -36,53 +38,32 @@ pub struct Voter {
pub snapshot_tag: String,
}

impl Insertable<voters::table> for Voter {
type Values = (
diesel::dsl::Eq<voters::voting_key, String>,
diesel::dsl::Eq<voters::voting_power, i64>,
diesel::dsl::Eq<voters::voting_group, String>,
diesel::dsl::Eq<voters::snapshot_tag, String>,
);
// impl Insertable<voters::table> for Voter {
// type Values = (
// diesel::dsl::Eq<voters::voting_key, String>,
// diesel::dsl::Eq<voters::voting_power, i64>,
// diesel::dsl::Eq<voters::voting_group, String>,
// diesel::dsl::Eq<voters::snapshot_tag, String>,
// );

fn values(self) -> Self::Values {
(
voters::voting_key.eq(self.voting_key),
voters::voting_power.eq(self.voting_power),
voters::voting_group.eq(self.voting_group),
voters::snapshot_tag.eq(self.snapshot_tag),
)
}
}
// fn values(self) -> Self::Values {
// (
// voters::voting_key.eq(self.voting_key),
// voters::voting_power.eq(self.voting_power),
// voters::voting_group.eq(self.voting_group),
// voters::snapshot_tag.eq(self.snapshot_tag),
// )
// }
// }

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Queryable)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Queryable, Insertable)]
#[diesel(table_name = "contributions")]
#[serde(rename_all = "camelCase")]
pub struct Contributor {
pub struct Contribution {
pub stake_public_key: String,
pub reward_address: String,
pub value: i64,
pub voting_key: String,
pub voting_group: String,
pub snapshot_tag: String,
}

impl Insertable<contributors::table> for Contributor {
type Values = (
diesel::dsl::Eq<contributors::stake_public_key, String>,
diesel::dsl::Eq<contributors::reward_address, String>,
diesel::dsl::Eq<contributors::value, i64>,
diesel::dsl::Eq<contributors::voting_key, String>,
diesel::dsl::Eq<contributors::voting_group, String>,
diesel::dsl::Eq<contributors::snapshot_tag, String>,
);

fn values(self) -> Self::Values {
(
contributors::stake_public_key.eq(self.stake_public_key),
contributors::reward_address.eq(self.reward_address),
contributors::value.eq(self.value),
contributors::voting_key.eq(self.voting_key),
contributors::voting_group.eq(self.voting_group),
contributors::snapshot_tag.eq(self.snapshot_tag),
)
}
}

0 comments on commit fa2faa4

Please sign in to comment.