aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGav Wood <i@gavwood.com>2015-01-09 22:19:01 +0800
committerGav Wood <i@gavwood.com>2015-01-09 22:19:01 +0800
commit43d79cc7305524bb8503e660cfd43c121706e1ed (patch)
treef25900a00a0a1b20998ec91d5da9730b3b78deab
parent875ec9d47178637bc8608204a8520c1487c064e4 (diff)
parenta60d82d12ce84c87ae28e16dcd5002643f49637b (diff)
downloaddexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.tar
dexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.tar.gz
dexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.tar.bz2
dexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.tar.lz
dexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.tar.xz
dexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.tar.zst
dexon-solidity-43d79cc7305524bb8503e660cfd43c121706e1ed.zip
Merge branch 'develop' of github.com:ethereum/cpp-ethereum into develop
Conflicts: test/SolidityOptimizer.cpp
-rw-r--r--AST.cpp17
-rw-r--r--AST.h5
-rw-r--r--CMakeLists.txt1
-rw-r--r--Compiler.cpp44
-rw-r--r--CompilerUtils.cpp2
-rw-r--r--CompilerUtils.h4
-rw-r--r--ExpressionCompiler.cpp17
-rw-r--r--InterfaceHandler.cpp25
-rw-r--r--Types.cpp18
-rw-r--r--Types.h2
10 files changed, 70 insertions, 65 deletions
diff --git a/AST.cpp b/AST.cpp
index 79ac314c..d171006a 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -27,6 +27,8 @@
#include <libsolidity/Exceptions.h>
#include <libsolidity/AST_accept.h>
+#include <libdevcrypto/SHA3.h>
+
using namespace std;
namespace dev
@@ -50,18 +52,17 @@ void ContractDefinition::checkTypeRequirements()
function->checkTypeRequirements();
}
-vector<FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() const
+map<FixedHash<4>, FunctionDefinition const*> ContractDefinition::getInterfaceFunctions() const
{
- vector<FunctionDefinition const*> exportedFunctions;
+ map<FixedHash<4>, FunctionDefinition const*> exportedFunctions;
for (ASTPointer<FunctionDefinition> const& f: m_definedFunctions)
if (f->isPublic() && f->getName() != getName())
- exportedFunctions.push_back(f.get());
- auto compareNames = [](FunctionDefinition const* _a, FunctionDefinition const* _b)
- {
- return _a->getName().compare(_b->getName()) < 0;
- };
+ {
+ FixedHash<4> hash(dev::sha3(f->getCanonicalSignature()));
+ auto res = exportedFunctions.insert(std::make_pair(hash,f.get()));
+ solAssert(res.second, "Hash collision at Function Definition Hash calculation");
+ }
- sort(exportedFunctions.begin(), exportedFunctions.end(), compareNames);
return exportedFunctions;
}
diff --git a/AST.h b/AST.h
index 8493d432..95121d4c 100644
--- a/AST.h
+++ b/AST.h
@@ -183,8 +183,9 @@ public:
/// Can contain a nullptr in which case indicates absence of documentation
ASTPointer<ASTString> const& getDocumentation() const { return m_documentation; }
- /// Returns the functions that make up the calling interface in the intended order.
- std::vector<FunctionDefinition const*> getInterfaceFunctions() const;
+ /// @returns a map of canonical function signatures to FunctionDefinitions
+ /// as intended for use by the ABI.
+ std::map<FixedHash<4>, FunctionDefinition const*> getInterfaceFunctions() const;
/// Returns the constructor or nullptr if no constructor was specified
FunctionDefinition const* getConstructor() const;
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0a0b62bd..9c0b5077 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,6 +28,7 @@ endif()
target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES})
target_link_libraries(${EXECUTABLE} evmcore)
target_link_libraries(${EXECUTABLE} devcore)
+target_link_libraries(${EXECUTABLE} devcrypto)
install( TARGETS ${EXECUTABLE} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} )
diff --git a/Compiler.cpp b/Compiler.cpp
index 394ae5f8..4e5b7f55 100644
--- a/Compiler.cpp
+++ b/Compiler.cpp
@@ -100,7 +100,7 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor)
{
m_context << u256(argumentSize);
m_context.appendProgramSize();
- m_context << u256(1); // copy it to byte one as expected for ABI calls
+ m_context << u256(CompilerUtils::dataStartOffset); // copy it to byte four as expected for ABI calls
m_context << eth::Instruction::CODECOPY;
appendCalldataUnpacker(_constructor, true);
}
@@ -118,35 +118,27 @@ set<FunctionDefinition const*> Compiler::getFunctionsNeededByConstructor(Functio
void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
{
- vector<FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions();
- vector<eth::AssemblyItem> callDataUnpackerEntryPoints;
-
- if (interfaceFunctions.size() > 255)
- BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 255 public functions for contract."));
-
- // retrieve the first byte of the call data, which determines the called function
- // @todo This code had a jump table in a previous version which was more efficient but also
- // error prone (due to the optimizer and variable length tag addresses)
- m_context << u256(1) << u256(0) // some constants
- << eth::dupInstruction(1) << eth::Instruction::CALLDATALOAD
- << eth::dupInstruction(2) << eth::Instruction::BYTE
- << eth::dupInstruction(2);
-
- // stack here: 1 0 <funid> 0, stack top will be counted up until it matches funid
- for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid)
+ map<FixedHash<4>, FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions();
+ map<FixedHash<4>, const eth::AssemblyItem> callDataUnpackerEntryPoints;
+
+ // retrieve the function signature hash from the calldata
+ m_context << u256(1) << u256(0);
+ CompilerUtils(m_context).loadFromMemory(0, 4, false, true);
+
+ // stack now is: 1 0 <funhash>
+ // for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
+ for (auto const& it: interfaceFunctions)
{
- callDataUnpackerEntryPoints.push_back(m_context.newTag());
- m_context << eth::dupInstruction(2) << eth::dupInstruction(2) << eth::Instruction::EQ;
- m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.back());
- if (funid < interfaceFunctions.size() - 1)
- m_context << eth::dupInstruction(4) << eth::Instruction::ADD;
+ callDataUnpackerEntryPoints.insert(std::make_pair(it.first, m_context.newTag()));
+ m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it.first)) << eth::Instruction::EQ;
+ m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.at(it.first));
}
m_context << eth::Instruction::STOP; // function not found
- for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid)
+ for (auto const& it: interfaceFunctions)
{
- FunctionDefinition const& function = *interfaceFunctions[funid];
- m_context << callDataUnpackerEntryPoints[funid];
+ FunctionDefinition const& function = *it.second;
+ m_context << callDataUnpackerEntryPoints.at(it.first);
eth::AssemblyItem returnTag = m_context.pushNewTag();
appendCalldataUnpacker(function);
m_context.appendJumpTo(m_context.getFunctionEntryLabel(function));
@@ -158,7 +150,7 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory)
{
// We do not check the calldata size, everything is zero-padded.
- unsigned dataOffset = 1;
+ unsigned dataOffset = CompilerUtils::dataStartOffset; // the 4 bytes of the function hash signature
//@todo this can be done more efficiently, saving some CALLDATALOAD calls
for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
{
diff --git a/CompilerUtils.cpp b/CompilerUtils.cpp
index 680e9190..a5254b42 100644
--- a/CompilerUtils.cpp
+++ b/CompilerUtils.cpp
@@ -31,6 +31,8 @@ namespace dev
namespace solidity
{
+const unsigned int CompilerUtils::dataStartOffset = 4;
+
void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, bool _fromCalldata)
{
if (_bytes == 0)
diff --git a/CompilerUtils.h b/CompilerUtils.h
index 928f0e2d..6bd8d315 100644
--- a/CompilerUtils.h
+++ b/CompilerUtils.h
@@ -58,10 +58,14 @@ public:
static unsigned getSizeOnStack(std::vector<T> const& _variables);
static unsigned getSizeOnStack(std::vector<std::shared_ptr<Type const>> const& _variableTypes);
+ /// Bytes we need to the start of call data.
+ /// - The size in bytes of the function (hash) identifier.
+ static const unsigned int dataStartOffset;
private:
CompilerContext& m_context;
};
+
template <class T>
unsigned CompilerUtils::getSizeOnStack(std::vector<T> const& _variables)
{
diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp
index 8e8f492e..cf91a763 100644
--- a/ExpressionCompiler.cpp
+++ b/ExpressionCompiler.cpp
@@ -401,7 +401,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
case Type::Category::CONTRACT:
{
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
- m_context << type.getFunctionIndex(member);
+ m_context << type.getFunctionIdentifier(member);
break;
}
case Type::Category::MAGIC:
@@ -663,7 +663,11 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
{
solAssert(_arguments.size() == _functionType.getParameterTypes().size(), "");
- unsigned dataOffset = _options.bare ? 0 : 1; // reserve one byte for the function index
+ _options.obtainAddress();
+ if (!_options.bare)
+ CompilerUtils(m_context).storeInMemory(0, CompilerUtils::dataStartOffset);
+
+ unsigned dataOffset = _options.bare ? 0 : CompilerUtils::dataStartOffset; // reserve 4 bytes for the function's hash identifier
for (unsigned i = 0; i < _arguments.size(); ++i)
{
_arguments[i]->accept(*this);
@@ -690,12 +694,13 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
_options.obtainValue();
else
m_context << u256(0);
- _options.obtainAddress();
- if (!_options.bare)
- m_context << u256(0) << eth::Instruction::MSTORE8;
+ m_context << eth::dupInstruction(6); //copy contract address
+
m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB
<< eth::Instruction::CALL
- << eth::Instruction::POP; // @todo do not ignore failure indicator
+ << eth::Instruction::POP // @todo do not ignore failure indicator
+ << eth::Instruction::POP; // pop contract address
+
if (retSize > 0)
{
bool const leftAligned = firstType->getCategory() == Type::Category::STRING;
diff --git a/InterfaceHandler.cpp b/InterfaceHandler.cpp
index 06b9824c..4ce6e989 100644
--- a/InterfaceHandler.cpp
+++ b/InterfaceHandler.cpp
@@ -39,7 +39,7 @@ std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinitio
{
Json::Value methods(Json::arrayValue);
- for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions())
+ for (auto const& it: _contractDef.getInterfaceFunctions())
{
Json::Value method;
Json::Value inputs(Json::arrayValue);
@@ -58,10 +58,10 @@ std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinitio
return params;
};
- method["name"] = f->getName();
- method["constant"] = f->isDeclaredConst();
- method["inputs"] = populateParameters(f->getParameters());
- method["outputs"] = populateParameters(f->getReturnParameters());
+ method["name"] = it.second->getName();
+ method["constant"] = it.second->isDeclaredConst();
+ method["inputs"] = populateParameters(it.second->getParameters());
+ method["outputs"] = populateParameters(it.second->getReturnParameters());
methods.append(method);
}
return std::unique_ptr<std::string>(new std::string(m_writer.write(methods)));
@@ -70,8 +70,9 @@ std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinitio
unique_ptr<string> InterfaceHandler::getABISolidityInterface(ContractDefinition const& _contractDef)
{
string ret = "contract " + _contractDef.getName() + "{";
- for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions())
+ for (auto const& it: _contractDef.getInterfaceFunctions())
{
+ FunctionDefinition const* f = it.second;
auto populateParameters = [](vector<ASTPointer<VariableDeclaration>> const& _vars)
{
string r = "";
@@ -94,10 +95,10 @@ std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(ContractDefi
Json::Value doc;
Json::Value methods(Json::objectValue);
- for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions())
+ for (auto const& it: _contractDef.getInterfaceFunctions())
{
Json::Value user;
- auto strPtr = f->getDocumentation();
+ auto strPtr = it.second->getDocumentation();
if (strPtr)
{
resetUser();
@@ -105,7 +106,7 @@ std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(ContractDefi
if (!m_notice.empty())
{// since @notice is the only user tag if missing function should not appear
user["notice"] = Json::Value(m_notice);
- methods[f->getName()] = user;
+ methods[it.second->getName()] = user;
}
}
}
@@ -135,10 +136,10 @@ std::unique_ptr<std::string> InterfaceHandler::getDevDocumentation(ContractDefin
doc["title"] = m_title;
}
- for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions())
+ for (auto const& it: _contractDef.getInterfaceFunctions())
{
Json::Value method;
- auto strPtr = f->getDocumentation();
+ auto strPtr = it.second->getDocumentation();
if (strPtr)
{
resetDev();
@@ -161,7 +162,7 @@ std::unique_ptr<std::string> InterfaceHandler::getDevDocumentation(ContractDefin
method["return"] = m_return;
if (!method.empty()) // add the function, only if we have any documentation to add
- methods[f->getName()] = method;
+ methods[it.second->getName()] = method;
}
}
doc["methods"] = methods;
diff --git a/Types.cpp b/Types.cpp
index dcfc704a..da042edb 100644
--- a/Types.cpp
+++ b/Types.cpp
@@ -460,8 +460,8 @@ MemberList const& ContractType::getMembers() const
if (!m_members)
{
map<string, shared_ptr<Type const>> members;
- for (FunctionDefinition const* function: m_contract.getInterfaceFunctions())
- members[function->getName()] = make_shared<FunctionType>(*function, false);
+ for (auto const& it: m_contract.getInterfaceFunctions())
+ members[it.second->getName()] = make_shared<FunctionType>(*it.second, false);
m_members.reset(new MemberList(members));
}
return *m_members;
@@ -480,15 +480,13 @@ shared_ptr<FunctionType const> const& ContractType::getConstructorType() const
return m_constructorType;
}
-unsigned ContractType::getFunctionIndex(string const& _functionName) const
+u256 ContractType::getFunctionIdentifier(string const& _functionName) const
{
- unsigned index = 0;
- for (FunctionDefinition const* function: m_contract.getInterfaceFunctions())
- {
- if (function->getName() == _functionName)
- return index;
- ++index;
- }
+ auto interfaceFunctions = m_contract.getInterfaceFunctions();
+ for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
+ if (it->second->getName() == _functionName)
+ return FixedHash<4>::Arith(it->first);
+
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested."));
}
diff --git a/Types.h b/Types.h
index 828c53b9..8c800fa0 100644
--- a/Types.h
+++ b/Types.h
@@ -291,7 +291,7 @@ public:
/// is not used, as this type cannot be the type of a variable or expression.
std::shared_ptr<FunctionType const> const& getConstructorType() const;
- unsigned getFunctionIndex(std::string const& _functionName) const;
+ u256 getFunctionIdentifier(std::string const& _functionName) const;
private:
ContractDefinition const& m_contract;