Your childhood friend, Clay, is the biggest fan of all things related to the Blockchain technology. You've been listening to him for months and years talking non-stop about cryptocurrencies and the security of decentralized data storage.
Despite Clay's insistence on you investing in cryptos, the truth is that what you've always been interested in how the Blockchain can be applied in other applications beyond cryptocurrencies. So, as a software developer, you decide to create your own project with this technology: a to-do list app that stores all the tasks and their history of changes in Blockchain.
How it works?
This API has to allow the user to add new notes and mark them as completed. The flow will be always the same: first you need to add a new note/task and then you save it on the Blockchain. In order to make the Blockchain data persistent, we need to store the Blockchain in a file.
Workflow
The workflow of the app is as follows:
Send request to the API to add or mark as completed any task
Load the Blockchain data from the file, if exists
Create a Block with the new to-do/task data
Add the new Block to the Blockchain
Save the Blockchain with the new data on a file
Technical Considerations
Keep in mind the following considerations related to the Blockchain:
Every Block has multiple properties:
timestamp
: the timestamp for the moment when the block was created
lastHash
: hash of the previous block on the Blockchain
data
: information we want to store in the block (in our case the description of the task)
hash
: a SHA256 string for the block, calculated concatenating the timestamp, lastHash and data.
The implementation of the Blockchain must follow these contracts:
interface Blockchain {
/** Adds new block to blockchain */
addBlock(block: Block): Block
/**
* Validates the chain by checking if:
* - every element's last hash value matches previous block's hash
* - data hasn't been tampered (which will produce a different hash value)
*/
isValid(blockchain: Blockchain): boolean
/** The new blockchain that is a candidate for replacing the current blockchain */
replace(blockchain: Blockchain): boolean
}
interface Block {
/** Generate the hash for the given block */
static generateHashFromBlock(block: Block): string
}
Tips
You can think about the Blockchain as a sort of Linked List, which have some extra properties to ensure the validity of the items and the list as a whole.
When you have to update a task or note of the application (i.e. when marking it as completed), you will have to create a new Block with this information, but the previous one will remain in the Blockchain with the outdated information.