Skip to content

Commit

Permalink
BLUE-222: verify transaction hash format
Browse files Browse the repository at this point in the history
reverting formatting changes as per review comment

reverting formatting changes

moved txHash validation on handler side
  • Loading branch information
aniketdivekar authored and mhanson-github committed Sep 3, 2024
1 parent 7b86f5e commit 9a4b925
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
29 changes: 28 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ let lastBlockInfo = {
//const errorHexStatus: string = '0x' //0x0 if you want an error! (handy for testing..)
const errorCode = 500 //server internal error
const errorBusy = { code: errorCode, message: 'Busy or error' }

const appErrorCode = -32000

export let txStatuses: TxStatus[] = []
const maxTxCountToStore = 10000
const txMemPool: {
Expand Down Expand Up @@ -143,6 +146,12 @@ function isHexOrEmptyHex(str: string) {
return str == '0x' || isHex(str)
}

const txnHashPattern = /^0x([A-Fa-f0-9]{64})$/

function isValidTxHashFormat(txHash: string): boolean {
return txnHashPattern.test(txHash)
}

// Utility function to ensure arguments are an array
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function ensureArrayArgs(args: RequestParamsLike, callback: JSONRPCCallbackTypePlain): args is any[] {
Expand Down Expand Up @@ -3213,8 +3222,14 @@ export const methods = {
return
}

const txHash = args[0].toLowerCase()
if (!isValidTxHashFormat(txHash)) {
callback({ code: appErrorCode, message: 'Please provide valid transaction hash' })
return
}

try {
const result = await replayTransaction(args[0], '-s')
const result = await replayTransaction(txHash, '-s')
callback(null, { structLogs: result })
countSuccessResponse(api_name, 'success', 'replayer')
} catch (e) {
Expand Down Expand Up @@ -3281,6 +3296,12 @@ export const methods = {
continue
}

txHash = txHash.toLowerCase()
if (!isValidTxHashFormat(txHash)) {
callback({ code: appErrorCode, message: 'Please provide valid transaction hash' })
return
}

const txResult = await replayTransaction(txHash, '-s')
result.push({ structLogs: txResult })
}
Expand Down Expand Up @@ -3357,6 +3378,12 @@ export const methods = {
continue
}

txHash = txHash.toLowerCase()
if (!isValidTxHashFormat(txHash)) {
callback({ code: appErrorCode, message: 'Please provide valid transaction hash' })
return
}

const txResult = await replayTransaction(txHash, '-s')
result.push({ structLogs: txResult })
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export async function updateNodeList(tryInfinate = false): Promise<void> {

const waitForAllPromise = new Deferred<void>()
let finished = 0
for(let i=0; i<nodes.length; i++) {
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
//promises.push(checkIfNodeIsActive(node)) //not stacking up promises
//@ts-ignore what are we using es5 for?
Expand Down Expand Up @@ -1569,6 +1569,7 @@ export async function replayGas(tx: { from: string; gas: string } & TxData): Pro
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function replayTransaction(txHash: string, flag: string): Promise<any> {
/* eslint-disable security/detect-non-literal-fs-filename */

const replayPath = path.join(__dirname, '../../../validator/dist/src/debug/replayTX.js')
const transactionsFolder = path.join(__dirname, '../../transactions')

Expand Down Expand Up @@ -1806,7 +1807,6 @@ export function hexToBN(hexString: string): BN {
return new BN(hexString, 16)
}


class Semaphore {
private queue: (() => void)[] = [];
private value: number;
Expand Down Expand Up @@ -1851,4 +1851,4 @@ class Deferred<T> {
this.reject = reject;
});
}
}
}

0 comments on commit 9a4b925

Please sign in to comment.