The Ultimate Corda Tutorial 2020

0

Are you looking for Corda Tutorial? If yes, then welcome as we are going to go through a detailed Corda blockchain Tutorial! You can also see the tutorial as the Corda tutorial for beginners.

Blockchain has come a long way. The idea of distributed ledger technology has solved many pain points that the current organizations go through. The main aim is to become more efficient, secure, and scalable without investing millions of dollars. 

Corda is an open-source blockchain platform that provides a global blockchain ecosystem. It offers strict privacy with the use of smart contracts. It also helps the business to transact directly and provide a cost-effective approach to streamline business operations.

Blockchain Certification Course

The main difference is how Corda handles the transactions. It is a DLT that wants to improve blockchain feasibility, speed, and scalability by not having a complete ledger with each peer. If a transaction can be completed between two peers without informing the whole network, then Corda will use that approach. It is also designed to include other parties that make it easy to follow the transaction and see its validity. It is a distributed ledger technology for the business.

We have already covered Cora in detail on 101Blockchains.com. If you wish to follow the tutorial, then we request you to go through the following article Corda Blockchain: Ruler Of the Financial Enterprises.

If you went through, all of them, congrats! However, a basic understanding of Corda will do.

Let’s get started with our Corda blockchain tutorial now!

How the Corda development tutorial is structured

We will cover Corda in detail. In most of the sections, we will try to cover the important Corda concepts.

The key concepts that we will cover include the following.

  • State
  • Contract
  • Flow sections

Our tutorial will create an IOU application. IOU stands for “I Owe yoU.” This is a simple presentation of the fact that one person owns money from another person. We will cover the concept more in detail later on.

To make things simpler, we will be using Kotlin programming language. As a learner, it would be great if you know Kotlin, but it is not a necessary skill to have to follow the tutorial.

Blockchain Certification Course

Tools

Before we get started with the Corda tutorial for beginners, we will need the following tools.

  • Kotlin
  • Gradle
  • Git
  • IntelliJ
  • Command-line

As of the integrated development environment, we are going to use the IntelliJ. It will simplify our development.

Corda Tutorial

Corda tutorial

What is Corda?

Corda is best defined as an open-source blockchain that solves business problems by providing an interoperable blockchain network. Its key features include strict privacy, efficiency, and direct transactions using smart contract technology.

Also read,

The smart contracts used in Corda can be written using JVM languages or Java. The dApps within the Corda platform is known as CorDapps. Also, they offer peer-to-peer nodes network with the ability to use Notary infrastructure. The infrastructure is used to validate and sequence transactions without needing to broadcast the transaction details to every peer on the network. Another important component used by the blockchain includes the Flow framework, which manages the negotiation and communication among participants.

Understanding the Corda Ledger

Corda Ledger is not similar to the ledger system utilized by traditional blockchain technologies. That’s why we need to have a clear grasp of how it works and understand it from both its functional point of view and data perspective.

The Corda ledger works like a graph. All the nodes in a graph are connected through each other, either directly or through some other nodes. All nodes can communicate with each other if they want to. The use of the graph means that there is no need to broadcast the transaction globally.

Nodes discovery

So, how do the nodes discover each other? They use a Network map to find each other. You can think of it, similar to a phone book. The Network map contains the metadata to locate each of these services and hence provides a nice way for map discovery.

How does it work?

The biggest challenge in a non-global broadcast blockchain network is consensus. So, how do the nodes know about each other and verify that information?

The answer here is shared facts. Each node overlaps any other node in some way or another. The shared facts are stored in the Corda ledger, which can then be used to verify the information. The Corda ledger contains a set of these sets. As there is no central node, these nodes act proxy when there is a need to verify things. To store that information, each node has a vault that contains the fact which cannot be changed.

States

States in the Corda blockchain are immutable. They are used to store shared facts about a transaction, agreement, or contract at a specific point of time.

Sounds technical? Let’s try to break it down in simpler words.

Shared facts

Let’s take two people using the Corda ledger. Let’s name them Alpha and Bravo.

Both of them have a shared fact. The shared fact is an IOU(I Own You). IOU is used when a lender has money due to the loan provider. To make the fact shared, both of them have the same shared information represented in the IOU.

Represent anything

States can be used to represent anything. This means that it can store any form of information for different use-cases. As a developer, you can use states to store KYC information, financial instruments, syndicated loans, and so on.

Also, states can be used to store multiple attributes. But, there is a limitation for states. The limitation is that once a state is given a type, its type cannot be changed. If you create an IOU state, then it will have to state the IOU state for the rest of its life.

Immutable and Evolving

States are created to be immutable. However, their immutability is concerning time. A state is presented at a given time. 

This means that a new state can be created in addition to the original state. It is done when there is a new change made to the state that needs to be stored. The old state is then termed as historical.

For example, if Alpha pays off its debt to Bravo, then a new state will be created, citing the fact that Alpha has cleared the debt. Also, if the state is completed, the IOU will expire. In simple terms, the life cycle of the state, in this case, is as follows.

  1. A first state is created to showcase that Alpha is under debt to Bravo. This states also share more information about the IoU, including the date, time, interest rate, the amount lent, and other useful information required for the IoU.
  2. Now, when Alpha resolves the debt, a new state is created that updates the new information. The old state is not touched in any way as it is immutable.
  3. The new state is now updated in the shared facts among the two users.
  4. Now, the old state is termed as historic. There can be multiple historical states in one single transaction. This can lead to creating a sequence of states.
  5. A sequence head is created, which refers to the latest state. Also, the latest states are also termed as unconsumed.

Getting the Development Environment Ready

Now, it is time to get our development environment ready for the Corda development tutorial. You need the following tools to get started.

Cloning Git repository

The best way to start our journey is to run an example of CorDapp in our development environment. The good news is that Corda offers an example CorDapp that you can download and run locally.

You can clone the repository locally using the git clone command as below.

git clone http://github.com/roger3cev/corda-training-template.git

Corda itself creates the repository for providing training to new learners. If you are curious, you can check out the Corda GitHub repository to find a plethora of projects that utilize Corda.

You can also use other samples that are available in the Corda samples repository. This should answer your Corda tutorial Github.

For the tutorial, we are going to use IntelliJ to import the git repository. Open up the IntelliJ. Once done, you will see the option to check out from Version Control. Click on it and then clone the repository on your computer.

Corda-tutorial-intelliJ

You can choose to save the project, you will find the project repository as below.

corda-tutorial-project-structure

At the bottom right, you will find a notification saying, import Gradle Project such as below. It is important for our Corda Tutorial to work.

Click on the “Import Gradle Project.”

Corda-tutorial-import-gradle-project

You also need to set up the Project SDK. Go to the project settings and then select the latest version of Java. In my case, it was set to Java 11.0.5.

If you find any error to run, then you should upgrade your Gradle and let it import the necessary repositories to make it work. It will take some time to do so. So, take a back seat and wait for it to get completed.

For me, it took around 20 mins to complete all of the Gradle dependencies updates.

Let’s quickly go through the structure of our Corda tutorial project. Our main repository is the src file. As we are using Kotlin, we will be using the Kotlin-source folder; you will find the src folder under it. Please do not confuse it with the java-source folder.

Corda-tutorial- kotlin-source

Here, you will find all the necessary files in the training folder. There is also a “test” folder which contains the test units for the application.

Another important file that we need to know about is the IOUState.kt. It is located within the training > state.

Getting State Units Tests Working

The IOUState class looks very basic. It is not completed yet. We are going to use a test-driven development methodology to develop the class and get it ready to work.

Test-driven development is a great approach when it comes to modern development practices. It helps iron out bugs or other issues right from the start.

