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

This commit is contained in:
Daniel Kirchner 2018-03-02 17:58:27 +01:00
parent 5982869e94
commit c633c0eacb
11 changed files with 102 additions and 9 deletions

View File

@ -402,7 +402,7 @@ State variables can be declared as ``constant``. In this case, they have to be
assigned from an expression which is a constant at compile time. Any expression assigned from an expression which is a constant at compile time. Any expression
that accesses storage, blockchain data (e.g. ``now``, ``this.balance`` or that accesses storage, blockchain data (e.g. ``now``, ``this.balance`` or
``block.number``) or ``block.number``) or
execution data (``msg.gas``) or make calls to external contracts are disallowed. Expressions execution data (``msg.value``) or make calls to external contracts are disallowed. Expressions
that might have a side-effect on memory allocation are allowed, but those that that might have a side-effect on memory allocation are allowed, but those that
might have a side-effect on other memory objects are not. The built-in functions might have a side-effect on other memory objects are not. The built-in functions
``keccak256``, ``sha256``, ``ripemd160``, ``ecrecover``, ``addmod`` and ``mulmod`` ``keccak256``, ``sha256``, ``ripemd160``, ``ecrecover``, ``addmod`` and ``mulmod``

View File

@ -315,8 +315,9 @@ Global Variables
- ``block.gaslimit`` (``uint``): current block gaslimit - ``block.gaslimit`` (``uint``): current block gaslimit
- ``block.number`` (``uint``): current block number - ``block.number`` (``uint``): current block number
- ``block.timestamp`` (``uint``): current block timestamp - ``block.timestamp`` (``uint``): current block timestamp
- ``gasleft() returns (uint256)``: remaining gas
- ``msg.data`` (``bytes``): complete calldata - ``msg.data`` (``bytes``): complete calldata
- ``msg.gas`` (``uint``): remaining gas - ``msg.gas`` (``uint``): remaining gas - deprecated in version 0.4.21 and to be replaced by ``gasleft()``
- ``msg.sender`` (``address``): sender of the message (current call) - ``msg.sender`` (``address``): sender of the message (current call)
- ``msg.value`` (``uint``): number of wei sent with the message - ``msg.value`` (``uint``): number of wei sent with the message
- ``now`` (``uint``): current block timestamp (alias for ``block.timestamp``) - ``now`` (``uint``): current block timestamp (alias for ``block.timestamp``)

View File

@ -58,8 +58,9 @@ Block and Transaction Properties
- ``block.gaslimit`` (``uint``): current block gaslimit - ``block.gaslimit`` (``uint``): current block gaslimit
- ``block.number`` (``uint``): current block number - ``block.number`` (``uint``): current block number
- ``block.timestamp`` (``uint``): current block timestamp as seconds since unix epoch - ``block.timestamp`` (``uint``): current block timestamp as seconds since unix epoch
- ``gasleft() returns (uint256)``: remaining gas
- ``msg.data`` (``bytes``): complete calldata - ``msg.data`` (``bytes``): complete calldata
- ``msg.gas`` (``uint``): remaining gas - ``msg.gas`` (``uint``): remaining gas - deprecated in version 0.4.21 and to be replaced by ``gasleft()``
- ``msg.sender`` (``address``): sender of the message (current call) - ``msg.sender`` (``address``): sender of the message (current call)
- ``msg.sig`` (``bytes4``): first four bytes of the calldata (i.e. function identifier) - ``msg.sig`` (``bytes4``): first four bytes of the calldata (i.e. function identifier)
- ``msg.value`` (``uint``): number of wei sent with the message - ``msg.value`` (``uint``): number of wei sent with the message

View File

@ -39,6 +39,7 @@ m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{
make_shared<MagicVariableDeclaration>("assert", make_shared<FunctionType>(strings{"bool"}, strings{}, FunctionType::Kind::Assert, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("assert", make_shared<FunctionType>(strings{"bool"}, strings{}, FunctionType::Kind::Assert, false, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::Block)), make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::Block)),
make_shared<MagicVariableDeclaration>("ecrecover", make_shared<FunctionType>(strings{"bytes32", "uint8", "bytes32", "bytes32"}, strings{"address"}, FunctionType::Kind::ECRecover, false, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("ecrecover", make_shared<FunctionType>(strings{"bytes32", "uint8", "bytes32", "bytes32"}, strings{"address"}, FunctionType::Kind::ECRecover, false, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("gasleft", make_shared<FunctionType>(strings(), strings{"uint256"}, FunctionType::Kind::GasLeft, false, StateMutability::View)),
make_shared<MagicVariableDeclaration>("keccak256", make_shared<FunctionType>(strings(), strings{"bytes32"}, FunctionType::Kind::SHA3, true, StateMutability::Pure)), make_shared<MagicVariableDeclaration>("keccak256", make_shared<FunctionType>(strings(), strings{"bytes32"}, FunctionType::Kind::SHA3, true, StateMutability::Pure)),
make_shared<MagicVariableDeclaration>("log0", make_shared<FunctionType>(strings{"bytes32"}, strings{}, FunctionType::Kind::Log0)), make_shared<MagicVariableDeclaration>("log0", make_shared<FunctionType>(strings{"bytes32"}, strings{}, FunctionType::Kind::Log0)),
make_shared<MagicVariableDeclaration>("log1", make_shared<FunctionType>(strings{"bytes32", "bytes32"}, strings{}, FunctionType::Kind::Log1)), make_shared<MagicVariableDeclaration>("log1", make_shared<FunctionType>(strings{"bytes32", "bytes32"}, strings{}, FunctionType::Kind::Log1)),

View File

@ -3000,8 +3000,10 @@ bool MagicType::operator==(Type const& _other) const
return other.m_kind == m_kind; return other.m_kind == m_kind;
} }
MemberList::MemberMap MagicType::nativeMembers(ContractDefinition const*) const MemberList::MemberMap MagicType::nativeMembers(ContractDefinition const *_contract) const
{ {
solAssert(_contract, "");
const bool v050 = _contract->sourceUnit().annotation().experimentalFeatures.count(ExperimentalFeature::V050);
switch (m_kind) switch (m_kind)
{ {
case Kind::Block: case Kind::Block:
@ -3014,13 +3016,17 @@ MemberList::MemberMap MagicType::nativeMembers(ContractDefinition const*) const
{"gaslimit", make_shared<IntegerType>(256)} {"gaslimit", make_shared<IntegerType>(256)}
}); });
case Kind::Message: case Kind::Message:
return MemberList::MemberMap({ {
std::vector<MemberList::Member> members = {
{"sender", make_shared<IntegerType>(160, IntegerType::Modifier::Address)}, {"sender", make_shared<IntegerType>(160, IntegerType::Modifier::Address)},
{"gas", make_shared<IntegerType>(256)},
{"value", make_shared<IntegerType>(256)}, {"value", make_shared<IntegerType>(256)},
{"data", make_shared<ArrayType>(DataLocation::CallData)}, {"data", make_shared<ArrayType>(DataLocation::CallData)},
{"sig", make_shared<FixedBytesType>(4)} {"sig", make_shared<FixedBytesType>(4)}
}); };
if (!v050)
members.emplace_back("gas", make_shared<IntegerType>(256));
return MemberList::MemberMap(members);
}
case Kind::Transaction: case Kind::Transaction:
return MemberList::MemberMap({ return MemberList::MemberMap({
{"origin", make_shared<IntegerType>(160, IntegerType::Modifier::Address)}, {"origin", make_shared<IntegerType>(160, IntegerType::Modifier::Address)},

View File

@ -902,7 +902,8 @@ public:
ByteArrayPush, ///< .push() to a dynamically sized byte array in storage ByteArrayPush, ///< .push() to a dynamically sized byte array in storage
ObjectCreation, ///< array creation using new ObjectCreation, ///< array creation using new
Assert, ///< assert() Assert, ///< assert()
Require ///< require() Require, ///< require()
GasLeft ///< gasleft()
}; };
virtual Category category() const override { return Category::Function; } virtual Category category() const override { return Category::Function; }

View File

@ -906,6 +906,9 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
m_context << success; m_context << success;
break; break;
} }
case FunctionType::Kind::GasLeft:
m_context << Instruction::GAS;
break;
default: default:
solAssert(false, "Invalid function type."); solAssert(false, "Invalid function type.");
} }
@ -921,6 +924,8 @@ bool ExpressionCompiler::visit(NewExpression const&)
bool ExpressionCompiler::visit(MemberAccess const& _memberAccess) bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
{ {
const bool v050 = m_context.experimentalFeatureActive(ExperimentalFeature::V050);
CompilerContext::LocationSetter locationSetter(m_context, _memberAccess); CompilerContext::LocationSetter locationSetter(m_context, _memberAccess);
// Check whether the member is a bound function. // Check whether the member is a bound function.
ASTString const& member = _memberAccess.memberName(); ASTString const& member = _memberAccess.memberName();
@ -1135,7 +1140,7 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
m_context << Instruction::CALLVALUE; m_context << Instruction::CALLVALUE;
else if (member == "origin") else if (member == "origin")
m_context << Instruction::ORIGIN; m_context << Instruction::ORIGIN;
else if (member == "gas") else if (!v050 && member == "gas")
m_context << Instruction::GAS; m_context << Instruction::GAS;
else if (member == "gasprice") else if (member == "gasprice")
m_context << Instruction::GASPRICE; m_context << Instruction::GASPRICE;

View File

@ -2481,6 +2481,25 @@ BOOST_AUTO_TEST_CASE(gas_and_value_basic)
BOOST_REQUIRE(callContractFunction("checkState()") == encodeArgs(false, 20 - 5)); 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) BOOST_AUTO_TEST_CASE(value_complex)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(

View File

@ -515,6 +515,39 @@ BOOST_AUTO_TEST_CASE(blockhash)
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); 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() BOOST_AUTO_TEST_SUITE_END()
} }

View File

@ -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"); 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) BOOST_AUTO_TEST_CASE(builtin_reject_value)
{ {
char const* text = R"( char const* text = R"(

View File

@ -109,6 +109,7 @@ BOOST_AUTO_TEST_CASE(environment_access)
"block.difficulty", "block.difficulty",
"block.number", "block.number",
"block.gaslimit", "block.gaslimit",
"gasleft()",
"msg.gas", "msg.gas",
"msg.value", "msg.value",
"msg.sender", "msg.sender",