# Setting Up Nucoin AI+POA Blockchain Network
## OS : Ubuntu 20.xx Server
```
# Install Nightly Build
rustup toolchain install nightly
# Using Rust Nightly Build
rustup use nightly
# Update your rust using
# rustup update
cd /root/
# Get Substrate Code
curl https://getsubstrate.io -sSf | bash
cd substrate
# Create a New Node (Nucoin)
substrate-node-new nucoin rezamt
# The previous command generates a new node template for your node
# Extracts the zip file into (nucoin folder)
cd /root/nucoin
make init # Install WASM Library
make release # Build the Susbtraet Node
```
`rustup toolchain install nightly`
### Nucoin Supernode(s) Folder Structure
```bash
mkdir -p /nucoin # Home
mkdir -p /nucoin/bin # Binary
mkdir -p /nucoin/chains # Ledger and Data (on AWS it should go to a NFS Mount or EBS Volume )
# Update your OS Path in. ~/.bashrc
export PATH=$PATH:/nucoin/bin
```
### Nucoin Chain Code
```bash
cp ./target/release/node-template /nucoin/bin/node
# Check the Node Binary
/nucoin/bin/node --help
```
## Purge any chain data from previous runs
This is required if you want to re-start again.
```
/nucoin/bin/node purge-chain --base-path /nucoin/data --chain local
Are you sure to remove "/nucoin/data/chains/local_testnet/db"? [y/N]: y
"/nucoin/data/chains/local_testnet/db" did not exist.
```
###### Note:
By default Substrate uses "RocksDb" as a Database for Ledger and it will be stored under:
```
example (when running /tmp/substrategFZ3HC/chains/dev/db )
/tmp/substrategFZ3HC/chains/dev/db
```
## Running Nucoin Node
## Single Node local Development
`/nucoin/bin/node --dev --tmp`
If you need to purge the databasem it will be under: `/tmp/substrateXXXXXX/chains/dev/db`
## Multi-node Testnet with 2 Authority Node + 1 NonAuthority Node
#### Start Super Node 1 - Mode: Authority & BootNode
```
/nucoin/bin/node \
--base-path /nucoin \
--chain local \
--name super_node_1 \
--port 30333 \
--ws-port 9945 \
--rpc-port 9933 \
--node-key 0000000000000000000000000000000000000000000000000000000000000001 \
--validator
:: OUTPUT ::
2020-12-06 09:10:23 Substrate Node
2020-12-06 09:10:23 ✌️ version 2.0.0-d96899e-x86_64-linux-gnu
2020-12-06 09:10:23 ❤️ by Anonymous, 2017-2020
2020-12-06 09:10:23 📋 Chain specification: Local Testnet
2020-12-06 09:10:23 🏷 Node name: super_node_1
2020-12-06 09:10:23 👤 Role: AUTHORITY
2020-12-06 09:10:23 💾 Database: RocksDb at /nucoin/chains/local_testnet/db
2020-12-06 09:10:23 ⛓ Native runtime: node-template-1 (node-template-1.tx1.au1)
2020-12-06 09:10:23 🔨 Initializing Genesis block/state (state: 0x1b24…4ae6, header-hash: 0x9646…3e8b)
2020-12-06 09:10:23 👴 Loading GRANDPA authority set from genesis on what appears to be first startup.
2020-12-06 09:10:23 ⏱ Loaded block-time = 6000 milliseconds from genesis on first-launch
2020-12-06 09:10:23 Using default protocol ID "sup" because none is configured in the chain specs
2020-12-06 09:10:23 🏷 Local node identity is: 12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp
2020-12-06 09:10:23 📦 Highest known block at #0
2020-12-06 09:10:23 〽️ Prometheus server started at 127.0.0.1:9615
2020-12-06 09:10:23 Listening for new connections on 127.0.0.1:9945.
# Folder is now looks like:
├── bin
│ └── node
├── chains
│ └── local_testnet
│ ├── db
│ │ ├── 000006.log
│ │ ├── CURRENT
│ │ ├── db_version
│ │ ├── IDENTITY
│ │ ├── LOCK
│ │ ├── LOG
│ │ ├── MANIFEST-000005
│ │ ├── OPTIONS-000028
│ │ └── OPTIONS-000030
│ ├── keystore
│ └── network
```
#### Start Super Node 2 - Mode: Authority
```
/nucoin/bin/node \
--base-path /nucoin \
--chain local \
--name super_node_2 \
--port 30334 \
--ws-port 9946 \
--rpc-port 9934 \
--validator \
--bootnodes /ip4/192.168.44.9/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp
```
NOTE:
In regards to boot node
1. Node1's IP Address, probably 127.0.0.1
1. Node1's Port, she specified 30333
1. Node1's Peer ID, copied from her log output.
1. If you run the node using --alice, --bob it means your are using substrate default actors - we must remove these actors from nucoin chain.
``` bash
--alice
Shortcut for `--name Alice --validator` with session keys for `Alice` added to keystore
--bob
Shortcut for `--name Bob --validator` with session keys for `Bob` added to keystore
```
Once the peer joined the network

