Have you ever tried to explain blockchain to your friends or colleagues, but you don't have an app where you can easily visualize it?
The blockchain is a set of multiple transaction ledgers kept in sync. This implies the use of a distributed database.
Each transaction contains a unique hash of its information, and it is linked to the previous transaction, creating a chain. Once a transaction is inserted in the ledger and there is a consensus in the ledgers, it is final. To change it, you would have to update all the subsequent transactions in all ledgers.
Some blockchains (more common in cryptocurrencies) have the concept of proof of work, meaning each transaction hash must comply with a rule. This rule check is used to confirm the transaction in multiple ledgers. For a peer to validate the block, it must try different nonces while hashing the block until it gets a hash that complies with the rule. This also serves the purpose of regulating how difficult it is to validate a block.
You can use the app on your web browser or point your OutSystems Now app to the QR code* using a tablet. Go ahead and open it.
Using the App
Click the Initialize Blockchain button or the Reset Chain link and add a few blocks.
You should see something similar to this.
This blockchain is valid; all blocks have the correct previous block hash, and they all have a valid hash calculation for the difficulty of 1. But, what happens if I try to break the chain by changing the data for block #3 and re-hashing it?
Changing the data causes the hash of the block to change; the next block is now invalid because it does not match the previous hash, which invalidates the entire chain from that point on.
For the chain to be valid again, I would need to rebuild and re-hash blocks #4 and #5 in the correct sequence. However, this is a distributed system, and the longest valid chain wins in the synchronization mechanism.
Changing the difficulty will cause the system to take longer to find the correct hash. Try it by changing blocks #3, #4, and #5 and compare the nonce. Remember to refresh and re-hash the blocks. You should see something similar, but your mileage may vary. If not, try changing block #2.
In the current example, the system is trying sequential numbers, starting from zero until it gets the desired number of zeros at the beginning of the hash. In this case, it also represents the number of attempts it took to get the correct hash.
As usual, the app is available for download from the OutSystems Forge.
Go build those apps!
QR CODE is a registered trademark of Denso Wave Incorporated.