Better error messages in whitelist check. (#3127)

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2020-08-07 15:12:16 +02:00 committed by GitHub
parent 606d1a8e6f
commit 484c63ddeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 16 deletions

View File

@ -1,7 +1,11 @@
import { chainsWithBlacklist } from "../common/blockchains"; import { chainsWithBlacklist } from "../common/blockchains";
import { getChainAssetsList, getChainWhitelistPath, getChainBlacklistPath } from "../common/repo-structure"; import { getChainAssetsList, getChainWhitelistPath, getChainBlacklistPath } from "../common/repo-structure";
import { readFileSync, writeFileSync } from "../common/filesystem"; import { readFileSync, writeFileSync } from "../common/filesystem";
import { arrayDiff, findCommonElementOrDuplicate } from "../common/types"; import {
arrayDiff,
findCommonElementOrDuplicate,
makeUnique
} from "../common/types";
import { ActionInterface, CheckStepInterface } from "./interface"; import { ActionInterface, CheckStepInterface } from "./interface";
import { formatSortJson, formatUniqueSortJson } from "../common/json"; import { formatSortJson, formatUniqueSortJson } from "../common/json";
import * as bluebird from "bluebird"; import * as bluebird from "bluebird";
@ -20,28 +24,41 @@ async function checkUpdateWhiteBlackList(chain: string, checkOnly: boolean ): Pr
const commonElementsOrDuplicated = findCommonElementOrDuplicate(currentWhitelist, currentBlacklist); const commonElementsOrDuplicated = findCommonElementOrDuplicate(currentWhitelist, currentBlacklist);
if (commonElementsOrDuplicated && commonElementsOrDuplicated.length > 0) { if (commonElementsOrDuplicated && commonElementsOrDuplicated.length > 0) {
wrongMsg += `Blacklist and whitelist for chain ${chain} should have no common elements or duplicates, found ${commonElementsOrDuplicated.length}, ${commonElementsOrDuplicated[0]}\n`; wrongMsg += `Blacklist and whitelist for chain ${chain} should have no common elements or duplicates, found ${commonElementsOrDuplicated}\n`;
} }
const removedAssets = arrayDiff(currentWhitelist, assets); const whitelistOrphan = arrayDiff(currentWhitelist, assets);
if (removedAssets && removedAssets.length > 0) { if (whitelistOrphan && whitelistOrphan.length > 0) {
wrongMsg += `Whitelist for chain ${chain} contains non-exitent assets, found ${removedAssets.length}, ${removedAssets[0]}\n`; wrongMsg += `Whitelist for chain ${chain} contains non-exitent assets, found ${whitelistOrphan.length}, ${whitelistOrphan[0]}\n`;
} }
const niceWhite = formatSortJson(assets); const newBlack = makeUnique(currentBlacklist.concat(whitelistOrphan));
if (niceWhite !== currentWhitelistText) { const newWhite = makeUnique(arrayDiff(assets, newBlack));
wrongMsg += `Whitelist for chain ${chain} has inconsistent content of formatting\n`; //console.log(currentWhitelist.length, "vs.", newWhite.length);
//console.log(currentBlacklist.length, "vs.", newBlack.length);
const wDiff1 = arrayDiff(newWhite, currentWhitelist);
if (wDiff1.length > 0) {
wrongMsg += `Some elements are missing from whitelist for chain ${chain}: ${wDiff1.length} ${wDiff1[0]}\n`;
} }
const newBlackList = currentBlacklist.concat(removedAssets); const wDiff2 = arrayDiff(currentWhitelist, newWhite);
const niceBlack = formatUniqueSortJson(newBlackList); if (wDiff2.length > 0) {
if (niceBlack !== currentBlacklistText) { wrongMsg += `Some elements should be removed from whitelist for chain ${chain}: ${wDiff2.length} ${wDiff2[0]}\n`;
wrongMsg += `Blacklist for chain ${chain} has inconsistent content of formatting\n`; }
const bDiff1 = arrayDiff(newBlack, currentBlacklist);
if (bDiff1.length > 0) {
wrongMsg += `Some elements are missing from blacklist for chain ${chain}: ${bDiff1.length} ${bDiff1[0]}\n`;
}
const bDiff2 = arrayDiff(currentBlacklist, newBlack);
if (bDiff2.length > 0) {
wrongMsg += `Some elements should be removed from blacklist for chain ${chain}: ${bDiff2.length} ${bDiff2[0]}\n`;
} }
if (wrongMsg.length > 0) { if (wrongMsg.length > 0) {
if (!checkOnly) { if (!checkOnly) {
// update // update
writeFileSync(whitelistPath, niceWhite); writeFileSync(whitelistPath, formatSortJson(newWhite));
writeFileSync(blacklistPath, niceBlack); writeFileSync(blacklistPath, formatSortJson(newBlack));
console.log(`Updated white and blacklists for chain ${chain}`); console.log(`Updated white and blacklists for chain ${chain}`);
} }
} }

View File

@ -46,6 +46,20 @@ export function findDuplicate(list: string[]): string {
// Check that two lists have no common elements, and no duplicates in either. // Check that two lists have no common elements, and no duplicates in either.
// Do a single check: checking for duplicates in the concatenated list. // Do a single check: checking for duplicates in the concatenated list.
export function findCommonElementOrDuplicate(list1: string[], list2: string[]) { export function findCommonElementOrDuplicate(list1: string[], list2: string[]): string {
return findDuplicate(list1.concat(list2)); return findDuplicate(list1.concat(list2));
} }
// Compare two arrays, order does not matter
export function arrayEqual(a1: any[], a2: any[]): boolean {
if (a1.length != a2.length) {
return false;
}
if (!(arrayDiff(a1, a2).length == 0)) {
return false;
}
if (!(arrayDiff(a2, a1).length == 0)) {
return false;
}
return true;
}

View File

@ -15,7 +15,8 @@ import {
mapList, mapList,
sortElements, sortElements,
makeUnique, makeUnique,
arrayDiff arrayDiff,
arrayEqual
} from "../script/common/types"; } from "../script/common/types";
import { findImagesToFetch } from "../script/action/binance"; import { findImagesToFetch } from "../script/action/binance";
@ -75,6 +76,7 @@ describe("Test type helpers", () => {
}); });
test(`Test arrayDiff`, () => { test(`Test arrayDiff`, () => {
expect(arrayDiff(["a", "b", "c"], ["c"]), `4 elems with 1 duplicate`).toEqual(["a", "b"]); expect(arrayDiff(["a", "b", "c"], ["c"]), `4 elems with 1 duplicate`).toEqual(["a", "b"]);
expect(arrayDiff(["a", "b", "c"], ["d"]), `4 elems with 0 duplicate`).toEqual(["a", "b", "c"]);
}); });
test(`Test findDuplicate`, () => { test(`Test findDuplicate`, () => {
expect(findDuplicate(["a", "bb", "ccc"]), `No duplicates`).toBe(null) expect(findDuplicate(["a", "bb", "ccc"]), `No duplicates`).toBe(null)
@ -92,6 +94,12 @@ describe("Test type helpers", () => {
expect(findCommonElementOrDuplicate(["a", "bb", "ccc", "1", "bb"], ["1", "22", "333", "22"]), `Intersection and duplicates`).toBe("22") expect(findCommonElementOrDuplicate(["a", "bb", "ccc", "1", "bb"], ["1", "22", "333", "22"]), `Intersection and duplicates`).toBe("22")
expect(findCommonElementOrDuplicate([], []), `Empty lists`).toBe(null) expect(findCommonElementOrDuplicate([], []), `Empty lists`).toBe(null)
}); });
test(`Test arrayEqual`, () => {
expect(arrayEqual(["a", "b", "c"], ["a", "b", "c"]), `equal`).toBe(true);
expect(arrayEqual(["a", "b", "c", "d"], ["a", "b", "c"]), `length mismatch`).toBe(false);
expect(arrayEqual(["a", "b", "c"], ["a", "b", "b"]), `length mismatch`).toBe(false);
expect(arrayEqual(["a", "b", "b"], ["a", "b", "c"]), `length mismatch`).toBe(false);
});
}); });
describe("Test action binance", () => { describe("Test action binance", () => {