Before reading this article, you should have some knowledge of the Ethereum language. If you don’t already, I suggest you read what Ethereum is.


Go – Ethereum client installed

The Go-Ethereum client, often referred to as Geth, is a command line interface that executes the full Ethereum node implemented on Go. Geth benefits from the multi-platform nature of the Go language and can be used on multiple platforms (such as Windows, Linux, Mac). Geth is the concrete implementation of ethereum protocol. Through Geth, you can realize various functions of Ethereum, such as creating and deleting accounts, opening mining, transferring ether coins, deploying and executing smart contracts, and so on. So, we chose the GEth tool for development. Since I am a MAC, I prefer to use MAC for development. The geTH installation is as follows:

brew tap ethereum/ethereum
brew install ethereum
Copy the code


Check whether the installation is successful

geth --help
Copy the code

If some help commands are displayed, the installation is successful. For other platforms, see Geth installation

Set up a private chain

Ethereum supports custom genesis blocks. To run a private chain, we need to define our own genesis blocks. The genesis block information is written in a JSON configuration file. Start by saving the following to a JSON file, such as genesis. Json.

The content of the JSON file is as follows:

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


Initialization: Write creation block

After preparing the Trands block JSON configuration file, initialize the blockchain and write the above trands block information into the blockchain. The first step is to create a new directory, data0, to store blockchain data (in fact, this directory, Data0, is a root node. When we generate a root node based on genesis.json, other people can connect to the root node and make transactions). The data0 directory structure is shown in the figure below:

Next go to the privatechain directory and execute the initialization command:

cd privatechain
geth --datadir data0 init genesis.json
Copy the code

The -datadir option is followed by a directory name, data0 in this case, to specify the directory where the data is stored. Genesis. Json is the parameter of init.


Running the above command will read the genesis. Json file and, based on its contents, write the Genesis block to the blockchain. If “Successfully wrote genesis State” is displayed in the log information, the initialization is successful.


After the initialization, the following directories are displayed:

Geth/ChainData stores block data, and keystore stores account data.


Start the private chain node

After initialization, you have a private chain of your own. Then you can start your own private chain node and do some operations. To start the node, enter the following command in the terminal:

geth --datadir data0 --networkid 1108 console
Copy the code

The body of the command above is geth console, which starts the node and goes to the interactive console. – datadir specifies data0 as the data directory, and – networkid is followed by a number, 1108, which specifies the networkid of the private chain as 1108. The network id is used when connecting to other nodes. The network id of the ethereum public network is 1. In order not to conflict with the public network, you need to specify your own network id when running the privateChain node.


After running the above command, the blockchain node is launched and the Javascript Console is entered:

This is an interactive Javascript execution environment in which Javascript code can be executed, where > is the command prompt. There are also Javascript objects built into the environment for manipulating Ethereum that can be used directly. These objects mainly include:


  • Eth: contains a number of methods related to operating blockchain
  • Net: contains the following methods to check the status of p2p networks
  • Admin: Contains some methods related to the management node
  • Miner: Contains methods for starting and stopping mining
  • Personal: Mainly contains methods for managing accounts
  • Txpool: contains some methods to view the transaction memory pool
  • Web3: contains the above objects, as well as some methods for unit conversion


How to play the Javascript Console

Once in the Ethereum Javascript Console, you can use the built-in objects to perform a variety of operations, such as viewing blocks and transactions, creating accounts, mining, sending transactions, deploying smart contracts, and so on. Here are a few common functions. In the following operations, the command followed by a > represents the command executed in the Javascript Console.


Create account

You can verify this by entering eth. Accounts in the JS console:

> eth.accounts
[]
Copy the code


There is no account at this point, so create an account using the Personal object:

> personal.newAccount()
> Passphrase:
> Repeat passphrase:
"0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f"
Copy the code


Passphrase is simply a password, and when you type your password twice, you create an account. Execute the command again:

> personal.newAccount()
> Passphrase:
> Repeat passphrase:
"0x46b24d04105551498587e3c6ce2c3341d5988938"
Copy the code


When you look at the accounts, there are two.

> eth.accounts
["0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f"."0x46b24d04105551498587e3c6ce2c3341d5988938"]
Copy the code


By default, the account is saved in the keystore folder of the data directory. Check the directory structure and find two more files in data0/keystore. These two files correspond to the two accounts created just now. These files are text files in JSON format and can be opened to view.


The format of the JSON file is as follows:

{
  "address": "4a3b0216e1644c1bbabda527a6da7fc5d178b58f"."crypto": {
    "cipher": "aes-128-ctr"."ciphertext": "238d6d48126b762c8f13e84622b1bbb7713f7244c2f24555c99b76396fae8355"."cipherparams": {
      "iv": "d0f5a3d3e6c1eeec77bf631bc938725d"
    },
    "kdf": "scrypt"."kdfparams": {
      "dklen": 32."n": 262144,
      "p": 1,
      "r": 8,
      "salt": "70dc72c4eb63bea50f7637d9ff85bb53f6ca8ace17f4245feae9c0bc9abaad82"
    },
    "mac": "bd7fc0c937c39f1cbbf1ca654c33b53d7f9c644c6dacfeefe1641d2f3decea04"
  },
  "id": "57803d82-0cd4-4a78-9c29-9f9252fdcf60"."version": 3}Copy the code


