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

Commit

Permalink
chore: update according to merge of Drive and Machine (#255)
Browse files Browse the repository at this point in the history
Co-authored-by: shuplenkov <[email protected]>
Co-authored-by: Ivan Shumkov <[email protected]>
  • Loading branch information
3 people authored Apr 6, 2020
1 parent 3773996 commit dbaa747
Show file tree
Hide file tree
Showing 33 changed files with 1,547 additions and 1,916 deletions.
5 changes: 0 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ DASHCORE_P2P_HOST = 127.0.0.1
DASHCORE_P2P_PORT = 30001
DASHCORE_P2P_NETWORK = testnet

# Drive service connection setting
DRIVE_RPC_HOST = 127.0.0.1
DRIVE_RPC_PORT = 6000

# Can be `testnet`, `regtest` and `livenet`
NETWORK = testnet

Expand All @@ -44,4 +40,3 @@ TENDERMINT_RPC_PORT=26657
# SERVICE_IMAGE_DAPI= # DAPI image name, if omitted dashpay/dapi is used
# SERVICE_IMAGE_INSIGHT= # Insight image name, if omitted dashpay/evoinsight is used
# SERVICE_IMAGE_CORE= # Dash Core image name, if omitted dashpay/dashcore is used
# SERVICE_IMAGE_MACHINE= # Machine image name, if omitted dashpay/js-machine is used
6 changes: 0 additions & 6 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const OPTIONS = {
DASHCORE_P2P_HOST: 'DASHCORE_P2P_HOST',
DASHCORE_P2P_PORT: 'DASHCORE_P2P_PORT',
DASHCORE_P2P_NETWORK: 'DASHCORE_P2P_NETWORK',
DRIVE_RPC_HOST: 'DRIVE_RPC_HOST',
DRIVE_RPC_PORT: 'DRIVE_RPC_PORT',
NETWORK: 'NETWORK',
BLOOM_FILTER_PERSISTENCE_TIMEOUT: 'BLOOM_FILTER_PERSISTENCE_TIMEOUT',
TENDERMINT_RPC_HOST: 'TENDERMINT_RPC_HOST',
Expand Down Expand Up @@ -89,10 +87,6 @@ module.exports = {
},
network: config[OPTIONS.NETWORK].toLowerCase(),
bloomFilterPersistenceTimeout: config[OPTIONS.BLOOM_FILTER_PERSISTENCE_TIMEOUT],
drive: {
host: config[OPTIONS.DRIVE_RPC_HOST],
port: parseInt(config[OPTIONS.DRIVE_RPC_PORT], 10),
},
tendermintCore: {
host: config[OPTIONS.TENDERMINT_RPC_HOST],
port: parseInt(config[OPTIONS.TENDERMINT_RPC_PORT], 10),
Expand Down
4 changes: 2 additions & 2 deletions lib/config/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function validateConfig(config) {
validationResults.push(validatePort(config.dashcore.rpc.port, 'DASHCORE_RPC_PORT'));
validationResults.push(validateHost(config.dashcore.zmq.host, 'DASHCORE_ZMQ_HOST'));
validationResults.push(validatePort(config.dashcore.zmq.port, 'DASHCORE_ZMQ_PORT'));
validationResults.push(validateHost(config.drive.host, 'DRIVE_RPC_HOST'));
validationResults.push(validatePort(config.drive.port, 'DRIVE_RPC_PORT'));
validationResults.push(validateHost(config.tendermintCore.host, 'TENDERMINT_RPC_HOST'));
validationResults.push(validatePort(config.tendermintCore.port, 'TENDERMINT_RPC_PORT'));
validationResults.push(validatePort(config.rpcServer.port.toString(), 'API_JSON_RPC_PORT'));
validationResults.push(validatePort(config.grpcServer.port.toString(), 'API_GRPC_PORT'));
validationResults.push(validatePort(config.txFilterStream.grpcServer.port.toString(), 'TX_FILTER_STREAM_GRPC_PORT'));
Expand Down
44 changes: 44 additions & 0 deletions lib/errors/AbciResponseError.js
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;
103 changes: 103 additions & 0 deletions lib/externalApis/drive/DriveStateRepository.js
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;
39 changes: 0 additions & 39 deletions lib/externalApis/driveAdapter/AbstractDriveAdapter.js

This file was deleted.

70 changes: 0 additions & 70 deletions lib/externalApis/driveAdapter/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ const {
InvalidArgumentGrpcError,
DeadlineExceededGrpcError,
ResourceExhaustedGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');

/**
* @typedef handleAbciResponse
* @param {{code: number, log: string}} response
* @typedef handleAbciResponseError
* @param {AbciResponseError} error
*/
function handleAbciResponse(response) {
if (response.code === undefined || response.code === 0) {
return;
}

const { error: { message, data } } = JSON.parse(response.log);
function handleAbciResponseError(error) {
const code = error.getErrorCode();
const message = error.getMessage();
const data = error.getData();

switch (response.code) {
switch (code) {
case 6: // MEMORY_LIMIT_EXCEEDED
throw new ResourceExhaustedGrpcError(message, data);
case 5: // EXECUTION_TIMED_OUT
throw new DeadlineExceededGrpcError(message, data);
case 3: // NOT_FOUND
throw new NotFoundGrpcError(message, data);
case 2: // INVALID_ARGUMENT
throw new InvalidArgumentGrpcError(message, data);
case 1: // INTERNAL
Expand All @@ -36,4 +37,4 @@ function handleAbciResponse(response) {
}
}

module.exports = handleAbciResponse;
module.exports = handleAbciResponseError;
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ const {
ApplyStateTransitionResponse,
} = require('@dashevo/dapi-grpc');

const AbciResponseError = require('../../../errors/AbciResponseError');

/**
*
* @param {jaysonClient} rpcClient
* @param {handleAbciResponse} handleAbciResponse
* @param {handleAbciResponseError} handleAbciResponseError
*
* @returns {applyStateTransitionHandler}
*/
function applyStateTransitionHandlerFactory(rpcClient, handleAbciResponse) {
function applyStateTransitionHandlerFactory(rpcClient, handleAbciResponseError) {
/**
* @typedef applyStateTransitionHandler
*
* @param {Object} call
*
* @return {Promise<ApplyStateTransitionResponse>}
*/
async function applyStateTransitionHandler(call) {
const { request } = call;
Expand All @@ -42,9 +47,21 @@ function applyStateTransitionHandlerFactory(rpcClient, handleAbciResponse) {

const { check_tx: checkTx, deliver_tx: deliverTx } = result;

handleAbciResponse(checkTx);
if (checkTx.code !== undefined && checkTx.code !== 0) {
const { error: abciError } = JSON.parse(checkTx.log);

handleAbciResponse(deliverTx);
handleAbciResponseError(
new AbciResponseError(checkTx.code, abciError),
);
}

if (deliverTx.code !== undefined && deliverTx.code !== 0) {
const { error: abciError } = JSON.parse(deliverTx.log);

handleAbciResponseError(
new AbciResponseError(deliverTx.code, abciError),
);
}

return new ApplyStateTransitionResponse();
}
Expand Down
Loading

0 comments on commit dbaa747

Please sign in to comment.