From baeb885e45034f32fe83cdda4c43074de027a4ce Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Thu, 4 Jan 2024 19:18:08 +0400 Subject: [PATCH 1/3] change xdc to 0x --- frontend/src/utils/formatter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/utils/formatter.ts b/frontend/src/utils/formatter.ts index d7b562c..a6064b4 100644 --- a/frontend/src/utils/formatter.ts +++ b/frontend/src/utils/formatter.ts @@ -1,7 +1,7 @@ import BigNumber from 'bignumber.js'; export function formatHashFromNonZero(hash: string): string { - return formatHash(hash.replace(/0x[0]*/, 'xdc')); + return formatHash(hash.replace(/0x[0]*/, '0x')); } export function formatHash(hash: string): string { From 469aa7fbc6ac98483e66ac0bcdae4bbe4f6ddb36 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Fri, 12 Jan 2024 14:43:01 +0400 Subject: [PATCH 2/3] update for confirmation status and minor refactor --- backend/Dockerfile.mac | 20 +++++++++++ backend/src/client/extensions.ts | 38 ++++++++++++++++++-- backend/src/client/parentchain/index.ts | 15 ++++++-- backend/src/client/subnet/index.ts | 4 +-- backend/src/controllers/blocks.controller.ts | 4 +-- 5 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 backend/Dockerfile.mac diff --git a/backend/Dockerfile.mac b/backend/Dockerfile.mac new file mode 100644 index 0000000..97c1225 --- /dev/null +++ b/backend/Dockerfile.mac @@ -0,0 +1,20 @@ +# NodeJS Version 16 +FROM node:16.18-alpine + +# Copy Dir +COPY . ./app + +# Work to Dir +WORKDIR /app + +# Install Node Package +RUN apk add g++ make python3 +RUN npm install --legacy-peer-deps + +# Set Env +ENV NODE_ENV production +EXPOSE 3000 +EXPOSE 443 + +# Cmd script +CMD ["npm", "run", "start"] diff --git a/backend/src/client/extensions.ts b/backend/src/client/extensions.ts index a519988..d5bb31e 100644 --- a/backend/src/client/extensions.ts +++ b/backend/src/client/extensions.ts @@ -50,15 +50,21 @@ export interface NetworkInfo { export interface Web3WithExtension extends Web3 { xdcSubnet: { getV2Block: (type: 'committed') => Promise; - getV2BlockByNumber: (bluckNum: string) => Promise; + getV2BlockByNumber: (blockNum: string) => Promise; getV2BlockByHash: (blockHash: string) => Promise; getMasternodesByNumber: (blockStatus: BlockStatus) => Promise; getCandidates: (param: 'latest') => Promise; getNetworkInfo: () => Promise; }; + xdcParentnet: { + getV2Block: (type: 'committed') => Promise; + getV2BlockByNumber: (blockNum: string) => Promise; + getV2BlockByHash: (blockHash: string) => Promise; + getNetworkInfo: () => Promise; + } } -export const networkExtensions = (extensionName = 'xdcSubnet') => { +export const subnetExtensions = (extensionName = 'xdcSubnet') => { return { property: extensionName, methods: [ @@ -95,3 +101,31 @@ export const networkExtensions = (extensionName = 'xdcSubnet') => { ], }; }; + +export const parentnetExtensions = (extensionName = 'xdcParentnet') => { + return { + property: extensionName, + methods: [ + { + name: 'getV2Block', + params: 1, + call: 'XDPoS_getV2BlockByNumber', + }, + { + name: 'getV2BlockByNumber', + params: 1, + call: 'XDPoS_getV2BlockByNumber', + }, + { + name: 'getV2BlockByHash', + params: 1, + call: 'XDPoS_getV2BlockByHash', + }, + { + name: 'getNetworkInfo', + params: 0, + call: 'XDPoS_networkInformation', + }, + ], + }; +}; \ No newline at end of file diff --git a/backend/src/client/parentchain/index.ts b/backend/src/client/parentchain/index.ts index 3647aaa..c99dd62 100644 --- a/backend/src/client/parentchain/index.ts +++ b/backend/src/client/parentchain/index.ts @@ -5,7 +5,7 @@ import { abi } from './contract'; import { logger } from '../../utils/logger'; import { CHECKPOINT_CONTRACT, PARENTNET_URL } from '../../config'; import { HttpException } from '../../exceptions/httpException'; -import { networkExtensions, Web3WithExtension } from '../extensions'; +import { parentnetExtensions, Web3WithExtension } from '../extensions'; export interface SmartContractAuditedBlockInfo { smartContractHash: string; @@ -21,7 +21,7 @@ export class ParentChainClient { constructor() { const keepaliveAgent = new HttpsAgent(); const provider = new Web3.providers.HttpProvider(PARENTNET_URL, { keepAlive: true, agent: { https: keepaliveAgent } }); - this.web3 = new Web3(provider).extend(networkExtensions()); + this.web3 = new Web3(provider).extend(parentnetExtensions()); this.smartContractInstance = new this.web3.eth.Contract(abi as any[], CHECKPOINT_CONTRACT); } @@ -94,7 +94,16 @@ export class ParentChainClient { */ async confirmBlock(subnetHash: string) { const { finalized, mainnet_num } = await this.smartContractInstance.methods.getHeader(subnetHash).call(); - const { Committed, Hash, Miner, Timestamp } = await this.web3.xdcSubnet.getV2BlockByNumber(Web3.utils.numberToHex(mainnet_num)); + if (!finalized){ + return{ + isCommitted: false, + parentchainHash: "0x0000000000000000000000000000000000000000000000000000000000000000", + parentChainNum: mainnet_num, + proposer: "0x0000000000000000000000000000000000000000000000000000000000000000", + timestamp: 0, + } + } + const { Committed, Hash, Miner, Timestamp } = await this.web3.xdcParentnet.getV2BlockByNumber(Web3.utils.numberToHex(mainnet_num)); return { isCommitted: Committed && finalized, parentchainHash: Hash, diff --git a/backend/src/client/subnet/index.ts b/backend/src/client/subnet/index.ts index 2851de2..dac38d5 100644 --- a/backend/src/client/subnet/index.ts +++ b/backend/src/client/subnet/index.ts @@ -1,6 +1,6 @@ import Web3 from 'web3'; import { HttpsAgent } from 'agentkeepalive'; -import { networkExtensions, Web3WithExtension } from '../extensions'; +import { subnetExtensions, Web3WithExtension } from '../extensions'; import { logger } from '../../utils/logger'; import { HttpException } from '../../exceptions/httpException'; import { SUBNET_URL } from '../../config'; @@ -19,7 +19,7 @@ export class SubnetClient { const keepaliveAgent = new HttpsAgent(); const provider = new Web3.providers.HttpProvider(SUBNET_URL, { keepAlive: true, agent: { https: keepaliveAgent } }); - this.web3 = new Web3(provider).extend(networkExtensions()); + this.web3 = new Web3(provider).extend(subnetExtensions()); } async getCandidates() { diff --git a/backend/src/controllers/blocks.controller.ts b/backend/src/controllers/blocks.controller.ts index cc9b6fc..ffe1f24 100644 --- a/backend/src/controllers/blocks.controller.ts +++ b/backend/src/controllers/blocks.controller.ts @@ -125,7 +125,7 @@ export class BlocksController { blockHash: parentChain.parentchainBlockHash, blockHeight: parentChain.parentchainBlockHeight, proposer: parentChain.proposer, - timestamp: subnet.timestamp.toString(), + timestamp: parentChain.timestamp.toString(), }, }; } else if (parseInt(input)) { @@ -145,7 +145,7 @@ export class BlocksController { blockHash: parentChain.parentchainBlockHash, blockHeight: parentChain.parentchainBlockHeight, proposer: parentChain.proposer, - timestamp: subnet.timestamp.toString(), + timestamp: parentChain.timestamp.toString(), }, }; } else { From 7628424720b13b0bb5aefc944ee0aaf75f734301 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 15 Jan 2024 14:18:41 +0400 Subject: [PATCH 3/3] simplify subnet_api and parentchain_api to xdc_api --- backend/src/client/extensions.ts | 38 ++----------------------- backend/src/client/parentchain/index.ts | 6 ++-- backend/src/client/subnet/index.ts | 14 ++++----- 3 files changed, 12 insertions(+), 46 deletions(-) diff --git a/backend/src/client/extensions.ts b/backend/src/client/extensions.ts index d5bb31e..8d16caa 100644 --- a/backend/src/client/extensions.ts +++ b/backend/src/client/extensions.ts @@ -48,7 +48,7 @@ export interface NetworkInfo { } export interface Web3WithExtension extends Web3 { - xdcSubnet: { + xdcApi: { getV2Block: (type: 'committed') => Promise; getV2BlockByNumber: (blockNum: string) => Promise; getV2BlockByHash: (blockHash: string) => Promise; @@ -56,15 +56,9 @@ export interface Web3WithExtension extends Web3 { getCandidates: (param: 'latest') => Promise; getNetworkInfo: () => Promise; }; - xdcParentnet: { - getV2Block: (type: 'committed') => Promise; - getV2BlockByNumber: (blockNum: string) => Promise; - getV2BlockByHash: (blockHash: string) => Promise; - getNetworkInfo: () => Promise; - } } -export const subnetExtensions = (extensionName = 'xdcSubnet') => { +export const xdcExtensions = (extensionName = 'xdcApi') => { return { property: extensionName, methods: [ @@ -101,31 +95,3 @@ export const subnetExtensions = (extensionName = 'xdcSubnet') => { ], }; }; - -export const parentnetExtensions = (extensionName = 'xdcParentnet') => { - return { - property: extensionName, - methods: [ - { - name: 'getV2Block', - params: 1, - call: 'XDPoS_getV2BlockByNumber', - }, - { - name: 'getV2BlockByNumber', - params: 1, - call: 'XDPoS_getV2BlockByNumber', - }, - { - name: 'getV2BlockByHash', - params: 1, - call: 'XDPoS_getV2BlockByHash', - }, - { - name: 'getNetworkInfo', - params: 0, - call: 'XDPoS_networkInformation', - }, - ], - }; -}; \ No newline at end of file diff --git a/backend/src/client/parentchain/index.ts b/backend/src/client/parentchain/index.ts index c99dd62..5b48768 100644 --- a/backend/src/client/parentchain/index.ts +++ b/backend/src/client/parentchain/index.ts @@ -5,7 +5,7 @@ import { abi } from './contract'; import { logger } from '../../utils/logger'; import { CHECKPOINT_CONTRACT, PARENTNET_URL } from '../../config'; import { HttpException } from '../../exceptions/httpException'; -import { parentnetExtensions, Web3WithExtension } from '../extensions'; +import { xdcExtensions, Web3WithExtension } from '../extensions'; export interface SmartContractAuditedBlockInfo { smartContractHash: string; @@ -21,7 +21,7 @@ export class ParentChainClient { constructor() { const keepaliveAgent = new HttpsAgent(); const provider = new Web3.providers.HttpProvider(PARENTNET_URL, { keepAlive: true, agent: { https: keepaliveAgent } }); - this.web3 = new Web3(provider).extend(parentnetExtensions()); + this.web3 = new Web3(provider).extend(xdcExtensions()); this.smartContractInstance = new this.web3.eth.Contract(abi as any[], CHECKPOINT_CONTRACT); } @@ -103,7 +103,7 @@ export class ParentChainClient { timestamp: 0, } } - const { Committed, Hash, Miner, Timestamp } = await this.web3.xdcParentnet.getV2BlockByNumber(Web3.utils.numberToHex(mainnet_num)); + const { Committed, Hash, Miner, Timestamp } = await this.web3.xdcApi.getV2BlockByNumber(Web3.utils.numberToHex(mainnet_num)); return { isCommitted: Committed && finalized, parentchainHash: Hash, diff --git a/backend/src/client/subnet/index.ts b/backend/src/client/subnet/index.ts index dac38d5..780d1a2 100644 --- a/backend/src/client/subnet/index.ts +++ b/backend/src/client/subnet/index.ts @@ -1,6 +1,6 @@ import Web3 from 'web3'; import { HttpsAgent } from 'agentkeepalive'; -import { subnetExtensions, Web3WithExtension } from '../extensions'; +import { xdcExtensions, Web3WithExtension } from '../extensions'; import { logger } from '../../utils/logger'; import { HttpException } from '../../exceptions/httpException'; import { SUBNET_URL } from '../../config'; @@ -19,12 +19,12 @@ export class SubnetClient { const keepaliveAgent = new HttpsAgent(); const provider = new Web3.providers.HttpProvider(SUBNET_URL, { keepAlive: true, agent: { https: keepaliveAgent } }); - this.web3 = new Web3(provider).extend(subnetExtensions()); + this.web3 = new Web3(provider).extend(xdcExtensions()); } async getCandidates() { try { - const { candidates, success } = await this.web3.xdcSubnet.getCandidates('latest'); + const { candidates, success } = await this.web3.xdcApi.getCandidates('latest'); if (!success) { throw new Error('Failed on getting the candidates data'); } @@ -37,7 +37,7 @@ export class SubnetClient { async getNetworkInfo() { try { - const { NetworkId, XDCValidatorAddress, Denom, NetworkName } = await this.web3.xdcSubnet.getNetworkInfo(); + const { NetworkId, XDCValidatorAddress, Denom, NetworkName } = await this.web3.xdcApi.getNetworkInfo(); return { networkId: NetworkId, validatorSmartContractAddress: XDCValidatorAddress, @@ -52,7 +52,7 @@ export class SubnetClient { async getLastMasternodesInformation(): Promise { try { - const { Number, Round, Masternodes, Penalty } = await this.web3.xdcSubnet.getMasternodesByNumber('latest'); + const { Number, Round, Masternodes, Penalty } = await this.web3.xdcApi.getMasternodesByNumber('latest'); return { currentBlockNumber: Number, currentRound: Round, @@ -67,7 +67,7 @@ export class SubnetClient { async getLatestCommittedBlockInfo(): Promise<{ hash: string; number: number; round: number }> { try { - const { Hash, Number, Round } = await this.web3.xdcSubnet.getV2Block('committed'); + const { Hash, Number, Round } = await this.web3.xdcApi.getV2Block('committed'); return { hash: Hash, number: Number, @@ -80,7 +80,7 @@ export class SubnetClient { } async getBlockInfoByHash(hash: string) { - const { Hash, Number, Committed, Miner, Timestamp } = await this.web3.xdcSubnet.getV2BlockByHash(hash); + const { Hash, Number, Committed, Miner, Timestamp } = await this.web3.xdcApi.getV2BlockByHash(hash); if (!Hash || !Number) { logger.warn(`Invalid block hash or height or ParentHash received, hash: ${hash}, number: ${Number}`); throw new HttpException(404, 'No such block exit in subnet');