###### Important Node
None of the Supernodes listed above generates or valdiates any blocks !!!!
## Generate Nucoin Super Nodes Keys
We need two keys (depends to the number of nodes and how we are planning to run them) - the following sections shows how to generate the key and use them in Nucoin SuperNodes.
###### Important Note
1- Depends to the type of Consensus (Block authoring and Finality) - we need at least 2 keys for every super node.
2- The Secret Phrase and Seed Must be stored in Secure Storage.
```bash
# Install Subkey (Substrate Node KeyGen)
# cargo install --force subkey --git https://github.com/paritytech/substrate --version 2.0.0
# If you are using standard nucoin node template the /nucoin/bin/subkey contains this binary
# Generate Keys
# Super Node 1
# This key will be used by Aura for block production.
root@supernode01:/nucoin# subkey generate --scheme sr25519
Secret phrase `wife scrub bike sponsor thank cherry response bullet street trim kitten answer` is account:
Secret seed: 0xedd72c66dea83f846d4ec51068acfc300705b13a6ad574f7212c8c601c357100
Public key (hex): 0x26bcc2405a2ac84ab3f8454deb265581eb80769e1287781384475cb5fe4a4d04
Account ID: 0x26bcc2405a2ac84ab3f8454deb265581eb80769e1287781384475cb5fe4a4d04
SS58 Address: 5CwVkSm5S2qqVUiotFwbrbp1bpnCjfn4mE4Pnq6xviDcXZYa
# This key will be used by GRANDPA for block finalization.
root@supernode02:/nucoin# subkey inspect --scheme ed25519 "wife scrub bike sponsor thank cherry response bullet street trim kitten answer"
Secret phrase `wife scrub bike sponsor thank cherry response bullet street trim kitten answer` is account:
Secret seed: 0xedd72c66dea83f846d4ec51068acfc300705b13a6ad574f7212c8c601c357100
Public key (hex): 0x8c4ef6f2ca68a12578ddb900b3eede7a9ec24a83568917e7ce8e4f5b29b7d8a7
Account ID: 0x8c4ef6f2ca68a12578ddb900b3eede7a9ec24a83568917e7ce8e4f5b29b7d8a7
SS58 Address: 5FEg22D8TsJZmS9Mam9oeNMNzttD5gwgwy2XgKd9ww1stNnd
# Super Node 2
# This key will be used by Aura for block production.
root@supernode02:/nucoin# subkey generate --scheme sr25519
Secret phrase `scissors palm easy extend pride similar stem photo another blade thrive off` is account:
Secret seed: 0x2002db73e8fe2096ad150be90099e1ac44cca5c7bc147a990e5e3f91d6712c59
Public key (hex): 0x1e8aa00ec8b7f53685459b9f26fd1b379e2894ce0fdf7df92a2a7e9c972ced3a
Account ID: 0x1e8aa00ec8b7f53685459b9f26fd1b379e2894ce0fdf7df92a2a7e9c972ced3a
SS58 Address: 5CkkUSmoU6iXagHaz94YRSTeK69Qu8mpBktiaYo6KWM4Je79
# This key will be used by GRANDPA for block finalization.
root@supernode02:/nucoin# subkey inspect --scheme ed25519 "scissors palm easy extend pride similar stem photo another blade thrive off"
Secret phrase `scissors palm easy extend pride similar stem photo another blade thrive off` is account:
Secret seed: 0x2002db73e8fe2096ad150be90099e1ac44cca5c7bc147a990e5e3f91d6712c59
Public key (hex): 0x431bac668a382dec7603b638fc35eeb46c9b67b0c110f0330bc0d3d88d68f0dd
Account ID: 0x431bac668a382dec7603b638fc35eeb46c9b67b0c110f0330bc0d3d88d68f0dd
SS58 Address: 5DahHYuk858zger1P5gC2atXUrGVzY4sbZBa3ZfgdSDxv4Kz
```
## Create NuCoin Chain Spec
There are 2 options:
1- Build the chain spec into the code (Rust Code - Once the code is mature )
2- Passing the build spec as a parameter (for initial testing and development)
###### Important Node
When you export a chainspec , the chain runtime (WASM) blob is included in the chain spec.
```bash
# We use the local chain spec - coming as a default spec for starting point
/nucoin/bin/node build-spec --disable-default-bootnode --chain local > nucoin-chain.json
```
##### Visual Presentation of Chain Spec

