1

Quick premise, I'm super new to blockchain and Solidity.

I've tried to figure out scalability implication of reading data from the blockchain. I understand that reading operations are free, but how do they work exactly? And what are the scalability limitations of reading data from a contract?

Say that I want to query information from some "objects" (is it the right term?) and I have a getter on a mapping, what's the best approach to request information about 10/100/1000 objects?

Edit: Oh wow I got downvoted (!)

Adding some example code:

contract TestContact {
    struct Object {
        uint id;
        uint price;
    }

    mapping (uint => Object) private objects; // this is in the 10/100ks range
    mapping (uint => address) private mapObjectToOwners; // this is in the 10/100ks range

    function getPrice(uint id) public view returns(uint price) {
        Object storage object = objects[id];
        return object.price;
    }
}

I'd like to get the prices of 100s of Objects at the time, but not sure what's the best way to go about it. I can query getPrice multiple times of course, but seems extremely unscaleable and I am not sure what's the best way to go about it.

  • Can you add some code to it ? so that it give more understanding to question. – Himanshu sharma Dec 29 '17 at 13:30
  • You're talking about scalability, but I think your question is actually about performance, right? Could you clarify? If I understand correctly, you're wondering how to batch your requests so as to fetch the data faster than if you made individual requests for each element. – user19510 Dec 29 '17 at 18:37
  • Yes, there are two aspects of it, 1) performance of the dapp (worst case I can create a cache in redis or a server mirroring the data on storage) 2) performance on the blockchain, don't wanna be disrupting like cryptokitties did :) – Dave Dec 31 '17 at 12:23
1

As you said, view and pure "reading" functions are free. The reason for this is that they are only executed in your own node and not broadcasted to the blockchain to other nodes. Nobody else really cares if you read some data from your node so no need to send that information further.

That way you don't have to worry about "burdening" the blockchain or paying big gas fees for subsequent read operations. In theory you can query the data as inefficiently as you want because you're only adding load to your own node.

For your own efficiency, you can return arrays of data with some restrictions (see http://solidity.readthedocs.io/en/develop/frequently-asked-questions.html#can-you-return-an-array-or-a-string-from-a-solidity-function-call for details).

0

As far as your DApp is concerned, the blockchain is a local database, and it's yours to query as much as you like - for free! Calling your example contract function getPrice() only results in a few local function calls and HTTP queries - nothing that affects the network in any way.

The only time you would have an impact like the "cryptokitties" did, is if you were to submit all those queries to the network as transactions. But don't do that because a) it costs gas and b) it burdens the network with transactions that could just be local calls.

So make sure you understand the difference between queries (which are just like querying a local file or database) and transactions (requests to change the blockchain state which get broadcast to the network).

To answer your question more directly, if firing 100s of local queries is too slow or inefficient for you, and you have the option to do so, you can optimize for that type of thing through contract design. In your example, I don't know offhand how to speed that up beyond batching in an array as @lauri-peltonen has pointed out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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