Let’s get started and create a class called Smart_Blockchain. The constructor method of this class currently creates two empty lists, one for storing transactions and the other for storing chain of blocks. Meanwhile, in the constructor method of this class, we also write the code for creating the initial genesis block. Now we need to complete the following four methods:new_block, new_transaction, last_block, hash
But there are no consensus issues in “Smart Blockchain” and neither the proof-of-work (POW) mechanism nor the proof-of-stock (POS) mechanism nor … needed. In other words, there is no need to produce a couple of alternatives of a single block like the Bitcoin and Ethereum network to let miners can choose a valid and correct block through spending billions of dollars and staggering power consumption. The following code is set exactly based on these features.
In the above codes, there is no proof variable or nonce and there is no need to block reward. Because, as mentioned earlier, there is no consensus on “smart blockchain.” Even transaction fees could be zero. But to avoid spamming, for example, we consider 0.00005 of the total cost of each transaction. This small amount is stored in the (BPSC) smart contract, and the total cost of transactions can be used for development, etc., or even burned to create negative inflation. Negative inflation is one of the most effective ways to maintain and strengthen the value of money as well as for cryptocurrencies.
We are now using the Flask framework so that we can communicate with the “smart blockchain” through requests based on the HTTP protocol platform. To do this, we create the following three methods:/transactions/newRequest to build a new transaction and add it to a block/mineAsk our server to mine a new block/chainRequest to return a complete list of blocks
According to the above code, the server will be launched on port 5000. Of course, another way is to import sys and make the following changes to the end codes. With this, you can always specify your desired port when running.
Now we concatenate the two main parts of the code. Of course, you can download all the code for this article here, and at this point, our file name is bpsc101.py. In the next step, we will run the server. We also use Postman to connect to the new API on the network.
We will start communicating with the new API by sending a POST transaction request to our server address:http://localhost:5000/transactions/newThe information for this transaction must be entered in the Body section, and obviously, we select raw and then JSON.
In the example above, we wanted to send one million tokens (for example, Aka tokens) from our wallet to another person (with a specific address). To do this, we sent the tokens and the request for this transaction to the server (which acts as a Block Producer Smart Contract). As you see, the server responded that this transaction would be added to the second block. Because the Genesis Block has already been defined in the code.
We will now send a GET mining request to our server address: http://localhost:5000/mineThis will create the next block. The content of each block includes all performed transactions and pending ones, for instance, here, the second block is created that contains only one transaction.
According to the image above, the amount of fifty tokens for the transaction fee remains in the (BPSC) wallet, and the rest of the amount is sent to the final recipient by (BPSC). Of course, as mentioned before, the transaction fees will either be burned or spent on network development and so on. However, the type of spending these amounts has not been modeled yet. Now, We know that there must be two blocks in our chain’s server. To see the current chain, we send a GET chain request to our server address: http://localhost:5000/chain
For further testing, you can send new requests for the transaction, and each time after a few transaction requests, you can send a mining request. Even, without having to do a transaction, you can mine an empty block. Meanwhile, the chain request determines the status of the chain for you at any time.