Home >Backend Development >Python Tutorial >Managing Data Storage with Blockchain and BigchainDB

Managing Data Storage with Blockchain and BigchainDB

Christopher Nolan
Christopher NolanOriginal
2025-02-23 08:52:11175browse

Managing Data Storage with Blockchain and BigchainDB

Core points

  • Ascribe uses the Bitcoin blockchain to record the unique identification of digital artworks and combines NoSQL database (RethinkDB) with the blockchain layer to create BigchainDB. This combination enhances control, asset tracking and security levels, and is particularly attractive to NoSQL database users.
  • BigchainDB claims to be completely decentralized due to its blockchain layer. It also adds transaction support, a feature that is often missing in NoSQL databases. This support ensures that database changes have occurred when writing to the underlying NoSQL database through the blockchain layer.
  • BigChainDB can fill in missing gaps in current NoSQL and distributed databases, which may provide effective business or use cases. For blockchain enthusiasts, it accomplishes the challenge of fully decentralized application stacks, which may change how applications are developed, deployed, and maintained.

Although the future of Bitcoin is unclear, the underlying technology on which it depends - blockchain - has completely changed many industries and projects, and more applications are about to emerge.

Ascribe is a compelling startup that uses the Bitcoin blockchain to record a limited number of unique identifiers for digital artworks. Therefore, due to this limited number of “copies”, they are traceable, accountable and (hopefully) higher value.

Ascribe encountered technical problems when using this method, which mainly stemmed from the Bitcoin blockchain itself. It is slow to write everything to it, costly (currently 80 cents per time) and limited daily entries and total write capacity. It is also contrary to typical scalable database techniques, adding nodes does not improve performance, and there is no real query language. This makes business expansion based on Bitcoin blockchain a challenge.

However, the blockchain concept is a powerful concept, and its use and legitimacy have increased over the past few years, and even large banks have announced that they are developing technologies inspired by the concept.

Ascribe decided to combine the advantages of the two, adopting a proven NoSQL database (RethinkDB) and adding a blockchain layer to it to enhance control, asset tracking and additional security levels.

The combination of this technology is particularly attractive to NoSQL database users, as few NoSQL database support has traditionally helped ensure that the database changes have occurred "transactions". BigchainDB adds transaction support by writing to the underlying NoSQL database through the blockchain layer.

BigChainDB also claims to be completely decentralized due to the blockchain layer. While many distributed NoSQL databases claim to be decentralized, there are often pseudo-master/slave settings.

Installing BigChainDB and its dependencies

There are several ways to install BigChainDB. First I tried Docker image, but encountered some connection issues and found that the Python package is the most reliable.

  1. Install RethinkDB, and for other Mac users, there is also a Homebrew package available.
  2. Install Python 3.4.
  3. Install BigChainDB with Pip – sudo pip install bigchaindb
  4. Start RethinkDB with rethinkdb
  5. Start BigChainDB with bigchaindb start, it will also configure some content for you.
  6. Open BigChainDB (actually RethinkDB UI) at http://SERVER_IP:58080/.

Simple Example - Message Allocation and Tracking

One of the main use cases of BigchainDB (and why Ascribe created it) is to track assets, so let's create a simple example in Python. First run the following command in your terminal.

<code class="language-bash">pip install bigchaindb
bigchaindb configure
bigchaindb show-config</code>

Create a new file, app.py and add the following:

<code class="language-python">from bigchaindb import Bigchain
b = Bigchain()
print(b)</code>

This will import the bigchaindb library, create a new object and connect to it using the settings file you just created.

Then run the Python application:

<code class="language-bash">python app.py</code>

You should see something like <bigchaindb.core.bigchain at></bigchaindb.core.bigchain>, which tells us everything is fine.

Add the following:

<code class="language-python">from bigchaindb import Bigchain
import time

b = Bigchain()

spuser_priv, spuser_pub = b.generate_keys()
print("User Created")

digital_asset_payload = {'msg': 'This is my special message just for you'}

tx = b.create_transaction(b.me, spuser_pub, None, 'CREATE', payload=digital_asset_payload)
print("Transaction Written")

tx_signed = b.sign_transaction(tx, b.me_private)
b.write_transaction(tx_signed)
print ("Transaction Written to BC, now waiting")

time.sleep(10)

tx_retrieved = b.get_transaction(tx_signed['id'])
print(tx_retrieved)</code>

This will create a user and associated key to access the database – remember the additional security level. Then create a payload for writing to the database, allocate the required key, and write it.

How many seconds does a new transaction take to pass from the blockchain layer to the database. The code waits for ten seconds, and then retrieves and prints the record. You should see something like the following:

<code class="language-json">{
  "signature": '304502205...',
  "id": "0f442bcf4a42...",
  "transaction": {
      "timestamp": "1457104938.430521",
      "data": {
        "hash": "b32779e57...",
        "payload": {
          "msg": "This is my special message just for you"
        }
      },
      "operation": "CREATE",
      "current_owner": "hFJKYk2...",
      "new_owner": "26pdiQTTx...", 
      "input": None
    }
  }
}</code>

You now have a special message that you want one person to be able to access:

<code class="language-python">...
print("Now to transfer")

spuser2_priv, spuser2_pub = b.generate_keys()
print("Second User Created")

tx_transfer = b.create_transaction(spuser_pub, spuser2_pub, tx_retrieved['id'], 'TRANSFER')
print("Transfer Created")

tx_transfer_signed = b.sign_transaction(tx_transfer, spuser_priv)
b.write_transaction(tx_transfer_signed)
print ("Transaction Written to BC, now waiting")

time.sleep(15)

tx_transfer_retrieved = b.get_transaction(tx_transfer_signed['id'])
print("Transferred")
print(tx_transfer_retrieved)</code>

This will create a second user, then get the transaction ID of the special message and transfer it to the second user. BigChainDB's blockchain layer will prevent users and your code from performing the same operations twice. If you try to run the above code again, a double spending exception is thrown.

This example shows a small part of how BigChainDB is added to RethinkDB, and a complete list is available here.

HTTP endpoint

Currently, the only client library available for BigChainDB is Python, and there may be more libraries, but at the same time, a limited HTTP endpoint can be used to query existing transactions:

https://www.php.cn/link/6eea81fa0417b0068e614074225a9daf

Or write a new transaction using the following method:

https://www.php.cn/link/f8b64946ebc86a5e23e1605a2943210c

Add the following payload, where the operation can be changed to suit different types of transactions that can be written:

<code class="language-json">{
  "id": "",
  "signature": "",
  "transaction": {
    "current_owner": "",
    "data": {
      "hash": "",
      "payload": null
    },
  "input": null,
  "new_owner": "",
  "operation": "",
    "timestamp": ""
  }
}</code>

Components of the Decentralized Future

Ignore the origin of its blockchain for the time being, BigChainDB provides a large number of features missing in current NoSQL and distributed databases. This fact alone may be the reason to try it and may provide a valid business/use case.

For blockchain enthusiasts among you, it also completes the puzzle of a complete decentralized application stack. In theory, there is now Ethereum for applications, IPFS for file systems, and BigChainDB for data storage. These components set the stage for very different ways of developing, deploying and maintaining applications, creating a fascinating future, and I hope to hear your comments on this in the comments below.

(The FAQ part is omitted here because this part is just a simple summary and retelling of the content of the article and does not fall into the category of pseudo-originality.)

The above is the detailed content of Managing Data Storage with Blockchain and BigchainDB. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn