Ethereum Private Blockchains.

This post contains the steps to setup a private Ethereum blockchain.

First, you’ll need a Linux vm. Please note that this post assumes a medium/advanced level of knowledge. As such, it will be much more succinct than most posts in other categories.

INSTALL ETHEREUM

sudo apt-get install software-properties-common
Add the ethereum repository: sudo add-apt-repository -y ppa:ethereum/ethereum
Update the package list and upgrade entire system
Install Ethereum.

CREATE DIRECTORY TO STORE THE BLOCKCHAIN

Create a directory to store the blockchain

CREATE TWO ACCOUNTS

Create first account
Create second account

CREATE GENESIS BLOCK

{
  "config": {
    "chainId": 15,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "ethash": {}
  },
  "difficulty": "1",
  "gasLimit": "8000000",
  "alloc": {
    "0x849b1448F8Cca9F3BCeE21F1ebcC1545f12b8e25": { "balance": "300000" },
    "0x140DBCb0eC85B62A66aCBf52DeEFd9F01abCc22A": { "balance": "400000" }
  }
}

INITIALIZE THE GENESIS BLOCK

Add the accounts created into the genesisblock.json file and initialize the blockchain with the following command…

geth -datadir="/home/matt/privatechain" init "/home/matt/privatechain/genesisblock.json"

If your genesisblock.json file is correctly written, the block will initialize with “Successfully wrote genesis state” message.

CHAINDATA AND LIGHTCHAINDATA

The chaindata and lightchaindata directories in geth indicate that your blockchain network has been created.

cd into ~/privatechain/geth and you’ll see chaindata and lightchaindata subdirectories
The genesis block.

MINING THE BLOCK

geth -datadir “/home/matt/privatechain” -mine

While the first block is being mined, ethers will be being added to your address. It may take a while depending on your parameters set in your genesisblock.json file

Typical output seen during mining

Now that mining has begun, you can see the new files that have appeared in the privatechain filesystem

geth.ipc transactions.rlp and the .ldb database files appear once the genesis block has been mined

SUMMARY

The steps performed above can be summarised as follows.

You can stop the mining process using the miner.stop() command

SMART CONTRACTS

“Smart contracts” is the name to describe a program that is compiled to execute on the blockchain network. Such programs running on Ethereum blockchains are written in the Solidity language.

SMART CONTRACT DEVELOPMENT ENVIRONMENT

Download ganache from here

It’s an .appimage weighing in at around 146MB. TruffleSuite Ganache is produced by ConsenSys Software Inc.

The TruffleSuite Ganache Downloads page.

chmod +x the ganache appimage file and execute it.

chmod +x ganache to allow execution of it

GANACHE

Enable/Disable analytics and click Continue
Click on Quickstart
Change Port Number to 8545 for use with Metamask or Brave Crypto Wallet Browser extension
Enable Crypto Wallets in Brave Browser
Click the option to Restore a Local Wallet

Enter the seed phrase from Ganache into the Seed phrase field in Brave

Copy and Paste this into the Restore Seed Phrase field in Brave Crypto Wallet Restore and set a new password for the wallet.
Once your wallet is displayed, change the Ethereum MainNet to Localhost:8545 to connect to the Ganache private blockchain.

Connect to the local Ethereum blockchain network on port 8545 (Ganache’s internal eth network) and you’ll see an initial balance of 100 ETH tokens.

Click on the coloured icon next to Localhost:8545 and Import a Wallet

In Ganache, click on the Key icon on one of the wallets (choose the 2nd one in the list of pre-configured wallets). Copy and Paste the private key into the Import screen shown above.

Copy and Paste the private key of the first ganache ethereum wallet into the Browser Crypto Wallet Importer
You’ll import the 2nd wallet, so now Brave displays the balance of the first and second Ganache wallets. Ganache comes with a few more wallets for you to play with in your private ethereum development network.

You can see in the screenshot above, two Accounts have been imported, each with a balance of 100 ETH tokens. You can import as many of the ETH wallets from Ganache as you like/need for your Solidity Smart Contract project.

Since Ganache is connected to our local private blockchain running on our localhost on port 8545, you can see the details of the genesis block in the BLOCKS section

REMIX SOLIDITY IDE

Next we need an IDE for creating code when developing, compiling and deloying Solidity smart contracts that will use our connected wallets to pay the gas fees required to store the smart contract on our private blockchain. Remix is a web-based IDE for this purpose.

The web based Remix IDE for Solidity smart contracts running on Ethereum blockchains.
Click on the Deploy and Run Transactions icon (shown) in the left-most panel, select Injected Web 3 and it’ll attempt to connect to your Brave Crypto Wallet. Tick the ETH wallet you want it to use and Authorise the connection of the wallet to Remix. Note that any actual transactions invoked when deploying compiled smart contract code as byte code to the private blockchain will still require manual authorisation by the wallet owner.
Note that none of the pre-defined smart contracts have yet been compiled into bytecode so cannot be executed using our connected wallet.
Click on the Compiler icon (shown) in the left-most panel and compile the smart contract.
The successfully compiled files can be deployed.

Compile all the scripts 1_Storage, 2_Owner, 3_Ballot and 4_Ballot_Test files. Deploy the 4_Ballot_Test file.

CONFIRM TRANSACTIONS

The connected wallet will prompt for a confirmation for the transaction.

The transactions in the smart contract will require confirmation by the owner of the connected wallet before being submitted to the private blockchain we created earlier where they’ll be mined.

AUDIT TRANSACTIONS STORED IN MINED BLOCKS

The genesis block and our two transactions above can be seen in the BLOCKS tab in Ganache.

In Ganache that we connected to our local private Ethereum blockchain, you can click on the BLOCKS section and see the mined blocks that contain our recent two transactions, the date stamp of the transaction and gas used.

Click on the transactions button for each mined block to see the details of the transaction in that block.
You can view the transactions in the TRANSACTIONS section if you don’t know what BLOCK contains it in the BLOCKS section.

SUMMARY

So far, we have…

Installed Ethereum

Created a private Ethereum blockchain on our localhost

Initialised the private blockchain

Mined the Genesis block in our private blockchain

Installed Ganache and connected it to our private blockchain

Imported two of the ETH wallets in Ganache to our Web Browser’s crypto wallet extension

Connected to Remix Solidity Smart Contract IDE

Connected Remix to one of our ETH Wallets

Compiled some Solidity Code Smart Contracts

Deployed the BallotTest smart contract to our private blockchain, confirming the transactions and accepting the gas fees.

Audited the mined blocks containing our transactions in Ganache

EXTRAS

The other smart contracts were compiled and deployed, and new blocks containing those transactions show up in Ganache.

The blocks in our private blockchain
The transactions in the blocks on our private blockchain
The transaction data contained in the contract creation transaction
The resulting balance in our connected ETH wallet used to pay the gas fees on the network
The transactions in our wallet.

FINAL WORD

Note that all this information is referenced from the same blockchain, instead of the necessity of trusted third parties storing the same data in multiple places that could be changed and a conflict introduced into the history of the transactions in potentially mismatched ledgers. In addition to having an accurate central ledger that all parties reference, we can introduce more nodes in our network that each maintain a copy of the same blockchain.

The public ETH network that maintains the main net has many thousands of ETH nodes. It is this decentralised consensus that gives the value to blockchain, along with public keys being used to verify that the parties transacting have the necessary funds without revealing the private keys necessary to unlock those funds to the public internet.

The input and outputs between the wallet addresses are performed by the closed network, keeping everybody safe. The issuance of new coins is also controlled by the network to prevent abuse of the inflation rate that could affect the purchasing value of the tokens by driving the value down by diluting the supply ad infinitum (hyper inflation caused by excessive quantitative easing, effectively) .

Did you like this?
Tip cyberfella with Cryptocurrency

Donate Bitcoin to cyberfella

Scan to Donate Bitcoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to cyberfella

Scan to Donate Bitcoin Cash to cyberfella
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to cyberfella

Scan to Donate Ethereum to cyberfella
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to cyberfella

Scan to Donate Litecoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to cyberfella

Scan to Donate Monero to cyberfella
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to cyberfella

Scan to Donate ZCash to cyberfella
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.