In this post, we will show how to write a simple smart contract. If you do a quick google search, you will find numerous articles on writing smart contracts. In my previous post Blockchain, I explained blockchain. But I didn’t talk about the smart contracts in that post. That’s why a separate post.
Smart Contract
In layman’s terms, the contract is nothing but an agreement between two parties, witnessed by a third party to hold both parties accountable for playing out the contract. So what is a Smart Contract then?
In Nick Szabo’s words
“Smart Contracts are a set of promises, specified in digital form, including protocols within which the parties perform on these promises.”
In Web Developer’s terms, a Smart contract is like an application API, but there are few exceptions. Like an API can call another external API, a smart contract can not call external API. A smart contract can call another smart contract. A smart contract comprised of a number of private functions and variables to implement the agreement.
More formal definition of a smart contract is a method of using Bitcoin to form agreements with people via the blockchain.
Ethereum
So how do we write these smart contracts? Ethereum is one such a platform that is used primarily for building and publishing distributed applications. It is a Turing-complete virtual machine built for the purpose of cryptocurrency. It is the fundamental underlying infrastructure platform that can run all blockchains and protocols. Each node in Ethereum runs an Ethereum Virtual Machine. This EVM hosts distributed programs (smart contracts) which get executed seamlessly.
Implementation of smart contracts
To address some basic questions like “How does a smart contract look?”, “What do you use for programming a smart contract?”, I will go over some simple concepts.
There are currently two programming languages that can be used to write a smart contract.
Solidity – A javascript look-alike language with file extensions .sol
Serpent – A python look-alike language with file extensions .se
Once a contract is written in either language, you can compile it using a solc compiler. A compiled contract then posted on the network. You can call this contract in your web app by using web3.js Javascript API.
Conclusion
In this post, I tried to explain one of the key concepts of blockchain, a smart contract. This can be used further in building decentralized applications. In the next post, I will show how to write a simple smart contract and run on a node with EVM.
Yes, there might be a plethora of articles about blockchain and how web developers can use to build applications. And this might not be a much different article either. In this post, I describe the basics of blockchain and crypto technology.
Introduction
“Blockchain has been the underlying technology for cryptocurrencies like bitcoin.“
Firstly, this is a basic understanding of blockchain. We will cover the rest of the basics of blockchain soon. In most banking or financial systems, all bank accounts track through a ledger that keeps track of incomes and expenses.
Secondly, in current times, our centralized financial systems follow certain rules and regulations. A central authority defined these rules and that’s how trust was built. But blockchain is a decentralized system of the ledger where a peer-to-peer network is involved. Based on the peer-to-peer network, miners involved in the process, build trust in a decentralized form. All transactions are recorded on ledger and ledger is verified by nodes in the network. These nodes communicate with each other cryptographically for verification of transactions. When new transactions are added, there is a consensus formed in the network, this consensus is nothing but a block.
Proof-of-work
One reason why blockchain is popular is that it solves a double-spending problem that has been there for a long time in computer science. When it comes to a distributed system, there is no way to correctly verify the integrity of transactions. In relational database systems, we use referential integrity to verify integrity.
This is the foundational algorithm in the blockchain. In the mining process, miners compete with each other in the network to verify transactions and produce new blocks. For this work, miners get cryptocurrency.
In network, transactions happen all the time between users. A decentralized ledger will keep track of all these transactions. Miners will verify these transactions through proof of work algorithm.
Person A sends $10 to person B and not to person C. How do we verify that money went to Person B and not to Person C? This is a double-spending problem. Proof of work helps to solve this problem. There are other aspects to this algorithm like how to avoid any security threats, faster block generation(Power of network), storage capabilities. We will not be discussing those aspects here.
Drawbacks
There are few drawbacks to this algorithm and one of the major one is 51% attack. The idea of 51% is when a user or a group of users control the majority of mining power. If this happens, the group can monopolize generating new blocks and this will lose the advantage of the decentralization principal.
Blocks
Blocks form the ledger which forms the basis of blockchain. Each block contains transaction information which we can call as a fact. A block is nothing but the arrangement of all these facts and each block will have a reference to the next block.
Before these facts get added to blocks, they are pending and as miners continue to work, they verify these facts to confirm.
Conclusion
In conclusion, I introduced blockchain with some basics of blockchain for the web developers. But this is just the tip of the iceberg, there is a lot to learn and bigger things to do in the cryptocurrency world. If you enjoyed this post, subscribe to my blog.
References
Blockchain: A blueprint for the new economy by Melanie Swann – Blockchain
In the previous post Redis Caching, we saw how to use Redis caching with all default settings. We didn’t have any Cache Manager or anything, but we were able to cache data. In this post, we will show how to use RedisCacheManager to cache the data. This manager can further be extended to customize the caching configuration even more. But we will not be looking into customization in this post particularly.
Implement CacheManager for RedisCacheManager
Most of the code for this post will be similar to what we implemented in the previous post. We will just show how to use CacheManager.
To implement CacheManager first we remove @EnableCaching annotation from the main class SpringAppCacheApplication. Now we add a new CacheConfig class to configure our cache manager.
Basically, this CacheConfig will define CacheManager which build a redisTemplate to get JedisConnectionFactory which will be our java client to connect to our Redis server. This JedisConnectionFactory will get server host and port properties from application.properties file. The source code will look like below:
In this introductory post, we will show how to use Redis caching in a simple spring boot application. In subsequent posts, we will evaluate different factors of Redis caching. But for now, we will try to focus on the simple problem of providing caching to a rest service that provides companies-related data to the user interface. This data is in a database, but caching will help us improve the performance.
What you need
Java 8
MySQL Database
IntelliJ Editor
Gradle
Redis Server and Redis Desktop Manager
Spring Boot Based Rest Service
As part of this post, we will build a simple spring-boot based rest service. This rest service will provide data related to companies which will be stored in mysql database.
We will be using Gradle to build our dependencies in this project. Important dependencies for this project are spring-boot-starter, spring-boot-jpa and spring-boot-starter-data-redis With all the needed Gradle dependencies, our Gradle script will look like below:
Here you can see, we have enabled caching with annotation @EnableCaching.
Now in our RestController class CompanyController , this will show annotation of @Cachable that helps decide when to cache data for the incoming request. This annotation caches data that has been fetched for the request based on configuration.
Here is a controller, if you see we are caching the data coming from the database with annotation @Cacheable
To make sure data gets cached with Redis server, we will need certain properties where these annotations will help us to cache the data. The properties to configure Redis server are below:
Once you build the project and run it, we will be able to perform the REST requests to fetch data. If we perform the same requests multiple times, we will be able to see the data in redis.
Conclusion
In this post, we showed how to use redis-caching to cache the data for a spring boot based REST service. The code from this post is available to download github
In this post, I will discuss one tip everyone can use to avoid bouncy castle error.
Problem
Recently I was working on building a SOAP webservice where we were using Apache CXF libraries along with Spring boot. We built the webservice, but when we were sending a SOAP request through the client, we kept getting following error:
Caused by: java.lang.NoSuchMethodError: org.bouncycastle.math.ec.ECCurve$Fp.<init>(Ljava/math/BigInteger;Ljava/math/BigInteger;Ljava/math/BigInteger;)V
at org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertCurve(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertPoint(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey.<init>(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
at sun.security.ssl.HandshakeMessage$ECDH_ServerKeyExchange.<init>(HandshakeMessage.java:1075)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:284)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1026)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:961)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:553)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:412)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:179)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:328)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:612)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:447)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:121)
A Simple tip to avoid this error –
We tried different things to resolve this issue. We tried to exclude bouncycastle jars from cxf-rt-ws-security dependencies we were using as we were pulling them from other dependencies. But this error would still pop up intermittently. Eventually, I figured out the issue. We had to add this dependency of bouncycastle explicitly as below in our Gradle build file. That’s when the error went away.
compile (‘org.bouncycastle:bcprov-jdk15on:1.56’)
Conclusion
In this post, I showed how to use this one tip to avoid the error caused by the bouncy castle. If you enjoyed this post, subscribe to my blog here.