Browse Source

Merge remote-tracking branch 'upstream/develop' into evmjit

Conflicts:
	evmjit/libevmjit/BasicBlock.h
	evmjit/libevmjit/Compiler.cpp
	evmjit/libevmjit/Compiler.h
	evmjit/libevmjit/Ext.cpp
	evmjit/libevmjit/Ext.h
	evmjit/libevmjit/Runtime.h
	evmjit/libevmjit/Utils.cpp
cl-refactor
Paweł Bylica 10 years ago
parent
commit
a87c563acb
  1. 2
      .gitignore
  2. 6
      alethzero/Main.ui
  3. 120
      alethzero/MainWin.cpp
  4. 3
      alethzero/MainWin.h
  5. 20
      eth/main.cpp
  6. 2
      libethereum/Client.cpp
  7. 57
      libsolidity/AST.cpp
  8. 5
      libsolidity/AST.h
  9. 1
      libsolidity/CMakeLists.txt
  10. 44
      libsolidity/Compiler.cpp
  11. 13
      libsolidity/CompilerStack.cpp
  12. 12
      libsolidity/CompilerStack.h
  13. 2
      libsolidity/CompilerUtils.cpp
  14. 4
      libsolidity/CompilerUtils.h
  15. 114
      libsolidity/ExpressionCompiler.cpp
  16. 3
      libsolidity/ExpressionCompiler.h
  17. 56
      libsolidity/GlobalContext.cpp
  18. 48
      libsolidity/InterfaceHandler.cpp
  19. 1
      libsolidity/InterfaceHandler.h
  20. 7
      libsolidity/Scanner.cpp
  21. 288
      libsolidity/Types.cpp
  22. 130
      libsolidity/Types.h
  23. 4
      mix/AssemblyDebuggerControl.cpp
  24. 4
      mix/CodeHighlighter.cpp
  25. 2
      mix/CodeModel.cpp
  26. 10
      mix/QContractDefinition.cpp
  27. 272
      neth/main.cpp
  28. 41
      solc/CommandLineInterface.cpp
  29. 2
      solc/CommandLineInterface.h
  30. 30
      test/SolidityABIJSON.cpp
  31. 25
      test/SolidityCompiler.cpp
  32. 377
      test/SolidityEndToEndTest.cpp
  33. 88
      test/SolidityExpressionCompiler.cpp
  34. 4
      test/SolidityNatspecJSON.cpp
  35. 26
      test/SolidityOptimizer.cpp
  36. 12
      test/SolidityScanner.cpp
  37. 8
      test/TestHelper.cpp
  38. 1
      test/TestHelper.h
  39. 20
      test/solidityExecutionFramework.h
  40. 85
      test/stSystemOperationsTestFiller.json
  41. 10
      test/state.cpp
  42. 3
      test/vm.cpp
  43. 56
      test/vmArithmeticTestFiller.json
  44. 56
      test/vmBlockInfoTestFiller.json
  45. 1244
      test/vmIOandFlowOperationsTestFiller.json

2
.gitignore

@ -66,3 +66,5 @@ project.pbxproj
evmjit
doc/html
*.autosave

6
alethzero/Main.ui

@ -165,6 +165,7 @@
<addaction name="debugDumpStatePre"/>
<addaction name="separator"/>
<addaction name="paranoia"/>
<addaction name="clearPending"/>
<addaction name="killBlockchain"/>
<addaction name="inject"/>
<addaction name="forceMining"/>
@ -2049,6 +2050,11 @@ font-size: 14pt</string>
<string>New Identity</string>
</property>
</action>
<action name="clearPending">
<property name="text">
<string>Clear Pe&amp;nd&amp;ing</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>

120
alethzero/MainWin.cpp

@ -105,9 +105,9 @@ static QString contentsOfQResource(std::string const& res)
return in.readAll();
}
Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
Address c_newConfig = Address("d5f9d8d94886e70b06e474c3fb14fd43e2f23970");
Address c_nameReg = Address("ddd1cea741d548f90d86fb87a3ae6492e18c03a1");
//Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
Address c_newConfig = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
//Address c_nameReg = Address("ddd1cea741d548f90d86fb87a3ae6492e18c03a1");
Main::Main(QWidget *parent) :
QMainWindow(parent),
@ -265,22 +265,32 @@ void Main::uninstallWatch(unsigned _w)
void Main::installWatches()
{
installWatch(dev::eth::LogFilter().address(c_config), [=]() { installNameRegWatch(); });
installWatch(dev::eth::LogFilter().address(c_config), [=]() { installCurrenciesWatch(); });
installWatch(dev::eth::LogFilter().address(c_newConfig), [=]() { installNameRegWatch(); });
installWatch(dev::eth::LogFilter().address(c_newConfig), [=]() { installCurrenciesWatch(); });
installWatch(dev::eth::PendingChangedFilter, [=](){ onNewPending(); });
installWatch(dev::eth::ChainChangedFilter, [=](){ onNewBlock(); });
}
Address Main::getNameReg() const
{
return abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)1)));
}
Address Main::getCurrencies() const
{
return abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)2)));
}
void Main::installNameRegWatch()
{
uninstallWatch(m_nameRegFilter);
m_nameRegFilter = installWatch(dev::eth::LogFilter().address((u160)ethereum()->stateAt(c_config, 0)), [=](){ onNameRegChange(); });
m_nameRegFilter = installWatch(dev::eth::LogFilter().address((u160)getNameReg()), [=](){ onNameRegChange(); });
}
void Main::installCurrenciesWatch()
{
uninstallWatch(m_currenciesFilter);
m_currenciesFilter = installWatch(dev::eth::LogFilter().address((u160)ethereum()->stateAt(c_config, 1)), [=](){ onCurrenciesChange(); });
m_currenciesFilter = installWatch(dev::eth::LogFilter().address((u160)getCurrencies()), [=](){ onCurrenciesChange(); });
}
void Main::installBalancesWatch()
@ -288,7 +298,9 @@ void Main::installBalancesWatch()
dev::eth::LogFilter tf;
vector<Address> altCoins;
Address coinsAddr = right160(ethereum()->stateAt(c_config, 1));
Address coinsAddr = getCurrencies();
// TODO: Update for new currencies reg.
for (unsigned i = 0; i < ethereum()->stateAt(coinsAddr, 0); ++i)
altCoins.push_back(right160(ethereum()->stateAt(coinsAddr, i + 1)));
for (auto i: m_myKeys)
@ -450,37 +462,37 @@ static Public stringToPublic(QString const& _a)
return Public();
}
static Address g_newNameReg;
//static Address g_newNameReg;
QString Main::pretty(dev::Address _a) const
{
static std::map<Address, QString> s_memos;
/* static std::map<Address, QString> s_memos;
if (!s_memos.count(_a))
{
if (!g_newNameReg)
g_newNameReg = abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)1)));
{*/
// if (!g_newNameReg)
auto g_newNameReg = getNameReg();
if (g_newNameReg)
{
QString s = QString::fromStdString(toString(abiOut<string32>(ethereum()->call(g_newNameReg, abiIn(2, _a)))));
s_memos[_a] = s;
// s_memos[_a] = s;
if (s.size())
return s;
}
}
/* }
else
if (s_memos[_a].size())
return s_memos[_a];
return s_memos[_a];*/
h256 n;
/*
if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0))
n = ethereum()->stateAt(nameReg, (u160)(_a));
if (!n)
n = ethereum()->stateAt(m_nameReg, (u160)(_a));
*/
return fromRaw(n);
}
@ -505,21 +517,21 @@ Address Main::fromString(QString const& _n) const
if (_n == "(Create Contract)")
return Address();
static std::map<QString, Address> s_memos;
/* static std::map<QString, Address> s_memos;
if (!s_memos.count(_n))
{
if (!g_newNameReg)
g_newNameReg = abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)1)));
{*/
// if (!g_newNameReg)
auto g_newNameReg = abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)1)));
if (g_newNameReg)
{
Address a = abiOut<Address>(ethereum()->call(g_newNameReg, abiIn(0, ::fromString(_n.toStdString()))));
s_memos[_n] = a;
// s_memos[_n] = a;
if (a)
return a;
}
}
/* }
else
if (s_memos[_n])
return s_memos[_n];
@ -532,14 +544,13 @@ Address Main::fromString(QString const& _n) const
memset(n.data() + sn.size(), 0, 32 - sn.size());
if (_n.size())
{
if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0))
if (h256 a = ethereum()->stateAt(nameReg, n))
return right160(a);
if (h256 a = ethereum()->stateAt(m_nameReg, n))
return right160(a);
}
}*/
if (_n.size() == 40)
return Address(fromHex(_n.toStdString()));
@ -566,8 +577,9 @@ QString Main::lookup(QString const& _a) const
*/
h256 ret;
if (h160 dnsReg = (u160)ethereum()->stateAt(c_config, 4, 0))
ret = ethereum()->stateAt(dnsReg, n);
// TODO: fix with the new DNSreg contract
// if (h160 dnsReg = (u160)ethereum()->stateAt(c_config, 4, 0))
// ret = ethereum()->stateAt(dnsReg, n);
/* if (!ret)
if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0, 0))
ret = ethereum()->stateAt(nameReg, n2);
@ -861,8 +873,8 @@ void Main::refreshBalances()
// update all the balance-dependent stuff.
ui->ourAccounts->clear();
u256 totalBalance = 0;
map<Address, tuple<QString, u256, u256>> altCoins;
Address coinsAddr = right160(ethereum()->stateAt(c_config, 1));
/* map<Address, tuple<QString, u256, u256>> altCoins;
Address coinsAddr = getCurrencies();
for (unsigned i = 0; i < ethereum()->stateAt(coinsAddr, 0); ++i)
{
auto n = ethereum()->stateAt(coinsAddr, i + 1);
@ -872,7 +884,7 @@ void Main::refreshBalances()
denom = 1;
// cdebug << n << addr << denom << sha3(h256(n).asBytes());
altCoins[addr] = make_tuple(fromRaw(n), 0, denom);
}
}*/
for (auto i: m_myKeys)
{
u256 b = ethereum()->balanceAt(i.address());
@ -880,18 +892,18 @@ void Main::refreshBalances()
->setData(Qt::UserRole, QByteArray((char const*)i.address().data(), Address::size));
totalBalance += b;
for (auto& c: altCoins)
get<1>(c.second) += (u256)ethereum()->stateAt(c.first, (u160)i.address());
// for (auto& c: altCoins)
// get<1>(c.second) += (u256)ethereum()->stateAt(c.first, (u160)i.address());
}
QString b;
for (auto const& c: altCoins)
/* for (auto const& c: altCoins)
if (get<1>(c.second))
{
stringstream s;
s << setw(toString(get<2>(c.second) - 1).size()) << setfill('0') << (get<1>(c.second) % get<2>(c.second));
b += QString::fromStdString(toString(get<1>(c.second) / get<2>(c.second)) + "." + s.str() + " ") + get<0>(c.second).toUpper() + " | ";
}
}*/
ui->balance->setText(b + QString::fromStdString(formatBalance(totalBalance)));
}
@ -1224,6 +1236,7 @@ void Main::on_transactionQueue_currentItemChanged()
if (i >= 0 && i < (int)ethereum()->pending().size())
{
Transaction tx(ethereum()->pending()[i]);
TransactionReceipt receipt(ethereum()->postState().receipt(i));
auto ss = tx.safeSender();
h256 th = sha3(rlpList(ss, tx.nonce()));
s << "<h3>" << th << "</h3>";
@ -1246,12 +1259,15 @@ void Main::on_transactionQueue_currentItemChanged()
if (tx.data().size())
s << dev::memDump(tx.data(), 16, true);
}
s << "<div>Hex: <span style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">" << toHex(tx.rlp()) << "</span></div>";
s << "<hr/>";
s << "<div>Log Bloom: " << receipt.bloom() << "</div>";
auto r = receipt.rlp();
s << "<div>Receipt: " << toString(RLP(r)) << "</div>";
s << "<div>Receipt-Hex: <span style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">" << toHex(receipt.rlp()) << "</span></div>";
s << renderDiff(ethereum()->diff(i, -1));
// s << "Pre: " << fs.rootHash() << "<br/>";
// s << "Post: <b>" << ts.rootHash() << "</b>";
s << renderDiff(ethereum()->diff(i, 0));
}
ui->pendingInfo->setHtml(QString::fromStdString(s.str()));
@ -1364,11 +1380,6 @@ void Main::on_blocks_currentItemChanged()
s << "<br/>R: <b>" << hex << nouppercase << tx.signature().r << "</b>";
s << "<br/>S: <b>" << hex << nouppercase << tx.signature().s << "</b>";
s << "<br/>Msg: <b>" << tx.sha3(eth::WithoutSignature) << "</b>";
s << "<div>Log Bloom: " << receipt.bloom() << "</div>";
s << "<div>Hex: <span style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">" << toHex(block[1][txi].data()) << "</span></div>";
auto r = receipt.rlp();
s << "<div>Receipt: " << toString(RLP(r)) << "</div>";
s << "<div>Receipt-Hex: <span style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">" << toHex(receipt.rlp()) << "</span></div>";
if (tx.isCreation())
{
if (tx.data().size())
@ -1379,6 +1390,12 @@ void Main::on_blocks_currentItemChanged()
if (tx.data().size())
s << dev::memDump(tx.data(), 16, true);
}
s << "<div>Hex: <span style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">" << toHex(block[1][txi].data()) << "</span></div>";
s << "<hr/>";
s << "<div>Log Bloom: " << receipt.bloom() << "</div>";
auto r = receipt.rlp();
s << "<div>Receipt: " << toString(RLP(r)) << "</div>";
s << "<div>Receipt-Hex: <span style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">" << toHex(receipt.rlp()) << "</span></div>";
s << renderDiff(ethereum()->diff(txi, h));
ui->debugCurrent->setEnabled(true);
ui->debugDumpState->setEnabled(true);
@ -1619,12 +1636,15 @@ void Main::on_data_textChanged()
{
m_data = fromHex(src);
}
else if (src.substr(0, 8) == "contract") // improve this heuristic
else if (src.substr(0, 8) == "contract" || src.substr(0, 5) == "//sol") // improve this heuristic
{
dev::solidity::CompilerStack compiler;
try
{
m_data = compiler.compile(src, m_enableOptimizer);
solidity = "<h4>Solidity</h4>";
solidity += "<pre>" + QString::fromStdString(compiler.getInterface()).replace(QRegExp("\\s"), "").toHtmlEscaped() + "</pre>";
solidity += "<pre>" + QString::fromStdString(compiler.getSolidityInterface()).toHtmlEscaped() + "</pre>";
}
catch (dev::Exception const& exception)
{
@ -1699,6 +1719,18 @@ void Main::on_data_textChanged()
updateFee();
}
void Main::on_clearPending_triggered()
{
writeSettings();
ui->mine->setChecked(false);
ui->net->setChecked(false);
web3()->stopNetwork();
ethereum()->clearPending();
readSettings(true);
installWatches();
refreshAll();
}
void Main::on_killBlockchain_triggered()
{
writeSettings();

3
alethzero/MainWin.h

@ -128,6 +128,7 @@ private slots:
void on_debugTimeline_valueChanged();
void on_jsInput_returnPressed();
void on_killBlockchain_triggered();
void on_clearPending_triggered();
void on_importKey_triggered();
void on_exportKey_triggered();
void on_inject_triggered();
@ -170,6 +171,8 @@ private:
QString prettyU256(dev::u256 _n) const;
QString lookup(QString const& _n) const;
dev::Address getNameReg() const;
dev::Address getCurrencies() const;
void populateDebugger(dev::bytesConstRef r);
void initDebugger();

20
eth/main.cpp

@ -30,6 +30,7 @@
#include <libdevcrypto/FileSystem.h>
#include <libevmcore/Instruction.h>
#include <libevm/VM.h>
#include <libevm/VMFactory.h>
#include <libethereum/All.h>
#include <libwebthree/WebThree.h>
#if ETH_READLINE
@ -121,7 +122,11 @@ void help()
<< " -u,--public-ip <ip> Force public ip to given (default; auto)." << endl
<< " -v,--verbosity <0 - 9> Set the log verbosity from 0 to 9 (Default: 8)." << endl
<< " -x,--peers <number> Attempt to connect to given number of peers (Default: 5)." << endl
<< " -V,--version Show the version and exit." << endl;
<< " -V,--version Show the version and exit." << endl
#if ETH_EVMJIT
<< " --jit Use EVM JIT (default: off)." << endl
#endif
;
exit(0);
}
@ -193,6 +198,7 @@ int main(int argc, char** argv)
bool upnp = true;
bool useLocal = false;
bool forceMining = false;
bool jit = false;
string clientName;
// Init defaults
@ -295,6 +301,15 @@ int main(int argc, char** argv)
return -1;
}
}
else if (arg == "--jit")
{
#if ETH_EVMJIT
jit = true;
#else
cerr << "EVM JIT not enabled" << endl;
return -1;
#endif
}
else if (arg == "-h" || arg == "--help")
help();
else if (arg == "-V" || arg == "--version")
@ -308,9 +323,10 @@ int main(int argc, char** argv)
cout << credits();
VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter);
NetworkPreferences netPrefs(listenPort, publicIP, upnp, useLocal);
dev::WebThreeDirect web3(
"Ethereum(++)/" + clientName + "v" + dev::Version + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM),
"Ethereum(++)/" + clientName + "v" + dev::Version + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM) + (jit ? "/JIT" : ""),
dbPath,
false,
mode == NodeMode::Full ? set<string>{"eth", "shh"} : set<string>(),

