-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for getTransactionByHash endpoint in the WS server (
#2273) (#2295) * refactor: refactored validateParamsLength Signed-off-by: Logan Nguyen <[email protected]> * feat: added support for getTransactionByHas endpoint in the WS server Signed-off-by: Logan Nguyen <[email protected]> * refactor: refactored sending requests method Signed-off-by: Logan Nguyen <[email protected]> --------- Signed-off-by: Logan Nguyen <[email protected]>
- Loading branch information
1 parent
624072c
commit 967056c
Showing
8 changed files
with
372 additions
and
36 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
packages/server/tests/acceptance/ws/getTransactionByHash.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/*- | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
// external resources | ||
import { expect } from 'chai'; | ||
import { ethers, WebSocketProvider } from 'ethers'; | ||
import RelayClient from '../../clients/relayClient'; | ||
import MirrorClient from '../../clients/mirrorClient'; | ||
import { AliasAccount } from '../../clients/servicesClient'; | ||
import { numberTo0x } from '@hashgraph/json-rpc-relay/src/formatters'; | ||
import { ONE_TINYBAR_IN_WEI_HEX } from '@hashgraph/json-rpc-relay/tests/lib/eth/eth-config'; | ||
|
||
describe('@release @web-socket eth_getTransactionByHash', async function () { | ||
const WS_RELAY_URL = `${process.env.WS_RELAY_URL}`; | ||
const METHOD_NAME = 'eth_getTransactionByHash'; | ||
const CHAIN_ID = process.env.CHAIN_ID || '0x12a'; | ||
const INVALID_PARAMS = [['hedera', 'hbar'], [], ['websocket', 'rpc', 'invalid']]; | ||
const INVALID_TX_HASH = ['0xhbar', '0xHedera', '', 66, 'abc', true, false, 39]; | ||
|
||
let accounts: AliasAccount[] = []; | ||
let mirrorNodeServer: MirrorClient, requestId: string, relayClient: RelayClient, wsProvider: WebSocketProvider; | ||
|
||
before(async () => { | ||
// @ts-ignore | ||
const { servicesNode, mirrorNode, relay } = global; | ||
|
||
mirrorNodeServer = mirrorNode; | ||
relayClient = relay; | ||
|
||
accounts[0] = await servicesNode.createAliasAccount(100, relay.provider, requestId); | ||
accounts[1] = await servicesNode.createAliasAccount(5, relay.provider, requestId); | ||
}); | ||
|
||
beforeEach(async () => { | ||
wsProvider = new ethers.WebSocketProvider(WS_RELAY_URL); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
}); | ||
|
||
afterEach(async () => { | ||
if (wsProvider) { | ||
await wsProvider.destroy(); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} | ||
}); | ||
|
||
for (const params of INVALID_PARAMS) { | ||
it(`Should throw predefined.INVALID_PARAMETERS if the request's params variable is invalid (params.length !== 1). params=[${params}]`, async () => { | ||
try { | ||
await wsProvider.send(METHOD_NAME, params); | ||
expect(true).to.eq(false); | ||
} catch (error) { | ||
expect(error.error).to.exist; | ||
expect(error.error.code).to.eq(-32602); | ||
expect(error.error.name).to.eq('Invalid parameters'); | ||
expect(error.error.message).to.eq('Invalid params'); | ||
} | ||
}); | ||
} | ||
|
||
for (const txHash of INVALID_TX_HASH) { | ||
it(`Should handle invalid data correctly. txHash = ${txHash}`, async () => { | ||
try { | ||
const res = await wsProvider.send(METHOD_NAME, [txHash]); | ||
if (txHash === '') { | ||
expect(res).to.be.null; | ||
} else { | ||
expect(true).to.eq(false); | ||
} | ||
} catch (error) { | ||
expect(error.error.code).to.eq(-32603); | ||
expect(error.error.name).to.eq(`Internal error`); | ||
expect(error.error.message).to.eq( | ||
'Error invoking RPC: "Invalid Transaction id. Please use \\"shard.realm.num-sss-nnn\\" format where sss are seconds and nnn are nanoseconds"', | ||
); | ||
} | ||
}); | ||
} | ||
|
||
it('Should handle valid data correctly', async () => { | ||
const tx = { | ||
value: ONE_TINYBAR_IN_WEI_HEX, | ||
gasLimit: numberTo0x(30000), | ||
chainId: Number(CHAIN_ID), | ||
to: accounts[1].address, | ||
nonce: await relayClient.getAccountNonce(accounts[0].address, requestId), | ||
maxFeePerGas: await relayClient.gasPrice(requestId), | ||
}; | ||
|
||
const signedTx = await accounts[0].wallet.signTransaction(tx); | ||
const txHash = await relayClient.sendRawTransaction(signedTx, requestId); | ||
const expectedTxReceipt = await mirrorNodeServer.get(`/contracts/results/${txHash}`); | ||
|
||
const txReceipt = await wsProvider.send(METHOD_NAME, [txHash]); | ||
|
||
expect(txReceipt.from).to.be.eq(accounts[0].address); | ||
expect(txReceipt.to).to.be.eq(accounts[1].address); | ||
expect(txReceipt.blockHash).to.be.eq(expectedTxReceipt.block_hash.slice(0, 66)); | ||
expect(txReceipt.hash).to.be.eq(expectedTxReceipt.hash); | ||
expect(txReceipt.r).to.be.eq(expectedTxReceipt.r); | ||
expect(txReceipt.s).to.be.eq(expectedTxReceipt.s); | ||
expect(Number(txReceipt.v)).to.be.eq(expectedTxReceipt.v); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* - | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { sendToClient } from '../utils/utils'; | ||
import { Relay } from '@hashgraph/json-rpc-relay'; | ||
import { validateParamsLength } from '../utils/validators'; | ||
import { handleSendingTransactionRequests } from './helpers'; | ||
|
||
/** | ||
* Handles the "eth_getTransactionByHash" method request by retrieving transaction details from the Hedera network. | ||
* Validates the parameters, retrieves the transaction details, and sends the response back to the client. | ||
* @param {any} ctx - The context object containing information about the WebSocket connection. | ||
* @param {any[]} params - The parameters of the method request, expecting a single parameter: the transaction hash. | ||
* @param {any} logger - The logger object for logging messages and events. | ||
* @param {Relay} relay - The relay object for interacting with the Hedera network. | ||
* @param {any} request - The request object received from the client. | ||
* @param {string} method - The JSON-RPC method associated with the request. | ||
* @param {string} socketIdPrefix - The prefix for the socket ID. | ||
* @param {string} requestIdPrefix - The prefix for the request ID. | ||
* @param {string} connectionIdPrefix - The prefix for the connection ID. | ||
* @returns {Promise<any>} Returns a promise that resolves with the JSON-RPC response to the client. | ||
* @throws {JsonRpcError} Throws a JsonRpcError if there is an issue with the parameters or an internal error occurs. | ||
*/ | ||
export const handleEthGetTransactionByHash = async ( | ||
ctx: any, | ||
params: any, | ||
logger: any, | ||
relay: Relay, | ||
request: any, | ||
method: string, | ||
socketIdPrefix: string, | ||
requestIdPrefix: string, | ||
connectionIdPrefix: string, | ||
) => { | ||
const TX_HASH = params[0]; | ||
const TAG = JSON.stringify({ method, signedTx: TX_HASH }); | ||
|
||
validateParamsLength( | ||
ctx, | ||
params, | ||
method, | ||
TAG, | ||
logger, | ||
sendToClient, | ||
1, | ||
socketIdPrefix, | ||
requestIdPrefix, | ||
connectionIdPrefix, | ||
); | ||
|
||
logger.info( | ||
`${connectionIdPrefix} ${requestIdPrefix} ${socketIdPrefix}: Retrieving transaction info with txHash=${TX_HASH} for tag=${TAG}`, | ||
); | ||
|
||
return handleSendingTransactionRequests( | ||
ctx, | ||
TAG, | ||
TX_HASH, | ||
relay, | ||
logger, | ||
request, | ||
method, | ||
'getTransactionByHash', | ||
socketIdPrefix, | ||
requestIdPrefix, | ||
connectionIdPrefix, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* - | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
import { sendToClient } from '../utils/utils'; | ||
import { Relay } from '@hashgraph/json-rpc-relay'; | ||
import { predefined } from '@hashgraph/json-rpc-relay'; | ||
import jsonResp from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcResponse'; | ||
|
||
/** | ||
* Handles sending transaction-related requests to the Hedera network, such as sending raw transactions or getting transaction information. | ||
* Executes the specified Hedera RPC call endpoint with the provided argument, retrieves the response, and sends it back to the client. | ||
* @param {any} ctx - The context object containing information about the WebSocket connection. | ||
* @param {string} tag - A tag used for logging and identifying the message. | ||
* @param {string} arg - The argument required for the Hedera RPC call. | ||
* @param {Relay} relay - The relay object for interacting with the Hedera network. | ||
* @param {any} logger - The logger object for logging messages and events. | ||
* @param {any} request - The request object received from the client. | ||
* @param {string} method - The JSON-RPC method associated with the request. | ||
* @param {string} rpcCallEndpoint - The Hedera RPC call endpoint to execute. | ||
* @param {string} socketIdPrefix - The prefix for the socket ID. | ||
* @param {string} requestIdPrefix - The prefix for the request ID. | ||
* @param {string} connectionIdPrefix - The prefix for the connection ID. | ||
* @returns {Promise<any>} Returns a promise that resolves with the JSON-RPC response to the client. | ||
* @throws {JsonRpcError} Throws a JsonRpcError if there is an issue with the Hedera RPC call or an internal error occurs. | ||
*/ | ||
export const handleSendingTransactionRequests = async ( | ||
ctx: any, | ||
tag: string, | ||
arg: string, | ||
relay: Relay, | ||
logger: any, | ||
request: any, | ||
method: string, | ||
rpcCallEndpoint: string, | ||
socketIdPrefix: string, | ||
requestIdPrefix: string, | ||
connectionIdPrefix: string, | ||
): Promise<any> => { | ||
try { | ||
const txRes = await relay.eth()[rpcCallEndpoint](arg, requestIdPrefix); | ||
|
||
if (txRes) { | ||
sendToClient(ctx.websocket, method, txRes, tag, logger, socketIdPrefix, requestIdPrefix, connectionIdPrefix); | ||
} else { | ||
logger.error( | ||
`${connectionIdPrefix} ${requestIdPrefix} ${socketIdPrefix}: Fail to retrieve result for tag=${tag}`, | ||
); | ||
} | ||
|
||
return jsonResp(request.id, null, txRes); | ||
} catch (error: any) { | ||
sendToClient( | ||
ctx.websocket, | ||
method, | ||
JSON.stringify(error.message || error), | ||
tag, | ||
logger, | ||
socketIdPrefix, | ||
requestIdPrefix, | ||
connectionIdPrefix, | ||
); | ||
|
||
throw predefined.INTERNAL_ERROR(JSON.stringify(error.message || error)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.