Topics In Demand
Notification
New

No notification found.

How to create a subnet on Avalanche
How to create a subnet on Avalanche

November 1, 2022

345

0

 

Avalanche subnets are a way to increase the efficiency and scalability of Avalanche transactions. By partitioning Avalanche nodes into smaller groups, called subnets, Avalanche can process more transactions simultaneously and faster. 

There are many advantages of running your own subnet. Subnets offer higher throughput, strict access control, and EVM customization. 

Here in this article, we will explore how to create an Avalanche subnet on the Fuji test network. The entire process can be described by breaking it down into three parts:  

  1. Running Local Avalanche Node
  2. Make it a validator
  3. Creating Subnet

So without further ado, let’s jump into the command line…

Running A Local Avalanche Node:

  • The first step is to set up GO– the default language for the official implementation of Avalanche. To verify if it’s already installed, run this command by opening the Command prompt. This should print the installed version of GO. 
$ go version
  • We also need to install Git- a handy DevOps tool used for source code management. Git comes installed default on MAC or Linux. Activate it by running the following command in the command prompt:
      git version 
  • Now we will move to the node implementation stage. Here, we are taking the native installation option. Hence, we need to clone the AvalancheGO repository by running:
git clone git@github.com:ava-labs/avalanchego.git

 

If the download is complete, run the below command to check out to master branch and move to the avalanche go folder

cd avalanchego

 

Then run this Build script

./scripts/build.sh

 

           The output of the script will be the avalanche binary named avalanchego located in  the  build directory

./build/avalanchego

If you want to take some other installation approach like a Docker file, pre-build binary, or an APT repository, check out AvalancheGo GitHub. But downloading the source code is recommended if you are a developer looking to build on Avalanche. 

  • Next, we need to run the node and connect it to the Fuji testnet. Execute:
./build/avalanchego --network-id=fuji

It’s time for bootstrapping now. It can take several hours or even days depending on the hardware. Usually, the database I/O is the problem during the bootstrapping process. Bootstrapping time can be reduced by using a more advanced CPU or increasing the database IOPS on the machine hosting the node. 

To check if it’s done bootstrapping call info.isBootstrapped in a new tab. To send a cURL request copy and paste the following command: 

curl -X POST --data '{
    "jsonrpc":"2.0",
    "id"     :1,
    "method" :"info.isBootstrapped",
    "params": {
        "chain":"X"
    }
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/info

It will return TRUE if done: 

{
  "jsonrpc": "2.0",
  "result": {
    "isBootstrapped": true
  },
  "id": 1
}

 

We now have a node on Avalanche primary network. You can access C, P, and X chains using this node endpoint. But it’s not a validator yet. The next step is to make it a validator node in the Fuji testnet. 

Adding A Validator With Avalanche Wallet:

  • The first step is to retrieve the node id. Call info.getNodeID and copy-paste the following:
curl -X POST --data '{
    "jsonrpc":"2.0",
    "id"     :1,
    "method" :"info.getNodeID"
}' -H 'content-type:application/json' 127.0.0.1:9650/ext/info

The response will contain your node id like this:

{
  "jsonrpc": "2.0",
  "result": {
    "nodeID": "NodeID-5mb46qkSBj81k9g9e4VFjGGSbaaSLFRzD"
  },
  "id": 1
}

 
  • Next, go to the Avalanche Wallet page. Create a new wallet, and select the Fuji testnet option. Now to become a Validator you at least need 1 AVAX in the P-chain. 

Copy your C-chain address to get 2 test AVAX from the faucet. Then transfer it to the p-chain. 

Then, Go to the EARN section and click on Add Validator. You will be asked for your Node Id. Fill out all other staking parameters. If you click on ‘Confirm’ a success message will be displayed. 

Congratulations, we have added our node as a validator in the Fuji testnet. That’s it. 

  • To verify, if your node is added to the Fuji testnet, call platform.getCurrentValidators

