From ec76302b85b2715e19e02317d90ee4a6d63e8470 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 22 May 2015 09:33:57 +0200 Subject: Path gas meter. --- libsolidity/GasMeter.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/libsolidity/GasMeter.cpp b/libsolidity/GasMeter.cpp index 43eb3f95..2508399f 100644 --- a/libsolidity/GasMeter.cpp +++ b/libsolidity/GasMeter.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -57,20 +58,38 @@ public: ); } - void testCreationTimeGas(string const& _sourceCode, string const& _contractName = "") + void testCreationTimeGas(string const& _sourceCode) { compileAndRun(_sourceCode); auto state = make_shared(); - GasMeter meter(state); - GasMeter::GasConsumption gas; - for (AssemblyItem const& item: *m_compiler.getAssemblyItems(_contractName)) - gas += meter.estimateMax(item); - u256 bytecodeSize(m_compiler.getRuntimeBytecode(_contractName).size()); + PathGasMeter meter(*m_compiler.getAssemblyItems()); + GasMeter::GasConsumption gas = meter.estimateMax(0, state); + u256 bytecodeSize(m_compiler.getRuntimeBytecode().size()); gas += bytecodeSize * c_createDataGas; BOOST_REQUIRE(!gas.isInfinite); BOOST_CHECK(gas.value == m_gasUsed); } + void testRunTimeGas(std::string const& _sig, vector _argumentVariants) + { + u256 gasUsed = 0; + FixedHash<4> hash(dev::sha3(_sig)); + for (bytes const& arguments: _argumentVariants) + { + sendMessage(hash.asBytes() + arguments, false, 0); + gasUsed = max(gasUsed, m_gasUsed); + } + + auto state = make_shared(); + //TODO modify state to include function hash in calldata + PathGasMeter meter(*m_compiler.getRuntimeAssemblyItems()); + GasMeter::GasConsumption gas = meter.estimateMax(0, state); + cout << "VM: " << gasUsed << endl; + cout << "est: " << gas << endl; + BOOST_REQUIRE(!gas.isInfinite); + BOOST_CHECK(gas.value == m_gasUsed); + } + protected: map m_gasCosts; }; @@ -149,6 +168,45 @@ BOOST_AUTO_TEST_CASE(updating_store) testCreationTimeGas(sourceCode); } +BOOST_AUTO_TEST_CASE(branches) +{ + char const* sourceCode = R"( + contract test { + uint data; + uint data2; + function f(uint x) { + if (x > 7) + data2 = 1; + else + data = 1; + } + } + )"; + testCreationTimeGas(sourceCode); + testRunTimeGas("f(uint256)", vector{encodeArgs(2), encodeArgs(8)}); +} + +BOOST_AUTO_TEST_CASE(function_calls) +{ + char const* sourceCode = R"( + contract test { + uint data; + uint data2; + function f(uint x) { + if (x > 7) + data2 = g(x**8) + 1; + else + data = 1; + } + function g(uint x) internal returns (uint) { + return data2; + } + } + )"; + testCreationTimeGas(sourceCode); + testRunTimeGas("f(uint256)", vector{encodeArgs(2), encodeArgs(8)}); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit From 964bcd6b857c48d084ac2cfe0cd395939da570f0 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 22 May 2015 10:48:54 +0200 Subject: Functional gas estimator. --- libsolidity/GasMeter.cpp | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/libsolidity/GasMeter.cpp b/libsolidity/GasMeter.cpp index 2508399f..c09849c0 100644 --- a/libsolidity/GasMeter.cpp +++ b/libsolidity/GasMeter.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include using namespace std; @@ -48,12 +48,11 @@ public: m_compiler.setSource(_sourceCode); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed"); - StructuralGasEstimator estimator; AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems(""); ASTNode const& sourceUnit = m_compiler.getAST(); BOOST_REQUIRE(items != nullptr); - m_gasCosts = estimator.breakToStatementLevel( - estimator.performEstimation(*items, vector({&sourceUnit})), + m_gasCosts = GasEstimator::breakToStatementLevel( + GasEstimator::structuralEstimation(*items, vector({&sourceUnit})), {&sourceUnit} ); } @@ -70,6 +69,8 @@ public: BOOST_CHECK(gas.value == m_gasUsed); } + /// Compares the gas computed by PathGasMeter for the given signature (but unknown arguments) + /// against the actual gas usage computed by the VM on the given set of argument variants. void testRunTimeGas(std::string const& _sig, vector _argumentVariants) { u256 gasUsed = 0; @@ -80,12 +81,10 @@ public: gasUsed = max(gasUsed, m_gasUsed); } - auto state = make_shared(); - //TODO modify state to include function hash in calldata - PathGasMeter meter(*m_compiler.getRuntimeAssemblyItems()); - GasMeter::GasConsumption gas = meter.estimateMax(0, state); - cout << "VM: " << gasUsed << endl; - cout << "est: " << gas << endl; + GasMeter::GasConsumption gas = GasEstimator::functionalEstimation( + *m_compiler.getRuntimeAssemblyItems(), + _sig + ); BOOST_REQUIRE(!gas.isInfinite); BOOST_CHECK(gas.value == m_gasUsed); } @@ -207,6 +206,28 @@ BOOST_AUTO_TEST_CASE(function_calls) testRunTimeGas("f(uint256)", vector{encodeArgs(2), encodeArgs(8)}); } +BOOST_AUTO_TEST_CASE(multiple_external_functions) +{ + char const* sourceCode = R"( + contract test { + uint data; + uint data2; + function f(uint x) { + if (x > 7) + data2 = g(x**8) + 1; + else + data = 1; + } + function g(uint x) returns (uint) { + return data2; + } + } + )"; + testCreationTimeGas(sourceCode); + testRunTimeGas("f(uint256)", vector{encodeArgs(2), encodeArgs(8)}); + testRunTimeGas("g(uint256)", vector{encodeArgs(2)}); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit From fbc331bd354ba0d0703ff8923e3c8dbd14f7a246 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 26 May 2015 11:29:41 +0200 Subject: Removed redundant std. --- libsolidity/GasMeter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/GasMeter.cpp b/libsolidity/GasMeter.cpp index c09849c0..5f442654 100644 --- a/libsolidity/GasMeter.cpp +++ b/libsolidity/GasMeter.cpp @@ -71,7 +71,7 @@ public: /// Compares the gas computed by PathGasMeter for the given signature (but unknown arguments) /// against the actual gas usage computed by the VM on the given set of argument variants. - void testRunTimeGas(std::string const& _sig, vector _argumentVariants) + void testRunTimeGas(string const& _sig, vector _argumentVariants) { u256 gasUsed = 0; FixedHash<4> hash(dev::sha3(_sig)); -- cgit