Blockchain-Private Chain Creation and Smart Contract Deployment Practice

Prepare:

ECS+ubuntu 16.0+go+go-ethereum

 

1. Genesis chain node:

{

    "config": {

        "chainId": 12345,

         "homesteadBlock":0,

         "eip155Block":0,

        "eip158Block":0

    },

 "coinbase":"0x0000000000000000000000000000000000000000",

 "difficulty":"0x400",

"extraData":"0x123456",

"gasLimit":"0xffffffff",

"nonce":"0x0000000000000042",

"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",

"timestamp":"0x00",

"alloc": { }

}

Field explanation:

mixhash: used for mining in conjunction with nonce, the hash generated by part of the previous block. Note that the settings of him and nonce need to meet Ethereum

nonce: nonce is a 64-bit random number used for mining. Note that the settings of him and mixhash need to meet the Yellow paper of Ethereum,

difficulty: Set the difficulty of the current block. If the difficulty is too high, it will be difficult for CPU mining. Set a lower difficulty here.

alloc: It is used to preset an account and the amount of ether in the account. Because private chain mining is easier, we do not need to preset an account with coins, and we can create it ourselves when we need it.

coinbase: the miner's account, just fill in

timestamp: set the timestamp of the genesis block

parentHash: the hash value of the previous block, because it is a genesis block, so this value is 0

extraData: Additional information, fill in casually, you can fill in your personal information, it must be a hexadecimal even character string

gasLimit: This value sets the limit on the total consumption of GAS, which is used to limit the sum of transaction information that the block can contain. Because we are a private chain, fill in the maximum.

 

2.geth --datadir "./" init genesis.json to create a genesis node

3.geth --datadir "./" --nodiscover console 2>>geth.log Create a new private chain and output the log to geth.log

4. Start the node and enter the console: geth --datadir ./ --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console

5. tail -f geth.log Dynamically display geth.log log update information

6.eth.accounts displays the user array

7.acc0=eth.accounts[0]

8.eth.getBalance (acc0)

9. When miner.start() is executed, geth console returns null.

Solution 1: First execute personal.listAccounts to check whether the current node has an account address, and then use eth.coinbase to check whether the coinbase account address is set. If not, use miner.setEtherbase(eth.accounts[0]) to set the coinbase address. Restart miner.start()

10.转账:eth.sendTransaction({from:"address1",to:"address2",gas:31000,gasPrice:web3.toWei(300,'gwei'),value:1})

The console prints out the fullhash and checks the transaction status according to hashid

11.txpool.status View unpackaged transactions in the mining pool

{

    pending:1,

    queued:0

}

12. eth.getBlock(number) View transaction block information

13.eth.getTransaction(fullhash)

    If the transaction is executed but there is still untransaction data when checking txpool.status, first check whether the mining is in progress, and then the block can be updated during mining.

14. Check txpool.status again

{

    pending:0,

    queued:0

}

15. Deploy the smart contract:

https://ethereum.github.io/browser-solidity/

Write and compile solc code online

Hello World code:

pragma solidity ^0.4.18; contract hello { string greeting; function hello(string _greeting) public { greeting = _greeting; } function say() constant public returns (string) { return greeting; } }

Click Details to get the deployment code, replace the variable string var _greeting = "Hello World";

After modification, copy it directly to the geth console, click Enter, and return to Contract mined! address:XXXXX transactionHash:XXXXX

(Note that you need to maintain the mining state to deploy, and remember the account of the unlock operation, personal.unlockAccount(address,'XXX'))

Run the contract:

>hello.say()

"Hello World"

 

Waiting for the problem to be resolved:

1. An error is reported in the geth console when the ssh server is reconnected. What should I do? Fatal: Error starting protocol stack: datadir already used by another process

 

Learning materials:

1. Install geth client and transfer money   http://blog.csdn.net/ddffr/article/details/74327876

2. Ethereum Study Notes: Private Chain Construction Operation  Guidehttp://blog.csdn.net/u013096666/article/details/72639906

3. Smart contract development environment and Hello World contract  http://www.cnblogs.com/tinyxiong/p/7898599.html

4. Blockchain technology  http://wiki.jikexueyuan.com/project/blockchain/

5. Solidity official website  https://solidity.readthedocs.io/en/latest/installing-solidity.html

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325067192&siteId=291194637