Browse Source

Merge pull request #824 from LefterisJP/sol_BlockhashTests

Sol blockhash tests
cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
41f33cf690
  1. 4
      libsolidity/ExpressionCompiler.cpp
  2. 8
      test/SolidityABIJSON.cpp
  3. 25
      test/SolidityExpressionCompiler.cpp
  4. 8
      test/SolidityNatspecJSON.cpp
  5. 12
      test/solidityExecutionFramework.h

4
libsolidity/ExpressionCompiler.cpp

@ -392,9 +392,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
m_context << eth::Instruction::COINBASE; m_context << eth::Instruction::COINBASE;
else if (member == "timestamp") else if (member == "timestamp")
m_context << eth::Instruction::TIMESTAMP; m_context << eth::Instruction::TIMESTAMP;
/* else if (member == "blockhash") else if (member == "difficulty")
m_context << eth::Instruction::BLOCKHASH;
*/ else if (member == "difficulty")
m_context << eth::Instruction::DIFFICULTY; m_context << eth::Instruction::DIFFICULTY;
else if (member == "number") else if (member == "number")
m_context << eth::Instruction::NUMBER; m_context << eth::Instruction::NUMBER;

8
test/SolidityABIJSON.cpp

@ -41,13 +41,9 @@ public:
{ {
m_compilerStack.parse(_code); m_compilerStack.parse(_code);
} }
catch (const std::exception& e) catch(boost::exception const& _e)
{ {
std::string const* extra = boost::get_error_info<errinfo_comment>(e); auto msg = std::string("Parsing contract failed with: ") + boost::diagnostic_information(_e);
std::string msg = std::string("Parsing contract failed with: ") +
e.what() + std::string("\n");
if (extra)
msg += *extra;
BOOST_FAIL(msg); BOOST_FAIL(msg);
} }
std::string generatedInterfaceString = m_compilerStack.getMetadata("", DocumentationType::ABI_INTERFACE); std::string generatedInterfaceString = m_compilerStack.getMetadata("", DocumentationType::ABI_INTERFACE);

25
test/SolidityExpressionCompiler.cpp

@ -86,13 +86,19 @@ Declaration const& resolveDeclaration(vector<string> const& _namespacedName,
} }
bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _functions = {}, bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _functions = {},
vector<vector<string>> _localVariables = {}) vector<vector<string>> _localVariables = {}, vector<shared_ptr<MagicVariableDeclaration const>> _globalDeclarations = {})
{ {
Parser parser; Parser parser;
ASTPointer<SourceUnit> sourceUnit; ASTPointer<SourceUnit> sourceUnit;
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode)))); BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
NameAndTypeResolver resolver({});
vector<Declaration const*> declarations;
declarations.reserve(_globalDeclarations.size() + 1);
for (ASTPointer<Declaration const> const& variable: _globalDeclarations)
declarations.push_back(variable.get());
NameAndTypeResolver resolver(declarations);
resolver.registerDeclarations(*sourceUnit); resolver.registerDeclarations(*sourceUnit);
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes()) for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{ {
@ -390,6 +396,21 @@ BOOST_AUTO_TEST_CASE(intermediately_overflowing_literals)
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
BOOST_AUTO_TEST_CASE(blockhash)
{
char const* sourceCode = "contract test {\n"
" function f() {\n"
" block.blockhash(3);\n"
" }\n"
"}\n";
bytes code = compileFirstExpression(sourceCode, {}, {},
{make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::BLOCK))});
bytes expectation({byte(eth::Instruction::PUSH1), 0x03,
byte(eth::Instruction::BLOCKHASH)});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

8
test/SolidityNatspecJSON.cpp

@ -45,13 +45,9 @@ public:
{ {
m_compilerStack.parse(_code); m_compilerStack.parse(_code);
} }
catch (const std::exception& e) catch(boost::exception const& _e)
{ {
std::string const* extra = boost::get_error_info<errinfo_comment>(e); auto msg = std::string("Parsing contract failed with: ") + boost::diagnostic_information(_e);
std::string msg = std::string("Parsing contract failed with: ") +
e.what() + std::string("\n");
if (extra)
msg += *extra;
BOOST_FAIL(msg); BOOST_FAIL(msg);
} }

12
test/solidityExecutionFramework.h

@ -46,7 +46,16 @@ public:
bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "")
{ {
dev::solidity::CompilerStack compiler; dev::solidity::CompilerStack compiler;
compiler.compile(_sourceCode, m_optimize); try
{
compiler.compile(_sourceCode, m_optimize);
}
catch(boost::exception const& _e)
{
auto msg = std::string("Compiling contract failed with: ") + boost::diagnostic_information(_e);
BOOST_FAIL(msg);
}
bytes code = compiler.getBytecode(_contractName); bytes code = compiler.getBytecode(_contractName);
sendMessage(code, true, _value); sendMessage(code, true, _value);
BOOST_REQUIRE(!m_output.empty()); BOOST_REQUIRE(!m_output.empty());
@ -97,6 +106,7 @@ public:
static bytes encode(char const* _value) { return encode(std::string(_value)); } static bytes encode(char const* _value) { return encode(std::string(_value)); }
static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; } static bytes encode(byte _value) { return bytes(31, 0) + bytes{_value}; }
static bytes encode(u256 const& _value) { return toBigEndian(_value); } static bytes encode(u256 const& _value) { return toBigEndian(_value); }
static bytes encode(h256 const& _value) { return _value.asBytes(); }
static bytes encode(bytes const& _value, bool _padLeft = true) static bytes encode(bytes const& _value, bool _padLeft = true)
{ {
bytes padding = bytes((32 - _value.size() % 32) % 32, 0); bytes padding = bytes((32 - _value.size() % 32) % 32, 0);

Loading…
Cancel
Save