Building a Private Ethereum Blockchain with Raspberry Pi: Transactions and Smart Contracts

·

This guide explores how to conduct transactions and deploy smart contracts on a private Ethereum blockchain using a Raspberry Pi. We will cover transferring funds, mining Ether, and executing a sample smart contract.

Setting Up Your Environment

Before starting, ensure your Raspberry Pi runs the latest Raspbian Lite. Update the system:

sudo apt-get update
sudo apt-get dist-upgrade

Older versions of Golang may not support newer Geth clients. Remove existing Golang installations and install dependencies:

sudo apt-get remove golang
sudo apt-get autoremove
sudo apt-get install git golang libgmp3-dev

For Geth 1.8, use Golang 1.9.2:

wget https://dl.google.com/go/go1.9.2.linux-armv6l.tar.gz
sudo tar -C /usr/local -xzf go1.9.2.linux-armv6l.tar.gz
export PATH=$PATH:/usr/local/go/bin

Add the last line to ~/.profile to make Golang available on login.

Install Geth 1.8:

mkdir src
cd src
git clone -b release/1.8 https://github.com/ethereum/go-ethereum.git
cd go-ethereum
make
sudo cp build/bin/geth /usr/local/bin/

Initializing the Private Blockchain

Follow these steps to create and initialize a private blockchain with two nodes (as detailed in Part 2 of this series):

  1. Create a custom genesis block JSON file.
  2. Initialize each node with geth init.
  3. Start each node with the --networkid and --rpc flags.
  4. Connect the nodes using admin.addPeer().

Confirm peer connectivity with admin.peers on each node.

Transferring Funds and Mining

Check account balances on each node:

web3.fromWei(eth.getBalance(eth.coinbase), "ether")

If one account has pre-allocated funds, transfer Ether to the other:

  1. On the recipient node, retrieve the account address: eth.coinbase.
  2. On the sender node, define the recipient address and send funds:
var recipientAddress = "0x1a49bcee41bff051b8ffd0a01d4a3be4485fd030"
personal.unlockAccount(eth.coinbase, "")
eth.sendTransaction({from: eth.coinbase, to: recipientAddress, value: web3.toWei(0.1, "ether")})

Start mining to confirm the transaction:

miner.start()

Mining generates the DAG (Directed Acyclic Graph) and processes pending transactions. Once mined, check the updated balances:

web3.fromWei(eth.getBalance(eth.coinbase), "ether")

Stop mining with miner.stop().

Deploying a Smart Contract

Smart contracts automate transactions using Solidity code. Below is a sample "Greeter" contract:

contract Mortal {
    address owner;
    function Mortal() { owner = msg.sender; }
    function kill() { if (msg.sender == owner) selfdestruct(owner); }
}

contract Greeter is Mortal {
    string greeting;
    function Greeter(string _greeting) public {
        greeting = _greeting;
    }
    function greet() constant returns (string) {
        return greeting;
    }
}

Compile the contract:

solc -o target --bin --abi Greeter.sol

Create a deployment script (greeter.js) using the ABI and bytecode:

var greeterFactory = eth.contract([ABI_CONTENT])
var greeterCompiled = "0x" + "BYTECODE_CONTENT"
var _greeting = "Hello DesignSpark!"
var greeter = greeterFactory.new(_greeting, {from: eth.accounts[0], data: greeterCompiled, gas: 1000000}, function(e, contract) {
    if (e) {
        console.error(e);
        return;
    }
    if (!contract.address) {
        console.log("Contract transaction sent: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
    } else {
        console.log("Contract mined! Address: " + contract.address);
    }
})

Unlock the account and deploy:

personal.unlockAccount(eth.coinbase, "")
loadScript("greeter.js")

Verify deployment:

eth.getCode(greeter.address)

Execute the contract:

greeter.greet()

Troubleshooting and Best Practices

Ethereum documentation can be outdated. Always verify instructions against your software version. For debugging:

👉 Explore advanced blockchain development tools

Frequently Asked Questions

What is a private Ethereum blockchain?
A private blockchain is a restricted network where participants require permission to join. It is ideal for testing and development, as it avoids real Ether costs and provides full control over the environment.

How is mining different in a private network?
Private networks often use low difficulty settings, allowing faster block generation. Mining rewards are still granted but have no real-world value.

Why use a Raspberry Pi for blockchain development?
Raspberry Pi devices are low-cost, energy-efficient, and capable of running lightweight nodes. They are excellent for learning and prototyping decentralized applications.

What is gas in Ethereum?
Gas is a unit measuring computational effort for transactions or smart contracts. It prevents network abuse and prioritizes resource allocation.

Can I interact with smart contracts after deployment?
Yes, contracts remain on the blockchain and can be called by any connected node. Their functions are accessible via the ABI and contract address.

How do I update a deployed smart contract?
Smart contracts are immutable once deployed. Changes require deploying a new contract and updating any dependent applications.

Conclusion

This tutorial demonstrated how to:

  1. Set up a private Ethereum blockchain with a Raspberry Pi.
  2. Transfer funds between accounts and mine Ether.
  3. Write, compile, and deploy a smart contract.

Private blockchains are valuable for testing and learning. With this foundation, you can expand your network, create more complex contracts, and explore Ethereum’s full potential.