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
6 changes: 3 additions & 3 deletions .github/workflows/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DRIVE_BRANCH=
# DASHMATE_BRANCH=
TEST_SUITE_BRANCH=fix-document-test
DRIVE_BRANCH=update_consensus_params_feature_flag
DASHMATE_BRANCH=v0.21-dev
TEST_SUITE_BRANCH=feature-flags
# SDK_BRANCH=
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
51 changes: 51 additions & 0 deletions lib/externalApis/tenderdash/getConsensusParamsFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const RPCError = require('../../rpcServer/RPCError');

/**
* @param {RpcClient} rpcClient
* @return {getConsensusParams}
*/
function getConsensusParamsFactory(rpcClient) {
/**
* @typedef getConsensusParams
* @param {number} [height]
* @returns {Promise<{
* block: {
* max_bytes: string,
* max_gas: string,
* time_iota_ms: string
* },
* evidence: {
* max_age_num_blocks: string,
* max_age_duration: string,
* max_bytes: string,
* }
* }>}
*/
async function getConsensusParams(height = undefined) {
const params = {};

if (height !== undefined) {
params.height = height;
}

const { result, error } = await rpcClient.request('consensus_params', params);

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

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

return getConsensusParams;
}

module.exports = getConsensusParamsFactory;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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.setMaxAgeNumBlocks(consensusParams.evidence.max_age_num_blocks);
evidence.setMaxAgeDuration(consensusParams.evidence.max_age_duration);
evidence.setMaxBytes(consensusParams.evidence.max_bytes);

response.setEvidence(evidence);

return response;
}

return getConsensusParamsHandler;
}

module.exports = getConsensusParamsHandlerFactory;
23 changes: 23 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,12 +69,16 @@ const getIdentityIdsByPublicKeyHashesHandlerFactory = require(
const waitForStateTransitionResultHandlerFactory = require(
'./waitForStateTransitionResultHandlerFactory',
);
const getConsensusParamsHandlerFactory = require(
'./getConsensusParamsHandlerFactory',
);

const fetchProofForStateTransitionFactory = require('../../../externalApis/drive/fetchProofForStateTransitionFactory');
const waitForTransactionToBeProvableFactory = require('../../../externalApis/tenderdash/waitForTransactionToBeProvable/waitForTransactionToBeProvableFactory');
const waitForTransactionResult = require('../../../externalApis/tenderdash/waitForTransactionToBeProvable/waitForTransactionResult');
const waitForHeightFactory = require('../../../externalApis/tenderdash/waitForHeightFactory');
const getExistingTransactionResultFactory = require('../../../externalApis/tenderdash/waitForTransactionToBeProvable/getExistingTransactionResult');
const getConsensusParamsFactory = require('../../../externalApis/tenderdash/getConsensusParamsFactory');

/**
* @param {jaysonClient} rpcClient
Expand Down Expand Up @@ -220,6 +227,21 @@ function platformHandlersFactory(
wrapInErrorHandler(waitForStateTransitionResultHandler),
);

// get Consensus Params
const getConsensusParams = getConsensusParamsFactory(rpcClient);
const getConsensusParamsHandler = getConsensusParamsHandlerFactory(getConsensusParams);

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

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

Expand Down
Loading