Blog

Blockchain Challenges And Triumphs

Starting with blockchain can be difficult because there are a lot of frameworks/clients for the different cryptocurrencies, and misleading information in various posts I read when I was trying to create my private chain. For example, the commands being used were outdated. Getting to know Blockchain and its benefits is very useful, mastering the topic and languages is challenging, but who doesn’t like a good challenge?

When I first started to look at Blockchain I got hooked. The fact that it’s incorruptible, transparent and decentralized got me thinking of the possibilities. I jumped in to learn about cryptocurrencies and I focused on Ethereum. I was really curious about Smart Contracts, and how they self execute when a given condition is met. There are many frameworks such as Mist, Meta mask, and Embark. There are clients for Ethereum written in Go, Rust, C++, Python, JavaScript, Java and Ruby with Geth(Go) and Parity(Rust) being the leading ones. I tried some of the frameworks but as a developer I must understand how everything works, the consensus, the mining and such, so I opted to use Geth (Go-Ethereum). With Geth you can connect to the live network, join the test-nets or create your private network. I figured I should create my own network to start mining, testing contracts, making scripts and such. I looked online for tutorials and I had very little success, most posts were outdated so I started to look in the source code of Geth. I read the Yellow paper for Ethereum which was hard. It contains a lot of raw information which took me a little while to comprehend. I also read a book on Solidity and its documentation to write Smart Contracts. Solidity isn’t hard to write once you understand how it works and its limitations (of course there are workarounds). I read the White Paper for Dapps and I looked in the source code of Geth to figure out how to create my private chain.

Here’s one of the reasons why knowing the source code is useful. When I started my private chains I couldn’t control the Difficulty, which is the scalar value corresponding to the difficulty level applied when finding the nonce of a block. The higher the difficulty, the more calculations a miner must perform to find a valid block, making the process of testing Smart Contracts and Dapps slow. So I changed a value in the consensus in Geth freezing the difficulty value to 1. In a custom chain without this change, as the blockchain grows so will the difficulty, for example, if in block 0 the difficulty is 1, by block 500 the difficulty will be 166784 and the total Difficulty 74106029 (a value representing the total difficulty applied up to the last block). Now when I made the change to freeze the difficulty in block 0 the difficulty was 1 and by block 500 the difficulty was still 1 while the total Difficulty was 501 making the testing of Smart Contracts and Dapps much faster.

Geth offers a JS console to interact with the blockchain to start mining, creating accounts, and so on. Therefore I was wondering if I could make scripts in order to create, for example, an explorer or get the full history on the chain or its transactions. I created a script checking for every block in the blockchain if it had transactions, and showing the details of each transaction, or showing only transactions made to/from an account or a contract. Also I created a script to mine only when there was a transaction, to create and set a new coinbase and to create a register of user information such as Name and E-mail in a Smart Contract.

With these scripts I was curious to run a scalability test. I created a contract which saved user information, Name, Email, Permission. The idea of the permission was to give a user only access to specific parts of the Dapp. The goal was to make a cluster of 15 nodes and get them all to register users with different information and compete with each other to find the next available block. The process took 2:30 hours and there were over 45,000 users, 0.5 users per second.

Then I moved on to create a Smart Contract which could save documents, and keep track of who saves which documents in the blockchain. Due to Solidity limitations I created two Smart Contracts, a Storage contract and a Record contract. The Storage contract only needed to be deployed once since it saved any arbitrary amount of data with a name acting as a key for later data retrieval. It saved data identifiers to avoid overwriting, and retrieved the Record contract address through the user account address.

The Record contract was created each time a user was registered and the address for each contract was saved in the Storage contract, which saved each document name saved by the user. Due to solidity limitations, you cannot retrieve an object with a Solidity function. You can create a fixed object but it limits the scalability. The solution I used is that for each file saved in the Storage contract, the name of said file was saved in the user Record Contract. This approach was scalable and transparent as we could see who saved which file. These contracts are showcased in the Dapp Blockchain4LinkedData.

As example document, Smart Contracts have saved RDF documents (Linked Data) in the blockchain.

Linear workflow of De-centralized app

This diagram represents how the second contract (Storage Contract) works. From a cluster of nodes, the Dapp call the contract functions from the blockchain and returns to the user a dialog box to specify the document to be saved in the blockchain. Once the user specifies a document to be added to the blockchain, the document is given a unique identifier and the Smart Contract is executed resulting in a transaction saving the document in the chain. The document can be retrieved through its identifier, such as “example.rdf”.

Mastering the topic, I can understand why blockchain is on the rise. The possibilities are endless. I truly think this technology is going to change the world.