Using BigchainDB: A Database with Blockchain Characteristics

0
1192
blockchain bigchain db

Blockchain is a revolutionary technology that has taken the world by storm. It has a wide range of applications and is being deployed in various fields across industries. BigchainDB is an open source database that emulates the characteristics of blockchain. It can be used to build prototypes of blockchain systems or even be deployed to large scale enterprise products. In this article, we discuss the characteristics of BigchainDB and its use cases. We also see how BigchainDB can be deployed with the help of an example.

It is safe to say that we have an abundance of open source databases to choose from, be it for a hobby project or an enterprise. Whether your project requires a structured database such as MySQL or a non-structured database such as MongoDB, the options are plenty. However, what if you want to implement a blockchain-like concept or design on a database where entities are immutable and hold specific data with regards to the ownership of the entity? Consider, for instance, implementing a non-fungible token (NFT) system but without having the hassle of building an entire blockchain to do so. Perhaps you just want to build a quick proof-of-concept for a blockchain platform.

Why BigchainDB?
The traditional SQL/NoSQL databases are not adept to work with blockchain technologies. SQL databases divide data into rows and columns whereas NoSQL databases use JSON/XML to define their schema. Neither format is compatible with an asset-centric model that blockchain requires.

BigchainDB was launched in 2016 to address this particular need in the blockchain development environment. It was designed to be a simple to implement open source database that could be used to create asset-centric nodes, which could be distributed to a large-scale platform. What makes BigchainDB a better solution is its ability to define assets in the form of immutable nodes containing a unique ID and additional queryable metadata. In 2017, the makers of this database announced the IPDB Foundation with the help of its open source community to handle its governance and create a live public network for deploying BigchainDB projects for real-time users.

The BigchainDB transaction model
The easiest way to understand the BigchainDB transaction model is with the help of an example. Mike is a painter. He would like to sell his painting as an NFT to Sarah. So what steps are required to achieve this? First, Mike will have to register his painting as an immutable asset of which he has ownership. When registering, he will have to share some metadata regarding the painting, such as the name, date of creation, etc. He will then have to upload the NFT publicly for other people to see. We can think of these processes as transactions. The process of creating the NFT can be considered a ‘CREATE’ transaction and uploading the NFT can be considered a ‘SEND’ transaction. Similarly, the process of transferring the ownership of the NFT from Mike to Sarah can be considered a ‘TRANSFER’ transaction. Each transaction requires a certain input, for which we generate a certain output.

So if you were to implement this example using BigchainDB, the process of registering the painting as an asset becomes the ‘CREATE’ transaction. The process of uploading the asset as a node in the network becomes the ‘SEND’ transaction. The process of transferring the ownership from Mike to Sarah becomes the ‘TRANSFER’ transaction.

BigchainDB uses the public key cryptography system for registering users and maintaining the integrity and security of the assets. Each asset has to be signed with the owner’s private key. This provides surety that a transaction for the asset is being made by the owner of the asset and no one else. Additionally, each transaction generates its own unique hash called the transaction ID. Figure 1 illustrates a ‘CREATE’ transaction life cycle and Figure 2 illustrates a ‘TRANSFER’ transaction life cycle.

CREATE transaction in BigchainDB
Figure 1: CREATE transaction in BigchainDB (Courtesy: BigchainDB)

Creating and transferring an asset
Thus far, we have understood how BigchainDB works from a theoretical perspective. Now comes the fun part where we get to test our understanding with actual code. So let us continue with the example we have used in the previous section. Mike wants to register his painting as an asset and transfer its ownership to Sarah.

TRANSFER transaction in BigchainDB
Figure 2: TRANSFER transaction in BigchainDB (Courtesy: BigchainDB)

For starters, we need to decide what language we are going to use. BigchainDB provides drivers for all major programming languages. For the purpose of this example, we shall be using Python.

Step 1: Install the Python driver for BigchainDB.

$ pip3 install bigchaindb_driver

Step 2: Now let’s open up a new Python file and import the necessary modules.

from bigchaindb_driver import BigchainDB
from bigchaindb_driver.crypto import generate_keypair
from time import sleep
from sys import exit

Step 3: Register Mike and Sarah as users by generating their public/private key pair.

mike, sarah = generate_keypair(), generate_keypair()

Step 4: Set up the root URL. If you are running the node on your local device, then it can be localhost.