All we need to do is change the authority addresses listed (currently default `Alice` and `Bob`) to our own addresses that we generated in the previous step. The sr25519 addresses go in the aura section, and the ed25519 addresses in the grandpa section. You may add as many validators as you like. For additional context, read about keys in Substrate.
We have now a customized chain:

##### Convert Chain Spec to Raw Format
```bash
root@supernode01:/nucoin# node build-spec --chain=chain.json --raw --disable-default-bootnode > nucoin-chain.json
2020-12-06 10:55:10 Building chain spec
root@supernode02:/nucoin# node build-spec --chain=chain.json --raw --disable-default-bootnode > nucoin-chain.json
2020-12-06 10:55:12 Building chain spec
#### OUTPUT ####
.
├── bin
│ ├── node
│ └── subkey
├── chain.json
├── chains
└── nucoin-chain.json
```
### Starting Super Nodes with Customized Chain ( NuCoin Chain - Test Network )
Once your node is running, you will again notice that no blocks are being produced. At this point,
you need to add your keys into the keystore. Remember you will need to complete these steps for each node in your network.
You will add two types of keys for each node: Aura and GRANDPA keys.
- Aura keys are necessary for block production
- GRANDPA keys are necessary for block finalization
#### Step 1: Starting Node using NuCoin Chain Spec
```bash
# On Super Node 1
/nucoin/bin/node \
--base-path /nucoin \
--chain ./nucoin-chain.json \
--port 30333 \
--ws-port 9944 \
--rpc-port 9933 \
--validator \
--rpc-methods=Unsafe \
--name super_node_1
# On Super Node 2
/nucoin/bin/node \
--base-path /nucoin \
--chain ./nucoin-chain.json \
--port 30333 \
--ws-port 9944 \
--rpc-port 9933 \
--validator \
--rpc-methods=Unsafe \
--name super_node_2
### OUTPUT ####
2020-12-06 11:05:03 Substrate Node
2020-12-06 11:05:03 ✌️ version 2.0.0-d96899e-x86_64-linux-gnu
2020-12-06 11:05:03 ❤️ by Anonymous, 2017-2020
2020-12-06 11:05:03 📋 Chain specification: NuCoin TestNetwork
2020-12-06 11:05:03 🏷 Node name: super_node_1
2020-12-06 11:05:03 👤 Role: AUTHORITY
2020-12-06 11:05:03 💾 Database: RocksDb at /nucoin/chains/nucoin_test/db
2020-12-06 11:05:03 ⛓ Native runtime: node-template-1 (node-template-1.tx1.au1)
2020-12-06 11:05:03 🔨 Initializing Genesis block/state (state: 0xbf93…5692, header-hash: 0x0c7b…7f05)
2020-12-06 11:05:03 👴 Loading GRANDPA authority set from genesis on what appears to be first startup.
2020-12-06 11:05:03 ⏱ Loaded block-time = 6000 milliseconds from genesis on first-launch
2020-12-06 11:05:03 Using default protocol ID "sup" because none is configured in the chain specs
2020-12-06 11:05:03 🏷 Local node identity is: 12D3KooWJg7d3WmEyzYaGGSi8v3GMaBPsLXtqmX42YwYXzDf7Wyq
2020-12-06 11:05:03 📦 Highest known block at #0
2020-12-06 11:05:03 〽️ Prometheus server started at 127.0.0.1:9615
2020-12-06 11:05:03 Listening for new connections on 127.0.0.1:9944.
2020-12-06 11:05:39 Substrate Node
2020-12-06 11:05:39 ✌️ version 2.0.0-d96899e-x86_64-linux-gnu
2020-12-06 11:05:39 ❤️ by Anonymous, 2017-2020
2020-12-06 11:05:39 📋 Chain specification: NuCoin TestNetwork
2020-12-06 11:05:39 🏷 Node name: super_node_2
2020-12-06 11:05:39 👤 Role: AUTHORITY
2020-12-06 11:05:39 💾 Database: RocksDb at /nucoin/chains/nucoin_test/db
2020-12-06 11:05:39 ⛓ Native runtime: node-template-1 (node-template-1.tx1.au1)
2020-12-06 11:05:40 🔨 Initializing Genesis block/state (state: 0xbf93…5692, header-hash: 0x0c7b…7f05)
2020-12-06 11:05:40 👴 Loading GRANDPA authority set from genesis on what appears to be first startup.
2020-12-06 11:05:40 ⏱ Loaded block-time = 6000 milliseconds from genesis on first-launch
2020-12-06 11:05:40 Using default protocol ID "sup" because none is configured in the chain specs
2020-12-06 11:05:40 🏷 Local node identity is: 12D3KooWSTQERSzNRxZgjDZN5NHGQkNSfAKJj1qwu9pKnF6fPGT4
2020-12-06 11:05:40 📦 Highest known block at #0
2020-12-06 11:05:40 〽️ Prometheus server started at 127.0.0.1:9615
2020-12-06 11:05:40 Listening for new connections on 127.0.0.1:9944.
2020-12-06 11:05:41 🔍 Discovered new external address for our node: /ip4/192.168.44.10/tcp/30333/p2p/12D3KooWSTQERSzNRxZgjDZN5NHGQkNSfAKJj1qwu9pKnF6fPGT4
```
###### Visual Presentation

