Introduction to Ethereum Private Chains
Setting up a private Ethereum blockchain allows developers and organizations to create their own blockchain networks for testing, development, or specific business applications. This comprehensive guide covers private chain setup on both Ubuntu and Windows platforms, including wallet deployment and smart contract implementation.
Ubuntu Environment Setup
Basic Environment Configuration
Command Line Installation
- Open terminal and execute these commands sequentially:
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install EthereumSource Code Compilation (Ubuntu/Mac)
cd go-ethereum
make gethEnvironment Variables Configuration
Add Geth to your environment variables:
vim ~/.bashrcAdd these lines to the file:
export GETH="$GOPATH/src/github.com/ethereum/go-ethereum/build"
export PATH="$PATH:$GETH/bin"Activate the configuration:
source ~/.bashrcVerify installation by running:
geth -hGeth Installation and Private Chain Setup
- Create a new account:
- Edit genesis.json file (sample configuration below)
- Initialize private chain:
geth --datadir data --networkid 20140628 --rpc --rpccorsdomain "*" init ./genesis.jsonKey parameters:
--networkid: Private chain identifier (must match across nodes)--datadir: Directory for private chain data storage
- Start private chain client:
geth --datadir data --networkid 20140628 --rpc --rpccorsdomain "*" --nodiscover --port 16333 --rpcport 8546 consoleFor additional nodes, use different port numbers.
Mining Operations
- Create a new account with password protection:
personal.newAccount("3zzz")- Check account balance:
eth.getBalance("account_address")- Set mining account:
miner.setEtherbase("account_address")- Start mining (single thread):
miner.start(1)- Stop mining:
miner.stop()Transactions and Ether Conversion
Ethereum units conversion:
web3.fromWei(10000000000000000, "ether") // Returns "0.01"
web3.toWei(1) // Returns "1000000000000000000"Send transaction between accounts:
web3.eth.sendTransaction({from:web3.eth.accounts[1], to: web3.eth.accounts[2], value: web3.toWei(1, "ether")})๐ Learn more about Ethereum transactions
Windows Platform Setup
Environment Preparation
Install Ethereum-Wallet:
- Create dedicated directory (e.g., D:\Eth)
- Extract Ethereum-Wallet package
Install Geth:
- Download from official repository
- Install in dedicated directory
- Create privatechain folder for blockchain data
Private Chain Configuration
- Create genesis.json file with custom configuration:
{
"nonce": "0x0000000000000042",
"difficulty": "0x40000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0xffffffff",
"alloc": {
"3282791d6fd713f1e94f4bfd565eaa78b3a0599d": {
"balance": "1337000000000000000000"
},
"17961d633bcf20a7b029a7d94b7df4da2ec5427f": {
"balance": "229427000000000000000"
}
},
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}- Initialize genesis block:
geth -datadir "%cd%\\privatechain\\node1" init genesis.json- Start private chain node:
geth --targetgaslimit 4294967295 --rpc --rpcport "8101" --port "30301" --rpcapi "eth,web3,personal" --networkid 2020 --identity 2020 --nodiscover --maxpeers 5 --datadir "%cd%\\privatechain\\node1" --unlock 0 --rpccorsdomain "*" consoleKey parameters:
--targetgaslimit: Gas limit per block--rpcport/--port: Communication ports--networkid: Network identifier--datadir: Data directory path
Wallet Operations
- Account management commands:
// Create new account
personal.newAccount()
// View accounts
eth.accounts
// Check balance
eth.getBalance(eth.accounts[0])
// Unlock account
personal.unlockAccount(eth.accounts[0])- Mining operations:
// Start mining (single thread)
miner.start(1)
// Stop mining
miner.stop()๐ Explore Ethereum wallet features
Smart Contract Implementation
Contract Deployment
Sample token contract:
pragma solidity ^0.4.18;
contract Token {
mapping (address => uint) public balancesOf;
address public owner;
function Token() public {
owner = msg.sender;
balancesOf[msg.sender] = 10000;
}
function transfer(address _to, uint _value) public {
require(balancesOf[msg.sender] > _value);
require(balancesOf[_to] + _value > balancesOf[_to]);
balancesOf[msg.sender] -= _value;
balancesOf[_to] += _value;
}
function mint(uint _amount) public {
balancesOf[owner] += _amount;
}
}Contract Interaction
- Deploy contract through wallet interface
- Activate contract by mining:
miner.start(1)- Interact with contract methods:
// Transfer tokens
contractInstance.transfer.sendTransaction(receiverAddress, amount, {from: senderAddress})FAQ Section
Q: Why is my transaction not going through?
A: Transactions require miner confirmation. Ensure you have mining active (miner.start(1)) for transaction processing.
Q: How do I convert between Ether and Wei?
A: Use the conversion functions:
web3.fromWei(amount, "ether") // Wei to Ether
web3.toWei(amount, "ether") // Ether to WeiQ: What's the difference between mainnet and private chain?
A: Mainnet is the public Ethereum network (networkid: 1). Private chains use custom network IDs and aren't connected to the main Ethereum network.
Q: How do I unlock my account?
A: Use:
personal.unlockAccount(accountAddress, "password")Q: Why can't I see my mined Ether?
A: By default, mining rewards go to your coinbase account (first account). Check with:
eth.getBalance(eth.accounts[0])Q: How do I set up multiple nodes?
A: Use different data directories (--datadir) and ports (--port, --rpcport) for each node.