If I add a message to require
as a second parameter, will it increase gas consumption?
I did small test in Remix
Here is small contract
contract Sharer {
function sendHalf(address addr) public payable {
require(msg.value % 2 == 0);
}
}
Gas estimation without message:
{
"Creation": {
"codeDepositCost": "32000",
"executionCost": "81",
"totalCost": "32081"
},
"External": {
"sendHalf(address)": "147"
}
}
Gas estimation with message:
{
"Creation": {
"codeDepositCost": "51600",
"executionCost": "105",
"totalCost": "51705"
},
"External": {
"sendHalf(address)": "240"
}
}
-
Interesting. Does this mean that it's okay to use revert messages on testnets, but, ideally, use as few as possible in production? – Paul Razvan Berg Nov 12 '18 at 14:52
-
that is personal choice. if you use messages in dapp logic you will need these in production – Aquila Nov 12 '18 at 16:06
Using @Aquila's contract to perform more tests.
Contract
V1
pragma solidity ^0.4.25;
contract Sharer {
function sendHalf() public payable {
require(msg.value % 2 == 0);
}
}
V2
pragma solidity ^0.4.25;
contract Sharer {
function sendHalf() public payable {
require(msg.value % 2 == 0, "zonk bonk");
}
}
Deployment
V1
{
"Creation": {
"codeDepositCost": "26800",
"executionCost": "81",
"totalCost": "26881"
},
"External": {
"sendHalf()": "133"
}
}
V2
{
"Creation": {
"codeDepositCost": "46400",
"executionCost": "99",
"totalCost": "46499"
},
"External": {
"sendHalf()": "228"
}
}
V2 needs a whopping 19618 more gas at deployment:
Transaction
Importantly, there's no difference in gas costs when transfering an even number of wei
to the contract. However, when the number is odd and require
reverts the transaction...
V1
consumed 21401 gas while V2
21500, the difference being 99. It's not a huge figure, but neither a trivial one. I guess convenience has a cost!