官术网_书友最值得收藏!

Testing the createNewBlock method

Let's follow the below mentioned steps to test the createNewBlock method: 

  1. Firstly, underneath where we created our bitcoin variable, type in the following highlighted line of code:
const Blockchain = require('./blockchain');

const bitcoin = new Blockchain();

bitcoin.createNewBlock();

console.log(bitcoin);
  1. This createNewBlock() method requires three parameters, such as nonce, previousBlockHash, and a hash. For test purposes, we can just pass in whatever we want for now. Here, the nonce will just be a number. Then we will create a dummy hash for our previousBlockHash, followed by another hash for our hash parameter, as follows:
bitcoin.createNewBlock(2389,'OIUOEREDHKHKD','78s97d4x6dsf');

Right now, we are creating our bitcoin blockchain, followed by a new block in our bitcoin blockchain. When we log out of our bitcoin blockchain, we should have one block in it.

  1. Save this file and run our test.js file again in the terminal. You'll then get to observe the following output: 

In the preceding screenshot, you can observe the entire blockchain data structure in the chain array. This has one block in it, or one object in it. This block also has the hash, nonce, and previousBlockHash parameters that we had passed. It also has the timestamp and the index of 1. It has no transactions because we haven't created any transactions yet. Consequently, we can conclude that the createNewBlock method works just fine. 

  1. Now let's test our method even further by creating a couple more blocks in our chain. Let's duplicate the following lines of code  multiple times and then try to change the values in it as we wish:
bitcoin.createNewBlock(2389,'OIUOEREDHKHKD','78s97d4x6dsf');
  1. After duplicating the code and changing the value, save the file. Now, when we run our test.js file, we should have three blocks in our chain, as shown in the following screenshot: 

In the preceding screenshot, you may have observed the three blocks inside of the chain array. These are all of the blocks that we've created with our createNewBlock method. 

主站蜘蛛池模板: 诏安县| 恩平市| 全南县| 会理县| 商丘市| 大港区| 三亚市| 鄂托克旗| 广宗县| 澄江县| 洪湖市| 故城县| 嘉禾县| 灵武市| 铜陵市| 齐河县| 郓城县| 临江市| 吴旗县| 凤台县| 东平县| 贵港市| 曲松县| 盐池县| 边坝县| 酒泉市| 新营市| 集安市| 化隆| 新竹县| 三门峡市| 奉贤区| 临邑县| 东兴市| 彭泽县| 尤溪县| 和顺县| 永昌县| 三门峡市| 蛟河市| 洛川县|