pragma solidity ^0.4.0;
contract A{
byte[10] arr;
function setElement(uint index, byte value) public {
require(index >= 0 && index < arr.length); //Should I leave it as is?
arr[index] = value;
}
function getElement(uint index) view public returns (byte) {
require(index >= 0 && index < arr.length); //Or not?
return arr[index];
}
}
As I know an assert-style exception is generated in the following situations and not only:
But should I check the condition every time?
Also I would like to refund the remaining gas to the executor.
You are using it correctly. require
is intended to be used to check input parameters whereas assert
is to verify the internals of your contract (mostly for testing purposes). If a require
condition fails, the remaining gas will be refunded.
From the Solidity documentation:
The convenience functions assert and require can be used to check for conditions and throw an exception if the condition is not met. The assert function should only be used to test for internal errors, and to check invariants. The require function should be used to ensure valid conditions, such as inputs, or contract state variables are met, or to validate return values from calls to external contracts.