Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (4.1k points)

I am playing with a private Ethereum blockchain, and I am interested in implementing some smart contracts. However, information is very limited since this is a newer implementation of the blockchain.

Just as an example, say I want a contract that holds information about a person. Is it more efficient to create a new contract for each person, or simply hold information about all users in the same contract?

In pseudo-code, the two options look like this.

Option 1 (instantiate a new contract for each person):
 

contract = // contract code
ethereum.newContract(contract, userInfo);

Option 2 (hold info of all users in one contract):

contract = {
  var users = [];
  // other contract code
}
ethereum.newContract(contract, userInfo);

Here's how we can quantify "efficiency" in this case:

  1. Each time a new contract is instantiated, we have to mine the block for the contract, and then mine any transactions the user makes to the contract. If we only instantiate one contract, however, we only mine the contract deployment once, and then any transactions thereafter, but...
  2. If we go with the option of storing all info for all users in one contract, is only the "diff" of the contract data (the "array" of all users) stored as a block, or is the entire set of data stored in every block? Or...
  3. If we go with the option of "contract per user", does it "waste space" if we're storing the entire contract definition multiple times (and is it worth the possibly redundant mining that needs to take place)?

Hopefully I was clear in my question, but if not, please let me know. I believe this question is one of "trade offs".

(Re: the tags -- I'm using the golang implementation of Ethereum, and a JavaScript API to interact with it.)

1 Answer

0 votes
by (14.4k points)
edited by

Answers to your questions: 

  1. Yes, each time a new contract is instantiated, we have to mine the block for the contract, and then mine any transactions the user makes to the contract. However, each time users are to be added, you will have to send a transaction for adding a new record to the existing contract.

  2. Blocks consist of transactions. Each time users are added, you will have to create a transaction for the corresponding function call. But, you will only have to do this once and not the data will not be redundantly copied to future blocks.

  3. Yes, if we go with the option of "contract per user", it will waste space if we are storing the entire contract definition multiple times. 

There is a lot more to learn than this. Enroll now in Blockchain Online Course to learn more.

Browse Categories

...