Skip to content

Commit 5567fa1

Browse files
committed
Added #147 dragon features and DTOs
1 parent 552afbc commit 5567fa1

File tree

14 files changed

+507
-126
lines changed

14 files changed

+507
-126
lines changed
Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/*
2-
* Copyright 2018 NEM
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
1+
// /*
2+
// * Copyright 2018 NEM
3+
// *
4+
// * Licensed under the Apache License, Version 2.0 (the "License");
5+
// * you may not use this file except in compliance with the License.
6+
// * You may obtain a copy of the License at
7+
// *
8+
// * http://www.apache.org/licenses/LICENSE-2.0
9+
// *
10+
// * Unless required by applicable law or agreed to in writing, software
11+
// * distributed under the License is distributed on an "AS IS" BASIS,
12+
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// * See the License for the specific language governing permissions and
14+
// * limitations under the License.
15+
// */
1616

1717
import {expect} from 'chai';
1818
import {BlockchainHttp} from '../../src/infrastructure/BlockchainHttp';
@@ -43,29 +43,29 @@ describe('BlockchainHttp', () => {
4343
});
4444
});
4545

46-
describe('getBlockTransactions', () => {
47-
let nextId: string;
48-
let firstId: string;
46+
// describe('getBlockTransactions', () => {
47+
// let nextId: string;
48+
// let firstId: string;
4949

50-
it('should return block transactions data given height', (done) => {
51-
blockchainHttp.getBlockTransactions(1)
52-
.subscribe((transactions) => {
53-
nextId = transactions[0].transactionInfo!.id;
54-
firstId = transactions[1].transactionInfo!.id;
55-
expect(transactions.length).to.be.greaterThan(0);
56-
done();
57-
});
58-
});
50+
// it('should return block transactions data given height', (done) => {
51+
// blockchainHttp.getBlockTransactions(1)
52+
// .subscribe((transactions) => {
53+
// nextId = transactions[0].transactionInfo!.id;
54+
// firstId = transactions[1].transactionInfo!.id;
55+
// expect(transactions.length).to.be.greaterThan(0);
56+
// done();
57+
// });
58+
// });
5959

60-
it('should return block transactions data given height with paginated transactionId', (done) => {
61-
blockchainHttp.getBlockTransactions(1, new QueryParams(10, nextId))
62-
.subscribe((transactions) => {
63-
expect(transactions[0].transactionInfo!.id).to.be.equal(firstId);
64-
expect(transactions.length).to.be.greaterThan(0);
65-
done();
66-
});
67-
});
68-
});
60+
// it('should return block transactions data given height with paginated transactionId', (done) => {
61+
// blockchainHttp.getBlockTransactions(1, new QueryParams(10, nextId))
62+
// .subscribe((transactions) => {
63+
// expect(transactions[0].transactionInfo!.id).to.be.equal(firstId);
64+
// expect(transactions.length).to.be.greaterThan(0);
65+
// done();
66+
// });
67+
// });
68+
// });
6969

