Skip to content

Commit 26f48c8

Browse files
committed
Fix bug: Cached addresses were not checked for duplicates
1 parent c6430de commit 26f48c8

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

groups/voteForDelegate.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,45 @@ module.exports = (nodeManager) => {
3333

3434
const uniqueVotes = [];
3535

36-
votes.forEach((vote, i) => {
36+
for (let i = votes.length - 1; i >= 0; i--) {
37+
const vote = votes[i];
3738
const voteName = vote.slice(1);
3839
const voteDirection = vote.charAt(0);
3940

4041
const cachedPublicKey = publicKeysCache[voteName];
4142

4243
if (cachedPublicKey) {
4344
votes[i] = `${voteDirection}${cachedPublicKey}`;
44-
45-
continue;
46-
}
47-
48-
if (validator.validateAdmVoteForAddress(vote)) {
49-
const res = await get(nodeManager)('/accounts', { address: voteName });
50-
51-
if (res.success) {
52-
const publicKey = res.data.account.publicKey;
53-
54-
votes[i] = `${voteDirection}${publicKey}`;
55-
publicKeysCache[voteName] = publicKey;
56-
} else {
57-
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);
58-
45+
} else {
46+
if (validator.validateAdmVoteForAddress(vote)) {
47+
const res = await get(nodeManager)('/accounts', { address: voteName });
48+
49+
if (res.success) {
50+
const publicKey = res.data.account.publicKey;
51+
52+
votes[i] = `${voteDirection}${publicKey}`;
53+
publicKeysCache[voteName] = publicKey;
54+
} else {
55+
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);
56+
57+
return validator.badParameter('votes');
58+
}
59+
} else if (validator.validateAdmVoteForDelegateName(vote)) {
60+
const res = await get(nodeManager)('/delegates/get', { username: voteName });
61+
62+
if (res.success) {
63+
const publicKey = res.data.delegate.publicKey;
64+
65+
votes[i] = `${voteDirection}${publicKey}`;
66+
publicKeysCache[voteName] = publicKey;
67+
} else {
68+
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);
69+
70+
return validator.badParameter('votes');
71+
}
72+
} else if (!validator.validateAdmVoteForPublicKey(vote)) {
5973
return validator.badParameter('votes');
6074
}
61-
} else if (validator.validateAdmVoteForDelegateName(vote)) {
62-
const res = await get(nodeManager)('/delegates/get', { username: voteName });
63-
64-
if (res.success) {
65-
const publicKey = res.data.delegate.publicKey;
66-
67-
votes[i] = `${voteDirection}${publicKey}`;
68-
publicKeysCache[voteName] = publicKey;
69-
} else {
70-
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);
71-
72-
return validator.badParameter('votes');
73-
}
74-
} else if (!validator.validateAdmVoteForPublicKey(vote)) {
75-
return validator.badParameter('votes');
7675
}
7776

7877
// Exclude duplicates
@@ -81,7 +80,7 @@ module.exports = (nodeManager) => {
8180
if (!foundCopy) {
8281
uniqueVotes.push(votes[i]);
8382
}
84-
});
83+
}
8584

8685
const type = constants.transactionTypes.VOTE;
8786

0 commit comments

Comments
 (0)