Skip to content

Commit

Permalink
fix: block tx response, temp validator fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Sep 23, 2024
1 parent 5fcef75 commit 4e5ee1e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 28 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/lib/block/block.ts
Original file line number Diff line number Diff line change
@@ -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) {}
Expand Down Expand Up @@ -29,7 +30,7 @@ export class Block {
pageSize = 30,
page = 1
) => {
return await this.validator.call<BlockType[]>('push_getBlocks', [
return await this.validator.call<BlockResponse>('push_getBlocks', [
startTime,
direction,
showDetails,
Expand All @@ -43,7 +44,7 @@ export class Block {
* @param txHash
*/
search = async (blockHash: string) => {
return await this.validator.call<BlockType>('push_getBlockByHash', [
return await this.validator.call<BlockResponse>('push_getBlockByHash', [
blockHash,
]);
};
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/lib/block/block.types.ts
Original file line number Diff line number Diff line change
@@ -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;
};
16 changes: 10 additions & 6 deletions packages/core/src/lib/tx/tx.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {}
Expand Down Expand Up @@ -98,18 +99,21 @@ export class Tx {
page = 1,
category?: string
) => {
return await this.validator.call<TxWithBlockHash[]>(
'push_getTransactions',
[startTime, direction, pageSize, page, category]
);
return await this.validator.call<BlockResponse>('push_getTransactions', [
startTime,
direction,
pageSize,
page,
category,
]);
};

/**
* Search Transaction with a given hash
* @param txHash
*/
search = async (txHash: string) => {
return await this.validator.call<TxWithBlockHash>(
return await this.validator.call<BlockResponse>(
'push_getTransactionByHash',
[txHash]
);
Expand Down
16 changes: 11 additions & 5 deletions packages/core/src/lib/tx/tx.types.ts
Original file line number Diff line number Diff line change
@@ -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;
};
62 changes: 47 additions & 15 deletions packages/core/src/lib/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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<JsonRpcResponse<T>>(
`${vUrl()}/api/v1/rpc`,
requestBody
);
const response = await axios.post<JsonRpcResponse<T>>(url, requestBody);

if (response.data.error) {
console.error('JSON-RPC Error:', response.data.error);
Expand All @@ -122,7 +112,7 @@ export class Validator {
*/
private static ping = async (validatorUrl: string): Promise<boolean> => {
return await this.sendJsonRpcRequest<boolean>(
validatorUrl,
Validator.vNodeUrlModifier(validatorUrl),
'push_listening'
);
};
Expand All @@ -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
Expand All @@ -159,6 +187,10 @@ export class Validator {
params: any[] = [],
url: string = this.activeValidatorURL
): Promise<T> => {
return await Validator.sendJsonRpcRequest<T>(url, fnName, params);
return await Validator.sendJsonRpcRequest<T>(
this.ReqModifier(url, fnName).url,
this.ReqModifier(url, fnName).fnName,
params
);
};
}

0 comments on commit 4e5ee1e

Please sign in to comment.