Check your account balance

The ETH object provides a method to view the account balance:

> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
Copy the code





Start & stop mining

Start mining by miner.start() :

> miner.start(10)
Copy the code

The start parameter indicates the number of threads used for mining. This process is a little slow. When the progress reaches 100%, mining will start. At this time, the screen will be flooded with mining information.


If you want to stop mining after the progress has reached 100%, you can enter it in the JS Console

Miner.stop () : Note: the input characters will be washed out by the mining brush information, it does not matter, as long as you complete the input of miner.stop() and press enter, you can stop mining.


Mining a block rewards five Ether coins, which go into the miner’s account, called Coinbase, which by default is the first account in the local account:

> eth.coinbase
"0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f"
Copy the code


Now coinbase is account 0, to enable mining rewards to go to other accounts, use miner.setetherbase () to set other accounts to Coinbase:

> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0x46b24d04105551498587e3c6ce2c3341d5988938"
Copy the code


After the block is mined, account 0 should have a balance:

> the eth. The getBalance (eth. Accounts [0]) 2.31 e+21Copy the code


> web3.fromWei(eth.getBalance(eth.accounts[0]),'ether')
2310
Copy the code


Send a deal

So far, the balance of account 1 is 0:

> eth.getBalance(eth.accounts[1])
0
Copy the code


It is possible to transfer 10 Ether from account 0 to account 1 by sending a single transaction:

> amount = web3.toWei(10,'ether')
"10000000000000000000"
> 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
Copy the code


The account is locked every once in a while. In order to send a transaction, the account must be unlocked first. Since we want to send a transaction from account 0, account 0 needs to be unlocked:

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f
Passphrase: 
true
Copy the code

To unlock the account, enter the password you set when creating the account. Then send the transaction:

> amount = web3.toWei(10,'ether')
"10000000000000000000"
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
INFO [03-07|11:13:11] Submitted transaction                    fullhash=0x1b21bba16dd79b659c83594b0c41de42debb2738b447f6b24e133d51149ae2a6 recipient=0x46B24d04105551498587e3C6CE2c3341d5988938
"0x1b21bba16dd79b659c83594b0c41de42debb2738b447f6b24e133d51149ae2a6"
Copy the code


Let’s check the balance in account 1:

> eth.getBalance(eth.accounts[1])
0
Copy the code

The transaction has been committed to the blockchain but has not yet been processed. This can be verified by looking at txPool:

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

There is a pending transaction. Pending is a transaction that has been submitted but not yet processed.


Mining is necessary for transactions to be processed. Here we start mining and wait for a block to be struck before stopping:

> miner.start(1); admin.sleepBlocks(1); miner.stop();Copy the code


When miner.stop() returns true, the number of pending transactions in txPool should be 0, indicating that the transaction has been processed and account 1 should receive coins:

> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether'10)Copy the code


View transactions and blocks

The ETH object encapsulates a method for viewing transaction and block information.


To view the current total number of blocks:

> eth.blockNumber
463
Copy the code


View blocks by block number:

> eth.getBlock(66)
{
  difficulty: 135266,
  extraData: "0xd783010802846765746886676f312e31308664617277696e",
  gasLimit: 3350537,
  gasUsed: 0,
  hash: "0x265dfcc0649bf6240812256b2b9b4e3ae48d51fd8e43e25329ac111556eacdc8",
  logsBloom: "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000",
  miner: "0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f",
  mixHash: "0xaf755722f62cac9b483d3437dbc795f2d3a02e28ec03d39d8ecbb6012906263c",
  nonce: "0x3cd80f6ec5c2f3e9",
  number: 66,
  parentHash: "0x099776a52223b892d13266bb3aec3cc04c455dc797185f0b3300d39f9fc0a8ec",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 535,
  stateRoot: "0x0c9feec5a201c8c98618331aecbfd2d4d93da1c6064abd0c41ae649fc08d8d06",
  timestamp: 1520391527,
  totalDifficulty: 8919666,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
Copy the code


View trades using trade hash:

> eth.getTransaction("0x1b21bba16dd79b659c83594b0c41de42debb2738b447f6b24e133d51149ae2a6")
{
  blockHash: "0x1cb368a27cc23c786ff5cdf7cd4351d48f4c8e8aea2e084a5e9d7c480449c79a",
  blockNumber: 463,
  from: "0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0x1b21bba16dd79b659c83594b0c41de42debb2738b447f6b24e133d51149ae2a6",
  input: "0x",
  nonce: 0,
  r: "0x31d22686e0d408a16497becf6d47fbfdffe6692d91727e5b7ed3d73ede9e66ea",
  s: "0x7ff7c14a20991e2dfdb813c2237b08a5611c8c8cb3c2dcb03a55ed282ce4d9c3",
  to: "0x46b24d04105551498587e3c6ce2c3341d5988938",
  transactionIndex: 0,
  v: "0x38",
  value: 10000000000000000000
}
Copy the code

Come and have a try!

Quant. la/Article/Vie…