Skip to content

Commit

Permalink
[#14][WIP] manual changes
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofwedrowicz committed Jun 26, 2019
1 parent ca7410c commit fd503b0
Show file tree
Hide file tree
Showing 57 changed files with 978 additions and 1,490 deletions.
1,715 changes: 613 additions & 1,102 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Off-chain oracle server for listening on data requests from Ethereum blockchain",
"main": "index.js",
"scripts": {
"start": "ts-node src/index.ts",
"start": "ts-node -r tsconfig-paths/register src/index.ts",
"build": "tsc",
"test": "mocha -r ts-node/register 'src/**/*.spec.ts'",
"test": "mocha -r ts-node/register -r tsconfig-paths/register 'src/**/*.spec.ts'",
"integration-test": "mocha -r ts-node/register 'src/**/*.integration.ts'",
"ganache": "ganache-cli -d",
"lint": "tslint --config ./tslint.json --project ./tsconfig.json 'src/**/*.ts'"
Expand Down Expand Up @@ -38,13 +38,17 @@
"lodash": "4.17.11",
"mongoose": "5.3.12",
"node-schedule": "1.3.0",
"web3": "1.0.0-beta.36",
"web3": "1.0.0-beta.55",
"websocket": "1.0.26",
"winston": "3.0.0",
"xmldom": "0.1.27",
"xpath": "0.0.27"
},
"devDependencies": {
"@types/dotenv": "^6.1.1",
"@types/express": "^4.17.0",
"@types/lodash": "^4.14.134",
"@types/mongoose": "^5.5.6",
"chai": "4.1.2",
"chai-as-promised": "7.1.1",
"coveralls": "3.0.2",
Expand All @@ -56,6 +60,7 @@
"sinon": "7.1.1",
"sinon-mongoose": "2.2.1",
"ts-node": "^8.3.0",
"tsconfig-paths": "^3.8.0",
"tslint": "^5.17.0",
"typescript": "^3.5.2"
}
Expand Down
8 changes: 4 additions & 4 deletions src/adapter/AxiosUrlDataFetcherAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import UrlDataFetcher from '@core/domain/common/port/UrlDataFetcherPort';
import { HttpError } from '@core/domain/common/utils/error';
import axios from 'axios';
import UrlDataFetcher from '../../domain/common/port/UrlDataFetcherPort';
import { HttpError } from '../../domain/common/utils/error';

class AxiosUrlDataFetcherAdapter extends UrlDataFetcher {
async fetch(url) {
class AxiosUrlDataFetcherAdapter implements UrlDataFetcher {
async fetch(url): Promise<string> {
try {
const response = await axios.get(url);

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/ConsoleLoggerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import LoggerPort from '../domain/common/port/LoggerPort';

class ConsoleLoggerAdapter extends LoggerPort {
class ConsoleLoggerAdapter implements LoggerPort {
info(message) {
console.log(message);
}
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AxiosUrlDataFetcherAdapter from './AxiosUrlDataFetcherAdapter';
import ConsoleLoggerAdapter from './ConsoleLoggerAdapter';
import IdentitySelectorAdapter from '../infrastructure/selectors/IdentitySelectorAdapter';
import JsonSelectorAdapter from '../infrastructure/selectors/JsonSelectorAdapter';
import XmlSelectorAdapter from '../infrastructure/selectors/XmlSelectorAdapter';
import AxiosUrlDataFetcherAdapter from './AxiosUrlDataFetcherAdapter';
import ConsoleLoggerAdapter from './ConsoleLoggerAdapter';

export {
AxiosUrlDataFetcherAdapter,
Expand Down
4 changes: 2 additions & 2 deletions src/application/dataFetcher/AxiosUrlDataFetcherAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import axios from 'axios';
import UrlDataFetcher from '../../domain/common/port/UrlDataFetcherPort';
import { HttpError } from '../../domain/common/utils/error';

class AxiosUrlDataFetcherAdapter extends UrlDataFetcher {
async fetch(url) {
class AxiosUrlDataFetcherAdapter implements UrlDataFetcher {
async fetch(url): Promise<string> {
try {
const response = await axios.get(url);

Expand Down
6 changes: 3 additions & 3 deletions src/application/logger/ConsoleLoggerAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable no-console */
import LoggerPort from '../../domain/common/port/LoggerPort';

class ConsoleLoggerAdapter extends LoggerPort {
info(message) {
class ConsoleLoggerAdapter implements LoggerPort {
info(message): void {
console.log(message);
}

error(message, error) {
error(message, error): void {
console.error(message, error);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/application/selector/identity/IdentitySelectorAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-unused-vars */
import DataSelectorPort from '../../../domain/common/port/DataSelectorPort';
import {DataSelectorPort} from '@core/domain/common/port';

class IdentitySelectorAdapter extends DataSelectorPort {
canHandle(contentType) {
class IdentitySelectorAdapter implements DataSelectorPort {
canHandle(contentType: string): boolean {
return contentType === 'ipfs';
}

select(data, path) {
select(data: string, path: string): string {
if (path) {
throw new Error('Path must be empty for IdentitySelector');
}
Expand Down
2 changes: 1 addition & 1 deletion src/application/selector/json/JsonSelectorAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as jp from 'jsonpath';
import DataSelectorPort from '../../../domain/common/port/DataSelectorPort';
import JsonResultConverter from './JsonResultConverter';

class JsonSelectorAdapter extends DataSelectorPort {
class JsonSelectorAdapter implements DataSelectorPort {
canHandle(contentType) {
return contentType === 'json';
}
Expand Down
18 changes: 9 additions & 9 deletions src/application/selector/xml/XmlSelectorAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { DOMParser, XMLSerializer } from 'xmldom';
import * as xpath from 'xpath';

import XmlResultConverter from './XmlResultConverter';
import DataSelectorPort from '../../../domain/common/port/DataSelectorPort';
import XmlResultConverter from './XmlResultConverter';

class XmlSelectorAdapter implements DataSelectorPort {
private readonly domParser: DOMParser;
private readonly xmlSerializer: XMLSerializer;

class XmlSelectorAdapter extends DataSelectorPort {
domParser: DOMParser;
xmlSerializer: XMLSerializer;
constructor() {
super();
this.domParser = new DOMParser();
this.xmlSerializer = new XMLSerializer();
}

canHandle(contentType) {
canHandle(contentType): boolean {
return ['xml', 'html'].includes(contentType);
}

select(data, path) {
select(data: string, path: string): string {
if (!path) {
return data;
}

const doc = this.domParser.parseFromString(data);
this.__checkXmlValidity(doc);
this.checkXmlValidity(doc);

const selected = xpath.select(path, doc);

return XmlResultConverter.toString(selected);
}

__checkXmlValidity(doc) {
private checkXmlValidity(doc): void {
// throws an error when doc is invalid xml
this.xmlSerializer.serializeToString(doc);
}
Expand Down
6 changes: 6 additions & 0 deletions src/config/configuredChai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);

export const { expect } = chai;
8 changes: 2 additions & 6 deletions src/domain/blockchain/port/BlockchainPort.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
class BlockchainPort {
getBlockNumber() {
throw new Error('Not implemented');
}
export default interface BlockchainPort {
getBlockNumber(): Promise<number>;
}

export default BlockchainPort;
15 changes: 4 additions & 11 deletions src/domain/blockchain/port/OracleGateway.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
/* eslint-disable no-unused-vars */
import Request from '@core/domain/request/Request';

class OracleGateway {
getRequests(fromBlock, toBlock) {
throw new Error('Not implemented');
}

sendResponse(response) {
throw new Error('Not implemented');
}
export default interface OracleGateway {
getRequests(fromBlock, toBlock): Promise<Request[]>;
sendResponse(response): Promise<void>;
}

export default OracleGateway;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, it, beforeEach } from 'mocha';
import { expect } from 'chai';
import { beforeEach, describe, it } from 'mocha';
import {ConsoleLoggerAdapter} from '@core/adapter';
import {OracleGateway} from '../port';
import FetchNewOracleRequestsUseCase from './FetchNewOracleRequestsUseCase';
import { Logger } from '../../common/utils/TestMocks';

describe('FetchNewOracleRequestUseCase', () => {
const oracle = () => ({
Expand All @@ -13,7 +14,7 @@ describe('FetchNewOracleRequestUseCase', () => {

beforeEach(() => {
// given
sut = new FetchNewOracleRequestsUseCase(oracle(), new Logger(), 1);
sut = new FetchNewOracleRequestsUseCase(oracle() as OracleGateway, new ConsoleLoggerAdapter(), 1);
});

it('should fetch new oracle responses', async () => {
Expand All @@ -23,7 +24,6 @@ describe('FetchNewOracleRequestUseCase', () => {
expect(requests).to.be.an.instanceof(Array);
expect(requests).to.have.lengthOf(1);
expect(sut.lastBlock).to.equal(4);
expect(sut.logger.list().length).to.equal(1);
});

it('should return empty array if new block is equal to current one', async () => {
Expand Down
18 changes: 11 additions & 7 deletions src/domain/blockchain/usecase/FetchNewOracleRequestsUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import {OracleGateway} from '@core/domain/blockchain/port';
import LoggerPort from '@core/domain/common/port/LoggerPort';
import Request from '@core/domain/request/Request';

class FetchNewOracleRequestsUseCase {
oracle: any;
logger: any;
lastBlock: number;
constructor(oracle, logger, startBlockNumber) {
this.oracle = oracle;
this.logger = logger;
private lastBlock: number;

constructor(
private readonly oracle: OracleGateway,
private readonly logger: LoggerPort,
startBlockNumber: number,
) {
this.lastBlock = startBlockNumber - 1;
}

async fetchNewRequests(blockNumber) {
async fetchNewRequests(blockNumber): Promise<Request[]> {
if (blockNumber <= this.lastBlock) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect } from '@core/config/configuredChai';
import { describe, it } from 'mocha';
const { expect } = require('chai').use(require('chai-as-promised'));
import SendResponseToOracleUseCase from './SendResponseToOracleUseCase';
import ConsoleLoggerAdapter from '../../../application/logger/ConsoleLoggerAdapter';
import Response from '../../response/Response';
import { Logger } from '../../common/utils/TestMocks';
import {OracleGateway} from '../port';
import SendResponseToOracleUseCase from './SendResponseToOracleUseCase';

describe('SendResponseUseCase', () => {
const oracle = () => ({
Expand All @@ -16,15 +17,15 @@ describe('SendResponseUseCase', () => {
it('should send response back to oracle without throwing error', async () => {
// given
const response = new Response('id');
const sut = new SendResponseToOracleUseCase(oracle(), new Logger());
const sut = new SendResponseToOracleUseCase(oracle() as unknown as OracleGateway, new ConsoleLoggerAdapter());
// when, then
return expect(sut.sendResponse(response)).to.be.fulfilled;
});

it('should throw error when sending response failed', async () => {
// given
const response = new Response('id');
const sut = new SendResponseToOracleUseCase(failingOracle(), new Logger());
const sut = new SendResponseToOracleUseCase(failingOracle() as unknown as OracleGateway, new ConsoleLoggerAdapter());
// when, then
return expect(() => sut.sendResponse(response)).to.be.rejected;
});
Expand Down
15 changes: 8 additions & 7 deletions src/domain/blockchain/usecase/SendResponseToOracleUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {OracleGateway} from '@core/domain/blockchain/port';
import LoggerPort from '@core/domain/common/port/LoggerPort';

class SendResponseToOracleUseCase {
oracle: any;
logger: any;
constructor(oracle, logger) {
this.oracle = oracle;
this.logger = logger;
}
constructor(
private readonly oracle: OracleGateway,
private readonly logger: LoggerPort,
) {}

async sendResponse(response) {
async sendResponse(response): Promise<void> {
await this.oracle.sendResponse(response);
this.logger.info(`Response sent to blockchain [requestId=${response.requestId}]`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/domain/common/DataSelectorFinder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect } from '@core/config/configuredChai';
import { describe, it } from 'mocha';
const { expect } = require('chai').use(require('chai-as-promised'));
import DataSelectorFinder from './DataSelectorFinder';


describe('DataSelectorFinder', () => {
it('should select selector which can handle given contentType', async () => {
// given
Expand Down
15 changes: 3 additions & 12 deletions src/domain/common/port/DataSelectorPort.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
/* eslint-disable no-unused-vars */

class DataSelectorPort {
select(data, path) {
throw new Error('Not implemented');
}

canHandle(contentType) {
throw new Error('Not implemented');
}
export default interface DataSelectorPort {
select(data, path): string;
canHandle(contentType): boolean;
}

export default DataSelectorPort;
15 changes: 3 additions & 12 deletions src/domain/common/port/LoggerPort.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
/* eslint-disable no-unused-vars */

class LoggerPort {
info(message) {
throw new Error('Not implemented');
}

error(message, error) {
throw new Error('Not implemented');
}
export default interface LoggerPort {
info(message): void;
error(message, error): void;
}

export default LoggerPort;
10 changes: 2 additions & 8 deletions src/domain/common/port/UrlDataFetcherPort.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/* eslint-disable no-unused-vars */

class UrlDataFetcherPort {
fetch(url) {
throw new Error('Not implemented');
}
export default interface UrlDataFetcherPort {
fetch(url: string): Promise<string>;
}

export default UrlDataFetcherPort;
Loading

0 comments on commit fd503b0

Please sign in to comment.