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?