aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/GasMeter.cpp
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2015-05-22 16:48:54 +0800
committerchriseth <c@ethdev.com>2015-05-22 22:12:40 +0800
commit964bcd6b857c48d084ac2cfe0cd395939da570f0 (patch)
tree90e923d434a40a11a8df4a52d3c5a1da7b58d616 /libsolidity/GasMeter.cpp
parentec76302b85b2715e19e02317d90ee4a6d63e8470 (diff)
downloaddexon-solidity-964bcd6b857c48d084ac2cfe0cd395939da570f0.tar.gz
dexon-solidity-964bcd6b857c48d084ac2cfe0cd395939da570f0.tar.zst
dexon-solidity-964bcd6b857c48d084ac2cfe0cd395939da570f0.zip
Functional gas estimator.
Diffstat (limited to 'libsolidity/GasMeter.cpp')
-rw-r--r--libsolidity/GasMeter.cpp41
1 files 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 <libevmasm/KnownState.h>
#include <libevmasm/PathGasMeter.h>
#include <libsolidity/AST.h>
-#include <libsolidity/StructuralGasEstimator.h>
+#include <libsolidity/GasEstimator.h>
#include <libsolidity/SourceReferenceFormatter.h>
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<ASTNode const*>({&sourceUnit})),
+ m_gasCosts = GasEstimator::breakToStatementLevel(
+ GasEstimator::structuralEstimation(*items, vector<ASTNode const*>({&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<bytes> _argumentVariants)
{
u256 gasUsed = 0;
@@ -80,12 +81,10 @@ public:
gasUsed = max(gasUsed, m_gasUsed);
}
- auto state = make_shared<KnownState>();
- //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<bytes>{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<bytes>{encodeArgs(2), encodeArgs(8)});
+ testRunTimeGas("g(uint256)", vector<bytes>{encodeArgs(2)});
+}
+
BOOST_AUTO_TEST_SUITE_END()
}