社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  区块链

当我console.log我的测试区块链时,为什么previouushash没有显示?

Dalton • 5 年前 • 594 次点击  

我正在尝试通过使用javascript构建一个小项目来了解更多关于区块链的信息。有人能给我解释一下为什么我控制台时previouushash没有显示出来吗?登录测试区块链?

(NPM安装--如果需要sha256,请保存crypto-js)

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

        class Block{
            //Index: (Optional) tells us where the block is in the chain
            //Timestamp: tells us when the block was created 
            //Data: Can include any kind of data; for a currency you could store details of the transaction(transfer amount, sender id, receiver id)
            //previousHash: String which contains the hash of the block which came before it (Ensures data integrity)

            constructor(index, timestamp, data, previousHash = ''){
                this.index = index;
                this.timestamp = timestamp;
                this.data = data;
                this.previousHash = previousHash;
                //Hash of this block
                this.hash = this.calculateHash();
            }

            //Runs values from block through a hashing function to create a hash
            calculateHash(){
                return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
            }
        }
        //8.44

        class Blockchain{
            //Initializes blockchain
            constructor(){
                this.chain = [this.createGenesisBlock];
            }

            // first block needs to be created manually 
            //index, timeStamp, data, previousHash(there is none in this case)
            createGenesisBlock(){
                return new Block(0, "01/01/2017", "Genesis block", "0");
            }

            //Returns most recently added block
            getLatestBlock(){
            return this.chain[this.chain.length -1];
            }

            //adds a new block to the chain
            addBlock(newBlock){
                newBlock.previousHash = this.getLatestBlock().hash;
                newBlock.hash = newBlock.calculateHash
                //The chain is just an array so you can use regular array methods
                this.chain.push(newBlock);
            }
        }

        //create instance of blockchhain
        let Coin = new Blockchain();
        Coin.addBlock(new Block(1, "10/07/2017", {amount: 4}));
        Coin.addBlock(new Block(2, "12/07/2017", {amount: 10}));

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

我希望console.logged的json文件包含previouushash以及索引、时间戳和数据。它包含所有的,除了先前的灰尘。

谢谢你的帮助,有一段时间一直在挠我的头,想弄明白这件事…

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30549
 
594 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Aminadav Glickshtein
Reply   •   1 楼
Aminadav Glickshtein    5 年前

你也没有 hash 在控制台中。这是有错误的行:

            newBlock.hash = newBlock.calculateHash

calculateHash 是一个函数

            newBlock.hash = newBlock.calculateHash()

现在它开始工作了:

{
    "chain": [
        null,
        {
            "index": 1,
            "timestamp": "10/07/2017",
            "data": {
                "amount": 4
            },
            "hash": "8f84adcf036e9aa052a4d7e689c7b8b06070b851eff535870f5cb8f7d53ab05a"
        },
        {
            "index": 2,
            "timestamp": "12/07/2017",
            "data": {
                "amount": 10
            },
            "previousHash": "8f84adcf036e9aa052a4d7e689c7b8b06070b851eff535870f5cb8f7d53ab05a",
            "hash": "a2479e7df8f2a61f97f3ae4830aff93c0d43041b4a7cbb8079c2309a915d8945"
        }
    ]
}
Adam Baranyai
Reply   •   2 楼
Adam Baranyai    5 年前

你忘记了这句话的偏执: this.chain = [this.createGenesisBlock()]; 因此,链的第一个元素将是对函数的引用,而不是 Block . 再加上妄想症,它就会奏效。

另外,在另一个函数调用中,您也忘记了偏执:当您试图调用函数时 newBlock.hash = newBlock.calculateHash()