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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update according to merge of Drive and Machine (#255)
Co-authored-by: shuplenkov <[email protected]> Co-authored-by: Ivan Shumkov <[email protected]>
- Loading branch information
1 parent
3773996
commit dbaa747
Showing
33 changed files
with
1,547 additions
and
1,916 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class AbciResponseError extends Error { | ||
/** | ||
* @param {number} errorCode | ||
* @param {Object} abciError | ||
* @param {string} abciError.message | ||
* @param {Object} abciError.data | ||
*/ | ||
constructor(errorCode, { message, data }) { | ||
super(); | ||
|
||
this.errorCode = errorCode; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
|
||
/** | ||
* Get error code | ||
* | ||
* @return {number} | ||
*/ | ||
getErrorCode() { | ||
return this.errorCode; | ||
} | ||
|
||
/** | ||
* Get error message | ||
* | ||
* @return {string} | ||
*/ | ||
getMessage() { | ||
return this.message; | ||
} | ||
|
||
/** | ||
* Get error data | ||
* | ||
* @return {Object} | ||
*/ | ||
getData() { | ||
return this.data; | ||
} | ||
} | ||
|
||
module.exports = AbciResponseError; |
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,103 @@ | ||
const jayson = require('jayson/promise'); | ||
|
||
const cbor = require('cbor'); | ||
|
||
const RPCError = require('../../rpcServer/RPCError'); | ||
const AbciResponseError = require('../../errors/AbciResponseError'); | ||
|
||
class DriveStateRepository { | ||
/** | ||
* @param options | ||
* @param {string} options.host | ||
* @param {number} options.port | ||
*/ | ||
constructor({ host, port }) { | ||
this.client = jayson.client.http({ host, port }); | ||
} | ||
|
||
/** | ||
* Makes request to Drive and handle response | ||
* | ||
* @param {string} path | ||
* @param {Object} data | ||
* | ||
* @return {Promise<Buffer>} | ||
*/ | ||
async request(path, data = {}) { | ||
const encodedData = cbor.encode(data); | ||
|
||
const { result, error } = await this.client.request( | ||
'abci_query', { | ||
path, | ||
data: encodedData.toString('hex'), | ||
}, | ||
); | ||
|
||
// Handle JSON RPC error | ||
if (error) { | ||
throw new RPCError( | ||
error.code || -32602, error.message || 'Internal error', error.data, | ||
); | ||
} | ||
|
||
// Check and handle ABCI errors | ||
const { response } = result; | ||
|
||
if (response.code === undefined || response.code === 0) { | ||
// no errors found return the serialized response value | ||
return Buffer.from(response.value, 'base64'); | ||
} | ||
|
||
const { error: abciError } = JSON.parse(response.log); | ||
|
||
throw new AbciResponseError(response.code, abciError); | ||
} | ||
|
||
/** | ||
* Fetch serialized data contract | ||
* | ||
* @param {string} contractId | ||
* | ||
* @return {Promise<Buffer>} | ||
*/ | ||
async fetchDataContract(contractId) { | ||
return this.request(`/dataContracts/${contractId}`); | ||
} | ||
|
||
/** | ||
* Fetch serialized documents | ||
* | ||
* @param {string} contractId | ||
* @param {string} type - Documents type to fetch | ||
* | ||
* @param options | ||
* @param {Object} options.where - Mongo-like query | ||
* @param {Object} options.orderBy - Mongo-like sort field | ||
* @param {number} options.limit - how many objects to fetch | ||
* @param {number} options.startAt - number of objects to skip | ||
* @param {number} options.startAfter - exclusive skip | ||
* | ||
* @return {Promise<Buffer[]>} | ||
*/ | ||
async fetchDocuments(contractId, type, options) { | ||
const serializedDocumentsArray = await this.request( | ||
`/dataContracts/${contractId}/documents/${type}`, | ||
options, | ||
); | ||
|
||
return cbor.decode(serializedDocumentsArray); | ||
} | ||
|
||
/** | ||
* Fetch serialized identity | ||
* | ||
* @param {string} id | ||
* | ||
* @return {Promise<Buffer>} | ||
*/ | ||
async fetchIdentity(id) { | ||
return this.request(`/identities/${id}`); | ||
} | ||
} | ||
|
||
module.exports = DriveStateRepository; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.