2
libethereum/Client.cpp

@ -534,7 +534,7 @@ eth::State Client::state(unsigned _txi) const
StateDiff Client::diff(unsigned _txi, int _block) const
{
State st = state(_block);
State st = asOf(_block);
return st.fromPending(_txi).diff(st.fromPending(_txi + 1));
}

57
libsolidity/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;
}
@ -173,7 +174,15 @@ void VariableDefinition::checkTypeRequirements()
{
// no type declared and no previous assignment, infer the type
m_value->checkTypeRequirements();
m_variable->setType(m_value->getType());
TypePointer type = m_value->getType();
if (type->getCategory() == Type::Category::INTEGER_CONSTANT)
{
auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
if (!intType)
BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString()));
type = intType;
}
m_variable->setType(type);
}
}
}
@ -192,16 +201,22 @@ void Assignment::checkTypeRequirements()
{
// compound assignment
m_rightHandSide->checkTypeRequirements();
TypePointer resultType = Type::binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
m_type, m_rightHandSide->getType());
TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
m_rightHandSide->getType());
if (!resultType || *resultType != *m_type)
BOOST_THROW_EXCEPTION(createTypeError("Operator not compatible with type."));
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_assigmentOperator)) +
" not compatible with types " +
m_type->toString() + " and " +
m_rightHandSide->getType()->toString()));
}
}
void ExpressionStatement::checkTypeRequirements()
{
m_expression->checkTypeRequirements();
if (m_expression->getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
if (!dynamic_pointer_cast<IntegerConstantType const>(m_expression->getType())->getIntegerType())
BOOST_THROW_EXCEPTION(m_expression->createTypeError("Invalid integer constant."));
}
void Expression::expectType(Type const& _expectedType)
@ -227,8 +242,8 @@ void UnaryOperation::checkTypeRequirements()
m_subExpression->checkTypeRequirements();
if (m_operator == Token::Value::INC || m_operator == Token::Value::DEC || m_operator == Token::Value::DELETE)
m_subExpression->requireLValue();
m_type = m_subExpression->getType();
if (!m_type->acceptsUnaryOperator(m_operator))
m_type = m_subExpression->getType()->unaryOperatorResult(m_operator);
if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
}
@ -236,7 +251,7 @@ void BinaryOperation::checkTypeRequirements()
{
m_left->checkTypeRequirements();
m_right->checkTypeRequirements();
m_commonType = Type::binaryOperatorResult(m_operator, m_left->getType(), m_right->getType());
m_commonType = m_left->getType()->binaryOperatorResult(m_operator, m_right->getType());
if (!m_commonType)
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_operator)) +
" not compatible with types " +
@ -300,7 +315,7 @@ void NewExpression::checkTypeRequirements()
m_contract = dynamic_cast<ContractDefinition const*>(m_contractName->getReferencedDeclaration());
if (!m_contract)
BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a contract."));
shared_ptr<ContractType const> type = make_shared<ContractType const>(*m_contract);
shared_ptr<ContractType const> type = make_shared<ContractType>(*m_contract);
m_type = type;
TypePointers const& parameterTypes = type->getConstructorType()->getParameterTypes();
if (parameterTypes.size() != m_arguments.size())
@ -351,7 +366,7 @@ void Identifier::checkTypeRequirements()
if (structDef)
{
// note that we do not have a struct type here
m_type = make_shared<TypeType const>(make_shared<StructType const>(*structDef));
m_type = make_shared<TypeType>(make_shared<StructType>(*structDef));
return;
}
FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(m_referencedDeclaration);
@ -360,13 +375,13 @@ void Identifier::checkTypeRequirements()
// a function reference is not a TypeType, because calling a TypeType converts to the type.
// Calling a function (e.g. function(12), otherContract.function(34)) does not do a type
// conversion.
m_type = make_shared<FunctionType const>(*functionDef);
m_type = make_shared<FunctionType>(*functionDef);
return;
}
ContractDefinition const* contractDef = dynamic_cast<ContractDefinition const*>(m_referencedDeclaration);
if (contractDef)
{
m_type = make_shared<TypeType const>(make_shared<ContractType>(*contractDef));
m_type = make_shared<TypeType>(make_shared<ContractType>(*contractDef));
return;
}
MagicVariableDeclaration const* magicVariable = dynamic_cast<MagicVariableDeclaration const*>(m_referencedDeclaration);
@ -380,14 +395,14 @@ void Identifier::checkTypeRequirements()
void ElementaryTypeNameExpression::checkTypeRequirements()
{
m_type = make_shared<TypeType const>(Type::fromElementaryTypeName(m_typeToken));
m_type = make_shared<TypeType>(Type::fromElementaryTypeName(m_typeToken));
}
void Literal::checkTypeRequirements()
{
m_type = Type::forLiteral(*this);
if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Literal value too large."));
BOOST_THROW_EXCEPTION(createTypeError("Invalid literal value."));
}
}

5
libsolidity/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;

1
libsolidity/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} )

44
libsolidity/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())
{

13
libsolidity/CompilerStack.cpp

@ -16,6 +16,7 @@
*/
/**
* @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Full-stack compiler that converts a source code string to bytecode.
*/
@ -140,10 +141,15 @@ void CompilerStack::streamAssembly(ostream& _outStream, string const& _contractN
string const& CompilerStack::getInterface(string const& _contractName) const
{
return getJsonDocumentation(_contractName, DocumentationType::ABI_INTERFACE);
return getMetadata(_contractName, DocumentationType::ABI_INTERFACE);
}
string const& CompilerStack::getJsonDocumentation(string const& _contractName, DocumentationType _type) const
string const& CompilerStack::getSolidityInterface(string const& _contractName) const
{
return getMetadata(_contractName, DocumentationType::ABI_SOLIDITY_INTERFACE);
}
string const& CompilerStack::getMetadata(string const& _contractName, DocumentationType _type) const
{
if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
@ -162,6 +168,9 @@ string const& CompilerStack::getJsonDocumentation(string const& _contractName, D
case DocumentationType::ABI_INTERFACE:
doc = &contract.interface;
break;
case DocumentationType::ABI_SOLIDITY_INTERFACE:
doc = &contract.solidityInterface;
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal documentation type."));
}

12
libsolidity/CompilerStack.h

@ -16,6 +16,7 @@
*/
/**
* @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Full-stack compiler that converts a source code string to bytecode.
*/
@ -43,7 +44,8 @@ enum class DocumentationType: uint8_t
{
NATSPEC_USER = 1,
NATSPEC_DEV,
ABI_INTERFACE
ABI_INTERFACE,
ABI_SOLIDITY_INTERFACE
};
/**
@ -81,11 +83,14 @@ public:
/// Returns a string representing the contract interface in JSON.
/// Prerequisite: Successful call to parse or compile.
std::string const& getInterface(std::string const& _contractName = "") const;
/// Returns a string representing the contract interface in Solidity.
/// Prerequisite: Successful call to parse or compile.
std::string const& getSolidityInterface(std::string const& _contractName = "") const;
/// Returns a string representing the contract's documentation in JSON.
/// Prerequisite: Successful call to parse or compile.
/// @param type The type of the documentation to get.
/// Can be one of 3 types defined at @c DocumentationType
std::string const& getJsonDocumentation(std::string const& _contractName, DocumentationType _type) const;
/// Can be one of 4 types defined at @c DocumentationType
std::string const& getMetadata(std::string const& _contractName, DocumentationType _type) const;
/// @returns the previously used scanner, useful for counting lines during error reporting.
Scanner const& getScanner(std::string const& _sourceName = "") const;
@ -118,6 +123,7 @@ private:
bytes bytecode;
std::shared_ptr<InterfaceHandler> interfaceHandler;
mutable std::unique_ptr<std::string const> interface;
mutable std::unique_ptr<std::string const> solidityInterface;
mutable std::unique_ptr<std::string const> userDocumentation;
mutable std::unique_ptr<std::string const> devDocumentation;

2
libsolidity/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)

4
libsolidity/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)
{

114
libsolidity/ExpressionCompiler.cpp

@ -71,12 +71,20 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
return false;
}
void ExpressionCompiler::endVisit(UnaryOperation const& _unaryOperation)
bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
{
//@todo type checking and creating code for an operator should be in the same place:
// the operator should know how to convert itself and to which types it applies, so
// put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
// represents the operator
if (_unaryOperation.getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
{
m_context << _unaryOperation.getType()->literalValue(nullptr);
return false;
}
_unaryOperation.getSubExpression().accept(*this);
switch (_unaryOperation.getOperator())
{
case Token::NOT: // !
@ -128,6 +136,7 @@ void ExpressionCompiler::endVisit(UnaryOperation const& _unaryOperation)
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " +
string(Token::toString(_unaryOperation.getOperator()))));
}
return false;
}
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
@ -139,17 +148,19 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
if (op == Token::AND || op == Token::OR) // special case: short-circuiting
appendAndOrOperatorCode(_binaryOperation);
else if (commonType.getCategory() == Type::Category::INTEGER_CONSTANT)
m_context << commonType.literalValue(nullptr);
else
{
bool cleanupNeeded = false;
if (commonType.getCategory() == Type::Category::INTEGER)
if (Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD)
cleanupNeeded = true;
bool cleanupNeeded = commonType.getCategory() == Type::Category::INTEGER &&
(Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD);
// for commutative operators, push the literal as late as possible to allow improved optimization
//@todo this has to be extended for literal expressions
bool swap = (m_optimize && Token::isCommutativeOp(op) && dynamic_cast<Literal const*>(&rightExpression)
&& !dynamic_cast<Literal const*>(&leftExpression));
auto isLiteral = [](Expression const& _e)
{
return dynamic_cast<Literal const*>(&_e) || _e.getType()->getCategory() == Type::Category::INTEGER_CONSTANT;
};
bool swap = m_optimize && Token::isCommutativeOp(op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
if (swap)
{
leftExpression.accept(*this);
@ -256,6 +267,61 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::SHA3;
break;
case Location::LOG0:
arguments.front()->accept(*this);
appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
// @todo move this once we actually use memory
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::LOG0;
break;
case Location::LOG1:
arguments[1]->accept(*this);
arguments[0]->accept(*this);
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
// @todo move this once we actually use memory
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::LOG1;
break;
case Location::LOG2:
arguments[2]->accept(*this);
arguments[1]->accept(*this);
arguments[0]->accept(*this);
appendTypeConversion(*arguments[2]->getType(), *function.getParameterTypes()[2], true);
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
// @todo move this once we actually use memory
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::LOG2;
break;
case Location::LOG3:
arguments[3]->accept(*this);
arguments[2]->accept(*this);
arguments[1]->accept(*this);
arguments[0]->accept(*this);
appendTypeConversion(*arguments[3]->getType(), *function.getParameterTypes()[3], true);
appendTypeConversion(*arguments[2]->getType(), *function.getParameterTypes()[2], true);
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
// @todo move this once we actually use memory
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::LOG3;
break;
case Location::LOG4:
arguments[4]->accept(*this);
arguments[3]->accept(*this);
arguments[2]->accept(*this);
arguments[1]->accept(*this);
arguments[0]->accept(*this);
appendTypeConversion(*arguments[4]->getType(), *function.getParameterTypes()[4], true);
appendTypeConversion(*arguments[3]->getType(), *function.getParameterTypes()[3], true);
appendTypeConversion(*arguments[2]->getType(), *function.getParameterTypes()[2], true);
appendTypeConversion(*arguments[1]->getType(), *function.getParameterTypes()[1], true);
appendTypeConversion(*arguments[0]->getType(), *function.getParameterTypes()[0], true);
// @todo move this once we actually use memory
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::LOG4;
break;
case Location::ECRECOVER:
case Location::SHA256:
case Location::RIPEMD160:
@ -335,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:
@ -423,10 +489,10 @@ void ExpressionCompiler::endVisit(Literal const& _literal)
{
switch (_literal.getType()->getCategory())
{
case Type::Category::INTEGER:
case Type::Category::INTEGER_CONSTANT:
case Type::Category::BOOL:
case Type::Category::STRING:
m_context << _literal.getType()->literalValue(_literal);
m_context << _literal.getType()->literalValue(&_literal);
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now."));
@ -562,9 +628,16 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con
if (_typeOnStack == _targetType && !_cleanupNeeded)
return;
if (_typeOnStack.getCategory() == Type::Category::INTEGER)
Type::Category stackTypeCategory = _typeOnStack.getCategory();
Type::Category targetTypeCategory = _targetType.getCategory();
if (stackTypeCategory == Type::Category::INTEGER)
{
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack));
else if (_typeOnStack.getCategory() == Type::Category::STRING)
}
else if (stackTypeCategory == Type::Category::INTEGER_CONSTANT)
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
else if (stackTypeCategory == Type::Category::STRING)
{
// nothing to do, strings are high-order-bit-aligned
//@todo clear lower-order bytes if we allow explicit conversion to shorter strings
@ -590,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);
@ -617,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;

3
libsolidity/ExpressionCompiler.h

@ -16,6 +16,7 @@
*/
/**
* @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Solidity AST to EVM bytecode compiler for expressions.
*/
@ -57,7 +58,7 @@ private:
m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {}
virtual bool visit(Assignment const& _assignment) override;
virtual void endVisit(UnaryOperation const& _unaryOperation) override;
virtual bool visit(UnaryOperation const& _unaryOperation) override;
virtual bool visit(BinaryOperation const& _binaryOperation) override;
virtual bool visit(FunctionCall const& _functionCall) override;
virtual bool visit(NewExpression const& _newExpression) override;

56
libsolidity/GlobalContext.cpp

@ -16,6 +16,7 @@
*/
/**
* @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Container of the (implicit and explicit) global objects.
*/
@ -33,33 +34,54 @@ namespace solidity
{
GlobalContext::GlobalContext():
// TODO: make this cleaner.
m_magicVariables(vector<shared_ptr<MagicVariableDeclaration const>>{make_shared<MagicVariableDeclaration>("block", make_shared<MagicType>(MagicType::Kind::BLOCK)),
make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::MSG)),
make_shared<MagicVariableDeclaration>("tx", make_shared<MagicType>(MagicType::Kind::TX)),
make_shared<MagicVariableDeclaration>("suicide",
make_shared<MagicVariableDeclaration>("msg", make_shared<MagicType>(MagicType::Kind::MSG)),
make_shared<MagicVariableDeclaration>("tx", make_shared<MagicType>(MagicType::Kind::TX)),
make_shared<MagicVariableDeclaration>("suicide",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(0,
IntegerType::Modifier::ADDRESS)}),
TypePointers(),
FunctionType::Location::SUICIDE)),
make_shared<MagicVariableDeclaration>("sha3",
TypePointers(),
FunctionType::Location::SUICIDE)),
make_shared<MagicVariableDeclaration>("sha3",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
FunctionType::Location::SHA3)),
make_shared<MagicVariableDeclaration>("sha256",
TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
FunctionType::Location::SHA3)),
make_shared<MagicVariableDeclaration>("log0",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
FunctionType::Location::SHA256)),
make_shared<MagicVariableDeclaration>("ecrecover",
TypePointers(),
FunctionType::Location::LOG0)),
make_shared<MagicVariableDeclaration>("log1",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers(),
FunctionType::Location::LOG1)),
make_shared<MagicVariableDeclaration>("log2",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers(),
FunctionType::Location::LOG2)),
make_shared<MagicVariableDeclaration>("log3",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers(),
FunctionType::Location::LOG3)),
make_shared<MagicVariableDeclaration>("log4",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH), std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers(),
FunctionType::Location::LOG4)),
make_shared<MagicVariableDeclaration>("sha256",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
FunctionType::Location::SHA256)),
make_shared<MagicVariableDeclaration>("ecrecover",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH),
std::make_shared<IntegerType>(8, IntegerType::Modifier::HASH),
std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH),
std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers({std::make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)}),
FunctionType::Location::ECRECOVER)),
make_shared<MagicVariableDeclaration>("ripemd160",
TypePointers({std::make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)}),
FunctionType::Location::ECRECOVER)),
make_shared<MagicVariableDeclaration>("ripemd160",
make_shared<FunctionType>(TypePointers({std::make_shared<IntegerType>(256, IntegerType::Modifier::HASH)}),
TypePointers({std::make_shared<IntegerType>(160, IntegerType::Modifier::HASH)}),
FunctionType::Location::RIPEMD160))})
TypePointers({std::make_shared<IntegerType>(160, IntegerType::Modifier::HASH)}),
FunctionType::Location::RIPEMD160))})
{
}

48
libsolidity/InterfaceHandler.cpp

@ -2,6 +2,7 @@
#include <libsolidity/InterfaceHandler.h>
#include <libsolidity/AST.h>
#include <libsolidity/CompilerStack.h>
using namespace std;
namespace dev
{
@ -26,6 +27,8 @@ std::unique_ptr<std::string> InterfaceHandler::getDocumentation(ContractDefiniti
return getDevDocumentation(_contractDef);
case DocumentationType::ABI_INTERFACE:
return getABIInterface(_contractDef);
case DocumentationType::ABI_SOLIDITY_INTERFACE:
return getABISolidityInterface(_contractDef);
}
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation type"));
@ -36,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);
@ -55,24 +58,47 @@ 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)));
}
unique_ptr<string> InterfaceHandler::getABISolidityInterface(ContractDefinition const& _contractDef)
{
string ret = "contract " + _contractDef.getName() + "{";
for (auto const& it: _contractDef.getInterfaceFunctions())
{
FunctionDefinition const* f = it.second;
auto populateParameters = [](vector<ASTPointer<VariableDeclaration>> const& _vars)
{
string r = "";
for (ASTPointer<VariableDeclaration> const& var: _vars)
r += (r.size() ? "," : "(") + var->getType()->toString() + " " + var->getName();
return r.size() ? r + ")" : "()";
};
ret += "function " + f->getName() + populateParameters(f->getParameters()) + (f->isDeclaredConst() ? "constant " : "");
if (f->getReturnParameters().size())
ret += "returns" + populateParameters(f->getReturnParameters());
else if (ret.back() == ' ')
ret.pop_back();
ret += "{}";
}
return unique_ptr<string>(new string(ret + "}"));
}
std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(ContractDefinition const& _contractDef)
{
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();
@ -80,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;
}
}
}
@ -110,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();
@ -136,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;

1
libsolidity/InterfaceHandler.h

@ -74,6 +74,7 @@ public:
/// @return A unique pointer contained string with the json
/// representation of the contract's ABI Interface
std::unique_ptr<std::string> getABIInterface(ContractDefinition const& _contractDef);
std::unique_ptr<std::string> getABISolidityInterface(ContractDefinition const& _contractDef);
/// Get the User documentation of the contract
/// @param _contractDef The contract definition
/// @return A unique pointer contained string with the json

7
libsolidity/Scanner.cpp

@ -455,7 +455,7 @@ void Scanner::scanToken()
token = Token::ADD;
break;
case '-':
// - -- -= Number
// - -- -=
advance();
if (m_char == '-')
{
@ -464,8 +464,6 @@ void Scanner::scanToken()
}
else if (m_char == '=')
token = selectToken(Token::ASSIGN_SUB);
else if (m_char == '.' || isDecimalDigit(m_char))
token = scanNumber('-');
else
token = Token::SUB;
break;
@ -650,8 +648,7 @@ Token::Value Scanner::scanNumber(char _charSeen)
}
else
{
if (_charSeen == '-')
addLiteralChar('-');
solAssert(_charSeen == 0, "");
// if the first character is '0' we must check for octals and hex
if (m_char == '0')
{

288
libsolidity/Types.cpp

@ -44,17 +44,17 @@ shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken)
if (bytes == 0)
bytes = 32;
int modifier = offset / 33;
return make_shared<IntegerType const>(bytes * 8,
return make_shared<IntegerType>(bytes * 8,
modifier == 0 ? IntegerType::Modifier::SIGNED :
modifier == 1 ? IntegerType::Modifier::UNSIGNED :
IntegerType::Modifier::HASH);
}
else if (_typeToken == Token::ADDRESS)
return make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS);
return make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS);
else if (_typeToken == Token::BOOL)
return make_shared<BoolType const>();
return make_shared<BoolType>();
else if (Token::STRING0 <= _typeToken && _typeToken <= Token::STRING32)
return make_shared<StaticStringType const>(int(_typeToken) - int(Token::STRING0));
return make_shared<StaticStringType>(int(_typeToken) - int(Token::STRING0));
else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unable to convert elementary typename " +
std::string(Token::toString(_typeToken)) + " to type."));
@ -64,11 +64,11 @@ shared_ptr<Type const> Type::fromUserDefinedTypeName(UserDefinedTypeName const&
{
Declaration const* declaration = _typeName.getReferencedDeclaration();
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration))
return make_shared<StructType const>(*structDef);
return make_shared<StructType>(*structDef);
else if (FunctionDefinition const* function = dynamic_cast<FunctionDefinition const*>(declaration))
return make_shared<FunctionType const>(*function);
return make_shared<FunctionType>(*function);
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
return make_shared<ContractType const>(*contract);
return make_shared<ContractType>(*contract);
return shared_ptr<Type const>();
}
@ -80,7 +80,7 @@ shared_ptr<Type const> Type::fromMapping(Mapping const& _typeName)
shared_ptr<Type const> valueType = _typeName.getValueType().toType();
if (!valueType)
BOOST_THROW_EXCEPTION(_typeName.getValueType().createTypeError("Invalid type name"));
return make_shared<MappingType const>(keyType, valueType);
return make_shared<MappingType>(keyType, valueType);
}
shared_ptr<Type const> Type::forLiteral(Literal const& _literal)
@ -89,14 +89,14 @@ shared_ptr<Type const> Type::forLiteral(Literal const& _literal)
{
case Token::TRUE_LITERAL:
case Token::FALSE_LITERAL:
return make_shared<BoolType const>();
return make_shared<BoolType>();
case Token::NUMBER:
return IntegerType::smallestTypeForLiteral(_literal.getValue());
return IntegerConstantType::fromLiteral(_literal.getValue());
case Token::STRING_LITERAL:
//@todo put larger strings into dynamic strings
return StaticStringType::smallestTypeForLiteral(_literal.getValue());
default:
return shared_ptr<Type const>();
return shared_ptr<Type>();
}
}
@ -112,19 +112,6 @@ TypePointer Type::commonType(TypePointer const& _a, TypePointer const& _b)
const MemberList Type::EmptyMemberList = MemberList();
shared_ptr<IntegerType const> IntegerType::smallestTypeForLiteral(string const& _literal)
{
bigint value(_literal);
bool isSigned = value < 0 || (!_literal.empty() && _literal.front() == '-');
if (isSigned)
// convert to positive number of same bit requirements
value = ((-value) - 1) << 1;
unsigned bytes = max(bytesRequired(value), 1u);
if (bytes > 32)
return shared_ptr<IntegerType const>();
return make_shared<IntegerType const>(bytes * 8, isSigned ? Modifier::SIGNED : Modifier::UNSIGNED);
}
IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
m_bits(_bits), m_modifier(_modifier)
{
@ -156,18 +143,26 @@ bool IntegerType::isExplicitlyConvertibleTo(Type const& _convertTo) const
return _convertTo.getCategory() == getCategory() || _convertTo.getCategory() == Category::CONTRACT;
}
bool IntegerType::acceptsUnaryOperator(Token::Value _operator) const
TypePointer IntegerType::unaryOperatorResult(Token::Value _operator) const
{
// "delete" is ok for all integer types
if (_operator == Token::DELETE)
return true;
if (isAddress())
return false;
if (_operator == Token::BIT_NOT)
return true;
if (isHash())
return false;
return _operator == Token::ADD || _operator == Token::SUB ||
_operator == Token::INC || _operator == Token::DEC;
return shared_from_this();
// no further unary operators for addresses
else if (isAddress())
return TypePointer();
// "~" is ok for all other types
else if (_operator == Token::BIT_NOT)
return shared_from_this();
// nothing else for hashes
else if (isHash())
return TypePointer();
// for non-hash integers, we allow +, -, ++ and --
else if (_operator == Token::ADD || _operator == Token::SUB ||
_operator == Token::INC || _operator == Token::DEC)
return shared_from_this();
else
return TypePointer();
}
bool IntegerType::operator==(Type const& _other) const
@ -186,17 +181,11 @@ string IntegerType::toString() const
return prefix + dev::toString(m_bits);
}
u256 IntegerType::literalValue(Literal const& _literal) const
{
bigint value(_literal.getValue());
return u256(value);
}
TypePointer IntegerType::binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const
TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
if (getCategory() != _other->getCategory())
if (_other->getCategory() != Category::INTEGER_CONSTANT && _other->getCategory() != getCategory())
return TypePointer();
auto commonType = dynamic_pointer_cast<IntegerType const>(Type::commonType(_this, _other));
auto commonType = dynamic_pointer_cast<IntegerType const>(Type::commonType(shared_from_this(), _other));
if (!commonType)
return TypePointer();
@ -216,17 +205,152 @@ TypePointer IntegerType::binaryOperatorResultImpl(Token::Value _operator, TypePo
const MemberList IntegerType::AddressMemberList =
MemberList({{"balance",
make_shared<IntegerType const>(256)},
make_shared<IntegerType >(256)},
{"callstring32",
make_shared<FunctionType const>(TypePointers({make_shared<StaticStringType const>(32)}),
TypePointers(), FunctionType::Location::BARE)},
make_shared<FunctionType>(TypePointers({make_shared<StaticStringType>(32)}),
TypePointers(), FunctionType::Location::BARE)},
{"callstring32string32",
make_shared<FunctionType const>(TypePointers({make_shared<StaticStringType const>(32),
make_shared<StaticStringType const>(32)}),
TypePointers(), FunctionType::Location::BARE)},
make_shared<FunctionType>(TypePointers({make_shared<StaticStringType>(32),
make_shared<StaticStringType>(32)}),
TypePointers(), FunctionType::Location::BARE)},
{"send",
make_shared<FunctionType const>(TypePointers({make_shared<IntegerType const>(256)}),
TypePointers(), FunctionType::Location::SEND)}});
make_shared<FunctionType>(TypePointers({make_shared<IntegerType>(256)}),
TypePointers(), FunctionType::Location::SEND)}});
shared_ptr<IntegerConstantType const> IntegerConstantType::fromLiteral(string const& _literal)
{
return make_shared<IntegerConstantType>(bigint(_literal));
}
bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{
TypePointer integerType = getIntegerType();
return integerType && integerType->isImplicitlyConvertibleTo(_convertTo);
}
bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{
TypePointer integerType = getIntegerType();
return integerType && integerType->isExplicitlyConvertibleTo(_convertTo);
}
TypePointer IntegerConstantType::unaryOperatorResult(Token::Value _operator) const
{
bigint value;
switch (_operator)
{
case Token::BIT_NOT:
value = ~m_value;
break;
case Token::ADD:
value = m_value;
break;
case Token::SUB:
value = -m_value;
break;
default:
return TypePointer();
}
return make_shared<IntegerConstantType>(value);
}
TypePointer IntegerConstantType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
if (_other->getCategory() == Category::INTEGER)
{
shared_ptr<IntegerType const> integerType = getIntegerType();
if (!integerType)
return TypePointer();
return integerType->binaryOperatorResult(_operator, _other);
}
else if (_other->getCategory() != getCategory())
return TypePointer();
IntegerConstantType const& other = dynamic_cast<IntegerConstantType const&>(*_other);
if (Token::isCompareOp(_operator))
{
shared_ptr<IntegerType const> thisIntegerType = getIntegerType();
shared_ptr<IntegerType const> otherIntegerType = other.getIntegerType();
if (!thisIntegerType || !otherIntegerType)
return TypePointer();
return thisIntegerType->binaryOperatorResult(_operator, otherIntegerType);
}
else
{
bigint value;
switch (_operator)
{
case Token::BIT_OR:
value = m_value | other.m_value;
break;
case Token::BIT_XOR:
value = m_value ^ other.m_value;
break;
case Token::BIT_AND:
value = m_value & other.m_value;
break;
case Token::ADD:
value = m_value + other.m_value;
break;
case Token::SUB:
value = m_value - other.m_value;
break;
case Token::MUL:
value = m_value * other.m_value;
break;
case Token::DIV:
if (other.m_value == 0)
return TypePointer();
value = m_value / other.m_value;
break;
case Token::MOD:
if (other.m_value == 0)
return TypePointer();
value = m_value % other.m_value;
break;
default:
return TypePointer();
}
return make_shared<IntegerConstantType>(value);
}
}
bool IntegerConstantType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
return m_value == dynamic_cast<IntegerConstantType const&>(_other).m_value;
}
string IntegerConstantType::toString() const
{
return "int_const " + m_value.str();
}
u256 IntegerConstantType::literalValue(Literal const*) const
{
// we ignore the literal and hope that the type was correctly determined
solAssert(m_value <= u256(-1), "Integer constant too large.");
solAssert(m_value >= -(bigint(1) << 255), "Integer constant too small.");
if (m_value >= 0)
return u256(m_value);
else
return s2u(s256(m_value));
}
shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const
{
bigint value = m_value;
bool negative = (value < 0);
if (negative) // convert to positive number of same bit requirements
value = ((-value) - 1) << 1;
if (value > u256(-1))
return shared_ptr<IntegerType const>();
else
return make_shared<IntegerType>(max(bytesRequired(value), 1u) * 8,
negative ? IntegerType::Modifier::SIGNED
: IntegerType::Modifier::UNSIGNED);
}
shared_ptr<StaticStringType> StaticStringType::smallestTypeForLiteral(string const& _literal)
{
@ -257,12 +381,13 @@ bool StaticStringType::operator==(Type const& _other) const
return other.m_bytes == m_bytes;
}
u256 StaticStringType::literalValue(const Literal& _literal) const
u256 StaticStringType::literalValue(const Literal* _literal) const
{
solAssert(_literal, "");
u256 value = 0;
for (char c: _literal.getValue())
for (char c: _literal->getValue())
value = (value << 8) | byte(c);
return value << ((32 - _literal.getValue().length()) * 8);
return value << ((32 - _literal->getValue().length()) * 8);
}
bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
@ -278,22 +403,23 @@ bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
return isImplicitlyConvertibleTo(_convertTo);
}
u256 BoolType::literalValue(Literal const& _literal) const
u256 BoolType::literalValue(Literal const* _literal) const
{
if (_literal.getToken() == Token::TRUE_LITERAL)
solAssert(_literal, "");
if (_literal->getToken() == Token::TRUE_LITERAL)
return u256(1);
else if (_literal.getToken() == Token::FALSE_LITERAL)
else if (_literal->getToken() == Token::FALSE_LITERAL)
return u256(0);
else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Bool type constructed from non-boolean literal."));
}
TypePointer BoolType::binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const
TypePointer BoolType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
if (getCategory() != _other->getCategory())
return TypePointer();
if (Token::isCompareOp(_operator) || _operator == Token::AND || _operator == Token::OR)
return _this;
return _other;
else
return TypePointer();
}
@ -334,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;
@ -347,22 +473,20 @@ shared_ptr<FunctionType const> const& ContractType::getConstructorType() const
{
FunctionDefinition const* constructor = m_contract.getConstructor();
if (constructor)
m_constructorType = make_shared<FunctionType const>(*constructor);
m_constructorType = make_shared<FunctionType>(*constructor);
else
m_constructorType = make_shared<FunctionType const>(TypePointers(), TypePointers());
m_constructorType = make_shared<FunctionType>(TypePointers(), TypePointers());
}
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."));
}
@ -521,21 +645,21 @@ MagicType::MagicType(MagicType::Kind _kind):
switch (m_kind)
{
case Kind::BLOCK:
m_members = MemberList({{"coinbase", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)},
{"timestamp", make_shared<IntegerType const>(256)},
{"prevhash", make_shared<IntegerType const>(256, IntegerType::Modifier::HASH)},
{"difficulty", make_shared<IntegerType const>(256)},
{"number", make_shared<IntegerType const>(256)},
{"gaslimit", make_shared<IntegerType const>(256)}});
m_members = MemberList({{"coinbase", make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)},
{"timestamp", make_shared<IntegerType >(256)},
{"prevhash", make_shared<IntegerType>(256, IntegerType::Modifier::HASH)},
{"difficulty", make_shared<IntegerType>(256)},
{"number", make_shared<IntegerType>(256)},
{"gaslimit", make_shared<IntegerType>(256)}});
break;
case Kind::MSG:
m_members = MemberList({{"sender", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)},
{"gas", make_shared<IntegerType const>(256)},
{"value", make_shared<IntegerType const>(256)}});
m_members = MemberList({{"sender", make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)},
{"gas", make_shared<IntegerType>(256)},
{"value", make_shared<IntegerType>(256)}});
break;
case Kind::TX:
m_members = MemberList({{"origin", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)},
{"gasprice", make_shared<IntegerType const>(256)}});
m_members = MemberList({{"origin", make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)},
{"gasprice", make_shared<IntegerType>(256)}});
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of magic."));

130
libsolidity/Types.h

