Geth
geth is the the command line interface for running a full ethereum node implemented in Go.
Install geth
First we need to install geth on our Ubuntu machine. These steps are from the documentation: https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
Next we need to initialise the private blockchain with a genesis block.
mkdir MyPrivateNetwork
touch genesis.json
{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x400",
"alloc": {},
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x8000000",
"config": {}
}
Now we’re ready to initialise the blockchain.
geth --datadir MyPrivateNetwork init genesis.json
Start a node with geth
geth --identity "MyPrivateNetwork" --cache 6000 --port 3000 --networkid 58342 --nodiscover --datadir="MyPrivateNetwork" --maxpeers=0 --rpc --rpcport 8545 --rpcaddr 0.0.0.0 --rpccorsdomain "*" --rpcapi "db,eth,net,web3,personal,web3" console 2>>geth.log
After starting the geth node you should see something like IPC endpoint opened:
/home/someone/privchain/geth.ipc
Console access
geth attach ipc:/root/MyPrivateNetwork/MyPrivateNetwork/geth.ipc
Logs
bash tail -f geth.log
Account management
coinbase
web3.eth.coinbase
newAccount()
personal.newAccount('your-password')
listAccounts()
personal.listAccounts
unlockAccount()
personal.unlockAccount(web3.eth.coinbase, "password", 15000)
checkAllBalances()
Print all balances with a JavaScript function:
function checkAllBalances() {
var totalBal = 0;
for (var acctNum in eth.accounts) {
var acct = eth.accounts[acctNum];
var acctBal = web3.fromWei(eth.getBalance(acct), "ether");
totalBal += parseFloat(acctBal);
console.log(" eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");
}
console.log("Total balance: " + totalBal + " ether");
}
That can then be executed with:
checkAllBalances()
Mining
set Ether base account for mining
miner.setEtherbase(web3.eth.coinbase)
miner.start(2)
Smart Contract Deploying
- Using Truffle Framework
- Using Mist v0.9
1. Truffle Framework
Requires NodeJS 5.0+, MacOS, Linux or Windows**
Truffle is a world class development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get:
- Built-in smart contract compilation, linking, deployment and binary management.
- Automated contract testing for rapid development.
- Scriptable, extensible deployment & migrations framework.
- Network management for deploying to any number of public & private networks.
- Package management with EthPM & NPM, using the ERC190 standard.
- Interactive console for direct contract communication.
- Configurable build pipeline with support for tight integration.
- External script runner that executes scripts within a Truffle environment.
npm install -g truffle
Create a project
mkdir myproject
cd myproject
truffle init
Using Minimum Viable Token from ethereum.org
The token contract is quite complex. But in essence a very basic token boils down to this:
contract MyToken {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(
uint256 initialSupply
) {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
}
/* Send coins */
function transfer(address _to, uint256 _value) {
if (balanceOf[msg.sender] < _value) throw; // Check if the sender has enough
if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
}
}
Compiling and deploying
truffle compile
truffle migrate
or
truffle migrate --network remotehost
Using the console
truffle console
truffle.js
module.exports = {
rpc: {
host: "GETH_REMOTE_ADDR",
port: 8545
},
networks: {
localhost: {
host: "GETH_REMOTE_ADDR",
port: 8545,
network_id: 58342,
gasPrice: 30000000,
gas: 30000000
},
live: {
host: "localhost",
port: 8545,
network_id: 1,
gasPrice: 3000,
from: "0x058f862dea3e18b1da307f59fdc7e94a560279bc"
}
}
};
truffle compile
truffle migrate --network localhost
Troubleshooting
Deploying Migrations...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: authentication needed: password or unlock
In the geth console, enter the following to unlock the default account, ensuring to replace password with the value entered when the account was created previously.
personal.unlockAccount(web3.eth.coinbase, "password", 15000)
Deploying Migrations...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: Insufficient funds for gas * price + value
Start mining
miner.setEtherbase(web3.eth.coinbase)
miner.start(2)
2. Mist v0.9
For macOS
cd /Applications
cd Ethereum\ Wallet.app/
cd Contents
cd MacOS
ls
./Ethereum\ Wallet --rpc http://GETH_REMOTE_ADDR:8545 --networkid 58342 --node-ipcpath /root/MyPrivateNetwork/geth.ipc
Use the interface
While I already knew this, a well written guide for sure!
Interesting Github link at the end will definitely be forking that repo!