platform.getCurrentValidators({

  subnetID: string, // optional 

  nodeIDs: string[], // optional 

}) -> {

  validators: []{

    txID: string,

    startTime: string,

    endTime: string,

    stakeAmount: string,

    nodeID: string,

    weight: string,

    rewardOwner: {

      locktime: string,

      threshold: string,

      addresses: string[]

   },

    potentialReward: string,

    delegationFee: string,

    uptime: string,

    connected: bool,

    delegators: []{

       txID: string,

       startTime: string,

       endTime: string, stakeAmount: string,

       nodeID: string,

       rewardOwner: {

           locktime: string,

           threshold: string,

           addresses: string[]

    },

   potentialReward: string,

   }

 }

}

If your node id is there, move on to the next part: Subnet Creation. 

Creating Subnet:

  • To use in the testnet node only, we will create a user with the Key store API. Things to note here is key store exists at the node level, and the node operator has access to your username and password. So create a key store user only on a node that you operate. Plus, don’t use it in the mainnet. 
  • To create a new user, Call: 
curl -X POST --data '{
    "jsonrpc":"2.0",
    "id"     :1,
    "method" :"keystore.createUser",
    "params" :{
        "username":"myUsername",
        "password":"myPassword"
    }
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/keystore

If successful, you will get a response like this:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {}
}

 
  • To create a new address on P-chain controlled by the new user, call 
curl -X POST --data '{
    "jsonrpc": "2.0",
    "method": "platform.createAddress",
    "params": {
        "username":"myUsername",
        "password":"myPassword"
    },
    "id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/P

Here is the example response: 

{
  "jsonrpc": "2.0",
  "result": {
    "address": "P-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5"
  },
  "id": 1
}

 
  • Now we have to create a new subnet. Here we will need the address generated in the last step. But before we run platform.createSubnet, we need to top up the address with P-chain AVAX. We need to import the current wallet using its private key. Run:
curl -X POST --data '{
    "jsonrpc":"2.0",
    "id"     :1,
    "method" :"platform.exportKey",
    "params" :{
        "username" :"myUsername",
        "password": "myPassword",
        "address": "P-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5"
    }
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/P

The response will show you the private key for the generated address: 

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "privateKey": "PrivateKey-Lf49kAJw3CbaL783vmbeAJvhscJqC7vi5yBYLxw2XfbzNS5RS"
  }
}

 
  • Import wallet for the generated private key. Then follow the same steps you did in the first part to get test AVAX for C-chain and do a cross-chain transfer to get it on the P-chain. We are almost done. Now run: 
curl -X POST --data '{
    "jsonrpc": "2.0",
    "method": "platform.createSubnet",
    "params": {
        "controlKeys":[
            "P-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5",
            "P-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5"
        ],
        "threshold":2,
        "from": ["P-avax18jma8ppw3nhx5r4ap8clazz0dps7rv5ukulre5"],
        "changeAddr": "P-avax103y30cxeulkjfe3kwfnpt432ylmnxux8r73r8u",
        "username":"myUsername",
        "password":"myPassword"
    },
    "id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/P

If everything goes well, you should get the following response: 

{
  "jsonrpc": "2.0",
  "result": {
    "txID": "hJfC5xGhtjhCGBh1JWn3vZ51KJP696TZrsbadPHNbQG2Ve5yd"
  },
  "id": 1
}

 

The transaction Id in the above response is our subnet Id. We have successfully created our subnet. 

Want to avoid these long and complex processes? We can help. 

 

About The Author 

Author

Dr. Ravi Chamria is co-founder CEO of Zeeve Inc, an Enterprise Blockchain company. He has an experience of 18+ years in IT consulting spanning across Fintech, InsureTech, Supply Chain and eCommerce. He is an executive MBA from IIM, Lucknow and a prolific speaker on emerging technologies like Blockchain, IoT and AI/ML.

Passionate About: Blockchain, Supply Chain Management, Digital Lending, Digital Payments, AI/ML, IoT

Specialities: Strategic Management, Technology Innovation, Product Management


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


Zeeve is an enterprise-grade Blockchain Infrastructure Automation Platform. Join the growing list of clients that trust us with their Blockchain initiatives

© Copyright nasscom. All Rights Reserved.