@ -70,12 +70,12 @@ private:
/**
* Abstract base class that forms the root of the type hierarchy.
*/
class Type: private boost::noncopyable
class Type: private boost::noncopyable, public std::enable_shared_from_this<Type>
{
public:
enum class Category
{
INTEGER, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE, MAGIC
INTEGER, INTEGER_CONSTANT, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE, MAGIC
};
///@{
@ -92,12 +92,6 @@ public:
static TypePointer forLiteral(Literal const& _literal);
/// @returns a pointer to _a or _b if the other is implicitly convertible to it or nullptr otherwise
static TypePointer commonType(TypePointer const& _a, TypePointer const& _b);
/// @returns the resulting type of applying the given operator or an empty pointer if this is not possible.
/// The default implementation allows comparison operators if a common type exists
static TypePointer binaryOperatorResult(Token::Value _operator, TypePointer const& _a, TypePointer const& _b)
{
return _a->binaryOperatorResultImpl(_operator, _a, _b);
}
virtual Category getCategory() const = 0;
virtual bool isImplicitlyConvertibleTo(Type const& _other) const { return *this == _other; }
@ -105,7 +99,17 @@ public:
{
return isImplicitlyConvertibleTo(_convertTo);
}
virtual bool acceptsUnaryOperator(Token::Value) const { return false; }
/// @returns the resulting type of applying the given unary operator or an empty pointer if
/// this is not possible.
/// The default implementation does not allow any unary operator.
virtual TypePointer unaryOperatorResult(Token::Value) const { return TypePointer(); }
/// @returns the resulting type of applying the given binary operator or an empty pointer if
/// this is not possible.
/// The default implementation allows comparison operators if a common type exists
virtual TypePointer binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
return Token::isCompareOp(_operator) ? commonType(shared_from_this(), _other) : TypePointer();
}
virtual bool operator==(Type const& _other) const { return getCategory() == _other.getCategory(); }
virtual bool operator!=(Type const& _other) const { return !this->operator ==(_other); }
@ -131,18 +135,13 @@ public:
TypePointer getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); }
virtual std::string toString() const = 0;
virtual u256 literalValue(Literal const&) const
virtual u256 literalValue(Literal const*) const
{
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested "
"for type without literals."));
}
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _a, TypePointer const& _b) const
{
return Token::isCompareOp(_operator) ? commonType(_a, _b) : TypePointer();
}
/// Convenience object used when returning an empty member list.
static const MemberList EmptyMemberList;
};
@ -159,15 +158,12 @@ public:
};
virtual Category getCategory() const override { return Category::INTEGER; }
/// @returns the smallest integer type for the given literal or an empty pointer
/// if no type fits.
static std::shared_ptr<IntegerType const> smallestTypeForLiteral(std::string const& _literal);
explicit IntegerType(int _bits, Modifier _modifier = Modifier::UNSIGNED);
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool acceptsUnaryOperator(Token::Value _operator) const override;
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
virtual TypePointer binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const override;
virtual bool operator==(Type const& _other) const override;
@ -177,22 +173,52 @@ public:
virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; }
virtual std::string toString() const override;
virtual u256 literalValue(Literal const& _literal) const override;
int getNumBits() const { return m_bits; }
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
bool isAddress() const { return m_modifier == Modifier::ADDRESS; }
int isSigned() const { return m_modifier == Modifier::SIGNED; }
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override;
private:
int m_bits;
Modifier m_modifier;
static const MemberList AddressMemberList;
};
/**
* Integer constants either literals or computed. Example expressions: 2, 2+10, ~10.
* There is one distinct type per value.
*/
class IntegerConstantType: public Type
{
public:
virtual Category getCategory() const override { return Category::INTEGER_CONSTANT; }
static std::shared_ptr<IntegerConstantType const> fromLiteral(std::string const& _literal);
explicit IntegerConstantType(bigint _value): m_value(_value) {}
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
virtual TypePointer binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const override;
virtual bool operator==(Type const& _other) const override;
virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const override { return false; }
virtual unsigned getSizeOnStack() const override { return 1; }
virtual std::string toString() const override;
virtual u256 literalValue(Literal const* _literal) const override;
/// @returns the smallest integer type that can hold the value or an empty pointer if not possible.
std::shared_ptr<IntegerType const> getIntegerType() const;
private:
bigint m_value;
};
/**
* String type with fixed length, up to 32 bytes.
*/
@ -214,7 +240,7 @@ public:
virtual bool isValueType() const override { return true; }
virtual std::string toString() const override { return "string" + dev::toString(m_bytes); }
virtual u256 literalValue(Literal const& _literal) const override;
virtual u256 literalValue(Literal const* _literal) const override;
int getNumBytes() const { return m_bytes; }
@ -231,19 +257,17 @@ public:
BoolType() {}
virtual Category getCategory() const { return Category::BOOL; }
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool acceptsUnaryOperator(Token::Value _operator) const override
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override
{
return _operator == Token::NOT || _operator == Token::DELETE;
return (_operator == Token::NOT || _operator == Token::DELETE) ? shared_from_this() : TypePointer();
}
virtual TypePointer binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const override;
virtual unsigned getCalldataEncodedSize() const { return 1; }
virtual bool isValueType() const override { return true; }
virtual std::string toString() const override { return "bool"; }
virtual u256 literalValue(Literal const& _literal) const override;
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override;
virtual u256 literalValue(Literal const* _literal) const override;
};
/**
@ -267,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;
@ -285,9 +309,9 @@ class StructType: public Type
public:
virtual Category getCategory() const override { return Category::STRUCT; }
StructType(StructDefinition const& _struct): m_struct(_struct) {}
virtual bool acceptsUnaryOperator(Token::Value _operator) const override
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override
{
return _operator == Token::DELETE;
return _operator == Token::DELETE ? shared_from_this() : TypePointer();
}
virtual bool operator==(Type const& _other) const override;
@ -318,7 +342,7 @@ public:
/// INTERNAL: jump tag, EXTERNAL: contract address + function index,
/// BARE: contract address (non-abi contract call)
/// OTHERS: special virtual function, nothing on the stack
enum class Location { INTERNAL, EXTERNAL, SEND, SHA3, SUICIDE, ECRECOVER, SHA256, RIPEMD160, BARE };
enum class Location { INTERNAL, EXTERNAL, SEND, SHA3, SUICIDE, ECRECOVER, SHA256, RIPEMD160, LOG0, LOG1, LOG2, LOG3, LOG4, BARE };
virtual Category getCategory() const override { return Category::FUNCTION; }
explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true);
@ -378,20 +402,12 @@ public:
virtual Category getCategory() const override { return Category::VOID; }
VoidType() {}
virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override { return TypePointer(); }
virtual std::string toString() const override { return "void"; }
virtual bool canBeStored() const override { return false; }
virtual u256 getStorageSize() const override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable void type requested.")); }
virtual bool canLiveOutsideStorage() const override { return false; }
virtual unsigned getSizeOnStack() const override { return 0; }
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override
{
(void)_operator;
(void)_this;
(void)_other;
return TypePointer();
}
};
/**
@ -403,24 +419,15 @@ class TypeType: public Type
public:
virtual Category getCategory() const override { return Category::TYPE; }
TypeType(TypePointer const& _actualType): m_actualType(_actualType) {}
TypePointer const& getActualType() const { return m_actualType; }
virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override { return TypePointer(); }
virtual bool operator==(Type const& _other) const override;
virtual bool canBeStored() const override { return false; }
virtual u256 getStorageSize() const override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable type type requested.")); }
virtual bool canLiveOutsideStorage() const override { return false; }
virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; }
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override
{
(void)_operator;
(void)_this;
(void)_other;
return TypePointer();
}
private:
TypePointer m_actualType;
};
@ -437,6 +444,12 @@ public:
virtual Category getCategory() const override { return Category::MAGIC; }
MagicType(Kind _kind);
virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override
{
return TypePointer();
}
virtual bool operator==(Type const& _other) const;
virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const override { return true; }
@ -445,15 +458,6 @@ public:
virtual std::string toString() const override;
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override
{
(void)_operator;
(void)_this;
(void)_other;
return TypePointer();
}
private:
Kind m_kind;

4
mix/AssemblyDebuggerControl.cpp

@ -50,7 +50,7 @@ QString toQString(dev::u256 _value)
return QString::fromStdString(s.str());
}
AssemblyDebuggerControl::AssemblyDebuggerControl(AppContext* _context):
AssemblyDebuggerControl::AssemblyDebuggerControl(dev::mix::AppContext* _context):
Extension(_context, ExtensionDisplayBehavior::ModalDialog), m_running(false)
{
qRegisterMetaType<QVariableDefinition*>("QVariableDefinition*");
@ -111,7 +111,7 @@ void AssemblyDebuggerControl::debugState(QVariantMap _state)
executeSequence(transactionSequence, balance);
}
void AssemblyDebuggerControl::executeSequence(std::vector<TransactionSettings> const& _sequence, u256 _balance)
void AssemblyDebuggerControl::executeSequence(std::vector<TransactionSettings> const& _sequence, dev::u256 _balance)
{
if (m_running)
throw (std::logic_error("debugging already running"));

4
mix/CodeHighlighter.cpp

@ -64,7 +64,7 @@ namespace
};
}
CodeHighlighter::FormatRange::FormatRange(CodeHighlighterSettings::Token _t, solidity::Location const& _location):
CodeHighlighter::FormatRange::FormatRange(CodeHighlighterSettings::Token _t, dev::solidity::Location const& _location):
token(_t), start(_location.start), length(_location.end - _location.start)
{}
@ -91,7 +91,7 @@ void CodeHighlighter::processSource(std::string const& _source)
std::sort(m_formats.begin(), m_formats.end());
}
void CodeHighlighter::processAST(solidity::ASTNode const& _ast)
void CodeHighlighter::processAST(dev::solidity::ASTNode const& _ast)
{
HighlightVisitor visitor(&m_formats);
_ast.accept(visitor);

2
mix/CodeModel.cpp

@ -48,7 +48,7 @@ CompilationResult::CompilationResult():
m_codeHighlighter(new CodeHighlighter())
{}
CompilationResult::CompilationResult(const solidity::CompilerStack& _compiler):
CompilationResult::CompilationResult(const dev::solidity::CompilerStack& _compiler):
QObject(nullptr),
m_successful(true),
m_codeHash(qHash(QString()))

10
mix/QContractDefinition.cpp

@ -33,11 +33,9 @@ using namespace dev::mix;
QContractDefinition::QContractDefinition(dev::solidity::ContractDefinition const* _contract): QBasicNodeDefinition(_contract)
{
std::vector<FunctionDefinition const*> functions = _contract->getInterfaceFunctions();
for (unsigned i = 0; i < functions.size(); i++)
{
FunctionDefinition const* func = functions.at(i);
m_functions.append(new QFunctionDefinition(func, i));
}
auto interfaceFunctions = _contract->getInterfaceFunctions();
unsigned i = 0;
for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it, ++i)
m_functions.append(new QFunctionDefinition(it->second, i));
}

272
neth/main.cpp

@ -24,6 +24,7 @@
#include <chrono>
#include <fstream>
#include <iostream>
#include <signal.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim_all.hpp>
#include <libdevcrypto/FileSystem.h>
@ -63,54 +64,54 @@ void help()
{
cout
<< "Usage neth [OPTIONS] <remote-host>" << endl
<< "Options:" << endl
<< " -a,--address <addr> Set the coinbase (mining payout) address to addr (default: auto)." << endl
<< " -c,--client-name <name> Add a name to your client's version string (default: blank)." << endl
<< " -d,--db-path <path> Load database from path (default: ~/.ethereum " << endl
<< " <APPDATA>/Etherum or Library/Application Support/Ethereum)." << endl
<< " -h,--help Show this help message and exit." << endl
<< "Options:" << endl
<< " -a,--address <addr> Set the coinbase (mining payout) address to addr (default: auto)." << endl
<< " -c,--client-name <name> Add a name to your client's version string (default: blank)." << endl
<< " -d,--db-path <path> Load database from path (default: ~/.ethereum " << endl
<< " <APPDATA>/Etherum or Library/Application Support/Ethereum)." << endl
<< " -h,--help Show this help message and exit." << endl
#if ETH_JSONRPC
<< " -j,--json-rpc Enable JSON-RPC server (default: off)." << endl
<< " --json-rpc-port Specify JSON-RPC server port (implies '-j', default: 8080)." << endl
<< " -j,--json-rpc Enable JSON-RPC server (default: off)." << endl
<< " --json-rpc-port Specify JSON-RPC server port (implies '-j', default: 8080)." << endl
#endif
<< " -l,--listen <port> Listen on the given port for incoming connected (default: 30303)." << endl
<< " -m,--mining <on/off> Enable mining (default: off)" << endl
<< " -n,--upnp <on/off> Use upnp for NAT (default: on)." << endl
<< " -o,--mode <full/peer> Start a full node or a peer node (Default: full)." << endl
<< " -p,--port <port> Connect to remote port (default: 30303)." << endl
<< " -r,--remote <host> Connect to remote host (default: none)." << endl
<< " -s,--secret <secretkeyhex> Set the secret key for use with send command (default: auto)." << endl
<< " -u,--public-ip <ip> Force public ip to given (default; auto)." << endl
<< " -v,--verbosity <0..9> Set the log verbosity from 0 to 9 (tmp forced to 1)." << endl
<< " -x,--peers <number> Attempt to connect to given number of peers (default: 5)." << endl
<< " -V,--version Show the version and exit." << endl;
exit(0);
<< " -l,--listen <port> Listen on the given port for incoming connected (default: 30303)." << endl
<< " -m,--mining <on/off> Enable mining (default: off)" << endl
<< " -n,--upnp <on/off> Use upnp for NAT (default: on)." << endl
<< " -o,--mode <full/peer> Start a full node or a peer node (Default: full)." << endl
<< " -p,--port <port> Connect to remote port (default: 30303)." << endl
<< " -r,--remote <host> Connect to remote host (default: none)." << endl
<< " -s,--secret <secretkeyhex> Set the secret key for use with send command (default: auto)." << endl
<< " -u,--public-ip <ip> Force public ip to given (default; auto)." << endl
<< " -v,--verbosity <0..9> Set the log verbosity from 0 to 9 (tmp forced to 1)." << endl
<< " -x,--peers <number> Attempt to connect to given number of peers (default: 5)." << endl
<< " -V,--version Show the version and exit." << endl;
exit(0);
}
void interactiveHelp()
{
cout
<< "Commands:" << endl
<< " netstart <port> Starts the network sybsystem on a specific port." << endl
<< " netstop Stops the network subsystem." << endl
<< "Commands:" << endl
<< " netstart <port> Starts the network sybsystem on a specific port." << endl
<< " netstop Stops the network subsystem." << endl
#if ETH_JSONRPC
<< " jsonstart <port> Starts the JSON-RPC server." << endl
<< " jsonstop Stops the JSON-RPC server." << endl
<< " jsonstart <port> Starts the JSON-RPC server." << endl
<< " jsonstop Stops the JSON-RPC server." << endl
#endif
<< " connect <addr> <port> Connects to a specific peer." << endl
<< " minestart Starts mining." << endl
<< " minestop Stops mining." << endl
<< " address Gives the current address." << endl
<< " secret Gives the current secret" << endl
<< " block Gives the current block height." << endl
<< " balance Gives the current balance." << endl
<< " peers List the peers that are connected" << endl
<< " transact Execute a given transaction." << endl
<< " send Execute a given transaction with current secret." << endl
<< " contract Create a new contract with current secret." << endl
<< " inspect <contract> Dumps a contract to <APPDATA>/<contract>.evm." << endl
<< " reset Resets ncurses windows" << endl
<< " exit Exits the application." << endl;
<< " connect <addr> <port> Connects to a specific peer." << endl
<< " minestart Starts mining." << endl
<< " minestop Stops mining." << endl
<< " address Gives the current address." << endl
<< " secret Gives the current secret" << endl
<< " block Gives the current block height." << endl
<< " balance Gives the current balance." << endl
<< " peers List the peers that are connected" << endl
<< " transact Execute a given transaction." << endl
<< " send Execute a given transaction with current secret." << endl
<< " contract Create a new contract with current secret." << endl
<< " inspect <contract> Dumps a contract to <APPDATA>/<contract>.evm." << endl
<< " reset Resets ncurses windows" << endl
<< " exit Exits the application." << endl;
}
string credits()
@ -121,17 +122,8 @@ string credits()
<< " Code by Gav Wood & , (c) 2013, 2014." << endl
<< " Based on a design by Vitalik Buterin." << endl << endl;
string vs = toString(dev::Version);
vs = vs.substr(vs.find_first_of('.') + 1)[0];
int pocnumber = stoi(vs);
string m_servers;
if (pocnumber == 5)
m_servers = "54.72.69.180";
else
m_servers = "54.76.56.74";
ccout << "Type 'netstart 30303' to start networking" << endl;
ccout << "Type 'connect " << m_servers << " 30303' to connect" << endl;
ccout << "Type 'connect " << Host::pocHost() << " 30303' to connect" << endl;
ccout << "Type 'exit' to quit" << endl << endl;
return ccout.str();
}
@ -139,12 +131,13 @@ string credits()
void version()
{
cout << "neth version " << dev::Version << endl;
cout << "Network protocol version: " << dev::eth::c_protocolVersion << endl;
cout << "Client database version: " << dev::eth::c_databaseVersion << endl;
cout << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl;
exit(0);
}
Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
string pretty(h160 _a, dev::eth::State _st)
{
string ns;
@ -161,6 +154,13 @@ string pretty(h160 _a, dev::eth::State _st)
return ns;
}
bool g_exit = false;
void sighandler(int)
{
g_exit = true;
}
namespace nc
{
@ -298,13 +298,17 @@ int main(int argc, char** argv)
string remoteHost;
unsigned short remotePort = 30303;
string dbPath;
bool mining = false;
unsigned mining = ~(unsigned)0;
NodeMode mode = NodeMode::Full;
unsigned peers = 5;
#if ETH_JSONRPC
int jsonrpc = 8080;
#endif
string publicIP;
bool bootstrap = false;
bool upnp = true;
bool useLocal = false;
bool forceMining = false;
string clientName;
// Init defaults
@ -365,14 +369,21 @@ int main(int argc, char** argv)
{
string m = argv[++i];
if (isTrue(m))
mining = true;
mining = ~(unsigned)0;
else if (isFalse(m))
mining = false;
mining = 0;
else if (int i = stoi(m))
mining = i;
else
{
cerr << "Unknown mining option: " << m << endl;
cerr << "Unknown -m/--mining option: " << m << endl;
return -1;
}
}
else if (arg == "-b" || arg == "--bootstrap")
bootstrap = true;
else if (arg == "-f" || arg == "--force-mining")
forceMining = true;
#if ETH_JSONRPC
else if ((arg == "-j" || arg == "--json-rpc"))
jsonrpc = jsonrpc ? jsonrpc : 8080;
@ -394,12 +405,50 @@ int main(int argc, char** argv)
if (!clientName.empty())
clientName += "/";
WebThreeDirect web3("NEthereum(++)/" + clientName + "v" + dev::Version + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM), dbPath);
Client& c = *web3.ethereum();
cout << credits();
NetworkPreferences netPrefs(listenPort, publicIP, upnp, useLocal);
dev::WebThreeDirect web3(
"NEthereum(++)/" + clientName + "v" + dev::Version + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM),
dbPath,
false,
mode == NodeMode::Full ? set<string>{"eth", "shh"} : set<string>(),
netPrefs
);
web3.setIdealPeerCount(peers);
eth::Client* c = mode == NodeMode::Full ? web3.ethereum() : nullptr;
if (c)
{
c->setForceMining(forceMining);
c->setAddress(coinbase);
}
c.setForceMining(true);
auto nodesState = contents((dbPath.size() ? dbPath : getDataDir()) + "/nodeState.rlp");
web3.restoreNodes(&nodesState);
cout << credits();
web3.startNetwork();
if (bootstrap)
web3.connect(Host::pocHost());
if (remoteHost.size())
web3.connect(remoteHost, remotePort);
#if ETH_JSONRPC
shared_ptr<WebThreeStubServer> jsonrpcServer;
unique_ptr<jsonrpc::AbstractServerConnector> jsonrpcConnector;
if (jsonrpc > -1)
{
jsonrpcConnector = unique_ptr<jsonrpc::AbstractServerConnector>(new jsonrpc::HttpServer(jsonrpc));
jsonrpcServer = shared_ptr<WebThreeStubServer>(new WebThreeStubServer(*jsonrpcConnector.get(), web3, vector<KeyPair>({us})));
jsonrpcServer->setIdentities({us});
jsonrpcServer->StartListening();
}
#endif
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler);
std::ostringstream ccout;
@ -461,28 +510,6 @@ int main(int argc, char** argv)
wmove(mainwin, 1, 4);
if (!remoteHost.empty())
{
web3.setIdealPeerCount(peers);
web3.setNetworkPreferences(NetworkPreferences(listenPort, publicIP, upnp));
web3.startNetwork();
web3.connect(remoteHost, remotePort);
}
if (mining)
c.startMining();
#if ETH_JSONRPC
shared_ptr<WebThreeStubServer> jsonrpcServer;
unique_ptr<jsonrpc::AbstractServerConnector> jsonrpcConnector;
if (jsonrpc > -1)
{
jsonrpcConnector = unique_ptr<jsonrpc::AbstractServerConnector>(new jsonrpc::HttpServer(jsonrpc));
jsonrpcServer = shared_ptr<WebThreeStubServer>(new WebThreeStubServer(*jsonrpcConnector.get(), web3, vector<KeyPair>({us})));
jsonrpcServer->setIdentities({us});
jsonrpcServer->StartListening();
}
#endif
while (true)
{
wclrtobot(consolewin);
@ -532,13 +559,25 @@ int main(int argc, char** argv)
{
web3.stopNetwork();
}
else if (cmd == "minestart")
else if (c && cmd == "minestart")
{
c->startMining();
}
else if (c && cmd == "minestop")
{
c.startMining();
c->stopMining();
}
else if (cmd == "minestop")
else if (c && cmd == "mineforce")
{
c.stopMining();
string enable;
iss >> enable;
c->setForceMining(isTrue(enable));
}
else if (cmd == "verbosity")
{
if (iss.peek() != -1)
iss >> g_logVerbosity;
cout << "Verbosity: " << g_logVerbosity << endl;
}
#if ETH_JSONRPC
else if (cmd == "jsonport")
@ -575,7 +614,7 @@ int main(int argc, char** argv)
}
else if (cmd == "block")
{
unsigned n = c.blockChain().details().number;
unsigned n = c->blockChain().details().number;
ccout << "Current block # ";
ccout << toString(n) << endl;
}
@ -588,13 +627,13 @@ int main(int argc, char** argv)
}
else if (cmd == "balance")
{
u256 balance = c.balanceAt(us.address());
u256 balance = c->balanceAt(us.address());
ccout << "Current balance:" << endl;
ccout << toString(balance) << endl;
}
else if (cmd == "transact")
{
auto const& bc = c.blockChain();
auto const& bc = c->blockChain();
auto h = bc.currentHash();
auto blockData = bc.block(h);
BlockInfo info(blockData);
@ -663,7 +702,7 @@ int main(int argc, char** argv)
{
Secret secret = h256(fromHex(sechex));
Address dest = h160(fromHex(fields[0]));
c.transact(secret, amount, dest, data, gas, gasPrice);
c->transact(secret, amount, dest, data, gas, gasPrice);
}
}
}
@ -696,19 +735,19 @@ int main(int argc, char** argv)
}
else
{
auto const& bc = c.blockChain();
auto const& bc = c->blockChain();
auto h = bc.currentHash();
auto blockData = bc.block(h);
BlockInfo info(blockData);
u256 minGas = (u256)Client::txGas(bytes(), 0);
Address dest = h160(fromHex(fields[0]));
c.transact(us.secret(), amount, dest, bytes(), minGas);
c->transact(us.secret(), amount, dest, bytes(), minGas);
}
}
}
else if (cmd == "contract")
{
auto const& bc = c.blockChain();
auto const& bc = c->blockChain();
auto h = bc.currentHash();
auto blockData = bc.block(h);
BlockInfo info(blockData);
@ -768,7 +807,7 @@ int main(int argc, char** argv)
cwarn << "Minimum gas amount is" << minGas;
else
{
c.transact(us.secret(), endowment, init, gas);
c->transact(us.secret(), endowment, init, gas);
}
}
}
@ -786,10 +825,10 @@ int main(int argc, char** argv)
try
{
auto storage = c.storageAt(address);
auto storage = c->storageAt(address);
for (auto const& i: storage)
s << "@" << showbase << hex << i.first << " " << showbase << hex << i.second << endl;
s << endl << disassemble(c.codeAt(address)) << endl;
s << endl << disassemble(c->codeAt(address)) << endl;
string outFile = getDataDir() + "/" + rechex + ".evm";
ofstream ofs;
@ -824,7 +863,7 @@ int main(int argc, char** argv)
// Lock to prevent corrupt block-chain errors
auto const& bc = c.blockChain();
auto const& bc = c->blockChain();
ccout << "Genesis hash: " << bc.genesisHash() << endl;
// Blocks
@ -838,11 +877,11 @@ int main(int argc, char** argv)
auto b = bc.block(h);
for (auto const& i: RLP(b)[1])
{
Transaction t(i[0].data());
Transaction t(i.data());
auto s = t.receiveAddress() ?
boost::format(" %1% %2%> %3%: %4% [%5%]") %
toString(t.safeSender()) %
(c.codeAt(t.receiveAddress(), 0).size() ? '*' : '-') %
(c->codeAt(t.receiveAddress(), 0).size() ? '*' : '-') %
toString(t.receiveAddress()) %
toString(formatBalance(t.value())) %
toString((unsigned)t.nonce()) :
@ -862,12 +901,12 @@ int main(int argc, char** argv)
// Pending
y = 1;
for (Transaction const& t: c.pending())
for (Transaction const& t: c->pending())
{
auto s = t.receiveAddress() ?
boost::format("%1% %2%> %3%: %4% [%5%]") %
toString(t.safeSender()) %
(c.codeAt(t.receiveAddress(), 0).size() ? '*' : '-') %
(c->codeAt(t.receiveAddress(), 0).size() ? '*' : '-') %
toString(t.receiveAddress()) %
toString(formatBalance(t.value())) %
toString((unsigned)t.nonce()) :
@ -877,7 +916,7 @@ int main(int argc, char** argv)
toString(formatBalance(t.value())) %
toString((unsigned)t.nonce());
mvwaddnstr(pendingwin, y++, x, s.str().c_str(), qwidth);
if (y > height * 1 / 5 - 4)
if (y > height * 1 / 5 - 2)
break;
}
@ -885,27 +924,27 @@ int main(int argc, char** argv)
// Contracts and addresses
y = 1;
int cc = 1;
auto acs = c.addresses();
auto acs = c->addresses();
for (auto const& i: acs)
if (c.codeAt(i, 0).size())
if (c->codeAt(i, 0).size())
{
auto s = boost::format("%1%%2% : %3% [%4%]") %
toString(i) %
pretty(i, c.postState()) %
toString(formatBalance(c.balanceAt(i))) %
toString((unsigned)c.countAt(i, 0));
pretty(i, c->postState()) %
toString(formatBalance(c->balanceAt(i))) %
toString((unsigned)c->countAt(i, 0));
mvwaddnstr(contractswin, cc++, x, s.str().c_str(), qwidth);
if (cc > qheight - 2)
break;
}
for (auto const& i: acs)
if (c.codeAt(i, 0).empty())
if (c->codeAt(i, 0).empty())
{
auto s = boost::format("%1%%2% : %3% [%4%]") %
toString(i) %
pretty(i, c.postState()) %
toString(formatBalance(c.balanceAt(i))) %
toString((unsigned)c.countAt(i, 0));
pretty(i, c->postState()) %
toString(formatBalance(c->balanceAt(i))) %
toString((unsigned)c->countAt(i, 0));
mvwaddnstr(addswin, y++, x, s.str().c_str(), width / 2 - 4);
if (y > height * 3 / 5 - 4)
break;
@ -935,21 +974,18 @@ int main(int argc, char** argv)
// Balance
stringstream ssb;
u256 balance = c.balanceAt(us.address());
Address coinsAddr = right160(c.stateAt(c_config, 1));
Address gavCoin = right160(c.stateAt(coinsAddr, c.stateAt(coinsAddr, 1)));
u256 totalGavCoinBalance = c.stateAt(gavCoin, (u160)us.address());
ssb << "Balance: " << formatBalance(balance) << " | " << totalGavCoinBalance << " GAV";
u256 balance = c->balanceAt(us.address());
ssb << "Balance: " << formatBalance(balance);
mvwprintw(consolewin, 0, x, ssb.str().c_str());
// Block
mvwprintw(blockswin, 0, x, "Block # ");
unsigned n = c.blockChain().details().number;
unsigned n = c->blockChain().details().number;
mvwprintw(blockswin, 0, 10, toString(n).c_str());
// Pending
string pc;
pc = "Pending: " + toString(c.pending().size());
pc = "Pending: " + toString(c->pending().size());
mvwprintw(pendingwin, 0, x, pc.c_str());
// Contracts
@ -962,10 +998,10 @@ int main(int argc, char** argv)
mvwprintw(peerswin, 0, 9, toString(web3.peers().size()).c_str());
// Mining flag
if (c.isMining())
if (c->isMining())
{
mvwprintw(consolewin, qheight - 1, width / 4 - 11, "Mining ON");
dev::eth::MineProgress p = c.miningProgress();
dev::eth::MineProgress p = c->miningProgress();
auto speed = boost::format("%2% kH/s @ %1%s") % (p.ms / 1000) % (p.ms ? p.hashes / p.ms : 0);
mvwprintw(consolewin, qheight - 2, width / 4 - speed.str().length() - 2, speed.str().c_str());
}

41
solc/CommandLineInterface.cpp

@ -16,6 +16,7 @@
*/
/**
* @author Lefteris <lefteris@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Solidity command line interface.
*/
@ -50,7 +51,8 @@ namespace solidity
// LTODO: Maybe some argument class pairing names with
// extensions and other attributes would be a better choice here?
static string const g_argAbiStr = "abi";
static string const g_argAbiStr = "json-abi";
static string const g_argSolAbiStr = "sol-abi";
static string const g_argAsmStr = "asm";
static string const g_argAstStr = "ast";
static string const g_argBinaryStr = "binary";
@ -60,7 +62,7 @@ static string const g_argNatspecUserStr = "natspec-user";
static void version()
{
cout << "solc, the solidity complier commandline interface " << dev::Version << endl
cout << "solc, the solidity compiler commandline interface " << dev::Version << endl
<< " by Christian <c@ethdev.com> and Lefteris <lefteris@ethdev.com>, (c) 2014." << endl
<< "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl;
exit(0);
@ -73,9 +75,11 @@ static inline bool argToStdout(po::variables_map const& _args, string const& _na
static bool needStdout(po::variables_map const& _args)
{
return argToStdout(_args, g_argAbiStr) || argToStdout(_args, g_argNatspecUserStr) ||
argToStdout(_args, g_argNatspecDevStr) || argToStdout(_args, g_argAsmStr) ||
argToStdout(_args, g_argOpcodesStr) || argToStdout(_args, g_argBinaryStr);
return
argToStdout(_args, g_argAbiStr) || argToStdout(_args, g_argSolAbiStr) ||
argToStdout(_args, g_argNatspecUserStr) ||
argToStdout(_args, g_argNatspecDevStr) || argToStdout(_args, g_argAsmStr) ||
argToStdout(_args, g_argOpcodesStr) || argToStdout(_args, g_argBinaryStr);
}
static inline bool outputToFile(OutputType type)
@ -146,8 +150,7 @@ void CommandLineInterface::handleBytecode(string const& _contract)
handleBinary(_contract);
}
void CommandLineInterface::handleJson(DocumentationType _type,
string const& _contract)
void CommandLineInterface::handleMeta(DocumentationType _type, string const& _contract)
{
std::string argName;
std::string suffix;
@ -157,10 +160,15 @@ void CommandLineInterface::handleJson(DocumentationType _type,
case DocumentationType::ABI_INTERFACE:
argName = g_argAbiStr;
suffix = ".abi";
title = "Contract ABI";
title = "Contract JSON ABI";
break;
case DocumentationType::ABI_SOLIDITY_INTERFACE:
argName = g_argSolAbiStr;
suffix = ".sol";
title = "Contract Solidity ABI";
break;
case DocumentationType::NATSPEC_USER:
argName = "g_argNatspecUserStr";
argName = g_argNatspecUserStr;
suffix = ".docuser";
title = "User Documentation";
break;
@ -180,13 +188,13 @@ void CommandLineInterface::handleJson(DocumentationType _type,
if (outputToStdout(choice))
{
cout << title << endl;
cout << m_compiler.getJsonDocumentation(_contract, _type);
cout << m_compiler.getMetadata(_contract, _type) << endl;
}
if (outputToFile(choice))
{
ofstream outFile(_contract + suffix);
outFile << m_compiler.getJsonDocumentation(_contract, _type);
outFile << m_compiler.getMetadata(_contract, _type);
outFile.close();
}
}
@ -214,7 +222,9 @@ bool CommandLineInterface::parseArguments(int argc, char** argv)
(g_argBinaryStr.c_str(), po::value<OutputType>(),
"Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR)
(g_argAbiStr.c_str(), po::value<OutputType>(),
"Request to output the contract's ABI interface. " OUTPUT_TYPE_STR)
"Request to output the contract's JSON ABI interface. " OUTPUT_TYPE_STR)
(g_argSolAbiStr.c_str(), po::value<OutputType>(),
"Request to output the contract's Solidity ABI interface. " OUTPUT_TYPE_STR)
(g_argNatspecUserStr.c_str(), po::value<OutputType>(),
"Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR)
(g_argNatspecDevStr.c_str(), po::value<OutputType>(),
@ -384,9 +394,10 @@ void CommandLineInterface::actOnInput()
}
handleBytecode(contract);
handleJson(DocumentationType::ABI_INTERFACE, contract);
handleJson(DocumentationType::NATSPEC_DEV, contract);
handleJson(DocumentationType::NATSPEC_USER, contract);
handleMeta(DocumentationType::ABI_INTERFACE, contract);
handleMeta(DocumentationType::ABI_SOLIDITY_INTERFACE, contract);
handleMeta(DocumentationType::NATSPEC_DEV, contract);
handleMeta(DocumentationType::NATSPEC_USER, contract);
} // end of contracts iteration
}

2
solc/CommandLineInterface.h

@ -56,7 +56,7 @@ private:
void handleBinary(std::string const& _contract);
void handleOpcode(std::string const& _contract);
void handleBytecode(std::string const& _contract);
void handleJson(DocumentationType _type,
void handleMeta(DocumentationType _type,
std::string const& _contract);
/// Compiler arguments variable map

30
test/SolidityABIJSON.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.getMetadata("", DocumentationType::ABI_INTERFACE);
Json::Value generatedInterface;
m_reader.parse(generatedInterfaceString, generatedInterface);
Json::Value expectedInterface;
@ -236,20 +236,6 @@ BOOST_AUTO_TEST_CASE(const_function)
"}\n";
char const* interface = R"([
{
"name": "boo",
"constant": true,
"inputs": [{
"name": "a",
"type": "uint32"
}],
"outputs": [
{
"name": "b",
"type": "uint256"
}
]
},
{
"name": "foo",
"constant": false,
@ -269,6 +255,20 @@ BOOST_AUTO_TEST_CASE(const_function)
"type": "uint256"
}
]
},
{
"name": "boo",
"constant": true,
"inputs": [{
"name": "a",
"type": "uint32"
}],
"outputs": [
{
"name": "b",
"type": "uint256"
}
]
}
])";

25
test/SolidityCompiler.cpp

@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
"}\n";
bytes code = compileContract(sourceCode);
unsigned boilerplateSize = 40;
unsigned boilerplateSize = 73;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x0, // initialize local variable x
byte(Instruction::PUSH1), 0x2,
@ -115,9 +115,8 @@ BOOST_AUTO_TEST_CASE(different_argument_numbers)
" function g() returns (uint e, uint h) { h = f(1, 2, 3); }\n"
"}\n";
bytes code = compileContract(sourceCode);
unsigned shift = 68;
unsigned boilerplateSize = 81;
unsigned shift = 103;
unsigned boilerplateSize = 116;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x0, // initialize return variable d
byte(Instruction::DUP3),
@ -135,10 +134,10 @@ BOOST_AUTO_TEST_CASE(different_argument_numbers)
byte(Instruction::JUMPDEST), // beginning of g
byte(Instruction::PUSH1), 0x0,
byte(Instruction::PUSH1), 0x0, // initialized e and h
byte(Instruction::PUSH1), byte(0x2a + shift), // ret address
byte(Instruction::PUSH1), 0x1, byte(Instruction::PUSH1), 0xff, byte(Instruction::AND),
byte(Instruction::PUSH1), 0x2, byte(Instruction::PUSH1), 0xff, byte(Instruction::AND),
byte(Instruction::PUSH1), 0x3, byte(Instruction::PUSH1), 0xff, byte(Instruction::AND),
byte(Instruction::PUSH1), byte(0x21 + shift), // ret address
byte(Instruction::PUSH1), 0x1,
byte(Instruction::PUSH1), 0x2,
byte(Instruction::PUSH1), 0x3,
byte(Instruction::PUSH1), byte(0x1 + shift),
// stack here: ret e h 0x20 1 2 3 0x1
byte(Instruction::JUMP),
@ -166,9 +165,8 @@ BOOST_AUTO_TEST_CASE(ifStatement)
" function f() { bool x; if (x) 77; else if (!x) 78; else 79; }"
"}\n";
bytes code = compileContract(sourceCode);
unsigned shift = 27;
unsigned boilerplateSize = 40;
unsigned shift = 60;
unsigned boilerplateSize = 73;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x0,
byte(Instruction::DUP1),
@ -208,9 +206,8 @@ BOOST_AUTO_TEST_CASE(loops)
" function f() { while(true){1;break;2;continue;3;return;4;} }"
"}\n";
bytes code = compileContract(sourceCode);
unsigned shift = 27;
unsigned boilerplateSize = 40;
unsigned shift = 60;
unsigned boilerplateSize = 73;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x1,

