0

I have an use-case where the user should be able to change a contract's state only when the inputs pass a validation check. However the validations are expensive to perform and can be done locally because they depend only on the function parameters. Is it is possible to perform the validation with a contract call and then, if it passes, broadcast the state-changing transaction with a send transaction?

This is the example I have in mind:

contract A {

    uint8 stateVariable = 0;

    //constant callable function for expensive verification
    function constantVerifier(uint8 param) constant {
        //expensive verification
        require(param <= 5);
        //broadcast transaction to change state of this instance
        A(this).changeState();
    }

    //state changing function
    function changeState() {
        //only this contract allowed to change state
        require(msg.sender == address(this));
        //change state
        stateVariable++;
    }
}

This particular snippet doesn't change the state. Is it possible to achieve something like this in Solidity? In general, what is the design pattern to handle expensive checks before changing the state of contracts?

1

You can split constantVerifier to avoid expensive verification. The code as follows:

contract A {

    uint8 stateVariable = 0;

    //constant callable function for expensive verification
    function constantVerifier(uint8 param) constant returns (bool){
        if (param <= 5) {
            return true;
        }
        return false;
    }

    //state changing function
    function changeState() {
        //only this contract allowed to change state
        require(msg.sender == address(this));
        //change state
        stateVariable++;
    }

}

At first, you should call constantVerifier function to check whether the param is valid. If return true and then calling changeState, or nothing. Hope it helps~

  • I just realised how silly obvious that was. Thanks :) – Ketan Bhokray Sep 28 '17 at 9:25
  • EDIT: Oh wait. I need the changeState() function to be called only if the verification passes. The above solution is gives me a way to change the state even when the params are not valid. That should not happen – Ketan Bhokray Sep 28 '17 at 9:32
  • At first initing contract A, and then call this code: if (contractA.constantVerifier(param)) { contractA.changeState(); } else { // do something } – BinGoBinBin Sep 28 '17 at 9:40
  • That would still cost me gas and moreover increase the gas consumed due to the extra call, as stated in this thread: ethereum.stackexchange.com/q/13851/5791 I am looking for a non-charged non-state changing trusted validation mechanism, if it is possible – Ketan Bhokray Sep 28 '17 at 10:08
  • @KetanBhokray The constantVerifier function is constant function, it doesn't consume gas. – BinGoBinBin Sep 28 '17 at 10:42

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.