Our goal? Making
This is part 2 of 2. Part one focuses on Blockchain: just another hype or the next big thing?
We got inspired by a YouTube video about the blockchain technology (https://anders.com/blockchain/ ) and we decided to build our own example in the Mendix Application platform to get a better understanding of the underlying technology. Of course, you can view the code used in this video but we would also like to share the low-code version we built in Mendix and demonstrate what we have learned.
We’ve created a demo of the Mendix blockchain as well. Have a look at this video to see how to use the Mendix app: https://www.youtube.com/watch?v=9eUodR_40IY
Download the example Mendix app from the Mendix Appstore here: https://appstore.home.mendix.com/link/app/106518/Appronto/Blockchain-explanation-demo
We highly recommend you to have a look at the Anders video before diving into the details of the example app found in the appstore. Also have a look at our blog about Blockchain from a generic point of view: Blockchain: just another hype or the next big thing?. Once you have a functional understanding it’s time to open up the Mendix model to have a look under the hood. The rest of this article is meant to give you a few insights in this application model.
Blocks and transactions
We’ve split the model in what we call a MendixBlockchain module and an implementation of this MendixBlockchain: MyGenuineCertificatesBlockchain. The first module is 100% generic, the second one is an implementation of an example case about certificates of genuine products in a blockchain.
The model has two persistent entities: Block and Transaction. A block in the blockchain can have multiple transactions. Don’t think about Transaction as a Financial transaction. A transaction can be whatever you want it to be, it’s just a database record that stores Data (hence the attribute Data). This data might be a financial transaction or could also be a customer’s address. Note that there isn’t any entity ‘Chain’. It’s not needed: the blocks with their reference (PrevHash) together are forming the chain.
Having the transactions stored in a block safe and sound is great. But how can one prove a certificate is really theirs? That’s where private and public keys come in. Every certificate is signed by the owner (of the product). Only the owner with his private key is able to regenerate the signature of a certificate to prove his ownership. We won’t go into further details about private and public key pairs at this point, but you can have a look in the model to get a better understanding.
We’ve created two non-persistent entities to manage the private/public keys (UserKeyPair and PrivateKey). These keys only temporarily exist in memory and are encrypted in your browser cache to provide a better user experience. With these keys you are able to sign your transactions and prove ownership.
Save your private key to prove later that your transaction is yours.
Creating a block and mining
To create an example of a blockchain you first need transactions (data). The entity in our example, GenuineCertificate, inherits from Transaction and has some extra attributes defining a unique genuine certificate for a product built in a specific year in a special place. Just add a few records to this table as shown in the demo video.
After creating one or more GenuineCertificates you’re able to create a block in the blockchain. When creating a block, you need a valid Public and Private key to sign your transactions and all the transactions that are not already in a block will be added.
Creating a block is done by concatenating all the data in the transactions (individually signed by your public key and private key). When this is done a hash of the block’s data is created. The hashes will be stored to chain blocks together. Every block (as you can see in the domain model) contains a reference to the previous block. For example, this can be a SHA256 Hash. This will result in a random string like: 89441726368f69b71faf9aac07f18665838b4672376748849df0028b748c52bc. The process of creating these hashes is very fast and easy.
However, creating a simple hash is not what we usually want for a blockchain. The harder it is to create a hash the harder it will be to manipulate hashes afterwards. Because every block contains a hash reference to its previous block, to change the first block without anybody noticing you need not only to recalculate the first block hash, but also the hashes of all succeeding blocks.
So the harder it is to calculate these hashes the harder it will be to change the entire chain. Therefore in a lot of blockchain techniques ‘mining’ is introduced. Mining is the process of calculating the hash, but not the simple one: a hash that starts with a certain amount of zeros. To create a hash starting with a zero you’ll have to add data for as long as it will take to get a hash starting with a zero. The data that is added is called a nonce, just a number. To find the hash starting with a zero you first hash SHA256(1 + ‘yourdata’). This is repeated until you get the hash starting with a zero. The number used to get this hash will be your block nonce.
The more zeros a hash starts with the more complex it is to find the hash. Nonces will become bigger on average. The more zeros, the more complexity. Bitcoin is changing the amount of zeros occasionally and is therefore making mining more complex.
Below our mining microflow is shown. Have a look and you’ll probably recognize all of the above. In our example we use a complexity of 4 zeros. Change this up to 10 and you’ll see that mining will take longer and longer.
The next block uses the hash of the previous block so even if you change a previous block and re-mine it it will break the chain! The previous hash guarantees immutability.
Note that this process of calculating (mining) is only applicable in Proof of Work blockchains. As you can imagine, mining is very intensive and also very expensive in terms of electricity used. This is why a lot of blockchains are thinking about moving from Proof of Work to Proof of Stake. The latter one doesn’t involve mining to create blocks. For more details see: https://blockgeeks.com/guides/proof-of-work-vs-proof-of-stake/
In the world of cryptocurrencies this mining is rewarded but you have to be the first to claim a mined block.