区块链模拟代码

//data之前区块哈希 
//自己哈希:自算(data + 之前区块哈希)
const sha256=require('crypto-js/sha256')

class Block{
constructor(data,previousHash){
this.data=data
this.previousHash=previousHash
this.hash=this.computerHash()
}
//返回自己计算的哈希值
computerHash(){
return sha256(this.data+this.previousHash).toString()}
}
//const block = new Block('','123')
//console.log(block)

//链
class Chain{
constructor(){
this.chain =[this.bigBang()]
}
//创世区块
bigBang(){
const genesisBlock = new Block('I an genesisBlock','')
return genesisBlock}

//找到最近一个block的哈希
getLatestBlock(){
return this.chain[this.chain.length-1]}
//添加区块到区块链上
addBlockToChain(block){
//找到最近一个block的哈希
newBlock.previousHash = this.getLatestBlock().hash
newBlock.hash = newBlock.computeHash();
this.chain.push(newBlock)}
}

//创建区块链
const luotuoChain = new Chain()

const block1=new Block('10','')
luotuoChain.addBlockToChain(block1)
console.log(luotuoChain)
//验证当前区块链是否合法,通过判断前一个区块的哈希值
//通过计算每个区块的哈希值对比验证当前区块的真实性
//验证当前区块的previousHash != previous区块的hash
validateChain(){
    if (this.chain.length===1){
        if(this.chain[0].hash !== this.chain[0].computeHash() ){
            return false        
        }   
        return true
    }
    for(let i = 1;i <= this.chain.length-1;i++){
        //拿到第二个块往后检查
        const blockToValidate = this.chain[i]
        if(blockToValidate.hash !== blockToValidate.computeHash()){
            console.log("Error!篡改")
            return false        
        }
        const previousBlock = this.chain[i-1]
        if(blockToValidate.previousHash !== previousBlock.hash) {
            console.log("Error!断链")
            return false  
        }
    }
}
//创建区块链
const luotuoChain = new Chain()

const block1=new Block('10','')
luotuoChain.addBlockToChain(block1)
console.log(luotuoChain)
console.log(luotuoChain.validateChain())

//篡改区块
luotuoChain.chain[1].data = "100000"
console.log(luotuoChain.validateChain())
luotuoChain.chain[1].hash=luotuoChain.chain[1].computeHash()

经供参考!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Matlab中实现区块链区块的传播模拟,你可以使用以下示例代码: ```matlab classdef Block properties index % 区块索引 timestamp % 时间戳 data % 区块数据 previousHash % 前一个区块的哈希值 hash % 当前区块的哈希值 end methods function block = Block(index, data, previousHash) block.index = index; block.timestamp = datetime('now'); block.data = data; block.previousHash = previousHash; block.hash = block.calculateHash(); end function hash = calculateHash(block) hashStr = string(block.index) + string(block.timestamp) + string(block.data) + string(block.previousHash); hash = char(md5hash(hashStr)); end end end classdef Blockchain properties chain % 区块链 end methods function blockchain = Blockchain() genesisBlock = Block(0, 'Genesis Block', ''); blockchain.chain = genesisBlock; end function addBlock(blockchain, data) previousBlock = blockchain.chain; newIndex = previousBlock.index + 1; newBlock = Block(newIndex, data, previousBlock.hash); blockchain.chain(newIndex+1) = newBlock; end function isValid(blockchain) for i = 2:length(blockchain.chain) currentBlock = blockchain.chain(i); previousBlock = blockchain.chain(i-1); % 验证当前区块的哈希值是否正确 if ~strcmp(currentBlock.hash, currentBlock.calculateHash()) disp('区块链无效: 错误的哈希值'); return; end % 验证当前区块的前一个哈希值是否与前一个区块的哈希值一致 if ~strcmp(currentBlock.previousHash, previousBlock.hash) disp('区块链无效: 前一个哈希值不一致'); return; end end disp('区块链有效'); end end end % 创建区块链 blockchain = Blockchain(); % 添加区块 blockchain.addBlock('Transaction 1'); blockchain.addBlock('Transaction 2'); blockchain.addBlock('Transaction 3'); % 验证区块链的有效性 blockchain.isValid(); ``` 这个示例代码定义了一个 `Block` 类来表示区块,并定义了一个 `Blockchain` 类来管理区块链。在 `Blockchain` 类中,你可以使用 `addBlock` 方法来添加新的区块,使用 `isValid` 方法来验证区块链的有效性。 你可以根据需要修改代码,并添加其他功能来满足你的需求。请注意,这只是一个简单的示例代码,实际使用时可能需要根据具体需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值