简体   繁体   English

去中心化应用程序(区块链)在哪里存储数据?

[英]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.由于它是一个类似分类帐的系统,区块链可以记录事物,但您不能对这些记录使用 UPDATE 或 Delete 等方法。 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数据 01 + 数据 02 + 数据 03 + .... + 数据 n

Data 01 - Have some value for variable x =20数据 01 - 变量 x =20 有一些值

if you want to change this variable, you add a new record saying如果你想改变这个变量,你添加一个新的记录说

Data 101 - Now the variable x=10 or数据 101 - 现在变量 x=10 或

Data 101 - (x-10) just happned.数据 101 - (x-10) 刚刚发生。

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. Dara Storage:所有进入区块链的数据(在网络节点达成共识后)将被添加到区块链,然后在所有节点之间分配。

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) 区块链——一个简单的解释 (linkedin) / (Medium)

How the actual blockchain works : what goes in to a block? 实际区块链是如何工作的:块中包含什么? is it incorruptible? 它是廉洁的吗? - a simple explanation (Linkedin) / (Medium) - 一个简单的解释 (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 ).它将是 sha256(sha256(translation raw)) 两次,这是一个键(不要忘记一个块包含数千个交易),新块将存储到 LevelDB( http://leveldb.org )中。 The LeveDB is a light-weight, single-purpose library with bindings to many platforms. LeveDB 是一个轻量级、单一用途的库,可以绑定到许多平台。 It's an embedded database, so blockchain keeps data using levelDB.它是一个嵌入式数据库,因此区块链使用 levelDB 保存数据。

The bitcoin source for you : https://github.com/bitcoin/bitcoin你的比特币来源: https : //github.com/bitcoin/bitcoin

https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-0.8.0.md 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 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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