Skip to content

Commit

Permalink
Fix bug: Cached addresses were not checked for duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
martiliones committed Feb 12, 2022
1 parent c6430de commit 26f48c8
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions groups/voteForDelegate.js
Expand Up @@ -33,46 +33,45 @@ module.exports = (nodeManager) => {

const uniqueVotes = [];

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

const cachedPublicKey = publicKeysCache[voteName];

if (cachedPublicKey) {
votes[i] = `${voteDirection}${cachedPublicKey}`;

continue;
}

if (validator.validateAdmVoteForAddress(vote)) {
const res = await get(nodeManager)('/accounts', { address: voteName });

if (res.success) {
const publicKey = res.data.account.publicKey;

votes[i] = `${voteDirection}${publicKey}`;
publicKeysCache[voteName] = publicKey;
} else {
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);

} else {
if (validator.validateAdmVoteForAddress(vote)) {
const res = await get(nodeManager)('/accounts', { address: voteName });

if (res.success) {
const publicKey = res.data.account.publicKey;

votes[i] = `${voteDirection}${publicKey}`;
publicKeysCache[voteName] = publicKey;
} else {
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);

return validator.badParameter('votes');
}
} else if (validator.validateAdmVoteForDelegateName(vote)) {
const res = await get(nodeManager)('/delegates/get', { username: voteName });

if (res.success) {
const publicKey = res.data.delegate.publicKey;

votes[i] = `${voteDirection}${publicKey}`;
publicKeysCache[voteName] = publicKey;
} else {
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);

return validator.badParameter('votes');
}
} else if (!validator.validateAdmVoteForPublicKey(vote)) {
return validator.badParameter('votes');
}
} else if (validator.validateAdmVoteForDelegateName(vote)) {
const res = await get(nodeManager)('/delegates/get', { username: voteName });

if (res.success) {
const publicKey = res.data.delegate.publicKey;

votes[i] = `${voteDirection}${publicKey}`;
publicKeysCache[voteName] = publicKey;
} else {
logger.warn(`[ADAMANT js-api] Failed to get public key for ${vote}. ${res.errorMessage}.`);

return validator.badParameter('votes');
}
} else if (!validator.validateAdmVoteForPublicKey(vote)) {
return validator.badParameter('votes');
}

// Exclude duplicates
Expand All @@ -81,7 +80,7 @@ module.exports = (nodeManager) => {
if (!foundCopy) {
uniqueVotes.push(votes[i]);
}
});
}

const type = constants.transactionTypes.VOTE;

Expand Down

0 comments on commit 26f48c8

Please sign in to comment.