Skip to content

Commit

Permalink
Merge pull request #5 from lens-protocol/cleanup
Browse files Browse the repository at this point in the history
chore: hash changes
  • Loading branch information
joshstevens19 authored Dec 14, 2023
2 parents 128c6e0 + 0665307 commit 257a137
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
25 changes: 1 addition & 24 deletions scripts/last-synced.json
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
{
"FOLLOW": [
"FOLLOW-FeeFollowModule-0xa8f6ba162dB789039D07A4f0ce90b1aE1f46FE8E",
"FOLLOW-RevertFollowModule-0x4E2D0157464501C98E781491a9dbC99c2F3fD90f"
],
"OPEN_ACTION": [
"OPEN_ACTION-LegacyFreeCollectModule-0x23b9467334bEb345aAa6fd1545538F3d54436e96",
"OPEN_ACTION-LegacyMultirecipientFeeCollectModule-0xfa9dA21D0A18C7B7dE4566481c1e8952371F880a",
"OPEN_ACTION-LegacySimpleCollectModule-0x54325d507eD1b3776c146700eFF61a98B45AeC76",
"OPEN_ACTION-LegacyFeeCollectModule-0x1292E6dF9a4697DAAfDDBD61D5a7545A634af33d",
"OPEN_ACTION-LegacyLimitedFeeCollectModule-0xEF13EFa565FB29Cd55ECf3De2beb6c69bD988212",
"OPEN_ACTION-LegacyTimedFeeCollectModule-0x1292E6dF9a4697DAAfDDBD61D5a7545A634af33d",
"OPEN_ACTION-LegacyLimitedTimedFeeCollectModule-0x7B94f57652cC1e5631532904A4A038435694636b",
"OPEN_ACTION-SimpleCollectOpenActionModule-0x060f5448ae8aCF0Bc06D040400c6A89F45b488bb",
"OPEN_ACTION-LegacyFeeCollectModule-0xa31FF85E840ED117E172BC9Ad89E55128A999205",
"OPEN_ACTION-LegacyRevertCollectModule-0x1292E6dF9a4697DAAfDDBD61D5a7545A634af33d",
"OPEN_ACTION-MultirecipientFeeCollectOpenActionModule-0x50f1D8779078c790b82dE5Aa8d72A841e1CBBbE1"
],
"REFERENCE": [
"REFERENCE-LegacyFollowerOnlyReferenceModule-0x17317F96f0C7a845FFe78c60B10aB15789b57Aaa",
"REFERENCE-FollowerOnlyReferenceModule-0x19F8980aF29207bbd7A6D0aD0858d59bd11d4647",
"REFERENCE-DegreesOfSeparationReferenceModule-0xBe69302b0E3bAc1018B2772A7B26EB2325b3F9C8"
]
}
{}
66 changes: 48 additions & 18 deletions scripts/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require("fs");
const axios = require("axios");
const crypto = require("crypto");

const SECRET = process.env.SECRET ?? "";

Expand All @@ -26,15 +27,36 @@ const lastSyncedFileReadData = fs.readFileSync(
);
const lastSyncedFileData = JSON.parse(lastSyncedFileReadData);

let lastSynced = {};
let currentSyncData = {};
const verifyPromises = [];

function hashObject(obj) {
const str = JSON.stringify(obj);
return crypto.createHash("md5").update(str).digest("hex");
}

function updateLastSynced(file, moduleId, currentHash) {
if (currentSyncData[file.type]) {
if (currentSyncData[file.type][moduleId]) return;
currentSyncData[file.type][moduleId] = currentHash;
} else {
currentSyncData[file.type] = { [moduleId]: currentHash };
}
return;
}

for (const file of files) {
const data = fs.readFileSync(file.filename, "utf8");
const modules = JSON.parse(data);

for (const module of modules) {
const moduleId = `${file.type}-${module.name}-${module.address}`;
const currentHash = hashObject(module);

if (lastSyncedFileData[file.type]?.[moduleId]) {
updateLastSynced(file, moduleId, currentHash);
if (lastSyncedFileData[file.type][moduleId] === currentHash) continue;
}

const variables = {
request: {
Expand All @@ -56,12 +78,7 @@ for (const file of files) {
`Module Verified: ${module.name}, Response:`,
response.data
);
if (lastSynced[file.type]) {
if (lastSynced[file.type].includes(moduleId)) return;
lastSynced[file.type].push(moduleId);
} else {
lastSynced[file.type] = [moduleId];
}
updateLastSynced(file, moduleId, currentHash);
})
.catch((error) => {
console.error(`Error verify module: ${module.name}`, error);
Expand All @@ -72,23 +89,36 @@ for (const file of files) {
}

Promise.all(verifyPromises).then(() => {
console.log("✅ All modules verified.");
if (verifyPromises.length === 0) {
console.log("✅ Everything is up to date.");
} else {
console.log("✅ All modules verified.");
}

console.log("⌛ Checking removed modules...");
const removedModules = {};

for (const key in lastSyncedFileData) {
removedModules[key] = lastSyncedFileData[key].filter(
(item) => !lastSynced[key]?.includes(item)
);
if (currentSyncData.hasOwnProperty(key)) {
removedModules[key] = {};
for (const moduleId in lastSyncedFileData[key]) {
if (!currentSyncData[key].hasOwnProperty(moduleId)) {
// If the moduleId doesn't exist in currentSyncData, it's considered missing
removedModules[key][moduleId] = lastSyncedFileData[key][moduleId];
}
}
} else {
removedModules[key] = lastSyncedFileData[key];
}
}

console.log("➡︎ Removed Modules -", removedModules);
for (const type in removedModules) {
for (const moduleKey of removedModules[type]) {
console.log("Unverifing module:", moduleKey);
for (const moduleId in removedModules[type]) {
console.log("Unverifing module:", moduleId);

const moduleType = moduleKey.split("-")[0];
const moduleAddress = moduleKey.split("-")[2];
const moduleType = moduleId.split("-")[0];
const moduleAddress = moduleId.split("-")[2];

axios
.post(API_URL, {
Expand All @@ -105,18 +135,18 @@ Promise.all(verifyPromises).then(() => {
})
.then((response) => {
console.log(
`🚮 Module unverified: ${moduleKey}, Response:`,
`🚮 Module unverified: ${moduleId}, Response:`,
response.data
);
})
.catch((error) => {
console.error(`Error unverify module: ${moduleKey}`, error);
console.error(`Error unverify module: ${moduleId}`, error);
});
}
}

fs.writeFileSync(
"scripts/last-synced.json",
JSON.stringify(lastSynced, null, 2)
JSON.stringify(currentSyncData, null, 2)
);
});

0 comments on commit 257a137

Please sign in to comment.