That’s why the template that you downloaded already has corresponding tests for each of the classes, including contract, flow, and state.

To get started, we need to go to the Tests > Kotlin> net > Corda > training > state > IOUStateTests

Corda-tutorial- IOUStatetests

There you will find tests already written. It also comes with tons of documentation. The R3 Corda team has taken their time to create the template.

You need to uncomment the section, as shown below, to make a single test to run.

Corda-tutorial- uncomment-code

Select the code and press Ctrl + / to uncomment. If you use MAC, use CMD + / to uncomment.

Now, it’s time for us to run the unit test.

As our IOUState class is almost empty, the tests will fail, and you will get the following test error.

Corda-tutorial- failed-tests

As you can see, it is saying that the Kotlin-source:test failed due to the NoSuchFieldException. This means that the test is expecting the amount variable where it found none.

To fix it, we need to go back to our IOUState.kt file and edit it.

Before we proceed, we need to get a better understanding of Kotlin. To make sense of the code that we are going to edit, it is better to get an introduction. Kotlin is a dense programming language. Also, Kotlin is going to convert into JVM virtual machine, so we should be fine with the use of it in our tutorial.

corda-tutorial-project-structure

  • data class IOUState → This keyword means that we define a data class named “IOUState.”
  • val data → variable declaration. The name of the field is data
  • String = “data” → this defines the default value of the data variable if nothing is defined.
  • ContractState → Interface

If you made all the changes to the IOUState.kt file according to the test, the IOUState class will look as below.

data class IOUState(val amount: Amount<Currency>,

  val lender: Party,

  val borrower: Party,

  val paid: Amount<Currency> = Amount(0, amount.token),

  override val linearId: UniqueIdentifier = UniqueIdentifier()): LinearState {

  /**

 *  This property holds a list of the nodes which can “use” this state in a valid transaction. In this case, the

 *  lender or the borrower.

 */

  override val participants: List<Party> get() = listOf(lender, borrower)

  /**

 * Helper methods for when building transactions for settling and transferring IOUs.

 * – [pay] adds an amount to the paid property. It does no validation.

 * – [withNewLender] creates a copy of the current state with a newly specified lender. For use when transferring.

 */

  fun pay(amountToPay: Amount<Currency>) = copy(paid = paid.plus(amountToPay))

  fun withNewLender(newLender: Party) = copy(lender = newLender)

}

Now, you should be able to add helper methods according to the comments above.

Corda Contacts

We learned that the States could evolve into new states. Developers can control the on-ledger state’s evolution using contracts. In this section, we will learn about contracts in detail.

As R3 Corda doesn’t depend on broadcasting messages to the whole network, it utilizes a common transaction verification definition for each state type. In this way, the Corda network verifies transaction and gain consensus.

Corda Contracts are implemented via functions. You can think of states as data storage units and contracts is a way to verify the data and its state changes. These verification functions are specific to the state type.

To ensure that states can be verified and are serviceable, each state needs to be referenced with a contract.

Each contract contains two items, including contract code and legal prose. 

To understand it, let’s take a look at the following image.

Corda-tutorial-Contract-state

Source: R3 Corda

As you can see, the contract contains two items, and in the state, there is an IOU contract reference for that code.

Legal Prose: Legal Prose is used as a reference to legal contract for the agreement. It is used to ensure that the existing legal framework and judicial system can be used if a dispute occurs.

Other key features of Corda Contracts include the following

  • Multiple contracts can reference a contract. Also, a contract can contain multiple state types
  • Corda utilizes a verification function to verify a proposed transaction
  • The signature of verification method is as below
  • fun verify(tx: Transaction): Unit
  • The verification method verifies the transaction by executing all constraints. If the constraint fails, then an exception will be thrown; otherwise, the transaction will be deemed valid.
  • The verification function is deterministic.

