Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content

JavaScript开发区块链只需200行代码

空港训港j
23 0 0
使用JavaScript实现简单的开发一个区块链。通过javascript这一开发区块链的实现过程,你将会真正理解区块链是什么:区块链就是一个分布式数据库,存储结构是一个不断增长的链表,链表中包含着许多有序的记录。

然而,在通常情况下,当我们谈到区块链的时候也会谈起使用区块链来解决的问题,这两者很容易混淆。像流行的比特币和以太坊这样基于区块链的项目就是这样。“区块链”这个术语通常和像交易、智能合约、加密货币这样的概念紧紧联系在一起。

这就令理解区块链变得不必要得复杂起来,特别是当你想理解源码的时候。下面我将通过 200 行 JS 实现的超级简单的区块链来帮助大家理解它,我给这段代码起名为 NaiveChain。你可以在[Github](https://github.com/lhartikk/naivechain) 查看更多的技术细节。

块结构

第一个逻辑步骤是决定块结构。为了保证事情尽可能的简单,我们只选择最必要的部分:index(下标)、timestamp(时间戳)、data(数据)、hash(哈希值)和 previous hash(前置哈希值)。

![200行node.js代码让你彻底理解区块链是什么]()

这个块中必须能找到前一个块的哈希值,以此来保证整条链的完整性。
  1. class Block {
  2.     constructor(index, previousHash, timestamp, data, hash) {
  3.         this.index = index;
  4.         this.previousHash = previousHash.toString();
  5.         this.timestamp = timestamp;
  6.         this.data = data;
  7.         this.hash = hash.toString();
  8.     }
  9. }
Copy the Code
BitMere.com is Information release platform,just provides information storage space services.
The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
You have to log in before you can reply Login | 立即注册

Points Rules

Write the first review

空港训港j 小学生
  • Follow

    0

  • Following

    0

  • Articles

    3

Promoted