• Complain

Eric Traub [Eric Traub] - Learn Blockchain Programming with JavaScript

Here you can read online Eric Traub [Eric Traub] - Learn Blockchain Programming with JavaScript full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: Packt Publishing, genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Eric Traub [Eric Traub] Learn Blockchain Programming with JavaScript

Learn Blockchain Programming with JavaScript: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Learn Blockchain Programming with JavaScript" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Explore the essentials of blockchain technology with JavaScript to develop highly secure bitcoin-like applications

Key Features
  • Develop bitcoin and blockchain-based cryptocurrencies using JavaScript
  • Create secure and high-performant blockchain networks
  • Build custom APIs and decentralized networks to host blockchain applications
Book Description

Learn Blockchain Programming with JavaScript begins by giving you a clear understanding of what blockchain technology is. Youll then set up an environment to build your very own blockchain and youll add various functionalities to it. By adding functionalities to your blockchain such as the ability to mine new blocks, create transactions, and secure your blockchain through a proof-of-work youll gain an in-depth understanding of how blockchain technology functions.

As you make your way through the chapters, youll learn how to build an API server to interact with your blockchain and how to host your blockchain on a decentralized network. Youll also build a consensus algorithm and use it to verify data and keep the entire blockchain network synchronized. In the concluding chapters, youll finish building your blockchain prototype and gain a thorough understanding of why blockchain technology is so secure and valuable.

By the end of this book, youll understand how decentralized blockchain networks function and why decentralization is such an important feature for securing a blockchain.

What you will learn
  • Gain an in-depth understanding of blockchain and the environment setup
  • Create your very own decentralized blockchain network from scratch
  • Build and test the various endpoints necessary to create a decentralized network
  • Learn about proof-of-work and the hashing algorithm used to secure data
  • Mine new blocks, create new transactions, and store the transactions in blocks
  • Explore the consensus algorithm and use it to synchronize the blockchain network
Who this book is for

Learn Blockchain Programming with JavaScript is for JavaScript developers who wish to learn about blockchain programming or build their own blockchain using JavaScript frameworks.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Eric Traub [Eric Traub]: author's other books


Who wrote Learn Blockchain Programming with JavaScript? Find out the surname, the name of the author of the book and a list of all author's works by series.

Learn Blockchain Programming with JavaScript — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Learn Blockchain Programming with JavaScript" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
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. F or 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 - photo 1

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 - photo 2

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.

Refactoring the /mine endpoint

Let's refactor the /mine endpoint by implementing the following steps:

  1. Head over to the dev/networkNode.js file. In the /mine endpoint, beneath the part where we had defined the newBlock variable, let's add the functionality to broadcast the new block to all the other nodes in the network. To do this, follow the same process that we introduced in the previous sectionsthat is, to loop through all the other nodes inside the network, make a request to the nodes, and send the newBlock variable as data:
bitcoin.networkNodes.forEach(networkNodeUrl => {
})

The preceding line mentions that for each of the networkNodes, we're going to make a request and send along the newBlock.

  1. We then need some request options to send. These options will be defined as follows:
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
};
})
  1. The first option in this object is the uri. The uri to which we want to send the request will be the networkNodeUrl and the new endpoint that we are going to create, which will be /receive-new-block. We'll work on this endpoint in the next section:
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
uri: networkNodeUrl + '/ receive-new-block',
};
})
  1. The next option to be added is the method that will be used, which is the POST method:
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
uri: networkNodeUrl + '/ receive-new-block',
method: 'POST',

};
})
  1. Next, let's send along the data that will be inside the body. We also want to send along a newBlock instance:
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
uri: networkNodeUrl + '/ receive-new-block',
method: 'POST',
body: { newBlock: newBlock }
};
})
  1. Finally , after the body , set json to true, as follows:
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
uri: networkNodeUrl + '/ receive-new-block',
method: 'POST',
body: { newBlock: newBlock },
json: true
};
})
  1. After that, make the request by adding the following highlighted line of code:
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
uri: networkNodeUrl + '/ receive-new-block',
method: 'POST',
body: { newBlock: newBlock },
json: true
};
rp(requestOptions)
})
  1. Every time one of these requests is made, it's going to return a promise. Let's make an array of all of these promises by adding the following highlighted code:
const requestPromises = [];
bitcoin.networkNodes.forEach(networkNodeUrl => {
const requestOptions = {
uri: networkNodeUrl + '/ receive-new-block',
method: 'POST',
body: { newBlock: newBlock },
json: true
};
requestPromises.push(rp(requestOptions));
});

After our forEach loop has run, we should have an array that is filled with promises.

  1. Next, let's run all of those promises. Therefore, after the forEach block, add the following code:
Promise.all(requestPromises)
.then(data => {
// ....
})

After all of the requests have run, we want to carry out another calculation inside .then(data => { }). If you remember, when a new transaction is created, the mining rewards transaction code, bitcoin.createNewTransaction(12.5, "00" , nodeAddress);, needs to be broadcast throughout the entire blockchain network. At the moment, when a new block is mined, we create a mining reward transaction, but it is not broadcast to the whole network. To broadcast it, the request will be sent to the /transaction/broadcast endpoint, because it already has the functionality to broadcast transactions. We're just going to make a call to this endpoint with the mining reward transaction data passed in.

  1. Before passing the mining reward transaction data, however, we need some request options:
Promise.all(requestPromises)
.then(data => {
const requestOptions = {
uri: bitcoin.currentNodeUrl + '/transaction/broadcast',
method: 'POST',
};
})
  1. The body data will be sent as an object. In the body, let's add the mining reward transaction data:
Promise.all(requestPromises)
.then(data => {
const requestOptions = {
uri: bitcoin.currentNodeUrl + '/transaction/broadcast',
method: 'POST',
body: {
amount: 12.5,
sender:"00",
recipient: nodeAddress
}
};
})
  1. Finally, after the body, set json to true by adding the following line:
json: true
  1. Then, after the requestOptions, let's send the following request:
return rp(requestOptions);

At this point, inside the /mine endpoint, a bunch of calculations are being carried out to create a new block. Then, once the new block is created, it is broadcast to all the other nodes inside the network. After the broadcast is complete inside the .then block, a new request to the /transaction/broadcast endpoint is made. This request will create a mining reward transaction and the nodes will then broadcast it to the entire blockchain network. Then, after the request runs and all of the calculations are complete, a response is sent: New block mined successfully .

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Learn Blockchain Programming with JavaScript»

Look at similar books to Learn Blockchain Programming with JavaScript. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Learn Blockchain Programming with JavaScript»

Discussion, reviews of the book Learn Blockchain Programming with JavaScript and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.