Move msg.gas to global function gasleft(). Closes #2971.

This commit is contained in:
Daniel Kirchner
2018-03-05 11:18:04 +01:00
parent 5982869e94
commit c633c0eacb
11 changed files with 102 additions and 9 deletions
+19
View File
@@ -2481,6 +2481,25 @@ BOOST_AUTO_TEST_CASE(gas_and_value_basic)
BOOST_REQUIRE(callContractFunction("checkState()") == encodeArgs(false, 20 - 5));
}
BOOST_AUTO_TEST_CASE(gas_left)
{
char const* sourceCode = R"(
contract test {
function getGasLeft() public returns (uint256 val) { return msg.gas; }
}
)";
compileAndRun(sourceCode);
BOOST_REQUIRE(callContractFunction("getGasLeft()") == encodeArgs(99978604));
sourceCode = R"(
contract test {
function getGasLeft() public returns (uint256 val) { return gasleft(); }
}
)";
compileAndRun(sourceCode);
BOOST_REQUIRE(callContractFunction("getGasLeft()") == encodeArgs(99978604));
}
BOOST_AUTO_TEST_CASE(value_complex)
{
char const* sourceCode = R"(
@@ -515,6 +515,39 @@ BOOST_AUTO_TEST_CASE(blockhash)
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_CASE(gas_left)
{
char const* sourceCode = R"(
contract test {
function f() returns (uint256 val) {
return msg.gas;
}
}
)";
bytes code = compileFirstExpression(
sourceCode, {}, {},
{make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::Message))}
);
bytes expectation({byte(Instruction::GAS)});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
sourceCode = R"(
contract test {
function f() returns (uint256 val) {
return gasleft();
}
}
)";
code = compileFirstExpression(
sourceCode, {}, {},
{make_shared<MagicVariableDeclaration>("gasleft", make_shared<FunctionType>(strings(), strings{"uint256"}, FunctionType::Kind::GasLeft))}
);
expectation = bytes({byte(Instruction::GAS)});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_SUITE_END()
}
@@ -7409,6 +7409,31 @@ BOOST_AUTO_TEST_CASE(builtin_reject_gas)
CHECK_ERROR(text, TypeError, "Member \"gas\" not found or not visible after argument-dependent lookup");
}
BOOST_AUTO_TEST_CASE(gas_left)
{
char const* text = R"(
contract C {
function f() public returns (uint256 val) { return msg.gas; }
}
)";
CHECK_SUCCESS(text);
text = R"(
contract C {
function f() public returns (uint256 val) { return gasleft(); }
}
)";
CHECK_SUCCESS(text);
text = R"(
pragma experimental "v0.5.0";
contract C {
function f() public returns (uint256 val) { return msg.gas; }
}
)";
CHECK_ERROR(text, TypeError, "Member \"gas\" not found or not visible after argument-dependent lookup in msg");
}
BOOST_AUTO_TEST_CASE(builtin_reject_value)
{
char const* text = R"(
+1
View File
@@ -109,6 +109,7 @@ BOOST_AUTO_TEST_CASE(environment_access)
"block.difficulty",
"block.number",
"block.gaslimit",
"gasleft()",
"msg.gas",
"msg.value",
"msg.sender",