简体   繁体   中英

Where do decentralized applications (blockchain) store their data?

As the rise of crypto craze, I have been reading up and knew that all crypto database is based on something called blockchain. I know what a blockchain is and how a it works but where and how do all these decentralized data is store?

不同的区块链实现可能在每个节点需要多少数据才能在网络上处于活动状态方面有所不同,但在最常见/最简单的情况下,区块链网络的每个节点都存储区块链的完整副本 - 以及其中的所有数据 - 本地。

The decentralized word itself explains that data not stored at some specific place it stores data at various places and for blockchain, those places are all the nodes In the network, those nodes are nothing but it's your computer if you are connected to a network. Everyone has the copy of data locally.

You can think of a blockchain as a data structure, which can relate to a database which ONLY have append-only architecture. Since its a ledger like system, the blockchain can record things, but you can not use methods like, UPDATE or Delete for those records. All you can do is add a new record and make the previous record obsolete.

As a simple example,

Lets day the blockchain is designed with blocks like

Data 01 + Data 02 + Data 03 + .... + Data n

Data 01 - Have some value for variable x =20

if you want to change this variable, you add a new record saying

Data 101 - Now the variable x=10 or

Data 101 - (x-10) just happned.

Dara Storage: All data that goes in to the blockchain (after the consensus from the nodes of the network) will be added to the blockchain and then distributed among ALL Nodes.

When you think of using blockchain as a database, you have to find if its suitable for your solution and fins the best architecture that works with the technology behind blockchain.

These are two articles I wrote which will give an idea on how it actually works. hope this helps,

Blockchain — a simple explanation (linkedin) / (Medium)

How the actual blockchain works : what goes in to a block? is it incorruptible? - a simple explanation (Linkedin) / (Medium)

This is a cool question. I think you mean where to store data in the blockchain. The blockchain uses key-value to store data. the key is hash code. Like when a transaction adding to a new block. it'll be sha256(sha256(translation raw)) twice that is a key(don't forget a Block contains thousands of transactions) the new block will be store into LevelDB ( http://leveldb.org ). The LeveDB is a light-weight, single-purpose library with bindings to many platforms. It's an embedded database, so blockchain keeps data using levelDB.

The bitcoin source for you : https://github.com/bitcoin/bitcoin

https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-0.8.0.md

LevelDB, a fast, open-source, non-relational database from Google, is
now used to store transactions and block indices.  LevelDB works much better
on machines with slow I/O and is faster in general. Berkeley DB is now only
used for the wallet.dat file (public and private wallet keys and transactions
relevant to you).

I create a project to explain it.

https://github.com/tomgtqq/Private-Blockchain-Leveldb

/* ===== Persist data with LevelDB ==================
|  Learn more: level: https://github.com/Level/level |
/===================================================*/

const level = require('level');
const chainDB = './chaindata';

class LevelSandbox {

    constructor() {
        this.db = level(chainDB);
    }

    // Get data from levelDB with key (Promise)
    getLevelDBData(key){
        let self = this;
        return new Promise(function(resolve, reject) {
            self.db.get(key, (err, value) => {
                if(err){
                    if (err.type == 'NotFoundError') {
                        resolve(undefined);  //@cool if NotFoundError 
                    }else {
                        reject(err);
                    }
                }else {
                    resolve(value);
                }       
            });
        });
    }

    // Get All Blocks in DB
    getAllBlocks(){
        let self = this;
        let dataArray = [];
        return new Promise(function(resolve, reject){
            self.db.createReadStream().on('data', function(chunk){
                dataArray.push(chunk);
            }).on('error', function(err){
                reject(error);
            }).on('close', function(){
                resolve(JSON.stringify(dataArray).toString());
            });
        });
    }

    // Add data to levelDB with key and value (Promise)
    addLevelDBData(key, value) {
        let self = this;
        return new Promise(function(resolve, reject) {
            self.db.put(key, value, function(err) {
                if (err) {
                    reject(err);
                }
                resolve(value);
            });
        });
    }

    // Method that return the height
    getBlocksCount() {
        let self = this;
        return new Promise(function(resolve, reject){
            let count = 0;
            self.db.createReadStream().on('data', function(chunk){
                count++;
            }).on('error', function(err){
                reject(error);
            }).on('close', function(){
                resolve(count - 1);
            });
        });
    }


}

module.exports.LevelSandbox = LevelSandbox;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM