Setting Up a Private Ethereum Network using GETH
Nearly a month ago, I knew nearly nothing about anything to do with blockchain or cryptocurrencies. To me and most of America, cryptocurrencies represent nothing but a new form of money, similar to a dollar or stock, and to some extent they still are. But let me tell you a little bit about what I learned over the course of my capstone at Fullstack Academy.
Satoshi Nakomoto, the mysterious father of bitcoin, described bitcoin as this:
A purely peer-to-peer version of electronic cash [that] would allow online payments to be sent directly from one party to another without going through a financial institution. -Satoshi Nakomoto, Bitcoin: A Peer-to-Peer Electronic Cash System
It was beginning with this paper and bitcoin’s rapid rise to fame that blockchain technology rose began its prolific spread.
Blockchain lends itself quite well to the idea of money since the ability to conduct a transaction which is immutable and double-spending free eliminates many of the hidden costs of purchasing something. There is no bank middleman and confirm the validity of the transaction, transactions are almost impossible to fake or change, and transactions can only be made if the paying party has enough coins in their wallet.
My capstone team consisted of two seasoned professionals with different skills from a financial background, Sid Reddy and Ken Russo, myself who I would consider a recently graduated biomechanical engineer, and a sophomore from NYU Stern, Aatish Varma. Together we implemented our own private Ethereum blockchain which utilized technologies that we learned such as React.js and Redux, Express.js, Node.js, and SQL database in addition to completely new technologies such as go-ethereum, truffle, solidity, azure, and web3 in order to incorporate our app into a private blockchain.
We settled on Ethereum due to our interests in smart contracts, transactions which automatically complete themselves when a certain event happens. These contracts are the main difference between Bitcoin, which is simply a currency, and Ethereum, which is a platform for many different things including applications due to the idea of smart contracts.
Starting something from scratch that is at the bleeding edge of technology is difficult. Go-ethereum documentation is useful, but does not provide the whole picture needed to start your own decentralized network. In fact, blog posts detailing how to create local nodes and mine ether off the Ethereum network helped immensely in starting our rocky start from a couple of guys who knew practically nothing.
Geth is a method in which you can connect to the Ethereum blockchain and begin participating. Just installing the client and using the command connects you to the main Ethereum network, which requires you to download the blockchain in its entirety, but after that you can begin mining if you attach a geth console to the process. There are plenty of guides to do this out there, and I won’t go into much detail here.
After getting the one node setup, this is where it got interesting. We wanted to create an application to solve the problem of student tardiness at Fullstack Academy by recording student checkin times as a form of attendance (though it was more like we wanted to play with blockchain technology and Ethereum and used this solution as an excuse to do it), so the blockchain we were using had to be private since it contained information about the people at Fullstack Academy. There are also plenty of guides out there describing how to create a private chain on a local machine, but I’ll go through the process since I had some difficulties here.
To create a private chain you’ll need a CustomGenesis.json file. This file is the details of the first node in your chain, the seven commandments, the bible, whatever you want to think of it as. Every block after it will be affected by this somehow. There are plenty of cookie cutter ones out there, but I’ll provide you with the one I used specifically:
It’s a pretty basic genesis file but two things to note here are the difficulty which is set to 1 which will be explained later, and the allocation. To allocate an account on your blockchain this money, you have to first create an account before you initiate your blockchain with this file. I too many difficulties doing this one thing, but what worked in the end was to initialize the blockchain, attach a console to the geth.ipc process, and then create an account through the console. Then I deleted the geth folder containing all the chain data for the blockchain, and reinitialized it. There probably is an easier way, but accounts are associated in a separate folder away from the chain allowing them to exist even if there is no blockchain. After this, changed the account hash in the alloc of the CustomGenesis.json (short for initial allocation) and now that account contains more ether than you could ever imagine. The commands to execute the above are as follows:
Great. Now you have a genesis block and an account with an obscene amount of money on this blockchain! To start your blockchain instance on your machine, simply run the following command:
It’s pretty overwhelming with all the flags you can use, but simply use the general format for now. Fiddle with the flags later. We can now attach a console to the blockchain process through the following command:
This will attach a console to your blockchain process allowing you to execute commands. Typing in eth, miner, net, or personal, will bring up some of the global variables you can use to interact with your blockchain or pull relevant information out of it. Now you have your own local private blockchain! Fool around with it by creating more accounts or sending money from one account to another. Yay!
But that’s pretty useless. What’s the point of a blockchain if you can’t have the peer-to-peer connectivity of it? What about nodes being able to communicate with one another through the internet and updating their own copy of the blockchain when any of them are updated? What about being able to simulate a (pseudo) decentralized network? It was here that setting up the decentralized network really got difficult since no posts really describe what goes into it.
We began by trying to setup a two node system between a local machine (my own laptop) and an AWS remote server. Some of the issues include networking stuff that Information Technology guys usually deal with: opening ports, making sure that two computers can communicate with one another, etc… However, it just so happened that both me and Ken had relevant IT and Devops experience which made it possible in the end.
To connect two nodes, first you need to init a genesis block on each machine you want to connect together. It should be exactly the same, so grab a usb or send a message or something to yourself. Use the command above to create the genesis block on each machine. You also need to allow the two of them to connect on the same network id, and configure the RPC (remote procedural call) options properly. You can have two nodes connect directly to one another by specifying the enode address in the geth command which starts the chain (not to be confused with the one that initializes the chain).
Unfortunately I can’t find the code, but it’s similar enough to the bootnode example that you can use it to sort of figure it out from there. A bootnode is a sort of hub that all nodes can connnect to to figure out other nodes in the system. This is nice because you don’t have to go out and find all the enodes of the nodes that you are trying to connect together and I do recommend trying to utilize a bootnode if you’re connecting more than three nodes together. The bootnode documentation isn’t so bad, so I’ll skip over that as well. The geth command to run a blockchain with other nodes is as follows:
Let it be said that RPC is NOT secure. I know of the risks and only allowed it since the nature of what we were doing wasn’t all that important after all. Some of the folks at the ethereum/geth gitter were very helpful in setting it up and suggested IPC instead, but that’s an unexplored avenue for me.
Great. Now you have a node network which you can call your own! Before I go, here are some tips regarding setting up a node network:
If your nodes aren’t connecting, first make sure that the two computers can accept RPC from one another. An easy way to do that is to use telnet.
If it doesn’t connect, configure your ports to accept RPC on the port that you specified in the geth command. In fact, look carefully at all the RPC flags that are used above. It was only after playing around with RPC and ports for about a day before we could get two nodes to actually connect to one another.
I’ll post a follow up explaining how you can use web3 and express to make api calls to your blockchain to pull out relevant data. Thanks for taking the time to read the post!