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

Managing Data Storage with Blockchain and BigchainDB

Feb 23, 2025 am 08:52 AM

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.

1

2

3

pip install bigchaindb

bigchaindb configure

bigchaindb show-config

Copy after login

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

1

2

3

from bigchaindb import Bigchain

b = Bigchain()

print(b)

Copy after login

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:

1

python app.py

Copy after login

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

Add the following:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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)

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

{

  "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

    }

  }

}

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

...

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)

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

{

  "id": "",

  "signature": "",

  "transaction": {

    "current_owner": "",

    "data": {

      "hash": "",

      "payload": null

    },

  "input": null,

  "new_owner": "",

  "operation": "",

    "timestamp": ""

  }

}

Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

How Much Python Can You Learn in 2 Hours? How Much Python Can You Learn in 2 Hours? Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles