3

I'm looking for a best practice answer re testing smart contracts. We have internal variables of the contract that we'd like to access during tests. However, making them public incurs more gas during deployment due to the added accessor functions. Is there an alternative way to access these vars during testing?

  • if the variable is public you don't need any accessor!! – Badr Bellaj Nov 30 '16 at 10:22
  • 2
    @BadrBellaj Marking a variable public is just a way of telling Solidity to make accessors for it. They don't clutter up your code but you still have to pay gas for them when you deploy the contract. – Edmund Edgar Nov 30 '16 at 11:31
  • Exactly, so I'm wondering what's the best practice for being able to access the vars in test w/o paying the deployment gas. – ronme Nov 30 '16 at 11:37
  • @EdmundEdgar indeed i means that by declaring public variables we don't need to define explicit accessors but they are implicitly defined by solidity. – Badr Bellaj Nov 30 '16 at 12:21
4

I've found it effective to use inheritance for such cases.

contract My {
    uint someVar;
}

contract MyTestabe is My {
    function someVar() constant returns(uint) {
        return someVar;
    }
}

Then just perform tests on MyTestable instead of the base contract. You should be aware that in this case you can't be 100% sure that the base contract will perform the same as testable(for example new functions will affect gas costs of the old functions), but in most cases it will be alright.

2

You can either make them public during testing and change them back to internal for release, or you can use events at every place you want to watch your variable.

Both solution cost more gas but are to be removed before release anyway.

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.