区块链c端应用小程序_区块链如何真正起作用? 我建立了一个应用程序向您展示。...

区块链c端应用小程序

by Sean Han

通过肖恩·韩

区块链如何真正起作用? 我建立了一个应用程序向您展示。 (How does blockchain really work? I built an app to show you.)

According to Wikipedia, a blockchain is:

根据维基百科,一个区块链是:

A distributed database that is used to maintain a continuously growing list of records, called blocks.

一种分布式数据库,用于维护不断增长的记录列表(称为块)

That sounds nice, but how does it work?

听起来不错,但如何运作?

To illustrate a blockchain, we will use an open source command-line interface called Blockchain CLI.

为了说明区块链,我们将使用一个名为Blockchain CLI的开源命令行界面。

I also built a browser-based version of this here.

我还在这里构建了基于浏览器的版本

安装命令行界面版本 (Installing the Command-Line Interface version)

If you haven’t already, install Node.js.

如果尚未安装Node.js。

Then run the following in your terminal:

然后在终端中运行以下命令:

# Clone this repository
$ git clone https://github.com/seanseany/blockchain-cli

# Go into the repository
$ cd blockchain-cli

# Install dependencies
$ npm install

# Run the app
$ npm start

You should see ? Welcome to Blockchain CLI!and a blockchain → prompt ready to take commands.

你应该看到? Welcome to Blockchain CLI!a ? Welcome to Blockchain CLI!a和ab lockchain →提示准备好接受命令。

块是什么样的? (What does a block look like?)

To see your current blockchain, enter blockchain or bc into the command prompt. You should see a block like the image below.

要查看您当前的区块链,请在命令提示符下输入blockchainbc 。 您应该看到如下图所示的块。

  • Index (Block #): Which block is it? (Genesis block has index 0)

    索引(块号):它是哪个块? (创世记块的索引为0)

  • Hash: Is the block valid?

    哈希:该区块有效吗?

  • Previous Hash: Is the previous block valid?

    上一个哈希:上一个区块有效吗?

  • Timestamp: When was the block added?

    时间戳记:什么时候添加了该区块?

  • Data: What information is stored on the block?

    数据:块上存储了什么信息?

  • Nonce: How many iterations did we go through before we found a valid block?

    Nonce:在找到有效块之前,我们经历了几次迭代?

创世块 (Genesis Block)

Every blockchain will start with the? Genesis Block. As you will see later, each block on the blockchain is dependent on the previous block. So, the Genesis block is needed to mine our first block.

每个区块链都将以? Genesis Block. ? Genesis Block. 如您将在后面看到的,区块链上的每个块都依赖于前一个块。 因此,需要Genesis区块来挖掘我们的第一个区块。

开采新区块时会发生什么? (What happens when a new block is mined?)

Let’s mine our first block. Enter mine freeCodeCamp♥︎ into the prompt.

让我们开采我们的第一个街区。 在提示中输入mine freeCodeCamp♥︎

The blockchain looks at the latest block on the blockchain for the index and previous hash. In this case Genesis block is the latest block.

区块链将查看区块链上的最新区块以获取索引和先前的哈希值。 在这种情况下,Genesis块是最新的块。

  • Index: o+1 = 1

    索引: o + 1 = 1

  • Previous Hash: 0000018035a828da0…

    上一个哈希: 0000018035a828da0…

  • Timestamp: When the block is added

    时间戳:添加块时

  • Data: freeCodeCamp❤

    数据: freeCodeCamp❤

  • Hash: ??

    哈希:

  • Nonce: ??

    Nonce: ??

哈希是如何计算的? (How is the hash calculated?)

A hash value is a numeric value of a fixed length that uniquely identifies data.

哈希值是唯一标识数据的固定长度的数字

The hash is calculated by taking the index, previous block hash, timestamp, block data, and nonce as input.

通过将索引,先前的块哈希,时间戳,块数据和随机数作为输入来计算哈希。

CryptoJS.SHA256(index + previousHash + timestamp + data + nonce)

The SHA256 algorithm will calculate a unique hash, given those inputs. The same inputs will always return the same hash.

给定这些输入,SHA256算法将计算唯一的哈希。 相同的输入将始终返回相同的哈希。

您是否注意到区块哈希中的四个前导0? (Did you notice the four leading 0’s in the block hash?)

The four leading 0’s is a minimum requirement for a valid hash. The number of leading 0’s required is called difficulty.

有效散列的最低要求是四个前导0。 所需的前导0的数字称为难度

function isValidHashDifficulty(hash, difficulty) {
  for (var i = 0, b = hash.length; i < b; i ++) {
      if (hash[i] !== '0') {
          break;
      }
  }
  return i >= difficulty;
}

This is also known as the Proof-of-Work system.

这也称为工作量证明系统

什么是随机数? (What’s a nonce?)

A nonce is a number used to find a valid hash.

随机数是用于查找有效哈希的数字。

let nonce = 0;
let hash;
let input;

while(!isValidHashDifficulty(hash)) {     
  nonce = nonce + 1;
  input = index + previousHash + timestamp + data + nonce;
  hash = CryptoJS.SHA256(input)
}

The nonce iterates until the hash is valid. In our case, a valid hash has at least four leading 0’s. The process of finding a nonce that corresponds to a valid hash is mining.

随机数迭代直到哈希有效为止。 在我们的情况下,有效哈希至少具有四个前导0。 查找与有效哈希对应的随机数的过程是采矿

As the difficulty increases, the number of possible valid hashes decreases. With less possible valid hashes, it takes more processing power to find a valid hash.

随着难度的增加 ,有效哈希的数量会减少 有效哈希越少,查找有效哈希就需要更多处理能力。

为什么这么重要? (Why does this matter?)

It matters because it keeps the blockchain immutable.

这很重要,因为它使区块链保持不变。

If we have the following blockchain A → B → C, and someone wants to change data on Block A. This is what happens:

如果我们有以下区块链A→B→C,并且有人想要更改区块A上的数据,则会发生这种情况:

  1. Data changes on Block A.

    数据在A块上更改。
  2. Block A’s hash changes because data is used to calculate the hash.

    块A的哈希值发生变化,因为数据用于计算哈希值。
  3. Block A becomes invalid because its hash no longer has four leading 0’s.

    块A无效,因为其哈希不再具有四个前导0。
  4. Block B’s hash changes because Block A’s hash was used to calculate Block B’s hash.

    块B的哈希值发生变化,因为使用了块A的哈希值来计算块B的哈希值。
  5. Block B becomes invalid because its hash no longer has four leading 0's.

    块B无效,因为其哈希不再具有四个前导0。
  6. Block C’s hash changes because Block B’s hash was used to calculate Block C’s hash.

    块C的哈希值发生变化,因为使用了块B的哈希值来计算块C的哈希值。
  7. Block C becomes invalid because its hash no longer has four leading 0's.

    块C无效,因为其哈希不再具有四个前导0。

The only way to mutate a block would be to mine the block again, and all the blocks after. Since new blocks are always being added, it’s nearly impossible to mutate the blockchain.

变异方块的唯一方法是再次挖掘该方块,然后再挖掘所有方块。 由于总是会添加新的块,因此几乎不可能对区块链进行变异。

I hope this tutorial was helpful for you!

希望本教程对您有所帮助!

If you would like to checkout a web version of the demo, head on over to http://blockchaindemo.io

如果您想查看该演示的网络版本,请转到http://blockchaindemo.io

翻译自: https://www.freecodecamp.org/news/how-does-blockchain-really-work-i-built-an-app-to-show-you-6b70cd4caf7d/

区块链c端应用小程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值