377
test/SolidityEndToEndTest.cpp

@ -17,6 +17,7 @@
*/
/**
* @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014
* Unit tests for the solidity expression compiler, testing the behaviour of the code.
*/
@ -44,7 +45,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
" function f(uint a) returns(uint d) { return a * 7; }\n"
"}\n";
compileAndRun(sourceCode);
testSolidityAgainstCppOnRange(0, [](u256 const& a) -> u256 { return a * 7; }, 0, 100);
testSolidityAgainstCppOnRange("f(uint256)", [](u256 const& a) -> u256 { return a * 7; }, 0, 100);
}
BOOST_AUTO_TEST_CASE(empty_contract)
@ -52,7 +53,7 @@ BOOST_AUTO_TEST_CASE(empty_contract)
char const* sourceCode = "contract test {\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, bytes()).empty());
BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()).empty());
}
BOOST_AUTO_TEST_CASE(recursive_calls)
@ -72,7 +73,7 @@ BOOST_AUTO_TEST_CASE(recursive_calls)
return n * recursive_calls_cpp(n - 1);
};
testSolidityAgainstCppOnRange(0, recursive_calls_cpp, 0, 5);
testSolidityAgainstCppOnRange("f(uint256)", recursive_calls_cpp, 0, 5);
}
BOOST_AUTO_TEST_CASE(multiple_functions)
@ -84,11 +85,11 @@ BOOST_AUTO_TEST_CASE(multiple_functions)
" function f() returns(uint n) { return 3; }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, bytes()) == toBigEndian(u256(0)));
BOOST_CHECK(callContractFunction(1, bytes()) == toBigEndian(u256(1)));
BOOST_CHECK(callContractFunction(2, bytes()) == toBigEndian(u256(2)));
BOOST_CHECK(callContractFunction(3, bytes()) == toBigEndian(u256(3)));
BOOST_CHECK(callContractFunction(4, bytes()) == bytes());
BOOST_CHECK(callContractFunction("a()", bytes()) == toBigEndian(u256(0)));
BOOST_CHECK(callContractFunction("b()", bytes()) == toBigEndian(u256(1)));
BOOST_CHECK(callContractFunction("c()", bytes()) == toBigEndian(u256(2)));
BOOST_CHECK(callContractFunction("f()", bytes()) == toBigEndian(u256(3)));
BOOST_CHECK(callContractFunction("i_am_not_there()", bytes()) == bytes());
}
BOOST_AUTO_TEST_CASE(while_loop)
@ -112,7 +113,7 @@ BOOST_AUTO_TEST_CASE(while_loop)
return nfac;
};
testSolidityAgainstCppOnRange(0, while_loop_cpp, 0, 5);
testSolidityAgainstCppOnRange("f(uint256)", while_loop_cpp, 0, 5);
}
BOOST_AUTO_TEST_CASE(break_outside_loop)
@ -124,7 +125,7 @@ BOOST_AUTO_TEST_CASE(break_outside_loop)
" }\n"
"}\n";
compileAndRun(sourceCode);
testSolidityAgainstCpp(0, [](u256 const&) -> u256 { return 2; }, u256(0));
testSolidityAgainstCpp("f(uint256)", [](u256 const&) -> u256 { return 2; }, u256(0));
}
BOOST_AUTO_TEST_CASE(nested_loops)
@ -173,7 +174,7 @@ BOOST_AUTO_TEST_CASE(nested_loops)
return n;
};
testSolidityAgainstCppOnRange(0, nested_loops_cpp, 0, 12);
testSolidityAgainstCppOnRange("f(uint256)", nested_loops_cpp, 0, 12);
}
BOOST_AUTO_TEST_CASE(for_loop)
@ -195,7 +196,7 @@ BOOST_AUTO_TEST_CASE(for_loop)
return nfac;
};
testSolidityAgainstCppOnRange(0, for_loop_cpp, 0, 5);
testSolidityAgainstCppOnRange("f(uint256)", for_loop_cpp, 0, 5);
}
BOOST_AUTO_TEST_CASE(for_loop_empty)
@ -223,7 +224,7 @@ BOOST_AUTO_TEST_CASE(for_loop_empty)
return ret;
};
testSolidityAgainstCpp(0, for_loop_empty_cpp);
testSolidityAgainstCpp("f()", for_loop_empty_cpp);
}
BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr)
@ -247,7 +248,7 @@ BOOST_AUTO_TEST_CASE(for_loop_simple_init_expr)
return nfac;
};
testSolidityAgainstCppOnRange(0, for_loop_simple_init_expr_cpp, 0, 5);
testSolidityAgainstCppOnRange("f(uint256)", for_loop_simple_init_expr_cpp, 0, 5);
}
BOOST_AUTO_TEST_CASE(calling_other_functions)
@ -292,11 +293,11 @@ BOOST_AUTO_TEST_CASE(calling_other_functions)
return y;
};
testSolidityAgainstCpp(2, collatz_cpp, u256(0));
testSolidityAgainstCpp(2, collatz_cpp, u256(1));
testSolidityAgainstCpp(2, collatz_cpp, u256(2));
testSolidityAgainstCpp(2, collatz_cpp, u256(8));
testSolidityAgainstCpp(2, collatz_cpp, u256(127));
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(0));
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(1));
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(2));
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(8));
testSolidityAgainstCpp("run(uint256)", collatz_cpp, u256(127));
}
BOOST_AUTO_TEST_CASE(many_local_variables)
@ -317,7 +318,7 @@ BOOST_AUTO_TEST_CASE(many_local_variables)
u256 y = a + b + c + x1 + x2 + x3;
return y + b + x2;
};
testSolidityAgainstCpp(0, f, u256(0x1000), u256(0x10000), u256(0x100000));
testSolidityAgainstCpp("run(uint256,uint256,uint256)", f, u256(0x1000), u256(0x10000), u256(0x100000));
}
BOOST_AUTO_TEST_CASE(packing_unpacking_types)
@ -330,7 +331,7 @@ BOOST_AUTO_TEST_CASE(packing_unpacking_types)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0"))
BOOST_CHECK(callContractFunction("run(bool,uint32,uint64)", fromHex("01""0f0f0f0f""f0f0f0f0f0f0f0f0"))
== fromHex("00000000000000000000000000000000000000""01""f0f0f0f0""0f0f0f0f0f0f0f0f"));
}
@ -342,7 +343,7 @@ BOOST_AUTO_TEST_CASE(multiple_return_values)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, bytes(1, 1) + toBigEndian(u256(0xcd)))
BOOST_CHECK(callContractFunction("run(bool,uint256)", bytes(1, 1) + toBigEndian(u256(0xcd)))
== toBigEndian(u256(0xcd)) + bytes(1, 1) + toBigEndian(u256(0)));
}
@ -362,7 +363,7 @@ BOOST_AUTO_TEST_CASE(short_circuiting)
return n;
};
testSolidityAgainstCppOnRange(0, short_circuiting_cpp, 0, 2);
testSolidityAgainstCppOnRange("run(uint256)", short_circuiting_cpp, 0, 2);
}
BOOST_AUTO_TEST_CASE(high_bits_cleaning)
@ -382,7 +383,7 @@ BOOST_AUTO_TEST_CASE(high_bits_cleaning)
return 0;
return x;
};
testSolidityAgainstCpp(0, high_bits_cleaning_cpp);
testSolidityAgainstCpp("run()", high_bits_cleaning_cpp);
}
BOOST_AUTO_TEST_CASE(sign_extension)
@ -402,7 +403,7 @@ BOOST_AUTO_TEST_CASE(sign_extension)
return 0;
return u256(x) * -1;
};
testSolidityAgainstCpp(0, sign_extension_cpp);
testSolidityAgainstCpp("run()", sign_extension_cpp);
}
BOOST_AUTO_TEST_CASE(small_unsigned_types)
@ -419,7 +420,7 @@ BOOST_AUTO_TEST_CASE(small_unsigned_types)
uint32_t x = uint32_t(0xffffff) * 0xffffff;
return x / 0x100;
};
testSolidityAgainstCpp(0, small_unsigned_types_cpp);
testSolidityAgainstCpp("run()", small_unsigned_types_cpp);
}
BOOST_AUTO_TEST_CASE(small_signed_types)
@ -434,7 +435,7 @@ BOOST_AUTO_TEST_CASE(small_signed_types)
{
return -int32_t(10) * -int64_t(20);
};
testSolidityAgainstCpp(0, small_signed_types_cpp);
testSolidityAgainstCpp("run()", small_signed_types_cpp);
}
BOOST_AUTO_TEST_CASE(strings)
@ -457,12 +458,12 @@ BOOST_AUTO_TEST_CASE(strings)
expectation[4] = byte(0xff);
expectation[5] = byte('_');
expectation[6] = byte('_');
BOOST_CHECK(callContractFunction(0, bytes()) == expectation);
BOOST_CHECK(callContractFunction("fixed()", bytes()) == expectation);
expectation = bytes(17, 0);
expectation[0] = 0;
expectation[1] = 2;
expectation[16] = 1;
BOOST_CHECK(callContractFunction(1, bytes({0x00, 0x02, 0x01})) == expectation);
BOOST_CHECK(callContractFunction("pipeThrough(string2,bool)", bytes({0x00, 0x02, 0x01})) == expectation);
}
BOOST_AUTO_TEST_CASE(empty_string_on_stack)
@ -476,7 +477,7 @@ BOOST_AUTO_TEST_CASE(empty_string_on_stack)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, bytes(1, 0x02)) == bytes({0x00, 0x02, 0x61/*'a'*/, 0x62/*'b'*/, 0x63/*'c'*/, 0x00}));
BOOST_CHECK(callContractFunction("run(string0,uint8)", bytes(1, 0x02)) == bytes({0x00, 0x02, 0x61/*'a'*/, 0x62/*'b'*/, 0x63/*'c'*/, 0x00}));
}
BOOST_AUTO_TEST_CASE(state_smoke_test)
@ -494,14 +495,14 @@ BOOST_AUTO_TEST_CASE(state_smoke_test)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0)));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0)));
BOOST_CHECK(callContractFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes());
BOOST_CHECK(callContractFunction(1, bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes());
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x1234)));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == toBigEndian(u256(0x8765)));
BOOST_CHECK(callContractFunction(1, bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes());
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == toBigEndian(u256(0x3)));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0)));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == toBigEndian(u256(0)));
BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x00) + toBigEndian(u256(0x1234))) == bytes());
BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x01) + toBigEndian(u256(0x8765))) == bytes());
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0x1234)));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == toBigEndian(u256(0x8765)));
BOOST_CHECK(callContractFunction("set(uint8,uint256)", bytes(1, 0x00) + toBigEndian(u256(0x3))) == bytes());
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == toBigEndian(u256(0x3)));
}
BOOST_AUTO_TEST_CASE(compound_assign)
@ -529,14 +530,14 @@ BOOST_AUTO_TEST_CASE(compound_assign)
value2 *= value3 + value1;
return value2 += 7;
};
testSolidityAgainstCpp(0, f, u256(0), u256(6));
testSolidityAgainstCpp(0, f, u256(1), u256(3));
testSolidityAgainstCpp(0, f, u256(2), u256(25));
testSolidityAgainstCpp(0, f, u256(3), u256(69));
testSolidityAgainstCpp(0, f, u256(4), u256(84));
testSolidityAgainstCpp(0, f, u256(5), u256(2));
testSolidityAgainstCpp(0, f, u256(6), u256(51));
testSolidityAgainstCpp(0, f, u256(7), u256(48));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(0), u256(6));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(1), u256(3));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(2), u256(25));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(3), u256(69));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(4), u256(84));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(5), u256(2));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(6), u256(51));
testSolidityAgainstCpp("f(uint256,uint256)", f, u256(7), u256(48));
}
BOOST_AUTO_TEST_CASE(simple_mapping)
@ -553,21 +554,21 @@ BOOST_AUTO_TEST_CASE(simple_mapping)
compileAndRun(sourceCode);
// msvc seems to have problems with initializer-list, when there is only 1 param in the list
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00));
callContractFunction(1, bytes({0x01, 0xa1}));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0xa1));
BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00));
callContractFunction(1, bytes({0x00, 0xef}));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0xef));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0xa1));
BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00));
callContractFunction(1, bytes({0x01, 0x05}));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x00)) == bytes(1, 0xef));
BOOST_CHECK(callContractFunction(0, bytes(1, 0x01)) == bytes(1, 0x05));
BOOST_CHECK(callContractFunction(0, bytes(1, 0xa7)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00));
callContractFunction("set(uint8,uint8)", bytes({0x01, 0xa1}));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0xa1));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00));
callContractFunction("set(uint8,uint8)", bytes({0x00, 0xef}));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0xef));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0xa1));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00));
callContractFunction("set(uint8,uint8)", bytes({0x01, 0x05}));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x00)) == bytes(1, 0xef));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0x01)) == bytes(1, 0x05));
BOOST_CHECK(callContractFunction("get(uint8)", bytes(1, 0xa7)) == bytes(1, 0x00));
}
BOOST_AUTO_TEST_CASE(mapping_state)
@ -611,38 +612,38 @@ BOOST_AUTO_TEST_CASE(mapping_state)
auto getVoteCount = bind(&Ballot::getVoteCount, &ballot, _1);
auto grantVoteRight = bind(&Ballot::grantVoteRight, &ballot, _1);
auto vote = bind(&Ballot::vote, &ballot, _1, _2);
testSolidityAgainstCpp(0, getVoteCount, u160(0));
testSolidityAgainstCpp(0, getVoteCount, u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(2));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// voting without vote right shourd be rejected
testSolidityAgainstCpp(2, vote, u160(0), u160(2));
testSolidityAgainstCpp(0, getVoteCount, u160(0));
testSolidityAgainstCpp(0, getVoteCount, u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(2));
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// grant vote rights
testSolidityAgainstCpp(1, grantVoteRight, u160(0));
testSolidityAgainstCpp(1, grantVoteRight, u160(1));
testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(0));
testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(1));
// vote, should increase 2's vote count
testSolidityAgainstCpp(2, vote, u160(0), u160(2));
testSolidityAgainstCpp(0, getVoteCount, u160(0));
testSolidityAgainstCpp(0, getVoteCount, u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(2));
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(2));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// vote again, should be rejected
testSolidityAgainstCpp(2, vote, u160(0), u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(0));
testSolidityAgainstCpp(0, getVoteCount, u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(2));
testSolidityAgainstCpp("vote(address,address)", vote, u160(0), u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// vote without right to vote
testSolidityAgainstCpp(2, vote, u160(2), u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(0));
testSolidityAgainstCpp(0, getVoteCount, u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(2));
testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
// grant vote right and now vote again
testSolidityAgainstCpp(1, grantVoteRight, u160(2));
testSolidityAgainstCpp(2, vote, u160(2), u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(0));
testSolidityAgainstCpp(0, getVoteCount, u160(1));
testSolidityAgainstCpp(0, getVoteCount, u160(2));
testSolidityAgainstCpp("grantVoteRight(address)", grantVoteRight, u160(2));
testSolidityAgainstCpp("vote(address,address)", vote, u160(2), u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(0));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(1));
testSolidityAgainstCpp("getVoteCount(address)", getVoteCount, u160(2));
}
BOOST_AUTO_TEST_CASE(mapping_state_inc_dec)
@ -673,7 +674,7 @@ BOOST_AUTO_TEST_CASE(mapping_state_inc_dec)
table[value]++;
return --table[value++];
};
testSolidityAgainstCppOnRange(0, f, 0, 5);
testSolidityAgainstCppOnRange("f(uint256)", f, 0, 5);
}
BOOST_AUTO_TEST_CASE(multi_level_mapping)
@ -693,14 +694,14 @@ BOOST_AUTO_TEST_CASE(multi_level_mapping)
if (_z == 0) return table[_x][_y];
else return table[_x][_y] = _z;
};
testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(0));
testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0));
testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(9));
testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(0));
testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0));
testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(7));
testSolidityAgainstCpp(0, f, u256(4), u256(5), u256(0));
testSolidityAgainstCpp(0, f, u256(5), u256(4), u256(0));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(9));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(7));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(4), u256(5), u256(0));
testSolidityAgainstCpp("f(uint256,uint256,uint256)", f, u256(5), u256(4), u256(0));
}
BOOST_AUTO_TEST_CASE(structs)
@ -735,9 +736,9 @@ BOOST_AUTO_TEST_CASE(structs)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction(1) == bytes());
BOOST_CHECK(callContractFunction(0) == bytes(1, 0x01));
BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00));
BOOST_CHECK(callContractFunction("set()") == bytes());
BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01));
}
BOOST_AUTO_TEST_CASE(struct_reference)
@ -763,9 +764,9 @@ BOOST_AUTO_TEST_CASE(struct_reference)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0) == bytes(1, 0x00));
BOOST_CHECK(callContractFunction(1) == bytes());
BOOST_CHECK(callContractFunction(0) == bytes(1, 0x01));
BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x00));
BOOST_CHECK(callContractFunction("set()") == bytes());
BOOST_CHECK(callContractFunction("check()") == bytes(1, 0x01));
}
BOOST_AUTO_TEST_CASE(constructor)
@ -786,8 +787,8 @@ BOOST_AUTO_TEST_CASE(constructor)
{
return data[_x];
};
testSolidityAgainstCpp(0, get, u256(6));
testSolidityAgainstCpp(0, get, u256(7));
testSolidityAgainstCpp("get(uint256)", get, u256(6));
testSolidityAgainstCpp("get(uint256)", get, u256(7));
}
BOOST_AUTO_TEST_CASE(balance)
@ -798,7 +799,7 @@ BOOST_AUTO_TEST_CASE(balance)
" }\n"
"}\n";
compileAndRun(sourceCode, 23);
BOOST_CHECK(callContractFunction(0) == toBigEndian(u256(23)));
BOOST_CHECK(callContractFunction("getBalance()") == toBigEndian(u256(23)));
}
BOOST_AUTO_TEST_CASE(blockchain)
@ -811,7 +812,7 @@ BOOST_AUTO_TEST_CASE(blockchain)
" }\n"
"}\n";
compileAndRun(sourceCode, 27);
BOOST_CHECK(callContractFunction(0, bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1)));
BOOST_CHECK(callContractFunction("someInfo()", bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1)));
}
BOOST_AUTO_TEST_CASE(function_types)
@ -830,8 +831,8 @@ BOOST_AUTO_TEST_CASE(function_types)
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0, bytes{0}) == toBigEndian(u256(11)));
BOOST_CHECK(callContractFunction(0, bytes{1}) == toBigEndian(u256(12)));
BOOST_CHECK(callContractFunction("a(bool)", bytes{0}) == toBigEndian(u256(11)));
BOOST_CHECK(callContractFunction("a(bool)", bytes{1}) == toBigEndian(u256(12)));
}
BOOST_AUTO_TEST_CASE(send_ether)
@ -845,10 +846,102 @@ BOOST_AUTO_TEST_CASE(send_ether)
u256 amount(130);
compileAndRun(sourceCode, amount + 1);
u160 address(23);
BOOST_CHECK(callContractFunction(0, address, amount) == toBigEndian(u256(1)));
BOOST_CHECK(callContractFunction("a(address,uint256)", address, amount) == toBigEndian(u256(1)));
BOOST_CHECK_EQUAL(m_state.balance(address), amount);
}
BOOST_AUTO_TEST_CASE(log0)
{
char const* sourceCode = "contract test {\n"
" function a() {\n"
" log0(1);\n"
" }\n"
"}\n";
u256 amount(130);
compileAndRun(sourceCode, amount + 1);
u160 address(23);
callContractFunction("a()", address, amount);
BOOST_CHECK_EQUAL(m_logs.size(), 1);
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 0);
}
BOOST_AUTO_TEST_CASE(log1)
{
char const* sourceCode = "contract test {\n"
" function a() {\n"
" log1(1, 2);\n"
" }\n"
"}\n";
u256 amount(130);
compileAndRun(sourceCode, amount + 1);
u160 address(23);
callContractFunction("a()", address, amount);
BOOST_CHECK_EQUAL(m_logs.size(), 1);
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 1);
BOOST_CHECK_EQUAL(m_logs[0].topics[0], h256(u256(2)));
}
BOOST_AUTO_TEST_CASE(log2)
{
char const* sourceCode = "contract test {\n"
" function a() {\n"
" log2(1, 2, 3);\n"
" }\n"
"}\n";
u256 amount(130);
compileAndRun(sourceCode, amount + 1);
u160 address(23);
callContractFunction("a()", address, amount);
BOOST_CHECK_EQUAL(m_logs.size(), 1);
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 2);
for (unsigned i = 0; i < 2; ++i)
BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
}
BOOST_AUTO_TEST_CASE(log3)
{
char const* sourceCode = "contract test {\n"
" function a() {\n"
" log3(1, 2, 3, 4);\n"
" }\n"
"}\n";
u256 amount(130);
compileAndRun(sourceCode, amount + 1);
u160 address(23);
callContractFunction("a()", address, amount);
BOOST_CHECK_EQUAL(m_logs.size(), 1);
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 3);
for (unsigned i = 0; i < 3; ++i)
BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
}
BOOST_AUTO_TEST_CASE(log4)
{
char const* sourceCode = "contract test {\n"
" function a() {\n"
" log4(1, 2, 3, 4, 5);\n"
" }\n"
"}\n";
u256 amount(130);
compileAndRun(sourceCode, amount + 1);
u160 address(23);
callContractFunction("a()", address, amount);
BOOST_CHECK_EQUAL(m_logs.size(), 1);
BOOST_CHECK_EQUAL(m_logs[0].address, m_contractAddress);
BOOST_CHECK_EQUAL(h256(m_logs[0].data), h256(u256(1)));
BOOST_CHECK_EQUAL(m_logs[0].topics.size(), 4);
for (unsigned i = 0; i < 4; ++i)
BOOST_CHECK_EQUAL(m_logs[0].topics[i], h256(u256(i + 2)));
}
BOOST_AUTO_TEST_CASE(suicide)
{
char const* sourceCode = "contract test {\n"
@ -860,7 +953,7 @@ BOOST_AUTO_TEST_CASE(suicide)
u256 amount(130);
compileAndRun(sourceCode, amount);
u160 address(23);
BOOST_CHECK(callContractFunction(0, address) == bytes());
BOOST_CHECK(callContractFunction("a(address)", address) == bytes());
BOOST_CHECK(!m_state.addressHasCode(m_contractAddress));
BOOST_CHECK_EQUAL(m_state.balance(address), amount);
}
@ -877,9 +970,9 @@ BOOST_AUTO_TEST_CASE(sha3)
{
return dev::sha3(toBigEndian(_x));
};
testSolidityAgainstCpp(0, f, u256(4));
testSolidityAgainstCpp(0, f, u256(5));
testSolidityAgainstCpp(0, f, u256(-1));
testSolidityAgainstCpp("a(hash256)", f, u256(4));
testSolidityAgainstCpp("a(hash256)", f, u256(5));
testSolidityAgainstCpp("a(hash256)", f, u256(-1));
}
BOOST_AUTO_TEST_CASE(sha256)
@ -896,9 +989,9 @@ BOOST_AUTO_TEST_CASE(sha256)
dev::sha256(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32));
return ret;
};
testSolidityAgainstCpp(0, f, u256(4));
testSolidityAgainstCpp(0, f, u256(5));
testSolidityAgainstCpp(0, f, u256(-1));
testSolidityAgainstCpp("a(hash256)", f, u256(4));
testSolidityAgainstCpp("a(hash256)", f, u256(5));
testSolidityAgainstCpp("a(hash256)", f, u256(-1));
}
BOOST_AUTO_TEST_CASE(ripemd)
@ -915,9 +1008,9 @@ BOOST_AUTO_TEST_CASE(ripemd)
dev::ripemd160(dev::ref(toBigEndian(_input)), bytesRef(&ret[0], 32));
return u256(ret) >> (256 - 160);
};
testSolidityAgainstCpp(0, f, u256(4));
testSolidityAgainstCpp(0, f, u256(5));
testSolidityAgainstCpp(0, f, u256(-1));
testSolidityAgainstCpp("a(hash256)", f, u256(4));
testSolidityAgainstCpp("a(hash256)", f, u256(5));
testSolidityAgainstCpp("a(hash256)", f, u256(-1));
}
BOOST_AUTO_TEST_CASE(ecrecover)
@ -933,7 +1026,7 @@ BOOST_AUTO_TEST_CASE(ecrecover)
u256 r("0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f");
u256 s("0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549");
u160 addr("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
BOOST_CHECK(callContractFunction(0, h, v, r, s) == toBigEndian(addr));
BOOST_CHECK(callContractFunction("a(hash256,uint8,hash256,hash256)", h, v, r, s) == toBigEndian(addr));
}
BOOST_AUTO_TEST_CASE(inter_contract_calls)
@ -959,11 +1052,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls)
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("setHelper(address)", helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress));
u256 a(3456789);
u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b));
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b));
}
BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters)
@ -989,12 +1082,12 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_complex_parameters)
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("setHelper(address)", helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", 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_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, true, b) == toBigEndian(a * 3));
BOOST_REQUIRE(callContractFunction("callHelper(uint256,bool,uint256)", a, false, b) == toBigEndian(b * 3));
}
BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this)
@ -1020,9 +1113,9 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_accessing_this)
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_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress));
BOOST_REQUIRE(callContractFunction("callHelper()") == toBigEndian(helperAddress));
}
BOOST_AUTO_TEST_CASE(calls_to_this)
@ -1051,11 +1144,11 @@ BOOST_AUTO_TEST_CASE(calls_to_this)
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("setHelper(address)", helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress));
u256 a(3456789);
u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 10));
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 10));
}
BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars)
@ -1086,11 +1179,11 @@ BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars)
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("setHelper(address)", helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress));
u256 a(3456789);
u256 b("0x282837623374623234aa74");
BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 9));
BOOST_REQUIRE(callContractFunction("callHelper(uint256,uint256)", a, b) == toBigEndian(a * b + 9));
}
BOOST_AUTO_TEST_CASE(strings_in_calls)
@ -1116,9 +1209,9 @@ BOOST_AUTO_TEST_CASE(strings_in_calls)
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_CHECK(callContractFunction(0, bytes({0, 'a', 1})) == bytes({0, 'a', 0, 0, 0}));
BOOST_REQUIRE(callContractFunction("setHelper(address)", helperAddress) == bytes());
BOOST_REQUIRE(callContractFunction("getHelper()", helperAddress) == toBigEndian(helperAddress));
BOOST_CHECK(callContractFunction("callHelper(string2,bool)", bytes({0, 'a', 1})) == bytes({0, 'a', 0, 0, 0}));
}
BOOST_AUTO_TEST_CASE(constructor_arguments)
@ -1143,8 +1236,8 @@ BOOST_AUTO_TEST_CASE(constructor_arguments)
function getName() returns (string3 ret) { return h.getName(); }
})";
compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction(0) == bytes({byte(0x01)}));
BOOST_REQUIRE(callContractFunction(1) == bytes({'a', 'b', 'c'}));
BOOST_REQUIRE(callContractFunction("getFlag()") == bytes({byte(0x01)}));
BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'}));
}
BOOST_AUTO_TEST_CASE(functions_called_by_constructor)
@ -1161,7 +1254,7 @@ BOOST_AUTO_TEST_CASE(functions_called_by_constructor)
function setName(string3 _name) { name = _name; }
})";
compileAndRun(sourceCode);
BOOST_REQUIRE(callContractFunction(0) == bytes({'a', 'b', 'c'}));
BOOST_REQUIRE(callContractFunction("getName()") == bytes({'a', 'b', 'c'}));
}
BOOST_AUTO_TEST_SUITE_END()

88
test/SolidityExpressionCompiler.cpp

@ -174,8 +174,8 @@ BOOST_AUTO_TEST_CASE(comparison)
bytes code = compileFirstExpression(sourceCode);
bytes expectation({byte(eth::Instruction::PUSH1), 0x1,
byte(eth::Instruction::PUSH2), 0x11, 0xaa, byte(eth::Instruction::PUSH2), 0xff, 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH2), 0x10, 0xaa, byte(eth::Instruction::PUSH2), 0xff, 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH2), 0x11, 0xaa,
byte(eth::Instruction::PUSH2), 0x10, 0xaa,
byte(eth::Instruction::LT),
byte(eth::Instruction::EQ),
byte(eth::Instruction::ISZERO)});
@ -189,20 +189,18 @@ BOOST_AUTO_TEST_CASE(short_circuiting)
"}\n";
bytes code = compileFirstExpression(sourceCode);
bytes expectation({byte(eth::Instruction::PUSH1), 0xa,
byte(eth::Instruction::PUSH1), 0x8,
byte(eth::Instruction::ADD), byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x4, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
bytes expectation({byte(eth::Instruction::PUSH1), 0x12, // 8 + 10
byte(eth::Instruction::PUSH1), 0x4,
byte(eth::Instruction::GT),
byte(eth::Instruction::ISZERO), // after this we have 10 + 8 >= 4
byte(eth::Instruction::ISZERO), // after this we have 4 <= 8 + 10
byte(eth::Instruction::DUP1),
byte(eth::Instruction::PUSH1), 0x20,
byte(eth::Instruction::PUSH1), 0x11,
byte(eth::Instruction::JUMPI), // short-circuit if it is true
byte(eth::Instruction::POP),
byte(eth::Instruction::PUSH1), 0x2, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x9, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x2,
byte(eth::Instruction::PUSH1), 0x9,
byte(eth::Instruction::EQ),
byte(eth::Instruction::ISZERO), // after this we have 2 != 9
byte(eth::Instruction::ISZERO), // after this we have 9 != 2
byte(eth::Instruction::JUMPDEST),
byte(eth::Instruction::PUSH1), 0x1,
byte(eth::Instruction::EQ),
@ -213,28 +211,24 @@ BOOST_AUTO_TEST_CASE(short_circuiting)
BOOST_AUTO_TEST_CASE(arithmetics)
{
char const* sourceCode = "contract test {\n"
" function f() { var x = ((((((((9 ^ 8) & 7) | 6) - 5) + 4) % 3) / 2) * 1); }"
" function f(uint y) { var x = ((((((((y ^ 8) & 7) | 6) - 5) + 4) % 3) / 2) * 1); }"
"}\n";
bytes code = compileFirstExpression(sourceCode);
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}, {"test", "f", "x"}});
bytes expectation({byte(eth::Instruction::PUSH1), 0x1,
byte(eth::Instruction::PUSH1), 0x2,
byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x3,
byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x4,
byte(eth::Instruction::PUSH1), 0x5,
byte(eth::Instruction::PUSH1), 0x6,
byte(eth::Instruction::PUSH1), 0x7,
byte(eth::Instruction::PUSH1), 0x8,
byte(eth::Instruction::PUSH1), 0x9,
byte(eth::Instruction::DUP10),
byte(eth::Instruction::XOR),
byte(eth::Instruction::AND),
byte(eth::Instruction::OR),
byte(eth::Instruction::SUB),
byte(eth::Instruction::ADD),
byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::MOD),
byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::DIV),
byte(eth::Instruction::MUL)});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
@ -243,15 +237,15 @@ BOOST_AUTO_TEST_CASE(arithmetics)
BOOST_AUTO_TEST_CASE(unary_operators)
{
char const* sourceCode = "contract test {\n"
" function f() { var x = !(~+- 1 == 2); }"
" function f(int y) { var x = !(~+- y == 2); }"
"}\n";
bytes code = compileFirstExpression(sourceCode);
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}, {"test", "f", "x"}});
bytes expectation({byte(eth::Instruction::PUSH1), 0x2, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x1,
bytes expectation({byte(eth::Instruction::PUSH1), 0x2,
byte(eth::Instruction::DUP3),
byte(eth::Instruction::PUSH1), 0x0,
byte(eth::Instruction::SUB),
byte(eth::Instruction::NOT), byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::NOT),
byte(eth::Instruction::EQ),
byte(eth::Instruction::ISZERO)});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
@ -315,7 +309,7 @@ BOOST_AUTO_TEST_CASE(assignment)
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "b"}});
// Stack: a, b
bytes expectation({byte(eth::Instruction::PUSH1), 0x2, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
bytes expectation({byte(eth::Instruction::PUSH1), 0x2,
byte(eth::Instruction::DUP2),
byte(eth::Instruction::DUP4),
byte(eth::Instruction::ADD),
@ -338,14 +332,14 @@ BOOST_AUTO_TEST_CASE(function_call)
{{"test", "f", "a"}, {"test", "f", "b"}});
// Stack: a, b
bytes expectation({byte(eth::Instruction::PUSH1), 0x02, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
byte(eth::Instruction::PUSH1), 0x12,
byte(eth::Instruction::PUSH1), 0x01, byte(eth::Instruction::PUSH1), 0xff, byte(eth::Instruction::AND),
bytes expectation({byte(eth::Instruction::PUSH1), 0x02,
byte(eth::Instruction::PUSH1), 0x0c,
byte(eth::Instruction::PUSH1), 0x01,
byte(eth::Instruction::DUP5),
byte(eth::Instruction::ADD),
// Stack here: a b 2 <ret label> (a+1)
byte(eth::Instruction::DUP4),
byte(eth::Instruction::PUSH1), 0x19,
byte(eth::Instruction::PUSH1), 0x13,
byte(eth::Instruction::JUMP),
byte(eth::Instruction::JUMPDEST),
// Stack here: a b 2 g(a+1, b)
@ -363,40 +357,36 @@ BOOST_AUTO_TEST_CASE(function_call)
BOOST_AUTO_TEST_CASE(negative_literals_8bits)
{
// these all fit in 8 bits
char const* sourceCode = "contract test {\n"
" function f() { int8 x = -0 + -1 + -0x01 + -127 + -128; }\n"
" function f() { int8 x = -0x80; }\n"
"}\n";
bytes code = compileFirstExpression(sourceCode);
bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x80) +
bytes({byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x81) +
bytes({byte(eth::Instruction::PUSH32)}) + bytes(32, 0xff) +
bytes({byte(eth::Instruction::PUSH32)}) + bytes(32, 0xff) +
bytes({byte(eth::Instruction::PUSH1), 0x00,
byte(eth::Instruction::ADD),
byte(eth::Instruction::ADD),
byte(eth::Instruction::ADD),
byte(eth::Instruction::ADD)}));
bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x80));
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_CASE(negative_literals_16bits)
{
// -1 should need 8 bits, -129 should need 16 bits, how many bits are used is visible
// from the SIGNEXTEND opcodes
char const* sourceCode = "contract test {\n"
" function f() { int64 x = int64(-1 + -129); }\n"
" function f() { int64 x = ~0xabc; }\n"
"}\n";
bytes code = compileFirstExpression(sourceCode);
bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(30, 0xff) + bytes{0xf5, 0x43});
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}
BOOST_AUTO_TEST_CASE(intermediately_overflowing_literals)
{
// first literal itself is too large for 256 bits but it fits after all constant operations
// have been applied
char const* sourceCode = "contract test {\n"
" function f() { var x = (0xffffffffffffffffffffffffffffffffffffffff * 0xffffffffffffffffffffffffff01) & 0xbf; }\n"
"}\n";
bytes code = compileFirstExpression(sourceCode);
bytes expectation(bytes({byte(eth::Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x7f) +
bytes({byte(eth::Instruction::PUSH32)}) + bytes(32, 0xff) +
bytes({byte(eth::Instruction::PUSH1), 0x00,
byte(eth::Instruction::SIGNEXTEND),
byte(eth::Instruction::ADD),
byte(eth::Instruction::PUSH1), 0x01,
byte(eth::Instruction::SIGNEXTEND)}));
bytes expectation(bytes({byte(eth::Instruction::PUSH1), 0xbf}));
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
}

4
test/SolidityNatspecJSON.cpp

@ -56,9 +56,9 @@ public:
}
if (_userDocumentation)
generatedDocumentationString = m_compilerStack.getJsonDocumentation("", DocumentationType::NATSPEC_USER);
generatedDocumentationString = m_compilerStack.getMetadata("", DocumentationType::NATSPEC_USER);
else
generatedDocumentationString = m_compilerStack.getJsonDocumentation("", DocumentationType::NATSPEC_DEV);
generatedDocumentationString = m_compilerStack.getMetadata("", DocumentationType::NATSPEC_DEV);
Json::Value generatedDocumentation;
m_reader.parse(generatedDocumentationString, generatedDocumentation);
Json::Value expectedDocumentation;

26
test/SolidityOptimizer.cpp

@ -55,12 +55,12 @@ public:
}
template <class... Args>
void compareVersions(byte _index, Args const&... _arguments)
void compareVersions(std::string _sig, Args const&... _arguments)
{
m_contractAddress = m_nonOptimizedContract;
bytes nonOptimizedOutput = callContractFunction(_index, _arguments...);
bytes nonOptimizedOutput = callContractFunction(_sig, _arguments...);
m_contractAddress = m_optimizedContract;
bytes optimizedOutput = callContractFunction(_index, _arguments...);
bytes optimizedOutput = callContractFunction(_sig, _arguments...);
BOOST_CHECK_MESSAGE(nonOptimizedOutput == optimizedOutput, "Computed values do not match."
"\nNon-Optimized: " + toHex(nonOptimizedOutput) +
"\nOptimized: " + toHex(optimizedOutput));
@ -81,8 +81,8 @@ BOOST_AUTO_TEST_CASE(smoke_test)
return a;
}
})";
compileBothVersions(4, sourceCode);
compareVersions(0, u256(7));
compileBothVersions(29, sourceCode);
compareVersions("f(uint256)", u256(7));
}
BOOST_AUTO_TEST_CASE(large_integers)
@ -94,8 +94,8 @@ BOOST_AUTO_TEST_CASE(large_integers)
b = 0x10000000000000000000000002;
}
})";
compileBothVersions(33, sourceCode);
compareVersions(0);
compileBothVersions(36, sourceCode);
compareVersions("f()");
}
BOOST_AUTO_TEST_CASE(invariants)
@ -106,8 +106,8 @@ BOOST_AUTO_TEST_CASE(invariants)
return int(0) | (int(1) * (int(0) ^ (0 + a)));
}
})";
compileBothVersions(28, sourceCode);
compareVersions(0, u256(0x12334664));
compileBothVersions(41, sourceCode);
compareVersions("f(uint256)", u256(0x12334664));
}
BOOST_AUTO_TEST_CASE(unused_expressions)
@ -120,8 +120,8 @@ BOOST_AUTO_TEST_CASE(unused_expressions)
data;
}
})";
compileBothVersions(11, sourceCode);
compareVersions(0);
compileBothVersions(33, sourceCode);
compareVersions("f()");
}
BOOST_AUTO_TEST_CASE(constant_folding_both_sides)
@ -135,8 +135,8 @@ BOOST_AUTO_TEST_CASE(constant_folding_both_sides)
return 98 ^ (7 * ((1 | (x | 1000)) * 40) ^ 102);
}
})";
compileBothVersions(31, sourceCode);
compareVersions(0);
compileBothVersions(37, sourceCode);
compareVersions("f(uint256)");
}
BOOST_AUTO_TEST_SUITE_END()

