-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dapi): invalid state transition failed with already in chain error
- Loading branch information
Showing
2 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...grpcServer/handlers/platform/broadcastStateTransition/fetchCachedStateTransitionResult.js
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,72 @@ | ||
const UnavailableGrpcError = require('@dashevo/grpc-common/lib/server/error/UnavailableGrpcError'); | ||
const ResourceExhaustedGrpcError = require('@dashevo/grpc-common/lib/server/error/ResourceExhaustedGrpcError'); | ||
const logger = require('../../../../logger'); | ||
|
||
/** | ||
* @param {jaysonClient} rpcClient | ||
* @param {string} uri | ||
* @param {Object} params | ||
* @return {Promise<any>} | ||
*/ | ||
async function request(rpcClient, uri, params = {}) { | ||
let response; | ||
try { | ||
response = await rpcClient.request(uri, params); | ||
} catch (e) { | ||
if (e.message === 'socket hang up') { | ||
throw new UnavailableGrpcError('Tenderdash is not available'); | ||
} | ||
|
||
e.message = `Failed to fetch cached transaction: ${e.message}`; | ||
|
||
throw e; | ||
} | ||
|
||
const { result, error: jsonRpcError } = response; | ||
|
||
if (jsonRpcError) { | ||
if (typeof jsonRpcError.data === 'string') { | ||
if (jsonRpcError.data.includes('too_many_resets')) { | ||
throw new ResourceExhaustedGrpcError('tenderdash is not responding: too many requests'); | ||
} | ||
} | ||
|
||
const error = new Error(); | ||
Object.assign(error, jsonRpcError); | ||
|
||
logger.error(error, `Unexpected JSON RPC error during broadcasting state transition: ${JSON.stringify(jsonRpcError)}`); | ||
|
||
throw error; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* | ||
* @param {jaysonClient} rpcClient | ||
* @return {fetchCachedStateTransitionResult} | ||
*/ | ||
function fetchCachedStateTransitionResultFactory(rpcClient) { | ||
/** | ||
* @typedef fetchCachedStateTransitionResult | ||
* @param {Buffer} stBytes | ||
* @return {Promise<Object>} | ||
*/ | ||
return async function fetchCachedStateTransitionResult(stBytes) { | ||
// Subscribing to future result | ||
const stHash = crypto.createHash('sha256') | ||
.update(stBytes) | ||
.digest(); | ||
|
||
// Search cached state transition in mempool | ||
// rpcClient.request('/unconfirmed_txs'); | ||
|
||
// Search in blockchain data | ||
const result = await request(rpcClient, '/tx', { hash: `0x${stHash.toString('hex')}` }); | ||
|
||
|
||
}; | ||
} | ||
|
||
module.exports = fetchCachedStateTransitionResultFactory; |
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