-
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
4 changed files
with
206 additions
and
10 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/dapi/lib/externalApis/tenderdash/requestTenderRpc.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,55 @@ | ||
const UnavailableGrpcError = require('@dashevo/grpc-common/lib/server/error/UnavailableGrpcError'); | ||
const ResourceExhaustedGrpcError = require('@dashevo/grpc-common/lib/server/error/ResourceExhaustedGrpcError'); | ||
const RPCError = require('../../rpcServer/RPCError'); | ||
|
||
/** | ||
* @param {jaysonClient} rpcClient | ||
* @return {requestTenderRpc} A function to make RPC requests to Tenderdash. | ||
*/ | ||
function requestTenderRpcFactory(rpcClient) { | ||
/** | ||
* @function | ||
* @typedef requestTenderRpc | ||
* @param {string} uri | ||
* @param {Object} [params={}] | ||
* @return {Promise<Object>} | ||
*/ | ||
async function requestTenderRpc(uri, params = {}) { | ||
let response; | ||
try { | ||
response = await rpcClient.request(uri, params); | ||
} catch (e) { | ||
if (e.code === 'ECONNRESET' || e.message === 'socket hang up') { | ||
throw new UnavailableGrpcError('Tenderdash is not available'); | ||
} | ||
|
||
throw new RPCError( | ||
e.code || -32602, | ||
`Failed to request ${uri}: ${e.message}`, | ||
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'); | ||
} | ||
} | ||
|
||
throw new RPCError( | ||
jsonRpcError.code || -32602, | ||
jsonRpcError.message || 'Internal error', | ||
jsonRpcError.data, | ||
); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
return requestTenderRpc; | ||
} | ||
|
||
module.exports = requestTenderRpcFactory; |
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
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