Spectrum Development — Test Network Construction

zhu anray
zhu anray
Sep 9, 2018 · 4 min read

This article demonstrates the process of setting up the Spectrum test network on the server side. The operating system is CentOS 7.4, installed and configured to run the environment, and the Spectrum source has been downloaded and successfully compiled. For those unfamiliar with the above system environment setup, please refer to the article “Spectrum Chain installation for Linux”.

1. Initialize the genesis block

1.1 Prepare the creation block configuration file: genesis.json

First, create a new directory to hold genesis.json. Suppose the new directory is ~/spectrumchain and genesis.json is saved in ~/spectrumchain. The directory structure is as follows:

spectrumchain
└── genesis.json

Then, edit the genesis.json file as follows:

{
"config": {
"chainId": 20180908,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc" : {}
}

1.2 Writing genesis block

Once the genesis block configuration file is ready, you need to initialize the Spectrum chain and write the above genesis block information into Spectrum. Create a new directory in the spectrumchain directory to store the blockchain data. At this time, the directory structure is as follows::

spectrumchain
├── spectrumdata
└── genesis.json

Enter the spectrumchain directory and execute the initialization command:

$ cd spectrumchain
$ smc --datadir spectrumdata init genesis.json

After the command is executed, the genesis.json file is read and the genesis block is written into the blockchain based on its contents. If you see the following output, the initialization is successful.

Successfully wrote genesis state database=lightchaindata hash=5e1fc7…d790e0

After the initialization is successful, two folders, smc and keystore, will be generated in the data directory spectrumdata, where the block data is stored in smc/chaindata and the account data is stored in the keystore.

2. Start test network

After the initialization is complete, you can start the test network. Execute the following command to start the node:

$ smc --datadir spectrumdata --networkid 20180908 console

After executing the above command, the blockchain node is started and enters the console:

...Welcome to the Geth JavaScript console! instance: Smc/v0.5.1-unstable-9bbe523c/linux-amd64/go1.9.4 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 >

3. Use the test network

3.1 Create an account

Once the test network is up, you can create an account. The first running test network has no account, so you can enter eth.accounts in the console to see it:

> eth.accounts 
[]

You can use personal.newAccount () to create an account. You will be prompted to enter your password and confirm your password:

> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xc4cc8cd3c8a4e6a05cf21d182ebbf41868401275"

You can create multiple accounts, and then create an account to facilitate the demonstration of transactions between accounts:

> personal.newAccount() 
Passphrase:
Repeat passphrase:
"0x9358844d589f31c5c5515d65d156c3233d2fdc24"

Enter eth.accounts to see the two accounts:

> eth.accounts 
["0xc4cc8cd3c8a4e6a05cf21d182ebbf41868401275", "0x9358844d589f31c5c5515d65d156c3233d2fdc24"]

3.2 Check account balance

eth.getBalance () can view the balance of the specified account:

> eth.getBalance(eth.accounts[0]) 
0
> eth.getBalance(eth.accounts[1])
0

The current balance of both accounts is 0. To make the account balance, you can either transfer it from another account or get a reward by digging mines.

3.3 Start & stop mining

miner.start() can start digging:

> miner.start(1)

miner.stop() can stop digging:

> miner.stop()

The prize for mining goes to the miner’s account, called coinbase, which by default is the first account in the local account:

> eth.coinbase 
"0xc4cc8cd3c8a4e6a05cf21d182ebbf41868401275"

Once you get to the block, you have a balance in coinbase:

> eth.getBalance(eth.coinbase) 
150000000000000000000

3.4 Send a deal

Currently, the two account balances are respectively:

> eth.getBalance(eth.accounts[0]) 
150000000000000000000
> eth.getBalance(eth.accounts[1])
0

You can send a transaction to move five SMT from eth.accounts [0] to eth.accounts [1]:

> amount = web3.toWei(5,'ether') 
"5000000000000000000"
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:amount})
Error: authentication needed: password or unlock at web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at <anonymous>:1:1

An error was reported here because to send the transaction, you must first unlock the account:

> personal.unlockAccount(eth.accounts[0]) 
Unlock account 0xc4cc8cd3c8a4e6a05cf21d182ebbf41868401275 Passphrase:
true

Enter the password to unlock the account. Then the transaction is sent:

> amount = web3.toWei(5,'ether') 
"5000000000000000000"
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:amount})
INFO [09-08|20:08:40] Submitted transaction fullhash=0x250470a72b41109aab1b64cb487b8f5c5754a03203b9a4243eb97a1a6d8f2521 recipient=0x9358844d589f31C5c5515D65d156c3233D2fDC24
"0x250470a72b41109aab1b64cb487b8f5c5754a03203b9a4243eb97a1a6d8f2521"

At this time, the transaction has been submitted to the blockchain and returns the hash of the transaction, but it has not been processed yet, which can be verified using txpool.status:

> txpool.status 
{
pending: 1,
queued: 0
}

There is one pending transaction, pending for transactions that have been submitted but not yet processed.

To get the deal processed, a mine must be dug. Here we start digging, then wait until we get to a block and then stop:

> miner.start(1); admin.sleepBlocks(1); miner.stop(); 
true

When miner.stop () returns true and txpool.status is executed, the number of pending values is 0, indicating that the transaction has been processed:

> txpool.status 
{
pending: 0,
queued: 0
}

At this point, the transaction is in effect, and five SMT should have been received in eth.accounts[1]:

> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether') 
5

Information about this transaction can be viewed through eth.getTransaction ():

>eth.getTransaction("0x250470a72b41109aab1b64cb487b8f5c5754a03203b9a4243eb97a1a6d8f2521") 
{
blockHash: "0x1ce828d9dce08dc7868f3aab92f22d7115186515cdbc426d9a7b8a6d96ae979d",
blockNumber: 35,
from: "0xc4cc8cd3c8a4e6a05cf21d182ebbf41868401275",
gas: 90000,
gasPrice: 18000000000,
hash: "0x250470a72b41109aab1b64cb487b8f5c5754a03203b9a4243eb97a1a6d8f2521",
input: "0x",
nonce: 0,
r: "0x1577c4f7a78e6b37e5159b2488d5636feb2bbb5ef10e8b01484155dc3710e884",
s: "0x2837e1477e77431a7ab946de5d96ced06b71bab364453e1d36ffba7caf65acc3",
to: "0x9358844d589f31c5c5515d65d156c3233d2fdc24", transactionIndex: 0,
v: "0x267df7b",
value: 5000000000000000000
}

4. Connect to other nodes

To connect the nodes, three conditions must be met:

  1. The Internet is connected;
  2. Initialize the node using the same genesis.json;
  3. When the node is started, the -networkid option specifies the same networkid.

The existing two nodes, node 1 and node 2, networkid are both 20180908, and the connection is established through the following steps.

  1. Use admin.nodeinfo.enode in the node 1 console to get the enode information
> admin.nodeInfo.enode
"enode://cfbed48b370aa24e31728459c45f4e0751026ce678bfebb618743bb9db37cb3104d50b8a1db6d8f435bfb7b8b6ca033389f1a336a75f708b07b4c96dd54effd8@[::]:60303"

2. Use admin.addpeer() on the node 2 console to load the enode information for node 1

>admin.addPeer("enode://cfbed48b370aa24e31728459c45f4e0751026ce678bfebb618743bb9db37cb3104d50b8a1db6d8f435bfb7b8b6ca033389f1a336a75f708b07b4c96dd54effd8@172.17.0.12:60303") 
true

3. After the two nodes are successfully connected, node 2 will start to synchronize the block of node 1. After the synchronization is completed, any node will start mining and the other node will automatically synchronize the block.

SmartMesh Developer Community

Welcome to contribute, have the opportunity to get SMT rewards, and be invited to join smartmesh developer community.

zhu anray

Written by

zhu anray

SmartMesh Developer Community

Welcome to contribute, have the opportunity to get SMT rewards, and be invited to join smartmesh developer community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade