aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-08-14 18:14:53 +0800
committerGitHub <noreply@github.com>2018-08-14 18:14:53 +0800
commitd01ffd1ad9737a13a7d39e33246d101b36b5cd93 (patch)
tree20a7a250cd491fd2d639a4bfd4a19f77a2fc5542 /test/libsolidity
parent6ca3973944546d1a5518274fea47e635a07f9ca5 (diff)
parentb325a70d59baf1afc4e284cc8738d71069ec97c2 (diff)
downloaddexon-solidity-d01ffd1ad9737a13a7d39e33246d101b36b5cd93.tar.gz
dexon-solidity-d01ffd1ad9737a13a7d39e33246d101b36b5cd93.tar.zst
dexon-solidity-d01ffd1ad9737a13a7d39e33246d101b36b5cd93.zip
Merge pull request #4799 from ethereum/semanticsTestsGasEtAl
Semantics tests for ``gasleft()``, ``blockhash()`` ``tx.gasprice`` and ``block.gaslimit``
Diffstat (limited to 'test/libsolidity')
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index 5ffdf6af..d8e95ad2 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -2988,6 +2988,99 @@ BOOST_AUTO_TEST_CASE(gas_and_value_basic)
BOOST_REQUIRE(callContractFunction("checkState()") == encodeArgs(false, 20 - 5));
}
+BOOST_AUTO_TEST_CASE(gasleft_decrease)
+{
+ char const* sourceCode = R"(
+ contract C {
+ uint v;
+ function f() public returns (bool) {
+ uint startGas = gasleft();
+ v++;
+ assert(startGas > gasleft());
+ return true;
+ }
+ function g() public returns (bool) {
+ uint startGas = gasleft();
+ assert(startGas > gasleft());
+ return true;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ ABI_CHECK(callContractFunction("f()"), encodeArgs(true));
+ ABI_CHECK(callContractFunction("g()"), encodeArgs(true));
+}
+
+BOOST_AUTO_TEST_CASE(gaslimit)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() public returns (uint) {
+ return block.gaslimit;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ ABI_CHECK(callContractFunction("f()"), encodeArgs(gasLimit()));
+}
+
+BOOST_AUTO_TEST_CASE(gasprice)
+{
+ char const* sourceCode = R"(
+ contract C {
+ function f() public returns (uint) {
+ return tx.gasprice;
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ ABI_CHECK(callContractFunction("f()"), encodeArgs(gasPrice()));
+}
+
+BOOST_AUTO_TEST_CASE(blockhash)
+{
+ char const* sourceCode = R"(
+ contract C {
+ uint256 counter;
+ function g() public returns (bool) { counter++; return true; }
+ function f() public returns (bytes32[] memory r) {
+ r = new bytes32[](259);
+ for (uint i = 0; i < 259; i++)
+ r[i] = blockhash(block.number - 257 + i);
+ }
+ }
+ )";
+ compileAndRun(sourceCode);
+ // generate a sufficient amount of blocks
+ while (blockNumber() < u256(255))
+ ABI_CHECK(callContractFunction("g()"), encodeArgs(true));
+
+ vector<u256> hashes;
+ // currently the test only works for pre-constantinople
+ if (Options::get().evmVersion() < EVMVersion::constantinople())
+ {
+ // ``blockhash()`` is only valid for the last 256 blocks, otherwise zero
+ hashes.emplace_back(0);
+ for (u256 i = blockNumber() - u256(255); i <= blockNumber(); i++)
+ hashes.emplace_back(blockHash(i));
+ // the current block hash is not yet known at execution time and therefore zero
+ hashes.emplace_back(0);
+ // future block hashes are zero
+ hashes.emplace_back(0);
+ }
+ else
+ // TODO: Starting from constantinople blockhash always seems to return zero.
+ // The blockhash contract introduced in EIP96 seems to break in our setup of
+ // aleth (setting the constantinople fork block to zero and resetting the chain
+ // to block zero before each test run). Pre-deploying the blockchain contract
+ // during genesis seems to help, but currently causes problems with other tests.
+ // Set the expectation to zero for now, so that this test tracks changes in this
+ // behavior.
+ hashes.assign(259, 0);
+
+ ABI_CHECK(callContractFunction("f()"), encodeDyn(hashes));
+}
+
BOOST_AUTO_TEST_CASE(value_complex)
{
char const* sourceCode = R"(