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

Creating the createNewTransaction method

The next method that we are going to add to our blockchain constructor function is called createNewTransaction. This method will create a new transaction for us. Let's follow the below mentioned steps to create the method: 

  1. Start building up this method by adding the following line of code after our getLastBlock method:
Blockchain.prototype.createNewTransaction = function () {

}
  1. The function () will take three parameters, such as the following:
Blockchain.prototype.createNewTransaction = function (amount, sender, recipient) {

}

What these three parameters will do is as follows: 

  • amount: This parameter will take in the amount of the transaction or how much is being sent in this transaction.
  • sender: This will take in the sender's address.
  • recipient: This will take in the recipient's address.
  1. The next thing that we want to do inside of our createNewTransaction method is create a transaction object. So, add the following line of code to our method:
const newTransaction = {

}
  1. This object will have three properties in it. It will have an amount, a sender, and the recipient. These are the same three parameters that we passed into our function(). So, type in the following:
Blockchain.prototype.createNewTransaction = function (amount, sender, recipient) {
const newTransaction = {
amount: amount,
sender: sender,
recipient: recipient,
};

}

This is what our transaction object will look like. All of the transactions that we record on our Blockchain are going to look just like this. They all are going to have an amount, a sender, and the recipient, which is pretty straightforward and simple.

  1. The next thing that we want to do now is push this newTransaction data into our newTransactions array. Let's do this by adding the following code after our newTransaction object:
this.newTransactions.push(newTransaction);

So, the new transaction that we just created will now be pushed into our newTransactions array.

Now, let's just try to understand what this newTransactions array actually is. Basically, what is happening here with this newTransactions array is that on our blockchain there are going to be a lot of people who will be making a lot of different transactions. They will be sending money from one person to another and this will be happening repetitively. Every time a new transaction is created, it's going to be pushed into our newTransactions array.

However, all of the transactions in this array are not really set in stone. They're not really recorded in our blockchain yet. They will get recorded in our blockchain when a new block is mined, which is when a new block is created. All of these new transactions are pretty much just pending transactions, and they have not been validated yet. They get validated, set in stone, and recorded in our blockchain when we create a new block with the help of the createNewBlock method. 

In our createNewBlock method, you can observe in transactions: this.newTransactions that we set the transactions on a new block equal to the newTransactions or the pending transactions in our blockchain. You can think of this newTransactions property on our blockchain as a pending transactions property.

For easy reference,  let's actually change all of the newTransactions properties in our code to pendingTransactions properties. Overall, when a new transaction is created, it is pushed into our pendingTransactions array. Then, when a new block is mined or when a new block is created, that's when all of our pending transactions become recorded on our blockchain, and they are then set in stone and can never be changed.

The point of all this is that before our method ends, we want to return in which block we will be able to find the new transaction because our new transaction will be in the next block when it is mined. Consequently, we'll simply type the following code:

this.newTransactions.push(newTransaction);
return.this.getlastBlock()['index'] + 1;

In the preceding code, this.getlastBlock() returns a block object for us. We want to get the index property of this block – adding ['index'] will provide us with the index of the last block in our chain, and adding + 1 will provide us with the number of the block our transaction was pushed to. 

Let's have a quick recap,  the createNewTransaction method simply creates a newTransaction object, and then we push that newTransaction into our pendingTransactions array. Finally, we return the number of the block that the newTransaction will be added to.

主站蜘蛛池模板: 旬阳县| 乾安县| 青海省| 德州市| 香河县| 海丰县| 麻阳| 淮南市| 西安市| 宁陵县| 子长县| 秦皇岛市| 新野县| 汶川县| 潜山县| 景洪市| 林口县| 江陵县| 溧阳市| 新邵县| 瓮安县| 新竹县| 麟游县| 潞城市| 将乐县| 修文县| 德昌县| 泽库县| 丰都县| 塔城市| 九龙城区| 盘锦市| 沭阳县| 科技| 镇沅| 英吉沙县| 珠海市| 宿迁市| 加查县| 潜江市| 鄂尔多斯市|