From 4e5ee1e58d5cb0d25cfb749cc60e0e7bd80f1cb3 Mon Sep 17 00:00:00 2001 From: aman035 Date: Mon, 23 Sep 2024 13:13:19 +0530 Subject: [PATCH] fix: block tx response, temp validator fix --- packages/core/src/lib/block/block.ts | 5 +- packages/core/src/lib/block/block.types.ts | 17 ++++++ packages/core/src/lib/tx/tx.ts | 16 +++-- packages/core/src/lib/tx/tx.types.ts | 16 +++-- packages/core/src/lib/validator/validator.ts | 62 +++++++++++++++----- 5 files changed, 88 insertions(+), 28 deletions(-) diff --git a/packages/core/src/lib/block/block.ts b/packages/core/src/lib/block/block.ts index a31cf84a..53297e57 100644 --- a/packages/core/src/lib/block/block.ts +++ b/packages/core/src/lib/block/block.ts @@ -1,6 +1,7 @@ import { ENV } from '../constants'; import { Validator } from '../validator/validator'; import { Block as BlockType } from '../generated/block'; +import { BlockResponse } from './block.types'; export class Block { private constructor(private validator: Validator) {} @@ -29,7 +30,7 @@ export class Block { pageSize = 30, page = 1 ) => { - return await this.validator.call('push_getBlocks', [ + return await this.validator.call('push_getBlocks', [ startTime, direction, showDetails, @@ -43,7 +44,7 @@ export class Block { * @param txHash */ search = async (blockHash: string) => { - return await this.validator.call('push_getBlockByHash', [ + return await this.validator.call('push_getBlockByHash', [ blockHash, ]); }; diff --git a/packages/core/src/lib/block/block.types.ts b/packages/core/src/lib/block/block.types.ts index e69de29b..629a8d04 100644 --- a/packages/core/src/lib/block/block.types.ts +++ b/packages/core/src/lib/block/block.types.ts @@ -0,0 +1,17 @@ +import { TxResponse } from '../tx/tx.types'; + +export type BlockType = { + blockHash: string; + blockData: string; + blockDataAsJson: any; + blockSize: number; + ts: number; + transactions: TxResponse[]; + totalNumberOfTxns: number; +}; + +export type BlockResponse = { + blocks: BlockType[]; + lastTs: number; + totalPages: number; +}; diff --git a/packages/core/src/lib/tx/tx.ts b/packages/core/src/lib/tx/tx.ts index 85f62dc0..8ab4d398 100644 --- a/packages/core/src/lib/tx/tx.ts +++ b/packages/core/src/lib/tx/tx.ts @@ -1,6 +1,6 @@ import { v4 as uuidv4, parse } from 'uuid'; import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils'; -import { TxCategory, TxWithBlockHash } from './tx.types'; +import { TxCategory } from './tx.types'; import { Transaction } from '../generated/tx'; import { InitDid } from '../generated/txData/init_did'; import { InitSessionKey } from '../generated/txData/init_session_key'; @@ -9,6 +9,7 @@ import { Validator } from '../validator/validator'; import { TokenReply } from '../validator/validator.types'; import { hexToBytes } from 'viem'; import { privateKeyToAccount } from 'viem/accounts'; +import { BlockResponse } from '../block/block.types'; export class Tx { private constructor(private validator: Validator) {} @@ -98,10 +99,13 @@ export class Tx { page = 1, category?: string ) => { - return await this.validator.call( - 'push_getTransactions', - [startTime, direction, pageSize, page, category] - ); + return await this.validator.call('push_getTransactions', [ + startTime, + direction, + pageSize, + page, + category, + ]); }; /** @@ -109,7 +113,7 @@ export class Tx { * @param txHash */ search = async (txHash: string) => { - return await this.validator.call( + return await this.validator.call( 'push_getTransactionByHash', [txHash] ); diff --git a/packages/core/src/lib/tx/tx.types.ts b/packages/core/src/lib/tx/tx.types.ts index 0f1287a1..9e1f9971 100644 --- a/packages/core/src/lib/tx/tx.types.ts +++ b/packages/core/src/lib/tx/tx.types.ts @@ -1,12 +1,18 @@ -import { Transaction } from '../generated/tx'; - // TxCategory supported for Serealization / Deserealization by core export enum TxCategory { INIT_DID = 'INIT_DID', INIT_SESSION_KEY = 'INIT_SESSION_KEY', } -export type TxWithBlockHash = Transaction & { - // can be undefined in case of rejected / failed Tx - blockHash?: string; +export type TxResponse = { + txnHash: string; + ts: number; + /**@dev - Null In case of rejected Tx */ + blockHash: string | null; + category: string; + sender: string; + status: 'SUCCESS' | 'REJECTED'; + recipients: string[]; + txnData: string; + sig: string; }; diff --git a/packages/core/src/lib/validator/validator.ts b/packages/core/src/lib/validator/validator.ts index 75bcef9b..9c371937 100644 --- a/packages/core/src/lib/validator/validator.ts +++ b/packages/core/src/lib/validator/validator.ts @@ -70,7 +70,8 @@ export class Validator { abi: config.ABIS.VALIDATOR, address: config.VALIDATOR[env].VALIDATOR_CONTRACT as `0x${string}`, client: { - public: client, + // Viem type causes issue with some codebases + public: client as never, }, }) as unknown as ValidatorContract; }; @@ -91,19 +92,8 @@ export class Validator { id: this.idCounter++, }; - // Local Docker URL Mapping Changes - const vUrl = () => { - if (url.includes('.local')) { - return url.replace('.local', '.localh'); - } - return url; - }; - try { - const response = await axios.post>( - `${vUrl()}/api/v1/rpc`, - requestBody - ); + const response = await axios.post>(url, requestBody); if (response.data.error) { console.error('JSON-RPC Error:', response.data.error); @@ -122,7 +112,7 @@ export class Validator { */ private static ping = async (validatorUrl: string): Promise => { return await this.sendJsonRpcRequest( - validatorUrl, + Validator.vNodeUrlModifier(validatorUrl), 'push_listening' ); }; @@ -149,6 +139,44 @@ export class Validator { } }; + private static vNodeUrlModifier = (url: string) => { + let modifiedUrl = url; + if (url.includes('.local')) { + modifiedUrl = url.replace('.local', '.localh'); + } + return `${modifiedUrl}/api/v1/rpc`; + }; + + /** + * @dev - This is a Temp Function which will be removed in the future + */ + private ReqModifier = (url: string, fnName: string) => { + let modifiedUrl = Validator.vNodeUrlModifier(url); + let modifiedFnName = fnName; + if ( + fnName === 'push_getBlocks' || + fnName === 'push_getBlockByHash' || + fnName === 'push_getTransactions' || + fnName === 'push_getTransactionByHash' + ) { + if (this.env === ENV.LOCAL) { + modifiedUrl = 'http://localhost:5001/rpc'; + } + if (this.env === ENV.DEV) { + modifiedUrl = 'https://anode1.push.org/rpc'; + } + modifiedFnName = `RpcService.${fnName.replace('push_', '')}`; + + if (fnName === 'push_getTransactions') { + modifiedFnName = 'RpcService.getTxs'; + } + if (fnName === 'push_getTransactionByHash') { + modifiedFnName = 'RpcService.getTxByHash'; + } + } + return { url: modifiedUrl, fnName: modifiedFnName }; + }; + /** * @description Get calls to validator * @returns Reply of the call @@ -159,6 +187,10 @@ export class Validator { params: any[] = [], url: string = this.activeValidatorURL ): Promise => { - return await Validator.sendJsonRpcRequest(url, fnName, params); + return await Validator.sendJsonRpcRequest( + this.ReqModifier(url, fnName).url, + this.ReqModifier(url, fnName).fnName, + params + ); }; }