Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grandmaster validation #43

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backend/src/client/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,24 @@ interface MasternodesInfo {
Penalty: string[];
}

export interface Candidates {
candidates: {
[key in string]: {
capacity: number;
status: 'MASTERNODE' | 'PROPOSED' | 'SLASHED';
};
};
success: boolean;
epoch: number;
}

export interface Web3WithExtension extends Web3 {
xdcSubnet: {
getV2Block: (type: 'committed') => Promise<FetchedV2BlockInfo>;
getV2BlockByNumber: (bluckNum: string) => Promise<FetchedV2BlockInfo>;
getV2BlockByHash: (blockHash: string) => Promise<FetchedV2BlockInfo>;
getMasternodesByNumber: (blockStatus: BlockStatus) => Promise<MasternodesInfo>;
getCandidates: (param: 'latest') => Promise<Candidates>;
};
}

Expand Down Expand Up @@ -62,6 +74,11 @@ export const networkExtensions = (extensionName = 'xdcSubnet') => {
params: 1,
call: 'XDPoS_getMasternodesByNumber',
},
{
name: 'getCandidates',
params: 1,
call: 'eth_getCandidates',
},
],
};
};
14 changes: 14 additions & 0 deletions backend/src/client/subnet/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Candidates } from './../extensions';
import Web3 from 'web3';
import { HttpsAgent } from 'agentkeepalive';
import { networkExtensions, Web3WithExtension } from '../extensions';
Expand All @@ -22,6 +23,19 @@ export class SubnetClient {
this.web3 = new Web3(provider).extend(networkExtensions());
}

async getCandidates() {
try {
const { candidates, success } = await this.web3.xdcSubnet.getCandidates('latest');
if (!success) {
throw new Error('Failed on getting the candidates data');
}
return candidates;
} catch (error) {
logger.error(`Fail to load candidates information from subnet nodes, ${error}`);
throw new HttpException(500, error.message ? error.message : 'Exception when getting candidates information from subnet node');
}
}

async getLastMasternodesInformation(): Promise<MasternodesInfo> {
try {
const { Number, Round, Masternodes, Penalty } = await this.web3.xdcSubnet.getMasternodesByNumber('latest');
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controllers/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class RelayerController {
public getChainDetails = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
const data = {
rpcUrl: SUBNET_URL,
grandmasterAddress: 'xxx',
grandmasterAddress: '0xaF41973D6b9EA4ADbD497365a76E6443FFB49fC5',
minimumDelegation: '10000000000000000000000000',
};
res.status(200).json(data);
Expand Down
9 changes: 9 additions & 0 deletions backend/src/controllers/masternode.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ export class MasterNodeController {
next(error);
}
};

public getCandidates = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
const data = await this.masternodeService.getLatestCandidates();
res.status(200).json(data);
} catch (error) {
next(error);
}
};
}
35 changes: 35 additions & 0 deletions backend/src/services/masternodes.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Service } from 'typedi';
import { MasternodesStorage } from './../storage/masternodes';
import { SubnetClient } from '../client/subnet';
import { Candidates } from '../client/extensions';
@Service()
export class MasternodesService {
private masternodesStorage: MasternodesStorage;
Expand All @@ -23,4 +24,38 @@ export class MasternodesService {
this.masternodesStorage.addNodesToCache(data);
return data;
}

public async getLatestCandidates(): Promise<CandidateDetails[]> {
const rawCandiates = await this.subnetClient.getCandidates();
return Object.entries(rawCandiates)
.map(entry => {
const [address, { capacity, status }] = entry;
return {
address,
delegation: weiToEther(capacity),
status,
};
})
.sort((a, b) => b.delegation - a.delegation);
}
}

export type CandidateDetailsStatus = 'MASTERNODE' | 'PROPOSED' | 'SLASHED';

export interface CandidateDetails {
address: string;
delegation: number;
status: CandidateDetailsStatus;
}

export function formatTime(unixTimestamp: number) {
const date = new Date(unixTimestamp * 1000);

const formattedDate = date.toLocaleString();

return formattedDate;
}

const weiToEther = (wei: number) => {
return Number(BigInt(wei) / BigInt(1e18));
};
28 changes: 28 additions & 0 deletions backend/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ paths:
description: 'Conflict'
'500':
description: 'Server Error'
/information/candidates:
get:
summary: Get candidates information
tags:
- management
response:
'200':
description: Candidates information/status and their delegation
content:
'application/json':
schema:
type: object
properties:
address:
type: string
delegation:
description: Delegation in wei unit
type: number
status:
type: enum
enum: ['MASTERNODE', 'PROPOSED' , 'SLASHED']
'400':
description: 'Bad Request'
'409':
description: 'Conflict'
'500':
description: 'Server Error'

/information/masternodes:
get:
summary: Get master nodes information such as committed size and their status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ServiceContext } from '@/contexts/ServiceContext';
import AddMasterNodeDialog from '@/pages/management-master-committee-page/components/add-master-node-dialog/AddMasterNodeDialog';
import PromoteDialog from '@/pages/management-master-committee-page/components/promote-dialog/PromoteDialog';
import RemoveMasterNodeDialog from '@/pages/management-master-committee-page/components/remove-master-node-dialog/RemoveMasterNodeDialog';
import { CandidateDetailsStatus } from '@/services/grandmaster-manager';
import { CandidateDetailsStatus } from '@/services/grandmaster-manager/statsServiceClient';
import { ManagerError } from '@/services/grandmaster-manager/errors';
import { TableContent } from '@/types/managementMasterCommitteePage';
import { formatHash } from '@/utils/formatter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ServiceContext } from '@/contexts/ServiceContext';
import {
setMasterNodeDialogFailResult, setMasterNodeDialogSuccessResult
} from '@/pages/management-master-committee-page/utils/helper';
import { CandidateDetails } from '@/services/grandmaster-manager';
import { CandidateDetails } from '@/services/grandmaster-manager/statsServiceClient';
import { formatHash } from '@/utils/formatter';

import type { ManagementLoaderData } from '@/types/loaderData';
Expand Down
1 change: 1 addition & 0 deletions frontend/src/services/grandmaster-manager/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum ErrorTypes {
CONFLICT_WITH_METAMASK = "CONFLICT_WITH_METAMASK",
WALLET_NOT_LOGIN = "WALLET_NOT_LOGIN",
NOT_GRANDMASTER = "NOT_GRANDMASTER",
NOT_ON_THE_RIGHT_NETWORK = "NOT_ON_THE_RIGHT_NETWORK",
// Transaction section
INVALID_TRANSACTION = "INVALID_TRANSACTION",
NOT_ENOUGH_BALANCE = "NOT_ENOUGH_BALANCE",
Expand Down
Loading