#### Step 2: Adding Keys to the Keystore
In this part - we are going to use `curl` and key file to add the keys to the node keystore.
###### On Super Node 1
```bash
cat >>/nucoin/aura.json<<EOF
{
"jsonrpc":"2.0",
"id":1,
"method":"author_insertKey",
"params": [
"aura",
"wife scrub bike sponsor thank cherry response bullet street trim kitten answer",
"0x26bcc2405a2ac84ab3f8454deb265581eb80769e1287781384475cb5fe4a4d04"
]
}
EOF
cat >>/nucoin/gran.json<<EOF
{
"jsonrpc":"2.0",
"id":1,
"method":"author_insertKey",
"params": [
"gran",
"wife scrub bike sponsor thank cherry response bullet street trim kitten answer",
"0x8c4ef6f2ca68a12578ddb900b3eede7a9ec24a83568917e7ce8e4f5b29b7d8a7"
]
}
EOF
# Submit a new key via RPC, connect to where your `rpc-port` is listening
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d "@/nucoin/aura.json"
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d "@/nucoin/gran.json"
```
###### Visual Presentation

###### On Super Node 2
```bash
cat >>/nucoin/aura.json<<EOF
{
"jsonrpc":"2.0",
"id":1,
"method":"author_insertKey",
"params": [
"aura",
"scissors palm easy extend pride similar stem photo another blade thrive off",
"0x1e8aa00ec8b7f53685459b9f26fd1b379e2894ce0fdf7df92a2a7e9c972ced3a"
]
}
EOF
cat >>/nucoin/gran.json<<EOF
{
"jsonrpc":"2.0",
"id":1,
"method":"author_insertKey",
"params": [
"gran",
"scissors palm easy extend pride similar stem photo another blade thrive off",
"0x431bac668a382dec7603b638fc35eeb46c9b67b0c110f0330bc0d3d88d68f0dd"
]
}
EOF
# Submit a new key via RPC, connect to where your `rpc-port` is listening
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d "@/nucoin/aura.json"
curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d "@/nucoin/gran.json"
```
###### Visual Presentation

### Consensus

## REFRENCES
[Building Rust Node](https://hackernoon.com/build-your-blockchain-with-parity-substrate-a8ddc4872ed7)
[install-development-tools-linux](https://ostechnix.com/install-development-tools-linux/)