12
test/SolidityScanner.cpp

@ -103,14 +103,17 @@ BOOST_AUTO_TEST_CASE(negative_numbers)
BOOST_CHECK_EQUAL(scanner.getCurrentToken(), Token::VAR);
BOOST_CHECK_EQUAL(scanner.next(), Token::IDENTIFIER);
BOOST_CHECK_EQUAL(scanner.next(), Token::ASSIGN);
BOOST_CHECK_EQUAL(scanner.next(), Token::SUB);
BOOST_CHECK_EQUAL(scanner.next(), Token::NUMBER);
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "-.2");
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), ".2");
BOOST_CHECK_EQUAL(scanner.next(), Token::ADD);
BOOST_CHECK_EQUAL(scanner.next(), Token::SUB);
BOOST_CHECK_EQUAL(scanner.next(), Token::NUMBER);
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "-0x78");
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "0x78");
BOOST_CHECK_EQUAL(scanner.next(), Token::ADD);
BOOST_CHECK_EQUAL(scanner.next(), Token::SUB);
BOOST_CHECK_EQUAL(scanner.next(), Token::NUMBER);
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "-7.3");
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "7.3");
BOOST_CHECK_EQUAL(scanner.next(), Token::ADD);
BOOST_CHECK_EQUAL(scanner.next(), Token::NUMBER);
BOOST_CHECK_EQUAL(scanner.getCurrentLiteral(), "8.9");
@ -130,8 +133,9 @@ BOOST_AUTO_TEST_CASE(locations)
BOOST_CHECK_EQUAL(scanner.next(), Token::SEMICOLON);
BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 24);
BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 25);
BOOST_CHECK_EQUAL(scanner.next(), Token::SUB);
BOOST_CHECK_EQUAL(scanner.next(), Token::NUMBER);
BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 26);
BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 27);
BOOST_CHECK_EQUAL(scanner.getCurrentLocation().end, 32);
BOOST_CHECK_EQUAL(scanner.next(), Token::IDENTIFIER);
BOOST_CHECK_EQUAL(scanner.getCurrentLocation().start, 45);

8
test/TestHelper.cpp

@ -489,4 +489,12 @@ void processCommandLineOptions()
}
}
LastHashes lastHashes(u256 _currentBlockNumber)
{
LastHashes ret;
for (u256 i = 1; i <= 256 && i <= _currentBlockNumber; ++i)
ret.push_back(sha3(toString(_currentBlockNumber - i)));
return ret;
}
} } // namespaces

1
test/TestHelper.h

@ -77,6 +77,7 @@ void executeTests(const std::string& _name, const std::string& _testPathAppendix
std::string getTestPath();
void userDefinedTest(std::string testTypeFlag, std::function<void(json_spirit::mValue&, bool)> doTests);
void processCommandLineOptions();
eth::LastHashes lastHashes(u256 _currentBlockNumber);
template<typename mapType>
void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs)

20
test/solidityExecutionFramework.h

@ -56,34 +56,36 @@ public:
return m_output;
}
bytes const& callContractFunction(byte _index, bytes const& _data = bytes(), u256 const& _value = 0)
bytes const& callContractFunction(std::string _sig, bytes const& _data = bytes(),
u256 const& _value = 0)
{
sendMessage(bytes(1, _index) + _data, false, _value);
FixedHash<4> hash(dev::sha3(_sig));
sendMessage(hash.asBytes() + _data, false, _value);
return m_output;
}
template <class... Args>
bytes const& callContractFunction(byte _index, Args const&... _arguments)
bytes const& callContractFunction(std::string _sig, Args const&... _arguments)
{
return callContractFunction(_index, argsToBigEndian(_arguments...));
return callContractFunction(_sig, argsToBigEndian(_arguments...));
}
template <class CppFunction, class... Args>
void testSolidityAgainstCpp(byte _index, CppFunction const& _cppFunction, Args const&... _arguments)
void testSolidityAgainstCpp(std::string _sig, CppFunction const& _cppFunction, Args const&... _arguments)
{
bytes solidityResult = callContractFunction(_index, _arguments...);
bytes solidityResult = callContractFunction(_sig, _arguments...);
bytes cppResult = callCppAndEncodeResult(_cppFunction, _arguments...);
BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match."
"\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult));
}
template <class CppFunction, class... Args>
void testSolidityAgainstCppOnRange(byte _index, CppFunction const& _cppFunction,
void testSolidityAgainstCppOnRange(std::string _sig, CppFunction const& _cppFunction,
u256 const& _rangeStart, u256 const& _rangeEnd)
{
for (u256 argument = _rangeStart; argument < _rangeEnd; ++argument)
{
bytes solidityResult = callContractFunction(_index, argument);
bytes solidityResult = callContractFunction(_sig, argument);
bytes cppResult = callCppAndEncodeResult(_cppFunction, argument);
BOOST_CHECK_MESSAGE(solidityResult == cppResult, "Computed values do not match."
"\nSolidity: " + toHex(solidityResult) + "\nC++: " + toHex(cppResult) +
@ -143,6 +145,7 @@ private:
m_state.noteSending(m_sender);
executive.finalize();
m_output = executive.out().toVector();
m_logs = executive.logs();
}
protected:
@ -153,6 +156,7 @@ protected:
u256 const m_gasPrice = 100 * eth::szabo;
u256 const m_gas = 1000000;
bytes m_output;
eth::LogEntries m_logs;
};
}

85
test/stSystemOperationsTestFiller.json

@ -529,12 +529,53 @@
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) [[ 0 ]] (CALL 1000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 0 2) }",
"code" : "{ [[ 0 ]] (CALL 1000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 0 31 1) [[ 1 ]] @0 }",
"storage": {}
},
"945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "23",
"code" : "0x6001600155603760005360026000f3",
"code" : "0x6001600155602a601f536001601ff3",
"nonce" : "0",
"storage" : {
}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "",
"storage": {}
}
},
"transaction" : {
"nonce" : "0",
"gasPrice" : "1",
"gasLimit" : "10000",
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87",
"value" : "100000",
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
"data" : ""
}
},
"CallToReturn1ForDynamicJump0": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "10000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "0x6001601f60006000601773945304eb96065b2a98b57a48a06ae28d285a71b56103e8f1600055600051565b6023602355",
"storage": {}
},
"945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "23",
"code" : "0x6001600155602a601f536001601ff3",
"nonce" : "0",
"storage" : {
}
@ -545,7 +586,47 @@
"code" : "",
"storage": {}
}
},
"transaction" : {
"nonce" : "0",
"gasPrice" : "1",
"gasLimit" : "10000",
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87",
"value" : "100000",
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
"data" : ""
}
},
"CallToReturn1ForDynamicJump1": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "10000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "0x6001601f60006000601773945304eb96065b2a98b57a48a06ae28d285a71b56103e8f160005560005156605b6023602355",
"storage": {}
},
"945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "23",
"code" : "0x6001600155602b601f536001601ff3",
"nonce" : "0",
"storage" : {
}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "",
"storage": {}
}
},
"transaction" : {
"nonce" : "0",

10
test/state.cpp

@ -39,16 +39,6 @@ using namespace dev::eth;
namespace dev { namespace test {
LastHashes lastHashes(u256 _currentBlockNumber)
{
LastHashes ret;
for (u256 i = 1; i <= 256 && i <= _currentBlockNumber; ++i)
ret.push_back(sha3(toString(_currentBlockNumber - i)));
return ret;
}
void doStateTests(json_spirit::mValue& v, bool _fillin)
{
processCommandLineOptions();

3
test/vm.cpp

@ -33,7 +33,7 @@ using namespace dev::eth;
using namespace dev::test;
FakeExtVM::FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock, unsigned _depth): /// TODO: XXX: remove the default argument & fix.
ExtVMFace(Address(), Address(), Address(), 0, 1, bytesConstRef(), bytes(), _previousBlock, _currentBlock, LastHashes(), _depth) {}
ExtVMFace(Address(), Address(), Address(), 0, 1, bytesConstRef(), bytes(), _previousBlock, _currentBlock, test::lastHashes(_currentBlock.number), _depth) {}
h160 FakeExtVM::create(u256 _endowment, u256& io_gas, bytesConstRef _init, OnOpFunc const&)
{
@ -117,6 +117,7 @@ void FakeExtVM::importEnv(mObject& _o)
previousBlock.hash = h256(_o["previousHash"].get_str());
currentBlock.number = toInt(_o["currentNumber"]);
lastHashes = test::lastHashes(currentBlock.number);
currentBlock.gasLimit = toInt(_o["currentGasLimit"]);
currentBlock.difficulty = toInt(_o["currentDifficulty"]);
currentBlock.timestamp = toInt(_o["currentTimestamp"]);

56
test/vmArithmeticTestFiller.json

@ -814,6 +814,62 @@
}
},
"sdiv4": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "1000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "{ [[ 0 ]] (SDIV 5 (- 0 4) ) }",
"storage": {}
}
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"origin" : "cd1722f2947def4cf144679da39c4c32bdc35681",
"caller" : "cd1722f2947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000",
"data" : "",
"gasPrice" : "100000000000000",
"gas" : "10000"
}
},
"sdiv5": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "1000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "{ [[ 0 ]] (SDIV (- 0 57896044618658097711785492504343953926634992332820282019728792003956564819967) (- 0 1) ) }",
"storage": {}
}
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"origin" : "cd1722f2947def4cf144679da39c4c32bdc35681",
"caller" : "cd1722f2947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000",
"data" : "",
"gasPrice" : "100000000000000",
"gas" : "10000"
}
},
"mod0": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",

56
test/vmBlockInfoTestFiller.json

@ -111,6 +111,62 @@
}
},
"blockhashInRange": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "257",
"currentGasLimit" : "1000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "{ [[ 0 ]] (BLOCKHASH 1) [[ 1 ]] (BLOCKHASH 2) [[ 2 ]] (BLOCKHASH 256) }",
"storage": {}
}
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000",
"data" : "",
"gasPrice" : "100000000000000",
"gas" : "10000"
}
},
"blockhashOutOfRange": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "257",
"currentGasLimit" : "1000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "{ [[ 0 ]] (BLOCKHASH 0) [[ 1 ]] (BLOCKHASH 257) [[ 2 ]] (BLOCKHASH 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) }",
"storage": {}
}
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000",
"data" : "",
"gasPrice" : "100000000000000",
"gas" : "10000"
}
},
"coinbase": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",

1244
test/vmIOandFlowOperationsTestFiller.json

File diff suppressed because it is too large
Loading…
Cancel
Save