javascript - 为什么当我 console.log 我的测试区 block 链时 previousHash 没有显示?

标签 javascript blockchain

我正在尝试通过使用 JavaScript 构建一个小项目来了解更多关于区 block 链的信息。谁能向我解释为什么当我对测试区 block 链进行 console.log 时 previousHash 不显示?

(npm install --save crypto-js 如果你需要 SHA256)

        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 文件包含 previousHash 以及索引、时间戳和数据。它包含除 previousHash 之外的所有内容。

感谢您的帮助,我绞尽脑汁想弄清楚这个问题......

最佳答案

您忘记了行中的括号:this.chain = [this.createGenesisBlock()]; 行,因此链的第一个元素将是对函数的引用,而不是Block 的实例。添加括号,它应该可以工作。

此外,您还忘记了另一个函数调用中的括号:当您尝试调用函数 newBlock.hash = newBlock.calculateHash()

关于javascript - 为什么当我 console.log 我的测试区 block 链时 previousHash 没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54118212/

相关文章:

java - 无法为 com.r3.corda.finance.obligation.flows.CreateObligation$Initiator 类型的 FlowLogic 构造 FlowLogicRef

rust - 如何在 Substrate 特定类型和 Rust 原始类型之间进行转换?

javascript - 如何在 HTML 中编写算法/伪代码?

javascript - 在带有以太 JS 的 Next Js 中使用 window.ethereum 时出错

javascript - 如何创建带有函数参数的按钮?

websocket - 如何在 BSC 上获取待处理的交易

javascript - 如何将嵌入/嵌套的 FormGroup 转换为 FormData

javascript - 仅在使用 CSS 的较长页面中显示按钮

blockchain - 错误 : [client-utils. js]:sendPeersProposal - Promise 被拒绝:错误:无法反序列化创建者身份

JSON 写入创世 block 失败 : invalid character '\\' looking for beginning of object key string