2020-07-29 13:42:51 +00:00
import { chainsWithBlacklist } from "../common/blockchains" ;
import { getChainAssetsList , getChainWhitelistPath , getChainBlacklistPath } from "../common/repo-structure" ;
import { readFileSync , writeFileSync } from "../common/filesystem" ;
2020-08-07 13:12:16 +00:00
import {
arrayDiff ,
2020-08-07 14:01:57 +00:00
arrayDiffNocase ,
findCommonElementsOrDuplicates ,
2020-08-07 13:12:16 +00:00
makeUnique
} from "../common/types" ;
2020-08-06 19:17:38 +00:00
import { ActionInterface , CheckStepInterface } from "./interface" ;
2020-08-10 08:56:41 +00:00
import { formatSortJson } from "../common/json" ;
2020-08-06 19:17:38 +00:00
import * as bluebird from "bluebird" ;
async function checkUpdateWhiteBlackList ( chain : string , checkOnly : boolean ) : Promise < [ boolean , string ] > {
let wrongMsg = "" ;
const assets = getChainAssetsList ( chain ) ;
const whitelistPath = getChainWhitelistPath ( chain ) ;
const blacklistPath = getChainBlacklistPath ( chain ) ;
const currentWhitelistText = readFileSync ( whitelistPath ) ;
const currentBlacklistText = readFileSync ( blacklistPath ) ;
const currentWhitelist = JSON . parse ( currentWhitelistText ) ;
const currentBlacklist = JSON . parse ( currentBlacklistText ) ;
2020-08-07 14:01:57 +00:00
const commonElementsOrDuplicates = findCommonElementsOrDuplicates ( currentWhitelist , currentBlacklist ) ;
if ( commonElementsOrDuplicates && commonElementsOrDuplicates . length > 0 ) {
wrongMsg += ` Blacklist and whitelist for chain ${ chain } should have no common elements or duplicates, found ${ commonElementsOrDuplicates . length } ${ commonElementsOrDuplicates [ 0 ] } \ n ` ;
2020-08-06 19:17:38 +00:00
}
2020-08-07 13:12:16 +00:00
const whitelistOrphan = arrayDiff ( currentWhitelist , assets ) ;
if ( whitelistOrphan && whitelistOrphan . length > 0 ) {
wrongMsg += ` Whitelist for chain ${ chain } contains non-exitent assets, found ${ whitelistOrphan . length } , ${ whitelistOrphan [ 0 ] } \ n ` ;
2020-08-06 19:17:38 +00:00
}
2020-08-07 13:12:16 +00:00
const newBlack = makeUnique ( currentBlacklist . concat ( whitelistOrphan ) ) ;
2020-08-07 14:01:57 +00:00
const newWhite = makeUnique ( arrayDiffNocase ( assets , newBlack ) ) ;
2020-08-07 13:12:16 +00:00
//console.log(currentWhitelist.length, "vs.", newWhite.length);
//console.log(currentBlacklist.length, "vs.", newBlack.length);
2020-08-07 14:01:57 +00:00
const wDiff1 = arrayDiffNocase ( newWhite , currentWhitelist ) ;
2020-08-07 13:12:16 +00:00
if ( wDiff1 . length > 0 ) {
wrongMsg += ` Some elements are missing from whitelist for chain ${ chain } : ${ wDiff1 . length } ${ wDiff1 [ 0 ] } \ n ` ;
}
2020-08-07 14:01:57 +00:00
const wDiff2 = arrayDiffNocase ( currentWhitelist , newWhite ) ;
2020-08-07 13:12:16 +00:00
if ( wDiff2 . length > 0 ) {
wrongMsg += ` Some elements should be removed from whitelist for chain ${ chain } : ${ wDiff2 . length } ${ wDiff2 [ 0 ] } \ n ` ;
}
2020-08-07 14:01:57 +00:00
const bDiff1 = arrayDiffNocase ( newBlack , currentBlacklist ) ;
2020-08-07 13:12:16 +00:00
if ( bDiff1 . length > 0 ) {
wrongMsg += ` Some elements are missing from blacklist for chain ${ chain } : ${ bDiff1 . length } ${ bDiff1 [ 0 ] } \ n ` ;
2020-08-06 19:17:38 +00:00
}
2020-08-07 14:01:57 +00:00
const bDiff2 = arrayDiffNocase ( currentBlacklist , newBlack ) ;
2020-08-07 13:12:16 +00:00
if ( bDiff2 . length > 0 ) {
wrongMsg += ` Some elements should be removed from blacklist for chain ${ chain } : ${ bDiff2 . length } ${ bDiff2 [ 0 ] } \ n ` ;
2020-08-06 19:17:38 +00:00
}
2020-08-10 08:56:41 +00:00
// additionally check for nice formatting, sorting:
const newWhiteText = formatSortJson ( newWhite ) ;
const newBlackText = formatSortJson ( newBlack ) ;
if ( newWhiteText !== currentWhitelistText ) {
wrongMsg += ` Whitelist for chain ${ chain } : not formatted nicely \ n ` ;
}
if ( newBlackText !== currentBlacklistText ) {
wrongMsg += ` Blacklist for chain ${ chain } : not formatted nicely \ n ` ;
}
2020-08-06 19:17:38 +00:00
if ( wrongMsg . length > 0 ) {
2020-08-10 08:56:41 +00:00
// sg wrong, may need to fix
2020-08-06 19:17:38 +00:00
if ( ! checkOnly ) {
// update
2020-08-10 08:56:41 +00:00
writeFileSync ( whitelistPath , newWhiteText ) ;
writeFileSync ( blacklistPath , newBlackText ) ;
2020-08-06 19:17:38 +00:00
console . log ( ` Updated white and blacklists for chain ${ chain } ` ) ;
}
}
return [ ( wrongMsg . length == 0 ) , wrongMsg ] ;
2020-07-29 13:42:51 +00:00
}
2020-08-06 19:17:38 +00:00
export class Whitelist implements ActionInterface {
getName ( ) : string { return "Whitelists" ; }
2020-08-10 08:56:41 +00:00
getSanityChecks = null ;
getConsistencyChecks ( ) : CheckStepInterface [ ] {
2020-08-06 19:17:38 +00:00
const steps : CheckStepInterface [ ] = [ ] ;
chainsWithBlacklist . forEach ( chain = > {
steps . push (
{
getName : ( ) = > { return ` Whitelist and blacklist for ${ chain } should be consistent with assets ` } ,
check : async ( ) = > {
const [ isOK , msg ] = await checkUpdateWhiteBlackList ( chain , true ) ;
if ( ! isOK ) {
return msg ;
}
return "" ;
}
}
) ;
} ) ;
return steps ;
}
2020-08-10 08:56:41 +00:00
sanityFix = null ;
2020-08-06 19:17:38 +00:00
2020-08-10 08:56:41 +00:00
async consistencyFix ( ) : Promise < void > {
2020-08-06 19:17:38 +00:00
await bluebird . each ( chainsWithBlacklist , async ( chain ) = > await checkUpdateWhiteBlackList ( chain , false ) ) ;
}
update = null ;
2020-07-29 13:42:51 +00:00
}