Skip to content

Commit 2c8be0b

Browse files
author
Greg S
committed
PR #101: fixed block DTOs with beneficiary field DTO problem
1 parent 221887b commit 2c8be0b

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

src/infrastructure/BlockchainHttp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {UInt64} from '../model/UInt64';
2626
import {BlockchainRepository} from './BlockchainRepository';
2727
import {Http} from './Http';
2828
import {QueryParams} from './QueryParams';
29-
import {CreateTransactionFromDTO} from './transaction/CreateTransactionFromDTO';
29+
import {CreateTransactionFromDTO, extractBeneficiary} from './transaction/CreateTransactionFromDTO';
3030

3131
/**
3232
* Blockchain http repository.
@@ -75,7 +75,7 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
7575
blockDTO.block.blockTransactionsHash,
7676
blockDTO.block.blockReceiptsHash,
7777
blockDTO.block.stateHash,
78-
PublicAccount.createFromPublicKey(blockDTO.block.beneficiaryPublicKey, networkType),
78+
extractBeneficiary(blockDTO, networkType),
7979
);
8080
}));
8181
}
@@ -125,7 +125,7 @@ export class BlockchainHttp extends Http implements BlockchainRepository {
125125
blockDTO.block.blockTransactionsHash,
126126
blockDTO.block.blockReceiptsHash,
127127
blockDTO.block.stateHash,
128-
PublicAccount.createFromPublicKey(blockDTO.block.beneficiaryPublicKey, networkType),
128+
extractBeneficiary(blockDTO, networkType),
129129
);
130130
});
131131
}));

src/infrastructure/Listener.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as WebSocket from 'ws';
2020
import {Address} from '../model/account/Address';
2121
import {PublicAccount} from '../model/account/PublicAccount';
2222
import {BlockInfo} from '../model/blockchain/BlockInfo';
23+
import {NetworkType} from '../model/blockchain/NetworkType';
2324
import {NamespaceId} from '../model/namespace/NamespaceId';
2425
import {AggregateTransaction} from '../model/transaction/AggregateTransaction';
2526
import {AggregateTransactionCosignature} from '../model/transaction/AggregateTransactionCosignature';
@@ -33,7 +34,7 @@ import {Transaction} from '../model/transaction/Transaction';
3334
import {TransactionStatusError} from '../model/transaction/TransactionStatusError';
3435
import {TransferTransaction} from '../model/transaction/TransferTransaction';
3536
import {UInt64} from '../model/UInt64';
36-
import {CreateTransactionFromDTO} from './transaction/CreateTransactionFromDTO';
37+
import {CreateTransactionFromDTO, extractBeneficiary} from './transaction/CreateTransactionFromDTO';
3738

3839
enum ListenerChannelName {
3940
block = 'block',
@@ -140,7 +141,7 @@ export class Listener {
140141
message.block.blockTransactionsHash,
141142
message.block.blockReceiptsHash,
142143
message.block.stateHash,
143-
PublicAccount.createFromPublicKey(message.block.beneficiaryPublicKey, networkType),
144+
extractBeneficiary(message, networkType), // passing `message` as `blockDTO`
144145
),
145146
});
146147
} else if (message.status) {

src/infrastructure/transaction/CreateTransactionFromDTO.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ const CreateStandaloneTransactionFromDTO = (transactionDTO, transactionInfo): Tr
322322
throw new Error('Unimplemented transaction with type ' + transactionDTO.type);
323323
};
324324

325-
const extractNetworkType = (version: number): NetworkType => {
325+
export const extractNetworkType = (version: number): NetworkType => {
326326
const networkType = parseInt(version.toString(16).substr(0, 2), 16);
327327
if (networkType === NetworkType.MAIN_NET) {
328328
return NetworkType.MAIN_NET;
@@ -336,7 +336,7 @@ const extractNetworkType = (version: number): NetworkType => {
336336
throw new Error('Unimplemented network type');
337337
};
338338

339-
const extractTransactionVersion = (version: number): number => {
339+
export const extractTransactionVersion = (version: number): number => {
340340
return parseInt(version.toString(16).substr(2, 2), 16);
341341
};
342342

@@ -349,7 +349,7 @@ const extractTransactionVersion = (version: number): number => {
349349
* @param recipient {string} Encoded hexadecimal recipient notation
350350
* @return {Address | NamespaceId}
351351
*/
352-
const extractRecipient = (recipient: string): Address | NamespaceId => {
352+
export const extractRecipient = (recipient: string): Address | NamespaceId => {
353353
// If bit 0 of byte 0 is not set (like in 0x90), then it is a regular address.
354354
// Else (e.g. 0x91) it represents a namespace id which starts at byte 1.
355355
const bit0 = convert.hexToUint8(recipient.substr(1, 2))[0];
@@ -374,7 +374,7 @@ const extractRecipient = (recipient: string): Address | NamespaceId => {
374374
* @param mosaics {Array | undefined} The DTO array of mosaics (with UInt64 Id notation)
375375
* @return {Mosaic[]}
376376
*/
377-
const extractMosaics = (mosaics: any): Mosaic[] => {
377+
export const extractMosaics = (mosaics: any): Mosaic[] => {
378378

379379
if (mosaics === undefined) {
380380
return [];
@@ -396,3 +396,44 @@ const extractMosaics = (mosaics: any): Mosaic[] => {
396396
return new Mosaic(new MosaicId(mosaicDTO.id), new UInt64(mosaicDTO.amount));
397397
});
398398
};
399+
400+
/**
401+
* Extract beneficiary public key from DTO.
402+
*
403+
* @todo Upgrade of catapult-rest WITH catapult-service-bootstrap versioning.
404+
*
405+
* With `cow` upgrade (nemtech/catapult-server@0.3.0.2), `catapult-rest` block DTO
406+
* was updated and latest catapult-service-bootstrap uses the wrong block DTO.
407+
* This will be fixed with next catapult-server upgrade to `dragon`.
408+
*
409+
* :warning It is currently not possible to read the block's beneficiary public key
410+
* except when working with a local instance of `catapult-rest`.
411+
*
412+
* @param beneficiary {string | undefined} The beneficiary public key if set
413+
* @return {Mosaic[]}
414+
*/
415+
export const extractBeneficiary = (
416+
blockDTO: any,
417+
networkType: NetworkType
418+
): PublicAccount | undefined => {
419+
420+
let dtoPublicAccount: PublicAccount | undefined;
421+
let dtoFieldValue: string | undefined;
422+
if (blockDTO.beneficiaryPublicKey) {
423+
dtoFieldValue = blockDTO.beneficiaryPublicKey;
424+
} else if (blockDTO.beneficiary) {
425+
dtoFieldValue = blockDTO.beneficiary;
426+
}
427+
428+
if (! dtoFieldValue) {
429+
return undefined;
430+
}
431+
432+
try {
433+
// @FIX with latest catapult-service-bootstrap version, catapult-rest still returns
434+
// a `string` formatted copy of the public *when it is set at all*.
435+
dtoPublicAccount = PublicAccount.createFromPublicKey(dtoFieldValue, networkType);
436+
} catch (e) { dtoPublicAccount =  undefined; }
437+
438+
return dtoPublicAccount;
439+
};

src/model/blockchain/BlockInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class BlockInfo {
117117
/**
118118
* The beneficiary public key.
119119
*/
120-
public readonly beneficiaryPublicKey: PublicAccount,) {
120+
public readonly beneficiaryPublicKey?: PublicAccount | undefined) {
121121

122122
}
123123
}

test/model/blockchain/BlockInfo.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('BlockInfo', () => {
8787
expect(blockInfo.blockTransactionsHash).to.be.equal(blockDTO.block.blockTransactionsHash);
8888
expect(blockInfo.blockReceiptsHash).to.be.equal(blockDTO.block.blockReceiptsHash);
8989
expect(blockInfo.stateHash).to.be.equal(blockDTO.block.stateHash);
90-
expect(blockInfo.beneficiaryPublicKey.publicKey).to.be.equal(blockDTO.block.beneficiaryPublicKey);
90+
expect((blockInfo.beneficiaryPublicKey as PublicAccount).publicKey).to.be.equal(blockDTO.block.beneficiaryPublicKey);
9191

9292
});
9393
});

0 commit comments

Comments
 (0)