mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Functional gas estimator.
This commit is contained in:
parent
ec76302b85
commit
964bcd6b85
@ -25,7 +25,7 @@
|
|||||||
#include <libevmasm/KnownState.h>
|
#include <libevmasm/KnownState.h>
|
||||||
#include <libevmasm/PathGasMeter.h>
|
#include <libevmasm/PathGasMeter.h>
|
||||||
#include <libsolidity/AST.h>
|
#include <libsolidity/AST.h>
|
||||||
#include <libsolidity/StructuralGasEstimator.h>
|
#include <libsolidity/GasEstimator.h>
|
||||||
#include <libsolidity/SourceReferenceFormatter.h>
|
#include <libsolidity/SourceReferenceFormatter.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -48,12 +48,11 @@ public:
|
|||||||
m_compiler.setSource(_sourceCode);
|
m_compiler.setSource(_sourceCode);
|
||||||
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
|
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
|
||||||
|
|
||||||
StructuralGasEstimator estimator;
|
|
||||||
AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems("");
|
AssemblyItems const* items = m_compiler.getRuntimeAssemblyItems("");
|
||||||
ASTNode const& sourceUnit = m_compiler.getAST();
|
ASTNode const& sourceUnit = m_compiler.getAST();
|
||||||
BOOST_REQUIRE(items != nullptr);
|
BOOST_REQUIRE(items != nullptr);
|
||||||
m_gasCosts = estimator.breakToStatementLevel(
|
m_gasCosts = GasEstimator::breakToStatementLevel(
|
||||||
estimator.performEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
|
GasEstimator::structuralEstimation(*items, vector<ASTNode const*>({&sourceUnit})),
|
||||||
{&sourceUnit}
|
{&sourceUnit}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -70,6 +69,8 @@ public:
|
|||||||
BOOST_CHECK(gas.value == m_gasUsed);
|
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)
|
void testRunTimeGas(std::string const& _sig, vector<bytes> _argumentVariants)
|
||||||
{
|
{
|
||||||
u256 gasUsed = 0;
|
u256 gasUsed = 0;
|
||||||
@ -80,12 +81,10 @@ public:
|
|||||||
gasUsed = max(gasUsed, m_gasUsed);
|
gasUsed = max(gasUsed, m_gasUsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto state = make_shared<KnownState>();
|
GasMeter::GasConsumption gas = GasEstimator::functionalEstimation(
|
||||||
//TODO modify state to include function hash in calldata
|
*m_compiler.getRuntimeAssemblyItems(),
|
||||||
PathGasMeter meter(*m_compiler.getRuntimeAssemblyItems());
|
_sig
|
||||||
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
|
);
|
||||||
cout << "VM: " << gasUsed << endl;
|
|
||||||
cout << "est: " << gas << endl;
|
|
||||||
BOOST_REQUIRE(!gas.isInfinite);
|
BOOST_REQUIRE(!gas.isInfinite);
|
||||||
BOOST_CHECK(gas.value == m_gasUsed);
|
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)});
|
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()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user