The template also contains the code for the Corda contracts. You should also approach the Corda contracts similar to the states. It would help if you went to the Corda test units and then add the required code in the Contract Class.

Corda Transactions

Our next stop is Corda Transactions. It is transactions that make state changes possible.

So, what is a transaction? It can be best defined as a container that contains input state references(0 or more). They also produce states(0 or more).

Transactions enable the network to perform optimally by making it possible for multiple peers to interact with each other in real-time. It makes it possible for two parties to make an atomic swap without hampering other peer activities. Also, transactions are atomic in nature. This means a transaction can never be reduced or divided.

There are also three types of transactions: Insurances, Updates, and Exits. Each one has their own agenda. Let’s list them below.

  • Insurance → Creates new states
  • Updates → Change state properties
  • Exits → Remove states from the ledger

If you want to read more about Corda transactions, check out their official documentation page here.

Also, go through the template and complete the transaction coding by following a test-driven approach.

Corda Flows

Corda Flows are the steps required for peers to carry out(execute) a successful transaction.

The flow determines the CorDapp core business logic. It is responsible for making sure the transactions are executed in the correct order and correct parameters. As transactions are complex in nature, flows are needed to ensure that everything works as intended.

Flow control ensures when to communicate, what to communicate, and with whom to communicate.

This is the complete opposite of other distributed ledger technologies that focus on broadcasting the transaction details to the whole network. Corda thrives on point-to-point transactions where the transactions need to have two important parameters, sender and receiver.

Key things that you should know about flows

  • Flows can complete within seconds or can take multiple days to complete
  • They are completely autonomous
  • Flows sleep before they find a wait for a response
  • Flows follow Flow Framework, which provides a mechanism for developers to ensure proper state machines development.
  • Flows can be both paired and one-sided
  • Developers can also code sub-flows to make code simpler

Read more about flows here

Consensus

Out of all the exciting things that we have discussed until now, the thing that Corda special is how it achieves consensus. Corda has a unique approach and hence makes it viable for businesses out there.

First of all, Corda utilizes two consensus methods. It includes the following

  • Uniqueness consensus → This consensus method ensures that no conflicts ever happen on the ledger
  • Verification consensus → The verification consensus handles the transactions and ensures that no conflicts happen as per the contract code. It facilitates the evolution of a shared state within the network.

Key things that you should about Corda Consensus:

  • Unique consensus ensures that the ledger updates are always unique. Also, it makes sure the ledger is valid when the update is being made.
  • The verification consensus ensures that the update is valid.

If you want to read about consensus, then check out the following articles

Corda Node

The last section of Corda that we are going to discuss here is the Corda Node. Corda Node is a high-level view of the Corda.

So, what exactly is the Corda node?

A Corda node is an entity that runs in Java Virtual Machine. It involves the following

  • It offers storage services and vaults
  • SQL DB backs the vaults and storage services
  • Dedicated folders are used to store attachments
  • Offers custom functionality and built-in extensions known as CorDapps
  • Offers RPC client framework

This is what the Corda node looks like.

Corda-tutorial-Corda-node

Source: Corda documentation

As you can see, it contains all the necessary aspects to run the Corda network. They interact with flow and provide custom CorDapp functionality.

Conclusion

This leads us to the end of our Corda tutorial. Here, we looked at the Corda and tried to see an IoU CorDapp. We discussed its different sections by using the template training available on the Corda GitHub repository.

Our focus is to get started with Corda without trying to explain everything that Corda offers. For that, you can always use the Corda documentation. R3 Corda has also developed an exciting development tutorial on Udemy. You should surely check it out if you want to need a complete development guide.

So, what do you think about R3 Corda? Is it technically strong enough to stand in the market? Comment below and let us know your opinion.


About Author

Nitish holds a BSc in computer engineering. He is a blockchain enthusiast and in spare time likes to read about the moon. His articles have published on Dzone, InfoWorld, and Hongkiat.

Leave A Reply