1

Say a contract factory is used to spawn multiple child contracts, each with associated data within them. The contract factor stores all addresses of the derived contracts along with a unique name of each contract. Is there an easy, efficient way to query information across all the child contracts?

For example, say the child contracts represent people; is it possible to write a query to return all child contracts that have a particular age, name and location of birth (where these properties are member variables on the child contracts). This is in effect running a SQL query over all the child contracts.

Clearly, the naive solution is to iterate over all child contracts from the map within the factory. This is not ideal in any way. Alternatively, an offchain solution could be built where a linking table is made offchain with the associated contract addresses. This solution really is not ideal as I'd like to keep to an on-chain solution. This also results in duplication of data on chain, making the blockchain redundant.

In simple terms: is there a way to build an efficient query structure in order to search for contracts, if the contract addresses are known. This sounds very much like a relational database but running on-chain. IPFS wont help in this context as you are effectively stuck with the same query problem; how to search effectively through all records for particular sub information within entries.

1

I had thought of a similar approach when I was new to solidity i.e. to create a factory and create a new contract for each entity. This is not efficient and will pain you in more ways than one.

  1. What happens when you need to upgrade (fix a bug or change some behavior) those child contract objects?
  2. It does not work like a db and querying each of them is going to be very inefficient and slow process

What I have come to realize over time is that such use cases are not the best to be solved using a public blockchain (I have seen something similar possible in Hyperledger Fabric). But if you still want to do it you can consider following:

  1. Create a separate 'data container' contract to hold all your objects which is not affected when you update your business contract

  2. As an idea, the data container can store only key/value so that new attributes don't require you to change your data contract in future. This contract is referenced like a db from the business contract.

  3. Whatever aggregates (count, sum) you might need you precalculate and store them when the value is stored in your data contract. (Not very efficient but still a better solution than iterating on demand)
  4. Remember, iterating over an array in a contract is unsafe as well as very expensive.

Ideally, a public blockchain should only be used to prove the authenticity of some data/state. And the rest of such business queries and analysis can be done with a private storage containing the same data.

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.