7070
describe('getBlocksByHeightWithLimit', () => {
7171
it('should return block info given height and limit', (done) => {
@@ -77,37 +77,37 @@ describe('BlockchainHttp', () => {
7777
});
7878
});
7979

80-
describe('getBlockchainHeight', () => {
81-
it('should return blockchain height', (done) => {
82-
blockchainHttp.getBlockchainHeight()
83-
.subscribe((height) => {
84-
expect(height.lower).to.be.greaterThan(0);
85-
done();
86-
});
87-
});
88-
});
80+
// describe('getBlockchainHeight', () => {
81+
// it('should return blockchain height', (done) => {
82+
// blockchainHttp.getBlockchainHeight()
83+
// .subscribe((height) => {
84+
// expect(height.lower).to.be.greaterThan(0);
85+
// done();
86+
// });
87+
// });
88+
// });
8989

90-
describe('getBlockchainScore', () => {
91-
it('should return blockchain score', (done) => {
92-
blockchainHttp.getBlockchainScore()
93-
.subscribe((blockchainScore) => {
94-
expect(blockchainScore.scoreLow).to.not.be.equal(undefined);
95-
expect(blockchainScore.scoreHigh.lower).to.be.equal(0);
96-
expect(blockchainScore.scoreHigh.higher).to.be.equal(0);
97-
done();
98-
});
99-
});
100-
});
90+
// describe('getBlockchainScore', () => {
91+
// it('should return blockchain score', (done) => {
92+
// blockchainHttp.getBlockchainScore()
93+
// .subscribe((blockchainScore) => {
94+
// expect(blockchainScore.scoreLow).to.not.be.equal(undefined);
95+
// expect(blockchainScore.scoreHigh.lower).to.be.equal(0);
96+
// expect(blockchainScore.scoreHigh.higher).to.be.equal(0);
97+
// done();
98+
// });
99+
// });
100+
// });
101101

102-
describe('getDiagnosticStorage', () => {
103-
it('should return blockchain diagnostic storage', (done) => {
104-
blockchainHttp.getDiagnosticStorage()
105-
.subscribe((blockchainStorageInfo) => {
106-
expect(blockchainStorageInfo.numBlocks).to.be.greaterThan(0);
107-
expect(blockchainStorageInfo.numTransactions).to.be.greaterThan(0);
108-
expect(blockchainStorageInfo.numAccounts).to.be.greaterThan(0);
109-
done();
110-
});
111-
});
112-
});
113-
});
102+
// describe('getDiagnosticStorage', () => {
103+
// it('should return blockchain diagnostic storage', (done) => {
104+
// blockchainHttp.getDiagnosticStorage()
105+
// .subscribe((blockchainStorageInfo) => {
106+
// expect(blockchainStorageInfo.numBlocks).to.be.greaterThan(0);
107+
// expect(blockchainStorageInfo.numTransactions).to.be.greaterThan(0);
108+
// expect(blockchainStorageInfo.numAccounts).to.be.greaterThan(0);
109+
// done();
110+
// });
111+
// });
112+
// });
113+
// });