bdb_root_url = ‘https://localhost:8080’

Step 5: Now let us create the painting as an asset and generate it under Mike’s ownership.

bdb = BigchainDB(bdb_root_url)
painting_asset = {
    ‘data’: {
        ‘painting’: {
            ‘name’: ‘New Mona Lisa’,
            ‘painter’: ‘Mike’
        },
    },
}

painting_asset_metadata = {
    ‘date_started’: ‘10/1/2021’
    ‘date_completed’: ‘12/3/2021’
}
prepared_creation_tx = bdb.transactions.prepare(
    operation=’CREATE’,
    signers=mike.public_key,
    asset=painting_asset,
    metadata=painting_asset_metadata
)

fulfilled_creation_tx = bdb.transactions.fulfill(
    prepared_creation_tx,
    private_keys=mike.private_key
)

Step 6: Finally, we upload the asset as a node into the database.

sent_creation_tx = bdb.transactions.send_commit(fulfilled_creation_tx)

Step 7: Now we will prepare the transfer of the asset ownership from Mike to Sarah.

txid = fulfilled_creation_tx[‘id’] 
asset_id = txid
transfer_asset = {
    ‘id’: asset_id
}
output_index = 0
output = fulfilled_creation_tx[‘outputs’][output_index]
transfer_input = {
    ‘fulfillment’: output[‘condition’][‘details’],
    ‘fulfills’: {
        ‘output_index’: output_index,
        ‘transaction_id’: fulfilled_creation_tx[‘id’]
    },
    ‘owners_before’: output[‘public_keys’]
}
prepared_transfer_tx = bdb.transactions.prepare(
    operation=’TRANSFER’,
    asset=transfer_asset,
    inputs=transfer_input,
    recipients=sarah.public_key,
)
fulfilled_transfer_tx = bdb.transactions.fulfill(
    prepared_transfer_tx,
    private_keys=mike.private_key,
)

Step 8: Finally, we send the commit to the database to complete the TRANSFER transaction.

sent_transfer_tx = bdb.transactions.send_commit(fulfilled_transfer_tx)

Step 9: Let us confirm that the transactions went through as planned and the ownership has been assigned correctly from Mike to Sarah.

print(“Is Sarah the owner of the painting?”,
    sent_transfer_tx[‘outputs’][0][‘public_keys’][0] == sarah.public_key)
print(“Was Mike the previous owner of the painting?”,
    fulfilled_transfer_tx[‘inputs’][0][‘owners_before’][0] == mike.public_key)

Use cases for BigchainDB
As discussed, BigchainDB treats every node in its database as an independent asset. This opens up room for a wide array of applications for it. Some of those use cases have been listed below.

  1. BigchainDB can be used for registering claims for assets such as vehicles and real-estate properties.
  2. It can be used for modelling and designing scalable NFT platforms.
  3. BigchainDB is great for token creation and distribution due to its support for divisible assets. This means that multiple assets can point to a single asset in BigchainDB, making it ideal for token launches.
  4. BigchainDB can be used as a version control system for volatile and sensitive assets such as documents.
  5. It can be used for storage of time series data, although there are better alternatives available for that purpose.
  6. BigchainDB is excellent for creating state machines, as it is ideal for marking state changes using the accessible metadata feature.

Alternatives to using BigchainDB
As far as emulating the functionality of blockchain technology is concerned, BigchainDB stands one of one for now. Currently, there are no direct alternatives to using BigchainDB in the sense that there are no other databases that provide all the features exactly as this database has to offer. However, there are a few indirect alternatives worth mentioning.

  • First up is Cassandra. While Cassandra is a NoSQL database, it has a few blockchain emulating features that could potentially make it an ideal alternative. It uses distributed computing techniques which makes your data highly secure. There are no single points of failure. Cassandra is also fault-tolerant due to its replicating feature across multiple data centres and has extremely low latency. What’s more, it’s an open source Apache project.
  • ChainifyDB is a proprietary alternative to BigchainDB in that it offers a cloud based blockchain service using which you can secure your local database.
  • Postchain can also be a good alternative if the solution you are looking for is focused more on creating a blockchain rather than a blockchain-like database. Postchain is a framework for creating blockchains that provides deeper integration with SQL databases.

LEAVE A REPLY

Please enter your comment!
Please enter your name here