Skip to content

Commit

Permalink
Merge pull request #83 from open-ibc/thomas/fix-sanity-check-UC
Browse files Browse the repository at this point in the history
bug: fix sanity check for universal channels
  • Loading branch information
kenobijon authored Apr 23, 2024
2 parents 80a2927 + b48555c commit 8ec8bb8
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions scripts/private/_sanity-check-universal.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ async function main() {
let dispatcherAddr;
let ucHandler;

if (sanityCheck) {
// If the sanity check fails, we don't need to continue with the rest of the checks
if (!sanityCheck) {
console.log(`
⛔ Sanity check failed for network ${networkName},
check if the values provided in the .env file for the Universal Channel Mw and the dispatcher are correct.
--------------------------------------------------
🔮 Expected Universal Channel Handler (in IBC contract): ${ucHandlerAddr}...
🗃️ Found Universal Channel Handler (in .env file): ${envUcHandlerAddr}...
--------------------------------------------------
`);
return;
} else {
try {
ucHandler = await getUcHandler(networkName);
dispatcherAddr = await ucHandler.dispatcher();
Expand All @@ -64,53 +75,63 @@ async function main() {
console.log(`❌ Error getting dispatcher address from Universal Channel Mw or from config: ${error}`);
return;
}
} else {
}

// If sanity check is false, log an error and return
if (!sanityCheck) {
console.log(`
⛔ Sanity check failed for network ${networkName},
check if the values provided in the .env file for the Universal Channel Mw and the dispatcher are correct.
--------------------------------------------------
🔮 Expected Universal Channel Handler (in IBC contract): ${ucHandlerAddr}...
🗃️ Found Universal Channel Handler (in .env file): ${envUcHandlerAddr}...
🔮 Expected Dispatcher (in Universal Channel Handler contract): ${dispatcherAddr}...
🗃️ Found Dispatcher (in .env file): ${envDispatcherAddr}...
--------------------------------------------------
`);
`);
return;
}
} else {
// If the sanity check passes, we can continue to check the channel ID stored in the Universal Channel Mw
let counter = 0;
let channelId, envChannelId;
let channelBytes;
let foundChannel = true;
// We don't know how many channels are connected to the Universal Channel Mw, so we loop until we get an error
do {
// Try to get the channel ID at the index
try {
channelBytes = await ucHandler.connectedChannels(counter);
} catch (error) {
// If we get an error, it means we reached the end of the list, do not return, just log the error and set foundChannel to false
// console.log(`❌ No channel ID at index: ${counter}`);
foundChannel = false;
}
counter++;
} while (foundChannel);

if (sanityCheck) {
const channelBytes = await ucHandler.connectedChannels(0);
const channelId = hre.ethers.decodeBytes32String(channelBytes);
const envChannelId = config['sendUniversalPacket'][networkName]['channelId'];
// channelBytes should be the last (populated) index in the connectedChannels array
channelId = hre.ethers.decodeBytes32String(channelBytes);
console.log(`Channel ID in UCH contract: ${channelId}`);
envChannelId = config['sendUniversalPacket'][networkName]['channelId'];

if (channelId !== envChannelId) {
// Compare the channel ID with the one in the .env file and log an error if they don't match
// Run only after we've encountered an error fetching a channel ID at a new index
if (channelId === undefined && channelId !== envChannelId) {
sanityCheck = false;
console.log(`
⛔ Sanity check failed for network ${networkName},
check if the channel id value for the Universal channel in the config is correct.
--------------------------------------------------
🔮 Expected Channel ID (in Universal Channel Handler contract): ${channelId}...
🗃️ Found Dispatcher (in .env file): ${envChannelId}...
🗃️ Found Channel ID (in config file): ${envChannelId}...
--------------------------------------------------
`);
`);
return;
}
}

// 5. Print the result of the sanity check
// If true, it means all values in the contracts check out with those in the .env file and we can continue with the script.
if (sanityCheck) {
console.log(`✅ Sanity check passed for network ${networkName}`);
} else {
console.log(`
⛔ Sanity check failed for network ${networkName},
check if the values provided in the .env file for the Universal Channel Mw and the dispatcher are correct.
--------------------------------------------------
🔮 Expected Dispatcher (in Universal Channel Handler contract): ${dispatcherAddr}...
🗃️ Found Dispatcher (in .env file): ${envDispatcherAddr}...
--------------------------------------------------
`);
}
console.log(`✅ Sanity check passed for network ${networkName}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
Expand Down

0 comments on commit 8ec8bb8

Please sign in to comment.