Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 90 additions & 130 deletions src/infrastructure/AccountHttp.ts

Large diffs are not rendered by default.

159 changes: 68 additions & 91 deletions src/infrastructure/BlockHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

import { ClientResponse } from 'http';
import {from as observableFrom, Observable, throwError} from 'rxjs';
import {catchError, map, mergeMap} from 'rxjs/operators';
import {from as observableFrom, Observable} from 'rxjs';
import {map, mergeMap} from 'rxjs/operators';
import {PublicAccount} from '../model/account/PublicAccount';
import {BlockInfo} from '../model/blockchain/BlockInfo';
import { MerklePathItem } from '../model/blockchain/MerklePathItem';
Expand Down Expand Up @@ -80,33 +79,29 @@ export class BlockHttp extends Http implements BlockRepository {
* @returns Observable<BlockInfo>
*/
public getBlockByHeight(height: number): Observable<BlockInfo> {
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(
map((response: { response: ClientResponse; body: BlockInfoDTO; } ) => {
const blockDTO = response.body;
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
return new BlockInfo(
blockDTO.meta.hash,
blockDTO.meta.generationHash,
new UInt64(blockDTO.meta.totalFee),
blockDTO.meta.numTransactions,
blockDTO.block.signature,
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
networkType,
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
blockDTO.block.type,
new UInt64(blockDTO.block.height),
new UInt64(blockDTO.block.timestamp),
new UInt64(blockDTO.block.difficulty),
blockDTO.block.feeMultiplier,
blockDTO.block.previousBlockHash,
blockDTO.block.blockTransactionsHash,
blockDTO.block.blockReceiptsHash,
blockDTO.block.stateHash,
extractBeneficiary(blockDTO, networkType),
);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(map((blockDTO: BlockInfoDTO) => {
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
return new BlockInfo(
blockDTO.meta.hash,
blockDTO.meta.generationHash,
new UInt64(blockDTO.meta.totalFee),
blockDTO.meta.numTransactions,
blockDTO.block.signature,
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
networkType,
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
blockDTO.block.type,
new UInt64(blockDTO.block.height),
new UInt64(blockDTO.block.timestamp),
new UInt64(blockDTO.block.difficulty),
blockDTO.block.feeMultiplier,
blockDTO.block.previousBlockHash,
blockDTO.block.blockTransactionsHash,
blockDTO.block.blockReceiptsHash,
blockDTO.block.stateHash,
extractBeneficiary(blockDTO, networkType),
);
}));
}

/**
Expand All @@ -122,14 +117,11 @@ export class BlockHttp extends Http implements BlockRepository {
this.queryParams(queryParams).pageSize,
this.queryParams(queryParams).id,
this.queryParams(queryParams).order))
.pipe(map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
const transactionsDTO = response.body;
.pipe(map((transactionsDTO: TransactionInfoDTO[]) => {
return transactionsDTO.map((transactionDTO) => {
return CreateTransactionFromDTO(transactionDTO);
});
}),
catchError((error) => throwError(this.errorHandling(error))),
);
}));
}

/**
Expand All @@ -140,35 +132,31 @@ export class BlockHttp extends Http implements BlockRepository {
*/
public getBlocksByHeightWithLimit(height: number, limit: LimitType = LimitType.N_25): Observable<BlockInfo[]> {
return observableFrom(
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(
map((response: { response: ClientResponse; body: BlockInfoDTO[]; }) => {
const blocksDTO = response.body;
return blocksDTO.map((blockDTO) => {
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
return new BlockInfo(
blockDTO.meta.hash,
blockDTO.meta.generationHash,
new UInt64(blockDTO.meta.totalFee),
blockDTO.meta.numTransactions,
blockDTO.block.signature,
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
networkType,
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
blockDTO.block.type,
new UInt64(blockDTO.block.height),
new UInt64(blockDTO.block.timestamp),
new UInt64(blockDTO.block.difficulty),
blockDTO.block.feeMultiplier,
blockDTO.block.previousBlockHash,
blockDTO.block.blockTransactionsHash,
blockDTO.block.blockReceiptsHash,
blockDTO.block.stateHash,
extractBeneficiary(blockDTO, networkType),
);
});
}),
catchError((error) => throwError(this.errorHandling(error))),
);
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(map((blocksDTO: BlockInfoDTO[]) => {
return blocksDTO.map((blockDTO) => {
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
return new BlockInfo(
blockDTO.meta.hash,
blockDTO.meta.generationHash,
new UInt64(blockDTO.meta.totalFee),
blockDTO.meta.numTransactions,
blockDTO.block.signature,
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
networkType,
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
blockDTO.block.type,
new UInt64(blockDTO.block.height),
new UInt64(blockDTO.block.timestamp),
new UInt64(blockDTO.block.difficulty),
blockDTO.block.feeMultiplier,
blockDTO.block.previousBlockHash,
blockDTO.block.blockTransactionsHash,
blockDTO.block.blockReceiptsHash,
blockDTO.block.stateHash,
extractBeneficiary(blockDTO, networkType),
);
});
}));
}

/**
Expand All @@ -183,18 +171,14 @@ export class BlockHttp extends Http implements BlockRepository {
*/
public getMerkleReceipts(height: number, hash: string): Observable<MerkleProofInfo> {
return observableFrom(
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(
map((response: { response: ClientResponse; body: MerkleProofInfoDTO; } ) => {
const merkleProofReceipt = response.body;
return new MerkleProofInfo(
new MerkleProofInfoPayload(
merkleProofReceipt.payload.merklePath!.map(
(payload) => new MerklePathItem(payload.position, payload.hash))),
merkleProofReceipt.type,
);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(map((merkleProofReceipt: MerkleProofInfoDTO) => {
return new MerkleProofInfo(
new MerkleProofInfoPayload(
merkleProofReceipt.payload.merklePath!.map(
(payload) => new MerklePathItem(payload.position, payload.hash))),
merkleProofReceipt.type,
);
}));
}

/**
Expand All @@ -209,18 +193,13 @@ export class BlockHttp extends Http implements BlockRepository {
*/
public getMerkleTransaction(height: number, hash: string): Observable<MerkleProofInfo> {
return observableFrom(
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(
map((response: { response: ClientResponse; body: MerkleProofInfoDTO; } ) => {
const merkleProofTransaction = response.body;
return new MerkleProofInfo(
new MerkleProofInfoPayload(
merkleProofTransaction.payload.merklePath!.map((payload) =>
new MerklePathItem(payload.position, payload.hash))),
merkleProofTransaction.type,
);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(map((merkleProofTransaction: MerkleProofInfoDTO) => {
return new MerkleProofInfo(
new MerkleProofInfoPayload(
merkleProofTransaction.payload.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash))),
merkleProofTransaction.type,
);
}));
}

/**
Expand All @@ -233,11 +212,9 @@ export class BlockHttp extends Http implements BlockRepository {
return this.getNetworkTypeObservable().pipe(
mergeMap((networkType) => observableFrom(
this.blockRoutesApi.getBlockReceipts(height)).pipe(
map((response: { response: ClientResponse; body: StatementsDTO; }) => {
const receiptDTO = response.body;
map((receiptDTO: StatementsDTO) => {
return CreateStatementFromDTO(receiptDTO, networkType);
}),
catchError((error) => throwError(this.errorHandling(error))),
),
),
);
Expand Down
31 changes: 11 additions & 20 deletions src/infrastructure/ChainHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

import { ClientResponse } from 'http';
import {from as observableFrom, Observable, throwError} from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import {from as observableFrom, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {BlockchainScore} from '../model/blockchain/BlockchainScore';
import {UInt64} from '../model/UInt64';
import { BlockchainScoreDTO,
Expand Down Expand Up @@ -51,29 +50,21 @@ export class ChainHttp extends Http implements ChainRepository {
* @returns Observable<UInt64>
*/
public getBlockchainHeight(): Observable<UInt64> {
return observableFrom(this.chainRoutesApi.getBlockchainHeight()).pipe(
map((response: { response: ClientResponse; body: HeightInfoDTO; } ) => {
const heightDTO = response.body;
return new UInt64(heightDTO.height);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
return observableFrom(this.chainRoutesApi.getBlockchainHeight()).pipe(map((heightDTO: HeightInfoDTO) => {
return new UInt64(heightDTO.height);
}));
}

/**
* Gets current blockchain score
* @returns Observable<BlockchainScore>
*/
public getBlockchainScore(): Observable<BlockchainScore> {
return observableFrom(this.chainRoutesApi.getBlockchainScore()).pipe(
map((response: { response: ClientResponse; body: BlockchainScoreDTO; } ) => {
const blockchainScoreDTO = response.body;
return new BlockchainScore(
new UInt64(blockchainScoreDTO.scoreLow),
new UInt64(blockchainScoreDTO.scoreHigh),
);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
return observableFrom(this.chainRoutesApi.getBlockchainScore()).pipe(map((blockchainScoreDTO: BlockchainScoreDTO) => {
return new BlockchainScore(
new UInt64(blockchainScoreDTO.scoreLow),
new UInt64(blockchainScoreDTO.scoreHigh),
);
}));
}
}
35 changes: 13 additions & 22 deletions src/infrastructure/DiagnosticHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

import { ClientResponse } from 'http';
import {from as observableFrom, Observable, throwError} from 'rxjs';
import {catchError, map} from 'rxjs/operators';
import {from as observableFrom, Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {BlockchainStorageInfo} from '../model/blockchain/BlockchainStorageInfo';
import { ServerInfo } from '../model/diagnostic/ServerInfo';
import { DiagnosticRoutesApi, ServerDTO, StorageInfoDTO } from './api';
Expand Down Expand Up @@ -50,17 +49,13 @@ export class DiagnosticHttp extends Http implements DiagnosticRepository {
*/
public getDiagnosticStorage(): Observable<BlockchainStorageInfo> {
return observableFrom(
this.diagnosticRoutesApi.getDiagnosticStorage()).pipe(
map((response: { response: ClientResponse; body: StorageInfoDTO; } ) => {
const blockchainStorageInfoDTO = response.body;
return new BlockchainStorageInfo(
blockchainStorageInfoDTO.numBlocks,
blockchainStorageInfoDTO.numTransactions,
blockchainStorageInfoDTO.numAccounts,
);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
this.diagnosticRoutesApi.getDiagnosticStorage()).pipe(map((blockchainStorageInfoDTO: StorageInfoDTO) => {
return new BlockchainStorageInfo(
blockchainStorageInfoDTO.numBlocks,
blockchainStorageInfoDTO.numTransactions,
blockchainStorageInfoDTO.numAccounts,
);
}));
}

/**
Expand All @@ -69,13 +64,9 @@ export class DiagnosticHttp extends Http implements DiagnosticRepository {
*/
public getServerInfo(): Observable<ServerInfo> {
return observableFrom(
this.diagnosticRoutesApi.getServerInfo()).pipe(
map((response: { response: ClientResponse; body: ServerDTO; } ) => {
const serverDTO = response.body;
return new ServerInfo(serverDTO.serverInfo.restVersion,
serverDTO.serverInfo.sdkVersion);
}),
catchError((error) => throwError(this.errorHandling(error))),
);
this.diagnosticRoutesApi.getServerInfo()).pipe(map((serverDTO: ServerDTO) => {
return new ServerInfo(serverDTO.serverInfo.restVersion,
serverDTO.serverInfo.sdkVersion);
}));
}
}
11 changes: 0 additions & 11 deletions src/infrastructure/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,4 @@ export abstract class Http {
order: queryParams ? queryParams.order : undefined,
};
}

errorHandling(error: any): Error {
if (error.response && error.response.statusCode && error.response.body) {
const formattedError = {
statusCode: error.response.statusCode,
errorDetails: error.response.body,
};
return new Error(JSON.stringify(formattedError));
}
return new Error(error);
}
}
Loading