Original: How to Write, Deploy, and Interact with Ethereum Smart contracts on a Private Blockchain
Author: Jack_schultz
No, I'm flying.
Abstract: The author gives a very long length of the complete code, related details steps, user interface, etc. of the smart contracts that are written, deployed, and interacted with Ethereum on a private blockchain. The author hopes that with this article, you can write and deploy a smart contract on your own private ethereum blockchain, which is translated below.
The rule here is that if you read through this article, you must deploy a smart contract on your own private ethereum blockchain. All the code I use is given on GitHub, so you have no reason not to.
But if you don't follow the rules and just want to read it, hopefully this will help provide a perspective on how to make a blockchain application from scratch.
Finally, you'll create a private ethereum blockchain, connect two different nodes as peers, write and compile a smart contract, have a Web interface that allows users to ask questions, deploy problems on the blockchain, and then let the user answer.
If you're confused, have an error, or want to say something else, write a comment, get in touch with Twitter, or make a comment.
This is GitHub's repo, so go ahead and fork it (if you don't want to copy and paste all the code), and if you have an update that you want to share, I'll put it in the Readme file. private block chain creation
If you want a complete explanation of the field, look at the solution to the stack overflow. The difficulty in this case is very low, because you do not want to wait a long time on the test network, chunks can be dug out, and then the value of gaslimit is high enough to allow a node in the chunk to do the work to handle each transaction.
To open a terminal, make sure that the Geth (Ethereum client) is installed in any way appropriate for your operating system, and then CD (DOS command) to the folder where the Genesis.json is saved. Run the following command to initialize the blockchain for that node.
$ geth--datadir "/users/username/library/priveth" Init genesis.json
- DATADIR Specifies the location of all data in the blockchain. On Mac operating systems, the default is the ~/library/ethereum directory. Because multiple nodes are running, they cannot be shared with the same data folder, so they need to be specified. Linux and Windows machines have different default datadir, so see where these data should generally be located.
After running the init command with the Genesis.json file, check the--datadir directory and see a bunch of files, so feel free to look around. It's not necessary now, but we'll have to look at it eventually.
For such a blockchain, multiple nodes are required. To make the blockchain a peers, they need to have the same founding file. So to run from the same directory and the same command above, but this time using a different datadir.
geth--datadir "/users/username/library/priveth2" Init genesis.json
1 2 All the code here , it will work in the same directory. The code is the same, but with command-line options, these processes can be distinguished by command-line arguments.
Initializes a chain of two nodes.
1 2There are more options here.
-networkid similar to the Genesis.json file, what you need to do here is to make sure that you do not use network ID 1-4.
-PORT specifies the port to which the. ipc file will be used. This is how the database is connected using the Web3.js library, and the default port is 30303. So keep it in that area, but this is the first node, so its port is 30301.
Nodiscover told Geth not to find peers at first. This is really important in this case. This is a private network. You do not want the nodes to attempt to connect to other nodes without specifying them, and you do not want these nodes to be discovered without telling them.
In the case where the first Geth node is running, the same command is run at a different terminal with a second-datadir, and the node runs on a different port.
Run the same command in the console of another node.
Create a new account.
Since this is the first account created by the node, you will see that it is also listed in
> Eth.coinbase
0x538341f72db4b64e320e6c7c698499ca68a6880c
By running another piece of information that can be crawled on the console
> Personal.listwallets
[{
accounts:[{
address: "0x538341f72db4b64e320e6c7c698499ca68a6880c",
URL: "Keystore:///users/jackschultz/library/ethprivlocal/keystore/ Utc--2017-12-09t16-21-48.056824000z--538341f72db4b64e320e6c7c698499ca68a6880c "
}],
status:" Locked ",
URL: "keystore:///users/jackschultz/library/ethprivlocal/keystore/ Utc--2017-12-09t16-21-48.056824000z--538341f72db4b64e320e6c7c698499ca68a6880c "
}]
So sad. This is what we expect, to start the console on a flag other than 1-4 network ID and nodiscover. This means that each node needs to be told to connect to another node with a specific command. Do this by sharing the Enode address.
> Admin.nodeInfo.enode
"enode:// 13b835d68917bd4970502b53d8125db1e124b466f6473361c558ea481e31ce4197843ec7d8684011b15ce63def5eeb73982d04425af3a0b6f3437a030 878C8A9 @ [:]:30301 discport = 0 "
This is the Enode information that Geth uses to connect to different nodes, where they can share transactions and successfully tap information.
To use this URL to connect to a node, you need to call the Addpeer function.
If you want to copy the return value from one of the nodes Admin.nodeInfo.enode, run the following command in another node.
> Admin.addpeer ("enode:// 13b835d68917bd4970502b53d8125db1e124b466f6473361c558ea481e31ce4197843ec7d8684011b15ce63def5eeb73982d04425af3a0b6f3437a030 878C8A9 @ [::]:30301. DiscPort = 0 ")
This tells the node how to reach the other node and requests that the other node be connected, and they will all be peers to each other. To test, run the admin.peers command on two nodes, and you will see that they are connected together. The code is as follows:
> Admin.peers
[{
caps: ["ETH/63"],
ID: " 99bf59fe629dbea3cb3da94be4a6cff625c40da21dfffacddc4f723661aa1aa77cd4fb7921eb437b0d5e9333c01ed57bfc0d433b9f718a2c95287d354 2f2e9a8 ",
Name:" geth/v1.7.1-stable-05101641/darwin-amd64/go1.9.1 ",
network: {
localaddress:" [:: 1] : 30301 ",
remoteaddress:" [:: 1]:50042 "
},
protocols: {
ETH: {
difficulty:935232,
head:" 0x8dd2dc7968328c8bbd5aacc53f87e590a469e5bde3945bee0f6ae13392503d17 ",
version:63
}}}
]
To add a peer, just tell one node to connect to another node, and then check the other node, and you'll see the following output:
Once again so sad. Since this account is not assigned to the founding blocks, it is necessary to start mining these accounts.
In the console, run Miner.start () to start mining for this node, and then run Miner.stop () to stop the mining. In the mining, not only to see the account number of the etheric currency, but also to observe the point-to-point interaction between two nodes.
In the picture below, you will see the balance of the master account for each of the two nodes checked. Then start digging on Node 1, let it run for about 5 seconds, and then stop digging after 7 full chunks. Check the balance on the other side, there are 35 etheric coins, in the console this number represents Wei. On the other node, you will see that it receives information from 7 chunks that were mined from node 1.
Now copy the address from the Coinbase account of another node and go back to the unlocked account node
> hisaddress = "0x846774a81e8e48379c6283a3aa92e9036017172a"
After this, the Sendtransaction command is a bit simpler.
> eth.sendtransaction ({from:eth.coinbase, to:hisaddress, value:100000000})
INFO [12-09|10:29:36] Submitted Transaction fullhash=0x776689315d837b5f0d9220dc7c0e7315ef45907e188684a6609fde8fcd97dd57 recipient= 0X846774A81E8E48379C6283A3AA92E9036017172A
" 0x776689315d837b5f0d9220dc7c0e7315ef45907e188684a6609fde8fcd97dd57 "
There's one more thing to be aware of, and it's easy to confuse, and that's why these numbers are worth 0 more. This is because the values are represented by Wei, so you do not have to deal with floating-point numbers that may cause problems on different systems. This will be related to gas (a measure roughly equivalent to the calculation step). Each transaction needs to include a gas limit and a fee that is willing to pay for each gas; the miners can choose to trade and charge together, and they need to start specifying contract deployment and trading.
If you want to know how many etheric tokens are sent with this value, the command is as follows:
> Web3.fromwei (100000000, ' ether ')
"0.0000000001"
To send the transaction and see the difference between the different balances, you need to start the miner in the node, then stop after digging a chunk, and now check the balance to see the change.
> Miner.start ()
...... ..... > miner.stop ()
> Web3.eth.getBalance (eth.coinbase)
59999999999900000000
> Web3.eth.getBalance (hisaddress)
100000000
Now take a look at this huge picture below. Similarly, node 1 is on the left and Node 2 is on the right. So first check the balance of the respective Coinbase account on each node. On Node 1, copy the address of Node 2, send the transaction, and then log in from the node that received the transaction that was submitted, and then start digging. You'll find that node 8 has Txs=1, which means it dug up a deal in that block. After digging a few more chunks, stop digging. Check the account balance for node 1. There are 12 blocks, each of which rewards 5 ethereum, but then it pays 100000000wei.
Now, back to Node 2, check the balance of its Coinbase account, the balance is 0. Then, remember to restart the console of the feast point 1, and do not set the two nodes to peers. Therefore, print node 1 of the Enode, as a peer to add it to Node 2. After you add a peer, you'll see that node 2 receives the missing block, including 1 trades. Then check the balance again and find it has 100000000Wei.