0

I've been curious about something for a while. If you wanted to create a state object to track a bunch of the same object, is it more efficient(cheaper) to create a contract for each object or one contract for all the objects?

For instance, say you wanted to keep track of the state of a piece of fruit. Is it more efficient to create a contract for each piece of fruit:

(in pseudocode)

init(id) {
    self.id = id
    self.is_peeled = false
}
peel() {
    self.is_peeled = true
}
isPeeled() {
    return self.is_peeled;
}

Or a contract that uses a lot of storage to track all of the fruit:

init() {
    self.fruit = []
}
addFruit(id) {
    self.fruit[id] = { 
        is_peeled: false
        isPeeled: function() { return self.is_peeled }
    }
}
getFruit(id) {
    return self.fruit[id]
}

And what if it ends up being thousands of objects; does that change the efficiency?

I guess the cost of storage versus the cost of deploying a new contract isn't something I fully grasp yet.

1

One contract for all objects is more blockchain space efficient.

If you create multiple contracts you need to redeploy the contract code payload for each contract.

  • Could you elaborate at all? Are there any drawbacks to using one contract in a scenario like this? – Mike Shultz Jun 8 '17 at 5:13
  • I cannot think any drawbacks using one contract. – Mikko Ohtamaa Jun 8 '17 at 5:25
  • After some testing, it absolutely seems like this is the right answer. Thanks. – Mike Shultz Jun 23 '17 at 6:31
1

Yes, you will have to break your contracts in multiple, otherwise keeping all storage schemas and functions only in one contract would give you problems in future. It may give you out of gas error at some point of time and at that point, you either have to remove some old function or storage in order to add new functionality.

  • I think the only time this would ever be a problem is if I'm iterating over lots of items(which I'm not). It's something worth considering, and perhaps you could add to your answer with use cases that would have this problem. For mine, it doesn't appear to be an issue. – Mike Shultz Jun 23 '17 at 6:33

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.