/**
     * 1. 迷你区块链 --chain
     * 2. 区块链的生成 ,新增 校验
     * 3. 交易
     * 4. 非对称加密
     * 5. 挖矿
     * 6. p2p网络
     */
    // [
    //     //创世区块
    //     {
    //         index: 0, // 索引
    //         timetamp: 时间戳,
    //         data: 区块的具体信息,
    //         prehash: 上个区块的哈希, //哈希0
    //         hash: 当前区块的哈希, //哈希1
    //         nonce: 随机数,
    //     },
    
    //     {
    //         index: 1, //索引
    //         timetamp: 时间戳,
    //         data: 区块的具体信息,
    //         prehash: 上个区块的哈希, //哈希1
    //         hash: 当前区块的哈希, //哈希2
    //         nonce: 随机数,
    //     },
    // ];
    const crypto = require("crypto");
    
    
    //创世区块
    const initBlock ={index: 0,
        timestamp: 112342311214,
        data: 'hello chain',
        prehash: '0',
        nonce: 62264,
        hash: '0000c0255f520a5b0b06f65fcc47483a9c952d5bc70a5f927c66c22347094272'
    }
    
    
    class Blockchain {
    
        constructor() {
            this.blockchain = [
                initBlock
            ];
            this.data = [];
            this.difficulty = 4;
        }
        getLastBlock() {
            return this.blockchain[this.blockchain.length - 1];
        }
        //挖矿
        mine() {
            /**
             * 1.生成新区块
             */
            const newBlock =this.generateNewBlock();
            if(this.isValidBlock(newBlock) && this.isValidChain()){
                this.blockchain.push(newBlock)
            }else {
                console.log('Error,Invalid Block',newBlock)
            }
        }
    
        //生成新区块
        generateNewBlock() {
            let nonce =0;
            const  index = this.blockchain.length;
            const  data = 'hello chain';
            const prehash = this.getLastBlock().hash;
            let timestamp = new Date().getTime();
            let hash = this.computeHash(index, timestamp,data,prehash,nonce);
            while (hash.slice(0,this.difficulty) !=='0'.repeat(this.difficulty)) {
                nonce += 1;
                hash = this.computeHash(index, timestamp, data, prehash, nonce);
    
    
            }
            return( "创世区块",{
                index,
                timestamp,
                data,
                prehash,
                nonce,
                hash
            });
        }
    
        //
        computeHashForBlock({index, timetamp, data, prehash, nonce}){
            return this.computeHash(index, timetamp, data, prehash, nonce)
        }
        //计算哈希
        computeHash(index, timetamp, data, prehash, nonce) {
            // index: 1, //索引
            // timetamp: 时间戳,
            // data: 区块的具体信息,
            // prehash: 上个区块的哈希, //哈希1
            // hash: 当前区块的哈希, //哈希2
            // nonce: 随机数,
            return crypto
                    .createHash("sha256")
                    .update(index + timetamp + data + prehash + nonce)
                    .digest("hex");
        }
    
        //校验新区块
        isValidBlock(newBlock,lastBlock=this.getLastBlock()) {
            /**
             * 新区块的index ==区块上的index+1
             * 新区块的time大于区块上的time
             * 新区块的prehash ==区块上的hash
             * 新区块的哈希值是否计算正确
             * 区块的哈希值,符合难度要求
             */
            if (newBlock.index !== lastBlock.index+1) {
                return false;
            }else if(newBlock.timestamp < lastBlock.timestamp){
                return false;
            }else if(newBlock.prehash !== lastBlock.hash){
                return false;
            }else if(newBlock.hash.slice(0,this.difficulty) !=='0'.repeat(this.difficulty)) {
                return false;
                // }else if(newBlock.hash !== this.computeHashForBlock(newBlock)){
                //     return false;
                // }
            }
            return true;
        }
    
        //校验区块链
        isValidChain(chain =this.blockchain) {
            for (let i = chain.length-1; i >=1;i = i-1){
                if (!this.isValidBlock(chain[i],chain[i-1])){
                    return false;
                }
            }
            // 校验创世区块
            if(JSON.stringify(chain[0]) !==JSON.stringify(initBlock)){
                return false;
            }
            return true;
        }
    }
    
    let bc = new Blockchain();
    bc.mine()
    bc.mine()
    bc.mine()
    console.log(bc.blockchain)
最后修改:2022 年 10 月 08 日
如果觉得我的文章对你有用,请随意赞赏