fix script\

This commit is contained in:
Pedro Gomes 2019-06-17 18:09:18 +02:00
parent 4d489c7a86
commit 012c383451
8 changed files with 48 additions and 42 deletions

View File

@ -14,5 +14,5 @@
"infoURL": "https://forum.poa.network/c/xdai-chain", "infoURL": "https://forum.poa.network/c/xdai-chain",
"shortName": "xdai", "shortName": "xdai",
"chainId": 100, "chainId": 100,
"networkId": 1 "networkId": 100
} }

View File

@ -15,5 +15,5 @@
"infoURL": "https://rsk.co", "infoURL": "https://rsk.co",
"shortName": "rsk", "shortName": "rsk",
"chainId": 30, "chainId": 30,
"networkId": 775 "networkId": 30
} }

View File

@ -17,5 +17,5 @@
"infoURL": "https://rsk.co", "infoURL": "https://rsk.co",
"shortName": "trsk", "shortName": "trsk",
"chainId": 31, "chainId": 31,
"networkId": 8052 "networkId": 31
} }

View File

@ -14,5 +14,5 @@
"infoURL": "https://ellaism.org", "infoURL": "https://ellaism.org",
"shortName": "ella", "shortName": "ella",
"chainId": 64, "chainId": 64,
"networkId": 1 "networkId": 64
} }

View File

@ -14,5 +14,5 @@
"infoURL": "https://mix-blockchain.org", "infoURL": "https://mix-blockchain.org",
"shortName": "mix", "shortName": "mix",
"chainId": 76, "chainId": 76,
"networkId": 1 "networkId": 76
} }

View File

@ -16,5 +16,5 @@
"infoURL": "https://poa.network", "infoURL": "https://poa.network",
"shortName": "poa", "shortName": "poa",
"chainId": 77, "chainId": 77,
"networkId": 1 "networkId": 77
} }

View File

@ -14,5 +14,5 @@
"infoURL": "https://poa.network", "infoURL": "https://poa.network",
"shortName": "skl", "shortName": "skl",
"chainId": 99, "chainId": 99,
"networkId": 2 "networkId": 99
} }

View File

@ -31,7 +31,7 @@ function formatRpcUrl (rpcUrl) {
} }
async function writeJson (filePath, json) { async function writeJson (filePath, json) {
console.log('Overwriting', filePath) // console.log('Overwriting', filePath)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const data = JSON.stringify(json, null, 2) const data = JSON.stringify(json, null, 2)
fs.writeFile(filePath, data, (err, res) => { fs.writeFile(filePath, data, (err, res) => {
@ -91,6 +91,38 @@ async function getChainId (rpcUrl) {
} }
} }
async function queryMulti (urls, apiCall) {
let result = null
let results = await Promise.all(
urls.map(async url => {
try {
return await apiCall(url)
} catch (error) {
return null
}
})
)
if (results && results.length) {
results = results.filter(x => !!x)
result = results[0] || null
}
return result
}
async function verifyJson (json) {
if (json.rpc && json.rpc.length) {
const chainId = await queryMulti(json.rpc, getChainId)
if (chainId) {
json.chainId = chainId
}
const networkId = await queryMulti(json.rpc, getNetworkId)
if (networkId) {
json.networkId = networkId
}
}
return json
}
fs.readdir(CHAINS_DIRECTORY, function (err, files) { fs.readdir(CHAINS_DIRECTORY, function (err, files) {
if (err) { if (err) {
console.error('Could not list the directory.', err) console.error('Could not list the directory.', err)
@ -108,43 +140,17 @@ fs.readdir(CHAINS_DIRECTORY, function (err, files) {
const ext = path.extname(file) const ext = path.extname(file)
if (stat.isFile() && ext === '.json') { if (stat.isFile() && ext === '.json') {
const json = require(filePath) let json = require(filePath)
console.log('Verifying file', file, 'through file name')
const fileName = file.replace(ext, '') const fileName = file.replace(ext, '')
if (toNumber(fileName) !== toNumber(json.chainId)) { if (toNumber(fileName)) {
console.log(`File ${file} chainId doesn't match file name`)
json.chainId = toNumber(fileName) json.chainId = toNumber(fileName)
} }
json = await verifyJson(json)
if (json.rpc && json.rpc.length) { console.log(
console.log('Verifying file', file, 'through rpc url') `${json.chain.toUpperCase()} chainId=${json.chainId} networId=${
try { json.networkId
const chainIdArr = await Promise.all( }`
json.rpc.map(rpcUrl => getChainId(rpcUrl)) )
).filter(x => !!x)
const chainId =
chainIdArr && chainIdArr.length ? chainIdArr[0] : null
if (chainId && chainId !== toNumber(json.chainId)) {
console.log(`File ${file} chainId doesn't match rpc response`)
json.chainId = chainId
}
} catch (error) {
// do nothing
}
try {
const networkIdArr = await Promise.all(
json.rpc.map(rpcUrl => getNetworkId(rpcUrl))
).filter(x => !!x)
const networkId =
networkIdArr && networkIdArr.length ? networkIdArr[0] : null
if (networkId && networkId !== toNumber(json.networkId)) {
console.log(`File ${file} networkId doesn't match rpc response`)
json.networkId = networkId
}
} catch (error) {
// do nothing
}
}
await writeJson(filePath, json) await writeJson(filePath, json)
} }
}) })