![]() Now, let’s add one or more transactions like this: After that, send the exact opposite request as you can see in the image. A small remark, don’t forget to add a header to tell the server we will send JSON objects: Content-Type -> application/json. We will use Postman for sending these request, you can use whatever tool you prefer. We have to make both nodes aware of each other’s existence. We add an extra environment variable to tell the start command on which port it has to run. Open two terminals and execute the start command. Let’s spin up two instances of our blockchain on port 50. The GET /balances/:address endpoint returns how much codeCoins you have. This can be seen as the synchronization process between nodes.Īt last, we all want to know how rich we are. The other nodes will retrieve the new blockchain via GET /blockchain.Ī route called GET /blockchain/resolve has been added as well for newly added nodes to find the longest chain and retrieve it. In addition, the function will send an event to all other nodes in the network about the addition of a new block. Don’t forget to add the rewardAddress parameter in the body of the request as each miner gets 100 codeCoins as a reward for mining a new block. It will trigger the mining function on our blockchain and find the correct hash. Now we have some transactions, we can mine our block including the pending transactions. This field is optional as the genesis block doesn’t have a predecessor.Ĭonstructor(timestamp, transactions, previousHash = '') amount: Transferred amount of Codecoins. At last, we define a field that contains the link to the previous block using a hash. Next parameter is an array of transactions ready to be mined. First, we pass a timestamp to indicate the moment the block got created. Let’s take a look at the different classes.Ī block consists of several properties. A lot of other packages can be found in this package.json file, mainly for compiling JavaScript ES7. In my opinion, a hash is the glue between two blocks. ![]() This package contains crypto standards that can be used in JavaScript, like calculating our hash. The most important package we will use is crypto-js. package.json : contains our installed npm packages.blockchain.js: contains the classes for constructing our blockchain, blocks, and transactions.app.js: contains the Express API for interacting with our blockchain.Project Setup: How To Code a Bitcoin-like Blockchain In JavaScriptĪs we like simplicity, we will only use three different files: ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |