geth console control

Table of contents

geth console control

Start the console: the output results are displayed directly

Start the console: the console output is written to the output.log file, but MetaMask cannot be connected

exit console 

Common commands

start mining

stop mining

transfer

Problem: An error password or unlock appears

Problem: An invalid sender error occurs, maybe your gensis.json is missing something

Question: Check the account and find that the transfer has not been successful, and the account amount has not changed

External Connection MetaMask

 win10 metamask connect localhost 8584

Win10 uses metamask to connect to the private chain in the virtual machine ubuntu

Problem: Error raised by --dev: Failed to unlock developer account

Problem: Using the latest version of geth, when executing personal.unlockAccount() or calling the personal_unlockAccount interface in the program, an exception will appear: account unlock with HTTP access is forbidden

问题:the method net_version does not exist/is not available

View transaction history

Method 1: Query by transaction id

Method 2: Query according to the block id


geth console control

Host win10, use geth to create a private chain in Ubuntu

Start the console: the output results are displayed directly

 At the end of the private chain code, add console to start the console when starting the private chain

/geth --datadir ~/桌面/geth-project/myChain --networkid 15 console

Start the console: the console output is written to the output.log file, but MetaMask cannot be connected

Of course, writing in this way will directly write the information of Info to the console, a lot, it looks very uncomfortable

The console output can be written to the output.log file

geth --datadir ~/桌面/geth-project/myChain --networkid 15 console 2>~/桌面/geth-project/myChain/output.log

View output so far

cat ~/桌面/geth-project/myChain/output.log

View output results in real time 

tail -f ~/桌面/geth-project/myChain/output.log

exit console 

ctrl + d or console input exit

Geth Console is an interactive JavaScript execution environment, which has some built-in JavaScript objects used to operate Ethereum. We can directly call these objects to obtain relevant information on the blockchain. 


 Common commands

The objects mainly include:

  • admin: mainly contains methods related to management nodes
  • eth: mainly includes methods related to accessing and interacting with the blockchain
  • personal: contains methods for account management
  • txpool: contains the method of viewing the transaction memory pool
  • miner: mainly includes some methods related to mining
  • net: mainly includes methods to view p2p network status
  • web3: Contains all the above objects, and also contains some common methods

Commonly used commands are:

  • personal.newAccount([password]): Create an account
  • personal.unlockAccount(account id): unlock account
  • eth.accounts: list accounts in the system
  • eth.getBalance(account id): Check the account balance, the unit of return value is Wei
  • eth.blockNumber: list the current block height
  • eth.getTransaction(transaction id): get transaction information
  • eth.sendTransaction({from: account id1, to: account id2, value: money, data:'hexadecimal information'}): transfer
  • eth.estimateGas({from: account id1, to: account id2, value: money, data: 'hexadecimal information'}): can be used to estimate gas before transfer
  • eth.getBlock (block id is blockNumber): get block information
  • eth.getBlockTransactionCount (block id is blockNumber): query the number of transactions in this block
  • miner.start(): start mining
  • miner.stop(): stop mining
  • miner.setEtherbase: which account should mine Ethereum
  • web3.fromWei(money): Convert Wei to Ether
  • web3.toWei(): Convert Ethereum to Wei
  • txpool.status: status in the transaction pool

The ether in eth.accounts[0] is mined

start mining

miner.start(1)

Even if it returns true or null, it is already mining

It is a normal phenomenon to mine Generating DAG in progress for the first time. It takes a long time to initialize, so be patient

 So that's the start of mining

stop mining

miner.stop()

Solution: broadcast the transfer message , that is, re-mining, just stop mining for a while 

miner.start(1)

miner.stop()

 transfer

eth.sendTransaction({from:eth.accounts[0], to:"0x1E17ECE93b3", value:web3.toWei(500, 'ether')})

Problem: An error password or unlock appears

Solution: Unlock your account

personal.unlockAccount(你的账户)
personal.unlockAccount(eth.accounts[0])

retransfer later

 Transfer unit: wei

eth.sendTransaction({from:eth.accounts[0], to:"0x1E17ECE93b3", value:500})

 Transfer unit: ether

eth.sendTransaction({from:eth.accounts[0], to:"0x1E17ECE93b3", value:web3.toWei(15, 'ether')})

Problem: An invalid sender error occurs , maybe your gensis.json is missing something

Solution: Check the latest official version or use the following version, then delete the original genesis block and re-initialize the new genesis block

