Monthly Archives: March 2018

Smart Contracts in Blockchain – Part II

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.

References

  1. Building a smart contract – Smart Contract Ethereum
  2. Blockchain for web developers – Blockchain

 

Blockchain for Web Developers

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.

Blockchain for web developers

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

  1. Blockchain: A blueprint for the new economy by Melanie Swann – Blockchain
  2. Proof of work – Proof of work
  3. Ethereum for Web developers – Ethereum for web developers

 

Redis Caching with RedisCacheManager

Introduction

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.

RedisCacheManager with Redis

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:

package com.betterjavacode.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@EnableCaching
@ComponentScan("com.betterjavacode.config")
@PropertySource("classpath:/application.properties")
public class CacheConfig extends CachingConfigurerSupport
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CacheConfig.class);
    private @Value("${spring.redis.host}") String redisHost;
    private @Value("${spring.redis.port}") int redisPort;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public JedisConnectionFactory redisConnectionFactory()
    {
        LOGGER.info(" Inside redisConnectionFactory()...");

        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();

        redisConnectionFactory.setHostName(redisHost);
        redisConnectionFactory.setPort(redisPort);
        redisConnectionFactory.setUsePool(true);
        return redisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory rf)
    {
        LOGGER.info(" Inside redisTemplate()...");

        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate)
    {
        LOGGER.info(" Inside cacheManager()...");
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(300);
        return cacheManager;
    }
}

Now if we build our application and run it, Spring boot console will show the following output

2018-02-28 20:31:41.913  INFO 9856 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-02-28 20:31:42.034  INFO 9856 --- [           main] o.s.j.d.DriverManagerDataSource          : Loaded JDBC driver:com.mysql.jdbc.Driver
2018-02-28 20:31:42.244  INFO 9856 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-02-28 20:31:42.288  INFO 9856 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [ name: default        ...]
2018-02-28 20:31:42.495  INFO 9856 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate
 Core {5.2.13.Final}
2018-02-28 20:31:42.499  INFO 9856 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate
.properties not found
2018-02-28 20:31:42.599  INFO 9856 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hiberna
te Commons Annotations {5.0.1.Final}
2018-02-28 20:31:43.688  INFO 9856 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dia
lect: org.hibernate.dialect.MySQL5Dialect
2018-02-28 20:31:43.764  INFO 9856 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000423: Disabling
 contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2018-02-28 20:31:44.684  INFO 9856 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA Enti
tyManagerFactory for persistence unit 'default'
2018-02-28 20:31:45.184  INFO 9856 --- [           main] com.betterjavacode.config.CacheConfig    :  Inside redisConnectionFactory()...
2018-02-28 20:31:45.288  INFO 9856 --- [           main] com.betterjavacode.config.CacheConfig    :  Inside redisTemplate()...
2018-02-28 20:31:45.346  INFO 9856 --- [           main] com.betterjavacode.config.CacheConfig    :  Inside cacheManager()...
2018-02-28 20:31:45.985  INFO 9856 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@30946e09: startup dat
e [Wed Feb 28 20:31:37 CST 2018]; root of context hierarchy
2018-02-28 20:31:46.214  INFO 9856 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/cachedemo/v1/companies/{id}/],methods=[GET],produces=[application/json]}" onto public com.betterjavacode.models.Company com.bette
rjavacode.resources.CompanyController.getCompany(int)
2018-02-28 20:31:46.217  INFO 9856 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/cachedemo/v1/companies],methods=[GET],produces=[application/json]}" onto public java.util.List<com.betterjavacode.models.Company>
 com.betterjavacode.resources.CompanyController.getAllCompanies()
2018-02-28 20:31:46.222  INFO 9856 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}"onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframewo
rk.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-02-28 20:31:46.223  INFO 9856 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web
.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-02-28 20:31:46.300  INFO 9856 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-28 20:31:46.301  INFO 9856 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-28 20:31:46.377  INFO 9856 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-02-28 20:31:47.071  INFO 9856 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-02-28 20:31:47.184  INFO 9856 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-02-28 20:31:47.195  INFO 9856 --- [           main] c.b.S.SpringAppCacheApplication          : Started SpringAppCacheApplication in 10.626 seconds (JVM running for 11.552)

In this console output, we will see our log statements Inside redisConnectionFactory, Inside redisTemplate, Inside cacheManager.

Conclusion

In this short post, we showed how to use RedisCacheManager to configure Redis for a spring boot application.

References