Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Configuring DDC with a setup script #84

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Also, check the [config file](./sdk/src/config/index.js), to ensure that you are
The [examples](./examples/) folder contains scripts that demonstrate how to properly send transacrions to DDC Bucket contracts using the [@polkadot/api](https://polkadot.js.org/docs/api/) library. These simple scenarious should help other team members to clarify the business logic flow, and quickly detect issues related to various business constraints and infrastructue constraints (i.e. gas price, attached tokens, method signature, etc.). Scripts should be updated while the contract is evolving to reflect the actual logic.


#### DDC Bucket scenario
#### DDC Contract demo scenario

Run the script as:
```
Expand All @@ -18,22 +18,22 @@ ENV=devnet yarn run demo-ddc-bucket
The execution progress will be displayed in the console along with links to explorer that will help you to investigate the details of each transaction


#### Display DDC Bucket state
#### Display DDC Contract state

Run the script as:
```
ENV=devnet yarn run print-ddc-bucket
```


## Deployment using script
## Deployment DDC Contract using script

The [deployment](./deployment/) folder contains scripts that allow you to deploy artifacts to a local or remote network.
Typically, these scripts are used for EDC, Devnet and QAnet environments during development.
For Testnet and Mainnet environments, it is recomended to upload the contract code manually and assert all the required keys, attached tokens, gas limits, etc. during deployment and contract instantianating.


#### DDC Bucket deployment
#### DDC Contract deployment

Run the script as:
```
Expand All @@ -42,6 +42,15 @@ yarn run deploy-ddc-bucket
Optionally, the command can accept a code hash as the first parameter, and constructor name as the second parameter. In order to use these options, your contract artifacts [must be registered](./sdk/src/deploymentRegistry.js) to retrieve the required metadata from artifacts.


#### DDC Contract setup

Run the script as:
```
DDC_CONTRACT='6PZ6ndAdULxsnzLjyNmPuWonS9jSxTRgXN1EtVMnbi41VZZg' SUPERADMIN='//Alice' ENV='devnet' yarn run setup-ddc-bucket
```
Use the DDC Contract address in place of the `DDC_CONTRACT` variable that you want to configure, the admin of DDC Contract from the deployment step in place of `SUPERADMIN` variable, and DDC environment that you are targeting in place of the `ENV` variable


#### Build and Deploy all contracts

To run both building and deployment for all contracts, you can use the [build-and-deploy.sh](./../build-and-deploy.sh) script.
167 changes: 98 additions & 69 deletions scripts/ddc-setup/ddcBucketSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ const {
CERE,
MGAS,
ddcBucket,
randomAccount,
deploymentRegistry,
config
} = require("../sdk/src");
const ddcConfig = require('./ddcConfig.js');
const log = console.log;

const DDC_BUCKET_CONTRACT_NAME = config.DDC_BUCKET_CONTRACT_NAME;
const ENV = process.env.ENV;
const ddcEnvConfig = ddcConfig[ENV];
const SUPERADMIN_MNEMONIC = process.env.SUPERADMIN;
const DDC_CONTRACT_ADDRESS = process.env.DDC_CONTRACT;


const ddcEnvConfig = ddcConfig[ENV];
if (!ddcEnvConfig) {
console.error("Please provide ENV as one of ", Object.keys(ddcConfig));
process.exit(-1);
console.error("Please provide ENV as one of ", Object.keys(ddcConfig));
process.exit(-1);
}
console.log(ddcEnvConfig);

Expand All @@ -31,41 +29,42 @@ if (!SUPERADMIN_MNEMONIC) {
process.exit(-1);
}

deploymentRegistry.initContract(
DDC_BUCKET_CONTRACT_NAME,
ENV,
ddcEnvConfig.contract_address
);
if (!DDC_CONTRACT_ADDRESS) {
console.error("Please provide DDC_CONTRACT address");
process.exit(-1);
}

deploymentRegistry.initContract(config.DDC_BUCKET_CONTRACT_NAME, ENV, DDC_CONTRACT_ADDRESS);

async function main() {
const {api, chainName, getExplorerUrl} = await connect(ddcEnvConfig.ws_provider);
const {api, chainName, getExplorerUrl} = await connect(ddcEnvConfig.blockchainUrl);
log("Connected to blockchain:", chainName);

const sadmin = accountFromUri(SUPERADMIN_MNEMONIC);
console.log(`Superadmin: ${sadmin.address}`);

const bucketContract = getContract(DDC_BUCKET_CONTRACT_NAME, ENV, api);
log("Using bucket contract", DDC_BUCKET_CONTRACT_NAME, "at", bucketContract.address.toString());
const bucketContract = getContract(config.DDC_BUCKET_CONTRACT_NAME, ENV, api);
log("Using bucket contract", config.DDC_BUCKET_CONTRACT_NAME, "at", bucketContract.address.toString());

const txOptions = {
storageDepositLimit: null,
gasLimit: 100_000_000_000n,
};

console.log( "\n", `Setup Started`, "\n");

{
log("1. accountSetUsdPerCere");
log(`Setting USD per CERE rate ...`);
const tx = bucketContract.tx.accountSetUsdPerCere(
txOptions,
1000n * CERE
);

const result = await sendTx(sadmin, tx);
log(getExplorerUrl(result), "\n");
}

{
log("2. grantTrustedManagerPermission");
log(`Granting trusted managers permissions ...`);
const tx = bucketContract.tx.grantTrustedManagerPermission(
txOptions,
sadmin.address
Expand All @@ -74,70 +73,100 @@ async function main() {
log(getExplorerUrl(result), "\n");
}

const cdnNodesKeys = []
{
console.log("3. cdnNodeCreate");
for (let i = 0; i < ddcEnvConfig.cdn_node_params.length; i++) {
const cdnNodeKey = ddcEnvConfig.cdn_node_params[i].publicKey;
cdnNodesKeys.push(cdnNodeKey);

const tx = bucketContract.tx.cdnNodeCreate(
txOptions,
cdnNodeKey,
JSON.stringify(ddcEnvConfig.cdn_node_params[i])
);

const result = await sendTx(sadmin, tx);
log(getExplorerUrl(result), "\n");
}
}

const storageNodesKeys = []
{
console.log("4. nodeCreate");
for (let i = 0; i < ddcEnvConfig.storage_node_params.length; i++) {
const param = JSON.stringify(ddcEnvConfig.storage_node_params[i]);
const user = randomAccount();

fs.appendFileSync('secrets.txt', `${user.address}: ${user.mnemonic} -- ${ENV} storage ${i}\n`);
console.log(` node ${i}: address ${user.address}, param ${param}`);

const storageNodeKey = user.address;
storageNodesKeys.push(storageNodeKey);
for (let i = 0; i < ddcEnvConfig.clusters.length; i++) {
const cluster = ddcEnvConfig.clusters[i];
const clusterParams = JSON.stringify(cluster.params);
const resourcePerVNode = 100000n;

const tx = bucketContract.tx.nodeCreate(
console.log(`Creating Cluster ${i} ...`);
const clusterCreateTx = bucketContract.tx.clusterCreate(
txOptions,
clusterParams,
resourcePerVNode
);
const result = await sendTx(sadmin, clusterCreateTx);
log(getExplorerUrl(result), "\n");
let { clusterId } = ddcBucket.findClusterCreatedEvent(result.contractEvents || []);
console.log(`Cluster ${clusterId} created`);

for (let j = 0; j < cluster.storageNodes.length; j++) {
const storageNode = cluster.storageNodes[j];
const storageNodeKey = storageNode.pubKey;
const storageNodeParams = JSON.stringify(storageNode.params);
const vNodes = storageNode.vNodes;
const storageNodeCapacity = 100000n;
const rentVNodePerMonth = 1n * CERE;

console.log(`Creating Storage node ${storageNodeKey} ...`);
const nodeCreateTx = bucketContract.tx.nodeCreate(
txOptions,
storageNodeKey,
param,
100000n,
1n * CERE
storageNodeParams,
storageNodeCapacity,
rentVNodePerMonth,
);
const result1 = await sendTx(sadmin, nodeCreateTx);
log(getExplorerUrl(result1), "\n");

const result = await sendTx(sadmin, tx);
log(getExplorerUrl(result), "\n");
console.log(`Adding Storage node ${storageNodeKey} to Cluster ${clusterId} ...`);
const clusterAddNodeTx = bucketContract.tx.clusterAddNode(
txOptions,
clusterId,
storageNodeKey,
vNodes
)
const result2 = await sendTx(sadmin, clusterAddNodeTx);
log(getExplorerUrl(result2), "\n");

const storageNodeStatus = 'ACTIVE';
console.log(`Setting '${storageNodeStatus}' status ${storageNodeKey} in Cluster ${clusterId} ...`);
const clusterSetNodeStatusTx = bucketContract.tx.clusterSetNodeStatus(
txOptions,
clusterId,
storageNodeKey,
storageNodeStatus,
)
const result3 = await sendTx(sadmin, clusterSetNodeStatusTx);
log(getExplorerUrl(result3), "\n");
}
}

const clustersIds = [];
{
for (let key in ddcEnvConfig.cluster) {
console.log("5. clusterCreate ");

const tx1 = bucketContract.tx.clusterCreate(
txOptions,
JSON.stringify(ddcEnvConfig.cluster[key].param),
100000n
);
for (let j = 0; j < cluster.cdnNodes.length; j++) {
const cdnNode = cluster.cdnNodes[j];
const cdnNodeKey = cdnNode.pubKey;
const cdnNodeParams = JSON.stringify(cdnNode.params);

const result1 = await sendTx(sadmin, tx1);
console.log(`Creating CDN node ${cdnNodeKey} ...`);
const cdnNodeCreateTx = bucketContract.tx.cdnNodeCreate(
txOptions,
cdnNodeKey,
cdnNodeParams
);
const result1 = await sendTx(sadmin, cdnNodeCreateTx);
log(getExplorerUrl(result1), "\n");
let { clusterId } = ddcBucket.findClusterCreatedEvent(result1.contractEvents || []);
clustersIds.push(clusterId);

console.log(`Adding CDN node ${cdnNodeKey} to Cluster ${clusterId} ...`);
const clusterAddCdnNodeTx = bucketContract.tx.clusterAddCdnNode(
txOptions,
clusterId,
cdnNodeKey,
)
const result2 = await sendTx(sadmin, clusterAddCdnNodeTx);
log(getExplorerUrl(result2), "\n");

const cdnNodeStatus = 'ACTIVE';
console.log(`Setting '${cdnNodeStatus}' status ${cdnNodeKey} in Cluster ${clusterId} ...`);
const clusterSetCdnNodeStatusTx = bucketContract.tx.clusterSetCdnNodeStatus(
txOptions,
clusterId,
cdnNodeKey,
cdnNodeStatus,
)
const result3 = await sendTx(sadmin, clusterSetCdnNodeStatusTx);
log(getExplorerUrl(result3), "\n");
}
}

// TODO: Add Storage nodes and CDN nodes to clusters

console.log( "\n", `Setup Finished`, "\n");
process.exit(0);
}

Expand Down
Loading