Dive into Nebulas 2 — A Quick Start

Roy Shang
Nebulasio
Published in
4 min readApr 10, 2018

--

I’m excited to share a quick start guide on how to get Nebulas up and running within minutes.

Install Nebulas

We implement Nebulas in Golang, called “Go-Nebulas”. In this part, I’ll introduce how to install Go-Nebulas on Mac. The procedure is similar to Linux. Feel free to contact us in Slack if you have any questions about Go-Nebulas.

Prepare Go

Make sure the version of Golang ≥ 1.9, and configure GOPATH.

export GOPATH=/path/to/workspace

Clone Repo

Note: We have only ONE official repo now.

mkdir $GOPATH/src/github.com/nebulasio/
cd $GOPATH/src/github.com/nebulasio
git clone https://github.com/nebulasio/go-nebulas.git

Choose Branch

There are three main branches in our repo: develop, testnet and master.

Git Workflow

Above is our Git Workflow.

  • All new features will be developed in standalone branches, before merging into develop branch.
  • After which, we’ll merge all changes into testnet branch and deploy them on the official Testnet.
  • If everything works well in the official Testnet, the new features will be accepted by master branch and be deployed on Mainnet.

We always recommend you to use master branch, which is the most stable.

git checkout master

Install Dependencies

Comparing between RocksDB and LevelDB, we chose RocksDB as our default persistent storage for better performance.

Homebrew can be used to install RocksDB in Mac.

brew install rocksdb

For the convenience of most developers today, we chose Javascript (the most popular programming language) as the first smart contract language to be supported by Nebulas. Chrome V8 is also integrated into our virtual machine to execute smart contracts written in Javascript.

We have compiled dynamic libraries of V8 on both Mac and Linux. A single command is required for easy installation.

make deploy-v8

Then, we use golang/dep to install all go dependencies.

brew install dep
make dep

Compile Neb

Now we can build the executable neb.

make build

We’ll get ./neb in current directory.

Launch Nebulas

Based on the configuration, we can use neb to join Mainnet, Testnet or a private net. In this section, I hope to help you launch your first private net. In your private net, you will be able to explore Nebulas freely.

We provided the default configuration for local private net. neb can be launched with it directly.

cd $GOPATH/src/github.com/nebulasio/go-nebulas
./neb -c conf/default/config.conf

If you’ve seen “Started Neblet” printed in console, it means everything works well.

INFO[2018–04–08T17:18:07+08:00] Started Neblet. file=neblet.go func=”neblet.(*Neblet).Start” line=259

In the same way, we have to launch another miner node to mint new blocks so that the submitted transactions can be executed.

cd $GOPATH/src/github.com/nebulasio/go-nebulas
./neb -c conf/example/miner.conf

Congratulations! You have successfully launched Go-Nebulas!

Send Transaction

After the private net launched, you’ll own 100 million tokens in the private net. In this section, I will guide you on how to transfer these tokens between accounts with http requests.

Check Balance

In this private net, 100 million tokens are distributed into twenty accounts evenly. In Nebulas, account and address are one to one.

Select two, check their balance.

> curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1Q8mxXp4PtHaXtebhY12BnHEwu4mryEkXH"}'{“result”:{“balance”:”5000000000000000000000000",”nonce”:”0",”type”:87}}> curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1RYagU8n3JSuV4R7q4Qs5gQJ3pEmrZd6cJ"}'{“result”:{“balance”:”5000000000000000000000000",”nonce”:”0",”type”:87}}

The unit of balance is Wei (1 NAS = 10¹⁸ Wei). Each of these two accounts has 5 million NAS in the private net.

Account’s nonce in Nebulas is used as a transaction counter in each account, preventing replay attacks. Only after the transaction with nonce N is accepted, the transaction with nonce N+1 can be processed.

Nebulas offers a special feature: Type of account.

Two types of accounts are available now: Normal Account(87) and Smart Contract Account(88). These information is encoded into every address generated by Nebulas.

The type of accounts can be verified according to their addresses without any context. It’s much safer to use them in different scenarios.

Transfer Token

In order to transfer tokens from A to B, A is required to sign a new transaction and send it to a launched neb node.

  1. Sign a new transaction and get its raw data in base64 encoding.
> curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/admin/sign -d '{"transaction":{"from":"n1Q8mxXp4PtHaXtebhY12BnHEwu4mryEkXH","to":"n1RYagU8n3JSuV4R7q4Qs5gQJ3pEmrZd6cJ", "value":"1000000000000000000","nonce":1,"gasPrice":"1000000","gasLimit":"2000000"}, "passphrase":"passphrase"}'{“result”:{“data”:”CiDeG1hvAirdI85PlzKejLYpt8dI2ctS6K+HImH2A9p9chIaGVdphHtJoAmJkrt6HOiivNI3fQxrhux0NZAaGhlXeP0tS1WtTIWeOkHelwMW3MZLpO4ia4dzIhAAAAAAAAAAAA3gtrOnZAAAKAEw/9un1gU6CAoGYmluYXJ5QGRKEAAAAAAAAAAAAAAAAAAPQkBSEAAAAAAAAAAAAAAAAAAehIBYAWJBIkg6k+NKV53tYSllF/Ka29TCUfgZr3l2qOBq6Zo79Ss68ugn2253ola958b65LGOb1mQG85nYRL+PsCDL34d8wA=”}}

2. Send the transaction to the launched neb node.


> curl -i -H ‘Content-Type: application/json’ -X POST http://localhost:8685/v1/user/rawtransaction -d ‘{“data”:”CiDeG1hvAirdI85PlzKejLYpt8dI2ctS6K+HImH2A9p9chIaGVdphHtJoAmJkrt6HOiivNI3fQxrhux0NZAaGhlXeP0tS1WtTIWeOkHelwMW3MZLpO4ia4dzIhAAAAAAAAAAAA3gtrOnZAAAKAEw/9un1gU6CAoGYmluYXJ5QGRKEAAAAAAAAAAAAAAAAAAPQkBSEAAAAAAAAAAAAAAAAAAehIBYAWJBIkg6k+NKV53tYSllF/Ka29TCUfgZr3l2qOBq6Zo79Ss68ugn2253ola958b65LGOb1mQG85nYRL+PsCDL34d8wA=”}’
{“result”:{“txhash”:”de1b586f022add23ce4f97329e8cb629b7c748d9cb52e8af872261f603da7d72",”contract_address”:””}}

Check Receipt

If we try to check the receipt of the transaction immediately, we’ll find the status is “2”, which means that the transaction is pending.

> curl -i -H ‘Content-Type: application/json’ -X POST http://localhost:8685/v1/user/getTransactionReceipt -d ‘{“hash”:”de1b586f022add23ce4f97329e8cb629b7c748d9cb52e8af872261f603da7d72"}’{"result":{"hash":"de1b586f022add23ce4f97329e8cb629b7c748d9cb52e8af872261f603da7d72","chainId":100,"from":"n1Q8mxXp4PtHaXtebhY12BnHEwu4mryEkXH","to":"n1RYagU8n3JSuV4R7q4Qs5gQJ3pEmrZd6cJ","value":"1000000000000000000","nonce":"1","timestamp":"1523183103","type":"binary","data":null,"gas_price":"1000000","gas_limit":"2000000","contract_address":"","status":2,"gas_used":""}}

Wait for a few seconds before checking the receipt again. The status will become “1”, which means that the transaction is accepted.

> curl -i -H ‘Content-Type: application/json’ -X POST http://localhost:8685/v1/user/getTransactionReceipt -d ‘{“hash”:”de1b586f022add23ce4f97329e8cb629b7c748d9cb52e8af872261f603da7d72"}’{"result":{"hash":"de1b586f022add23ce4f97329e8cb629b7c748d9cb52e8af872261f603da7d72","chainId":100,"from":"n1Q8mxXp4PtHaXtebhY12BnHEwu4mryEkXH","to":"n1RYagU8n3JSuV4R7q4Qs5gQJ3pEmrZd6cJ","value":"1000000000000000000","nonce":"1","timestamp":"1523183103","type":"binary","data":null,"gas_price":"1000000","gas_limit":"2000000","contract_address":"","status":1,"gas_used":"20000"}}

We’ve finished the transaction successfully!

Double Check

In theory, the two accounts’ states should have been updated.

> curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1Q8mxXp4PtHaXtebhY12BnHEwu4mryEkXH"}'{"result":{"balance":"4999998999999980000000000","nonce":"1","type":87}}> curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1RYagU8n3JSuV4R7q4Qs5gQJ3pEmrZd6cJ"}'{"result":{"balance":"5000001000000000000000000","nonce":"0","type":87}}

The first account will have 1 NAS taken and some Gas fee paid, while the second account will receive that 1 NAS.

All is going well! Congratulations on sending your first private net NAS successfully!

What’s next?

We will be covering the following guides in the next few blog posts to come.

  1. How to create a new account?
  2. How to ensure a successful transaction?
  3. How to deploy and call a smart contract?
  4. How to subscribe events?
  5. How to join Testnet and Mainnet?

And many more… Stay tuned!

--

--