Skip to content

Commit

Permalink
feat: add delegate.voters attribute (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiodf committed Apr 25, 2022
1 parent 0c03aaf commit fc745fc
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 33 deletions.
2 changes: 2 additions & 0 deletions packages/core-api/src/resources-new/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type DelegateResource = {
address: string;
publicKey: string;
votes: Utils.BigNumber;
voters: number;
rank: number;
isResigned: boolean;
blocks: {
Expand Down Expand Up @@ -47,6 +48,7 @@ export const delegateCriteriaSchemaObject = {
address: walletCriteriaSchemaObject.address,
publicKey: walletCriteriaSchemaObject.publicKey,
votes: Schemas.createRangeCriteriaSchema(Schemas.nonNegativeBigNumber),
voters: Schemas.createRangeCriteriaSchema(Joi.number().integer().min(0)),
rank: Schemas.createRangeCriteriaSchema(Joi.number().integer().min(1)),
isResigned: Joi.boolean(),
blocks: {
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/src/services/delegate-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class DelegateSearchService {
address: wallet.getAddress(),
publicKey: publicKey!,
votes: delegateAttribute.voteBalance,
voters: delegateAttribute.voters,
rank: delegateAttribute.rank,
isResigned: !!delegateAttribute.resigned,
blocks: {
Expand Down
72 changes: 49 additions & 23 deletions packages/core-api/src/www/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -676,143 +676,167 @@
{
"name": "forged.fees",
"in": "query",
"description": "Exact amount, in satoshis, of all fees forged by the delegates(s) to be returned",
"description": "Exact amount, in satoshis, of all fees forged by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.fees.from",
"in": "query",
"description": "Minimum amount, in satoshis, of all fees forged by the delegates(s) to be returned",
"description": "Minimum amount, in satoshis, of all fees forged by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.fees.to",
"in": "query",
"description": "Maximum amount, in satoshis, of all fees forged by the delegates(s) to be returned",
"description": "Maximum amount, in satoshis, of all fees forged by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.burnedFees",
"in": "query",
"description": "Exact amount, in satoshis, of all fees burned by the delegates(s) to be returned",
"description": "Exact amount, in satoshis, of all fees burned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.burnedFees.from",
"in": "query",
"description": "Minimum amount, in satoshis, of all fees burned by the delegates(s) to be returned",
"description": "Minimum amount, in satoshis, of all fees burned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.burnedFees.to",
"in": "query",
"description": "Maximum amount, in satoshis, of all fees burned by the delegates(s) to be returned",
"description": "Maximum amount, in satoshis, of all fees burned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.rewards",
"in": "query",
"description": "Exact amount, in satoshis, of all block rewards earned by the delegates(s) to be returned",
"description": "Exact amount, in satoshis, of all block rewards earned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.rewards.from",
"in": "query",
"description": "Minimum amount, in satoshis, of all block rewards earned by the delegates(s) to be returned",
"description": "Minimum amount, in satoshis, of all block rewards earned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.rewards.to",
"in": "query",
"description": "Maximum amount, in satoshis, of all block rewards earned by the delegates(s) to be returned",
"description": "Maximum amount, in satoshis, of all block rewards earned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.total",
"in": "query",
"description": "Exact amount, in satoshis, of all unburned fees plus block rewards earned by the delegates(s) to be returned",
"description": "Exact amount, in satoshis, of all unburned fees plus block rewards earned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.total.from",
"in": "query",
"description": "Minimum amount, in satoshis, of all unburned fees plus block rewards earned by the delegates(s) to be returned",
"description": "Minimum amount, in satoshis, of all unburned fees plus block rewards earned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "forged.total.to",
"in": "query",
"description": "Maximum amount, in satoshis, of all unburned fees plus block rewards earned by the delegates(s) to be returned",
"description": "Maximum amount, in satoshis, of all unburned fees plus block rewards earned by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "votes",
"in": "query",
"description": "Exact amount of vote weight for the delegates(s) to be returned",
"description": "Exact amount of vote weight for the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "votes.from",
"in": "query",
"description": "Minimum amount of vote weight for the delegates(s) to be returned",
"description": "Minimum amount of vote weight for the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "votes.to",
"in": "query",
"description": "Maximum amount of vote weight for the delegates(s) to be returned",
"description": "Maximum amount of vote weight for the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "voters",
"in": "query",
"description": "Exact number of voters for the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "voters.from",
"in": "query",
"description": "Minimum number of voters for the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "voters.to",
"in": "query",
"description": "Maximum number of voters for the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "blocks.produced",
"in": "query",
"description": "Exact number of blocks produced by the delegates(s) to be returned",
"description": "Exact number of blocks produced by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "blocks.produced.from",
"in": "query",
"description": "Minimum number of blocks produced by the delegates(s) to be returned",
"description": "Minimum number of blocks produced by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
},
{
"name": "blocks.produced.to",
"in": "query",
"description": "Maximum number of blocks produced by the delegates(s) to be returned",
"description": "Maximum number of blocks produced by the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/zeroOrMore"
}
Expand All @@ -828,15 +852,15 @@
{
"name": "rank.from",
"in": "query",
"description": "Lowest rank of the delegates(s) to be returned",
"description": "Lowest rank of the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/oneOrMore"
}
},
{
"name": "rank.to",
"in": "query",
"description": "Highest rank of the delegates(s) to be returned",
"description": "Highest rank of the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/oneOrMore"
}
Expand All @@ -852,15 +876,15 @@
{
"name": "version.from",
"in": "query",
"description": "Earliest node version running on the delegates(s) to be returned",
"description": "Earliest node version running on the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/semver"
}
},
{
"name": "version.to",
"in": "query",
"description": "Latest node version running on the delegates(s) to be returned",
"description": "Latest node version running on the delegate(s) to be returned",
"schema": {
"$ref": "#/components/schemas/semver"
}
Expand Down Expand Up @@ -895,7 +919,9 @@
"version:asc",
"version:desc",
"votes:asc",
"votes:desc"
"votes:desc",
"voters:asc",
"voters:desc"
]
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/core-kernel/src/contracts/state/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export type WalletFactory = (address: string) => Wallet;
export interface WalletDelegateAttributes {
username: string;
voteBalance: Utils.BigNumber;
voters: number;
forgedFees: Utils.BigNumber;
burnedFees: Utils.BigNumber;
forgedRewards: Utils.BigNumber;
Expand Down
1 change: 1 addition & 0 deletions packages/core-state/src/dpos/dpos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class DposState implements Contracts.State.DposState {
voter.getAttribute("vote"),
);

delegate.setAttribute("delegate.voters", delegate.getAttribute("delegate.voters") + 1);
const voteBalance: Utils.BigNumber = delegate.getAttribute("delegate.voteBalance");

const lockedBalance = voter.getAttribute("htlc.lockedBalance", Utils.BigNumber.ZERO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class DelegateRegistrationTransactionHandler extends TransactionHandler {
"delegate.username",
"delegate.version", // Used by the API
"delegate.voteBalance",
"delegate.voters", // Used by the API
"delegate",
];
}
Expand Down Expand Up @@ -128,6 +129,7 @@ export class DelegateRegistrationTransactionHandler extends TransactionHandler {
forgedRewards: Utils.BigNumber.ZERO,
producedBlocks: 0,
round: 0,
voters: 0,
});

this.walletRepository.index(sender);
Expand Down
40 changes: 30 additions & 10 deletions packages/core-transactions/src/handlers/one/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,26 @@ export class VoteTransactionHandler extends TransactionHandler {
Utils.assert.defined<string[]>(transaction.data.asset?.votes);

for (const vote of transaction.data.asset.votes) {
let delegateWallet: Contracts.State.Wallet;
let delegateVote: string = vote.slice(1);
if (delegateVote.length !== 66) {
delegateWallet = this.walletRepository.findByUsername(delegateVote);
delegateVote = delegateWallet.getPublicKey()!;
} else {
delegateWallet = this.walletRepository.findByPublicKey(delegateVote);
}

let voters: number = delegateWallet.getAttribute("delegate.voters");

if (vote.startsWith("+")) {
let delegateVote: string = vote.slice(1);
if (delegateVote.length !== 66) {
const delegateWallet: Contracts.State.Wallet = this.walletRepository.findByUsername(delegateVote);
delegateVote = delegateWallet.getPublicKey()!;
}
voters++;
sender.setAttribute("vote", delegateVote);
} else {
voters--;
sender.forgetAttribute("vote");
}

delegateWallet.setAttribute("delegate.voters", voters);
}
}

Expand All @@ -157,16 +167,26 @@ export class VoteTransactionHandler extends TransactionHandler {
Utils.assert.defined<Interfaces.ITransactionAsset>(transaction.data.asset?.votes);

for (const vote of transaction.data.asset.votes.slice().reverse()) {
let delegateWallet: Contracts.State.Wallet;
let delegateVote: string = vote.slice(1);
if (delegateVote.length !== 66) {
delegateWallet = this.walletRepository.findByUsername(delegateVote);
delegateVote = delegateWallet.getPublicKey()!;
} else {
delegateWallet = this.walletRepository.findByPublicKey(delegateVote);
}

let voters: number = delegateWallet.getAttribute("delegate.voters");

if (vote.startsWith("+")) {
voters--;
sender.forgetAttribute("vote");
} else {
let delegateVote: string = vote.slice(1);
if (delegateVote.length !== 66) {
const delegateWallet: Contracts.State.Wallet = this.walletRepository.findByUsername(delegateVote);
delegateVote = delegateWallet.getPublicKey()!;
}
voters++;
sender.setAttribute("vote", delegateVote);
}

delegateWallet.setAttribute("delegate.voters", voters);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class DelegateRegistrationTransactionHandler extends One.DelegateRegistra
forgedRewards: Utils.BigNumber.ZERO,
producedBlocks: 0,
rank: undefined,
voters: 0,
});

this.walletRepository.index(wallet);
Expand Down

0 comments on commit fc745fc

Please sign in to comment.