一步步解决问题,简单写个链,

const SHA256 = require("crypto-js/sha256");class Block {constructor(index, transactions, timestamp, previousHash) {this.index = index;this.transactions = transactions;this.timestamp = timestamp;this.previousHash = previousHash;this.hash = this.calculateHash();this.nonce = 0; //工作量证明}calculateHash() {return SHA256(this.index +this.previousHash +this.timestamp +JSON.stringify(this.transactions) +this.nonce).toString();}mineBlock(difficulty) {while (this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {this.nonce++;this.hash = this.calculateHash();}}
}class Blockchain {constructor() {this.index = 0;this.difficulty = 2;this.pendingTransactions = [];this.miningReward = 200;this.chain = [this.createGenesisBlock()];}createGenesisBlock() {let firstBlock = new Block(this.index++, "创世love", new Date(), "我爱你");firstBlock.mineBlock(this.difficulty);return firstBlock;}getLatestBlock() {return this.chain[this.chain.length - 1];}createTransaction(transactions) {// 这里应该有一些校验!// 推入待处理交易数组this.pendingTransactions.push(transactions);}isChainValid() {for (let i = 1; i < this.chain.length; i++) {const currentBlock = this.chain[i];const previousBlock = this.chain[i - 1];if (currentBlock.hash !== currentBlock.calculateHash()) {return false;}if (currentBlock.previousHash !== previousBlock.hash) {return false;}}return true;}minePendingTransactions(miningRewardAddress) {// 用所有待交易来创建新的区块并且开挖..let block = new Block(this.index++,this.pendingTransactions,new Date(),this.getLatestBlock().hash);block.mineBlock(this.difficulty);// 将新挖的看矿加入到链上this.chain.push(block);// 重置待处理交易列表并且发送奖励this.pendingTransactions = [new Transaction(null, miningRewardAddress, this.miningReward),];}getBalanceOfAddress(address) {let balance = 0; // you start at zero!// 遍历每个区块以及每个区块内的交易for (const block of this.chain) {for (const trans of block.transactions) {// 如果地址是发起方 -> 减少余额if (trans.fromAddress === address) {balance -= trans.amount;}// 如果地址是接收方 -> 增加余额if (trans.toAddress === address) {balance += trans.amount;}}}return balance;}
}// 交易信息
class Transaction {constructor(fromAddress, toAddress, amount) {this.fromAddress = fromAddress;this.toAddress = toAddress;this.amount = amount;}
}let chain1 = new Blockchain();chain1.createTransaction(new Transaction("anni", "zhanguo", 50));
chain1.createTransaction(new Transaction("anni", "zhanguo", 60));
chain1.createTransaction(new Transaction("anni", "zhanguo", 100));chain1.minePendingTransactions("anni");chain1.createTransaction(new Transaction("anni", "zhanguo", 50));
chain1.createTransaction(new Transaction("anni", "zhanguo", 60));
chain1.createTransaction(new Transaction("anni", "zhanguo", 100));chain1.minePendingTransactions("anni");chain1.minePendingTransactions("anni");console.log(chain1.chain,chain1.getBalanceOfAddress("anni"),chain1.getBalanceOfAddress("zhanguo")
);

js 实现简单区块链相关推荐

  1. vtk删除一个actor_如何构建一个基于actor的简单区块链

    vtk删除一个actor Scalachain is a blockchain built using the Scala programming language and the actor mod ...

  2. python模拟一个简单的取款机,python简单区块链模拟详解

    最近学习了一点python,那就试着做一做简单的编程练习. 首先是这个编程的指导图,如下: 对的,类似一个简单区块链的模拟. 代码如下: class DaDaBlockCoin: #index 索引, ...

  3. 【区块链】Go 实现简单区块链

    本文主要利用 Go 语言对区块链模型进行了简单的实现,通过 GoLand 创建链式结构和一个简单的 http server,对外暴露读写接口,运行 rpc 并以地址访问形式向区块链发送数据和读取数据. ...

  4. python实现简单区块链

    python实现简单区块链 import hashlib import json from time import time from typing import Any, Dict, List, O ...

  5. 区块链python实现_最简单区块链的python实现

    简单的记录下最近自己在学习区块链的笔记,随着比特币的价格越来越高,区块链的概念也越来越火.我简单的把区块链理解成存储数据的数据库,由一个个区块作为容器存储数据,通过hash值相互连接,类似链表结构. ...

  6. python 区块链_Python 模拟简单区块链

    另外还要说明一下,暑假指导老师让我们做一些关于区块链的应用.这里只是涉及极其简单的模拟,主要是记录这些天自己学习的知识. 什么是区块链? 下面简单说一下区块链是什么,做个比喻,区块就像一个人,区块链好 ...

  7. java实现简单区块链毕业设计

    技术:jdk1.8 + maven3.0.5 http://qklbishe.com/ 概述 区块链是分布式数据存储.点对点传输.共识机制.加密算法等计算机技术的新型应用模式.所谓共识机制是区块链系统 ...

  8. python实现简单区块链结构

    区块链 比特币从诞生到现在已经10年了,最近接触到了区块链相关的技术,为了揭开其背后的神秘面纱,我就从头开始构建一个简单的区块链. 文章目录 区块链 一.比特币内部结构 二.实现的比特币结构 三.代码 ...

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

    JavaScript开发区块链只需200行代码 用JavaScript开发实现一个简单区块链.通过这一开发过程,你将理解区块链技术是什么:区块链就是一个分布式数据库,存储结构是一个不断增长的链表,链表 ...

最新文章

  1. 有了这个科研思路,高水平SCI就在手边!
  2. 开源Android容器化框架Atlas开发者指南
  3. 重写 View 的 Touch 方法,实现一个酷炫的九宫格图片
  4. 采用流水线技术实现8位加法器
  5. phpAdmin修改密码后拒绝访问
  6. Cygwin 下载极速源推荐
  7. href up test.php,test.php
  8. latex生成的pdf论文在打印时页码混乱的解决方法
  9. Proteus仿真之工业顺序控制实验
  10. Tecplot 10 将输入的多个plt文件做成动画 :
  11. 《梁宁产品思维30讲》
  12. 半导体的光学性质和光电与发光现象
  13. 小菜openstack nova 源码学习之 evacuate
  14. 判定两颗二叉树是否相同
  15. 网易云歌单添加到php,给自己的网站添加网易云音乐歌单吧^ ^
  16. GBase 8a 数据查询性能
  17. 计算机按硬件组合及用途分为,计算机基础.doc
  18. 【嵌入式开发-AD19】六文搞定Altium Designer-第一章:AD介绍及原理图库的创建
  19. 360篡改了我的microsoft edge,卸载了也没用!!!
  20. python自动演奏Freepiano【双手合奏】

热门文章

  1. x64dbg 自动化控制插件
  2. 服务器报错 http error 503.the service is unavailable怎么解决
  3. 【报告分享】2021年中国新能源汽车行业洞察-Mob研究院(附下载)
  4. Onenote无法登录报错显示0xE000078D解决方法
  5. BeanUtils.populate的用法
  6. 幻立方解法之素数3阶幻立方
  7. linux卸载phpstudy_centos
  8. CodeForces 868C Qualification Rounds
  9. ads1278_24位高性能模数转换器ADS1274/ADS1278及其应用
  10. 【交易架构day4】京东到家交易系统的演进之路