aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsubtly <subtly@users.noreply.github.com>2014-12-08 21:44:07 +0800
committersubtly <subtly@users.noreply.github.com>2014-12-08 21:44:07 +0800
commit8aba43c5e06a03d6f692af3ddf6e783d5e3a6068 (patch)
tree6816e6e248187f97208e7ff92340ca7cdbb66410
parent6475a4ffb6116bfa9fdf8b2db5e1b34d5417de6c (diff)
parenta78aff544c85453a68fdbaa3ab789e19a67c3a76 (diff)
downloaddexon-solidity-8aba43c5e06a03d6f692af3ddf6e783d5e3a6068.tar.gz
dexon-solidity-8aba43c5e06a03d6f692af3ddf6e783d5e3a6068.tar.zst
dexon-solidity-8aba43c5e06a03d6f692af3ddf6e783d5e3a6068.zip
Merge branch 'develop' into network
-rw-r--r--solidityCompiler.cpp25
-rw-r--r--solidityEndToEndTest.cpp135
-rw-r--r--solidityExpressionCompiler.cpp47
-rw-r--r--solidityJSONInterfaceTest.cpp2
-rw-r--r--solidityNameAndTypeResolution.cpp8
-rw-r--r--solidityNatspecJSON.cpp4
-rw-r--r--solidityParser.cpp55
7 files changed, 235 insertions, 41 deletions
diff --git a/solidityCompiler.cpp b/solidityCompiler.cpp
index 3e86919a..9862cba8 100644
--- a/solidityCompiler.cpp
+++ b/solidityCompiler.cpp
@@ -46,16 +46,23 @@ namespace
bytes compileContract(const string& _sourceCode)
{
Parser parser;
- ASTPointer<ContractDefinition> contract;
- BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
+ ASTPointer<SourceUnit> sourceUnit;
+ BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
NameAndTypeResolver resolver({});
- BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
-
- Compiler compiler;
- compiler.compileContract(*contract, {});
- // debug
- //compiler.streamAssembly(cout);
- return compiler.getAssembledBytecode();
+ resolver.registerDeclarations(*sourceUnit);
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
+
+ Compiler compiler;
+ compiler.compileContract(*contract, {});
+ // debug
+ //compiler.streamAssembly(cout);
+ return compiler.getAssembledBytecode();
+ }
+ BOOST_FAIL("No contract found in source.");
+ return bytes();
}
/// Checks that @a _compiledCode is present starting from offset @a _offset in @a _expectation.
diff --git a/solidityEndToEndTest.cpp b/solidityEndToEndTest.cpp
index 8b25d403..9e02438e 100644
--- a/solidityEndToEndTest.cpp
+++ b/solidityEndToEndTest.cpp
@@ -47,9 +47,11 @@ class ExecutionFramework
public:
ExecutionFramework() { g_logVerbosity = 0; }
- bytes const& compileAndRun(string const& _sourceCode, u256 const& _value = 0)
+ bytes const& compileAndRun(string const& _sourceCode, u256 const& _value = 0, string const& _contractName = "")
{
- bytes code = dev::solidity::CompilerStack::staticCompile(_sourceCode);
+ dev::solidity::CompilerStack compiler;
+ compiler.compile(_sourceCode);
+ bytes code = compiler.getBytecode(_contractName);
sendMessage(code, true, _value);
BOOST_REQUIRE(!m_output.empty());
return m_output;
@@ -115,6 +117,7 @@ private:
void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0)
{
+ m_state.addBalance(m_sender, _value); // just in case
eth::Executive executive(m_state);
eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec())
: eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec());
@@ -127,7 +130,7 @@ private:
catch (...) {}
if (_isCreation)
{
- BOOST_REQUIRE(!executive.create(Address(), _value, m_gasPrice, m_gas, &_data, Address()));
+ BOOST_REQUIRE(!executive.create(m_sender, _value, m_gasPrice, m_gas, &_data, m_sender));
m_contractAddress = executive.newAddress();
BOOST_REQUIRE(m_contractAddress);
BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
@@ -135,14 +138,16 @@ private:
else
{
BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
- BOOST_REQUIRE(!executive.call(m_contractAddress, Address(), _value, m_gasPrice, &_data, m_gas, Address()));
+ BOOST_REQUIRE(!executive.call(m_contractAddress, m_sender, _value, m_gasPrice, &_data, m_gas, m_sender));
}
BOOST_REQUIRE(executive.go());
+ m_state.noteSending(m_sender);
executive.finalize();
m_output = executive.out().toVector();
}
protected:
+ Address m_sender;
Address m_contractAddress;
eth::State m_state;
u256 const m_gasPrice = 100 * eth::szabo;
@@ -898,6 +903,128 @@ BOOST_AUTO_TEST_CASE(ecrecover)
BOOST_CHECK(callContractFunction(0, h, v, r, s) == toBigEndian(addr));
}
+BOOST_AUTO_TEST_CASE(inter_contract_calls)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function multiply(uint a, uint b) returns (uint c) {
+ return a * b;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, uint b) returns (uint c) {
+ return h.multiply(a, b);
+ }
+ function getHelper() returns (address haddress) {
+ return address(h);
+ }
+ function setHelper(address haddress) {
+ h = Helper(haddress);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b));
+}
+
+BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function sel(uint a, bool select, uint b) returns (uint c) {
+ if (select) return a; else return b;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, bool select, uint b) returns (uint c) {
+ return h.sel(a, select, b) * 3;
+ }
+ function getHelper() returns (address haddress) {
+ return address(h);
+ }
+ function setHelper(address haddress) {
+ h = Helper(haddress);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction(0, a, true, b) == toBigEndian(a * 3));
+ BOOST_REQUIRE(callContractFunction(0, a, false, b) == toBigEndian(b * 3));
+}
+
+BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function getAddress() returns (address addr) {
+ return address(this);
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper() returns (address addr) {
+ return h.getAddress();
+ }
+ function getHelper() returns (address addr) {
+ return address(h);
+ }
+ function setHelper(address addr) {
+ h = Helper(addr);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress));
+ BOOST_REQUIRE(callContractFunction(0) == toBigEndian(helperAddress));
+}
+
+BOOST_AUTO_TEST_CASE(calls_to_this)
+{
+ char const* sourceCode = R"(
+ contract Helper {
+ function invoke(uint a, uint b) returns (uint c) {
+ return this.multiply(a, b, 10);
+ }
+ function multiply(uint a, uint b, uint8 c) returns (uint ret) {
+ return a * b + c;
+ }
+ }
+ contract Main {
+ Helper h;
+ function callHelper(uint a, uint b) returns (uint ret) {
+ return h.invoke(a, b);
+ }
+ function getHelper() returns (address addr) {
+ return address(h);
+ }
+ function setHelper(address addr) {
+ h = Helper(addr);
+ }
+ })";
+ compileAndRun(sourceCode, 0, "Helper");
+ u160 const helperAddress = m_contractAddress;
+ compileAndRun(sourceCode, 0, "Main");
+ BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes());
+ BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress));
+ u256 a(3456789);
+ u256 b("0x282837623374623234aa74");
+ BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 10));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}
diff --git a/solidityExpressionCompiler.cpp b/solidityExpressionCompiler.cpp
index 6ea66bad..486b46eb 100644
--- a/solidityExpressionCompiler.cpp
+++ b/solidityExpressionCompiler.cpp
@@ -86,27 +86,34 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
vector<vector<string>> _localVariables = {})
{
Parser parser;
- ASTPointer<ContractDefinition> contract;
- BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
+ ASTPointer<SourceUnit> sourceUnit;
+ BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
NameAndTypeResolver resolver({});
- BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
- FirstExpressionExtractor extractor(*contract);
- BOOST_REQUIRE(extractor.getExpression() != nullptr);
-
- CompilerContext context;
- for (vector<string> const& function: _functions)
- context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
- for (vector<string> const& variable: _localVariables)
- context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)));
-
- ExpressionCompiler::compileExpression(context, *extractor.getExpression());
-
- for (vector<string> const& function: _functions)
- context << context.getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
- bytes instructions = context.getAssembledBytecode();
- // debug
- // cout << eth::disassemble(instructions) << endl;
- return instructions;
+ resolver.registerDeclarations(*sourceUnit);
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ {
+ BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
+ FirstExpressionExtractor extractor(*contract);
+ BOOST_REQUIRE(extractor.getExpression() != nullptr);
+
+ CompilerContext context;
+ for (vector<string> const& function: _functions)
+ context.addFunction(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
+ for (vector<string> const& variable: _localVariables)
+ context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)));
+
+ ExpressionCompiler::compileExpression(context, *extractor.getExpression());
+
+ for (vector<string> const& function: _functions)
+ context << context.getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver)));
+ bytes instructions = context.getAssembledBytecode();
+ // debug
+ // cout << eth::disassemble(instructions) << endl;
+ return instructions;
+ }
+ BOOST_FAIL("No contract found in source.");
+ return bytes();
}
} // end anonymous namespace
diff --git a/solidityJSONInterfaceTest.cpp b/solidityJSONInterfaceTest.cpp
index 17e97dc6..487508bb 100644
--- a/solidityJSONInterfaceTest.cpp
+++ b/solidityJSONInterfaceTest.cpp
@@ -50,7 +50,7 @@ public:
msg += *extra;
BOOST_FAIL(msg);
}
- std::string generatedInterfaceString = m_compilerStack.getJsonDocumentation(DocumentationType::ABI_INTERFACE);
+ std::string generatedInterfaceString = m_compilerStack.getJsonDocumentation("", DocumentationType::ABI_INTERFACE);
Json::Value generatedInterface;
m_reader.parse(generatedInterfaceString, generatedInterface);
Json::Value expectedInterface;
diff --git a/solidityNameAndTypeResolution.cpp b/solidityNameAndTypeResolution.cpp
index 8804c519..78397229 100644
--- a/solidityNameAndTypeResolution.cpp
+++ b/solidityNameAndTypeResolution.cpp
@@ -41,10 +41,12 @@ namespace
void parseTextAndResolveNames(std::string const& _source)
{
Parser parser;
- ASTPointer<ContractDefinition> contract = parser.parse(
- std::make_shared<Scanner>(CharStream(_source)));
+ ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
NameAndTypeResolver resolver({});
- resolver.resolveNamesAndTypes(*contract);
+ resolver.registerDeclarations(*sourceUnit);
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
+ resolver.resolveNamesAndTypes(*contract);
}
}
diff --git a/solidityNatspecJSON.cpp b/solidityNatspecJSON.cpp
index 5e4c1fca..f1795fe1 100644
--- a/solidityNatspecJSON.cpp
+++ b/solidityNatspecJSON.cpp
@@ -55,9 +55,9 @@ public:
}
if (_userDocumentation)
- generatedDocumentationString = m_compilerStack.getJsonDocumentation(DocumentationType::NATSPEC_USER);
+ generatedDocumentationString = m_compilerStack.getJsonDocumentation("", DocumentationType::NATSPEC_USER);
else
- generatedDocumentationString = m_compilerStack.getJsonDocumentation(DocumentationType::NATSPEC_DEV);
+ generatedDocumentationString = m_compilerStack.getJsonDocumentation("", DocumentationType::NATSPEC_DEV);
Json::Value generatedDocumentation;
m_reader.parse(generatedDocumentationString, generatedDocumentation);
Json::Value expectedDocumentation;
diff --git a/solidityParser.cpp b/solidityParser.cpp
index 6a97a5d9..a9e2d531 100644
--- a/solidityParser.cpp
+++ b/solidityParser.cpp
@@ -21,13 +21,15 @@
*/
#include <string>
-
+#include <memory>
#include <libdevcore/Log.h>
#include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h>
#include <libsolidity/Exceptions.h>
#include <boost/test/unit_test.hpp>
+using namespace std;
+
namespace dev
{
namespace solidity
@@ -40,7 +42,12 @@ namespace
ASTPointer<ContractDefinition> parseText(std::string const& _source)
{
Parser parser;
- return parser.parse(std::make_shared<Scanner>(CharStream(_source)));
+ ASTPointer<SourceUnit> sourceUnit = parser.parse(std::make_shared<Scanner>(CharStream(_source)));
+ for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
+ if (ASTPointer<ContractDefinition> contract = dynamic_pointer_cast<ContractDefinition>(node))
+ return contract;
+ BOOST_FAIL("No contract found in source.");
+ return ASTPointer<ContractDefinition>();
}
}
@@ -380,6 +387,50 @@ BOOST_AUTO_TEST_CASE(statement_starting_with_type_conversion)
BOOST_CHECK_NO_THROW(parseText(text));
}
+BOOST_AUTO_TEST_CASE(import_directive)
+{
+ char const* text = "import \"abc\";\n"
+ "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
+BOOST_AUTO_TEST_CASE(multiple_contracts)
+{
+ char const* text = "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "contract test2 {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n";
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
+BOOST_AUTO_TEST_CASE(multiple_contracts_and_imports)
+{
+ char const* text = "import \"abc\";\n"
+ "contract test {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "import \"def\";\n"
+ "contract test2 {\n"
+ " function fun() {\n"
+ " uint64(2);\n"
+ " }\n"
+ "}\n"
+ "import \"ghi\";\n";
+ BOOST_CHECK_NO_THROW(parseText(text));
+}
+
BOOST_AUTO_TEST_SUITE_END()
}