Skip to content

Commit 7e784d9

Browse files
authored
Fixed #760 (#761)
- Removed NamespaceName api call - filter transaction by unrsolved address
1 parent da4f378 commit 7e784d9

25 files changed

+178
-270
lines changed

src/infrastructure/Listener.ts

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

17-
import { Observable, of, OperatorFunction, Subject } from 'rxjs';
18-
import { catchError, filter, flatMap, map, mergeMap, share, switchMap } from 'rxjs/operators';
17+
import { Observable, of, Subject } from 'rxjs';
18+
import { catchError, filter, map, mergeMap, share, switchMap } from 'rxjs/operators';
1919
import { BlockInfoDTO } from 'symbol-openapi-typescript-fetch-client';
2020
import * as WebSocket from 'ws';
2121
import { UnresolvedAddress } from '../model';
2222
import { Address } from '../model/account/Address';
2323
import { PublicAccount } from '../model/account/PublicAccount';
2424
import { FinalizedBlock } from '../model/blockchain/FinalizedBlock';
2525
import { NewBlock } from '../model/blockchain/NewBlock';
26-
import { NamespaceName } from '../model/namespace/NamespaceName';
2726
import { AggregateTransaction } from '../model/transaction/AggregateTransaction';
2827
import { CosignatureSignedTransaction } from '../model/transaction/CosignatureSignedTransaction';
2928
import { Deadline } from '../model/transaction/Deadline';
@@ -335,24 +334,25 @@ export class Listener implements IListener {
335334
transactionHash?: string,
336335
subscribeMultisig = false,
337336
): Observable<T> {
338-
return this.getResolvedAddress(unresolvedAddress).pipe(
339-
mergeMap((address: Address) => {
340-
return this.subscribeWithMultig(address, channel, subscribeMultisig).pipe(
341-
switchMap((subscribers) => {
342-
return this.messageSubject.asObservable().pipe(
343-
filter((listenerMessage) => listenerMessage.channelName === channel),
344-
filter((listenerMessage) => listenerMessage.message instanceof Transaction),
345-
switchMap((_) => {
346-
const transactionObservable = of(_.message as T).pipe(
347-
filter((transaction) => this.filterHash(transaction, transactionHash)),
348-
);
349-
if (subscribers.includes(_.channelParam.toUpperCase())) {
350-
return transactionObservable;
351-
} else {
352-
return transactionObservable.pipe(this.filterByNotifyAccount(address));
353-
}
354-
}),
337+
return this.subscribeWithMultig(unresolvedAddress, channel, subscribeMultisig).pipe(
338+
switchMap((subscribers) => {
339+
return this.messageSubject.asObservable().pipe(
340+
filter((listenerMessage) => listenerMessage.channelName === channel),
341+
filter((listenerMessage) => listenerMessage.message instanceof Transaction),
342+
switchMap((_) => {
343+
const transactionObservable = of(_.message as T).pipe(
344+
filter((transaction) => this.filterHash(transaction, transactionHash)),
355345
);
346+
if (subscribers.includes(_.channelParam.toUpperCase())) {
347+
return transactionObservable;
348+
} else {
349+
return transactionObservable.pipe(
350+
filter(
351+
(transaction) =>
352+
transaction.isSigned(unresolvedAddress) || transaction.shouldNotifyAccount(unresolvedAddress),
353+
),
354+
);
355+
}
356356
}),
357357
);
358358
}),
@@ -414,18 +414,14 @@ export class Listener implements IListener {
414414
transactionHash: string | undefined,
415415
subscribeMultisig = false,
416416
): Observable<string> {
417-
return this.getResolvedAddress(unresolvedAddress).pipe(
418-
mergeMap((address: Address) => {
419-
return this.subscribeWithMultig(address, channel, subscribeMultisig).pipe(
420-
switchMap((subscribers) => {
421-
return this.messageSubject.asObservable().pipe(
422-
filter((_) => _.channelName === channel),
423-
filter((_) => typeof _.message === 'string'),
424-
filter((_) => subscribers.includes(_.channelParam.toUpperCase())),
425-
map((_) => _.message as string),
426-
filter((_) => !transactionHash || _.toUpperCase() == transactionHash.toUpperCase()),
427-
);
428-
}),
417+
return this.subscribeWithMultig(unresolvedAddress, channel, subscribeMultisig).pipe(
418+
switchMap((subscribers) => {
419+
return this.messageSubject.asObservable().pipe(
420+
filter((_) => _.channelName === channel),
421+
filter((_) => typeof _.message === 'string'),
422+
filter((_) => subscribers.includes(_.channelParam.toUpperCase())),
423+
map((_) => _.message as string),
424+
filter((_) => !transactionHash || _.toUpperCase() == transactionHash.toUpperCase()),
429425
);
430426
}),
431427
);
@@ -469,39 +465,6 @@ export class Listener implements IListener {
469465
}
470466
}
471467

472-
/**
473-
* It filters a transaction by address using the aliases.
474-
*
475-
* This method delegates the rest loading as much as possible. It tries to filter by signer first.
476-
*
477-
* Note: this filter performs one extra rest call and it should be down in the pipeline.
478-
*
479-
* @param address the address.
480-
* @return an observable filter.
481-
*/
482-
private filterByNotifyAccount<T extends Transaction>(address: Address): OperatorFunction<T, T> {
483-
return (transactionObservable): Observable<T> => {
484-
return transactionObservable.pipe(
485-
flatMap((transaction) => {
486-
if (transaction.isSigned(address)) {
487-
return of(transaction);
488-
}
489-
const namespaceIdsObservable = this.namespaceRepository.getAccountsNames([address]).pipe(
490-
map((names) => {
491-
return ([] as NamespaceName[])
492-
.concat(...Array.from(names.map((accountName) => accountName.names)))
493-
.map((name) => name.namespaceId);
494-
}),
495-
);
496-
return namespaceIdsObservable.pipe(
497-
filter((namespaceIds) => transaction.shouldNotifyAccount(address, namespaceIds)),
498-
map(() => transaction),
499-
);
500-
}),
501-
);
502-
};
503-
}
504-
505468
/**
506469
* Returns an observable stream of {@link CosignatureSignedTransaction} for specific address.
507470
* Each time a cosigner signs a transaction the address initialized,
@@ -512,17 +475,13 @@ export class Listener implements IListener {
512475
* @return an observable stream of {@link CosignatureSignedTransaction}
513476
*/
514477
public cosignatureAdded(unresolvedAddress: UnresolvedAddress, subscribeMultisig = false): Observable<CosignatureSignedTransaction> {
515-
return this.getResolvedAddress(unresolvedAddress).pipe(
516-
mergeMap((address: Address) => {
517-
return this.subscribeWithMultig(address, ListenerChannelName.cosignature, subscribeMultisig).pipe(
518-
switchMap((subscribers) => {
519-
return this.messageSubject.asObservable().pipe(
520-
filter((_) => _.channelName.toUpperCase() === ListenerChannelName.cosignature.toUpperCase()),
521-
filter((_) => _.message instanceof CosignatureSignedTransaction),
522-
filter((_) => subscribers.includes(_.channelParam.toUpperCase())),
523-
map((_) => _.message as CosignatureSignedTransaction),
524-
);
525-
}),
478+
return this.subscribeWithMultig(unresolvedAddress, ListenerChannelName.cosignature, subscribeMultisig).pipe(
479+
switchMap((subscribers) => {
480+
return this.messageSubject.asObservable().pipe(
481+
filter((_) => _.channelName.toUpperCase() === ListenerChannelName.cosignature.toUpperCase()),
482+
filter((_) => _.message instanceof CosignatureSignedTransaction),
483+
filter((_) => subscribers.includes(_.channelParam.toUpperCase())),
484+
map((_) => _.message as CosignatureSignedTransaction),
526485
);
527486
}),
528487
);
@@ -604,22 +563,26 @@ export class Listener implements IListener {
604563
* @param multisig subscribe multisig account
605564
* @returns {string[]}
606565
*/
607-
private subscribeWithMultig(cosigner: Address, channel: ListenerChannelName, multisig = false): Observable<string[]> {
566+
private subscribeWithMultig(cosigner: UnresolvedAddress, channel: ListenerChannelName, multisig = false): Observable<string[]> {
608567
if (!multisig) {
609568
this.subscribeTo(`${channel.toString()}/${cosigner.plain()}`);
610569
return of([cosigner.plain()]);
611570
}
612-
return this.multisigRepository!.getMultisigAccountInfo(cosigner).pipe(
613-
map((multisigInfo) => {
614-
const subscribers = [cosigner].concat(multisigInfo.multisigAddresses);
615-
subscribers.forEach((m) => {
616-
this.subscribeTo(`${channel.toString()}/${m.plain()}`);
617-
});
618-
return subscribers.map((m) => m.plain());
619-
}),
620-
catchError(() => {
621-
this.subscribeTo(`${channel.toString()}/${cosigner.plain()}`);
622-
return of([cosigner.plain()]);
571+
return this.getResolvedAddress(cosigner).pipe(
572+
mergeMap((address: Address) => {
573+
return this.multisigRepository!.getMultisigAccountInfo(address).pipe(
574+
map((multisigInfo) => {
575+
const subscribers = [cosigner].concat(multisigInfo.multisigAddresses);
576+
subscribers.forEach((m) => {
577+
this.subscribeTo(`${channel.toString()}/${m.plain()}`);
578+
});
579+
return subscribers.map((m) => m.plain());
580+
}),
581+
catchError(() => {
582+
this.subscribeTo(`${channel.toString()}/${cosigner.plain()}`);
583+
return of([cosigner.plain()]);
584+
}),
585+
);
623586
}),
624587
);
625588
}

src/model/namespace/NamespaceId.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,12 @@ export class NamespaceId {
106106
public encodeUnresolvedAddress(networkType: NetworkType): Uint8Array {
107107
return RawAddress.aliasToRecipient(Convert.hexToUint8(this.toHex()), networkType);
108108
}
109+
110+
/**
111+
* Get string value of id
112+
* @returns {string}
113+
*/
114+
public plain(): string {
115+
return this.toHex();
116+
}
109117
}

src/model/transaction/AccountAddressRestrictionTransaction.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import {
2727
} from 'catbuffer-typescript';
2828
import { Convert } from '../../core/format';
2929
import { DtoMapping, UnresolvedMapping } from '../../core/utils';
30-
import { Address, PublicAccount, UnresolvedAddress } from '../account';
31-
import { NamespaceId } from '../namespace';
30+
import { PublicAccount, UnresolvedAddress } from '../account';
3231
import { NetworkType } from '../network';
3332
import { Statement } from '../receipt';
3433
import { AddressRestrictionFlag } from '../restriction';
@@ -201,14 +200,13 @@ export class AccountAddressRestrictionTransaction extends Transaction {
201200
* @internal
202201
* Check a given address should be notified in websocket channels
203202
* @param address address to be notified
204-
* @param alias address alias (names)
205203
* @returns {boolean}
206204
*/
207-
public shouldNotifyAccount(address: Address, alias: NamespaceId[]): boolean {
205+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
208206
return (
209207
super.isSigned(address) ||
210-
this.restrictionAdditions.find((_) => _.equals(address) || alias.find((a) => _.equals(a)) !== undefined) !== undefined ||
211-
this.restrictionDeletions.find((_) => _.equals(address) || alias.find((a) => _.equals(a)) !== undefined) !== undefined
208+
this.restrictionAdditions.find((_) => _.equals(address)) !== undefined ||
209+
this.restrictionDeletions.find((_) => _.equals(address)) !== undefined
212210
);
213211
}
214212
}

src/model/transaction/AccountKeyLinkTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
TransactionBuilder,
2525
} from 'catbuffer-typescript';
2626
import { Convert } from '../../core/format';
27-
import { Address, PublicAccount } from '../account';
27+
import { Address, PublicAccount, UnresolvedAddress } from '../account';
2828
import { NetworkType } from '../network';
2929
import { UInt64 } from '../UInt64';
3030
import { Deadline } from './Deadline';
@@ -178,7 +178,7 @@ export class AccountKeyLinkTransaction extends Transaction {
178178
* @param address address to be notified
179179
* @returns {boolean}
180180
*/
181-
public shouldNotifyAccount(address: Address): boolean {
181+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
182182
return super.isSigned(address) || Address.createFromPublicKey(this.linkedPublicKey, this.networkType).equals(address);
183183
}
184184
}

src/model/transaction/AccountMetadataTransaction.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ import {
2525
} from 'catbuffer-typescript';
2626
import { Convert } from '../../core/format';
2727
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
28-
import { Address } from '../account/Address';
2928
import { PublicAccount } from '../account/PublicAccount';
3029
import { UnresolvedAddress } from '../account/UnresolvedAddress';
31-
import { NamespaceId } from '../namespace/NamespaceId';
3230
import { NetworkType } from '../network/NetworkType';
3331
import { UInt64 } from '../UInt64';
3432
import { Deadline } from './Deadline';
@@ -203,14 +201,9 @@ export class AccountMetadataTransaction extends Transaction {
203201
* @internal
204202
* Check a given address should be notified in websocket channels
205203
* @param address address to be notified
206-
* @param alias address alias (names)
207204
* @returns {boolean}
208205
*/
209-
public shouldNotifyAccount(address: Address, alias: NamespaceId[]): boolean {
210-
return (
211-
super.isSigned(address) ||
212-
this.targetAddress.equals(address) ||
213-
alias.find((name) => this.targetAddress.equals(name)) !== undefined
214-
);
206+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
207+
return super.isSigned(address) || this.targetAddress.equals(address);
215208
}
216209
}

src/model/transaction/AggregateTransaction.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import { KeyPair, MerkleHashBuilder, SHA3Hasher } from '../../core/crypto';
3131
import { Convert } from '../../core/format';
3232
import { DtoMapping } from '../../core/utils';
3333
import { CreateTransactionFromPayload } from '../../infrastructure/transaction';
34-
import { Account, Address, PublicAccount } from '../account';
35-
import { NamespaceId } from '../namespace';
34+
import { Account, PublicAccount, UnresolvedAddress } from '../account';
3635
import { NetworkType } from '../network';
3736
import { Statement } from '../receipt';
3837
import { UInt64 } from '../UInt64';
@@ -396,15 +395,13 @@ export class AggregateTransaction extends Transaction {
396395
* @internal
397396
* Check a given address should be notified in websocket channels
398397
* @param address address to be notified
399-
* @param alias address alias (names)
400398
* @returns {boolean}
401399
*/
402-
public shouldNotifyAccount(address: Address, alias: NamespaceId[]): boolean {
400+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
403401
return (
404402
super.isSigned(address) ||
405403
this.cosignatures.find((_) => _.signer.address.equals(address)) !== undefined ||
406-
this.innerTransactions.find((innerTransaction: InnerTransaction) => innerTransaction.shouldNotifyAccount(address, alias)) !==
407-
undefined
404+
this.innerTransactions.find((innerTransaction: InnerTransaction) => innerTransaction.shouldNotifyAccount(address)) !== undefined
408405
);
409406
}
410407
}

src/model/transaction/MosaicAddressRestrictionTransaction.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,9 @@ export class MosaicAddressRestrictionTransaction extends Transaction {
253253
* @internal
254254
* Check a given address should be notified in websocket channels
255255
* @param address address to be notified
256-
* @param alias address alias (names)
257256
* @returns {boolean}
258257
*/
259-
public shouldNotifyAccount(address: Address, alias: NamespaceId[]): boolean {
260-
return (
261-
super.isSigned(address) ||
262-
this.targetAddress.equals(address) ||
263-
alias.find((name) => this.targetAddress.equals(name)) !== undefined
264-
);
258+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
259+
return super.isSigned(address) || this.targetAddress.equals(address);
265260
}
266261
}

src/model/transaction/MosaicMetadataTransaction.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ import {
2727
import { Convert } from '../../core/format';
2828
import { DtoMapping } from '../../core/utils/DtoMapping';
2929
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
30-
import { Address } from '../account/Address';
3130
import { PublicAccount } from '../account/PublicAccount';
3231
import { UnresolvedAddress } from '../account/UnresolvedAddress';
3332
import { UnresolvedMosaicId } from '../mosaic/UnresolvedMosaicId';
34-
import { NamespaceId } from '../namespace/NamespaceId';
3533
import { NetworkType } from '../network/NetworkType';
3634
import { Statement } from '../receipt/Statement';
3735
import { UInt64 } from '../UInt64';
@@ -228,14 +226,9 @@ export class MosaicMetadataTransaction extends Transaction {
228226
* @internal
229227
* Check a given address should be notified in websocket channels
230228
* @param address address to be notified
231-
* @param alias address alias (names)
232229
* @returns {boolean}
233230
*/
234-
public shouldNotifyAccount(address: Address, alias: NamespaceId[]): boolean {
235-
return (
236-
super.isSigned(address) ||
237-
this.targetAddress.equals(address) ||
238-
alias.find((name) => this.targetAddress.equals(name)) !== undefined
239-
);
231+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
232+
return super.isSigned(address) || this.targetAddress.equals(address);
240233
}
241234
}

src/model/transaction/MultisigAccountModificationTransaction.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ import {
2525
} from 'catbuffer-typescript';
2626
import { Convert } from '../../core/format';
2727
import { UnresolvedMapping } from '../../core/utils/UnresolvedMapping';
28-
import { Address } from '../account/Address';
2928
import { PublicAccount } from '../account/PublicAccount';
3029
import { UnresolvedAddress } from '../account/UnresolvedAddress';
31-
import { NamespaceId } from '../namespace/NamespaceId';
3230
import { NetworkType } from '../network/NetworkType';
3331
import { UInt64 } from '../UInt64';
3432
import { Deadline } from './Deadline';
@@ -217,14 +215,13 @@ export class MultisigAccountModificationTransaction extends Transaction {
217215
* @internal
218216
* Check a given address should be notified in websocket channels
219217
* @param address address to be notified
220-
* @param alias address alias (names)
221218
* @returns {boolean}
222219
*/
223-
public shouldNotifyAccount(address: Address, alias: NamespaceId[]): boolean {
220+
public shouldNotifyAccount(address: UnresolvedAddress): boolean {
224221
return (
225222
super.isSigned(address) ||
226-
this.addressAdditions.find((_) => _.equals(address) || alias.find((a) => _.equals(a)) !== undefined) !== undefined ||
227-
this.addressDeletions.find((_) => _.equals(address) || alias.find((a) => _.equals(a)) !== undefined) !== undefined
223+
this.addressAdditions.find((_) => _.equals(address)) !== undefined ||
224+
this.addressDeletions.find((_) => _.equals(address)) !== undefined
228225
);
229226
}
230227
}

0 commit comments

Comments
 (0)