How Do Dragonchain Smart Contracts Work?

Before I jump hard into the actual implementation of our first Dragonchain smart contracts, it’ll be useful to have some idea of how they’re designed to work in the first place.

(We won’t spend much time on it, promise; I’d much rather be deploying code than talking theory, but in this case it’s important as I hit a number of mental roadblocks on my way to having at least some understanding.)

Docker Containers, or, “Where the Magic Happens”

As I mentioned briefly in the introductory post for this project, Dragonchain smart contracts live inside Docker containers. This is incredibly powerful.

I’ll go over more of the details in one of the next posts in this series, but for those who aren’t aware (I wasn’t until recently), Docker containers are self-contained packages of software that can be written in virtually any programming language.

They work kinda like “mini virtual machines” – not a full fledged standalone OS installation, and not a full OS running inside a virtual machine like you’d run in VirtualBox or VMware, but something even less resource intensive that can be easily deployed and maintained. It’s pretty brilliant.

Since I don’t know much more than that about Docker, let’s move on.

The fact that we can write any Docker-compatible programming language for our smart contracts is incredibly powerful. We don’t need to understand virtually anything at all about blockchain development to start taking advantage of the power of a fully realized blockchain network solution.

Now let’s get into some technical details.

Smart Contracts vs Transactions

Let’s assume we’ve already written our smart contract and deployed it to a Dragonchain level 1 business node. How do we actually invoke our smart contract and make it do work?

Before we get into that, let’s talk very briefly about the notion of transactions on a Dragonchain.

One of the beauties of Dragonchain is that you can put virtually anything you want into the “payload” for a transaction that gets recorded on your blockchain (and, by the way, Dragonchain transactions and blocks are represented as simple JSON objects).

The payloads we create might be a record of sending a currency from one address to another on that blockchain (or on other blockchains), it could record the addition of a new employee to our company, it could snapshot a persons ID information or latest medical test results, etc.

We can also add various transaction types to our business node, making them indexable and useful for multi-function systems.

A smart contract, in fact, in terms of how we invoke it, is just another transaction type (with some obvious extra magic involved), and the methods and arguments we use to invoke our smart contracts are just part of the payload.

Let’s say we have a smart contract called “calculator” that has a method called “addnumbers” which takes two arguments, adds them together, and spits out the result.

The transaction we create to invoke the smart contract might look something like this:

'transactionType': 'calculator',
'payload': {
    'method': 'addnumbers',
    'parameters': {
      'numA': 5,
      'numB': 3
    }
}

And the response transaction from the smart contract might look like this:

'transactionType': 'calculator',
'payload': {    
    'response': {
      'sum': 8
    }
}

Pretty simple. But did you notice I said “response transaction?” Let’s talk about the smart contract invocation “flow.”

When you invoke a smart contract on your Dragonchain, it will happen in three basic steps:

  1. You’ll create an “invocation request” transaction that names the smart contract it’s calling and passes any arguments required as the “payload” of the transaction
  2. The smart contract will be looked up by the node, passed the required info, and left to do its thing (your code) and then output any response that’s required.
  3. The smart contract’s output will be used to create a NEW transaction which references the invocation request transaction from step #1 and includes the response as its payload.

Pretty nifty, eh?

And with that, I think I’ll leave off on the theory (for now). In the next post, we’ll learn how to use the DCTL command line tool to create transactions, then how to use a simple block explorer to look them up easily.

Until next time,
John

Leave a comment

Your email address will not be published. Required fields are marked *