{
    "config": {
    "chainId": 15,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0
    },
    "alloc": {
    "0x1E17ECE93b3c72277E4B22a58B87934C9F654089":{"balance":"900000000000000000000"}},
    "coinbase": "0x0000000000000000000000000000000000000000",
    "difficulty": "0x200",
    "extraData": "",
    "gasLimit": "0x2fefd8",
    "nonce": "0x0000000000000042",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp": "0x00"
}

Question: Check the account and find that the transfer has not been successful, and the account amount has not changed

Solution: broadcast the transfer message , that is, re-mining, just stop mining for a while 

miner.start(1)

miner.stop()

The transfer is completed here

Because of mining, accounts[0] may have more money


External Connection MetaMask

 Win10 metamask connects to  localhost 8584

Solution: You only need to add --http or --rpc , the default port is 8545; you only need to add --rpcport  to modify the specified port

geth --datadir ~/桌面/geth-project/myChain --networkid 15 --http console 2>~/桌面/geth-project/myChain/output.log

Win10 uses metamask to connect to the private chain in the virtual machine ubuntu

Solution:

1. Add --rpc --rpcaddr "0.0.0.0" --rpcport 8545 parameters to geth console.

2. Then add the network ip address to MetaMask, which is the ip address of your virtual machine ubuntu

geth --datadir ~/桌面/geth-project/myChain --networkid 15 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 console 2>~/桌面/geth-project/myChain/output.log

# Pay attention to setting the startup parameter --rpcaddr 127.0.0.1, the browser metamask in the host cannot connect to the chain 

geth --rpccorsdomain "*" 允许跨域请求的域名列表
加了这个参数反而连不上geth

Then add the network ip address to MetaMask is the ip address of your virtual machine ubuntu

 At this time, the host MetaMask can be accessed

Problem: Error raised by --dev: Failed to unlock developer account

 error code

geth --datadir . --networkid 15 --dev

报错:Failed to unlock developer account:could not decrypt key with given passphrase

Solution: --dev means to enable the developer account , and because you specified the networkid , and the --dev developer mode is to help you create a new private chain , not based on your original creation, you don't need to add those parameters, as follows

geth --datadir . --dev

Problem: Using the latest version of geth, when executing personal.unlockAccount() or calling the personal_unlockAccount interface in the program, an exception will appear: account unlock with HTTP access is forbidden

报错:GoError: Error: account unlock with HTTP access is forbidden at web3.js:6347:37(47)

Reason: In the new version of geth, for security reasons, the HTTP channel is disabled by default to unlock the account 

Solution: Add parameters to the startup command

--allow-insecure-unlock

 示例:geth --rpc --rpcapi eth,web3,personal --allow-insecure-unloc

geth --datadir ~/桌面/geth-project/myChain --networkid 15 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpcapi eth,web3,personal --allow-insecure-unlock console 2>~/桌面/geth-project/myChain/output.log

问题:the method net_version does not exist/is not available

Solution: Include "net" when setting rpcapi in the rpc parameter when geth starts

  示例:geth --rpc --rpcapi eth,web3,personal,net --allow-insecure-unloc

geth --datadir ~/桌面/geth-project/myChain --networkid 15 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpcapi eth,web3,personal,net --allow-insecure-unlock console 2>~/桌面/geth-project/myChain/output.log 

View transaction history

Method 1: Query by transaction id

 Note that this is not the id of your account, but the id of the transaction

eth.getTransaction(transactionHash [, callback])
eth.getTransaction("0x21003fc7a543bc89a467510b6ab978bc638dc09619d1fba0288bdb3907830e4b")

# Note that this is not the id of your account, but the id of the transaction 

  • hash 32 Bytes - String: Hash value of the transaction
  • nonce - Number: The number of transactions that the transaction sender generated before this transaction
  • blockHash 32 Bytes - String: The hash value of the block where the transaction is located. If the transaction is pending, the value is null
  • blockNumber - Number: The block number of the transaction, if the transaction is pending, the value is null
  • transactionIndex - Number: The index position of the transaction in the block, if the transaction is pending, the value is null
  • from - String: The address of the sender of the transaction
  • to - String: The address of the recipient of the transaction. For transactions that create contracts, the value is null
  • value - String: transfer amount in wei
  • gasPrice - String: The gas price promised by the sender, in wei
  • gas - Number: The gas usage provided by the sender
  • input - String: the data to send with the transaction
  • r, s, v: signature

Method 2: Query according to the block id

# Query the number of transactions in the block 

eth.getBlockTransactionCount(blockNumber)
eth.getBlockTransactionCount(36)

# Query block information 

eth.getBlock(blockNumber)
eth.getBlock(36)

Guess you like

Origin blog.csdn.net/m0_46262108/article/details/123278614