Smart Contracts in Practice

Srini Karlekar
5 min readNov 8, 2020

Two and a half years ago, I decided to build an open-source, distributed knowledge sharing system called SmartQuora that emphasized and incentivized the quality of the content shared on the internet.

SmartQuora enables participants to pose questions about any topic with a declared award for the best answers. The award is in the form of Utility Tokens that the participants can exchange for cryptocurrency. Inquirers specify a time limit within which they want the question answered. The system entertains multiple answers per question, while the inquirer and other participants upvote the best answers. When the time expires, the system counts the popular votes and distributes the award proportionately amongst the responders.

I felt the need for such a system when I was exploring different blockchain frameworks for building Blockchain and Smart Contracts based applications for an employer. During my research, I could not find credible information on how various Blockchain frameworks such as Ethereum or Hyperledger enforced rules using Smart Contracts. Further, it was not clear how one could effectively integrate them with multiple capabilities such as Digital Identities, social-media-based authentication mechanisms, and databases to build an end-to-end solution. As these frameworks were open-source, the support for them was poor. Content on sites such as StackExchange was unreliable and outdated. While developers tend to help each other when they have time, there is no incentive to answer questions actively. What if an ecosystem allowed inquirers to pose questions for which they were ready to pay? What if the popularity of the answers drives the award amount for the responders? What if the questions were to be answered within a time limit? What if the system enforced rules such that participants cannot game the system to upvote their answers? What if the system has to be tamper-proof but open enough that anyone can audit its data and code?

Answers to these questions led to a set of rules that could form the basis for a knowledge-sharing application where participants have a stake in their content quality. While my research was on Smart Contracts, I quickly realized that I could use Smart Contracts itself to enforce the rules and build an open, fully transparent, decentralized, knowledge-sharing application around it. Thus, the idea for SmartQuora was born.

Blockchain is a continuously growing table of records organized in blocks that are linked together by cryptographic validation. This validation system results in new transactions being added irreversibly and old transactions preserved forever for all authorized participants to examine. Applications built on top of Blockchain technology are called DApps (or distributed apps). DApps consist of code artifacts called Smart Contracts stored in the Blockchain as Chain Code and get executed when a specific event takes place. The event could be universal such as the passage of time or an external event. This storage of code and data that it operates on in a tamper-proof medium is what makes Smart Contracts an ideal framework for distributed applications where there is no central authority to manage the network. This framework was what I needed to build the distributed, knowledge-sharing system.

The first hurdle I faced with the design of this distributed system was selecting a blockchain platform. Ethereum was the first choice given its popularity, but it required expensive mining operations for performing transactions and enforced usage of Ether as the cryptocurrency. Ethereum also uses a language that I was not familiar with called Solidity to code Smart Contracts. On the other hand, Hyperledger uses the much familiar JavaScript to code Smart Contracts, had no built-in cryptocurrency requirements, and required no mining for performing transactions.

Having settled on Hyperledger, the next challenge was identifying users effectively and authenticating them. A traditional system stores the user profiles in its central database that is captured during user registration. As a result, the system has full control over its user verification. On the other hand, a distributed system has to rely on a well-known identity verification system to authenticate the user and generate their profile. The open-source Passport authentication middleware came to the rescue. I used the Passport’s delegated authentication strategy to allow users to authenticate themselves using their Google account. When a user logs in for the first time on SmartQuora, a digital certificate is generated for that user. This information is stored in a digital envelope, also called Wallet in Blockchain parlance.

During the design, the next challenge was to find a cost-effective online data store to store all information regarding the users and their wallets. I chose MongoDB and specifically mLab, the managed-service version of MongoDB, and took advantage of its free tier.

Apart from storage, every distributed app requires a service layer to communicate with the Chain Code on the Blockchain and a front-end user interaction. I took advantage of Hyperledger’s REST server to create an API layer for the Chain Code and built a rudimentary UI to interact with the Smart Contracts running in the back-end. After integrating all the capabilities, the completed application was hosted on EC2 instances on AWS Cloud.

With the application ready, I crowdsourced the testing by encouraging my colleagues and family to take it for a test-drive. They provided valuable feedback, which I incorporated into the system.

After a detailed write-up, I presented this solution as workshops at multiple conferences such as the Google Developer Conference in Tysons, VA, and the Linux Foundation Conference in NYC to a broad and enthusiastic audience. You can find the details regarding the workshop and presentation materials at http://bit.ly/oftfscsk.

Overall it was a very involved and innovative project due to the sheer number of patterns, frameworks, and integration points implemented. While I did not get to take this proof-of-concept and build a commercial product, I created a foundation on which such a knowledge-sharing platform that incentivizes the quality of content can be erected. Additionally, I contributed to the open-source community with an end-to-end tutorial on building Smart Contracts.

For more information on Blockchain and Smart Contracts, refer to my whitepaper at http://bit.ly/blkchn2018. For the tutorial and source code of SmartQuora, go to http://bit.ly/SmartQuora.

--

--

Srini Karlekar

Servant Leader & Cloud Architect passionate about #Opensource, #Serverless, #MachineLearning, #AIaaS, Computer Vision and #IoT.