Browse Source

Commandline interface for gas estimation.

cl-refactor
chriseth 10 years ago
parent
commit
a09bb999cb
  1. 2
      libevmasm/GasMeter.h
  2. 32
      solc/CommandLineInterface.cpp
  3. 1
      solc/CommandLineInterface.h

2
libevmasm/GasMeter.h

@ -86,7 +86,7 @@ private:
inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption) inline std::ostream& operator<<(std::ostream& _str, GasMeter::GasConsumption const& _consumption)
{ {
if (_consumption.isInfinite) if (_consumption.isInfinite)
return _str << "inf"; return _str << "[???]";
else else
return _str << std::dec << _consumption.value; return _str << std::dec << _consumption.value;
} }

32
solc/CommandLineInterface.cpp

@ -34,6 +34,7 @@
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libdevcore/CommonIO.h> #include <libdevcore/CommonIO.h>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include <libevmcore/Params.h>
#include <libsolidity/Scanner.h> #include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/ASTPrinter.h> #include <libsolidity/ASTPrinter.h>
@ -55,6 +56,7 @@ namespace solidity
static string const g_argAbiStr = "json-abi"; static string const g_argAbiStr = "json-abi";
static string const g_argSolAbiStr = "sol-abi"; static string const g_argSolAbiStr = "sol-abi";
static string const g_argSignatureHashes = "hashes"; static string const g_argSignatureHashes = "hashes";
static string const g_argGas = "gas";
static string const g_argAsmStr = "asm"; static string const g_argAsmStr = "asm";
static string const g_argAsmJsonStr = "asm-json"; static string const g_argAsmJsonStr = "asm-json";
static string const g_argAstStr = "ast"; static string const g_argAstStr = "ast";
@ -94,6 +96,7 @@ static bool needsHumanTargetedStdout(po::variables_map const& _args)
{ {
return return
_args.count(g_argGas) ||
humanTargetedStdout(_args, g_argAbiStr) || humanTargetedStdout(_args, g_argAbiStr) ||
humanTargetedStdout(_args, g_argSolAbiStr) || humanTargetedStdout(_args, g_argSolAbiStr) ||
humanTargetedStdout(_args, g_argSignatureHashes) || humanTargetedStdout(_args, g_argSignatureHashes) ||
@ -245,6 +248,30 @@ void CommandLineInterface::handleMeta(DocumentationType _type, string const& _co
} }
} }
void CommandLineInterface::handleGasEstimation(string const& _contract)
{
using Gas = GasEstimator::GasConsumption;
if (!m_compiler->getAssemblyItems(_contract) && !m_compiler->getRuntimeAssemblyItems(_contract))
return;
cout << "Gas estimation:" << endl;
if (eth::AssemblyItems const* items = m_compiler->getAssemblyItems(_contract))
{
Gas gas = GasEstimator::functionalEstimation(*items);
u256 bytecodeSize(m_compiler->getRuntimeBytecode(_contract).size());
cout << "[construction]:\t";
cout << gas << " + " << (bytecodeSize * eth::c_createDataGas) << " = ";
gas += bytecodeSize * eth::c_createDataGas;
cout << gas << endl;
}
if (eth::AssemblyItems const* items = m_compiler->getRuntimeAssemblyItems(_contract))
for (auto it: m_compiler->getContractDefinition(_contract).getInterfaceFunctions())
{
string sig = it.second->externalSignature();
GasEstimator::GasConsumption gas = GasEstimator::functionalEstimation(*items, sig);
cout << sig << ":\t" << gas << endl;
}
}
bool CommandLineInterface::parseArguments(int argc, char** argv) bool CommandLineInterface::parseArguments(int argc, char** argv)
{ {
// Declare the supported options. // Declare the supported options.
@ -278,6 +305,8 @@ bool CommandLineInterface::parseArguments(int argc, char** argv)
"Request to output the contract's Solidity ABI interface.") "Request to output the contract's Solidity ABI interface.")
(g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argSignatureHashes.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's functions' signature hashes.") "Request to output the contract's functions' signature hashes.")
(g_argGas.c_str(),
"Request to output an estimate for each function's maximal gas usage.")
(g_argNatspecUserStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argNatspecUserStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
"Request to output the contract's Natspec user documentation.") "Request to output the contract's Natspec user documentation.")
(g_argNatspecDevStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"), (g_argNatspecDevStr.c_str(), po::value<OutputType>()->value_name("stdout|file|both"),
@ -553,6 +582,9 @@ void CommandLineInterface::actOnInput()
} }
} }
if (m_args.count(g_argGas))
handleGasEstimation(contract);
handleBytecode(contract); handleBytecode(contract);
handleSignatureHashes(contract); handleSignatureHashes(contract);
handleMeta(DocumentationType::ABIInterface, contract); handleMeta(DocumentationType::ABIInterface, contract);

1
solc/CommandLineInterface.h

@ -61,6 +61,7 @@ private:
void handleSignatureHashes(std::string const& _contract); void handleSignatureHashes(std::string const& _contract);
void handleMeta(DocumentationType _type, void handleMeta(DocumentationType _type,
std::string const& _contract); std::string const& _contract);
void handleGasEstimation(std::string const& _contract);
/// Compiler arguments variable map /// Compiler arguments variable map
boost::program_options::variables_map m_args; boost::program_options::variables_map m_args;

Loading…
Cancel
Save