This is a creation in Article, where the information may have evolved or changed.
The
environment in this article is the development environment on the MAC, and other operating systems are very similar to this, still have reference value.
As a blockchain development engineer, the local development environment is essential. Let's first look at what tools are needed:
- Go-ethereum
- Solc
- Ethereum Wallet
The installation of these tools is detailed in the official documentation, which is not covered in more detail here.
Directory structure
The
files in the following list are already the current path.
Start by creating the following initial directory.
.├── data└── genesis.json
genesis.json: Initializes the configuration file for the private chain.
data: The directory where the blockchain data is stored.
Configuration file
Ethereum supports custom creation blocks, and to run a private chain, you must define your own creation block. The information on the Genesis block is written in the genesis.json following sections:
{ "config": { "chainId": 15, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0 }, "alloc": {}, "nonce": "0x0000000000000042", "difficulty": "0x020000", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa", "gasLimit": "0x4c4b40"}
Some of these parameters are explained:
- Alloc: The number of ether used to pre-set account numbers and accounts. Because private chain mining is easier, we don't need to pre-set the account. For example, {"0x880004bb64282fb01a3a2500ddf1f4bb5ad4b64a": {"balance": "100000000000000000000000000000"}}
- Nonce: A 64-bit random number for mining.
- Mixhash: A hash that is generated by a portion of the previous block, with the nonce used for mining.
- Difficulty: The difficulty of setting the current block, if the difficulty is too large, the CPU mining is very difficult, so the setting here is very small, do not have to cross with their own.
- Coinbase: The default miner's account for mining.
- Timestamp: Sets the timestamp of the Genesis block.
- Parenthash: The hash value of the previous chunk, because it is a Genesis block, so the value is 0.
- Extradata: Additional information, please fill in.
- Gaslimit: Sets the total consumption limit for gas, which is used to limit the sum of the transaction information that the block can contain. Because we are a private chain, we can write a larger, convenient development test.
Initialization
Next we need to write the initial information of the Genesis block into the blockchain, using geth init commands.
# geth --datadir "./data" --networkid 31415926 --rpc --rpccorsdomain "*" init ./genesis.json
The following information is roughly output:
INFO [03-12|19:36:02] Allocated cache and file handlesINFO [03-12|19:36:02] Writing custom genesis blockINFO [03-12|19:36:02] Persisted trie from memory databaseINFO [03-12|19:36:02] Successfully wrote genesis state
The directory structure at this point becomes as follows:
.├── data│ ├── geth│ │ ├── chaindata│ │ │ ├── 000001.log│ │ │ ├── CURRENT│ │ │ ├── LOCK│ │ │ ├── LOG│ │ │ └── MANIFEST-000000│ │ └── lightchaindata│ │ ├── 000001.log│ │ ├── CURRENT│ │ ├── LOCK│ │ ├── LOG│ │ └── MANIFEST-000000│ └── keystore└── genesis.json
keystoredirectories are used to hold account information, and geth directories are used to store block information.
Start
Let's start the private chain now!
# geth --datadir data --networkid 31415926 --rpc --rpccorsdomain "*" --nodiscover console
The output is the console that successfully entered geth :
Create an Account
Don't rush to dig the mine first, we have to create an account, otherwise dug out of ether where to go!
ether is what we call the etheric coin (ETH).
Let's see if there's an account already,
> eth.accounts[]
We'll create two new (for the transfer demo between the back accounts),
> personal.newAccount()Passphrase:Repeat passphrase:"0x880004bb64282fb01a3a2500ddf1f4bb5ad4b64a"> personal.newAccount()Passphrase:Repeat passphrase:"0x29a079bdbc6d4d122178fbe01558e5df2d008523"
Now we have two accounts,
> eth.accounts["0x880004bb64282fb01a3a2500ddf1f4bb5ad4b64a", "0x29a079bdbc6d4d122178fbe01558e5df2d008523"]
To say more, we now go to look at keystore the directory, more than two files, that is, we have just created the two account key (lost it, you are tantamount to losing coins)
.├── UTC--2018-03-12T11-46-09.722094891Z--880004bb64282fb01a3a2500ddf1f4bb5ad4b64a└── UTC--2018-03-12T11-48-04.771328116Z--29a079bdbc6d4d122178fbe01558e5df2d008523
Let's see if there's any ether in the account.
> eth.getBalance(eth.accounts[0])0
Digging mine
There is no ether in the beginning of the account, so all of them need to be mined to get it. Use the miner.start() command to open mining, the default ether is saved to the eth.coinbase account, that is, the first account.
> eth.coinbaseINFO [03-12|19:55:13] Etherbase automatically configured address=0x880004Bb64282fb01A3A2500DDF1F4bB5AD4b64A"0x880004bb64282fb01a3a2500ddf1f4bb5ad4b64a"
If we want to deposit the mines we have dug into another account, you can:
> miner.setEtherbase(eth.accounts[1])true
All right, let's start digging.
> miner.start(1)> INFO [03-12|20:00:44] Commit new mining work number=1 txs=0 uncles=0 elapsed=513.188µsINFO [03-12|20:00:47] Generating DAG in progress epoch=0 percentage=0 elapsed=1.668sINFO [03-12|20:00:49] Generating DAG in progress epoch=0 percentage=1 elapsed=3.368s
Wait until the percentage reaches 100 to be able to dig out, please wait patiently ~
INFO [03-12|20:04:15] Successfully sealed new block number=4 hash=81db99…4db568INFO [03-12|20:04:15] mined potential block number=4 hash=81db99…4db568
When the little hammerhead comes in, it means you're digging!
So we're going to suspend mining,
> miner.stop()true
Then look at the account balance,
> eth.getBalance(eth.accounts[0])85000000000000000000
Do not be frightened by the number of zeros, here the default display for wei units, and 1 ether = 10^18 wei , so we convert the unit immediately clear,
> web3.fromWei(eth.getBalance(eth.accounts[0]), 'ether')85
Well, actually, we're digging 85 ether~ right now.
Transfer
Now that you have the money in hand, give your best friends a spot.
Remember to unlock yourself before the transfer, analogy with the bank card transfer, you have to enter the password before transfer.
> personal.unlockAccount(eth.accounts[0])Unlock account 0x880004bb64282fb01a3a2500ddf1f4bb5ad4b64aPassphrase:true
I decided to transfer to my good friend 8 ether!
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(8,'ether')})INFO [03-12|20:24:15] Submitted transaction fullhash=0x996a3037b75585415ece5b1dc28181833691760176b3f24066c93e7093a967e5 recipient=0x29a079BdbC6D4d122178FBe01558E5DF2D008523"0x996a3037b75585415ece5b1dc28181833691760176b3f24066c93e7093a967e5"
We can see that the transfer transaction is only submitted to the blockchain, and who will execute the transaction? Miners
We still have to open the mining model and execute the transfer transaction. And then we'll see how many ether there are in my good friend's account,
> eth.getBalance(eth.accounts[1])8000000000000000000
Ethereum Wallet
Thinking back first time to install the Ethereum wallet, in order to download the Ethereum main network of full-node data, a full 5 days, and then stuck in the last few blocks alive, finally abandoned therapy ...
This part of the torture experience, but later with the Ethfans Spark node plan, import the Super node, peers suddenly increased, download indeed much faster. To get back to the point, this time we need to let Ethereum Wallet connect to our private chain and then quickly develop and deploy smart contracts using the Wallet's UI interface.
/Applications/Ethereum\ Wallet.app/Contents/MacOS/Ethereum\ Wallet --rpc "/path/to/data/geth.ipc"
Note that the value of the parameter here is in the --ipc data contents of the project just now geth.ipc , is to let the wallet connected to the local private chain.
Abnormal conditions
Ore-digging anomalies
If Miner.start () returns null directly, see if the account has not been created yet.
If you have previously built a local private chain, please remove the previous. Ethash directory, which is in the home directory of the currently logged-in user.
Summarize
The author first chooses the simplest development process to practice: Download the official Ethereum Wallet and then synchronize the full node of the main network, then develop the contract and deploy it. But download the whole node experience is really do not want to recall, day and night synchronization for several days have not synchronized full (from the current new block is always poor so few pieces of synchronization, of course, later resolved). Later asked the technical group friends told me that I can use Remix + metamask This lightweight development process, is indeed convenient, debugging the development of a browser-based Remix IDE, the deployment of contracts with browser-based plug-in metamask.
But sometimes Remix load very slowly, and this solution is not the network (most of them have WIFI can be connected, but there is really no network ...) The private chain is a good solution to the two problems that plague me, and in the process of building and using the private chain, is a more complete blockchain experience ~
Welcome to the public number: "Bit buckle", explore the world of blockchain with me.