Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

feat: implement getConsensusParams method #393

Merged
merged 15 commits into from
Aug 17, 2021
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ test/rpc
coverage/
dapidb/
/.env
docker/cache
42 changes: 42 additions & 0 deletions lib/externalApis/tenderdash/getConsensusParamsFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const RPCError = require('../../rpcServer/RPCError');

/**
* @param {RpcClient} rpcClient
* @return {getConsensusParams}
*/
function getConsensusParamsFactory(rpcClient) {
/**
* @typedef getConsensusParams
* @returns {Promise<{
* block: {
* max_bytes: string,
* max_gas: string,
* time_iota_ms: string
* },
* evidence: {
* max_age: string,
* }
* }>}
*/
async function getConsensusParams() {
const { result, error } = await rpcClient.request('consensus_params');

// Handle JSON RPC error
if (error) {
throw new RPCError(
error.code || -32602,
error.message || 'Internal error',
error.data,
);
}

return {
block: result.block,
evidence: result.evidence,
};
}

return getConsensusParams;
}

module.exports = getConsensusParamsFactory;
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const InvalidArgumentGrpcError = require('@dashevo/grpc-common/lib/server/error/InvalidArgumentGrpcError');

const {
server: {
error: {
InternalGrpcError,
},
},
} = require('@dashevo/grpc-common');

const {
v0: {
GetConsensusParamsResponse,
ConsensusParamsBlock,
ConsensusParamsEvidence,
},
} = require('@dashevo/dapi-grpc');

const RPCError = require('../../../rpcServer/RPCError');

/**
*
* @param {getConsensusParams} getConsensusParams
* @returns {getConsensusParamsHandler}
*/
function getConsensusParamsHandlerFactory(getConsensusParams) {
/**
* @typedef getConsensusParamsHandler
* @param {Object} call
* @returns {Promise<>}
*/
async function getConsensusParamsHandler(call) {
const { request } = call;

const prove = request.getProve();

if (prove) {
throw new InvalidArgumentGrpcError('prove is not implemented yet');
}

let consensusParams;

try {
consensusParams = await getConsensusParams();
} catch (e) {
if (e instanceof RPCError) {
throw new InternalGrpcError(e);
}

throw e;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could respond with different errors, for example the height is not valid or something like that. In this case you nee to you other gRPC error codes.

}

const response = new GetConsensusParamsResponse();

const block = new ConsensusParamsBlock();
block.setMaxBytes(consensusParams.block.max_bytes);
block.setMaxGas(consensusParams.block.max_gas);
block.setTimeIotaMs(consensusParams.block.time_iota_ms);

response.setBlock(block);

const evidence = new ConsensusParamsEvidence();
evidence.setMaxAge(consensusParams.evidence.max_age);

response.setEvidence(evidence);

return response;
}

return getConsensusParamsHandler;
}

module.exports = getConsensusParamsHandlerFactory;
21 changes: 21 additions & 0 deletions lib/grpcServer/handlers/platform/platformHandlersFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
GetIdentitiesByPublicKeyHashesRequest,
GetIdentityIdsByPublicKeyHashesRequest,
WaitForStateTransitionResultRequest,
GetConsensusParamsRequest,
pbjs: {
BroadcastStateTransitionRequest: PBJSBroadcastStateTransitionRequest,
BroadcastStateTransitionResponse: PBJSBroadcastStateTransitionResponse,
Expand All @@ -37,6 +38,8 @@ const {
GetIdentityIdsByPublicKeyHashesRequest: PBJSGetIdentityIdsByPublicKeyHashesRequest,
WaitForStateTransitionResultRequest: PBJSWaitForStateTransitionResultRequest,
WaitForStateTransitionResultResponse: PBJSWaitForStateTransitionResultResponse,
GetConsensusParamsRequest: PBJSGetConsensusParamsRequest,
GetConsensusParamsResponse: PBJSGetConsensusParamsResponse,
},
},
} = require('@dashevo/dapi-grpc');
Expand Down Expand Up @@ -66,6 +69,9 @@ const getIdentityIdsByPublicKeyHashesHandlerFactory = require(
const waitForStateTransitionResultHandlerFactory = require(
'./waitForStateTransitionResultHandlerFactory',
);
const getConsensusParamsHandlerFactory = require(
'./getConsensusParamsHandlerFactory',
);

const fetchProofForStateTransitionFactory = require('../../../externalApis/drive/fetchProofForStateTransitionFactory');
const waitForTransactionToBeProvableFactory = require('../../../externalApis/tenderdash/waitForTransactionToBeProvable/waitForTransactionToBeProvableFactory');
Expand Down Expand Up @@ -220,6 +226,20 @@ function platformHandlersFactory(
wrapInErrorHandler(waitForStateTransitionResultHandler),
);

// get Consensus Params
const getConsensusParams = getConsensusParamsHandlerFactory();

const wrappedGetConsensusParams = jsonToProtobufHandlerWrapper(
jsonToProtobufFactory(
GetConsensusParamsRequest,
PBJSGetConsensusParamsRequest,
),
protobufToJsonFactory(
PBJSGetConsensusParamsResponse,
),
wrapInErrorHandler(getConsensusParams),
);

return {
broadcastStateTransition: wrappedBroadcastStateTransition,
getIdentity: wrappedGetIdentity,
Expand All @@ -228,6 +248,7 @@ function platformHandlersFactory(
getIdentitiesByPublicKeyHashes: wrappedGetIdentitiesByPublicKeyHashes,
getIdentityIdsByPublicKeyHashes: wrappedGetIdentityIdsByPublicKeyHashes,
waitForStateTransitionResult: wrappedWaitForStateTransitionResult,
getConsensusParams: wrappedGetConsensusParams,
};
}

Expand Down
Loading