Create a Custom Blockchain

Introduction

Avalanche supports creating blockchains with virtual machines in subnets. In this tutorial, we’ll create a custom blockchain using a custom Virtual Machine (Timestamp VM).
If you want a blockchain that has capabilities of X-Chain (AVM), see Create AVM Blockchain.
Note: IDs of Blockchains, Subnets, Transactions and Addresses can be different for each run/network. It means that some inputs, endpoints etc. in the tutorial can be different when you try.

Prerequisites

You will need a running node, a user on the node, and some AVAX in the address controlled by the user. All of that is covered in the Run an Avalanche Node tutorial.
Next, you need to have your node be a validator on the Primary Network. You can find out how to do that in the Add a Validator tutorial. It is recommended you do that with API calls, since that is the way you will be interacting with your node in the rest of this tutorial.

Create the Virtual Machine

Every blockchain is an instance of a virtual machine. For example X-Chain is an instance of AVM and C-Chain is EVM's instance. Avalanche supports creating new blockchains (instances) from Virtual Machines. In this case we will use Timestamp VM, which is an external VM plugin. Timestamp VM will communicate with our AvalancheGo node through RPC.

Create the Subnet

Every blockchain is validated by a subnet. Before you can create a blockchain, you’ll need a subnet to validate it. You can also use a subnet that already exists if you have a sufficient number of its control keys.

Add Validators to the Subnet

The subnet needs validators in it to, well, validate blockchains.

Create the Genesis Data

Each blockchain has some genesis state when it’s created. Each VM defines the format and semantics of its genesis data. TimestampVM uses CB58 encoded data as genesis data. There is encode and decode static API methods that can be used to encode/decode string data. See TimestampVM API.
Let's generate a simple genesis data for TimestampVM:
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method": "timestampvm.encode",
4
"params":{
5
"data":"helloworld"
6
},
7
"id": 1
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/vm/tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH
Copied!
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"bytes": "fP1vxkpyLWnH9dD6BQA",
5
"encoding": "cb58"
6
},
7
"id": 1
8
}
Copied!
Our genesis data will be fP1vxkpyLWnH9dD6BQA.

Create the Blockchain

Now let’s create the new blockchain. To do so, we call platform.createBlockchain. Your call should look like the one below. You have to change subnetID to the subnet that will validate your blockchain, and supply a username that controls a sufficient number of the subnet’s control keys. As a reminder, you can find out what a subnet’s threshold and control keys are by calling platform.getSubnets.
Recall that we used tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH as our VM ID in Create A Virtual Machine(VM).
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method": "platform.createBlockchain",
4
"params" : {
5
"subnetID": "KL1e8io1Zi2kr8cTXxvi321pAzfQuUa8tmBfadqpf9K2dc2TT",
6
"vmID":"tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH",
7
"name":"My new TSVM",
8
"genesisData": "fP1vxkpyLWnH9dD6BQA",
9
"username":"USERNAME GOES HERE",
10
"password":"PASSWORD GOES HERE"
11
},
12
"id": 1
13
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/P
Copied!
The response contains the transaction ID:
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"txID": "sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk",
5
"changeAddr": "P-avax103y30cxeulkjfe3kwfnpt432ylmnxux8r73r8u"
6
},
7
"id": 1
8
}
Copied!

Verify Success

After a few seconds, the transaction to create our blockchain should have been accepted and the blockchain should exist (assuming the request was well-formed, etc.)
To check, call platform.getBlockchains. This returns a list of all blockchains that exist.
1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"platform.getBlockchains",
5
"params" :{}
6
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/P
Copied!
The response confirms that the blockchain was created:
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"blockchains": [
5
{
6
"id": "AXerNQX7voY2AABaRdTAyXcawBURBR6thPRJp43LtPpZZi6Qz",
7
"name": "X-Chain",
8
"subnetID": "11111111111111111111111111111111LpoYY",
9
"vmID": "jvYyfQTxGMJLuGWa55kdP2p2zSUYsQ5Raupu4TW34ZAUBAbtq"
10
},
11
{
12
"id": "tZGm6RCkeGpVETUTp11DW3UYFZmm69zfqxchpHrSF7wgy8rmw",
13
"name": "C-Chain",
14
"subnetID": "11111111111111111111111111111111LpoYY",
15
"vmID": "mgj786NP7uDwBCcq6YwThhaN8FLyybkCa4zBWTQbNgmK6k9A6"
16
},
17
{
18
"id": "sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk",
19
"name": "My new TSVM",
20
"subnetID": "KL1e8io1Zi2kr8cTXxvi321pAzfQuUa8tmBfadqpf9K2dc2TT",
21
"vmID": "tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH"
22
},
23
{
24
"id": "xAd5n5PQFV6RRo8UgH54Gf5tJs8oQdctQS2ygp5F2dKZDckYH",
25
"name": "My new AVM",
26
"subnetID": "KL1e8io1Zi2kr8cTXxvi321pAzfQuUa8tmBfadqpf9K2dc2TT",
27
"vmID": "jvYyfQTxGMJLuGWa55kdP2p2zSUYsQ5Raupu4TW34ZAUBAbtq"
28
}
29
]
30
},
31
"id": 1
32
}
Copied!

