Shang Silicon Valley Ethereum blockchain learning (3)

Difficult to name 2022-04-05 05:58:41 阅读数:448

shang silicon valley ethereum blockchain


Preface

Tips : Service outsourcing blockchain learning
Only record operations
Direct link of Silicon Valley Ethereum blockchain

One 、 Build a private chain

1、 Create the genesis block

Create directory

$ mkdir myChain
$ cd myChain

Create a genesis.json The file of , And enter the content

$ touch genesis.json
$ vi genesis.json

Shang Silicon Valley Chuangshi block content :

{

"config":{

"chainId": 15
},
"difficulty": "2000",
"gasLimit": "2100000",
"alloc": {

"0x634c4CF26680fa8C2Ef3d8B9FA4E6AE748667BB8": {
"balance": "300000000000000000000"}
}
}

When a transaction is initiated behind the private chain configured according to the genesis block of Silicon Valley, an error of uninitialized transaction will be reported , So I found a non error reporting Chuangshi block on the Internet. The configuration is as follows , What is the specific item? I don't quite understand , If you want to know, please Baidu

{

"config": {

"chainId": 666,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {
}
},
"nonce": "0x0",
"timestamp": "0x5ddf8f3e",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x00002",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0x634c4CF26680fa8C2Ef3d8B9FA4E6AE748667BB8":{
"balance":"300000000000000000000"} },
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

2、 Blockchain initialization

Initialization command :

$ geth --datadir . init genesis.json

result
 Insert picture description here

 Insert picture description here

3、 Officially start the private chain

$ geth --datadir . --networkid 666 console

networkid Blockchain id It must be with the... In the genesis block id Agreement
console Print log

error

In this way, the startup will keep looking for nodes and reporting errors

 Insert picture description here No error command

geth --datadir . --networkid 15 --nodiscover --http --http.addr 0.0.0.0 --http.port 8545 --http.corsdomain "*" --port 30305 --allow-insecure-unlock console 2>>geth.log

nodiscover You can keep it from looking for nodes
http,http.addr,http.vhosts,http.port,http.corsdomain It's all about local services , I'll talk later about what it's for
console 2>>geth.log Print the output log to geth.log, Convenient view

 Insert picture description here Open another terminal , Enter the following command to view the printed log

tail -f geth.log

 Insert picture description here

Two 、Geth Console operation

1、 Common objects and commands

Common objects

eth: It mainly includes methods related to accessing and interacting with blockchain ;
net: It mainly includes checking P2P The method of network state ;
admin: It mainly includes the methods related to the management node ;
miner: It mainly includes mining related methods ;
personal: Including the method of account management ;
txpool: Including methods to view the transaction memory pool ;
web3: Including all the above objects , It also includes some general methods .

Common command

personal.newAccount(): Create an account ;
personal.unlockAccount(): Unlock the account ;
eth.accounts: List the accounts in the system ;
eth.getBalance(): Check the account balance , The unit of return value is Wei;
eth.blockNumber(): List the current block height , That is, the number of blocks ;
eth.getTransaction(): Access to trading information ;
eth.getBlock(): Get block information ;
miner.start(): Start mining ;
miner.stop(): Stop mining ;
web3.fromWei():Wei To convert it into ether ;
web3.toWei(): Ether is converted into Wei;
txpool.status: Status in the trading pool .

2、 Actual operation

1、 Check the default account number

In the genesis block alloc Is the default account ,balance It's the Ethernet balance of the preset account

eth.getBalance("0x634c4CF26680fa8C2Ef3d8B9FA4E6AE748667BB8")

Wei To Ether The transformation of

web3.fromWei(eth.getBalance("0x634c4CF26680fa8C2Ef3d8B9FA4E6AE748667BB8"))

 Insert picture description here

2、 Create a new account

The two methods

Method and enter the password

personal.newAccount()

Fill in the password for the method parameters

personal.newAccount("1234")

 Insert picture description here

Check the account again at this time

eth.accounts

There will be all accounts in the current chain
 Insert picture description here

All registered accounts will be here , But the default account will not be in

3、 Start mining

By default, the first registered account is the mining account , Therefore, the increase of money will also be given to the first account

miner.start(1)

Watch the log
 Insert picture description here That's what mining is about , Wait in peace
 Insert picture description here

The appearance of a small hammer means that the mining is successful

End mining

miner.stop()

 Insert picture description here

Check the block height of the chain , That is, the total number of blocks

eth.blockNumber

 Insert picture description here

Then check the balance of the first registered account

eth.getBalance(eth.accounts[0])

 Insert picture description here

3、 Launch a deal

Unlock the account before trading

personal.unlockAccount(eth.accounts[0])
personal.unlockAccount(eth.accounts[1])

 Insert picture description here
Launch a deal

eth.sendTransaction({
from:eth.accounts[0],to:eth.accounts[1],value:100000000000000000})

The result will return the transaction Hash value
Use the following command to view the transaction pool

txpool

The transaction needs to be packaged to take effect
So we need to mine again
After mining is successful, you can check whether the balance has changed

3、 ... and 、 link MataMask

1、 link MataMask

 Insert picture description here
choice Localhost 8545
At this time, I understand the reasons for the network configuration when starting the private chain
After the link is successful, the original account will be the default account in the creation block and its balance
 Insert picture description here
At this time, you can choose to import the account
choice Json file
To the chain file directory keystore Select the account number to import
Then enter the password you entered when registering your account
The waiting time for import may be long
 Insert picture description here
Set up -> The Internet ->Localhost 8545
Modify chain ID For... In the genesis block chainId
preservation
This will ensure that you can use MataMask Make the transaction as error free as possible

By the time you get here, you can already use MataMask Transferred money between your accounts
However, after initiating the transaction, it also needs to be in Geth The client packages the transaction
After packing successfully MataMask I'll give you a hint

Over

版权声明:本文为[Difficult to name]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2022/04/202204050555188447.html