src/infrastructure/BlockchainHttp.ts renamed to src/infrastructure/BlockHttp.ts

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {BlockchainRoutesApi} from 'nem2-library';
17+
import {BlockRoutesApi} from 'nem2-library';
1818
import {from as observableFrom, Observable} from 'rxjs';
1919
import {map} from 'rxjs/operators';
2020
import {PublicAccount} from '../model/account/PublicAccount';
21-
import {BlockchainScore} from '../model/blockchain/BlockchainScore';
22-
import {BlockchainStorageInfo} from '../model/blockchain/BlockchainStorageInfo';
2321
import {BlockInfo} from '../model/blockchain/BlockInfo';
22+
import { MerklePathItem } from '../model/blockchain/MerklePathItem';
23+
import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
24+
import { MerkleProofInfoPayload } from '../model/blockchain/MerkleProofInfoPayload';
2425
import {Transaction} from '../model/transaction/Transaction';
2526
import {UInt64} from '../model/UInt64';
26-
import {BlockchainRepository} from './BlockchainRepository';
27+
import {BlockRepository} from './BlockRepository';
2728
import {Http} from './Http';
2829
import {QueryParams} from './QueryParams';
2930
import {CreateTransactionFromDTO, extractBeneficiary} from './transaction/CreateTransactionFromDTO';
@@ -33,20 +34,20 @@ import {CreateTransactionFromDTO, extractBeneficiary} from './transaction/Create
3334
*
3435
* @since 1.0
3536
*/
36-
export class BlockchainHttp extends Http implements BlockchainRepository {
37+
export class BlockHttp extends Http implements BlockRepository {
3738
/**
3839
* @internal
39-
* Nem2 Library blockchain routes api
40+
* Nem2 Library block routes api
4041
*/
41-
private blockchainRoutesApi: BlockchainRoutesApi;
42+
private blockRoutesApi: BlockRoutesApi;
4243

4344
/**
4445
* Constructor
4546
* @param url
4647
*/
4748
constructor(url: string) {
4849
super(url);
49-
this.blockchainRoutesApi = new BlockchainRoutesApi(this.apiClient);
50+
this.blockRoutesApi = new BlockRoutesApi(this.apiClient);
5051
}
5152

5253
/**
@@ -55,7 +56,7 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
5556
* @returns Observable<BlockInfo>
5657
*/
5758
public getBlockByHeight(height: number): Observable<BlockInfo> {
58-
return observableFrom(this.blockchainRoutesApi.getBlockByHeight(height)).pipe(map((blockDTO) => {
59+
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(map((blockDTO) => {
5960
const networkType = parseInt(blockDTO.block.version.toString(16).substr(0, 2), 16);
6061
return new BlockInfo(
6162
blockDTO.meta.hash,
@@ -89,7 +90,7 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
8990
public getBlockTransactions(height: number,
9091
queryParams?: QueryParams): Observable<Transaction[]> {
9192
return observableFrom(
92-
this.blockchainRoutesApi.getBlockTransactions(height, queryParams != null ? queryParams : {})).pipe(map((transactionsDTO) => {
93+
this.blockRoutesApi.getBlockTransactions(height, queryParams != null ? queryParams : {})).pipe(map((transactionsDTO) => {
9394
return transactionsDTO.map((transactionDTO) => {
9495
return CreateTransactionFromDTO(transactionDTO);
9596
});
@@ -104,7 +105,7 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
104105
*/
105106
public getBlocksByHeightWithLimit(height: number, limit: number = 1): Observable<BlockInfo[]> {
106107
return observableFrom(
107-
this.blockchainRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(map((blocksDTO) => {
108+
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(map((blocksDTO) => {
108109
return blocksDTO.map((blockDTO) => {
109110
const networkType = parseInt(blockDTO.block.version.toString(16).substr(0, 2), 16);
110111
return new BlockInfo(
@@ -132,40 +133,44 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
132133
}
133134

134135
/**
135-
* Gets current blockchain height
136-
* @returns Observable<UInt64>
136+
* Get the merkle path for a given a receipt statement hash and block
137+
* Returns the merkle path for a [receipt statement or resolution](https://nemtech.github.io/concepts/receipt.html)
138+
* linked to a block. The path is the complementary data needed to calculate the merkle root.
139+
* A client can compare if the calculated root equals the one recorded in the block header,
140+
* verifying that the receipt was linked with the block.
141+
* @param height The height of the block.
142+
* @param hash The hash of the receipt statement or resolution.
143+
* @return Observable<MerkleProofInfo>
137144
*/
138-
public getBlockchainHeight(): Observable<UInt64> {
139-
return observableFrom(this.blockchainRoutesApi.getBlockchainHeight()).pipe(map((heightDTO) => {
140-
return new UInt64(heightDTO.height);
141-
}));
142-
}
143-
144-
/**
145-
* Gets current blockchain score
146-
* @returns Observable<BlockchainScore>
147-
*/
148-
public getBlockchainScore(): Observable<BlockchainScore> {
149-
return observableFrom(this.blockchainRoutesApi.getBlockchainScore()).pipe(map((blockchainScoreDTO) => {
150-
return new BlockchainScore(
151-
new UInt64(blockchainScoreDTO.scoreLow),
152-
new UInt64(blockchainScoreDTO.scoreHigh),
153-
);
145+
public getMerkleReceipts(height: number, hash: string): Observable<MerkleProofInfo> {
146+
return observableFrom(
147+
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(map((merkleProofReceipt) => {
148+
return new MerkleProofInfo(
149+
new MerkleProofInfoPayload(
150+
merkleProofReceipt.payload.merklePath.map((payload) => new MerklePathItem(payload.position, payload.hash))),
151+
merkleProofReceipt.type,
152+
);
154153
}));
155154
}
156155

157156
/**
158-
* Gets blockchain storage info.
159-
* @returns Observable<BlockchainStorageInfo>
157+
* Get the merkle path for a given a transaction and block
158+
* Returns the merkle path for a [transaction](https://nemtech.github.io/concepts/transaction.html)
159+
* included in a block. The path is the complementary data needed to calculate the merkle root.
160+
* A client can compare if the calculated root equals the one recorded in the block header,
161+
* verifying that the transaction was included in the block.
162+
* @param height The height of the block.
163+
* @param hash The hash of the transaction.
164+
* @return Observable<MerkleProofInfo>
160165
*/
161-
public getDiagnosticStorage(): Observable<BlockchainStorageInfo> {
166+
public getMerkleTransaction(height: number, hash: string): Observable<MerkleProofInfo> {
162167
return observableFrom(
163-
this.blockchainRoutesApi.getDiagnosticStorage()).pipe(map((blockchainStorageInfoDTO) => {
164-
return new BlockchainStorageInfo(
165-
blockchainStorageInfoDTO.numBlocks,
166-
blockchainStorageInfoDTO.numTransactions,
167-
blockchainStorageInfoDTO.numAccounts,
168-
);
168+
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(map((merkleProofReceipt) => {
169+
return new MerkleProofInfo(
170+
new MerkleProofInfoPayload(
171+
merkleProofReceipt.payload.merklePath.map((payload) => new MerklePathItem(payload.position, payload.hash))),
172+
merkleProofReceipt.type,
173+
);
169174
}));
170175
}
171176
}

src/infrastructure/BlockchainRepository.ts renamed to src/infrastructure/BlockRepository.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@
1515
*/
1616

1717
import {Observable} from 'rxjs';
18-
import {BlockchainScore} from '../model/blockchain/BlockchainScore';
19-
import {BlockchainStorageInfo} from '../model/blockchain/BlockchainStorageInfo';
2018
import {BlockInfo} from '../model/blockchain/BlockInfo';
19+
import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
2120
import {Transaction} from '../model/transaction/Transaction';
22-
import {UInt64} from '../model/UInt64';
2321
import {QueryParams} from './QueryParams';
2422

2523
/**
2624
* Blockchain interface repository.
2725
*
2826
* @since 1.0
2927
*/
30-
export interface BlockchainRepository {
28+
export interface BlockRepository {
3129

3230
/**
3331
* Gets a BlockInfo for a given block height
@@ -51,23 +49,33 @@ export interface BlockchainRepository {
5149
* @param limit - Number of blocks returned
5250
* @returns Observable<BlockInfo[]>
5351
*/
52+
5453
getBlocksByHeightWithLimit(height: number, limit: number): Observable<BlockInfo[]>;
5554

5655
/**
57-
* Gets current blockchain height
58-
* @returns Observable<UInt64>
56+
* Get the merkle path for a given a receipt statement hash and block
57+
* Returns the merkle path for a [receipt statement or resolution](https://nemtech.github.io/concepts/receipt.html)
58+
* linked to a block. The path is the complementary data needed to calculate the merkle root.
59+
* A client can compare if the calculated root equals the one recorded in the block header,
60+
* verifying that the receipt was linked with the block.
61+
* @param height The height of the block.
62+
* @param hash The hash of the receipt statement or resolution.
63+
* @return Observable<MerkleProofInfo>
5964
*/
60-
getBlockchainHeight(): Observable<UInt64>;
65+
getMerkleReceipts(height: number, hash: string): Observable<MerkleProofInfo>;
6166

6267
/**
63-
* Gets current blockchain score
64-
* @returns Observable<BlockchainScore>
68+
* Get the merkle path for a given a transaction and block
69+
* Returns the merkle path for a [transaction](https://nemtech.github.io/concepts/transaction.html)
70+
* included in a block. The path is the complementary data needed to calculate the merkle root.
71+
* A client can compare if the calculated root equals the one recorded in the block header,
72+
* verifying that the transaction was included in the block.
73+
* @param height The height of the block.
74+
* @param hash The hash of the transaction.
75+
* @return Observable<MerkleProofInfo>
6576
*/
66-
getBlockchainScore(): Observable<BlockchainScore>;
77+
getMerkleTransaction(height: number, hash: string): Observable<MerkleProofInfo>;
6778

68-
/**
69-
* Gets blockchain storage info.
70-
* @returns Observable<BlockchainStorageInfo>
71-
*/
72-
getDiagnosticStorage(): Observable<BlockchainStorageInfo>;
79+
// TODO:
80+
// getBlockReceipts(height: number): Observable<any>;
7381
}

0 commit comments

Comments
 (0)