[get] go language calls Ethereum | together with the school district block chain

Xiaosheng Fanyi 2021-08-07 19:01:52 阅读数:262

language calls ethereum school district


The most commonly used command line tool in Ethereum smart contract development .
Geth It is the official client software provided by Ethereum Foundation , use Go Programming language .Geth Provides an interactive command console , The command console contains various functions of Ethereum (API). full name go-ethereum.

1. geth brief introduction

1.1 Download address :

Official website ( Very slowly ):https://geth.ethereum.org/downloads/

Domestic mirror :https://ethfans.org/wikis/Ethereum-Geth-Mirror

1.2 install :

 Insert picture description here

 Insert picture description here
After downloading , Will be generated in the installation directory geth.exe
 Insert picture description here
Put this file in gopath Of bin Directory ,

 Insert picture description here
Just put it here

1.3 Check whether the installation is successful

geth version
 Insert picture description here
If you install it , But if it doesn't show this , Maybe it's because your system environment doesn't have go The path of , hold go Of bin The directory path is placed in the... Of the system variable path Just fine ~

2. geth Command Introduction

geth -help view help
 Insert picture description here
life Make :geth [ Options ] command [ Command options ] [ Parameters …]
Command parameters : Hyperdetail

3. geth Common commands

3.1 Specify the data directory --datadir

Such as :geth --datadir “ route ” account new
 Insert picture description here  Insert picture description here

3.2 Account related

  • View account :geth account list

    View account information
     Insert picture description here

  • Create an account :geth account new
    Then enter the password twice
     Insert picture description here

  • Update the specified account :geth account update " Address of the user "

    Enter the account password and confirm the password

 Insert picture description here

 Insert picture description here

By default, the user's account information will be stored in this directory

  • Import accounts :geth account import Your private key file path ( Export the private key to xx.txt)
    Specify the user who created the save path :geth --datadir account new

3.3 Console console:

Start and enter interactive javascript Environmental Science

 Insert picture description here

  • The default connection is ipc
  • geth attach http://IP:8545, Not on by default
  • geth attach ws://IP:8546, Not on by default

Access control :geth console You can specify --datadir " route "
Exit console :exit perhaps ctrl+d

Commonly used api:

  • Get the block number :eth.blockNumber
     Insert picture description here

  • Whether the port is listening net.listening
     Insert picture description here

  • Start mining :miner.start()
     Insert picture description here

  • Mining can also specify the number of processes :miner.start(1)

  • Stop mining :miner.stop()

  • Check if you are digging :eth.mining true: Mining ,false: Not mining

  • Get the account address array :eth.accounts

  • Get mining address :eth.coinbase By default, the first account created

  • Check the first account balance :eth.getBalance(eth.accounts[0])

  • Create user :personal.newAccount(“12345678”)

  • Set mining account :miner.setEtherbase(“ Address A”)

  • Transfer money between accounts :eth.sendTransation({from:“ Address A”,to:“ Address B”,value:web3.toWei(10,“ether”)})

  • Access to trading information :eth.getTransaction(‘ Address A’)

3.4 Delete data :

Don't just delete datadir Catalog , Use removedb

geth --datadir " route " removedb
This will delete the path /geth/chaindata, Nothing else will move , The user... Will not be deleted .

3.5 help

  • geth account -h
  • geth account help new View subcommands

3.6 Common mistakes

1 . Error: authentication needed: password or unlock undefined:

  • This error report can unlock the account
  • Account unlock :personal.unlockAccount(“ Account address ”)

2 . Returned error: exceeds block gas limit:

  • This error depends on the block number gaslimit, When deploying, change to always
  • Get block number :eth.blockNumber,
  • Get details according to the block number :eth.getBlock(0)

4. Go Language call contract

4.1 start-up rpc port

geth --datadir "fanone" --identity "fanone" --rpc --rpcport 8545 --rpcapi "db,eth,net,web3,personal" --rpccorsdomain "*" console

 Insert picture description here

  • –identity “fanone”: Node identity

  • –rpc: Turn on RPC Interface

  • –rpcport:RPC port , Default go yes 8545

  • –rpcapi “db,eth,net,web3”: Provided for use by others RPC API, The default is web3 Interface

  • –rpccorsdomain: Set up that can connect to your node URL, To complete RPC ren service .* Means any URL Can be connected to , By default, you can only connect by yourself

  • –datadir: Block data folder , If not specified , The one above is C The address of the disk .

in order to C Think about it , So I suggest this , Create a new folder and put it on another disk
 Insert picture description here
 Insert picture description here
 Insert picture description here
It will be stored here

  • –networkid:net_version Of id

  • –port: The port used to listen to other nodes

  • –nodiscover: Your node will not be discovered by others , Unless they add you manually

 Insert picture description here Is to connect this url

4.2 Go Call Ethereum

Download package :"github.com/ethereum/go-ethereum/rpc"
Sample code :

client, _ := rpc.Dial("http://localhost:8545")
var result bool
client.Call(&result ,"net_listening")
// Note that pointers must be used here , Otherwise, null pointer exception will be reported 

After starting
 Insert picture description here stay go Call in  Insert picture description here

4.3 Call interface


Currently connected to the network ID

net_version: Currently connected to the network ID

var networkid string
"2": Morden Testnet
"3": Ropsten Testnet
"4": Rinkeby Testnet
"42": Kovan Testnet

Start up rpc The service interface can be specified : --networkid


Whether the client is listening

var is_listing bool


Number of connected peer nodes

var count string
//"0x0" Express 0 individual 
//"0x2" Express 2 individual 

eth Interface

1 .eth_accounts: Account address array

var accounts []string

2 .eth_getBalance: Get the balance of the account at the specified address , Company :wei

var balance string

Parameters :
Account address
Integer block number , Or string "latest", “earliest” or “pending”
3 .eth_gasPrice: Returns the current gas Price , Company :wei.

var gas_price string


Get mining account address

var coinbase string
client.Call(&coinbase, "eth_coinbase")


Returns the version of the current Ethereum Protocol .

var proto_version string
client.Call(&proto_version , "eth_protocolVersion")


Whether the client is mining

var is_mining bool


Returns the number of hashes that can be calculated per second when mining nodes .

var hashrate string


Returns the number of transactions at the specified address

var transactionCount string
// Parameters 
// Account address 
// Integer block number , Or string "latest", "earliest" or "pending"

9.eth_blockNumber: Gets the current block number of the node

var blockNum string
client.Call(&blockNum ,"eth_blockNumber")

personal Interface

1 .personal_listAccounts:
Get all accounts under this node , and eth_accounts The effect is the same

var accounts []string

Create user

var newAccount string
client.Call(&newAccount, "personal_newAccount", "12345678")
Parameters :


Lock the specified account

var is_lock bool
client.Call(&is_lock, "personal_lockAccount", " Account address ")
// Parameters :
// Account address 


Unlock the designated account , Some operations must unlock the account before they can be operated

var is_unlock bool
client.Call(&is_unlock, "personal_lockAccount", " Account address "," Account password ")
Parameters :
Account address
Account password

db Interface


Store the string in the local database

var is_ok bool
client.Call(&is_ok, "db_putString", "db_name","key",""value")
Parameters :
Database name
key name
The string value to be stored


Read string from local database .

var val string
client.Call(&val, "db_getString", "db_name","key")
// Parameters :
// Database name 
// key name 


A small life , Looking forward to your attention .

版权声明:本文为[Xiaosheng Fanyi]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/08/20210807185920198m.html