forked from Ion-Protocol/nucleus-boring-vault
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lzConfigCheck.cjs
134 lines (112 loc) · 4.44 KB
/
lzConfigCheck.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { log } from 'console';
const fs = require('fs');
const readline = require('readline');
// Function to load the DVN JSON data
function loadDvns() {
const data = fs.readFileSync('./deployment-config/layerzero/dvn-deployments.json', 'utf8');
return JSON.parse(data);
}
// Function to find an address in the JSON data
function findAddressInJson(addresses, jsonData, searchKey = "") {
const results = [];
const lowerCaseAddresses = addresses.map(a => a.toLowerCase());
for (const [parentKey, parentValue] of Object.entries(jsonData)) {
for (const [key, value] of Object.entries(parentValue)) {
if (lowerCaseAddresses.includes(value.toLowerCase())) {
results.push([parentKey, key, value]);
}
}
}
if (searchKey === "") {
if (results.length === 1) {
const [parentKey, key, value] = results[0];
return [true, key, parentKey];
} else {
return [false, null, null];
}
} else {
for (const [parentKey, key, value] of results) {
if (key === searchKey) {
return [true, key, parentKey];
}
}
return [false, null, null];
}
}
// Function to get findings from a config file
function getFindingsInConfig(configName) {
const dvnJsonData = loadDvns();
const data = fs.readFileSync(`./deployment-config/${configName}`, 'utf8');
const configJsonData = JSON.parse(data);
const required = configJsonData.teller.dvnIfNoDefault.required;
const optional = configJsonData.teller.dvnIfNoDefault.optional;
const addresses = [...required, ...optional];
let chain = "";
for (const address of addresses) {
const [found, key, parentKey] = findAddressInJson([address], dvnJsonData);
if (found) {
chain = key;
break;
}
}
if (chain === "") {
throw new Error("❌ All provided configs have duplicates or are not found in the DVN registry");
}
const findings = [];
for (const address of addresses) {
const [found, key, parentKey] = findAddressInJson([address], dvnJsonData, chain);
if (found) {
findings.push({ address, chain: key, provider: parentKey });
} else {
console.log("Not Found ", address);
}
}
return {
findings,
requiredCount: required.length,
optionalCount: optional.length,
confirmations: configJsonData.teller.dvnIfNoDefault.blockConfirmationsRequiredIfNoDefault,
threshold: configJsonData.teller.dvnIfNoDefault.optionalThreshold
};
}
function assert(statement, message){
if(!statement){
throw new Error("❌ "+message)
}
}
// Main function
async function main() {
const args = process.argv.slice(2);
if (args.length != 2) {
console.error("Usage: node script.js <file1Name> <file2Name>");
process.exit(1);
}
const [file1Name, file2Name] = args;
try {
const findings1 = getFindingsInConfig(file1Name);
const findings2 = getFindingsInConfig(file2Name);
assert(findings1.confirmations == findings2.confirmations, "Confirmations do not match");
assert(findings1.threshold == findings2.threshold, "thresholds do not match");
assert(findings1.requiredCount == findings2.requiredCount, "required DVNs count does not match");
assert(findings1.optionalCount == findings2.optionalCount, "optional DVNs count does not match");
const chain1 = findings1.findings[0].chain;
const providers1 = findings1.findings.map(finding => finding.provider);
for (const finding of findings1.findings) {
assert(finding.chain == chain1, "Networks do not match for "+finding)
}
const chain2 = findings2.findings[0].chain;
const providers2 = findings2.findings.map(finding => finding.provider);
for (const finding of findings2.findings) {
assert(providers1.includes(finding.provider), "Provider: "+finding.provider+" does not have a matching provider in the first config");
assert(finding.chain == chain2, "Networks do not match for: "+finding);
}
console.log('chain1', chain1);
console.log('providers1', providers1);
console.log('chain2', chain2);
console.log('providers2', providers2);
console.log("✅ Config check passed");
} catch (error) {
console.error(error.message);
}
}
main();