[Ethereum] Error: gas required exceeds allowance or always failing transaction upon function execution

blockchainremixsolidity

Problem
The transaction fails because the transaction ran out of gas instead of it failing because the require condition was not met. It works fine before the solidity update to version 0.4.24. It works in all environment. It can even return the reason I provided with the error in the require parameter. Why is it saying that the gas is not enough? I've tried to assign a higher gas limit, higher than 3000000. I am using remix.ide

Note
Everything works fine if I don't make the require onlyOwner modifier to result to an error.

Expected result
Error with provided reason
Contract Code

pragma solidity ^0.4.24;

 contract Owned {
 address public owner;
 address public newOwner;

 event OwnershipTransferred(address indexed _from, address indexed _to);

 constructor() public {
     owner = msg.sender;
 }

 modifier onlyOwner {
     require(msg.sender == owner);
     _;
 }

 function transferOwnership(address _newOwner) public onlyOwner {
     newOwner = _newOwner;
 }

 function acceptOwnership() public {
     require(msg.sender == newOwner);
     emit OwnershipTransferred(owner, newOwner);
     owner = newOwner;
     newOwner = address(0);
 }
}

Using JavaScript VM Environment
expected result even on Injected Web3 and Web3 Provider Environment

-The image above is the result I expect to get when I use the Injected Web3 and Web3 Provider Environment as that is how it works before around the solidity version 0.4.23.

Using Injected Web3 and Web3 Provider Environment
Occurs upon execution of transferOwnership()

–The window on the above image will pop-up upon execution of the transferOwnership() function. The function with the onlyOwner modifier which require the msg.sender to be equal to the contract owner.

After I force execute the transaction of transferOwnership()

–The image is the result when I choose to still proceed with the transaction even after the warning from the 2nd Image. It results to the same "Transaction mined but execution failed" status like when I ran it using the JavaScriptVM Environment but without the "VM error: revert." return.

Best Answer

As per your code, the owner of your contract is the Account Address with which you deploy the contract on the network:

constructor() public { owner = msg.sender; ////For eg. 0xca35b7d915458ef540ade6068dfe2f44e8fa733c -> JS VM }

Now since the transferOwnership() function call uses the modifier "onlyOwner", it mandates msg.sender for this transaction should be same as the deployer of the contract.

Hope it helps!!