This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: implement getConsensusParams method #393
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
585ed0f
feat: implement getConsensusParams method
8ba4098
chore: update dependencies
28eaf80
Merge branch 'v0.21-dev' into get-consensus-params
51f4dd0
Merge branch 'v0.21-dev' into get-consensus-params
antouhou 51cf651
update lockfile
antouhou 1b9bb21
Merge remote-tracking branch 'origin/get-consensus-params' into get-c…
280b2dc
chore: update dependencies
e8edc40
Merge branch 'v0.21-dev' into get-consensus-params
49eb99e
chore: update dependencies
b597370
fix: eslint warning
dfe59fc
Merge branch 'v0.21-dev' into get-consensus-params
a08d1f2
fix: eslint warning
a71997c
chore: update dependencies
1f3e6f4
fix: bugfix
5f4f8c6
test: add unit tests for getConsensusParams
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ test/rpc | |
coverage/ | ||
dapidb/ | ||
/.env | ||
docker/cache |
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,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; |
73 changes: 73 additions & 0 deletions
73
lib/grpcServer/handlers/platform/getConsensusParamsHandlerFactory.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,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; | ||
} | ||
|
||
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; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.