mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Path gas meter.
This commit is contained in:
parent
ba2b77bb9a
commit
ec76302b85
@ -23,6 +23,7 @@
|
|||||||
#include <test/libsolidity/solidityExecutionFramework.h>
|
#include <test/libsolidity/solidityExecutionFramework.h>
|
||||||
#include <libevmasm/GasMeter.h>
|
#include <libevmasm/GasMeter.h>
|
||||||
#include <libevmasm/KnownState.h>
|
#include <libevmasm/KnownState.h>
|
||||||
|
#include <libevmasm/PathGasMeter.h>
|
||||||
#include <libsolidity/AST.h>
|
#include <libsolidity/AST.h>
|
||||||
#include <libsolidity/StructuralGasEstimator.h>
|
#include <libsolidity/StructuralGasEstimator.h>
|
||||||
#include <libsolidity/SourceReferenceFormatter.h>
|
#include <libsolidity/SourceReferenceFormatter.h>
|
||||||
@ -57,20 +58,38 @@ public:
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void testCreationTimeGas(string const& _sourceCode, string const& _contractName = "")
|
void testCreationTimeGas(string const& _sourceCode)
|
||||||
{
|
{
|
||||||
compileAndRun(_sourceCode);
|
compileAndRun(_sourceCode);
|
||||||
auto state = make_shared<KnownState>();
|
auto state = make_shared<KnownState>();
|
||||||
GasMeter meter(state);
|
PathGasMeter meter(*m_compiler.getAssemblyItems());
|
||||||
GasMeter::GasConsumption gas;
|
GasMeter::GasConsumption gas = meter.estimateMax(0, state);
|
||||||
for (AssemblyItem const& item: *m_compiler.getAssemblyItems(_contractName))
|
u256 bytecodeSize(m_compiler.getRuntimeBytecode().size());
|
||||||
gas += meter.estimateMax(item);
|
|
||||||
u256 bytecodeSize(m_compiler.getRuntimeBytecode(_contractName).size());
|
|
||||||
gas += bytecodeSize * c_createDataGas;
|
gas += bytecodeSize * c_createDataGas;
|
||||||
BOOST_REQUIRE(!gas.isInfinite);
|
BOOST_REQUIRE(!gas.isInfinite);
|
||||||
BOOST_CHECK(gas.value == m_gasUsed);
|
BOOST_CHECK(gas.value == m_gasUsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void testRunTimeGas(std::string const& _sig, vector<bytes> _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<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;
|
||||||
|
BOOST_REQUIRE(!gas.isInfinite);
|
||||||
|
BOOST_CHECK(gas.value == m_gasUsed);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts;
|
map<ASTNode const*, eth::GasMeter::GasConsumption> m_gasCosts;
|
||||||
};
|
};
|
||||||
@ -149,6 +168,45 @@ BOOST_AUTO_TEST_CASE(updating_store)
|
|||||||
testCreationTimeGas(sourceCode);
|
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<bytes>{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<bytes>{encodeArgs(2), encodeArgs(8)});
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user