Простая программа BlockChain - PullRequest
0 голосов
/ 22 января 2020

Я пытаюсь создать этот простой код «BlockChain». Я нашел его в Интернете, чтобы лучше понять технику блокчейна.

Но когда я запускаю его, он выдает ошибку при запуске.

Это ошибка, которую я получил: -

import {BlockChain} из 'BlockChain'; ^

Ошибка синтаксиса: неожиданный токен


класс Блок

 export class  Block{

    constructor(timestamp, data, previousHash = '') {
        this.timestamp = timestamp;
        this.data = data;
        this.previousHash = previousHash;
        // The calculation of the hash must be at the end so to ensure that all data is assigned correctly before calculation
        this.hash = this.calculateHash(); }

    calculateHash() {
        return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
    }
}

класс BlockChain

import {Block} from 'Block';



  export class  BlockChain {

    constructor() {
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock(){
        return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", "");
    }

    getLatestBlock() {
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock) {
        // The previous hash value of the new block is the hash value of the last block of the existing blockchain;
        newBlock.previousHash = this.getLatestBlock().hash;
        // Recalculate the hash value of the new block (because the previousHash is specified);
        newBlock.hash = newBlock.calculateHash();
        //Add new blocks to the chain;
        this.chain.push(newBlock);
    }

    isChainValid() {
        //Traverse all the blocks
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
            //Recalculate the has value of the current block. If the hash value is not matched, it indicates that data of the block was changed without permission, and therefore the has value is not recalculated.
            if (currentBlock.hash !== currentBlock.calculateHash()) {
                console.error("hash not equal: " + JSON.stringify(currentBlock));
                return false;
            }
            // Determine whether the previousHash of the current block is equal to the hash of the previous block. If they are not equal to each other, this means that the previous block was changed without permission. Although the hash value is recalculated correctly, the hash value of the subsequent block is not recalculated, resulting the the whole chain breaking.
            if (currentBlock.previousHash !== previousBlock.calculateHash) {
                console.error("previous hash not right: " + JSON.stringify(currentBlock));
                return false;
            }
        }
        return true;
    }

}

Класс теста

import {BlockChain} from 'BlockChain';
import {Block} from 'Block';



    let simpleChain = new BlockChain();

    simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10}));
    simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20}));


    console.log(JSON.stringify(simpleChain, null, 4));

    console.log("is the chain valid? " + simpleChain.isChainValid());

1 Ответ

1 голос
/ 23 января 2020

Код в порядке. Вы можете запустить код следующим образом:

  1. Установить crypto- js с помощью узла:

npm установить crypto- js

Откройте приглашение узла из окна команд / терминала:

Узел

Откройте редактор в узле:

enter image description here

Требуется модуль sha256. Вставьте следующий код в редактор:

const SHA256 = require ('crypto-js / sha256');

  1. Вставить блок, Классы BlockChain и Test, в редакторе:

Блок классов

class  Block{

    constructor(timestamp, data, previousHash = '') {
        this.timestamp = timestamp;
        this.data = data;
        this.previousHash = previousHash;
        // The calculation of the hash must be at the end so to ensure that all data is assigned correctly before calculation
        this.hash = this.calculateHash(); }

    calculateHash() {
        return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
    }
}

Класс BlockChain

class  BlockChain {

    constructor() {
        this.chain = [this.createGenesisBlock()];
    }

    createGenesisBlock(){
        return new Block("2018-11-11 00:00:00", "Genesis block of simple chain", "");
    }

    getLatestBlock() {
        return this.chain[this.chain.length - 1];
    }

    addBlock(newBlock) {
        // The previous hash value of the new block is the hash value of the last block of the existing blockchain;
        newBlock.previousHash = this.getLatestBlock().hash;
        // Recalculate the hash value of the new block (because the previousHash is specified);
        newBlock.hash = newBlock.calculateHash();
        //Add new blocks to the chain;
        this.chain.push(newBlock);
    }

    isChainValid() {
        //Traverse all the blocks
        for (let i = 1; i < this.chain.length; i++) {
            const currentBlock = this.chain[i];
            const previousBlock = this.chain[i - 1];
            //Recalculate the has value of the current block. If the hash value is not matched, it indicates that data of the block was changed without permission, and therefore the has value is not recalculated.
            if (currentBlock.hash !== currentBlock.calculateHash()) {
                console.error("hash not equal: " + JSON.stringify(currentBlock));
                return false;
            }
            // Determine whether the previousHash of the current block is equal to the hash of the previous block. If they are not equal to each other, this means that the previous block was changed without permission. Although the hash value is recalculated correctly, the hash value of the subsequent block is not recalculated, resulting the the whole chain breaking.
            if (currentBlock.previousHash !== previousBlock.calculateHash) {
                console.error("previous hash not right: " + JSON.stringify(currentBlock));
                return false;
            }
        }
        return true;
    }

}

Класс теста

let simpleChain = new BlockChain();

    simpleChain.addBlock(new Block("2018-11-11 00:00:01", {amount: 10}));
    simpleChain.addBlock(new Block("2018-11-11 00:00:02", {amount: 20}));


    console.log(JSON.stringify(simpleChain, null, 4));

    console.log("is the chain valid? " + simpleChain.isChainValid());
Нажмите ^ D, чтобы завершить sh

Это даст вам следующий вывод:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...