Skip to content

Commit 8abf1f2

Browse files
authored
Return after callback to prevent calling subsequent callback (#141)
1 parent 2d612b5 commit 8abf1f2

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

src/eth-handlers/eth_getTransactionByBlockHashAndIndex.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const buildGetTransactionByBlockHashAndIndex = ({
7373
callback(errorBusy)
7474
countFailedResponse(api_name, 'exception in collectorAPI.getBlock')
7575
logEventEmitter.emit('fn_end', ticket, { success: false }, performance.now())
76+
return
7677
}
7778
const blockHash = (args as any)[0]
7879
const index = parseInt((args as any)[1], 16)
@@ -104,21 +105,21 @@ export const buildGetTransactionByBlockHashAndIndex = ({
104105
{ nodeUrl, success: res.data.transactions.length ? true : false },
105106
performance.now()
106107
)
108+
return
107109
} catch (error) {
108110
/* prettier-ignore */ if (verbose) console.log('Error: eth_getTransactionByBlockHashAndIndex', (error as AxiosError).message)
109111
callback(null, null)
110112
countFailedResponse(api_name, 'exception in axios.get')
111113
logEventEmitter.emit('fn_end', ticket, { success: false }, performance.now())
114+
return
112115
}
113116
} else {
114117
console.log('queryFromValidator and/or queryFromExplorer turned off. Could not process request')
115118
callback(null, null)
116119
countFailedResponse(api_name, 'queryFromValidator and/or queryFromExplorer turned off')
117120
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
121+
return
118122
}
119-
callback(null, result)
120-
countSuccessResponse(api_name, 'success', 'fallback')
121-
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
122123
}
123124

124125
return eth_getTransactionByBlockHashAndIndex

src/eth-handlers/eth_getTransactionByBlockNumberAndIndex.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export const buildGetTransactionByBlockNumberAndIndex = ({
7272
callback(errorBusy)
7373
countFailedResponse(api_name, 'exception in collectorAPI.getBlock')
7474
logEventEmitter.emit('fn_end', ticket, { success: false }, performance.now())
75+
return
7576
}
77+
7678
/* prettier-ignore */ if (firstLineLogs) { console.log('Running eth_getTransactionByBlockNumberAndIndex', args) }
7779
let blockNumber = (args as any)[0]
7880
const index = parseInt((args as any)[1], 16)
@@ -105,21 +107,21 @@ export const buildGetTransactionByBlockNumberAndIndex = ({
105107
{ nodeUrl, success: res.data.transactions.length ? true : false },
106108
performance.now()
107109
)
110+
return
108111
} catch (error) {
109112
/* prettier-ignore */ if (verbose) console.log('Error: eth_getTransactionByBlockNumberAndIndex', (error as AxiosError).message)
110113
callback(null, null)
111114
countFailedResponse(api_name, 'exception in axios.get')
112115
logEventEmitter.emit('fn_end', ticket, { success: false }, performance.now())
116+
return
113117
}
114118
} else {
115119
console.log('queryFromExplorer turned off. Could not process request')
116120
callback(null, null)
117121
countFailedResponse(api_name, 'queryFromExplorer turned off')
118122
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
123+
return
119124
}
120-
callback(null, result)
121-
countSuccessResponse(api_name, 'success', 'fallback')
122-
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
123125
}
124126

125127
return eth_getTransactionByBlockNumberAndIndex

test/unit/eth-handlers/eth_getTransactionByBlockHashAndIndex.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ describe('eth_getTransactionByBlockHashAndIndex handler', () => {
247247
// The implementation calls callback twice:
248248
// First in the explorer path with null (no result)
249249
expect(mockCallback).toHaveBeenNthCalledWith(1, null, undefined)
250-
// Then again at the end of the function with the original result variable (still undefined)
251-
expect(mockCallback).toHaveBeenNthCalledWith(2, null, undefined)
252-
expect(mockCallback).toHaveBeenCalledTimes(2)
250+
expect(mockCallback).toHaveBeenCalledTimes(1)
253251
})
254252

255253
it('should handle explorer unsuccessful response', async () => {
@@ -271,9 +269,7 @@ describe('eth_getTransactionByBlockHashAndIndex handler', () => {
271269
// The implementation calls callback twice:
272270
// First in the explorer path with null (unsuccessful response)
273271
expect(mockCallback).toHaveBeenNthCalledWith(1, null, null)
274-
// Then again at the end of the function with the original result variable (undefined in the refactored test)
275-
expect(mockCallback).toHaveBeenNthCalledWith(2, null, undefined)
276-
expect(mockCallback).toHaveBeenCalledTimes(2)
272+
expect(mockCallback).toHaveBeenCalledTimes(1)
277273
})
278274
})
279275

test/unit/eth-handlers/eth_getTransactionByBlockNumberAndIndex.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,7 @@ describe('eth_getTransactionByBlockNumberAndIndex handler', () => {
312312
// The implementation calls callback twice:
313313
// First in the explorer path with null (no result)
314314
expect(mockCallback).toHaveBeenNthCalledWith(1, null, undefined)
315-
// Then again at the end of the function with the original result variable (still undefined)
316-
expect(mockCallback).toHaveBeenNthCalledWith(2, null, undefined)
317-
expect(mockCallback).toHaveBeenCalledTimes(2)
315+
expect(mockCallback).toHaveBeenCalledTimes(1)
318316
})
319317

320318
it('should handle explorer unsuccessful response', async () => {
@@ -339,9 +337,7 @@ describe('eth_getTransactionByBlockNumberAndIndex handler', () => {
339337
// The implementation calls callback twice:
340338
// First in the explorer path with null (unsuccessful response)
341339
expect(mockCallback).toHaveBeenNthCalledWith(1, null, null)
342-
// Then again at the end of the function with the original result variable (undefined in the refactored test)
343-
expect(mockCallback).toHaveBeenNthCalledWith(2, null, undefined)
344-
expect(mockCallback).toHaveBeenCalledTimes(2)
340+
expect(mockCallback).toHaveBeenCalledTimes(1)
345341
})
346342

347343
it('should handle "earliest" block tag correctly', async () => {

0 commit comments

Comments
 (0)