1

I am trying to understand the reasoning for this: (for an online class)

mapping (uint => uint) public multiplier;
/**
 * Called by the owner of the TollBoothOperator.
 *   Can be used to update a value.
 *   It should roll back if the vehicle type is 0.
 *   Setting the multiplier to 0 is equivalent to removing it and is acceptable.
 *   It should roll back if the same multiplier is already set to the vehicle type.
 * @param vehicleType The type of the vehicle being set.
 * @param multiplier The multiplier to use.
 * @return Whether the action was successful.
 * Emits LogMultiplierSet.
 */
function setMultiplier(uint vehicleType, uint multiplier) public returns(bool success) {
    require(vehicleType > 0); //maybe just not equal to 0
    int currentMultiplier = multiplier[vehicleType];
    require(currentMultiplier == 0);
    return true;
}

What is the point of having a boolean return type since the function will never get to the return value if it will not succeed since the transaction will be rolled back by the require statements.

1 Answer 1

2

One reason would be so that a calling contract can revert state changes if that function call fails.

The following code snippet would revert the state changes made should the execution of setMultiplier fail, preventing the event from triggering. This is made possible because the setMultiplier function has a boolean return value.

event TestEvent(bool indexed testEvent);
function testSetMultiplier() public {
    TestEvent(true);
    require(setMultiplier(0, 1));
}

If for example setMultiplier didn't have a boolean return value, the event TestEvent would trigger despite the setMultiplier function call failing as we would be forced to call setMultiplier like so:

event TestEvent(bool indexed testEvent);
function testSetMultiplier() public {
    TestEvent(true);
    setMultiplier(0, 1);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.