7

I'm currently trying to find practical examples for the usage of a blockchain, instead of a typical database like MySQL. I understand that the big advantage of a blockchain is the high security of data not being compromised.

But how can I imagine accessing or searching for a specific record in the blockchain?

Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?

4 Answers 4

5

You can access blockchain data like the way you access normal data in any MVC where you fetch data from database ,manipulate it in backend and display the result in front end. Incase of blockchain,what ayou need to understand is it is nothing but a decentralized database where transactions occor dynamically and the data is distibuted among all the nodes.

So to explain how the data is actually fetched from blockchain, what I do is ,Crawl each block in the blockchain and store the data in your preferred data base like MySql or Mongo

You can use rpc getblockhash "n", which will return the hash of the block "n", for example: eee7f58d111ef0b25f7f6e8703d80ab6b51ca957170daea949e93e6467906889

You can retrieve the block's contents by calling get block "b", where "b" is the hash. Which can look like this:

{
        "hash" : "eee7f58d111ef0b25f7f6e8703d80ab6b51ca957170daea949e93e6467906889",
        "confirmations" : 176023,
        "size" : 261,
        "height" : 0,
        "version" : 1,
        "merkleroot" : "695191c2e8f8fddc75331a38d658ab07672970b09981ad77c29c4b0b17580941",
        "tx" : [
            "695191c2e8f8fddc75331a38d658ab07672970b09981ad77c29c4b0b17580941"
        ],
        "time" : 1518520240,
        "nonce" : 2084635057,
        "bits" : "1e0ffff0",
        "difficulty" : 0.00024414,
        "nextblockhash" : "1bb4a4c35103a363ad63bf59bf7fa45c06babcfa8d848ceeb7e4958f9a9f660e"
    }

Now every key value of above output replicates some important concept which cannot be ignored. But for now lets just focus on "tx", the array of tx are the list of transaction that have been mined in the "n"th block, and now you can crawl each and evry transaction given in tx array using rpc getrawtransaction to know the detail of each transaction.

Personally, I store the details in MongoDB so I can keep track of my applications' users, their addresses and the transactions they have done.

So this is a simple approach by which you can access data from a blockchain.

3

I'm currently trying to find practical examples for the usage of a blockchain, instead of a typical database like MySQL. I understand that the big advantage of a blockchain is the high security of data not being compromised.

That's a meaningless question. A blockchain is not a replacement for a database; it is a mechanism for multiple parties which are already running their own databases to make sure they all end up in the same state.

For example, in Bitcoin, that database is the UTXO set, which maintains for every unspent coin the amount and owner. Every Bitcoin full node has its own copy of that UTXO set. It can be stored in any kind of database engine - the most common ones using a specialized LevelDB instance. The blockchain consists of a list of authenticated updates to that database. You never search the blockchain itself, as that would be like going through all your bank statements to find your balance. Instead, you just look at the balance itself.

In the case of Bitcoin, that database is a very simple key-value store, as that is sufficient for verifying transactions. It is highly specialized, and does not maintain any indexes beyond what is necessary for validating things. But in theory, it could instead be backed by MySQL - allowing you to issue queries against your own UTXO set.

Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?

That would be ridiculous. The problem a blockchain solves is consistency while permitting multiple independent and distrusting parties to make updates which may conflict with each other ("double spends" in Bitcoin's case). If you have a single party (the employer) who must approve updates to the database anyway (or a collection of identifiable parties who trust each other to some extent) much more efficient technologies exist.

1
  • So what does a blockchain store? If there's LevelDB to store the data, when will data from blockchain be ever fetched?
    – anotherDev
    Commented Jan 19, 2022 at 19:20
0

One possible simple implementation of this would be by making an Ethereum smart contract. You could easily store employee information in a mapping of their ethereum address to whatever information. You can fetch the stored information in javascript. The contract could be written so only the employee could view his data.

3
  • So a smart contract can have his own storage?
    – Felix
    Commented Jan 18, 2018 at 21:25
  • Yes, but writing is very expensive (you will need to pay ethereum gas), so I would not recommend to actually do this, but it's the easiest implementation that I can think of. Reading data is not expensive though. Commented Jan 18, 2018 at 21:30
  • When you say a smart contract can store data, means the data will still be stored on the blockchain, no? Because smart contracts live on the blockchain, right?
    – anotherDev
    Commented Jan 19, 2022 at 19:26
0

Blockchain is a P2P decentralised network of nodes.

The query of "Let's say, in a company we use a blockchain for saving all employee payrolls? How could one look up all payrolls for a specific employee?"

One simply will not use blockchain for this query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.