Skip to content

Commit eac4907

Browse files
authored
Return distinct cosignature count in aggregateTransactionService (#552)
* Fixed #551 * improved coverage * added test in account service
1 parent ed60712 commit eac4907

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

src/service/AggregateTransactionService.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { TransactionType } from '../model/transaction/TransactionType';
2727
import { Address } from '../model/account/Address';
2828
import { RepositoryFactory } from '../infrastructure/RepositoryFactory';
2929
import { NetworkRepository } from '../infrastructure/NetworkRepository';
30+
import { MultisigAccountInfo } from '../model/account/MultisigAccountInfo';
3031

3132
/**
3233
* Aggregated Transaction service
@@ -90,13 +91,10 @@ export class AggregateTransactionService {
9091
public getMaxCosignatures(address: Address): Observable<number> {
9192
return this.multisigRepository.getMultisigAccountGraphInfo(address).pipe(
9293
map((graph) => {
93-
let count = 0;
94-
graph.multisigAccounts.forEach((multisigInfo) => {
95-
multisigInfo.map((multisig) => {
96-
count += multisig.cosignatories.length;
97-
});
98-
});
99-
return count;
94+
const cosignatures = ([] as MultisigAccountInfo[])
95+
.concat(...Array.from(graph.multisigAccounts.values()))
96+
.map((info) => info.cosignatories.map((cosig) => cosig.publicKey));
97+
return new Set(([] as string[]).concat(...cosignatures)).size;
10098
}),
10199
);
102100
}

test/model/mosaic/MosaicAmountView.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,25 @@ describe('MosaicAmountView', () => {
5656
expect(mosaicAmountView.mosaicInfo).to.be.an.instanceof(MosaicInfo);
5757
expect(mosaicAmountView.amount.compact()).to.be.equal(100);
5858
});
59+
60+
it('should createComplete a Mosaic Amount View get correct amount divisibility 0', () => {
61+
const mosaicInfoZero = new MosaicInfo(
62+
new MosaicId([3294802500, 2243684972]), // mosaicId
63+
new UInt64([3403414400, 2095475]), // supply
64+
new UInt64([1, 0]), // height
65+
PublicAccount.createFromPublicKey('B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF', NetworkType.MIJIN_TEST),
66+
1, // revision
67+
MosaicFlags.create(true, true, true),
68+
0,
69+
UInt64.fromUint(1000),
70+
);
71+
const mosaicAmountView = new MosaicAmountView(mosaicInfoZero, UInt64.fromUint(100));
72+
expect(mosaicAmountView.relativeAmount()).to.be.equal(100);
73+
expect(mosaicAmountView.mosaicInfo).to.be.an.instanceof(MosaicInfo);
74+
});
75+
76+
it('should return full name', () => {
77+
const mosaicAmountView = new MosaicAmountView(mosaicInfo, UInt64.fromUint(100));
78+
expect(mosaicAmountView.fullName()).to.be.equal(new MosaicId([3294802500, 2243684972]).toHex());
79+
});
5980
});

test/service/AccountService.spec.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ describe('AccountService', () => {
4242
let account: Account;
4343
let account2: Account;
4444

45-
function mockAccountInfo(withMosaic = false): AccountInfo[] {
45+
function mockAccountInfo(withMosaic = false, noNamespace = false): AccountInfo[] {
4646
const mosaic = new Mosaic(new MosaicId('941299B2B7E1291C'), UInt64.fromUint(1));
47-
const mosaics = [
48-
NetworkCurrencyLocal.createAbsolute(1),
49-
NetworkCurrencyPublic.createAbsolute(1),
50-
NetworkHarvestLocal.createAbsolute(1),
51-
];
47+
const mosaics = noNamespace
48+
? []
49+
: [NetworkCurrencyLocal.createAbsolute(1), NetworkCurrencyPublic.createAbsolute(1), NetworkHarvestLocal.createAbsolute(1)];
5250
if (withMosaic) {
5351
mosaics.push(mosaic);
5452
}
@@ -116,15 +114,16 @@ describe('AccountService', () => {
116114
return new NamespaceName(id, name);
117115
}
118116

117+
let mockAccountRepository: AccountRepository;
119118
before(() => {
120119
account = TestingAccount;
121120
account2 = MultisigAccount;
122-
const mockAccountRepository = mock<AccountRepository>();
121+
mockAccountRepository = mock<AccountRepository>();
123122
const mockNamespaceRepository = mock<NamespaceRepository>();
124123
const mockRepoFactory = mock<RepositoryFactory>();
125124

126125
when(mockAccountRepository.getAccountsInfo(deepEqual([account.address]))).thenReturn(observableOf(mockAccountInfo()));
127-
when(mockAccountRepository.getAccountsInfo(deepEqual([account2.address]))).thenReturn(observableOf(mockAccountInfo(true)));
126+
128127
when(mockNamespaceRepository.getNamespacesFromAccounts(deepEqual([account.address]))).thenReturn(observableOf(mockNamespaceInfo()));
129128
when(mockNamespaceRepository.getNamespacesFromAccounts(deepEqual([account2.address]))).thenReturn(
130129
observableOf(mockNamespaceInfo()),
@@ -170,6 +169,7 @@ describe('AccountService', () => {
170169
});
171170

172171
it('should return accountInfo with mosaicId', async () => {
172+
when(mockAccountRepository.getAccountsInfo(deepEqual([account2.address]))).thenReturn(observableOf(mockAccountInfo(true)));
173173
const result = await accountService.accountInfoWithResolvedMosaic([account2.address]).toPromise();
174174
expect(result[0].resolvedMosaics).to.not.be.undefined;
175175
expect(result[0].resolvedMosaics![0].namespaceName?.name).to.be.equal('catapult.currency');
@@ -187,4 +187,13 @@ describe('AccountService', () => {
187187
expect(result![1].namespaceName).to.be.equal('symbol.xym');
188188
expect(result![2].namespaceName).to.be.equal('catapult.harvest');
189189
});
190+
191+
it('should return empty resolved namespaceInfo', async () => {
192+
when(mockAccountRepository.getAccountsInfo(deepEqual([account2.address]))).thenReturn(observableOf(mockAccountInfo(true, true)));
193+
const result = await accountService.accountInfoWithResolvedMosaic([account2.address]).toPromise();
194+
console.log(result[0].resolvedMosaics);
195+
expect(result).to.not.be.undefined;
196+
expect(result.length).to.be.greaterThan(0);
197+
expect(result![0].resolvedMosaics?.length).to.be.equal(1);
198+
});
190199
});

test/service/AggregateTransactionService.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ describe('AggregateTransactionService', () => {
127127
return new MultisigAccountGraphInfo(map);
128128
}
129129

130+
function givenMultisig2AccountGraphInfoDuplicated(): MultisigAccountGraphInfo {
131+
const map = new Map<number, MultisigAccountInfo[]>();
132+
map.set(0, [new MultisigAccountInfo(multisig2.publicAccount, 2, 1, [multisig1.publicAccount, account1.publicAccount], [])]).set(1, [
133+
new MultisigAccountInfo(
134+
multisig1.publicAccount,
135+
1,
136+
1,
137+
[account1.publicAccount, account2.publicAccount, account3.publicAccount],
138+
[multisig2.publicAccount],
139+
),
140+
]);
141+
142+
return new MultisigAccountGraphInfo(map);
143+
}
144+
130145
function givenMultisig3AccountGraphInfo(): MultisigAccountGraphInfo {
131146
const map = new Map<number, MultisigAccountInfo[]>();
132147
map.set(0, [new MultisigAccountInfo(multisig3.publicAccount, 2, 2, [account2.publicAccount, account3.publicAccount], [])]);
@@ -144,10 +159,11 @@ describe('AggregateTransactionService', () => {
144159
}
145160

146161
let mockNetworkRepository: NetworkRepository;
162+
let mockedAccountRepository: MultisigRepository;
147163
before(() => {
148164
mockNetworkRepository = mock<NetworkRepository>();
149165
const mockRepoFactory = mock<RepositoryFactory>();
150-
const mockedAccountRepository: MultisigRepository = mock<MultisigRepository>();
166+
mockedAccountRepository = mock<MultisigRepository>();
151167

152168
when(mockedAccountRepository.getMultisigAccountInfo(deepEqual(account1.address))).thenReturn(observableOf(givenAccount1Info()));
153169
when(mockedAccountRepository.getMultisigAccountInfo(deepEqual(account4.address))).thenReturn(observableOf(givenAccount4Info()));
@@ -626,4 +642,12 @@ describe('AggregateTransactionService', () => {
626642
const max = await aggregateTransactionService.getMaxCosignatures(multisig2.address).toPromise();
627643
expect(max).to.be.equal(4);
628644
});
645+
646+
it('should call getMaxCosignatures and returns distinct count', async () => {
647+
when(mockedAccountRepository.getMultisigAccountGraphInfo(deepEqual(multisig2.address))).thenReturn(
648+
observableOf(givenMultisig2AccountGraphInfoDuplicated()),
649+
);
650+
const max = await aggregateTransactionService.getMaxCosignatures(multisig2.address).toPromise();
651+
expect(max).to.be.equal(4);
652+
});
629653
});

0 commit comments

Comments
 (0)