A Java developer approaching the blockchain (I)

With the mainstream press having started mentioning buzzwords like Bitcoin, digital currencies, etc. few months ago I decided it was eventually time to get a better grasp of what a blockchain is. Why so many people are going crazy about it? What are they really getting out of it? And is this something that could possibly affect me, as a software developer?

The topic is huge and the Internet is already rich in articles and blogs about it. So I'll put aside the very interesting theories around the potential shifts in the World's economy due to the blockchain technology adoption... and the (so far mostly rewarding) speculation that's currently on. Instead, in this post I'll focus on how I got fun in learning a bit about the technology and in applying it to something relatively close to my day-to-day job topics.

One of key concepts around blockchain is decentralization. To put it very simple, the idea is that people can run applications based on the blockchain basically without the need of relying on software served from a central server or a cloud service. An ecosystem of decentralized applications is quickly growing up on top of the Ethereum blockchain, as a consequence of it having been explicitly designed to allow users deploying pieces of code named smart contracts. After having validated some conditions, a smart contract can transfer assets (ethers) between user accounts (wallets), with the transaction being stored on the blockchain; this effectively allows creating applications that deal with "value" without the need of a central authority.

With the above premises (and some time spent on studying the theory around this topic, which I'm assuming the reader already invested too), I decided to try writing a simple distributed application (DAPP) interacting with the Ethereum blockchain.

The ballot

One of the most common examples used when introducing this technology is the ballot, so without too much creativity I decided to go for an application that allows voting among alternatives. The idea is that each voter casts his vote by sending ethers towards a smart contract. When the voting phase is over, the alternative that got more ethers wins and the creators of the smart contract get the collected ethers as a prize.

The contract

Smart contracts can be written in few different programming languages, but the most common solution these days is using Solidity. I'm pretty sure future posts on this blog will cover a bit of the Solidity synthax and explain how to write a contract with it. As I started my journey in this "world" with Stefano, I lazily grabbed a ballot contract that he had already developed and focussed on different aspects first ;-)

The environment and supporting tools

Among the first tasks to be solved there's setting up a working environment for creating and testing the application. Ethereum features test networks, to allow developers playing with their creations without paying; however for those approaching this world for the first time like me and also needing a lightweight, clean and controllable testing enviroment, the solution is really simulating the network locally by running a local test blockchain. This is exactly what testrpc, a Node.js based Ethereum client, does. After having installed it, by running


> testrpc --account="<privatekey1>,<amount1>" --account="<privatekey2>,<amount2>"



a test blockchain is exposed locally at http://localhost:8545, with the addresses whose private keys are provided owning the specified amounts of ethers.

OK, cool, but how are private keys generated? Again, as I was really looking forward for something working and wanted to go deep into specific topics later, I didn't spend to much time on the gritty details regarding public/private key generation, key management tools and so. I went for what seemed me the most straightforward solution, so visited https://myetherwallet.com/ , which is a fairly known website allowing generating and exporting wallets (hence public/private keys), as well as performing transactions on the Ethereum network through a JS client running in the browser. On MyEtherWallet I created 3 accounts and saved their json format files, something like:

{"version":3,"id":"ec55392d-bc5e-4805-b420-e1aab8edce4b","address":"a84e876bacafbb51cb4b4e3f2276535096d10b14","Crypto":{"ciphertext":"1c18215281fc6694ec9315bc4f5ad44482cdcf9f21e00e479285848f945b03a9","cipherparams":{"iv":"8acbe9bf7b768ce55c056e7844c8970f"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"4f7d8a7ab9c07d01275d9f228729ce1d5bc64927a865b45b9ea4fbb51ab6b203","n":1024,"r":8,"p":1},"mac":"6849928a907acab44fe6e8ae1653e5091d7f7285968d078f989aa064ef6a854b"}}

Then, the same accounts could of course be used on my local testing environment too.
Speaking of testing, a common need is being able to check what's actually written in the blocks that build up the chain, the transactions. On the Ethereum main and test network, a very cool website offers this functionality: https://etherscan.io/ . However I could not use that on testrpc, so I chose EthExplorer, a simpler (and less powerfull) tool that I got from GitHub at https://github.com/etherparty/explorer . EthExplorer connects to the testrpc chain on port 8545 and exposes a simple web app for browsing the chain at http://localhost:8000/ .

Of course, a developer needs to also choose an editor / IDE for actually coding the application: that clearly depends the technology / language that's selected for creating the application... but given I'm running out of time for today, I'll go on and talk about this in the next post, very soon.
Stay tuned!

Comments

Popular Posts