From 9116c861442fa5906f31d105d63a362ea8357336 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 1 Jul 2024 17:03:07 +0400 Subject: [PATCH 01/13] automate endpoint deploy --- cicd/.gitignore | 3 + cicd/application.js | 0 cicd/endpointandregister.js | 210 ++++++++++++++++++++++++++++++++++++ cicd/package.json | 7 ++ 4 files changed, 220 insertions(+) create mode 100644 cicd/.gitignore create mode 100644 cicd/application.js create mode 100644 cicd/endpointandregister.js create mode 100644 cicd/package.json diff --git a/cicd/.gitignore b/cicd/.gitignore new file mode 100644 index 0000000..6fe20f0 --- /dev/null +++ b/cicd/.gitignore @@ -0,0 +1,3 @@ +node_modules +.env +package-lock.json \ No newline at end of file diff --git a/cicd/application.js b/cicd/application.js new file mode 100644 index 0000000..e69de29 diff --git a/cicd/endpointandregister.js b/cicd/endpointandregister.js new file mode 100644 index 0000000..65b9345 --- /dev/null +++ b/cicd/endpointandregister.js @@ -0,0 +1,210 @@ +process.chdir(__dirname) +const { execSync } = require("child_process"); +const fs = require('node:fs'); +const env = require("dotenv").config(); +const config = {} +const endpointConfig = {} + +const { Web3 } = require('web3') + +main() + +async function main(){ + check() + // console.log(config) + await getNetworkID() + deployEndpoint() + configureEndpoint() + registerEndpoint() + // console.log(config) +} + +function configureEndpoint(){ + endpointConfig["xdcsubnet"]={ + "endpoint": config.subnetEndpoint, + "registers":[ + { + "csc": config.reverseCSC, + "endpoint": config.parentnetEndpoint, + "chainId": config.parentnetID + } + ] + } + endpointConfig["xdcparentnet"]={ + "endpoint": config.parentnetEndpoint, + "registers":[ + { + "csc": config.csc, + "endpoint": config.subnetEndpoint, + "chainId": config.subnetID + } + ] + } + + console.log("writing endpointconfig.json") + fs.writeFileSync('../endpoint/endpointconfig.json', JSON.stringify(endpointConfig, null, 2) , 'utf-8', err => { + if (err) { + throw Error("error writing endpointconfig.json, "+err) + } + }); +} + +function check(){ + if (process.env.PARENTNET_URL){ + parentnetURL = process.env.PARENTNET_URL + } else if (process.env.PARENTNET){ + parentnet = process.env.PARENTNET + if (parentnet == "devnet") parentnetURL = "https://devnetstats.apothem.network/devnet"; + if (parentnet == "testnet") parentnetURL = "https://devnetstats.apothem.network/testnet"; + if (parentnet == "mainnet") parentnetURL = "https://devnetstats.apothem.network/mainnet"; + } else { + throw Error("PARENTNET or PARENTNET_URL not found") + } + + const reqENV = [ + "SUBNET_PK", + "PARENTNET_PK", + "SUBNET_URL", + "CSC", + "REVERSE_CSC", + ]; + const isEnabled = reqENV.every(envVar => envVar in process.env) + if (!isEnabled){ + throw Error("incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_URL, CSC, REVERSE_CSC") + } + subnetPK = process.env.SUBNET_PK.startsWith("0x") ? process.env.SUBNET_PK : `0x${process.env.SUBNET_PK}`; + parentnetPK = process.env.PARENTNET_PK.startsWith("0x") ? process.env.PARENTNET_PK : `0x${process.env.PARENTNET_PK}`; + csc = process.env.CSC.startsWith("0x") ? process.env.CSC : `0x${process.env.CSC}`; + reverseCSC = process.env.REVERSE_CSC.startsWith("0x") ? process.env.REVERSE_CSC : `0x${process.env.REVERSE_CSC}`; + subnetURL = process.env.SUBNET_URL; + + // return subnetURL, parentnetURL, subnetPK, parentnetPK, csc, reverseCSC + config["subnetPK"] = subnetPK + config["parentnetPK"] = parentnetPK + config["subnetURL"] = subnetURL + config["parentnetURL"] = parentnetURL + config["csc"] = csc + config["reverseCSC"] = reverseCSC + return +} + +function writeEndpointEnv(key){ + content = "PRIVATE_KEY="+key + fs.writeFileSync('../endpoint/.env', content, err => { + if (err) { + throw Error("error writing endpoint env, "+err) + } + }); +} + +function deployEndpoint(){ + console.log("writing network config") + writeEndpointNetworkJson() + console.log("configuring PK") + writeEndpointEnv(config.subnetPK) + console.log("deploying subnet endpoint") + subnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcsubnet") + subnetZeroEndpoint = parseEndpointOutput(subnetEndpointOut) + + console.log("configuring PK") + writeEndpointEnv(config.parentnetPK) + console.log("deploying parentnet endpoint") + parentnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcparentnet") + parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) + + // return subnetZeroEndpoint, parentnetZeroEndpoint + config["subnetEndpoint"] = subnetZeroEndpoint + config["parentnetEndpoint"] = parentnetZeroEndpoint +} + +function registerEndpoint(){ + console.log("writing network config") + writeEndpointNetworkJson() + console.log("configuring PK") + writeEndpointEnv(config.subnetPK) + console.log("register parentnet to subnet endpoint") + subnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcsubnet") + // subnetZeroEndpoint = parseEndpointOutput(subnetEndpointOut) + + console.log("configuring PK") + writeEndpointEnv(config.parentnetPK) + console.log("register subnet to parentnet endpoint") + parentnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcparentnet") + // parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) + +} + +function writeEndpointNetworkJson(){ + networkJson = { + "xdcparentnet": config.parentnetURL, + "xdcsubnet": config.subnetURL, + } + fs.writeFileSync('../endpoint/network.config.json', JSON.stringify(networkJson, null, 2) , 'utf-8', err => { + if (err) { + throw Error("error writing network.config.json, "+err) + } + }); +} + +function parseEndpointOutput(outString){ + strArr = outString.split("\n") + lastLine = strArr[strArr.length-1] + if (lastLine == ''){ + strArr.pop() + lastLine = strArr[strArr.length-1] + } + if (lastLine.startsWith("XDCZeroEndpoint")){ + idx = lastLine.indexOf("0x") + address = lastLine.slice(idx, idx+42) + return address + } else { + throw Error("invalid output string: "+outString) + } +} + +function callExec(command){ + try{ + // const stdout = execSync(command,{timeout: 12000}) + const stdout = execSync(command) + output = stdout.toString() + // console.log(`${stdout}`); + console.log(output); + return output + } catch (error){ + if (error.code) { + // Spawning child process failed + if (error.code == "ETIMEDOUT"){ + throw Error("Timed out (120 seconds)") + } else { + throw Error(error) + } + } else { + // Child was spawned but exited with non-zero exit code + // Error contains any stdout and stderr from the child + // const { stdout, stderr } = error; + // console.error({ stdout, stderr }); + throw Error(error) + } + } +} + +async function getNetworkID(){ + console.log("getting chainID") + const sub3 = new Web3(config.subnetURL) + subID = await sub3.eth.getChainId() + subID = subID.toString() + console.log("subnet chain ID:", subID) + const parent3 = new Web3(config.parentnetURL) + parentID = await parent3.eth.getChainId() + parentID = parentID.toString() + console.log("parentnet chain ID:", parentID) + console.log() + + // return subID.toString(), parentID.toString() + config["subnetID"] = subID + config["parentnetID"] = parentID +} + +function clean(){ + +} \ No newline at end of file diff --git a/cicd/package.json b/cicd/package.json new file mode 100644 index 0000000..259008d --- /dev/null +++ b/cicd/package.json @@ -0,0 +1,7 @@ +{ + "name": "zero-cicd", + "dependencies": { + "web3": "^4.10.0", + "dotenv": "^16.3.1" + } +} From a6c3cc924b8f2b9c29575a1d3850a2d70a90fa1a Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Tue, 2 Jul 2024 17:49:41 +0400 Subject: [PATCH 02/13] refactor and add application register --- cicd/.gitignore | 3 +- cicd/applicationregister.js | 155 ++++++++++++++++++ ...egister.js => endpointandregisterchain.js} | 153 ++++++----------- cicd/package.json | 4 +- cicd/{application.js => subswap.js} | 0 cicd/util.js | 77 +++++++++ endpoint/endpointconfig.example.json | 2 +- 7 files changed, 290 insertions(+), 104 deletions(-) create mode 100755 cicd/applicationregister.js rename cicd/{endpointandregister.js => endpointandregisterchain.js} (61%) rename cicd/{application.js => subswap.js} (100%) create mode 100644 cicd/util.js diff --git a/cicd/.gitignore b/cicd/.gitignore index 6fe20f0..63b196a 100644 --- a/cicd/.gitignore +++ b/cicd/.gitignore @@ -1,3 +1,4 @@ node_modules .env -package-lock.json \ No newline at end of file +package-lock.json +mount/* \ No newline at end of file diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js new file mode 100755 index 0000000..e213e5b --- /dev/null +++ b/cicd/applicationregister.js @@ -0,0 +1,155 @@ +process.chdir(__dirname) +const { execSync } = require("child_process"); +const fs = require('node:fs'); +const env = require("dotenv").config({path: 'mount/.env'}); +const config = {} +const endpointConfig = {} + +const { ethers } = require('ethers'); +const u = require('./util.js') + + +main() + +async function main(){ + importEndpointJson() + initApplicationRegister() + await getNetworkID() + configureEndpointJson() + registerApplication() + exportEndpointJson() + +} + + +function importEndpointJson(){ + if (!fs.existsSync('./mount/endpointconfig.json')) throw Error("mount/endpointconfig.json not found") + + const epjs = JSON.parse(fs.readFileSync('./mount/endpointconfig.json', 'utf8')); + if (epjs.xdcsubnet.endpoint && epjs.xdcparentnet.endpoint){ + endpointConfig["xdcsubnet"] = epjs.xdcsubnet + endpointConfig["xdcparentnet"] = epjs.xdcparentnet + } else { + throw Error("incomplete endpoint config") + } +} + +function initApplicationRegister(){ + if (process.env.PARENTNET_URL){ + parentnetURL = process.env.PARENTNET_URL + } else if (process.env.PARENTNET){ + parentnet = process.env.PARENTNET + if (parentnet == "devnet") parentnetURL = "https://devnetstats.apothem.network/devnet"; + if (parentnet == "testnet") parentnetURL = "https://devnetstats.apothem.network/testnet"; + if (parentnet == "mainnet") parentnetURL = "https://devnetstats.apothem.network/mainnet"; + } else { + throw Error("PARENTNET or PARENTNET_URL not found") + } + + const reqENV = [ + "SUBNET_PK", + "PARENTNET_PK", + "SUBNET_APP", + "PARENTNET_APP", + "SUBNET_URL", + ]; + const isEnabled = reqENV.every(envVar => envVar in process.env) + if (!isEnabled){ + throw Error("incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_APP, PARENTNET_APP, SUBNET_URL") + } + subnetPK = process.env.SUBNET_PK.startsWith("0x") ? process.env.SUBNET_PK : `0x${process.env.SUBNET_PK}`; + parentnetPK = process.env.PARENTNET_PK.startsWith("0x") ? process.env.PARENTNET_PK : `0x${process.env.PARENTNET_PK}`; + subnetApp = process.env.SUBNET_APP.startsWith("0x") ? process.env.SUBNET_APP : `0x${process.env.SUBNET_APP}`; + parentnetApp = process.env.PARENTNET_APP.startsWith("0x") ? process.env.PARENTNET_APP : `0x${process.env.PARENTNET_APP}`; + subnetURL = process.env.SUBNET_URL; + + config["subnetPK"] = subnetPK + config["parentnetPK"] = parentnetPK + config["subnetURL"] = subnetURL + config["parentnetURL"] = parentnetURL + config["subnetApp"] = subnetApp + config["parentnetApp"] = parentnetApp + +} + +async function getNetworkID(){ + [subID, parentID] = await u.getNetworkID(config.subnetURL, config.parentnetURL) + + config["subnetID"] = subID + config["parentnetID"] = parentID +} +function writeEndpointNetworkJson(){ + u.writeEndpointNetworkJson(config.subnetURL, config.parentnetURL) +} + +function configureEndpointJson(){ + subApp = { + "rid": config.parentnetID, + "rua": config.parentnetApp, + "sua": config.subnetApp + } + parentApp = { + "rid": config.subnetID, + "rua": config.subnetApp, + "sua": config.parentnetApp, + } + + existingSubApps = endpointConfig.xdcsubnet.applications + existingParentApps = endpointConfig.xdcparentnet.applications + + if (!(Array.isArray(existingSubApps) && Array.isArray(existingParentApps))){ + endpointConfig.xdcsubnet.applications = [subApp] + endpointConfig.xdcparentnet.applications = [parentApp] + } else{ + subDupe = false + for (var i=0; i { + if (err) { + throw Error("error writing endpointconfig.json, "+err) + } + }); +} + +function registerApplication(){ + console.log("writing network config") + writeEndpointNetworkJson() + console.log("configuring PK") + u.writeEndpointEnv(config.subnetPK) + console.log("register parentnet application to subnet") + subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerapplications.js --network xdcsubnet") + if (!subnetEndpointOut.includes("success")) throw Error("failed to register parentnet app to subnet") + + console.log("configuring PK") + u.writeEndpointEnv(config.parentnetPK) + console.log("register subnet application to parentnet endpoint") + parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerapplications.js --network xdcparentnet") + if (!parentnetEndpointOut.includes("success")) throw Error("failed to register subnet app to parentnet") + +} + + +function exportEndpointJson(){ + fs.copyFileSync('../endpoint/endpointconfig.json', './mount/endpointconfig.json') + ep = fs.readFileSync('../endpoint/endpointconfig.json').toString() + console.log("SUCCESS register application, endpointconfig:") + console.log(ep) +} diff --git a/cicd/endpointandregister.js b/cicd/endpointandregisterchain.js similarity index 61% rename from cicd/endpointandregister.js rename to cicd/endpointandregisterchain.js index 65b9345..f1f84d1 100644 --- a/cicd/endpointandregister.js +++ b/cicd/endpointandregisterchain.js @@ -1,55 +1,25 @@ process.chdir(__dirname) const { execSync } = require("child_process"); const fs = require('node:fs'); -const env = require("dotenv").config(); +const env = require("dotenv").config({path: 'mount/.env'}); const config = {} const endpointConfig = {} -const { Web3 } = require('web3') +const { ethers } = require('ethers') +const u = require('./util.js') main() async function main(){ - check() - // console.log(config) + initEndpointDeploy() await getNetworkID() deployEndpoint() - configureEndpoint() + configureEndpointJson() registerEndpoint() - // console.log(config) + exportEndpointJson() } -function configureEndpoint(){ - endpointConfig["xdcsubnet"]={ - "endpoint": config.subnetEndpoint, - "registers":[ - { - "csc": config.reverseCSC, - "endpoint": config.parentnetEndpoint, - "chainId": config.parentnetID - } - ] - } - endpointConfig["xdcparentnet"]={ - "endpoint": config.parentnetEndpoint, - "registers":[ - { - "csc": config.csc, - "endpoint": config.subnetEndpoint, - "chainId": config.subnetID - } - ] - } - - console.log("writing endpointconfig.json") - fs.writeFileSync('../endpoint/endpointconfig.json', JSON.stringify(endpointConfig, null, 2) , 'utf-8', err => { - if (err) { - throw Error("error writing endpointconfig.json, "+err) - } - }); -} - -function check(){ +function initEndpointDeploy(){ if (process.env.PARENTNET_URL){ parentnetURL = process.env.PARENTNET_URL } else if (process.env.PARENTNET){ @@ -85,31 +55,61 @@ function check(){ config["parentnetURL"] = parentnetURL config["csc"] = csc config["reverseCSC"] = reverseCSC - return + } -function writeEndpointEnv(key){ - content = "PRIVATE_KEY="+key - fs.writeFileSync('../endpoint/.env', content, err => { +function configureEndpointJson(){ + endpointConfig["xdcsubnet"]={ + "endpoint": config.subnetEndpoint, + "registers":[ + { + "csc": config.reverseCSC, + "endpoint": config.parentnetEndpoint, + "chainId": config.parentnetID + } + ], + "applications":[] + } + endpointConfig["xdcparentnet"]={ + "endpoint": config.parentnetEndpoint, + "registers":[ + { + "csc": config.csc, + "endpoint": config.subnetEndpoint, + "chainId": config.subnetID + } + ], + "applications":[] + } + + console.log("writing endpointconfig.json") + fs.writeFileSync('../endpoint/endpointconfig.json', JSON.stringify(endpointConfig, null, 2) , 'utf-8', err => { if (err) { - throw Error("error writing endpoint env, "+err) - } + throw Error("error writing endpointconfig.json, "+err) + } }); } +function exportEndpointJson(){ + fs.copyFileSync('../endpoint/endpointconfig.json', './mount/endpointconfig.json') + ep = fs.readFileSync('../endpoint/endpointconfig.json').toString() + console.log("SUCCESS deploy endpoint and register chain, endpointconfig:") + console.log(ep) +} + function deployEndpoint(){ console.log("writing network config") writeEndpointNetworkJson() console.log("configuring PK") - writeEndpointEnv(config.subnetPK) + u.writeEndpointEnv(config.subnetPK) console.log("deploying subnet endpoint") - subnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcsubnet") + subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcsubnet") subnetZeroEndpoint = parseEndpointOutput(subnetEndpointOut) console.log("configuring PK") - writeEndpointEnv(config.parentnetPK) + u.writeEndpointEnv(config.parentnetPK) console.log("deploying parentnet endpoint") - parentnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcparentnet") + parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcparentnet") parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) // return subnetZeroEndpoint, parentnetZeroEndpoint @@ -121,29 +121,21 @@ function registerEndpoint(){ console.log("writing network config") writeEndpointNetworkJson() console.log("configuring PK") - writeEndpointEnv(config.subnetPK) + u.writeEndpointEnv(config.subnetPK) console.log("register parentnet to subnet endpoint") - subnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcsubnet") + subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcsubnet") // subnetZeroEndpoint = parseEndpointOutput(subnetEndpointOut) console.log("configuring PK") - writeEndpointEnv(config.parentnetPK) + u.writeEndpointEnv(config.parentnetPK) console.log("register subnet to parentnet endpoint") - parentnetEndpointOut = callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcparentnet") + parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcparentnet") // parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) } function writeEndpointNetworkJson(){ - networkJson = { - "xdcparentnet": config.parentnetURL, - "xdcsubnet": config.subnetURL, - } - fs.writeFileSync('../endpoint/network.config.json', JSON.stringify(networkJson, null, 2) , 'utf-8', err => { - if (err) { - throw Error("error writing network.config.json, "+err) - } - }); + u.writeEndpointNetworkJson(config.subnetURL, config.parentnetURL) } function parseEndpointOutput(outString){ @@ -162,49 +154,10 @@ function parseEndpointOutput(outString){ } } -function callExec(command){ - try{ - // const stdout = execSync(command,{timeout: 12000}) - const stdout = execSync(command) - output = stdout.toString() - // console.log(`${stdout}`); - console.log(output); - return output - } catch (error){ - if (error.code) { - // Spawning child process failed - if (error.code == "ETIMEDOUT"){ - throw Error("Timed out (120 seconds)") - } else { - throw Error(error) - } - } else { - // Child was spawned but exited with non-zero exit code - // Error contains any stdout and stderr from the child - // const { stdout, stderr } = error; - // console.error({ stdout, stderr }); - throw Error(error) - } - } -} - async function getNetworkID(){ - console.log("getting chainID") - const sub3 = new Web3(config.subnetURL) - subID = await sub3.eth.getChainId() - subID = subID.toString() - console.log("subnet chain ID:", subID) - const parent3 = new Web3(config.parentnetURL) - parentID = await parent3.eth.getChainId() - parentID = parentID.toString() - console.log("parentnet chain ID:", parentID) - console.log() + [subID, parentID] = await u.getNetworkID(config.subnetURL, config.parentnetURL) - // return subID.toString(), parentID.toString() config["subnetID"] = subID config["parentnetID"] = parentID } -function clean(){ - -} \ No newline at end of file diff --git a/cicd/package.json b/cicd/package.json index 259008d..2db1dac 100644 --- a/cicd/package.json +++ b/cicd/package.json @@ -1,7 +1,7 @@ { "name": "zero-cicd", "dependencies": { - "web3": "^4.10.0", - "dotenv": "^16.3.1" + "dotenv": "^16.3.1", + "ethers": "^5.7.2" } } diff --git a/cicd/application.js b/cicd/subswap.js similarity index 100% rename from cicd/application.js rename to cicd/subswap.js diff --git a/cicd/util.js b/cicd/util.js new file mode 100644 index 0000000..2207365 --- /dev/null +++ b/cicd/util.js @@ -0,0 +1,77 @@ +process.chdir(__dirname) +const { execSync } = require("child_process"); +const fs = require('node:fs'); +const env = require("dotenv").config({path: 'mount/.env'}); + +const { ethers } = require('ethers') + + +function writeEndpointEnv(key){ + content = "PRIVATE_KEY="+key + fs.writeFileSync('../endpoint/.env', content, err => { + if (err) { + throw Error("error writing endpoint env, "+err) + } + }); +} + +function writeEndpointNetworkJson(subnetURL, parentnetURL){ + networkJson = { + "xdcsubnet": subnetURL, + "xdcparentnet": parentnetURL, + } + fs.writeFileSync('../endpoint/network.config.json', JSON.stringify(networkJson, null, 2) , 'utf-8', err => { + if (err) { + throw Error("error writing network.config.json, "+err) + } + }); +} + +function callExec(command){ + try{ + // const stdout = execSync(command,{timeout: 12000}) + const stdout = execSync(command) + output = stdout.toString() + // console.log(`${stdout}`); + console.log(output); + return output + } catch (error){ + if (error.code) { + // Spawning child process failed + if (error.code == "ETIMEDOUT"){ + throw Error("Timed out (120 seconds)") + } else { + throw Error(error) + } + } else { + // Child was spawned but exited with non-zero exit code + // Error contains any stdout and stderr from the child + // const { stdout, stderr } = error; + // console.error({ stdout, stderr }); + throw Error(error) + } + } +} + +async function getNetworkID(subnetURL, parentnetURL){ + console.log("getting chain ID") + const subRPC = new ethers.providers.JsonRpcProvider(subnetURL) + subID = await subRPC.getNetwork() + subID = subID.chainId.toString() + console.log("subnet chain ID:", subID) + + const parentRPC = new ethers.providers.JsonRpcProvider(parentnetURL) + parentID = await parentRPC.getNetwork() + parentID = parentID.chainId.toString() + console.log("parentnet chain ID:", parentID) + console.log() + + return [subID, parentID] +} + +module.exports = { + getNetworkID, + writeEndpointEnv, + writeEndpointNetworkJson, + callExec, +} \ No newline at end of file diff --git a/endpoint/endpointconfig.example.json b/endpoint/endpointconfig.example.json index ecfbd51..b6f9547 100644 --- a/endpoint/endpointconfig.example.json +++ b/endpoint/endpointconfig.example.json @@ -20,7 +20,7 @@ ], "applications": [ { - "rid": "8851", + "rid": "551", "rua": "0x5dff28627b2E4fd9516d18db6713c90d80d50f98", "sua": "0x5dff28627b2E4fd9516d18db6713c90d80d50f98" } From d0168886ee9f372f56123c4390bc9b33b58bec82 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Tue, 2 Jul 2024 17:51:13 +0400 Subject: [PATCH 03/13] add placeholder to make folder exist --- cicd/.gitignore | 3 ++- cicd/mount/placeholder.txt | 0 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 cicd/mount/placeholder.txt diff --git a/cicd/.gitignore b/cicd/.gitignore index 63b196a..3960447 100644 --- a/cicd/.gitignore +++ b/cicd/.gitignore @@ -1,4 +1,5 @@ node_modules .env package-lock.json -mount/* \ No newline at end of file +mount/* +!mount/placeholder.txt \ No newline at end of file diff --git a/cicd/mount/placeholder.txt b/cicd/mount/placeholder.txt new file mode 100644 index 0000000..e69de29 From d067ea3f05de560200f91a53d624ffa108694976 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Tue, 2 Jul 2024 18:09:34 +0400 Subject: [PATCH 04/13] support case without endpointconfig.json using env instead --- cicd/applicationregister.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index e213e5b..041cc49 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -23,14 +23,30 @@ async function main(){ function importEndpointJson(){ - if (!fs.existsSync('./mount/endpointconfig.json')) throw Error("mount/endpointconfig.json not found") + if (!fs.existsSync('./mount/endpointconfig.json')){ + if (process.env.SUBNET_ZERO_CONTRACT && process.env.PARENTNET_ZERO_CONTRACT){ + config.subnetEndpoint = process.env.SUBNET_ZERO_CONTRACT + config.parentnetEndpoint = process.env.PARENTNET_ZERO_CONTRACT + } else { + throw Error("mount/endpointconfig.json not found, and SUBNET_ZERO_CONTRACT and PARENTNET_ZERO_CONTRACT are not configured") + } + endpointConfig["xdcsubnet"]={ + "endpoint": config.subnetEndpoint, + "applications":[] + } + endpointConfig["xdcparentnet"]={ + "endpoint": config.parentnetEndpoint, + "applications":[] + } + return + } const epjs = JSON.parse(fs.readFileSync('./mount/endpointconfig.json', 'utf8')); if (epjs.xdcsubnet.endpoint && epjs.xdcparentnet.endpoint){ endpointConfig["xdcsubnet"] = epjs.xdcsubnet endpointConfig["xdcparentnet"] = epjs.xdcparentnet } else { - throw Error("incomplete endpoint config") + throw Error("incorrect endpointconfig.json format") } } From 2e89b972e2cc61884258b29fd358a5abca957a99 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Tue, 2 Jul 2024 19:21:22 +0400 Subject: [PATCH 05/13] add subswap deploy --- cicd/applicationregister.js | 2 +- cicd/endpointandregisterchain.js | 5 +- cicd/subswap.js | 126 +++++++++++++++++++++++++++++++ cicd/util.js | 24 ++++++ 4 files changed, 153 insertions(+), 4 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 041cc49..a692457 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -46,7 +46,7 @@ function importEndpointJson(){ endpointConfig["xdcsubnet"] = epjs.xdcsubnet endpointConfig["xdcparentnet"] = epjs.xdcparentnet } else { - throw Error("incorrect endpointconfig.json format") + throw Error("invalid endpointconfig.json format") } } diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index f1f84d1..2a53d7a 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -112,7 +112,6 @@ function deployEndpoint(){ parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcparentnet") parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) - // return subnetZeroEndpoint, parentnetZeroEndpoint config["subnetEndpoint"] = subnetZeroEndpoint config["parentnetEndpoint"] = parentnetZeroEndpoint } @@ -124,13 +123,13 @@ function registerEndpoint(){ u.writeEndpointEnv(config.subnetPK) console.log("register parentnet to subnet endpoint") subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcsubnet") - // subnetZeroEndpoint = parseEndpointOutput(subnetEndpointOut) + if (!subnetEndpointOut.includes("success")) throw Error("failed to register parentnet endpoint to subnet") console.log("configuring PK") u.writeEndpointEnv(config.parentnetPK) console.log("register subnet to parentnet endpoint") parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcparentnet") - // parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) + if (!parentnetEndpointOut.includes("success")) throw Error("failed to register subnet endpoint to parentnet") } diff --git a/cicd/subswap.js b/cicd/subswap.js index e69de29..b49250a 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -0,0 +1,126 @@ +process.chdir(__dirname) +const { execSync } = require("child_process"); +const fs = require('node:fs'); +const env = require("dotenv").config({path: 'mount/.env'}); +const config = {} +const endpointConfig = {} + +const { ethers } = require('ethers') +const u = require('./util.js') + +main() + +async function main(){ + checkEndpointConfig() + initSubswapDeploy() + deploySubswap() + exportSubswap() +} +function checkEndpointConfig(){ + if (!fs.existsSync('./mount/endpointconfig.json')){ + if (process.env.SUBNET_ZERO_CONTRACT && process.env.PARENTNET_ZERO_CONTRACT){ + config.subnetEndpoint = process.env.SUBNET_ZERO_CONTRACT + config.parentnetEndpoint = process.env.PARENTNET_ZERO_CONTRACT + return + } + throw Error("mount/endpointconfig.json not found, and SUBNET_ZERO_CONTRACT and PARENTNET_ZERO_CONTRACT are not configured") + } + + const epjs = JSON.parse(fs.readFileSync('./mount/endpointconfig.json', 'utf8')); + if (epjs.xdcsubnet.endpoint && epjs.xdcparentnet.endpoint){ + config.subnetEndpoint = epjs.xdcsubnet.endpoint + config.parentnetEndpoint = epjs.xdcparentnet.endpoint + return + } + throw Error("endpoints not found in mount/endpointconfig.json") +} + + +function initSubswapDeploy(){ + if (process.env.PARENTNET_URL){ + parentnetURL = process.env.PARENTNET_URL + } else if (process.env.PARENTNET){ + parentnet = process.env.PARENTNET + if (parentnet == "devnet") parentnetURL = "https://devnetstats.apothem.network/devnet"; + if (parentnet == "testnet") parentnetURL = "https://devnetstats.apothem.network/testnet"; + if (parentnet == "mainnet") parentnetURL = "https://devnetstats.apothem.network/mainnet"; + } else { + throw Error("PARENTNET or PARENTNET_URL not found") + } + + const reqENV = [ + "SUBNET_PK", + "PARENTNET_PK", + "SUBNET_URL", + ]; + const isEnabled = reqENV.every(envVar => envVar in process.env) + if (!isEnabled){ + throw Error("incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_URL") + } + subnetPK = process.env.SUBNET_PK.startsWith("0x") ? process.env.SUBNET_PK : `0x${process.env.SUBNET_PK}`; + parentnetPK = process.env.PARENTNET_PK.startsWith("0x") ? process.env.PARENTNET_PK : `0x${process.env.PARENTNET_PK}`; + subnetURL = process.env.SUBNET_URL; + + config["subnetPK"] = subnetPK + config["parentnetPK"] = parentnetPK + config["subnetURL"] = subnetURL + config["parentnetURL"] = parentnetURL +} + +function deploySubswap(){ + console.log("writing network config") + u.writeSubswapNetworkJson(config.subnetURL, config.parentnetURL) + console.log("writing deploy.config.json") + writeSubswapDeployJson() + + console.log("configuring PK") + u.writeSubswapEnv(config.subnetPK) + console.log("deploying subswap on subnet") + subnetEndpointOut = u.callExec("cd ../applications/subswap/contract; npx hardhat run scripts/subnettreasurydeploy.js --network xdcsubnet") + subnetSubswapAddr = parseEndpointOutput(subnetEndpointOut) + + console.log("configuring PK") + u.writeSubswapEnv(config.parentnetPK) + console.log("deploying subswap on parentnet") + parentnetEndpointOut = u.callExec("cd ../applications/subswap/contract; npx hardhat run scripts/subnettreasurydeploy.js --network xdcparentnet") + parentnetSubswapAddr = parseEndpointOutput(parentnetEndpointOut) + + config["subnetSubswap"] = subnetSubswapAddr + config["parentnetSubswap"] = parentnetSubswapAddr +} + +function exportSubswap(){ + finalSubnet = "SUBNET_APP="+config.subnetSubswap + finalParentnet = "PARENTNET_APP="+config.parentnetSubswap + + console.log("SUCCESS deploy subswap. Before register application step, please include the following into your .env ") + console.log(finalSubnet) + console.log(finalParentnet) +} +function writeSubswapDeployJson(){ + deployJson = { + "subnetendpoint": config.subnetEndpoint, + "parentnetendpoint": config.parentnetEndpoint, + } + fs.writeFileSync('../applications/subswap/contract/deploy.config.json', JSON.stringify(deployJson, null, 2) , 'utf-8', err => { + if (err) { + throw Error("error writing deploy.config.json, "+err) + } + }); +} + +function parseEndpointOutput(outString){ + strArr = outString.split("\n") + lastLine = strArr[strArr.length-1] + if (lastLine == ''){ + strArr.pop() + lastLine = strArr[strArr.length-1] + } + if (lastLine.includes("0x")){ + idx = lastLine.indexOf("0x") + address = lastLine.slice(idx, idx+42) + return address + } else { + throw Error("invalid output string: "+outString) + } +} \ No newline at end of file diff --git a/cicd/util.js b/cicd/util.js index 2207365..bfcc5e5 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -15,6 +15,15 @@ function writeEndpointEnv(key){ }); } +function writeSubswapEnv(key){ //refactor + content = "PRIVATE_KEY="+key + fs.writeFileSync('../applications/subswap/contract/.env', content, err => { + if (err) { + throw Error("error writing endpoint env, "+err) + } + }); +} + function writeEndpointNetworkJson(subnetURL, parentnetURL){ networkJson = { "xdcsubnet": subnetURL, @@ -27,6 +36,19 @@ function writeEndpointNetworkJson(subnetURL, parentnetURL){ }); } +function writeSubswapNetworkJson(subnetURL, parentnetURL){ //refactor + networkJson = { + "xdcsubnet": subnetURL, + "xdcparentnet": parentnetURL, + } + fs.writeFileSync('../applications/subswap/contract/network.config.json', JSON.stringify(networkJson, null, 2) , 'utf-8', err => { + if (err) { + throw Error("error writing network.config.json, "+err) + } + }); +} + + function callExec(command){ try{ // const stdout = execSync(command,{timeout: 12000}) @@ -72,6 +94,8 @@ async function getNetworkID(subnetURL, parentnetURL){ module.exports = { getNetworkID, writeEndpointEnv, + writeSubswapEnv, writeEndpointNetworkJson, + writeSubswapNetworkJson, callExec, } \ No newline at end of file From 6744663c25e28d83f9d1717362939a62a9de5019 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 3 Jul 2024 03:42:47 +0400 Subject: [PATCH 06/13] refactor clean code --- cicd/applicationregister.js | 22 ++++-------- cicd/endpointandregisterchain.js | 28 ++++++--------- cicd/subswap.js | 11 +++--- cicd/util.js | 61 ++++++++++++-------------------- 4 files changed, 46 insertions(+), 76 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index a692457..36a515b 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -2,7 +2,9 @@ process.chdir(__dirname) const { execSync } = require("child_process"); const fs = require('node:fs'); const env = require("dotenv").config({path: 'mount/.env'}); -const config = {} +const config = { + "relativePath": "../endpoint/" +} const endpointConfig = {} const { ethers } = require('ethers'); @@ -14,7 +16,7 @@ main() async function main(){ importEndpointJson() initApplicationRegister() - await getNetworkID() + await u.getNetworkID(config) configureEndpointJson() registerApplication() exportEndpointJson() @@ -88,16 +90,6 @@ function initApplicationRegister(){ } -async function getNetworkID(){ - [subID, parentID] = await u.getNetworkID(config.subnetURL, config.parentnetURL) - - config["subnetID"] = subID - config["parentnetID"] = parentID -} -function writeEndpointNetworkJson(){ - u.writeEndpointNetworkJson(config.subnetURL, config.parentnetURL) -} - function configureEndpointJson(){ subApp = { "rid": config.parentnetID, @@ -147,15 +139,15 @@ function configureEndpointJson(){ function registerApplication(){ console.log("writing network config") - writeEndpointNetworkJson() + u.writeNetworkJson(config) console.log("configuring PK") - u.writeEndpointEnv(config.subnetPK) + u.writeEnv(config.subnetPK, config.relativePath) console.log("register parentnet application to subnet") subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerapplications.js --network xdcsubnet") if (!subnetEndpointOut.includes("success")) throw Error("failed to register parentnet app to subnet") console.log("configuring PK") - u.writeEndpointEnv(config.parentnetPK) + u.writeEnv(config.parentnetPK, config.relativePath) console.log("register subnet application to parentnet endpoint") parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerapplications.js --network xdcparentnet") if (!parentnetEndpointOut.includes("success")) throw Error("failed to register subnet app to parentnet") diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index 2a53d7a..2047477 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -2,7 +2,9 @@ process.chdir(__dirname) const { execSync } = require("child_process"); const fs = require('node:fs'); const env = require("dotenv").config({path: 'mount/.env'}); -const config = {} +const config = { + "relativePath": "../endpoint/" +} const endpointConfig = {} const { ethers } = require('ethers') @@ -12,7 +14,7 @@ main() async function main(){ initEndpointDeploy() - await getNetworkID() + await u.getNetworkID(config) deployEndpoint() configureEndpointJson() registerEndpoint() @@ -99,15 +101,15 @@ function exportEndpointJson(){ function deployEndpoint(){ console.log("writing network config") - writeEndpointNetworkJson() + u.writeNetworkJson(config) console.log("configuring PK") - u.writeEndpointEnv(config.subnetPK) + u.writeEnv(config.subnetPK, config.relativePath) console.log("deploying subnet endpoint") subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcsubnet") subnetZeroEndpoint = parseEndpointOutput(subnetEndpointOut) console.log("configuring PK") - u.writeEndpointEnv(config.parentnetPK) + u.writeEnv(config.parentnetPK, config.relativePath) console.log("deploying parentnet endpoint") parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/endpointdeploy.js --network xdcparentnet") parentnetZeroEndpoint = parseEndpointOutput(parentnetEndpointOut) @@ -118,25 +120,21 @@ function deployEndpoint(){ function registerEndpoint(){ console.log("writing network config") - writeEndpointNetworkJson() + u.writeNetworkJson(config) console.log("configuring PK") - u.writeEndpointEnv(config.subnetPK) + u.writeEnv(config.subnetPK, config.relativePath) console.log("register parentnet to subnet endpoint") subnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcsubnet") if (!subnetEndpointOut.includes("success")) throw Error("failed to register parentnet endpoint to subnet") console.log("configuring PK") - u.writeEndpointEnv(config.parentnetPK) + u.writeEnv(config.parentnetPK, config.relativePath) console.log("register subnet to parentnet endpoint") parentnetEndpointOut = u.callExec("cd ../endpoint; npx hardhat run scripts/registerchain.js --network xdcparentnet") if (!parentnetEndpointOut.includes("success")) throw Error("failed to register subnet endpoint to parentnet") } -function writeEndpointNetworkJson(){ - u.writeEndpointNetworkJson(config.subnetURL, config.parentnetURL) -} - function parseEndpointOutput(outString){ strArr = outString.split("\n") lastLine = strArr[strArr.length-1] @@ -153,10 +151,4 @@ function parseEndpointOutput(outString){ } } -async function getNetworkID(){ - [subID, parentID] = await u.getNetworkID(config.subnetURL, config.parentnetURL) - - config["subnetID"] = subID - config["parentnetID"] = parentID -} diff --git a/cicd/subswap.js b/cicd/subswap.js index b49250a..9b29c38 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -2,7 +2,9 @@ process.chdir(__dirname) const { execSync } = require("child_process"); const fs = require('node:fs'); const env = require("dotenv").config({path: 'mount/.env'}); -const config = {} +const config = { + "relativePath": "../applications/subswap/contract/" +} const endpointConfig = {} const { ethers } = require('ethers') @@ -16,6 +18,7 @@ async function main(){ deploySubswap() exportSubswap() } + function checkEndpointConfig(){ if (!fs.existsSync('./mount/endpointconfig.json')){ if (process.env.SUBNET_ZERO_CONTRACT && process.env.PARENTNET_ZERO_CONTRACT){ @@ -69,18 +72,18 @@ function initSubswapDeploy(){ function deploySubswap(){ console.log("writing network config") - u.writeSubswapNetworkJson(config.subnetURL, config.parentnetURL) + u.writeNetworkJson(config) console.log("writing deploy.config.json") writeSubswapDeployJson() console.log("configuring PK") - u.writeSubswapEnv(config.subnetPK) + u.writeEnv(config.subnetPK, config.relativePath) console.log("deploying subswap on subnet") subnetEndpointOut = u.callExec("cd ../applications/subswap/contract; npx hardhat run scripts/subnettreasurydeploy.js --network xdcsubnet") subnetSubswapAddr = parseEndpointOutput(subnetEndpointOut) console.log("configuring PK") - u.writeSubswapEnv(config.parentnetPK) + u.writeEnv(config.parentnetPK, config.relativePath) console.log("deploying subswap on parentnet") parentnetEndpointOut = u.callExec("cd ../applications/subswap/contract; npx hardhat run scripts/subnettreasurydeploy.js --network xdcparentnet") parentnetSubswapAddr = parseEndpointOutput(parentnetEndpointOut) diff --git a/cicd/util.js b/cicd/util.js index bfcc5e5..8a7a4ae 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -5,54 +5,36 @@ const env = require("dotenv").config({path: 'mount/.env'}); const { ethers } = require('ethers') - -function writeEndpointEnv(key){ - content = "PRIVATE_KEY="+key - fs.writeFileSync('../endpoint/.env', content, err => { - if (err) { - throw Error("error writing endpoint env, "+err) - } - }); -} - -function writeSubswapEnv(key){ //refactor +function writeEnv(key, path){ content = "PRIVATE_KEY="+key - fs.writeFileSync('../applications/subswap/contract/.env', content, err => { + fullPath = path+"/"+".env" + fs.writeFileSync(fullPath, content, err => { if (err) { - throw Error("error writing endpoint env, "+err) + throw Error(`error writing ${fullPath}, `+err) } }); } - -function writeEndpointNetworkJson(subnetURL, parentnetURL){ +function writeNetworkJson(config){ networkJson = { - "xdcsubnet": subnetURL, - "xdcparentnet": parentnetURL, + "xdcsubnet": config.subnetURL, + "xdcparentnet": config.parentnetURL, } - fs.writeFileSync('../endpoint/network.config.json', JSON.stringify(networkJson, null, 2) , 'utf-8', err => { - if (err) { - throw Error("error writing network.config.json, "+err) - } - }); + writeJson(networkJson, config.relativePath, "network.config.json") } -function writeSubswapNetworkJson(subnetURL, parentnetURL){ //refactor - networkJson = { - "xdcsubnet": subnetURL, - "xdcparentnet": parentnetURL, - } - fs.writeFileSync('../applications/subswap/contract/network.config.json', JSON.stringify(networkJson, null, 2) , 'utf-8', err => { +function writeJson(obj, path, filename){ + fullPath = path+"/"+filename + fs.writeFileSync(fullPath, JSON.stringify(obj, null, 2) , 'utf-8', err => { if (err) { - throw Error("error writing network.config.json, "+err) + throw Error(`error writing ${fullPath}, `+err) } }); } - function callExec(command){ try{ - // const stdout = execSync(command,{timeout: 12000}) - const stdout = execSync(command) + const stdout = execSync(command,{timeout: 200000}) + // const stdout = execSync(command) output = stdout.toString() // console.log(`${stdout}`); console.log(output); @@ -61,7 +43,7 @@ function callExec(command){ if (error.code) { // Spawning child process failed if (error.code == "ETIMEDOUT"){ - throw Error("Timed out (120 seconds)") + throw Error("Timed out (200 seconds)") } else { throw Error(error) } @@ -75,7 +57,9 @@ function callExec(command){ } } -async function getNetworkID(subnetURL, parentnetURL){ +async function getNetworkID(config){ + subnetURL = config.subnetURL + parentnetURL = config.parentnetURL console.log("getting chain ID") const subRPC = new ethers.providers.JsonRpcProvider(subnetURL) subID = await subRPC.getNetwork() @@ -88,14 +72,13 @@ async function getNetworkID(subnetURL, parentnetURL){ console.log("parentnet chain ID:", parentID) console.log() - return [subID, parentID] + config["subnetID"] = subID + config["parentnetID"] = parentID } module.exports = { getNetworkID, - writeEndpointEnv, - writeSubswapEnv, - writeEndpointNetworkJson, - writeSubswapNetworkJson, callExec, + writeEnv, + writeNetworkJson, } \ No newline at end of file From f991f54d89c9ca3f4ba28e5f754747916f61459e Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 3 Jul 2024 17:08:30 +0400 Subject: [PATCH 07/13] add dockerfile readme --- cicd/.env.example | 19 ++++++++++++++++++ cicd/Dockerfile | 14 +++++++++++++ cicd/Dockerfile.dockerignore | 2 ++ cicd/README.md | 34 ++++++++++++++++++++++++++++++++ cicd/applicationregister.js | 1 + cicd/endpointandregisterchain.js | 5 +++++ cicd/subswap.js | 1 + 7 files changed, 76 insertions(+) create mode 100644 cicd/.env.example create mode 100644 cicd/Dockerfile create mode 100644 cicd/Dockerfile.dockerignore create mode 100644 cicd/README.md diff --git a/cicd/.env.example b/cicd/.env.example new file mode 100644 index 0000000..d3705e1 --- /dev/null +++ b/cicd/.env.example @@ -0,0 +1,19 @@ + +SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111 +PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222 +CSC=0x3333333333333333333333333333333333333333 +REVERSE_CSC=0x4444444444444444444444444444444444444444 + + +SUBNET_URL=https://devnetstats.apothem.network/subnet/ +# PARENTNET can be devnet,testnet,mainnet +PARENTNET=devnet +# custom PARENTNET_URL can be used instead of PARENTNET +# PARENTNET_URL= + + +# SUBNET_ZERO_CONTRACT= +# PARENTNET_ZERO_CONTRACT= + +# SUBNET_APP= +# PARENTNET_APP= \ No newline at end of file diff --git a/cicd/Dockerfile b/cicd/Dockerfile new file mode 100644 index 0000000..4ddb818 --- /dev/null +++ b/cicd/Dockerfile @@ -0,0 +1,14 @@ +FROM node:20-alpine + +COPY . /app + +WORKDIR /app/cicd +RUN yarn +WORKDIR /app/endpoint +RUN yarn +WORKDIR /app/applications/subswap/contract +RUN yarn + +WORKDIR /app/cicd + +ENTRYPOINT ["node"] \ No newline at end of file diff --git a/cicd/Dockerfile.dockerignore b/cicd/Dockerfile.dockerignore new file mode 100644 index 0000000..64a4213 --- /dev/null +++ b/cicd/Dockerfile.dockerignore @@ -0,0 +1,2 @@ +**node_modules +**package-lock.json \ No newline at end of file diff --git a/cicd/README.md b/cicd/README.md new file mode 100644 index 0000000..30b44bf --- /dev/null +++ b/cicd/README.md @@ -0,0 +1,34 @@ +1. create a .env like the following example +``` +PARENTNET=devnet +SUBNET_URL= +SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111 +PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222 +CSC=0x3333333333333333333333333333333333333333 +REVERSE_CSC=0x4444444444444444444444444444444444444444 +``` + +2. Deploy XDC-Zero endpoints and register chain +``` +docker run --env-file .env xinfinorg/xdc-zero:latest endpointandregisterchain.js +``` +- add the output to .env +``` +SUBNET_ZERO_CONTRACT=0x5555555555555555555555555555555555555555 +PARENTNET_ZERO_CONTRACT=0x6666666666666666666666666666666666666666 +``` + +3. Deploy Subswap +``` +docker run --env-file .env xinfinorg/xdc-zero:latest subswap.js +``` +- add the output to .env +``` +SUBNET_APP=0x7777777777777777777777777777777777777777 +PARENTNET_APP=0x8888888888888888888888888888888888888888 +``` + +4. Register Subswap to XDC-Zero +``` +docker run --env-file .env xinfinorg/xdc-zero:latest applicationregister.js +``` diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 36a515b..6be2fa7 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -14,6 +14,7 @@ const u = require('./util.js') main() async function main(){ + console.log("start application register") importEndpointJson() initApplicationRegister() await u.getNetworkID(config) diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index 2047477..58074bb 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -13,6 +13,7 @@ const u = require('./util.js') main() async function main(){ + console.log("start endpoint deploy and register chain") initEndpointDeploy() await u.getNetworkID(config) deployEndpoint() @@ -97,6 +98,10 @@ function exportEndpointJson(){ ep = fs.readFileSync('../endpoint/endpointconfig.json').toString() console.log("SUCCESS deploy endpoint and register chain, endpointconfig:") console.log(ep) + console.log() + console.log("SUCCESS deploy endpoint and register chain, env:") + console.log("SUBNET_ZERO_CONTRACT="+config.subnetEndpoint) + console.log("PARENTNET_ZERO_CONTRACT="+config.parentnetEndpoint) } function deployEndpoint(){ diff --git a/cicd/subswap.js b/cicd/subswap.js index 9b29c38..c030e9e 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -13,6 +13,7 @@ const u = require('./util.js') main() async function main(){ + console.log("start subswap deploy") checkEndpointConfig() initSubswapDeploy() deploySubswap() From 0a76a8d8e8d75b54aa23553e7e0c441d32ec67ce Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 3 Jul 2024 17:25:03 +0400 Subject: [PATCH 08/13] add workflow change trigger use main change trigger try ff again ff state Update endpointconfig.example.json --- .github/workflows/ci.yml | 41 +++++++++++++++++++++++ .github/workflows/pr_build.yml | 50 ++++++++++++++++++++++++++++ endpoint/endpointconfig.example.json | 25 +++++++++----- 3 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/pr_build.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..b88e541 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +name: Build and publish image +on: + push: + branches: + - main + tags: + - '*' + +jobs: + test_build_and_push_to_docker_registry: + name: Test, build and publish image to docker hub + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + # - uses: actions/setup-node@v3 + # with: + # node-version: '20.x' + # - name: Install dependencies + # run: npm install && npm run postinstall + # - name: Unit tests + # run: npm run test + - name: Docker login + env: + DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}} + DOCKER_PASSWORD: ${{secrets.DOCKER_ACCESS_TOKEN}} + run: | + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + + - name: Determine Docker Image Name + id: image + run: | + if [[ "${{github.ref_name}}" == "master" ]]; then + echo "name=xinfinorg/xdc-zero:latest" >> $GITHUB_OUTPUT + else + echo "name=xinfinorg/xdc-zero:${{github.ref_name}}" >> $GITHUB_OUTPUT + fi + + - name: Docker build and tag image + run: docker build . --file Dockerfile --tag ${{ steps.image.outputs.name }} + - name: Docker push + run: docker push ${{ steps.image.outputs.name }} \ No newline at end of file diff --git a/.github/workflows/pr_build.yml b/.github/workflows/pr_build.yml new file mode 100644 index 0000000..44ab4a4 --- /dev/null +++ b/.github/workflows/pr_build.yml @@ -0,0 +1,50 @@ +name: Build custom branch +on: + pull_request: + branches: + - main + +jobs: + build: + if: | + ( startsWith(github.head_ref, 'feature') || + startsWith(github.head_ref, 'fix') ) + name: Deploy on PR + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + + - name: check out trigger branch + run: | + git fetch origin $BRANCH + git checkout $BRANCH + env: + BRANCH: ${{ github.head_ref }} + + - name: Record branch env + id: branch + run: | + echo "repo=${{ github.repository }}" >> $GITHUB_OUTPUT + echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT + echo "commit=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + # echo $repo + # echo $branch + # echo $commit + + - name: Docker login + env: + DOCKER_USERNAME: ${{secrets.DOCKER_USERNAME}} + DOCKER_PASSWORD: ${{secrets.DOCKER_ACCESS_TOKEN}} + run: | + echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + + - name: Determine Docker Image Name + id: image + run: | + echo "name=xinfinorg/xdc-zero:${{ steps.branch.outputs.branch }}" >> $GITHUB_OUTPUT + + - name: Build and push image + run: | + docker build . --file docker/Dockerfile --tag ${{ steps.image.outputs.name }} + docker push ${{ steps.image.outputs.name }} diff --git a/endpoint/endpointconfig.example.json b/endpoint/endpointconfig.example.json index b6f9547..ea6f507 100644 --- a/endpoint/endpointconfig.example.json +++ b/endpoint/endpointconfig.example.json @@ -1,28 +1,35 @@ { "xdcparentnet": { - "endpoint": "0xf218D129CD4a8bB153c9593bc5dFc12FAdfc44f4", + "endpoint": "0xB575c682301e20935B8C5846313a01E6f0Bd9f4B", "registers": [ { - "csc": "0x8EC22d14cD38FAB7C5AeF1d97105A6ec3FefEe5f", - "endpoint": "0xD4449Bf3f8E6a1b3fb5224F4e1Ec4288BD765547", - "chainId": "8851" + "csc": "0x3C714ffDB5A13d8d7EF0bE5f41F12bd840DA9ef1", + "endpoint": "0x0bb5a292b13C7983BFDCd62538e0e81603793342", + "chainId": "953" + } + ], + "applications": [ + { + "rid": "953", + "rua": "0xdfc2cD2b6AA7fD236e3A4Efa255A9b81c94B3fF4", + "sua": "0x40DC79697399686cd4003Ab9B7B87115bA945397" } ] }, "xdcsubnet": { - "endpoint": "0x550491BD078F7c5f78F16395b296E80F82f58700", + "endpoint": "0x0bb5a292b13C7983BFDCd62538e0e81603793342", "registers": [ { - "csc": "0x2475Dcd4Fe333bE814Ef7C8f8CE8A1E9B5FcDEA0", - "endpoint": "0x2475Dcd4Fe333bE814Ef7C8f8CE8A1E9B5FcDEA0", + "csc": "0x720A33E9c54dDf240D42f822E8b20D9C70226AAC", + "endpoint": "0xB575c682301e20935B8C5846313a01E6f0Bd9f4B", "chainId": "551" } ], "applications": [ { "rid": "551", - "rua": "0x5dff28627b2E4fd9516d18db6713c90d80d50f98", - "sua": "0x5dff28627b2E4fd9516d18db6713c90d80d50f98" + "rua": "0x40DC79697399686cd4003Ab9B7B87115bA945397", + "sua": "0xdfc2cD2b6AA7fD236e3A4Efa255A9b81c94B3fF4" } ] } From 7e08ba7922765e18a4313bd7dffa584417b15c78 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 3 Jul 2024 17:49:49 +0400 Subject: [PATCH 09/13] trigger build --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b88e541..dec9f54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -38,4 +38,5 @@ jobs: - name: Docker build and tag image run: docker build . --file Dockerfile --tag ${{ steps.image.outputs.name }} - name: Docker push - run: docker push ${{ steps.image.outputs.name }} \ No newline at end of file + run: docker push ${{ steps.image.outputs.name }} + \ No newline at end of file From 98a7a14dd2cb4647ecf854fa2001e460f337b2f9 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 3 Jul 2024 17:51:03 +0400 Subject: [PATCH 10/13] fix path --- .github/workflows/ci.yml | 3 +-- .github/workflows/pr_build.yml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dec9f54..36c3c1a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,6 @@ jobs: fi - name: Docker build and tag image - run: docker build . --file Dockerfile --tag ${{ steps.image.outputs.name }} + run: docker build . --file cicd/Dockerfile --tag ${{ steps.image.outputs.name }} - name: Docker push run: docker push ${{ steps.image.outputs.name }} - \ No newline at end of file diff --git a/.github/workflows/pr_build.yml b/.github/workflows/pr_build.yml index 44ab4a4..9b792e1 100644 --- a/.github/workflows/pr_build.yml +++ b/.github/workflows/pr_build.yml @@ -46,5 +46,5 @@ jobs: - name: Build and push image run: | - docker build . --file docker/Dockerfile --tag ${{ steps.image.outputs.name }} + docker build . --file cicd/Dockerfile --tag ${{ steps.image.outputs.name }} docker push ${{ steps.image.outputs.name }} From c781321cf9891170e38cbd2612f509b250994e4e Mon Sep 17 00:00:00 2001 From: Galaxy <30950645+GalaxySciTech@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:35:29 +0400 Subject: [PATCH 11/13] Update README.md --- cicd/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cicd/README.md b/cicd/README.md index 30b44bf..19c4ca9 100644 --- a/cicd/README.md +++ b/cicd/README.md @@ -1,3 +1,4 @@ +## Deploy XDC Zero 1. create a .env like the following example ``` PARENTNET=devnet @@ -12,6 +13,9 @@ REVERSE_CSC=0x4444444444444444444444444444444444444444 ``` docker run --env-file .env xinfinorg/xdc-zero:latest endpointandregisterchain.js ``` + +## Regist Application to XDC Zero + - add the output to .env ``` SUBNET_ZERO_CONTRACT=0x5555555555555555555555555555555555555555 From 1c67f46dd0d856c9a1ff615d1f0eba9a48aa9463 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Thu, 4 Jul 2024 21:31:56 +0400 Subject: [PATCH 12/13] add deploy with repo --- cicd/README.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/cicd/README.md b/cicd/README.md index 19c4ca9..5c3db1c 100644 --- a/cicd/README.md +++ b/cicd/README.md @@ -1,3 +1,4 @@ +# Deploy with Docker ## Deploy XDC Zero 1. create a .env like the following example ``` @@ -14,14 +15,13 @@ REVERSE_CSC=0x4444444444444444444444444444444444444444 docker run --env-file .env xinfinorg/xdc-zero:latest endpointandregisterchain.js ``` -## Regist Application to XDC Zero - - add the output to .env ``` SUBNET_ZERO_CONTRACT=0x5555555555555555555555555555555555555555 PARENTNET_ZERO_CONTRACT=0x6666666666666666666666666666666666666666 ``` +## Deploy Subswap 3. Deploy Subswap ``` docker run --env-file .env xinfinorg/xdc-zero:latest subswap.js @@ -32,7 +32,56 @@ SUBNET_APP=0x7777777777777777777777777777777777777777 PARENTNET_APP=0x8888888888888888888888888888888888888888 ``` + +## Register Application to XDC Zero 4. Register Subswap to XDC-Zero ``` docker run --env-file .env xinfinorg/xdc-zero:latest applicationregister.js ``` + +
+
+ +# Deploy using this repository + +1. Install packages +``` +cd ../endpoint +yarn +cd ../applications/subswap/contract +yarn +cd cicd +yarn +``` + +2. Configure .env at cicd/mount/.env +``` +PARENTNET=devnet +SUBNET_URL= +SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111 +PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222 +CSC=0x3333333333333333333333333333333333333333 +REVERSE_CSC=0x4444444444444444444444444444444444444444 +``` + +3. Deploy endpoint and register chain +``` +cd cicd +node endpointandregisterchain.js +``` + +4. Deploy Subswap +``` +node subswap.js +``` +- add the output to cicd/mount/.env +``` +SUBNET_APP=0x7777777777777777777777777777777777777777 +PARENTNET_APP=0x8888888888888888888888888888888888888888 +``` + + +5. Register Application +``` +node applicationregister.js +``` From a0526f149945bd26c1bdd01ae25c4598230f4387 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Fri, 5 Jul 2024 10:05:05 +0400 Subject: [PATCH 13/13] custom url --- cicd/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cicd/README.md b/cicd/README.md index 5c3db1c..9cb78c1 100644 --- a/cicd/README.md +++ b/cicd/README.md @@ -1,6 +1,6 @@ # Deploy with Docker ## Deploy XDC Zero -1. create a .env like the following example +1. Create a .env as the below example PARENTNET can be devnet,testnet,mainnet. For custom url PARENTNET_URL will override PARENTNET. ``` PARENTNET=devnet SUBNET_URL= @@ -54,7 +54,7 @@ cd cicd yarn ``` -2. Configure .env at cicd/mount/.env +2. Configure .env at cicd/mount/.env. PARENTNET can be devnet,testnet,mainnet. For custom url PARENTNET_URL will override PARENTNET. ``` PARENTNET=devnet SUBNET_URL=