Created
April 13, 2020 04:54
-
-
Save ok1a/5612bbb311f29f2cdfbf195e3c3d1017 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// single chain solution | |
function findBadActor(poolMap) { | |
let successCount = 0; | |
let badActor = '' | |
let poolArr = [] | |
for (let [key, value] of Object.entries(poolMap)) { | |
poolArr.push({ ip: key, node: value[0] }) | |
} | |
let compareNode = poolArr[0] | |
let initNode = poolArr[0] | |
let swapped = false; | |
for (let i = 1; i < poolArr.length; i++) { | |
let currNode = poolArr[i] | |
if (compareNode['node']['address'] === currNode['node']['address']) { | |
if (!swapped) { | |
successCount++; | |
} else { | |
return initNode['ip'] | |
} | |
} else { | |
if (successCount > 1) { | |
badActor = currNode['ip'] | |
return badActor | |
} else if (successCount === 1) { | |
if (swapped) { | |
badActor = compareNode['ip']; | |
return badActor | |
} else { | |
compareNode = currNode | |
swapped = true; | |
} | |
} | |
} | |
} | |
} | |
//Each IP is a THORNode | |
// Each THORNode tracks chains (eg. BTC/ETH) | |
// Each chain has a unique pool address | |
// Objective: Output the pool address for each chain, only if all nodes agree. If one disagrees, attribute blame. | |
// Go! | |
const PoolAddressMap = { | |
['127.0.0.1']: [{ chain: 'c1', address: 'a1' }, { chain: 'c2', address: 'a2' }], | |
['127.0.0.2']: [{ chain: 'c1', address: 'a1' }, { chain: 'c2', address: 'a2' }], | |
['127.0.0.3']: [{ chain: 'c1', address: 'a1' }, { chain: 'c2', address: 'a3' }], // another address for c2 == bad actor | |
['127.0.0.4']: [{ chain: 'c1', address: 'a1' }, { chain: 'c2', address: 'a2' }], | |
['127.0.0.5']: [{ chain: 'c1', address: 'a4' }, { chain: 'c2', address: 'a2' }], // another address for c1 == bad actor | |
} | |
findBadActors(PoolAddressMap) | |
console.log(findBadActors(PoolAddressMap)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment