|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + */ |
| 16 | +import { expect } from 'chai'; |
| 17 | +import * as http from 'http'; |
| 18 | +import { |
| 19 | + BlockDTO, |
| 20 | + BlockInfoDTO, |
| 21 | + BlockMetaDTO, |
| 22 | + BlockRoutesApi, |
| 23 | + MerklePathItemDTO, |
| 24 | + MerkleProofInfoDTO, |
| 25 | + NetworkTypeEnum, |
| 26 | + PositionEnum, |
| 27 | +} from 'symbol-openapi-typescript-node-client'; |
| 28 | +import { instance, mock, reset, when } from 'ts-mockito'; |
| 29 | +import { DtoMapping } from '../../src/core/utils/DtoMapping'; |
| 30 | +import { BlockHttp } from '../../src/infrastructure/BlockHttp'; |
| 31 | +import { BlockRepository } from '../../src/infrastructure/BlockRepository'; |
| 32 | +import { BlockInfo } from '../../src/model/blockchain/BlockInfo'; |
| 33 | +import { MerklePathItem } from '../../src/model/blockchain/MerklePathItem'; |
| 34 | +import { Transaction } from '../../src/model/transaction/Transaction'; |
| 35 | +import { UInt64 } from '../../src/model/UInt64'; |
| 36 | + |
| 37 | +describe('BlockHttp', () => { |
| 38 | + |
| 39 | + const blockDTO = new BlockDTO(); |
| 40 | + blockDTO.version = 1; |
| 41 | + blockDTO.network = NetworkTypeEnum.NUMBER_152; |
| 42 | + blockDTO.beneficiaryPublicKey = 'a'; |
| 43 | + blockDTO.difficulty = '2'; |
| 44 | + blockDTO.feeMultiplier = 3; |
| 45 | + blockDTO.height = '4'; |
| 46 | + blockDTO.previousBlockHash = '5'; |
| 47 | + blockDTO.type = 6; |
| 48 | + blockDTO.signerPublicKey = '81E5E7AE49998802DABC816EC10158D3A7879702FF29084C2C992CD1289877A7'; |
| 49 | + blockDTO.timestamp = '7'; |
| 50 | + blockDTO.beneficiaryPublicKey = '81E5E7AE49998802DABC816EC10158D3A7879702FF29084C2C992CD1289877A8'; |
| 51 | + |
| 52 | + const blockMetaDTO = new BlockMetaDTO(); |
| 53 | + blockMetaDTO.generationHash = 'abc'; |
| 54 | + blockMetaDTO.hash = 'aHash'; |
| 55 | + blockMetaDTO.numStatements = 10; |
| 56 | + blockMetaDTO.numTransactions = 20; |
| 57 | + blockMetaDTO.totalFee = '30'; |
| 58 | + blockMetaDTO.stateHashSubCacheMerkleRoots = ['a', 'b', 'c']; |
| 59 | + |
| 60 | + const blockInfoDto = new BlockInfoDTO(); |
| 61 | + blockInfoDto.block = blockDTO; |
| 62 | + blockInfoDto.meta = blockMetaDTO; |
| 63 | + |
| 64 | + const url = 'http://someHost'; |
| 65 | + const response: http.IncomingMessage = mock(); |
| 66 | + const blockRoutesApi: BlockRoutesApi = mock(); |
| 67 | + const blockRepository: BlockRepository = DtoMapping.assign(new BlockHttp(url), {blockRoutesApi: instance(blockRoutesApi)}); |
| 68 | + |
| 69 | + const transactionInfoDTO = { |
| 70 | + meta: { |
| 71 | + hash: '671653C94E2254F2A23EFEDB15D67C38332AED1FBD24B063C0A8E675582B6A96', |
| 72 | + height: '18160', |
| 73 | + id: '5A0069D83F17CF0001777E55', |
| 74 | + index: 0, |
| 75 | + merkleComponentHash: '81E5E7AE49998802DABC816EC10158D3A7879702FF29084C2C992CD1289877A7', |
| 76 | + }, |
| 77 | + transaction: { |
| 78 | + deadline: '1000', |
| 79 | + maxFee: '0', |
| 80 | + signature: '939673209A13FF82397578D22CC96EB8516A6760C894D9B7535E3A1E0680' + |
| 81 | + '07B9255CFA9A914C97142A7AE18533E381C846B69D2AE0D60D1DC8A55AD120E2B606', |
| 82 | + signerPublicKey: '7681ED5023141D9CDCF184E5A7B60B7D466739918ED5DA30F7E71EA7B86EFF2D', |
| 83 | + minApprovalDelta: 1, |
| 84 | + minRemovalDelta: 1, |
| 85 | + modifications: [ |
| 86 | + { |
| 87 | + cosignatoryPublicKey: '589B73FBC22063E9AE6FBAC67CB9C6EA865EF556E5' + |
| 88 | + 'FB8B7310D45F77C1250B97', |
| 89 | + modificationAction: 0, |
| 90 | + }, |
| 91 | + ], |
| 92 | + type: 16725, |
| 93 | + version: 1, |
| 94 | + network: 144, |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + before(() => { |
| 99 | + reset(response); |
| 100 | + reset(blockRoutesApi); |
| 101 | + }); |
| 102 | + |
| 103 | + function assertBlockInfo(blockInfo: BlockInfo) { |
| 104 | + expect(blockInfo).to.be.not.null; |
| 105 | + expect(blockInfo.type).to.be.equals(blockInfoDto.block.type); |
| 106 | + expect(blockInfo.previousBlockHash).to.be.equals(blockInfoDto.block.previousBlockHash); |
| 107 | + expect(blockInfo.height.toString()).to.be.equals(blockInfoDto.block.height); |
| 108 | + expect(blockInfo.feeMultiplier).to.be.equals(blockInfoDto.block.feeMultiplier); |
| 109 | + expect(blockInfo.networkType).to.be.equals(blockInfoDto.block.network); |
| 110 | + expect(blockInfo.version).to.be.equals(blockInfoDto.block.version); |
| 111 | + expect(blockInfo.beneficiaryPublicKey!.publicKey).to.be.equals(blockInfoDto.block.beneficiaryPublicKey); |
| 112 | + expect(blockInfo.difficulty.toString()).to.be.equals(blockInfoDto.block.difficulty); |
| 113 | + expect(blockInfo.feeMultiplier).to.be.equals(blockInfoDto.block.feeMultiplier); |
| 114 | + expect(blockInfo.signer!.publicKey).to.be.equals(blockInfoDto.block.signerPublicKey); |
| 115 | + expect(blockInfo.signature).to.be.equals(blockInfoDto.block.signature); |
| 116 | + |
| 117 | + expect(blockInfo.generationHash).to.be.equals(blockInfoDto.meta.generationHash); |
| 118 | + expect(blockInfo.hash).to.be.equals(blockInfoDto.meta.hash); |
| 119 | + expect(blockInfo.numStatements).to.be.equals(blockInfoDto.meta.numStatements); |
| 120 | + expect(blockInfo.numTransactions).to.be.equals(blockInfoDto.meta.numTransactions); |
| 121 | + expect(blockInfo.totalFee.toString()).to.be.equals(blockInfoDto.meta.totalFee); |
| 122 | + } |
| 123 | + |
| 124 | + function assertTransaction(transaction: Transaction) { |
| 125 | + expect(transaction).to.be.not.null; |
| 126 | + expect(transaction.type).to.be.equals(transactionInfoDTO.transaction.type); |
| 127 | + expect(transaction.deadline.toString()).to.be.equals(transactionInfoDTO.transaction.deadline); |
| 128 | + } |
| 129 | + |
| 130 | + it('getBlockInfo', async () => { |
| 131 | + when(blockRoutesApi.getBlockByHeight('1')).thenReturn(Promise.resolve({response, body: blockInfoDto})); |
| 132 | + const blockInfo = await blockRepository.getBlockByHeight(UInt64.fromUint(1)).toPromise(); |
| 133 | + assertBlockInfo(blockInfo); |
| 134 | + }); |
| 135 | + |
| 136 | + it('getBlocksByHeightWithLimit', async () => { |
| 137 | + when(blockRoutesApi.getBlocksByHeightWithLimit('2', 10)).thenReturn(Promise.resolve({ |
| 138 | + response, |
| 139 | + body: [blockInfoDto], |
| 140 | + })); |
| 141 | + const blockInfos = await blockRepository.getBlocksByHeightWithLimit(UInt64.fromUint(2), 10).toPromise(); |
| 142 | + assertBlockInfo(blockInfos[0]); |
| 143 | + }); |
| 144 | + |
| 145 | + it('getBlockTransactions', async () => { |
| 146 | + when(blockRoutesApi.getBlockTransactions('2', undefined, undefined, undefined)).thenReturn(Promise.resolve({ |
| 147 | + response, |
| 148 | + body: [transactionInfoDTO], |
| 149 | + })); |
| 150 | + const transactions = await blockRepository.getBlockTransactions(UInt64.fromUint(2)).toPromise(); |
| 151 | + assertTransaction(transactions[0]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('getMerkleTransaction', async () => { |
| 155 | + const merkleProofInfoDTO = new MerkleProofInfoDTO(); |
| 156 | + const merklePathItemDTO = new MerklePathItemDTO(); |
| 157 | + merklePathItemDTO.hash = 'bbb'; |
| 158 | + merklePathItemDTO.position = PositionEnum.Left; |
| 159 | + merkleProofInfoDTO.merklePath = [merklePathItemDTO]; |
| 160 | + |
| 161 | + when(blockRoutesApi.getMerkleTransaction('2', 'abc')).thenReturn(Promise.resolve({ |
| 162 | + response, |
| 163 | + body: merkleProofInfoDTO, |
| 164 | + })); |
| 165 | + const merkleProofInfo = await blockRepository.getMerkleTransaction(UInt64.fromUint(2), 'abc').toPromise(); |
| 166 | + expect(merkleProofInfo).to.be.not.null; |
| 167 | + expect(merkleProofInfo.merklePath).to.deep.equals([new MerklePathItem(PositionEnum.Left, 'bbb')]); |
| 168 | + }); |
| 169 | + |
| 170 | +}); |
0 commit comments