Validating the Blockchain

Every blockchain needs a set of validators to validate and process transactions on it. You can check if a node is validating a given blockchain by calling platform.getBlockchainStatus on that node:
1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"platform.getBlockchainStatus",
5
"params" :{
6
"blockchainID":"sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk"
7
}
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/P
Copied!
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"status": "Validating"
5
},
6
"id": 1
7
}
Copied!
If it responds "Validating", the node is validating the given chain. If it responds "Syncing", then the chain tracked by this node but it is not validating. If it responde "Created" then the chain exists but it is not being synced. Note that in order to validate or watch a subnet, you need to start your node with argument --whitelisted-subnets=[subnet ID goes here] (e.g. --whitelisted-subnets=KL1e8io1Zi2kr8cTXxvi321pAzfQuUa8tmBfadqpf9K2dc2TT) as well as add the node to the subnet's validator set.
More information can be found in the Adding a Subnet Validator tutorial.

Interacting with the New Blockchain

You can interact with this new instance of the VM. The API endpoint of the blockchain is 127.0.0.1:9650/ext/bc/sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk. The last part in the endpoint is the blockchain ID, which is sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk. Every blockchain ID is different from each other, so this is not a static ID. Your blockchain ID and the endpoint can be different.
You can also alias this chain ID with timestampbc, or whatever you like, for simpler API URLs. More information: admin.aliasChain

Verify Genesis Block

In the genesis we specified fP1vxkpyLWnH9dD6BQA as the genesis data. Let’s verify that:
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method": "timestampvm.getBlock",
4
"params":{
5
"id":""
6
},
7
"id": 1
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk
Copied!
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"timestamp": "0",
5
"data": "nyfJkNxEwKeQ9KpPducrm3jRaDzpPNJXUdZtgCWeMZTUxPqGp",
6
"id": "24kWScv7DMA4LwdoFwmN1iRU3idyHRrrA2UxN9k6AuXihoK3mn",
7
"parentID": "11111111111111111111111111111111LpoYY"
8
},
9
"id": 1
10
}
Copied!
As you can see our first block has timestamp: 0. Also the parent ID (11111111111111111111111111111111LpoYY) is the P-chain's ID. Let's decode the genesis data with VM's static API method. Recall that our TimestampVM ID is aliased with timestampvm:
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"id" : 1,
4
"method" : "timestampvm.decode",
5
"params" : {
6
"bytes": "nyfJkNxEwKeQ9KpPducrm3jRaDzpPNJXUdZtgCWeMZTUxPqGp"
7
}
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/vm/tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH
Copied!
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"data":"helloworld",
5
"encoding": "cb58"
6
},
7
"id": 1
8
}
Copied!
We can see genesis data has the helloworld string.

Propose New Block

We can propose new blocks to our blockchain with some data in it.
Let's get encoded data first. Blocks expect to have 32-length bytes. There is a length argument in encode method:
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method" : "timestampvm.encode",
4
"params" : {
5
"data": "mynewblock",
6
"length": 32
7
}
8
"id" : 1,
9
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/vm/tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH
Copied!
Result:
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"bytes": "qDNkrS9xuyGmaAgdHAjbmANSvCKnK5BHvyCybJaFCAqx46Z8y",
5
"encoding": "cb58"
6
},
7
"id": 1
8
}
Copied!
Now we can propose a new block with the data:
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method": "timestampvm.proposeBlock",
4
"params":{
5
"data":"qDNkrS9xuyGmaAgdHAjbmANSvCKnK5BHvyCybJaFCAqx46Z8y"
6
},
7
"id": 1
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk
Copied!
Result:
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"Success": true
5
},
6
"id": 1
7
}
Copied!
Let's check latest block to verify existence of our proposed block:
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method": "timestampvm.getBlock",
4
"params":{
5
"id":""
6
},
7
"id": 1
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/sw813hGSWH8pdU9uzaYy9fCtYFfY7AjDd2c9rm64SbApnvjmk
Copied!
Result:
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"timestamp": "1625674027",
5
"data": "qDNkrS9xuyGmaAgdHAjbmANSvCKnK5BHvyCybJaFCAqx46Z8y",
6
"id": "Br36bggr9vEEoNTNVPsSCD7QHHoCqE31Coui6uh1rA71EGPve",
7
"parentID": "24kWScv7DMA4LwdoFwmN1iRU3idyHRrrA2UxN9k6AuXihoK3mn"
8
},
9
"id": 1
10
}
Copied!
Result contains data field has qDNkrS9xuyGmaAgdHAjbmANSvCKnK5BHvyCybJaFCAqx46Z8y. This is the same data as our proposed data in the previous step.
Last modified 1mo ago