- Learn Blockchain Programming with JavaScript
- Eric Traub
- 533字
- 2021-06-10 18:40:37
Testing the createNewTransaction method
Let's test the createNewTransaction method that we created in the previous section. Just as a heads up: this section is going to be a lot of fun, as here you will really start to understand how powerful a blockchain can be and how blocks and transactions work with each other. You'll also get to learn how the transactions are recorded in the blockchain. So let's get started:
- We're going to test our createNewTransaction method in our test.js file. In this file, we've already required our blockchain.js file and have made a new instance of our Blockchain called bitcoin, which we are logging out at the end of our file. Take a look at the following screenshot for a quick review:
- Now, the first thing that we're going to do in our test.js file is create a new block using our createNewBlock method, similarly to what we did in the Testing the createNewBlock method section. Type in the following into your test.js file:
bitcoin.createNewBlock(789457,'OIUOEDJETH8754DHKD','78SHNEG45DER56');
- Next, what we want to do is create some new transactions to test our createNewTransaction method. This createNewTransaction method takes in three parameters, such as amount, a sender, and the recipient. Let's add this transaction data to our test case:
bitcoin.createNewTransaction(100,'ALEXHT845SJ5TKCJ2','JENN5BG5DF6HT8NG9');
In the preceding line of code, we've set the amount of the transaction to 100 and the sender and recipient's address to some random hash numbers.
Now, let's just quickly summarize what we have done so far in our test case. Take a look at the following code block:
const Blockchain = require('./blockchain');
const bitcoin = new Blockchain();
bitcoin.createNewBlock(789457,'OIUOEDJETH8754DHKD','78SHNEG45DER56');
bitcoin.createNewTransaction(100,'ALEXHT845SJ5TKCJ2','JENN5BG5DF6HT8NG9');
console.log(bitcoin);
In the preceding code, we first required the bitcoin blockchain, and then we created a new block. After that, we created a new transaction, and then we logged out the bitcoin blockchain.
When we run this test.js file, we should expect to see our bitcoin blockchain, which should have one block in the chain as well as one transaction in the pendingTransactions array because we have not mined or created a new block after creating the transaction. Let's save this file and run it to see what we get.
- Now go to your terminal window, type in the following command, and then press Enter:
node dev/test.js
We get to observe the bitcoin blockchain on the terminal window, as shown in the following screenshot:
In the output on your window and in the preceding screenshot, you can observe our chain, which has the one block that we created. In our pendingTransactions array, we have one pending transaction, which is the transaction we created in the test case. Looking at the output of the test, we can conclude that, so far, our createNewTransaction method works fine.
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現
- Learning Bayesian Models with R
- Java程序設計與計算思維
- 秒懂設計模式
- Julia Cookbook
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Getting Started with Laravel 4
- 計算機應用基礎項目化教程
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- ASP.NET Web API Security Essentials
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- 算法秘籍
- 3ds Max 2018從入門到精通
- Data Manipulation with R(Second Edition)
- MongoDB Cookbook