Working with Go Ethereum (geth)
Installing libraries
sudo apt-get install -y build-essential libgmp3-dev golang git tree
Geth
Geth is the Ethereum node core written in Go. The core exists in several languages, and the Go one is said to be the most widely used. On real projects, Parity is reportedly used more often, for reasons including stability.
Installing
cd
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum/
git checkout refs/tags/v1.5.5
make geth
./build/bin/geth version
sudo cp build/bin/geth /usr/local/bin/
which geth
Preparing the data directory
mkdir ~/data_testnet
The genesis file
An identical genesis file means the network is recognized as the same coin, so creating a separate coin or a test network requires editing this file to change the block.
- cd data_testnet/
- vi genesis.json
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0",
"gasLimit": "0x8000000",
"difficulty": "0x4000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}The geth console
Initialize
geth --datadir /home/eth/data_testnet init /home/eth/data_testnet/genesis.json
cd
tree data_testnet/
Run
geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/eth/data_testnet console 2>> /home/eth/data_testnet/geth.log
Create accounts
personal.newAccount("pass0")
personal.newAccount("pass1")
Check accounts
eth.accounts
eth.accounts[0]
eth.accounts[1]
Exit the geth console
exit
Create an account from the geth command line
geth --datadir /home/eth/data_testnet account new
List accounts from the geth command line
geth --datadir /home/eth/data_testnet account list
Run geth
geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/eth/data_testnet console 2>> /home/eth/data_testnet/geth.log
Check accounts
eth.accounts
Check the etherbase
eth.coinbase
Change the etherbase
miner.setEtherbase(eth.accounts[1])
Check and restore the etherbase
eth.coinbase
miner.setEtherbase(eth.accounts[0])
eth.coinbase
Check account balances
eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])
eth.getBalance(eth.accounts[2])
Check the block number
The block number increases as mining proceeds. Since transactions are recorded in blocks, it is easy to assume a block only gets created when there is a transaction to record. In fact, once the block time passes with no transactions, an empty block is created anyway.
eth.blockNumber
Start mining
miner.start(1)
Confirm mining is running
eth.mining
Check the hashrate
eth.hashrate
Stop mining
miner.stop()
Check the etherbase balance
eth.getBalance(eth.coinbase)
Check account balances
eth.getBalance(eth.accounts[0])
eth.getBalance(eth.accounts[1])
eth.getBalance(eth.accounts[2])
Check balances through web3
web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
web3.fromWei(eth.getBalance(eth.accounts[2]), "ether")
Send a transaction
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(10, "ether")})
Unlock an account
personal.unlockAccount(eth.accounts[0])
personal.unlockAccount(eth.accounts[0], "pass0")
personal.unlockAccount(eth.accounts[0], "pass0", 0)
Check pending transactions
eth.pendingTransactions
Check the balance
web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
Send a transaction
eth.sendTransaction({from: eth.accounts[1], to: eth.accounts[2], value: web3.toWei(5, "ether")})
Check the balances
web3.fromWei(eth.getBalance(eth.accounts[2]), "ether")
web3.fromWei(eth.getBalance(eth.accounts[1]), "ether")
web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
Run geth in the background
nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/eth/data_testnet --mine --minerthreads 1 --rpc 2>> /home/eth/data_testnet/geth.log &
Attach to the geth console
geth attach rpc:http://localhost:8545
Run geth in the background with JSON-RPC
nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/eth/data_testnet --mine --minerthreads 1 --rpc --rpcaddr "0.0.0.0" --rpcport 8545 --rpccorsdomain "*" --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" 2>> /home/eth/data_testnet/geth.log &
Looking for a product partner?
Founders, teams, businesses: from problem framing to launch.