Browse Source

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

Conflicts:
	mix/AssemblyDebuggerControl.cpp
	mix/AssemblyDebuggerControl.h
	mix/CodeEditorExtensionManager.cpp
	mix/CodeEditorExtensionManager.h
	mix/ConstantCompilationControl.cpp
	mix/qml.qrc
	mix/qml/MainContent.qml
	mix/qml/main.qml
cl-refactor
yann300 10 years ago
parent
commit
9412f3d515
  1. 2
      .gitignore
  2. 7
      CMakeLists.txt
  3. 2
      alethzero/CMakeLists.txt
  4. 18
      alethzero/Main.ui
  5. 152
      alethzero/MainWin.cpp
  6. 14
      alethzero/MainWin.h
  7. 9
      cmake/EthDependencies.cmake
  8. 26
      eth/main.cpp
  9. 1
      evmjit/CMakeLists.txt
  10. 1
      evmjit/evmcc/evmcc.cpp
  11. 20
      evmjit/libevmjit-cpp/Env.cpp
  12. 1
      evmjit/libevmjit-cpp/JitVM.cpp
  13. 19
      evmjit/libevmjit/BasicBlock.cpp
  14. 29
      evmjit/libevmjit/BasicBlock.h
  15. 42
      evmjit/libevmjit/Cache.cpp
  16. 2
      evmjit/libevmjit/Cache.h
  17. 10
      evmjit/libevmjit/Common.h
  18. 239
      evmjit/libevmjit/Compiler.cpp
  19. 30
      evmjit/libevmjit/Compiler.h
  20. 5
      evmjit/libevmjit/CompilerHelper.cpp
  21. 7
      evmjit/libevmjit/CompilerHelper.h
  22. 16
      evmjit/libevmjit/ExecutionEngine.cpp
  23. 153
      evmjit/libevmjit/Ext.cpp
  24. 50
      evmjit/libevmjit/Ext.h
  25. 6
      evmjit/libevmjit/GasMeter.cpp
  26. 40
      evmjit/libevmjit/Instruction.cpp
  27. 8
      evmjit/libevmjit/Instruction.h
  28. 8
      evmjit/libevmjit/Memory.cpp
  29. 23
      evmjit/libevmjit/Runtime.cpp
  30. 5
      evmjit/libevmjit/Runtime.h
  31. 1
      evmjit/libevmjit/RuntimeData.h
  32. 2
      evmjit/libevmjit/RuntimeManager.cpp
  33. 26
      evmjit/libevmjit/Utils.cpp
  34. 2
      evmjit/libevmjit/Utils.h
  35. 3
      libdevcore/Common.h
  36. 2
      libdevcore/Exceptions.h
  37. 2
      libdevcore/vector_ref.h
  38. 3
      libdevcrypto/Common.cpp
  39. 17
      libdevcrypto/CryptoPP.cpp
  40. 129
      libethereum/Client.cpp
  41. 37
      libethereum/Client.h
  42. 2
      libethereum/EthereumPeer.cpp
  43. 14
      libethereum/Executive.cpp
  44. 15
      libethereum/Interface.h
  45. 17
      libethereum/LogFilter.cpp
  46. 6
      libethereum/State.cpp
  47. 4
      libethereum/State.h
  48. 17
      libethereum/Utility.cpp
  49. 10
      libevm/ExtVMFace.h
  50. 760
      libevm/VM.cpp
  51. 763
      libevm/VM.h
  52. 8
      libevmcore/Instruction.h
  53. 10
      libjsqrc/CMakeLists.txt
  54. 7
      libjsqrc/compilejs.sh
  55. 4
      libjsqrc/ethereumjs/.travis.yml
  56. 24
      libjsqrc/ethereumjs/README.md
  57. 152
      libjsqrc/ethereumjs/dist/ethereum.js
  58. 16
      libjsqrc/ethereumjs/dist/ethereum.js.map
  59. 2
      libjsqrc/ethereumjs/dist/ethereum.min.js
  60. 2
      libjsqrc/ethereumjs/example/contract.html
  61. 2
      libjsqrc/ethereumjs/index.js
  62. 5
      libjsqrc/ethereumjs/index_qt.js
  63. 29
      libjsqrc/ethereumjs/lib/abi.js
  64. 2
      libjsqrc/ethereumjs/lib/autoprovider.js
  65. 15
      libjsqrc/ethereumjs/lib/contract.js
  66. 5
      libjsqrc/ethereumjs/lib/web3.js
  67. 3
      libjsqrc/ethereumjs/lib/websocket.js
  68. 8
      libjsqrc/ethereumjs/package.json
  69. 37
      libjsqrc/ethereumjs/test/abi.parsers.js
  70. 18
      libjsqrc/ethereumjs/test/db.methods.js
  71. 42
      libjsqrc/ethereumjs/test/eth.methods.js
  72. 2
      libjsqrc/ethereumjs/test/mocha.opts
  73. 19
      libjsqrc/ethereumjs/test/shh.methods.js
  74. 15
      libjsqrc/ethereumjs/test/utils.js
  75. 18
      libjsqrc/ethereumjs/test/web3.methods.js
  76. 12
      libjsqrc/js.qrc
  77. 2
      libp2p/Host.cpp
  78. 2
      libqwebthree/CMakeLists.txt
  79. 30
      libqwebthree/QWebThree.cpp
  80. 4
      libqwebthree/QWebThree.h
  81. 57
      libsolidity/AST.cpp
  82. 5
      libsolidity/AST.h
  83. 1
      libsolidity/CMakeLists.txt
  84. 78
      libsolidity/Compiler.cpp
  85. 13
      libsolidity/CompilerStack.cpp
  86. 12
      libsolidity/CompilerStack.h
  87. 21
      libsolidity/CompilerUtils.cpp
  88. 19
      libsolidity/CompilerUtils.h
  89. 206
      libsolidity/ExpressionCompiler.cpp
  90. 9
      libsolidity/ExpressionCompiler.h
  91. 49
      libsolidity/GlobalContext.cpp
  92. 55
      libsolidity/InterfaceHandler.cpp
  93. 1
      libsolidity/InterfaceHandler.h
  94. 27
      libsolidity/Scanner.cpp
  95. 17
      libsolidity/Token.cpp
  96. 2
      libsolidity/Token.h
  97. 316
      libsolidity/Types.cpp
  98. 146
      libsolidity/Types.h
  99. 637
      libweb3jsonrpc/WebThreeStubServer.cpp
  100. 104
      libweb3jsonrpc/WebThreeStubServer.h

2
.gitignore

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

7
CMakeLists.txt

@ -49,6 +49,7 @@ endfunction()
function(createBuildInfo) function(createBuildInfo)
# Set build platform; to be written to BuildInfo.h # Set build platform; to be written to BuildInfo.h
set(ETH_BUILD_PLATFORM ${TARGET_PLATFORM})
if (CMAKE_COMPILER_IS_MINGW) if (CMAKE_COMPILER_IS_MINGW)
set(ETH_BUILD_PLATFORM "${ETH_BUILD_PLATFORM}/mingw") set(ETH_BUILD_PLATFORM "${ETH_BUILD_PLATFORM}/mingw")
elseif (CMAKE_COMPILER_IS_MSYS) elseif (CMAKE_COMPILER_IS_MSYS)
@ -64,10 +65,10 @@ function(createBuildInfo)
endif () endif ()
#cmake build type may be not specified when using msvc #cmake build type may be not specified when using msvc
if (${CMAKE_BUILD_TYPE}) if (CMAKE_BUILD_TYPE)
set(_cmake_build_type ${CMAKE_BUILD_TYPE}) set(_cmake_build_type ${CMAKE_BUILD_TYPE})
else() else()
set(_cmake_build_type "undefined") set(_cmake_build_type "${CMAKE_CFG_INTDIR}")
endif() endif()
# Generate header file containing useful build information # Generate header file containing useful build information
@ -156,7 +157,7 @@ endif ()
if (NOT HEADLESS) if (NOT HEADLESS)
add_subdirectory(libjsqrc) add_subdirectory(libjsqrc)
add_subdirectory(libqethereum) add_subdirectory(libqwebthree)
add_subdirectory(alethzero) add_subdirectory(alethzero)
add_subdirectory(third) add_subdirectory(third)
add_subdirectory(mix) add_subdirectory(mix)

2
alethzero/CMakeLists.txt

@ -34,7 +34,7 @@ add_dependencies(${EXECUTABLE} BuildInfo.h)
target_link_libraries(${EXECUTABLE} Qt5::Core) target_link_libraries(${EXECUTABLE} Qt5::Core)
target_link_libraries(${EXECUTABLE} webthree) target_link_libraries(${EXECUTABLE} webthree)
target_link_libraries(${EXECUTABLE} qethereum) target_link_libraries(${EXECUTABLE} qwebthree)
target_link_libraries(${EXECUTABLE} ethereum) target_link_libraries(${EXECUTABLE} ethereum)
target_link_libraries(${EXECUTABLE} evm) target_link_libraries(${EXECUTABLE} evm)
target_link_libraries(${EXECUTABLE} ethcore) target_link_libraries(${EXECUTABLE} ethcore)

18
alethzero/Main.ui

@ -165,6 +165,7 @@
<addaction name="debugDumpStatePre"/> <addaction name="debugDumpStatePre"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="paranoia"/> <addaction name="paranoia"/>
<addaction name="clearPending"/>
<addaction name="killBlockchain"/> <addaction name="killBlockchain"/>
<addaction name="inject"/> <addaction name="inject"/>
<addaction name="forceMining"/> <addaction name="forceMining"/>
@ -172,6 +173,7 @@
<addaction name="enableOptimizer"/> <addaction name="enableOptimizer"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="usePrivate"/> <addaction name="usePrivate"/>
<addaction name="jitvm"/>
</widget> </widget>
<widget class="QMenu" name="menu_View"> <widget class="QMenu" name="menu_View">
<property name="title"> <property name="title">
@ -2049,6 +2051,22 @@ font-size: 14pt</string>
<string>New Identity</string> <string>New Identity</string>
</property> </property>
</action> </action>
<action name="clearPending">
<property name="text">
<string>Clear Pe&amp;nd&amp;ing</string>
</property>
</action>
<action name="jitvm">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="text">
<string>Use &amp;LLVM-EVM</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<customwidgets> <customwidgets>

152
alethzero/MainWin.cpp

@ -40,6 +40,7 @@
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
#include <libevm/VM.h> #include <libevm/VM.h>
#include <libevm/VMFactory.h>
#include <libethereum/BlockChain.h> #include <libethereum/BlockChain.h>
#include <libethereum/ExtVM.h> #include <libethereum/ExtVM.h>
#include <libethereum/Client.h> #include <libethereum/Client.h>
@ -105,9 +106,9 @@ static QString contentsOfQResource(std::string const& res)
return in.readAll(); return in.readAll();
} }
Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f"); //Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
Address c_newConfig = Address("d5f9d8d94886e70b06e474c3fb14fd43e2f23970"); Address c_newConfig = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
Address c_nameReg = Address("ddd1cea741d548f90d86fb87a3ae6492e18c03a1"); //Address c_nameReg = Address("ddd1cea741d548f90d86fb87a3ae6492e18c03a1");
Main::Main(QWidget *parent) : Main::Main(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
@ -243,14 +244,14 @@ void Main::onKeysChanged()
installBalancesWatch(); installBalancesWatch();
} }
unsigned Main::installWatch(dev::eth::LogFilter const& _tf, std::function<void()> const& _f) unsigned Main::installWatch(dev::eth::LogFilter const& _tf, WatchHandler const& _f)
{ {
auto ret = ethereum()->installWatch(_tf); auto ret = ethereum()->installWatch(_tf);
m_handlers[ret] = _f; m_handlers[ret] = _f;
return ret; return ret;
} }
unsigned Main::installWatch(dev::h256 _tf, std::function<void()> const& _f) unsigned Main::installWatch(dev::h256 _tf, WatchHandler const& _f)
{ {
auto ret = ethereum()->installWatch(_tf); auto ret = ethereum()->installWatch(_tf);
m_handlers[ret] = _f; m_handlers[ret] = _f;
@ -265,22 +266,32 @@ void Main::uninstallWatch(unsigned _w)
void Main::installWatches() void Main::installWatches()
{ {
installWatch(dev::eth::LogFilter().address(c_config), [=]() { installNameRegWatch(); }); installWatch(dev::eth::LogFilter().address(c_newConfig), [=](LocalisedLogEntries const&) { installNameRegWatch(); });
installWatch(dev::eth::LogFilter().address(c_config), [=]() { installCurrenciesWatch(); }); installWatch(dev::eth::LogFilter().address(c_newConfig), [=](LocalisedLogEntries const&) { installCurrenciesWatch(); });
installWatch(dev::eth::PendingChangedFilter, [=](){ onNewPending(); }); installWatch(dev::eth::PendingChangedFilter, [=](LocalisedLogEntries const&){ onNewPending(); });
installWatch(dev::eth::ChainChangedFilter, [=](){ onNewBlock(); }); installWatch(dev::eth::ChainChangedFilter, [=](LocalisedLogEntries const&){ onNewBlock(); });
}
Address Main::getNameReg() const
{
return abiOut<Address>(ethereum()->call(c_newConfig, abiIn("lookup(uint256)", (u256)1)));
}
Address Main::getCurrencies() const
{
return abiOut<Address>(ethereum()->call(c_newConfig, abiIn("lookup(uint256)", (u256)2)));
} }
void Main::installNameRegWatch() void Main::installNameRegWatch()
{ {
uninstallWatch(m_nameRegFilter); 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()), [=](LocalisedLogEntries const&){ onNameRegChange(); });
} }
void Main::installCurrenciesWatch() void Main::installCurrenciesWatch()
{ {
uninstallWatch(m_currenciesFilter); 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()), [=](LocalisedLogEntries const&){ onCurrenciesChange(); });
} }
void Main::installBalancesWatch() void Main::installBalancesWatch()
@ -288,7 +299,9 @@ void Main::installBalancesWatch()
dev::eth::LogFilter tf; dev::eth::LogFilter tf;
vector<Address> altCoins; 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) for (unsigned i = 0; i < ethereum()->stateAt(coinsAddr, 0); ++i)
altCoins.push_back(right160(ethereum()->stateAt(coinsAddr, i + 1))); altCoins.push_back(right160(ethereum()->stateAt(coinsAddr, i + 1)));
for (auto i: m_myKeys) for (auto i: m_myKeys)
@ -296,7 +309,7 @@ void Main::installBalancesWatch()
tf.address(c).topic(h256(i.address(), h256::AlignRight)); tf.address(c).topic(h256(i.address(), h256::AlignRight));
uninstallWatch(m_balancesFilter); uninstallWatch(m_balancesFilter);
m_balancesFilter = installWatch(tf, [=](){ onBalancesChange(); }); m_balancesFilter = installWatch(tf, [=](LocalisedLogEntries const&){ onBalancesChange(); });
} }
void Main::onNameRegChange() void Main::onNameRegChange()
@ -450,37 +463,37 @@ static Public stringToPublic(QString const& _a)
return Public(); return Public();
} }
static Address g_newNameReg; //static Address g_newNameReg;
QString Main::pretty(dev::Address _a) const 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 (!s_memos.count(_a))
{ {*/
if (!g_newNameReg) // if (!g_newNameReg)
g_newNameReg = abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)1))); auto g_newNameReg = getNameReg();
if (g_newNameReg) if (g_newNameReg)
{ {
QString s = QString::fromStdString(toString(abiOut<string32>(ethereum()->call(g_newNameReg, abiIn(2, _a))))); QString s = QString::fromStdString(toString(abiOut<string32>(ethereum()->call(g_newNameReg, abiIn("nameOf(address)", _a)))));
s_memos[_a] = s; // s_memos[_a] = s;
if (s.size()) if (s.size())
return s; return s;
} }
} /* }
else else
if (s_memos[_a].size()) if (s_memos[_a].size())
return s_memos[_a]; return s_memos[_a];*/
h256 n; h256 n;
/*
if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0)) if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0))
n = ethereum()->stateAt(nameReg, (u160)(_a)); n = ethereum()->stateAt(nameReg, (u160)(_a));
if (!n) if (!n)
n = ethereum()->stateAt(m_nameReg, (u160)(_a)); n = ethereum()->stateAt(m_nameReg, (u160)(_a));
*/
return fromRaw(n); return fromRaw(n);
} }
@ -505,21 +518,21 @@ Address Main::fromString(QString const& _n) const
if (_n == "(Create Contract)") if (_n == "(Create Contract)")
return Address(); return Address();
static std::map<QString, Address> s_memos; /* static std::map<QString, Address> s_memos;
if (!s_memos.count(_n)) if (!s_memos.count(_n))
{ {*/
if (!g_newNameReg) // if (!g_newNameReg)
g_newNameReg = abiOut<Address>(ethereum()->call(c_newConfig, abiIn(1, (u256)1))); auto g_newNameReg = getNameReg();
if (g_newNameReg) if (g_newNameReg)
{ {
Address a = abiOut<Address>(ethereum()->call(g_newNameReg, abiIn(0, ::fromString(_n.toStdString())))); Address a = abiOut<Address>(ethereum()->call(g_newNameReg, abiIn("addressOf(string32)", ::fromString(_n.toStdString()))));
s_memos[_n] = a; // s_memos[_n] = a;
if (a) if (a)
return a; return a;
} }
} /* }
else else
if (s_memos[_n]) if (s_memos[_n])
return s_memos[_n]; return s_memos[_n];
@ -532,14 +545,13 @@ Address Main::fromString(QString const& _n) const
memset(n.data() + sn.size(), 0, 32 - sn.size()); memset(n.data() + sn.size(), 0, 32 - sn.size());
if (_n.size()) if (_n.size())
{ {
if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0)) if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0))
if (h256 a = ethereum()->stateAt(nameReg, n)) if (h256 a = ethereum()->stateAt(nameReg, n))
return right160(a); return right160(a);
if (h256 a = ethereum()->stateAt(m_nameReg, n)) if (h256 a = ethereum()->stateAt(m_nameReg, n))
return right160(a); return right160(a);
} }*/
if (_n.size() == 40) if (_n.size() == 40)
return Address(fromHex(_n.toStdString())); return Address(fromHex(_n.toStdString()));
@ -566,8 +578,9 @@ QString Main::lookup(QString const& _a) const
*/ */
h256 ret; h256 ret;
if (h160 dnsReg = (u160)ethereum()->stateAt(c_config, 4, 0)) // TODO: fix with the new DNSreg contract
ret = ethereum()->stateAt(dnsReg, n); // if (h160 dnsReg = (u160)ethereum()->stateAt(c_config, 4, 0))
// ret = ethereum()->stateAt(dnsReg, n);
/* if (!ret) /* if (!ret)
if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0, 0)) if (h160 nameReg = (u160)ethereum()->stateAt(c_config, 0, 0))
ret = ethereum()->stateAt(nameReg, n2); ret = ethereum()->stateAt(nameReg, n2);
@ -632,6 +645,7 @@ void Main::writeSettings()
s.setValue("url", ui->urlEdit->text()); s.setValue("url", ui->urlEdit->text());
s.setValue("privateChain", m_privateChain); s.setValue("privateChain", m_privateChain);
s.setValue("verbosity", ui->verbosity->value()); s.setValue("verbosity", ui->verbosity->value());
s.setValue("jitvm", ui->jitvm->isChecked());
bytes d = m_webThree->saveNodes(); bytes d = m_webThree->saveNodes();
if (d.size()) if (d.size())
@ -706,6 +720,7 @@ void Main::readSettings(bool _skipGeometry)
m_privateChain = s.value("privateChain", "").toString(); m_privateChain = s.value("privateChain", "").toString();
ui->usePrivate->setChecked(m_privateChain.size()); ui->usePrivate->setChecked(m_privateChain.size());
ui->verbosity->setValue(s.value("verbosity", 1).toInt()); ui->verbosity->setValue(s.value("verbosity", 1).toInt());
ui->jitvm->setChecked(s.value("jitvm", true).toBool());
ui->urlEdit->setText(s.value("url", "about:blank").toString()); //http://gavwood.com/gavcoin.html ui->urlEdit->setText(s.value("url", "about:blank").toString()); //http://gavwood.com/gavcoin.html
on_urlEdit_returnPressed(); on_urlEdit_returnPressed();
@ -805,6 +820,12 @@ void Main::on_usePrivate_triggered()
on_killBlockchain_triggered(); on_killBlockchain_triggered();
} }
void Main::on_jitvm_triggered()
{
bool jit = ui->jitvm->isChecked();
VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter);
}
void Main::on_urlEdit_returnPressed() void Main::on_urlEdit_returnPressed()
{ {
QString s = ui->urlEdit->text(); QString s = ui->urlEdit->text();
@ -861,8 +882,8 @@ void Main::refreshBalances()
// update all the balance-dependent stuff. // update all the balance-dependent stuff.
ui->ourAccounts->clear(); ui->ourAccounts->clear();
u256 totalBalance = 0; u256 totalBalance = 0;
map<Address, tuple<QString, u256, u256>> altCoins; /* map<Address, tuple<QString, u256, u256>> altCoins;
Address coinsAddr = right160(ethereum()->stateAt(c_config, 1)); Address coinsAddr = getCurrencies();
for (unsigned i = 0; i < ethereum()->stateAt(coinsAddr, 0); ++i) for (unsigned i = 0; i < ethereum()->stateAt(coinsAddr, 0); ++i)
{ {
auto n = ethereum()->stateAt(coinsAddr, i + 1); auto n = ethereum()->stateAt(coinsAddr, i + 1);
@ -872,7 +893,7 @@ void Main::refreshBalances()
denom = 1; denom = 1;
// cdebug << n << addr << denom << sha3(h256(n).asBytes()); // cdebug << n << addr << denom << sha3(h256(n).asBytes());
altCoins[addr] = make_tuple(fromRaw(n), 0, denom); altCoins[addr] = make_tuple(fromRaw(n), 0, denom);
} }*/
for (auto i: m_myKeys) for (auto i: m_myKeys)
{ {
u256 b = ethereum()->balanceAt(i.address()); u256 b = ethereum()->balanceAt(i.address());
@ -880,18 +901,18 @@ void Main::refreshBalances()
->setData(Qt::UserRole, QByteArray((char const*)i.address().data(), Address::size)); ->setData(Qt::UserRole, QByteArray((char const*)i.address().data(), Address::size));
totalBalance += b; totalBalance += b;
for (auto& c: altCoins) // for (auto& c: altCoins)
get<1>(c.second) += (u256)ethereum()->stateAt(c.first, (u160)i.address()); // get<1>(c.second) += (u256)ethereum()->stateAt(c.first, (u160)i.address());
} }
QString b; QString b;
for (auto const& c: altCoins) /* for (auto const& c: altCoins)
if (get<1>(c.second)) if (get<1>(c.second))
{ {
stringstream s; stringstream s;
s << setw(toString(get<2>(c.second) - 1).size()) << setfill('0') << (get<1>(c.second) % get<2>(c.second)); 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() + " | "; 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))); ui->balance->setText(b + QString::fromStdString(formatBalance(totalBalance)));
} }
@ -1150,8 +1171,11 @@ void Main::timerEvent(QTimerEvent*)
m_qweb->poll(); m_qweb->poll();
for (auto const& i: m_handlers) for (auto const& i: m_handlers)
if (ethereum()->checkWatch(i.first)) {
i.second(); auto ls = ethereum()->checkWatch(i.first);
if (ls.size())
i.second(ls);
}
} }
string Main::renderDiff(dev::eth::StateDiff const& _d) const string Main::renderDiff(dev::eth::StateDiff const& _d) const
@ -1224,6 +1248,7 @@ void Main::on_transactionQueue_currentItemChanged()
if (i >= 0 && i < (int)ethereum()->pending().size()) if (i >= 0 && i < (int)ethereum()->pending().size())
{ {
Transaction tx(ethereum()->pending()[i]); Transaction tx(ethereum()->pending()[i]);
TransactionReceipt receipt(ethereum()->postState().receipt(i));
auto ss = tx.safeSender(); auto ss = tx.safeSender();
h256 th = sha3(rlpList(ss, tx.nonce())); h256 th = sha3(rlpList(ss, tx.nonce()));
s << "<h3>" << th << "</h3>"; s << "<h3>" << th << "</h3>";
@ -1246,12 +1271,15 @@ void Main::on_transactionQueue_currentItemChanged()
if (tx.data().size()) if (tx.data().size())
s << dev::memDump(tx.data(), 16, true); 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 << "<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, 0));
// s << "Pre: " << fs.rootHash() << "<br/>"; // s << "Pre: " << fs.rootHash() << "<br/>";
// s << "Post: <b>" << ts.rootHash() << "</b>"; // s << "Post: <b>" << ts.rootHash() << "</b>";
s << renderDiff(ethereum()->diff(i, 0));
} }
ui->pendingInfo->setHtml(QString::fromStdString(s.str())); ui->pendingInfo->setHtml(QString::fromStdString(s.str()));
@ -1364,11 +1392,6 @@ void Main::on_blocks_currentItemChanged()
s << "<br/>R: <b>" << hex << nouppercase << tx.signature().r << "</b>"; s << "<br/>R: <b>" << hex << nouppercase << tx.signature().r << "</b>";
s << "<br/>S: <b>" << hex << nouppercase << tx.signature().s << "</b>"; s << "<br/>S: <b>" << hex << nouppercase << tx.signature().s << "</b>";
s << "<br/>Msg: <b>" << tx.sha3(eth::WithoutSignature) << "</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.isCreation())
{ {
if (tx.data().size()) if (tx.data().size())
@ -1379,6 +1402,12 @@ void Main::on_blocks_currentItemChanged()
if (tx.data().size()) if (tx.data().size())
s << dev::memDump(tx.data(), 16, true); 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)); s << renderDiff(ethereum()->diff(txi, h));
ui->debugCurrent->setEnabled(true); ui->debugCurrent->setEnabled(true);
ui->debugDumpState->setEnabled(true); ui->debugDumpState->setEnabled(true);
@ -1619,12 +1648,15 @@ void Main::on_data_textChanged()
{ {
m_data = fromHex(src); 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; dev::solidity::CompilerStack compiler;
try try
{ {
m_data = compiler.compile(src, m_enableOptimizer); 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) catch (dev::Exception const& exception)
{ {
@ -1699,6 +1731,18 @@ void Main::on_data_textChanged()
updateFee(); 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() void Main::on_killBlockchain_triggered()
{ {
writeSettings(); writeSettings();
@ -1770,8 +1814,6 @@ void Main::on_net_triggered()
web3()->setClientVersion(n); web3()->setClientVersion(n);
if (ui->net->isChecked()) if (ui->net->isChecked())
{ {
// TODO: alter network stuff?
//ui->port->value(), string(), 0, NodeMode::Full, ui->idealPeers->value(), ui->forceAddress->text().toStdString(), ui->upnp->isChecked(), m_privateChain.size() ? sha3(m_privateChain.toStdString()) : 0
web3()->setIdealPeerCount(ui->idealPeers->value()); web3()->setIdealPeerCount(ui->idealPeers->value());
web3()->setNetworkPreferences(netPrefs()); web3()->setNetworkPreferences(netPrefs());
ethereum()->setNetworkId(m_privateChain.size() ? sha3(m_privateChain.toStdString()) : 0); ethereum()->setNetworkId(m_privateChain.size() ? sha3(m_privateChain.toStdString()) : 0);

14
alethzero/MainWin.h

@ -34,7 +34,7 @@
#include <libethcore/CommonEth.h> #include <libethcore/CommonEth.h>
#include <libethereum/State.h> #include <libethereum/State.h>
#include <libethereum/Executive.h> #include <libethereum/Executive.h>
#include <libqethereum/QEthereum.h> #include <libqwebthree/QWebThree.h>
#include <libwebthree/WebThree.h> #include <libwebthree/WebThree.h>
namespace Ui { namespace Ui {
@ -66,6 +66,8 @@ struct WorldState
std::vector<WorldState const*> levels; std::vector<WorldState const*> levels;
}; };
using WatchHandler = std::function<void(dev::eth::LocalisedLogEntries const&)>;
class Main : public QMainWindow class Main : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
@ -128,6 +130,7 @@ private slots:
void on_debugTimeline_valueChanged(); void on_debugTimeline_valueChanged();
void on_jsInput_returnPressed(); void on_jsInput_returnPressed();
void on_killBlockchain_triggered(); void on_killBlockchain_triggered();
void on_clearPending_triggered();
void on_importKey_triggered(); void on_importKey_triggered();
void on_exportKey_triggered(); void on_exportKey_triggered();
void on_inject_triggered(); void on_inject_triggered();
@ -155,6 +158,7 @@ private slots:
void on_importKeyFile_triggered(); void on_importKeyFile_triggered();
void on_post_clicked(); void on_post_clicked();
void on_newIdentity_triggered(); void on_newIdentity_triggered();
void on_jitvm_triggered();
void refreshWhisper(); void refreshWhisper();
void refreshBlockChain(); void refreshBlockChain();
@ -170,6 +174,8 @@ private:
QString prettyU256(dev::u256 _n) const; QString prettyU256(dev::u256 _n) const;
QString lookup(QString const& _n) const; QString lookup(QString const& _n) const;
dev::Address getNameReg() const;
dev::Address getCurrencies() const;
void populateDebugger(dev::bytesConstRef r); void populateDebugger(dev::bytesConstRef r);
void initDebugger(); void initDebugger();
@ -191,8 +197,8 @@ private:
dev::u256 value() const; dev::u256 value() const;
dev::u256 gasPrice() const; dev::u256 gasPrice() const;
unsigned installWatch(dev::eth::LogFilter const& _tf, std::function<void()> const& _f); unsigned installWatch(dev::eth::LogFilter const& _tf, WatchHandler const& _f);
unsigned installWatch(dev::h256 _tf, std::function<void()> const& _f); unsigned installWatch(dev::h256 _tf, WatchHandler const& _f);
void uninstallWatch(unsigned _w); void uninstallWatch(unsigned _w);
void keysChanged(); void keysChanged();
@ -225,7 +231,7 @@ private:
std::unique_ptr<dev::WebThreeDirect> m_webThree; std::unique_ptr<dev::WebThreeDirect> m_webThree;
std::map<unsigned, std::function<void()>> m_handlers; std::map<unsigned, WatchHandler> m_handlers;
unsigned m_nameRegFilter = (unsigned)-1; unsigned m_nameRegFilter = (unsigned)-1;
unsigned m_currenciesFilter = (unsigned)-1; unsigned m_currenciesFilter = (unsigned)-1;
unsigned m_balancesFilter = (unsigned)-1; unsigned m_balancesFilter = (unsigned)-1;

9
cmake/EthDependencies.cmake

@ -113,6 +113,15 @@ if (NOT HEADLESS)
message(" - macdeployqt path: ${MACDEPLOYQT_APP}") message(" - macdeployqt path: ${MACDEPLOYQT_APP}")
endif() endif()
# TODO check node && npm version
find_program(ETH_NODE node)
string(REGEX REPLACE "node" "" ETH_NODE_DIRECTORY ${ETH_NODE})
message(" - nodejs location : ${ETH_NODE}")
find_program(ETH_NPM npm)
string(REGEX REPLACE "npm" "" ETH_NPM_DIRECTORY ${ETH_NPM})
message(" - npm location : ${ETH_NPM}")
endif() #HEADLESS endif() #HEADLESS
# use multithreaded boost libraries, with -mt suffix # use multithreaded boost libraries, with -mt suffix

26
eth/main.cpp

@ -30,6 +30,7 @@
#include <libdevcrypto/FileSystem.h> #include <libdevcrypto/FileSystem.h>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include <libevm/VM.h> #include <libevm/VM.h>
#include <libevm/VMFactory.h>
#include <libethereum/All.h> #include <libethereum/All.h>
#include <libwebthree/WebThree.h> #include <libwebthree/WebThree.h>
#if ETH_READLINE #if ETH_READLINE
@ -121,7 +122,11 @@ void help()
<< " -u,--public-ip <ip> Force public ip to given (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 (Default: 8)." << 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 << " -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); exit(0);
} }
@ -175,6 +180,12 @@ void sighandler(int)
g_exit = true; g_exit = true;
} }
enum class NodeMode
{
PeerServer,
Full
};
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
unsigned short listenPort = 30303; unsigned short listenPort = 30303;
@ -193,6 +204,7 @@ int main(int argc, char** argv)
bool upnp = true; bool upnp = true;
bool useLocal = false; bool useLocal = false;
bool forceMining = false; bool forceMining = false;
bool jit = false;
string clientName; string clientName;
// Init defaults // Init defaults
@ -295,6 +307,15 @@ int main(int argc, char** argv)
return -1; 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") else if (arg == "-h" || arg == "--help")
help(); help();
else if (arg == "-V" || arg == "--version") else if (arg == "-V" || arg == "--version")
@ -308,9 +329,10 @@ int main(int argc, char** argv)
cout << credits(); cout << credits();
VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter);
NetworkPreferences netPrefs(listenPort, publicIP, upnp, useLocal); NetworkPreferences netPrefs(listenPort, publicIP, upnp, useLocal);
dev::WebThreeDirect web3( 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, dbPath,
false, false,
mode == NodeMode::Full ? set<string>{"eth", "shh"} : set<string>(), mode == NodeMode::Full ? set<string>{"eth", "shh"} : set<string>(),

1
evmjit/CMakeLists.txt

@ -9,6 +9,7 @@ if(LLVM_DIR) # local LLVM build
find_package(LLVM REQUIRED CONFIG) find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
add_definitions(${LLVM_DEFINITIONS})
# TODO: bitwriter is needed only for evmcc # TODO: bitwriter is needed only for evmcc
llvm_map_components_to_libnames(LLVM_LIBS core support mcjit x86asmparser x86codegen bitwriter) llvm_map_components_to_libnames(LLVM_LIBS core support mcjit x86asmparser x86codegen bitwriter)
else() else()

1
evmjit/evmcc/evmcc.cpp

@ -190,7 +190,6 @@ int main(int argc, char** argv)
data.set(RuntimeData::CallValue, 0xabcd); data.set(RuntimeData::CallValue, 0xabcd);
data.set(RuntimeData::CallDataSize, 3); data.set(RuntimeData::CallDataSize, 3);
data.set(RuntimeData::GasPrice, 1003); data.set(RuntimeData::GasPrice, 1003);
data.set(RuntimeData::PrevHash, 1003);
data.set(RuntimeData::CoinBase, (u160)Address(101010101010101015)); data.set(RuntimeData::CoinBase, (u160)Address(101010101010101015));
data.set(RuntimeData::TimeStamp, 1005); data.set(RuntimeData::TimeStamp, 1005);
data.set(RuntimeData::Number, 1006); data.set(RuntimeData::Number, 1006);

20
evmjit/libevmjit-cpp/Env.cpp

@ -42,16 +42,15 @@ extern "C"
*o_value = eth2llvm(u); *o_value = eth2llvm(u);
} }
EXPORT void env_create(ExtVMFace* _env, i256* io_gas, i256* _endowment, byte* _initBeg, uint64_t _initSize, h256* o_address) EXPORT void env_blockhash(ExtVMFace* _env, i256* _number, h256* o_hash)
{ {
if (_env->depth == 1024) *o_hash = _env->blockhash(llvm2eth(*_number));
jit::terminate(jit::ReturnCode::OutOfGas); }
assert(_env->depth < 1024);
EXPORT void env_create(ExtVMFace* _env, i256* io_gas, i256* _endowment, byte* _initBeg, uint64_t _initSize, h256* o_address)
{
auto endowment = llvm2eth(*_endowment); auto endowment = llvm2eth(*_endowment);
if (_env->balance(_env->myAddress) >= endowment && _env->depth < 1024)
if (_env->balance(_env->myAddress) >= endowment)
{ {
_env->subBalance(endowment); _env->subBalance(endowment);
auto gas = llvm2eth(*io_gas); auto gas = llvm2eth(*io_gas);
@ -66,13 +65,8 @@ extern "C"
EXPORT bool env_call(ExtVMFace* _env, i256* io_gas, h256* _receiveAddress, i256* _value, byte* _inBeg, uint64_t _inSize, byte* _outBeg, uint64_t _outSize, h256* _codeAddress) EXPORT bool env_call(ExtVMFace* _env, i256* io_gas, h256* _receiveAddress, i256* _value, byte* _inBeg, uint64_t _inSize, byte* _outBeg, uint64_t _outSize, h256* _codeAddress)
{ {
if (_env->depth == 1024)
jit::terminate(jit::ReturnCode::OutOfGas);
assert(_env->depth < 1024);
auto value = llvm2eth(*_value); auto value = llvm2eth(*_value);
if (_env->balance(_env->myAddress) >= value) if (_env->balance(_env->myAddress) >= value && _env->depth < 1024)
{ {
_env->subBalance(value); _env->subBalance(value);
auto receiveAddress = right160(*_receiveAddress); auto receiveAddress = right160(*_receiveAddress);

1
evmjit/libevmjit-cpp/JitVM.cpp

@ -20,7 +20,6 @@ bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const&, uint64_t)
m_data.set(RuntimeData::CallValue, _ext.value); m_data.set(RuntimeData::CallValue, _ext.value);
m_data.set(RuntimeData::CallDataSize, _ext.data.size()); m_data.set(RuntimeData::CallDataSize, _ext.data.size());
m_data.set(RuntimeData::GasPrice, _ext.gasPrice); m_data.set(RuntimeData::GasPrice, _ext.gasPrice);
m_data.set(RuntimeData::PrevHash, _ext.previousBlock.hash);
m_data.set(RuntimeData::CoinBase, fromAddress(_ext.currentBlock.coinbaseAddress)); m_data.set(RuntimeData::CoinBase, fromAddress(_ext.currentBlock.coinbaseAddress));
m_data.set(RuntimeData::TimeStamp, _ext.currentBlock.timestamp); m_data.set(RuntimeData::TimeStamp, _ext.currentBlock.timestamp);
m_data.set(RuntimeData::Number, _ext.currentBlock.number); m_data.set(RuntimeData::Number, _ext.currentBlock.number);

19
evmjit/libevmjit/BasicBlock.cpp

@ -20,20 +20,21 @@ namespace jit
const char* BasicBlock::NamePrefix = "Instr."; const char* BasicBlock::NamePrefix = "Instr.";
BasicBlock::BasicBlock(ProgramCounter _beginInstIdx, ProgramCounter _endInstIdx, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder) : BasicBlock::BasicBlock(bytes::const_iterator _begin, bytes::const_iterator _end, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder, bool isJumpDest) :
m_beginInstIdx(_beginInstIdx), m_begin(_begin),
m_endInstIdx(_endInstIdx), m_end(_end),
m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), {NamePrefix, std::to_string(_beginInstIdx)}, _mainFunc)), // TODO: Add begin index to name
m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), NamePrefix, _mainFunc)),
m_stack(*this), m_stack(*this),
m_builder(_builder) m_builder(_builder),
m_isJumpDest(isJumpDest)
{} {}
BasicBlock::BasicBlock(std::string _name, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder) : BasicBlock::BasicBlock(std::string _name, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder, bool isJumpDest) :
m_beginInstIdx(0),
m_endInstIdx(0),
m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), _name, _mainFunc)), m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), _name, _mainFunc)),
m_stack(*this), m_stack(*this),
m_builder(_builder) m_builder(_builder),
m_isJumpDest(isJumpDest)
{} {}
BasicBlock::LocalStack::LocalStack(BasicBlock& _owner) : BasicBlock::LocalStack::LocalStack(BasicBlock& _owner) :

29
evmjit/libevmjit/BasicBlock.h

@ -1,9 +1,7 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <llvm/IR/BasicBlock.h> #include <llvm/IR/BasicBlock.h>
#include "Common.h"
#include "Stack.h" #include "Stack.h"
namespace dev namespace dev
@ -52,20 +50,21 @@ public:
BasicBlock& m_bblock; BasicBlock& m_bblock;
}; };
/// Basic block name prefix. The rest is beging instruction index. /// Basic block name prefix. The rest is instruction index.
static const char* NamePrefix; static const char* NamePrefix;
explicit BasicBlock(ProgramCounter _beginInstIdx, ProgramCounter _endInstIdx, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder); explicit BasicBlock(bytes::const_iterator _begin, bytes::const_iterator _end, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder, bool isJumpDest);
explicit BasicBlock(std::string _name, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder); explicit BasicBlock(std::string _name, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder, bool isJumpDest);
BasicBlock(const BasicBlock&) = delete; BasicBlock(const BasicBlock&) = delete;
void operator=(const BasicBlock&) = delete; void operator=(const BasicBlock&) = delete;
operator llvm::BasicBlock*() { return m_llvmBB; }
llvm::BasicBlock* llvm() { return m_llvmBB; } llvm::BasicBlock* llvm() { return m_llvmBB; }
ProgramCounter begin() { return m_beginInstIdx; } bytes::const_iterator begin() { return m_begin; }
ProgramCounter end() { return m_endInstIdx; } bytes::const_iterator end() { return m_end; }
bool isJumpDest() const { return m_isJumpDest; }
LocalStack& localStack() { return m_stack; } LocalStack& localStack() { return m_stack; }
@ -82,8 +81,8 @@ public:
void dump(std::ostream& os, bool _dotOutput = false); void dump(std::ostream& os, bool _dotOutput = false);
private: private:
ProgramCounter const m_beginInstIdx; bytes::const_iterator const m_begin;
ProgramCounter const m_endInstIdx; bytes::const_iterator const m_end;
llvm::BasicBlock* const m_llvmBB; llvm::BasicBlock* const m_llvmBB;
@ -99,16 +98,20 @@ private:
/// the item below the top and so on. The stack grows as the code /// the item below the top and so on. The stack grows as the code
/// accesses more items on the EVM stack but once a value is put on /// accesses more items on the EVM stack but once a value is put on
/// the stack, it will never be replaced. /// the stack, it will never be replaced.
std::vector<llvm::Value*> m_initialStack = {}; std::vector<llvm::Value*> m_initialStack;
/// This stack tracks the contents of the EVM stack as the basic block /// This stack tracks the contents of the EVM stack as the basic block
/// executes. It may grow on both sides, as the code pushes items on /// executes. It may grow on both sides, as the code pushes items on
/// top of the stack or changes existing items. /// top of the stack or changes existing items.
std::vector<llvm::Value*> m_currentStack = {}; std::vector<llvm::Value*> m_currentStack;
/// How many items higher is the current stack than the initial one. /// How many items higher is the current stack than the initial one.
/// May be negative. /// May be negative.
int m_tosOffset = 0; int m_tosOffset = 0;
/// Is the basic block a valid jump destination.
/// JUMPDEST is the first instruction of the basic block.
bool const m_isJumpDest = false;
}; };
} }

42
evmjit/libevmjit/Cache.cpp

@ -3,6 +3,7 @@
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <llvm/IR/Module.h> #include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/Path.h> #include <llvm/Support/Path.h>
#include <llvm/Support/FileSystem.h> #include <llvm/Support/FileSystem.h>
#include <llvm/Support/raw_os_ostream.h> #include <llvm/Support/raw_os_ostream.h>
@ -23,6 +24,33 @@ ObjectCache* Cache::getObjectCache()
return &objectCache; return &objectCache;
} }
namespace
{
llvm::MemoryBuffer* lastObject;
}
std::unique_ptr<llvm::Module> Cache::getObject(std::string const& id)
{
assert(!lastObject);
llvm::SmallString<256> cachePath;
llvm::sys::path::system_temp_directory(false, cachePath);
llvm::sys::path::append(cachePath, "evm_objs", id);
if (auto r = llvm::MemoryBuffer::getFile(cachePath.str(), -1, false))
lastObject = llvm::MemoryBuffer::getMemBufferCopy(r.get()->getBuffer());
else if (r.getError() != std::make_error_code(std::errc::no_such_file_or_directory))
std::cerr << r.getError().message(); // TODO: Add log
if (lastObject) // if object found create fake module
{
auto module = std::unique_ptr<llvm::Module>(new llvm::Module(id, llvm::getGlobalContext()));
auto mainFuncType = llvm::FunctionType::get(llvm::IntegerType::get(llvm::getGlobalContext(), 32), {}, false);
auto func = llvm::Function::Create(mainFuncType, llvm::Function::ExternalLinkage, id, module.get());
(void)func;
}
return nullptr;
}
void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object) void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object)
{ {
@ -43,16 +71,10 @@ void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::Memory
llvm::MemoryBuffer* ObjectCache::getObject(llvm::Module const* _module) llvm::MemoryBuffer* ObjectCache::getObject(llvm::Module const* _module)
{ {
auto&& id = _module->getModuleIdentifier(); (void)_module;
llvm::SmallString<256> cachePath; auto o = lastObject;
llvm::sys::path::system_temp_directory(false, cachePath); lastObject = nullptr;
llvm::sys::path::append(cachePath, "evm_objs", id); return o;
if (auto r = llvm::MemoryBuffer::getFile(cachePath.str(), -1, false))
return llvm::MemoryBuffer::getMemBufferCopy(r.get()->getBuffer());
else if (r.getError() != std::make_error_code(std::errc::no_such_file_or_directory))
std::cerr << r.getError().message(); // TODO: Add log
return nullptr;
} }
} }

2
evmjit/libevmjit/Cache.h

@ -2,7 +2,6 @@
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/ObjectCache.h> #include <llvm/ExecutionEngine/ObjectCache.h>
@ -34,6 +33,7 @@ class Cache
{ {
public: public:
static ObjectCache* getObjectCache(); static ObjectCache* getObjectCache();
static std::unique_ptr<llvm::Module> getObject(std::string const& id);
}; };
} }

10
evmjit/libevmjit/Common.h

@ -37,13 +37,15 @@ enum class ReturnCode
// TODO: Replace with h256 // TODO: Replace with h256
struct i256 struct i256
{ {
uint64_t a; uint64_t a = 0;
uint64_t b; uint64_t b = 0;
uint64_t c; uint64_t c = 0;
uint64_t d; uint64_t d = 0;
}; };
static_assert(sizeof(i256) == 32, "Wrong i265 size"); static_assert(sizeof(i256) == 32, "Wrong i265 size");
#define UNTESTED assert(false)
} }
} }
} }

239
evmjit/libevmjit/Compiler.cpp

@ -5,8 +5,6 @@
#include <fstream> #include <fstream>
#include <chrono> #include <chrono>
#include <boost/dynamic_bitset.hpp>
#include <llvm/ADT/PostOrderIterator.h> #include <llvm/ADT/PostOrderIterator.h>
#include <llvm/IR/CFG.h> #include <llvm/IR/CFG.h>
#include <llvm/IR/Module.h> #include <llvm/IR/Module.h>
@ -42,114 +40,87 @@ Compiler::Compiler(Options const& _options):
void Compiler::createBasicBlocks(bytes const& _bytecode) void Compiler::createBasicBlocks(bytes const& _bytecode)
{ {
std::set<ProgramCounter> splitPoints; // Sorted collections of instruction indices where basic blocks start/end /// Helper function that skips push data and finds next iterator (can be the end)
auto skipPushDataAndGetNext = [](bytes::const_iterator _curr, bytes::const_iterator _end)
std::map<ProgramCounter, ProgramCounter> directJumpTargets;
std::vector<ProgramCounter> indirectJumpTargets;
boost::dynamic_bitset<> validJumpTargets(std::max(_bytecode.size(), size_t(1)));
splitPoints.insert(0); // First basic block
validJumpTargets[0] = true;
for (auto curr = _bytecode.begin(); curr != _bytecode.end(); ++curr)
{ {
ProgramCounter currentPC = curr - _bytecode.begin(); static const auto push1 = static_cast<size_t>(Instruction::PUSH1);
validJumpTargets[currentPC] = true; static const auto push32 = static_cast<size_t>(Instruction::PUSH32);
size_t offset = 1;
auto inst = Instruction(*curr); if (*_curr >= push1 && *_curr <= push32)
switch (inst) offset += std::min<size_t>(*_curr - push1 + 1, (_end - _curr) - 1);
{ return _curr + offset;
};
auto begin = _bytecode.begin();
bool nextJumpDest = false;
for (auto curr = begin, next = begin; curr != _bytecode.end(); curr = next)
{
next = skipPushDataAndGetNext(curr, _bytecode.end());
case Instruction::ANY_PUSH: bool isEnd = false;
switch (Instruction(*curr))
{ {
auto val = readPushData(curr, _bytecode.end());
auto next = curr + 1;
if (next == _bytecode.end())
break;
auto nextInst = Instruction(*next);
if (nextInst == Instruction::JUMP || nextInst == Instruction::JUMPI)
{
// Create a block for the JUMP target.
ProgramCounter targetPC = val.ult(_bytecode.size()) ? val.getZExtValue() : _bytecode.size();
splitPoints.insert(targetPC);
ProgramCounter jumpPC = (next - _bytecode.begin());
directJumpTargets[jumpPC] = targetPC;
}
break;
}
case Instruction::JUMPDEST:
{
// A basic block starts here.
splitPoints.insert(currentPC);
indirectJumpTargets.push_back(currentPC);
break;
}
case Instruction::JUMP: case Instruction::JUMP:
case Instruction::JUMPI: case Instruction::JUMPI:
case Instruction::RETURN: case Instruction::RETURN:
case Instruction::STOP: case Instruction::STOP:
case Instruction::SUICIDE: case Instruction::SUICIDE:
{ isEnd = true;
// Create a basic block starting at the following instruction. break;
if (curr + 1 < _bytecode.end())
splitPoints.insert(currentPC + 1); case Instruction::JUMPDEST:
nextJumpDest = true;
break; break;
}
default: default:
break; break;
} }
}
// Remove split points generated from jumps out of code or into data. assert(next <= _bytecode.end());
for (auto it = splitPoints.cbegin(); it != splitPoints.cend();) if (next == _bytecode.end() || Instruction(*next) == Instruction::JUMPDEST)
{ isEnd = true;
if (*it > _bytecode.size() || !validJumpTargets[*it])
it = splitPoints.erase(it);
else
++it;
}
for (auto it = splitPoints.cbegin(); it != splitPoints.cend();) if (isEnd)
{ {
auto beginInstIdx = *it; auto beginIdx = begin - _bytecode.begin();
++it; m_basicBlocks.emplace(std::piecewise_construct, std::forward_as_tuple(beginIdx),
auto endInstIdx = it != splitPoints.cend() ? *it : _bytecode.size(); std::forward_as_tuple(begin, next, m_mainFunc, m_builder, nextJumpDest));
basicBlocks.emplace(std::piecewise_construct, std::forward_as_tuple(beginInstIdx), std::forward_as_tuple(beginInstIdx, endInstIdx, m_mainFunc, m_builder)); nextJumpDest = false;
begin = next;
}
} }
m_stopBB = llvm::BasicBlock::Create(m_mainFunc->getContext(), "Stop", m_mainFunc); m_stopBB = llvm::BasicBlock::Create(m_mainFunc->getContext(), "Stop", m_mainFunc);
m_badJumpBlock = std::unique_ptr<BasicBlock>(new BasicBlock("BadJumpBlock", m_mainFunc, m_builder)); }
m_jumpTableBlock = std::unique_ptr<BasicBlock>(new BasicBlock("JumpTableBlock", m_mainFunc, m_builder));
for (auto it = directJumpTargets.cbegin(); it != directJumpTargets.cend(); ++it) llvm::BasicBlock* Compiler::getJumpTableBlock()
{
if (!m_jumpTableBlock)
{ {
if (it->second >= _bytecode.size()) m_jumpTableBlock.reset(new BasicBlock("JumpTable", m_mainFunc, m_builder, true));
{ InsertPointGuard g{m_builder};
// Jumping out of code means STOP m_builder.SetInsertPoint(m_jumpTableBlock->llvm());
m_directJumpTargets[it->first] = m_stopBB; auto dest = m_jumpTableBlock->localStack().pop();
continue; auto switchInstr = m_builder.CreateSwitch(dest, getBadJumpBlock());
} for (auto&& p : m_basicBlocks)
auto blockIter = basicBlocks.find(it->second);
if (blockIter != basicBlocks.end())
{
m_directJumpTargets[it->first] = blockIter->second.llvm();
}
else
{ {
clog(JIT) << "Bad JUMP at PC " << it->first if (p.second.isJumpDest())
<< ": " << it->second << " is not a valid PC"; switchInstr->addCase(Constant::get(p.first), p.second.llvm());
m_directJumpTargets[it->first] = m_badJumpBlock->llvm();
} }
} }
return m_jumpTableBlock->llvm();
}
for (auto it = indirectJumpTargets.cbegin(); it != indirectJumpTargets.cend(); ++it) llvm::BasicBlock* Compiler::getBadJumpBlock()
m_indirectJumpTargets.push_back(&basicBlocks.find(*it)->second); {
if (!m_badJumpBlock)
{
m_badJumpBlock.reset(new BasicBlock("BadJump", m_mainFunc, m_builder, true));
InsertPointGuard g{m_builder};
m_builder.SetInsertPoint(m_badJumpBlock->llvm());
m_builder.CreateRet(Constant::get(ReturnCode::BadJumpDestination));
}
return m_badJumpBlock->llvm();
} }
std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode, std::string const& _id) std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode, std::string const& _id)
@ -176,14 +147,14 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode, std::str
Stack stack(m_builder, runtimeManager); Stack stack(m_builder, runtimeManager);
Arith256 arith(m_builder); Arith256 arith(m_builder);
m_builder.CreateBr(basicBlocks.begin()->second); m_builder.CreateBr(m_basicBlocks.empty() ? m_stopBB : m_basicBlocks.begin()->second.llvm());
for (auto basicBlockPairIt = basicBlocks.begin(); basicBlockPairIt != basicBlocks.end(); ++basicBlockPairIt) for (auto basicBlockPairIt = m_basicBlocks.begin(); basicBlockPairIt != m_basicBlocks.end(); ++basicBlockPairIt)
{ {
auto& basicBlock = basicBlockPairIt->second; auto& basicBlock = basicBlockPairIt->second;
auto iterCopy = basicBlockPairIt; auto iterCopy = basicBlockPairIt;
++iterCopy; ++iterCopy;
auto nextBasicBlock = (iterCopy != basicBlocks.end()) ? iterCopy->second.llvm() : nullptr; auto nextBasicBlock = (iterCopy != m_basicBlocks.end()) ? iterCopy->second.llvm() : nullptr;
compileBasicBlock(basicBlock, _bytecode, runtimeManager, arith, memory, ext, gasMeter, nextBasicBlock); compileBasicBlock(basicBlock, _bytecode, runtimeManager, arith, memory, ext, gasMeter, nextBasicBlock);
} }
@ -192,25 +163,6 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode, std::str
m_builder.SetInsertPoint(m_stopBB); m_builder.SetInsertPoint(m_stopBB);
m_builder.CreateRet(Constant::get(ReturnCode::Stop)); m_builder.CreateRet(Constant::get(ReturnCode::Stop));
m_builder.SetInsertPoint(m_badJumpBlock->llvm());
m_builder.CreateRet(Constant::get(ReturnCode::BadJumpDestination));
m_builder.SetInsertPoint(m_jumpTableBlock->llvm());
if (m_indirectJumpTargets.size() > 0)
{
auto dest = m_jumpTableBlock->localStack().pop();
auto switchInstr = m_builder.CreateSwitch(dest, m_badJumpBlock->llvm(),
m_indirectJumpTargets.size());
for (auto it = m_indirectJumpTargets.cbegin(); it != m_indirectJumpTargets.cend(); ++it)
{
auto& bb = *it;
auto dest = Constant::get(bb->begin());
switchInstr->addCase(dest, bb->llvm());
}
}
else
m_builder.CreateBr(m_badJumpBlock->llvm());
removeDeadBlocks(); removeDeadBlocks();
dumpCFGifRequired("blocks-init.dot"); dumpCFGifRequired("blocks-init.dot");
@ -218,7 +170,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode, std::str
if (m_options.optimizeStack) if (m_options.optimizeStack)
{ {
std::vector<BasicBlock*> blockList; std::vector<BasicBlock*> blockList;
for (auto& entry : basicBlocks) for (auto& entry : m_basicBlocks)
blockList.push_back(&entry.second); blockList.push_back(&entry.second);
if (m_jumpTableBlock) if (m_jumpTableBlock)
@ -229,7 +181,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode, std::str
dumpCFGifRequired("blocks-opt.dot"); dumpCFGifRequired("blocks-opt.dot");
} }
for (auto& entry : basicBlocks) for (auto& entry : m_basicBlocks)
entry.second.synchronizeLocalStack(stack); entry.second.synchronizeLocalStack(stack);
if (m_jumpTableBlock) if (m_jumpTableBlock)
m_jumpTableBlock->synchronizeLocalStack(stack); m_jumpTableBlock->synchronizeLocalStack(stack);
@ -259,9 +211,9 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
m_builder.SetInsertPoint(_basicBlock.llvm()); m_builder.SetInsertPoint(_basicBlock.llvm());
auto& stack = _basicBlock.localStack(); auto& stack = _basicBlock.localStack();
for (auto currentPC = _basicBlock.begin(); currentPC != _basicBlock.end(); ++currentPC) for (auto it = _basicBlock.begin(); it != _basicBlock.end(); ++it)
{ {
auto inst = static_cast<Instruction>(_bytecode[currentPC]); auto inst = Instruction(*it);
_gasMeter.count(inst); _gasMeter.count(inst);
@ -523,10 +475,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
case Instruction::ANY_PUSH: case Instruction::ANY_PUSH:
{ {
auto curr = _bytecode.begin() + currentPC; // TODO: replace currentPC with iterator auto value = readPushData(it, _basicBlock.end());
auto value = readPushData(curr, _bytecode.end());
currentPC = curr - _bytecode.begin();
stack.push(Constant::get(value)); stack.push(Constant::get(value));
break; break;
} }
@ -596,46 +545,45 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
case Instruction::JUMP: case Instruction::JUMP:
case Instruction::JUMPI: case Instruction::JUMPI:
{ {
// Generate direct jump iff:
// 1. this is not the first instruction in the block
// 2. m_directJumpTargets[currentPC] is defined (meaning that the previous instruction is a PUSH)
// Otherwise generate a indirect jump (a switch).
llvm::BasicBlock* targetBlock = nullptr; llvm::BasicBlock* targetBlock = nullptr;
if (currentPC != _basicBlock.begin()) auto target = stack.pop();
if (auto constant = llvm::dyn_cast<llvm::ConstantInt>(target))
{ {
auto pairIter = m_directJumpTargets.find(currentPC); auto&& c = constant->getValue();
if (pairIter != m_directJumpTargets.end()) auto targetIdx = c.getActiveBits() <= 64 ? c.getZExtValue() : -1;
targetBlock = pairIter->second; auto it = m_basicBlocks.find(targetIdx);
targetBlock = (it != m_basicBlocks.end() && it->second.isJumpDest()) ? it->second.llvm() : getBadJumpBlock();
} }
// TODO: Improve; check for constants
if (inst == Instruction::JUMP) if (inst == Instruction::JUMP)
{ {
if (targetBlock) if (targetBlock)
{ {
// The target address is computed at compile time,
// just pop it without looking...
stack.pop();
m_builder.CreateBr(targetBlock); m_builder.CreateBr(targetBlock);
} }
else else
m_builder.CreateBr(m_jumpTableBlock->llvm()); {
stack.push(target);
m_builder.CreateBr(getJumpTableBlock());
}
} }
else // JUMPI else // JUMPI
{ {
stack.swap(1);
auto val = stack.pop(); auto val = stack.pop();
auto zero = Constant::get(0); auto zero = Constant::get(0);
auto cond = m_builder.CreateICmpNE(val, zero, "nonzero"); auto cond = m_builder.CreateICmpNE(val, zero, "nonzero");
if (targetBlock) if (targetBlock)
{ {
stack.pop();
m_builder.CreateCondBr(cond, targetBlock, _nextBasicBlock); m_builder.CreateCondBr(cond, targetBlock, _nextBasicBlock);
} }
else else
m_builder.CreateCondBr(cond, m_jumpTableBlock->llvm(), _nextBasicBlock); {
stack.push(target);
m_builder.CreateCondBr(cond, getJumpTableBlock(), _nextBasicBlock);
}
} }
break; break;
} }
@ -647,7 +595,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
case Instruction::PC: case Instruction::PC:
{ {
auto value = Constant::get(currentPC); auto value = Constant::get(it - _bytecode.begin());
stack.push(value); stack.push(value);
break; break;
} }
@ -666,7 +614,6 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
case Instruction::CALLDATASIZE: case Instruction::CALLDATASIZE:
case Instruction::CODESIZE: case Instruction::CODESIZE:
case Instruction::GASPRICE: case Instruction::GASPRICE:
case Instruction::PREVHASH:
case Instruction::COINBASE: case Instruction::COINBASE:
case Instruction::TIMESTAMP: case Instruction::TIMESTAMP:
case Instruction::NUMBER: case Instruction::NUMBER:
@ -678,6 +625,14 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
break; break;
} }
case Instruction::BLOCKHASH:
{
auto number = stack.pop();
auto hash = _ext.blockhash(number);
stack.push(hash);
break;
}
case Instruction::BALANCE: case Instruction::BALANCE:
{ {
auto address = stack.pop(); auto address = stack.pop();
@ -835,6 +790,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode
default: // Invalid instruction - runtime exception default: // Invalid instruction - runtime exception
{ {
// TODO: Replace with return statement
_runtimeManager.raiseException(ReturnCode::BadInstruction); _runtimeManager.raiseException(ReturnCode::BadInstruction);
} }
@ -857,13 +813,13 @@ void Compiler::removeDeadBlocks()
do do
{ {
sthErased = false; sthErased = false;
for (auto it = basicBlocks.begin(); it != basicBlocks.end();) for (auto it = m_basicBlocks.begin(); it != m_basicBlocks.end();)
{ {
auto llvmBB = it->second.llvm(); auto llvmBB = it->second.llvm();
if (llvm::pred_begin(llvmBB) == llvm::pred_end(llvmBB)) if (llvm::pred_begin(llvmBB) == llvm::pred_end(llvmBB))
{ {
llvmBB->eraseFromParent(); llvmBB->eraseFromParent();
basicBlocks.erase(it++); m_basicBlocks.erase(it++);
sthErased = true; sthErased = true;
} }
else else
@ -871,13 +827,6 @@ void Compiler::removeDeadBlocks()
} }
} }
while (sthErased); while (sthErased);
// Remove jump table block if no predecessors
if (llvm::pred_begin(m_jumpTableBlock->llvm()) == llvm::pred_end(m_jumpTableBlock->llvm()))
{
m_jumpTableBlock->llvm()->eraseFromParent();
m_jumpTableBlock.reset();
}
} }
void Compiler::dumpCFGifRequired(std::string const& _dotfilePath) void Compiler::dumpCFGifRequired(std::string const& _dotfilePath)
@ -898,7 +847,7 @@ void Compiler::dumpCFGtoStream(std::ostream& _out)
<< " entry [share=record, label=\"entry block\"];\n"; << " entry [share=record, label=\"entry block\"];\n";
std::vector<BasicBlock*> blocks; std::vector<BasicBlock*> blocks;
for (auto& pair : basicBlocks) for (auto& pair : m_basicBlocks)
blocks.push_back(&pair.second); blocks.push_back(&pair.second);
if (m_jumpTableBlock) if (m_jumpTableBlock)
blocks.push_back(m_jumpTableBlock.get()); blocks.push_back(m_jumpTableBlock.get());
@ -937,7 +886,7 @@ void Compiler::dumpCFGtoStream(std::ostream& _out)
void Compiler::dump() void Compiler::dump()
{ {
for (auto& entry : basicBlocks) for (auto& entry : m_basicBlocks)
entry.second.dump(); entry.second.dump();
if (m_jumpTableBlock != nullptr) if (m_jumpTableBlock != nullptr)
m_jumpTableBlock->dump(); m_jumpTableBlock->dump();

30
evmjit/libevmjit/Compiler.h

@ -20,19 +20,13 @@ public:
struct Options struct Options
{ {
/// Optimize stack operations between basic blocks /// Optimize stack operations between basic blocks
bool optimizeStack; bool optimizeStack = true;
/// Rewrite switch instructions to sequences of branches /// Rewrite switch instructions to sequences of branches
bool rewriteSwitchToBranches; bool rewriteSwitchToBranches = true;
/// Dump CFG as a .dot file for graphviz /// Dump CFG as a .dot file for graphviz
bool dumpCFG; bool dumpCFG = false;
Options():
optimizeStack(true),
rewriteSwitchToBranches(true),
dumpCFG(false)
{}
}; };
using ProgramCounter = uint64_t; using ProgramCounter = uint64_t;
@ -47,6 +41,10 @@ private:
void compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode, class RuntimeManager& _runtimeManager, class Arith256& _arith, class Memory& _memory, class Ext& _ext, class GasMeter& _gasMeter, llvm::BasicBlock* _nextBasicBlock); void compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode, class RuntimeManager& _runtimeManager, class Arith256& _arith, class Memory& _memory, class Ext& _ext, class GasMeter& _gasMeter, llvm::BasicBlock* _nextBasicBlock);
llvm::BasicBlock* getJumpTableBlock();
llvm::BasicBlock* getBadJumpBlock();
void removeDeadBlocks(); void removeDeadBlocks();
/// Dumps basic block graph in graphviz format to a file, if option dumpCFG is enabled. /// Dumps basic block graph in graphviz format to a file, if option dumpCFG is enabled.
@ -65,22 +63,16 @@ private:
llvm::IRBuilder<> m_builder; llvm::IRBuilder<> m_builder;
/// Maps a program counter pc to a basic block that starts at pc (if any). /// Maps a program counter pc to a basic block that starts at pc (if any).
std::map<ProgramCounter, BasicBlock> basicBlocks = {}; std::map<ProgramCounter, BasicBlock> m_basicBlocks;
/// Maps a pc at which there is a JUMP or JUMPI to the target block of the jump.
std::map<ProgramCounter, llvm::BasicBlock*> m_directJumpTargets = {};
/// A list of possible blocks to which there may be indirect jumps.
std::vector<BasicBlock*> m_indirectJumpTargets = {};
/// Stop basic block - terminates execution with STOP code (0) /// Stop basic block - terminates execution with STOP code (0)
llvm::BasicBlock* m_stopBB = nullptr; llvm::BasicBlock* m_stopBB = nullptr;
/// Block with a jump table. /// Block with a jump table.
std::unique_ptr<BasicBlock> m_jumpTableBlock = nullptr; std::unique_ptr<BasicBlock> m_jumpTableBlock;
/// Default destination for indirect jumps. /// Destination for invalid jumps
std::unique_ptr<BasicBlock> m_badJumpBlock = nullptr; std::unique_ptr<BasicBlock> m_badJumpBlock;
/// Main program function /// Main program function
llvm::Function* m_mainFunc = nullptr; llvm::Function* m_mainFunc = nullptr;

5
evmjit/libevmjit/CompilerHelper.cpp

@ -35,6 +35,11 @@ llvm::Function* CompilerHelper::getMainFunction()
return nullptr; return nullptr;
} }
llvm::CallInst* CompilerHelper::createCall(llvm::Function* _func, std::initializer_list<llvm::Value*> const& _args)
{
return getBuilder().CreateCall(_func, {_args.begin(), _args.size()});
}
RuntimeHelper::RuntimeHelper(RuntimeManager& _runtimeManager): RuntimeHelper::RuntimeHelper(RuntimeManager& _runtimeManager):
CompilerHelper(_runtimeManager.getBuilder()), CompilerHelper(_runtimeManager.getBuilder()),

7
evmjit/libevmjit/CompilerHelper.h

@ -31,12 +31,7 @@ protected:
llvm::IRBuilder<>& m_builder; llvm::IRBuilder<>& m_builder;
llvm::IRBuilder<>& getBuilder() { return m_builder; } llvm::IRBuilder<>& getBuilder() { return m_builder; }
template<typename ..._Args> llvm::CallInst* createCall(llvm::Function* _func, std::initializer_list<llvm::Value*> const& _args);
llvm::CallInst* createCall(llvm::Function* _func, _Args*... _args)
{
llvm::Value* args[] = {_args...};
return getBuilder().CreateCall(_func, args);
}
friend class RuntimeHelper; friend class RuntimeHelper;
}; };

16
evmjit/libevmjit/ExecutionEngine.cpp

@ -4,8 +4,13 @@
#include <llvm/IR/Module.h> #include <llvm/IR/Module.h>
#include <llvm/ADT/Triple.h> #include <llvm/ADT/Triple.h>
#pragma warning(push)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <llvm/ExecutionEngine/ExecutionEngine.h> #include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h> #include <llvm/ExecutionEngine/SectionMemoryManager.h>
#pragma warning(pop)
#pragma GCC diagnostic pop
#include <llvm/ExecutionEngine/MCJIT.h> #include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/Support/TargetSelect.h> #include <llvm/Support/TargetSelect.h>
#include <llvm/Support/Host.h> #include <llvm/Support/Host.h>
@ -69,7 +74,13 @@ ReturnCode ExecutionEngine::run(bytes const& _code, RuntimeData* _data, Env* _en
} }
else else
{ {
auto module = Compiler({}).compile(_code, mainFuncName); bool objectCacheEnabled = true;
auto objectCache = objectCacheEnabled ? Cache::getObjectCache() : nullptr;
std::unique_ptr<llvm::Module> module;
if (objectCache)
module = Cache::getObject(mainFuncName);
if (!module)
module = Compiler({}).compile(_code, mainFuncName);
//module->dump(); //module->dump();
if (!ee) if (!ee)
{ {
@ -95,7 +106,8 @@ ReturnCode ExecutionEngine::run(bytes const& _code, RuntimeData* _data, Env* _en
module.release(); // Successfully created llvm::ExecutionEngine takes ownership of the module module.release(); // Successfully created llvm::ExecutionEngine takes ownership of the module
memoryManager.release(); // and memory manager memoryManager.release(); // and memory manager
ee->setObjectCache(Cache::getObjectCache()); if (objectCache)
ee->setObjectCache(objectCache);
entryFuncPtr = (EntryFuncPtr)ee->getFunctionAddress(mainFuncName); entryFuncPtr = (EntryFuncPtr)ee->getFunctionAddress(mainFuncName);
} }
else else

153
evmjit/libevmjit/Ext.cpp

@ -23,114 +23,137 @@ namespace jit
Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan): Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan):
RuntimeHelper(_runtimeManager), RuntimeHelper(_runtimeManager),
m_memoryMan(_memoryMan) m_memoryMan(_memoryMan)
// TODO: fix: either initialise properly or don't specify in constructor.
/*,
m_funcs{},
m_argAllocas{}*/
{ {
auto module = getModule();
m_args[0] = m_builder.CreateAlloca(Type::Word, nullptr, "ext.index");
m_args[1] = m_builder.CreateAlloca(Type::Word, nullptr, "ext.value");
m_arg2 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg2");
m_arg3 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg3");
m_arg4 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg4");
m_arg5 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg5");
m_arg6 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg6");
m_arg7 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg7");
m_arg8 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg8");
m_size = m_builder.CreateAlloca(Type::Size, nullptr, "env.size"); m_size = m_builder.CreateAlloca(Type::Size, nullptr, "env.size");
}
using Linkage = llvm::GlobalValue::LinkageTypes;
llvm::Type* argsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr};
m_sload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "env_sload", module); using FuncDesc = std::tuple<char const*, llvm::FunctionType*>;
m_sstore = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "env_sstore", module);
llvm::Type* sha3ArgsTypes[] = {Type::BytePtr, Type::Size, Type::WordPtr}; llvm::FunctionType* getFunctionType(llvm::Type* _returnType, std::initializer_list<llvm::Type*> const& _argsTypes)
m_sha3 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, sha3ArgsTypes, false), Linkage::ExternalLinkage, "env_sha3", module); {
return llvm::FunctionType::get(_returnType, llvm::ArrayRef<llvm::Type*>{_argsTypes.begin(), _argsTypes.size()}, false);
}
llvm::Type* createArgsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::WordPtr}; std::array<FuncDesc, sizeOf<EnvFunc>::value> const& getEnvFuncDescs()
m_create = llvm::Function::Create(llvm::FunctionType::get(Type::Void, createArgsTypes, false), Linkage::ExternalLinkage, "env_create", module); {
static std::array<FuncDesc, sizeOf<EnvFunc>::value> descs{{
FuncDesc{"env_sload", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})},
FuncDesc{"env_sstore", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})},
FuncDesc{"env_sha3", getFunctionType(Type::Void, {Type::BytePtr, Type::Size, Type::WordPtr})},
FuncDesc{"env_balance", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})},
FuncDesc{"env_create", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::WordPtr})},
FuncDesc{"env_call", getFunctionType(Type::Bool, {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::BytePtr, Type::Size, Type::WordPtr})},
FuncDesc{"env_log", getFunctionType(Type::Void, {Type::EnvPtr, Type::BytePtr, Type::Size, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr})},
FuncDesc{"env_blockhash", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})},
FuncDesc{"env_getExtCode", getFunctionType(Type::BytePtr, {Type::EnvPtr, Type::WordPtr, Type::Size->getPointerTo()})},
FuncDesc{"ext_calldataload", getFunctionType(Type::Void, {Type::RuntimeDataPtr, Type::WordPtr, Type::WordPtr})},
}};
return descs;
}
llvm::Type* callArgsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::BytePtr, Type::Size, Type::WordPtr}; llvm::Function* createFunc(EnvFunc _id, llvm::Module* _module)
m_call = llvm::Function::Create(llvm::FunctionType::get(Type::Bool, callArgsTypes, false), Linkage::ExternalLinkage, "env_call", module); {
auto&& desc = getEnvFuncDescs()[static_cast<size_t>(_id)];
return llvm::Function::Create(std::get<1>(desc), llvm::Function::ExternalLinkage, std::get<0>(desc), _module);
}
llvm::Type* logArgsTypes[] = {Type::EnvPtr, Type::BytePtr, Type::Size, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr}; llvm::Value* Ext::getArgAlloca()
m_log = llvm::Function::Create(llvm::FunctionType::get(Type::Void, logArgsTypes, false), Linkage::ExternalLinkage, "env_log", module); {
auto& a = m_argAllocas[m_argCounter++];
if (!a)
{
// FIXME: Improve order and names
InsertPointGuard g{getBuilder()};
getBuilder().SetInsertPoint(getMainFunction()->front().getFirstNonPHI());
a = getBuilder().CreateAlloca(Type::Word, nullptr, "arg");
}
llvm::Type* getExtCodeArgsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::Size->getPointerTo()}; return a;
m_getExtCode = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, getExtCodeArgsTypes, false), Linkage::ExternalLinkage, "env_getExtCode", module); }
// Helper function, not client Env interface llvm::Value* Ext::byPtr(llvm::Value* _value)
llvm::Type* callDataLoadArgsTypes[] = {Type::RuntimeDataPtr, Type::WordPtr, Type::WordPtr}; {
m_calldataload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, callDataLoadArgsTypes, false), Linkage::ExternalLinkage, "ext_calldataload", module); auto a = getArgAlloca();
getBuilder().CreateStore(_value, a);
return a;
} }
llvm::Function* Ext::getBalanceFunc() llvm::CallInst* Ext::createCall(EnvFunc _funcId, std::initializer_list<llvm::Value*> const& _args)
{ {
if (!m_balance) auto& func = m_funcs[static_cast<size_t>(_funcId)];
{ if (!func)
llvm::Type* argsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr}; func = createFunc(_funcId, getModule());
m_balance = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argsTypes, false), llvm::Function::ExternalLinkage, "env_balance", getModule());
} m_argCounter = 0;
return m_balance; return getBuilder().CreateCall(func, {_args.begin(), _args.size()});
} }
llvm::Value* Ext::sload(llvm::Value* _index) llvm::Value* Ext::sload(llvm::Value* _index)
{ {
m_builder.CreateStore(_index, m_args[0]); auto ret = getArgAlloca();
m_builder.CreateCall3(m_sload, getRuntimeManager().getEnvPtr(), m_args[0], m_args[1]); // Uses native endianness createCall(EnvFunc::sload, {getRuntimeManager().getEnvPtr(), byPtr(_index), ret}); // Uses native endianness
return m_builder.CreateLoad(m_args[1]); return m_builder.CreateLoad(ret);
} }
void Ext::sstore(llvm::Value* _index, llvm::Value* _value) void Ext::sstore(llvm::Value* _index, llvm::Value* _value)
{ {
m_builder.CreateStore(_index, m_args[0]); createCall(EnvFunc::sstore, {getRuntimeManager().getEnvPtr(), byPtr(_index), byPtr(_value)}); // Uses native endianness
m_builder.CreateStore(_value, m_args[1]);
m_builder.CreateCall3(m_sstore, getRuntimeManager().getEnvPtr(), m_args[0], m_args[1]); // Uses native endianness
} }
llvm::Value* Ext::calldataload(llvm::Value* _index) llvm::Value* Ext::calldataload(llvm::Value* _index)
{ {
m_builder.CreateStore(_index, m_args[0]); auto ret = getArgAlloca();
createCall(m_calldataload, getRuntimeManager().getDataPtr(), m_args[0], m_args[1]); createCall(EnvFunc::calldataload, {getRuntimeManager().getDataPtr(), byPtr(_index), ret});
auto ret = m_builder.CreateLoad(m_args[1]); ret = m_builder.CreateLoad(ret);
return Endianness::toNative(m_builder, ret); return Endianness::toNative(m_builder, ret);
} }
llvm::Value* Ext::balance(llvm::Value* _address) llvm::Value* Ext::balance(llvm::Value* _address)
{ {
auto address = Endianness::toBE(m_builder, _address); auto address = Endianness::toBE(m_builder, _address);
m_builder.CreateStore(address, m_args[0]); auto ret = getArgAlloca();
createCall(getBalanceFunc(), getRuntimeManager().getEnvPtr(), m_args[0], m_args[1]); createCall(EnvFunc::balance, {getRuntimeManager().getEnvPtr(), byPtr(address), ret});
return m_builder.CreateLoad(m_args[1]); return m_builder.CreateLoad(ret);
}
llvm::Value* Ext::blockhash(llvm::Value* _number)
{
auto hash = getArgAlloca();
createCall(EnvFunc::blockhash, {getRuntimeManager().getEnvPtr(), byPtr(_number), hash});
hash = m_builder.CreateLoad(hash);
return Endianness::toNative(getBuilder(), hash);
} }
llvm::Value* Ext::create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize) llvm::Value* Ext::create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize)
{ {
m_builder.CreateStore(_gas, m_args[0]); auto gas = byPtr(_gas);
m_builder.CreateStore(_endowment, m_arg2); auto ret = getArgAlloca();
auto begin = m_memoryMan.getBytePtr(_initOff); auto begin = m_memoryMan.getBytePtr(_initOff);
auto size = m_builder.CreateTrunc(_initSize, Type::Size, "size"); auto size = m_builder.CreateTrunc(_initSize, Type::Size, "size");
createCall(m_create, getRuntimeManager().getEnvPtr(), m_args[0], m_arg2, begin, size, m_args[1]); createCall(EnvFunc::create, {getRuntimeManager().getEnvPtr(), gas, byPtr(_endowment), begin, size, ret});
_gas = m_builder.CreateLoad(m_args[0]); // Return gas _gas = m_builder.CreateLoad(gas); // Return gas
llvm::Value* address = m_builder.CreateLoad(m_args[1]); llvm::Value* address = m_builder.CreateLoad(ret);
address = Endianness::toNative(m_builder, address); address = Endianness::toNative(m_builder, address);
return address; return address;
} }
llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress) llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress)
{ {
m_builder.CreateStore(_gas, m_args[0]); auto gas = byPtr(_gas);
auto receiveAddress = Endianness::toBE(m_builder, _receiveAddress); auto receiveAddress = Endianness::toBE(m_builder, _receiveAddress);
m_builder.CreateStore(receiveAddress, m_arg2);
m_builder.CreateStore(_value, m_arg3);
auto inBeg = m_memoryMan.getBytePtr(_inOff); auto inBeg = m_memoryMan.getBytePtr(_inOff);
auto inSize = m_builder.CreateTrunc(_inSize, Type::Size, "in.size"); auto inSize = m_builder.CreateTrunc(_inSize, Type::Size, "in.size");
auto outBeg = m_memoryMan.getBytePtr(_outOff); auto outBeg = m_memoryMan.getBytePtr(_outOff);
auto outSize = m_builder.CreateTrunc(_outSize, Type::Size, "out.size"); auto outSize = m_builder.CreateTrunc(_outSize, Type::Size, "out.size");
auto codeAddress = Endianness::toBE(m_builder, _codeAddress); auto codeAddress = Endianness::toBE(m_builder, _codeAddress);
m_builder.CreateStore(codeAddress, m_arg8); auto ret = createCall(EnvFunc::call, {getRuntimeManager().getEnvPtr(), gas, byPtr(receiveAddress), byPtr(_value), inBeg, inSize, outBeg, outSize, byPtr(codeAddress)});
auto ret = createCall(m_call, getRuntimeManager().getEnvPtr(), m_args[0], m_arg2, m_arg3, inBeg, inSize, outBeg, outSize, m_arg8); _gas = m_builder.CreateLoad(gas); // Return gas
_gas = m_builder.CreateLoad(m_args[0]); // Return gas
return m_builder.CreateZExt(ret, Type::Word, "ret"); return m_builder.CreateZExt(ret, Type::Word, "ret");
} }
@ -138,8 +161,9 @@ llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize)
{ {
auto begin = m_memoryMan.getBytePtr(_inOff); auto begin = m_memoryMan.getBytePtr(_inOff);
auto size = m_builder.CreateTrunc(_inSize, Type::Size, "size"); auto size = m_builder.CreateTrunc(_inSize, Type::Size, "size");
createCall(m_sha3, begin, size, m_args[1]); auto ret = getArgAlloca();
llvm::Value* hash = m_builder.CreateLoad(m_args[1]); createCall(EnvFunc::sha3, {begin, size, ret});
llvm::Value* hash = m_builder.CreateLoad(ret);
hash = Endianness::toNative(m_builder, hash); hash = Endianness::toNative(m_builder, hash);
return hash; return hash;
} }
@ -147,8 +171,7 @@ llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize)
MemoryRef Ext::getExtCode(llvm::Value* _addr) MemoryRef Ext::getExtCode(llvm::Value* _addr)
{ {
auto addr = Endianness::toBE(m_builder, _addr); auto addr = Endianness::toBE(m_builder, _addr);
m_builder.CreateStore(addr, m_args[0]); auto code = createCall(EnvFunc::getExtCode, {getRuntimeManager().getEnvPtr(), byPtr(addr), m_size});
auto code = createCall(m_getExtCode, getRuntimeManager().getEnvPtr(), m_args[0], m_size);
auto codeSize = m_builder.CreateLoad(m_size); auto codeSize = m_builder.CreateLoad(m_size);
auto codeSize256 = m_builder.CreateZExt(codeSize, Type::Word); auto codeSize256 = m_builder.CreateZExt(codeSize, Type::Word);
return {code, codeSize256}; return {code, codeSize256};
@ -158,7 +181,7 @@ void Ext::log(llvm::Value* _memIdx, llvm::Value* _numBytes, std::array<llvm::Val
{ {
auto begin = m_memoryMan.getBytePtr(_memIdx); auto begin = m_memoryMan.getBytePtr(_memIdx);
auto size = m_builder.CreateTrunc(_numBytes, Type::Size, "size"); auto size = m_builder.CreateTrunc(_numBytes, Type::Size, "size");
llvm::Value* args[] = {getRuntimeManager().getEnvPtr(), begin, size, m_arg2, m_arg3, m_arg4, m_arg5}; llvm::Value* args[] = {getRuntimeManager().getEnvPtr(), begin, size, getArgAlloca(), getArgAlloca(), getArgAlloca(), getArgAlloca()};
auto topicArgPtr = &args[3]; auto topicArgPtr = &args[3];
for (auto&& topic : _topics) for (auto&& topic : _topics)
@ -170,7 +193,7 @@ void Ext::log(llvm::Value* _memIdx, llvm::Value* _numBytes, std::array<llvm::Val
++topicArgPtr; ++topicArgPtr;
} }
m_builder.CreateCall(m_log, args); createCall(EnvFunc::log, {args[0], args[1], args[2], args[3], args[4], args[5], args[6]}); // TODO: use std::initializer_list<>
} }
} }

50
evmjit/libevmjit/Ext.h

@ -18,6 +18,28 @@ struct MemoryRef
llvm::Value* size; llvm::Value* size;
}; };
template<typename _EnumT>
struct sizeOf
{
static const size_t value = static_cast<size_t>(_EnumT::_size);
};
enum class EnvFunc
{
sload,
sstore,
sha3,
balance,
create,
call,
log,
blockhash,
getExtCode,
calldataload, // Helper function, not client Env interface
_size
};
class Ext : public RuntimeHelper class Ext : public RuntimeHelper
{ {
public: public:
@ -30,6 +52,7 @@ public:
llvm::Value* calldataload(llvm::Value* _index); llvm::Value* calldataload(llvm::Value* _index);
llvm::Value* create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize); llvm::Value* create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize);
llvm::Value* call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress); llvm::Value* call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress);
llvm::Value* blockhash(llvm::Value* _number);
llvm::Value* sha3(llvm::Value* _inOff, llvm::Value* _inSize); llvm::Value* sha3(llvm::Value* _inOff, llvm::Value* _inSize);
MemoryRef getExtCode(llvm::Value* _addr); MemoryRef getExtCode(llvm::Value* _addr);
@ -39,27 +62,16 @@ public:
private: private:
Memory& m_memoryMan; Memory& m_memoryMan;
llvm::Value* m_args[2];
llvm::Value* m_arg2;
llvm::Value* m_arg3;
llvm::Value* m_arg4;
llvm::Value* m_arg5;
llvm::Value* m_arg6;
llvm::Value* m_arg7;
llvm::Value* m_arg8;
llvm::Value* m_size; llvm::Value* m_size;
llvm::Value* m_data = nullptr; llvm::Value* m_data = nullptr;
llvm::Function* m_sload;
llvm::Function* m_sstore; std::array<llvm::Function*, sizeOf<EnvFunc>::value> m_funcs;
llvm::Function* m_calldataload; std::array<llvm::Value*, 8> m_argAllocas;
llvm::Function* m_balance = nullptr; size_t m_argCounter = 0;
llvm::Function* m_create;
llvm::Function* m_call; llvm::CallInst* createCall(EnvFunc _funcId, std::initializer_list<llvm::Value*> const& _args);
llvm::Function* m_sha3; llvm::Value* getArgAlloca();
llvm::Function* m_getExtCode; llvm::Value* byPtr(llvm::Value* _value);
llvm::Function* m_log;
llvm::Function* getBalanceFunc();
}; };

6
evmjit/libevmjit/GasMeter.cpp

@ -41,7 +41,7 @@ uint64_t const c_logDataGas = 1;
uint64_t const c_logTopicGas = 32; uint64_t const c_logTopicGas = 32;
uint64_t const c_copyGas = 1; uint64_t const c_copyGas = 1;
uint64_t getStepCost(Instruction inst) // TODO: Add this function to FeeSructure (pull request submitted) uint64_t getStepCost(Instruction inst)
{ {
switch (inst) switch (inst)
{ {
@ -119,7 +119,7 @@ void GasMeter::count(Instruction _inst)
if (!m_checkCall) if (!m_checkCall)
{ {
// Create gas check call with mocked block cost at begining of current cost-block // Create gas check call with mocked block cost at begining of current cost-block
m_checkCall = createCall(m_gasCheckFunc, m_runtimeManager.getRuntimePtr(), llvm::UndefValue::get(Type::Word)); m_checkCall = createCall(m_gasCheckFunc, {m_runtimeManager.getRuntimePtr(), llvm::UndefValue::get(Type::Word)});
} }
m_blockCost += getStepCost(_inst); m_blockCost += getStepCost(_inst);
@ -127,7 +127,7 @@ void GasMeter::count(Instruction _inst)
void GasMeter::count(llvm::Value* _cost) void GasMeter::count(llvm::Value* _cost)
{ {
createCall(m_gasCheckFunc, m_runtimeManager.getRuntimePtr(), _cost); createCall(m_gasCheckFunc, {m_runtimeManager.getRuntimePtr(), _cost});
} }
void GasMeter::countExp(llvm::Value* _exponent) void GasMeter::countExp(llvm::Value* _exponent)

40
evmjit/libevmjit/Instruction.cpp

@ -0,0 +1,40 @@
#include "Instruction.h"
#include <llvm/ADT/APInt.h>
namespace dev
{
namespace eth
{
namespace jit
{
llvm::APInt readPushData(bytes::const_iterator& _curr, bytes::const_iterator _end)
{
auto pushInst = *_curr;
assert(Instruction(pushInst) >= Instruction::PUSH1 && Instruction(pushInst) <= Instruction::PUSH32);
auto numBytes = pushInst - static_cast<size_t>(Instruction::PUSH1) + 1;
llvm::APInt value(256, 0);
++_curr; // Point the data
for (decltype(numBytes) i = 0; i < numBytes; ++i)
{
byte b = (_curr != _end) ? *_curr++ : 0;
value <<= 8;
value |= b;
}
--_curr; // Point the last real byte read
return value;
}
void skipPushData(bytes::const_iterator& _curr, bytes::const_iterator _end)
{
auto pushInst = *_curr;
assert(Instruction(pushInst) >= Instruction::PUSH1 && Instruction(pushInst) <= Instruction::PUSH32);
auto numBytes = pushInst - static_cast<size_t>(Instruction::PUSH1) + 1;
--_end;
for (decltype(numBytes) i = 0; i < numBytes && _curr < _end; ++i, ++_curr) {}
}
}
}
}

8
evmjit/libevmjit/Instruction.h

@ -58,7 +58,7 @@ enum class Instruction: uint8_t
EXTCODESIZE, ///< get external code size (from another contract) EXTCODESIZE, ///< get external code size (from another contract)
EXTCODECOPY, ///< copy external code (from another contract) EXTCODECOPY, ///< copy external code (from another contract)
PREVHASH = 0x40, ///< get hash of most recent complete block BLOCKHASH = 0x40, ///< get hash of most recent complete block
COINBASE, ///< get the block's coinbase address COINBASE, ///< get the block's coinbase address
TIMESTAMP, ///< get the block's timestamp TIMESTAMP, ///< get the block's timestamp
NUMBER, ///< get the block's number NUMBER, ///< get the block's number
@ -160,9 +160,13 @@ enum class Instruction: uint8_t
/// Reads PUSH data from pointed fragment of bytecode and constructs number out of it /// Reads PUSH data from pointed fragment of bytecode and constructs number out of it
/// Reading out of bytecode means reading 0 /// Reading out of bytecode means reading 0
/// @param _curr is updates and points the last real byte read /// @param _curr is updated and points the last real byte read
llvm::APInt readPushData(bytes::const_iterator& _curr, bytes::const_iterator _end); llvm::APInt readPushData(bytes::const_iterator& _curr, bytes::const_iterator _end);
/// Skips PUSH data in pointed fragment of bytecode.
/// @param _curr is updated and points the last real byte skipped
void skipPushData(bytes::const_iterator& _curr, bytes::const_iterator _end);
#define ANY_PUSH PUSH1: \ #define ANY_PUSH PUSH1: \
case Instruction::PUSH2: \ case Instruction::PUSH2: \
case Instruction::PUSH3: \ case Instruction::PUSH3: \

8
evmjit/libevmjit/Memory.cpp

@ -146,18 +146,18 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMet
llvm::Value* Memory::loadWord(llvm::Value* _addr) llvm::Value* Memory::loadWord(llvm::Value* _addr)
{ {
return createCall(m_loadWord, getRuntimeManager().getRuntimePtr(), _addr); return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr});
} }
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word)
{ {
createCall(m_storeWord, getRuntimeManager().getRuntimePtr(), _addr, _word); createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word});
} }
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word)
{ {
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte");
createCall(m_storeByte, getRuntimeManager().getRuntimePtr(), _addr, byte); createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte});
} }
llvm::Value* Memory::getData() llvm::Value* Memory::getData()
@ -181,7 +181,7 @@ llvm::Value* Memory::getBytePtr(llvm::Value* _index)
void Memory::require(llvm::Value* _offset, llvm::Value* _size) void Memory::require(llvm::Value* _offset, llvm::Value* _size)
{ {
createCall(m_require, getRuntimeManager().getRuntimePtr(), _offset, _size); createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size});
} }
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx,

23
evmjit/libevmjit/Runtime.cpp

@ -11,29 +11,12 @@ namespace eth
{ {
namespace jit namespace jit
{ {
namespace
{
jmp_buf_ref g_currJmpBuf;
}
jmp_buf_ref Runtime::getCurrJmpBuf()
{
return g_currJmpBuf;
}
Runtime::Runtime(RuntimeData* _data, Env* _env): Runtime::Runtime(RuntimeData* _data, Env* _env) :
m_data(*_data), m_data(*_data),
m_env(*_env), m_env(*_env),
m_currJmpBuf(m_jmpBuf), m_currJmpBuf(m_jmpBuf)
m_prevJmpBuf(g_currJmpBuf) {}
{
g_currJmpBuf = m_jmpBuf;
}
Runtime::~Runtime()
{
g_currJmpBuf = m_prevJmpBuf;
}
bytes Runtime::getReturnData() const // FIXME: Reconsider returning by copy bytes Runtime::getReturnData() const // FIXME: Reconsider returning by copy
{ {

5
evmjit/libevmjit/Runtime.h

@ -32,7 +32,6 @@ class Runtime
{ {
public: public:
Runtime(RuntimeData* _data, Env* _env); Runtime(RuntimeData* _data, Env* _env);
~Runtime();
Runtime(const Runtime&) = delete; Runtime(const Runtime&) = delete;
void operator=(const Runtime&) = delete; void operator=(const Runtime&) = delete;
@ -43,15 +42,13 @@ public:
bytes getReturnData() const; bytes getReturnData() const;
jmp_buf_ref getJmpBuf() { return m_jmpBuf; } jmp_buf_ref getJmpBuf() { return m_jmpBuf; }
static jmp_buf_ref getCurrJmpBuf();
private: private:
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract. RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract. Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
jmp_buf_ref m_currJmpBuf; ///< Pointer to jump buffer. Expected by compiled contract. jmp_buf_ref m_currJmpBuf; ///< Pointer to jump buffer. Expected by compiled contract.
byte* m_memoryData = nullptr; byte* m_memoryData = nullptr;
i256 m_memorySize = {}; i256 m_memorySize;
jmp_buf_ref m_prevJmpBuf;
std::jmp_buf m_jmpBuf; std::jmp_buf m_jmpBuf;
StackImpl m_stack; StackImpl m_stack;
MemoryImpl m_memory; MemoryImpl m_memory;

1
evmjit/libevmjit/RuntimeData.h

@ -21,7 +21,6 @@ struct RuntimeData
CallValue, CallValue,
CallDataSize, CallDataSize,
GasPrice, GasPrice,
PrevHash,
CoinBase, CoinBase,
TimeStamp, TimeStamp,
Number, Number,

2
evmjit/libevmjit/RuntimeManager.cpp

@ -63,7 +63,6 @@ llvm::Twine getName(RuntimeData::Index _index)
case RuntimeData::CallValue: return "callvalue"; case RuntimeData::CallValue: return "callvalue";
case RuntimeData::CallDataSize: return "calldatasize"; case RuntimeData::CallDataSize: return "calldatasize";
case RuntimeData::GasPrice: return "gasprice"; case RuntimeData::GasPrice: return "gasprice";
case RuntimeData::PrevHash: return "prevhash";
case RuntimeData::CoinBase: return "coinbase"; case RuntimeData::CoinBase: return "coinbase";
case RuntimeData::TimeStamp: return "timestamp"; case RuntimeData::TimeStamp: return "timestamp";
case RuntimeData::Number: return "number"; case RuntimeData::Number: return "number";
@ -154,7 +153,6 @@ llvm::Value* RuntimeManager::get(Instruction _inst)
case Instruction::CALLVALUE: return get(RuntimeData::CallValue); case Instruction::CALLVALUE: return get(RuntimeData::CallValue);
case Instruction::CALLDATASIZE: return get(RuntimeData::CallDataSize); case Instruction::CALLDATASIZE: return get(RuntimeData::CallDataSize);
case Instruction::GASPRICE: return get(RuntimeData::GasPrice); case Instruction::GASPRICE: return get(RuntimeData::GasPrice);
case Instruction::PREVHASH: return get(RuntimeData::PrevHash);
case Instruction::COINBASE: return get(RuntimeData::CoinBase); case Instruction::COINBASE: return get(RuntimeData::CoinBase);
case Instruction::TIMESTAMP: return get(RuntimeData::TimeStamp); case Instruction::TIMESTAMP: return get(RuntimeData::TimeStamp);
case Instruction::NUMBER: return get(RuntimeData::Number); case Instruction::NUMBER: return get(RuntimeData::Number);

26
evmjit/libevmjit/Utils.cpp

@ -1,8 +1,5 @@
#include <csetjmp>
#include "Utils.h" #include "Utils.h"
#include "Instruction.h"
#include "Runtime.h"
namespace dev namespace dev
{ {
@ -38,29 +35,6 @@ i256 eth2llvm(u256 _u)
return i; return i;
} }
llvm::APInt readPushData(bytes::const_iterator& _curr, bytes::const_iterator _end)
{
auto pushInst = *_curr;
assert(Instruction(pushInst) >= Instruction::PUSH1 && Instruction(pushInst) <= Instruction::PUSH32);
auto numBytes = pushInst - static_cast<size_t>(Instruction::PUSH1) + 1;
llvm::APInt value(256, 0);
++_curr; // Point the data
for (decltype(numBytes) i = 0; i < numBytes; ++i)
{
byte b = (_curr != _end) ? *_curr++ : 0;
value <<= 8;
value |= b;
}
--_curr; // Point the last real byte read
return value;
}
void terminate(ReturnCode _returnCode)
{
auto jmpBuf = Runtime::getCurrJmpBuf();
std::longjmp(jmpBuf, static_cast<int>(_returnCode));
}
} }
} }
} }

2
evmjit/libevmjit/Utils.h

@ -17,8 +17,6 @@ struct JIT: public NoteChannel { static const char* name() { return "JIT"; } };
u256 llvm2eth(i256); u256 llvm2eth(i256);
i256 eth2llvm(u256); i256 eth2llvm(u256);
void terminate(ReturnCode _returnCode);
} }
} }
} }

3
libdevcore/Common.h

@ -80,6 +80,9 @@ using StringMap = std::map<std::string, std::string>;
using u256Map = std::map<u256, u256>; using u256Map = std::map<u256, u256>;
using HexMap = std::map<bytes, std::string>; using HexMap = std::map<bytes, std::string>;
// String types.
using strings = std::vector<std::string>;
// Fixed-length string types. // Fixed-length string types.
using string32 = std::array<char, 32>; using string32 = std::array<char, 32>;

2
libdevcore/Exceptions.h

@ -22,6 +22,7 @@
#pragma once #pragma once
#include <exception> #include <exception>
#include <string>
#include <boost/exception/all.hpp> #include <boost/exception/all.hpp>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include "CommonData.h" #include "CommonData.h"
@ -40,6 +41,7 @@ struct NoNetworking: virtual Exception {};
struct NoUPnPDevice: virtual Exception {}; struct NoUPnPDevice: virtual Exception {};
struct RootNotFound: virtual Exception {}; struct RootNotFound: virtual Exception {};
struct FileError: virtual Exception {}; struct FileError: virtual Exception {};
struct InterfaceNotSupported: virtual Exception { public: InterfaceNotSupported(std::string _f): m_f("Interface " + _f + " not supported.") {} virtual const char* what() const noexcept { return m_f.c_str(); } private: std::string m_f; };
// error information to be added to exceptions // error information to be added to exceptions
typedef boost::error_info<struct tag_invalidSymbol, char> errinfo_invalidSymbol; typedef boost::error_info<struct tag_invalidSymbol, char> errinfo_invalidSymbol;

2
libdevcore/vector_ref.h

@ -28,7 +28,7 @@ public:
bool contentsEqual(std::vector<mutable_value_type> const& _c) const { return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count); } bool contentsEqual(std::vector<mutable_value_type> const& _c) const { return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count); }
std::vector<mutable_value_type> toVector() const { return std::vector<mutable_value_type>(m_data, m_data + m_count); } std::vector<mutable_value_type> toVector() const { return std::vector<mutable_value_type>(m_data, m_data + m_count); }
std::vector<unsigned char> toBytes() const { return std::vector<unsigned char>((unsigned char const*)m_data, m_data + m_count * sizeof(_T)); } std::vector<unsigned char> toBytes() const { return std::vector<unsigned char>((unsigned char const*)m_data, (unsigned char const*)m_data + m_count * sizeof(_T)); }
std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count * sizeof(_T)); } std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count * sizeof(_T)); }
template <class _T2> operator vector_ref<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return vector_ref<_T2>((_T2*)m_data, m_count * sizeof(_T) / sizeof(_T2)); } template <class _T2> operator vector_ref<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return vector_ref<_T2>((_T2*)m_data, m_count * sizeof(_T) / sizeof(_T2)); }

3
libdevcrypto/Common.cpp

@ -23,6 +23,7 @@
#include <random> #include <random>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <libdevcore/Guards.h>
#include "SHA3.h" #include "SHA3.h"
#include "FileSystem.h" #include "FileSystem.h"
#include "CryptoPP.h" #include "CryptoPP.h"
@ -139,7 +140,7 @@ h256 Nonce::get(bool _commit)
static h256 s_seed; static h256 s_seed;
static string s_seedFile(getDataDir() + "/seed"); static string s_seedFile(getDataDir() + "/seed");
static mutex s_x; static mutex s_x;
lock_guard<mutex> l(s_x); Guard l(s_x);
if (!s_seed) if (!s_seed)
{ {
static Nonce s_nonce; static Nonce s_nonce;

17
libdevcrypto/CryptoPP.cpp

@ -20,6 +20,7 @@
*/ */
#include "CryptoPP.h" #include "CryptoPP.h"
#include <libdevcore/Guards.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
@ -40,7 +41,7 @@ void Secp256k1::encrypt(Public const& _k, bytes& io_cipher)
ciphertext.resize(e.CiphertextLength(plen)); ciphertext.resize(e.CiphertextLength(plen));
{ {
lock_guard<mutex> l(x_rng); Guard l(x_rng);
e.Encrypt(m_rng, io_cipher.data(), plen, ciphertext.data()); e.Encrypt(m_rng, io_cipher.data(), plen, ciphertext.data());
} }
@ -65,7 +66,7 @@ void Secp256k1::decrypt(Secret const& _k, bytes& io_text)
DecodingResult r; DecodingResult r;
{ {
lock_guard<mutex> l(x_rng); Guard l(x_rng);
r = d.Decrypt(m_rng, io_text.data(), clen, plain.data()); r = d.Decrypt(m_rng, io_text.data(), clen, plain.data());
} }
@ -99,7 +100,7 @@ Signature Secp256k1::sign(Secret const& _key, h256 const& _hash)
ECP::Point rp; ECP::Point rp;
Integer r; Integer r;
{ {
lock_guard<mutex> l(x_params); Guard l(x_params);
rp = m_params.ExponentiateBase(k); rp = m_params.ExponentiateBase(k);
r = m_params.ConvertElementToInteger(rp); r = m_params.ConvertElementToInteger(rp);
} }
@ -149,7 +150,7 @@ Public Secp256k1::recover(Signature _signature, bytesConstRef _message)
ECP::Element x; ECP::Element x;
{ {
lock_guard<mutex> l(x_curve); Guard l(x_curve);
m_curve.DecodePoint(x, encodedpoint, 33); m_curve.DecodePoint(x, encodedpoint, 33);
if (!m_curve.VerifyPoint(x)) if (!m_curve.VerifyPoint(x))
return recovered; return recovered;
@ -158,7 +159,7 @@ Public Secp256k1::recover(Signature _signature, bytesConstRef _message)
// if (_signature[64] & 2) // if (_signature[64] & 2)
// { // {
// r += m_q; // r += m_q;
// lock_guard<mutex> l(x_params); // Guard l(x_params);
// if (r >= m_params.GetMaxExponent()) // if (r >= m_params.GetMaxExponent())
// return recovered; // return recovered;
// } // }
@ -171,7 +172,7 @@ Public Secp256k1::recover(Signature _signature, bytesConstRef _message)
ECP::Point p; ECP::Point p;
byte recoveredbytes[65]; byte recoveredbytes[65];
{ {
lock_guard<mutex> l(x_curve); Guard l(x_curve);
// todo: make generator member // todo: make generator member
p = m_curve.CascadeMultiply(u2, x, u1, m_params.GetSubgroupGenerator()); p = m_curve.CascadeMultiply(u2, x, u1, m_params.GetSubgroupGenerator());
m_curve.EncodePoint(recoveredbytes, p, false); m_curve.EncodePoint(recoveredbytes, p, false);
@ -210,7 +211,7 @@ void Secp256k1::exportPublicKey(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const&
bytes prefixedKey(_k.GetGroupParameters().GetEncodedElementSize(true)); bytes prefixedKey(_k.GetGroupParameters().GetEncodedElementSize(true));
{ {
lock_guard<mutex> l(x_params); Guard l(x_params);
m_params.GetCurve().EncodePoint(prefixedKey.data(), _k.GetPublicElement(), false); m_params.GetCurve().EncodePoint(prefixedKey.data(), _k.GetPublicElement(), false);
assert(Public::size + 1 == _k.GetGroupParameters().GetEncodedElementSize(true)); assert(Public::size + 1 == _k.GetGroupParameters().GetEncodedElementSize(true));
} }
@ -223,7 +224,7 @@ void Secp256k1::exponentToPublic(Integer const& _e, Public& o_p)
CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> pk; CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> pk;
{ {
lock_guard<mutex> l(x_params); Guard l(x_params);
pk.Initialize(m_params, m_params.ExponentiateBase(_e)); pk.Initialize(m_params, m_params.ExponentiateBase(_e));
} }

129
libethereum/Client.cpp

@ -159,9 +159,10 @@ void Client::clearPending()
WriteGuard l(x_stateDB); WriteGuard l(x_stateDB);
if (!m_postMine.pending().size()) if (!m_postMine.pending().size())
return; return;
for (unsigned i = 0; i < m_postMine.pending().size(); ++i) // for (unsigned i = 0; i < m_postMine.pending().size(); ++i)
appendFromNewPending(m_postMine.logBloom(i), changeds); // appendFromNewPending(m_postMine.logBloom(i), changeds);
changeds.insert(PendingChangedFilter); changeds.insert(PendingChangedFilter);
m_tq.clear();
m_postMine = m_preMine; m_postMine = m_preMine;
} }
@ -176,21 +177,29 @@ void Client::clearPending()
unsigned Client::installWatch(h256 _h) unsigned Client::installWatch(h256 _h)
{ {
auto ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0; unsigned ret;
m_watches[ret] = ClientWatch(_h); {
cwatch << "+++" << ret << _h; Guard l(m_filterLock);
ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0;
m_watches[ret] = ClientWatch(_h);
cwatch << "+++" << ret << _h;
}
auto ch = logs(ret);
{
Guard l(m_filterLock);
swap(m_watches[ret].changes, ch);
}
return ret; return ret;
} }
unsigned Client::installWatch(LogFilter const& _f) unsigned Client::installWatch(LogFilter const& _f)
{ {
lock_guard<mutex> l(m_filterLock);
h256 h = _f.sha3(); h256 h = _f.sha3();
{
if (!m_filters.count(h)) Guard l(m_filterLock);
m_filters.insert(make_pair(h, _f)); if (!m_filters.count(h))
m_filters.insert(make_pair(h, _f));
}
return installWatch(h); return installWatch(h);
} }
@ -198,7 +207,7 @@ void Client::uninstallWatch(unsigned _i)
{ {
cwatch << "XXX" << _i; cwatch << "XXX" << _i;
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
auto it = m_watches.find(_i); auto it = m_watches.find(_i);
if (it == m_watches.end()) if (it == m_watches.end())
@ -214,33 +223,82 @@ void Client::uninstallWatch(unsigned _i)
void Client::noteChanged(h256Set const& _filters) void Client::noteChanged(h256Set const& _filters)
{ {
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
// accrue all changes left in each filter into the watches.
for (auto& i: m_watches) for (auto& i: m_watches)
if (_filters.count(i.second.id)) if (_filters.count(i.second.id))
{ {
// cwatch << "!!!" << i.first << i.second.id; // cwatch << "!!!" << i.first << i.second.id;
i.second.changes++; try {
i.second.changes += m_filters.at(i.second.id).changes;
} catch(...){}
} }
// clear the filters now.
for (auto& i: m_filters)
i.second.changes.clear();
} }
void Client::appendFromNewPending(LogBloom _bloom, h256Set& o_changed) const LocalisedLogEntries Client::peekWatch(unsigned _watchId) const
{ {
// TODO: more precise check on whether the txs match. Guard l(m_filterLock);
lock_guard<mutex> l(m_filterLock);
for (pair<h256, InstalledFilter> const& i: m_filters) try {
if ((unsigned)i.second.filter.latest() > m_bc.number() && i.second.filter.matches(_bloom)) return m_watches.at(_watchId).changes;
o_changed.insert(i.first); } catch (...) {}
return LocalisedLogEntries();
}
LocalisedLogEntries Client::checkWatch(unsigned _watchId)
{
Guard l(m_filterLock);
LocalisedLogEntries ret;
try {
std::swap(ret, m_watches.at(_watchId).changes);
} catch (...) {}
return ret;
} }
void Client::appendFromNewBlock(h256 _block, h256Set& o_changed) const void Client::appendFromNewPending(TransactionReceipt const& _receipt, h256Set& io_changed)
{
Guard l(m_filterLock);
for (pair<h256 const, InstalledFilter>& i: m_filters)
if ((unsigned)i.second.filter.latest() > m_bc.number())
{
// acceptable number.
auto m = i.second.filter.matches(_receipt);
if (m.size())
{
// filter catches them
for (LogEntry const& l: m)
i.second.changes.push_back(LocalisedLogEntry(l, m_bc.number() + 1));
io_changed.insert(i.first);
}
}
}
void Client::appendFromNewBlock(h256 const& _block, h256Set& io_changed)
{ {
// TODO: more precise check on whether the txs match. // TODO: more precise check on whether the txs match.
auto d = m_bc.info(_block); auto d = m_bc.info(_block);
auto br = m_bc.receipts(_block);
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
for (pair<h256, InstalledFilter> const& i: m_filters) for (pair<h256 const, InstalledFilter>& i: m_filters)
if ((unsigned)i.second.filter.latest() >= d.number && (unsigned)i.second.filter.earliest() <= d.number && i.second.filter.matches(d.logBloom)) if ((unsigned)i.second.filter.latest() >= d.number && (unsigned)i.second.filter.earliest() <= d.number && i.second.filter.matches(d.logBloom))
o_changed.insert(i.first); // acceptable number & looks like block may contain a matching log entry.
for (TransactionReceipt const& tr: br.receipts)
{
auto m = i.second.filter.matches(tr);
if (m.size())
{
// filter catches them
for (LogEntry const& l: m)
i.second.changes.push_back(LocalisedLogEntry(l, (unsigned)d.number));
io_changed.insert(i.first);
}
}
} }
void Client::setForceMining(bool _enable) void Client::setForceMining(bool _enable)
@ -467,10 +525,10 @@ void Client::doWork()
// returns h256s as blooms, once for each transaction. // returns h256s as blooms, once for each transaction.
cwork << "postSTATE <== TQ"; cwork << "postSTATE <== TQ";
h512s newPendingBlooms = m_postMine.sync(m_bc, m_tq); TransactionReceipts newPendingReceipts = m_postMine.sync(m_bc, m_tq);
if (newPendingBlooms.size()) if (newPendingReceipts.size())
{ {
for (auto i: newPendingBlooms) for (auto i: newPendingReceipts)
appendFromNewPending(i, changeds); appendFromNewPending(i, changeds);
changeds.insert(PendingChangedFilter); changeds.insert(PendingChangedFilter);
@ -534,7 +592,7 @@ eth::State Client::state(unsigned _txi) const
StateDiff Client::diff(unsigned _txi, int _block) 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)); return st.fromPending(_txi).diff(st.fromPending(_txi + 1));
} }
@ -591,16 +649,16 @@ BlockInfo Client::uncle(h256 _blockHash, unsigned _i) const
return BlockInfo::fromHeader(b[2][_i].data()); return BlockInfo::fromHeader(b[2][_i].data());
} }
LogEntries Client::logs(LogFilter const& _f) const LocalisedLogEntries Client::logs(LogFilter const& _f) const
{ {
LogEntries ret; LocalisedLogEntries ret;
unsigned begin = min<unsigned>(m_bc.number(), (unsigned)_f.latest()); unsigned begin = min<unsigned>(m_bc.number() + 1, (unsigned)_f.latest());
unsigned end = min(begin, (unsigned)_f.earliest()); unsigned end = min(m_bc.number(), min(begin, (unsigned)_f.earliest()));
unsigned m = _f.max(); unsigned m = _f.max();
unsigned s = _f.skip(); unsigned s = _f.skip();
// Handle pending transactions differently as they're not on the block chain. // Handle pending transactions differently as they're not on the block chain.
if (begin == m_bc.number()) if (begin > m_bc.number())
{ {
ReadGuard l(x_stateDB); ReadGuard l(x_stateDB);
for (unsigned i = 0; i < m_postMine.pending().size(); ++i) for (unsigned i = 0; i < m_postMine.pending().size(); ++i)
@ -614,9 +672,10 @@ LogEntries Client::logs(LogFilter const& _f) const
if (s) if (s)
s--; s--;
else else
ret.insert(ret.begin(), le[j]); ret.insert(ret.begin(), LocalisedLogEntry(le[j], begin));
} }
} }
begin = m_bc.number();
} }
#if ETH_DEBUG #if ETH_DEBUG
@ -648,7 +707,7 @@ LogEntries Client::logs(LogFilter const& _f) const
if (s) if (s)
s--; s--;
else else
ret.insert(ret.begin(), le[j]); ret.insert(ret.begin(), LocalisedLogEntry(le[j], n));
} }
} }
} }

37
libethereum/Client.h

@ -57,12 +57,6 @@ enum ClientWorkState
Deleted Deleted
}; };
enum class NodeMode
{
PeerServer,
Full
};
class VersionChecker class VersionChecker
{ {
public: public:
@ -84,6 +78,7 @@ struct InstalledFilter
LogFilter filter; LogFilter filter;
unsigned refCount = 1; unsigned refCount = 1;
LocalisedLogEntries changes;
}; };
static const h256 PendingChangedFilter = u256(0); static const h256 PendingChangedFilter = u256(0);
@ -95,7 +90,7 @@ struct ClientWatch
explicit ClientWatch(h256 _id): id(_id) {} explicit ClientWatch(h256 _id): id(_id) {}
h256 id; h256 id;
unsigned changes = 1; LocalisedLogEntries changes;
}; };
struct WatchChannel: public LogChannel { static const char* name() { return "(o)"; } static const int verbosity = 7; }; struct WatchChannel: public LogChannel { static const char* name() { return "(o)"; } static const int verbosity = 7; };
@ -108,9 +103,9 @@ struct WorkChannel: public LogChannel { static const char* name() { return "-W-"
#define cworkout dev::LogOutputStream<dev::eth::WorkOutChannel, true>() #define cworkout dev::LogOutputStream<dev::eth::WorkOutChannel, true>()
template <class T> struct ABISerialiser {}; template <class T> struct ABISerialiser {};
template <unsigned N> struct ABISerialiser<FixedHash<N>> { static bytes serialise(FixedHash<N> const& _t) { return _t.asBytes(); } }; template <unsigned N> struct ABISerialiser<FixedHash<N>> { static bytes serialise(FixedHash<N> const& _t) { static_assert(N <= 32, "Cannot serialise hash > 32 bytes."); static_assert(N > 0, "Cannot serialise zero-length hash."); return bytes(32 - N, 0) + _t.asBytes(); } };
template <> struct ABISerialiser<u256> { static bytes serialise(u256 const& _t) { return h256(_t).asBytes(); } }; template <> struct ABISerialiser<u256> { static bytes serialise(u256 const& _t) { return h256(_t).asBytes(); } };
template <> struct ABISerialiser<u160> { static bytes serialise(u160 const& _t) { return h160(_t).asBytes(); } }; template <> struct ABISerialiser<u160> { static bytes serialise(u160 const& _t) { return bytes(12, 0) + h160(_t).asBytes(); } };
template <> struct ABISerialiser<string32> { static bytes serialise(string32 const& _t) { return bytesConstRef((byte const*)_t.data(), 32).toBytes(); } }; template <> struct ABISerialiser<string32> { static bytes serialise(string32 const& _t) { return bytesConstRef((byte const*)_t.data(), 32).toBytes(); } };
inline bytes abiInAux() { return {}; } inline bytes abiInAux() { return {}; }
@ -119,15 +114,15 @@ template <class T, class ... U> bytes abiInAux(T const& _t, U const& ... _u)
return ABISerialiser<T>::serialise(_t) + abiInAux(_u ...); return ABISerialiser<T>::serialise(_t) + abiInAux(_u ...);
} }
template <class ... T> bytes abiIn(byte _id, T const& ... _t) template <class ... T> bytes abiIn(std::string _id, T const& ... _t)
{ {
return bytes(1, _id) + abiInAux(_t ...); return sha3(_id).ref().cropped(0, 4).toBytes() + abiInAux(_t ...);
} }
template <class T> struct ABIDeserialiser {}; template <class T> struct ABIDeserialiser {};
template <unsigned N> struct ABIDeserialiser<FixedHash<N>> { static FixedHash<N> deserialise(bytesConstRef& io_t) { FixedHash<N> ret; io_t.cropped(0, N).populate(ret.ref()); return ret; } }; template <unsigned N> struct ABIDeserialiser<FixedHash<N>> { static FixedHash<N> deserialise(bytesConstRef& io_t) { static_assert(N <= 32, "Parameter sizes must be at most 32 bytes."); FixedHash<N> ret; io_t.cropped(32 - N, N).populate(ret.ref()); io_t = io_t.cropped(32); return ret; } };
template <> struct ABIDeserialiser<u256> { static u256 deserialise(bytesConstRef& io_t) { u256 ret = fromBigEndian<u256>(io_t.cropped(0, 32)); io_t = io_t.cropped(32); return ret; } }; template <> struct ABIDeserialiser<u256> { static u256 deserialise(bytesConstRef& io_t) { u256 ret = fromBigEndian<u256>(io_t.cropped(0, 32)); io_t = io_t.cropped(32); return ret; } };
template <> struct ABIDeserialiser<u160> { static u256 deserialise(bytesConstRef& io_t) { u160 ret = fromBigEndian<u160>(io_t.cropped(0, 20)); io_t = io_t.cropped(20); return ret; } }; template <> struct ABIDeserialiser<u160> { static u160 deserialise(bytesConstRef& io_t) { u160 ret = fromBigEndian<u160>(io_t.cropped(12, 20)); io_t = io_t.cropped(32); return ret; } };
template <> struct ABIDeserialiser<string32> { static string32 deserialise(bytesConstRef& io_t) { string32 ret; io_t.cropped(0, 32).populate(vector_ref<char>(ret.data(), 32)); io_t = io_t.cropped(32); return ret; } }; template <> struct ABIDeserialiser<string32> { static string32 deserialise(bytesConstRef& io_t) { string32 ret; io_t.cropped(0, 32).populate(vector_ref<char>(ret.data(), 32)); io_t = io_t.cropped(32); return ret; } };
template <class T> T abiOut(bytes const& _data) template <class T> T abiOut(bytes const& _data)
@ -188,11 +183,11 @@ public:
virtual unsigned installWatch(LogFilter const& _filter); virtual unsigned installWatch(LogFilter const& _filter);
virtual unsigned installWatch(h256 _filterId); virtual unsigned installWatch(h256 _filterId);
virtual void uninstallWatch(unsigned _watchId); virtual void uninstallWatch(unsigned _watchId);
virtual bool peekWatch(unsigned _watchId) const { std::lock_guard<std::mutex> l(m_filterLock); try { return m_watches.at(_watchId).changes != 0; } catch (...) { return false; } } virtual LocalisedLogEntries peekWatch(unsigned _watchId) const;
virtual bool checkWatch(unsigned _watchId) { std::lock_guard<std::mutex> l(m_filterLock); bool ret = false; try { ret = m_watches.at(_watchId).changes != 0; m_watches.at(_watchId).changes = 0; } catch (...) {} return ret; } virtual LocalisedLogEntries checkWatch(unsigned _watchId);
virtual LogEntries logs(unsigned _watchId) const { try { std::lock_guard<std::mutex> l(m_filterLock); return logs(m_filters.at(m_watches.at(_watchId).id).filter); } catch (...) { return LogEntries(); } } virtual LocalisedLogEntries logs(unsigned _watchId) const { try { Guard l(m_filterLock); return logs(m_filters.at(m_watches.at(_watchId).id).filter); } catch (...) { return LocalisedLogEntries(); } }
virtual LogEntries logs(LogFilter const& _filter) const; virtual LocalisedLogEntries logs(LogFilter const& _filter) const;
// [EXTRA API]: // [EXTRA API]:
@ -292,11 +287,11 @@ private:
/// Collate the changed filters for the bloom filter of the given pending transaction. /// Collate the changed filters for the bloom filter of the given pending transaction.
/// Insert any filters that are activated into @a o_changed. /// Insert any filters that are activated into @a o_changed.
void appendFromNewPending(LogBloom _pendingTransactionBloom, h256Set& o_changed) const; void appendFromNewPending(TransactionReceipt const& _receipt, h256Set& io_changed);
/// Collate the changed filters for the hash of the given block. /// Collate the changed filters for the hash of the given block.
/// Insert any filters that are activated into @a o_changed. /// Insert any filters that are activated into @a o_changed.
void appendFromNewBlock(h256 _blockHash, h256Set& o_changed) const; void appendFromNewBlock(h256 const& _blockHash, h256Set& io_changed);
/// Record that the set of filters @a _filters have changed. /// Record that the set of filters @a _filters have changed.
/// This doesn't actually make any callbacks, but incrememnts some counters in m_watches. /// This doesn't actually make any callbacks, but incrememnts some counters in m_watches.
@ -313,7 +308,7 @@ private:
TransactionQueue m_tq; ///< Maintains a list of incoming transactions not yet in a block on the blockchain. TransactionQueue m_tq; ///< Maintains a list of incoming transactions not yet in a block on the blockchain.
BlockQueue m_bq; ///< Maintains a list of incoming blocks not yet on the blockchain (to be imported). BlockQueue m_bq; ///< Maintains a list of incoming blocks not yet on the blockchain (to be imported).
mutable boost::shared_mutex x_stateDB; ///< Lock on the state DB, effectively a lock on m_postMine. mutable SharedMutex x_stateDB; ///< Lock on the state DB, effectively a lock on m_postMine.
OverlayDB m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it. OverlayDB m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it.
State m_preMine; ///< The present state of the client. State m_preMine; ///< The present state of the client.
State m_postMine; ///< The state of the client which we're mining (i.e. it'll have all the rewards added). State m_postMine; ///< The state of the client which we're mining (i.e. it'll have all the rewards added).
@ -321,7 +316,7 @@ private:
std::weak_ptr<EthereumHost> m_host; ///< Our Ethereum Host. Don't do anything if we can't lock. std::weak_ptr<EthereumHost> m_host; ///< Our Ethereum Host. Don't do anything if we can't lock.
std::vector<Miner> m_miners; std::vector<Miner> m_miners;
mutable boost::shared_mutex x_miners; mutable SharedMutex x_miners;
bool m_paranoia = false; ///< Should we be paranoid about our state? bool m_paranoia = false; ///< Should we be paranoid about our state?
bool m_turboMining = false; ///< Don't squander all of our time mining actually just sleeping. bool m_turboMining = false; ///< Don't squander all of our time mining actually just sleeping.
bool m_forceMining = false; ///< Mine even when there are no transactions pending? bool m_forceMining = false; ///< Mine even when there are no transactions pending?

2
libethereum/EthereumPeer.cpp

@ -317,6 +317,8 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r)
disable("Blacklisted client version."); disable("Blacklisted client version.");
else if (host()->isBanned(session()->id())) else if (host()->isBanned(session()->id()))
disable("Peer banned for previous bad behaviour."); disable("Peer banned for previous bad behaviour.");
else
transition(Asking::Nothing);
break; break;
} }
case GetTransactionsPacket: break; // DEPRECATED. case GetTransactionsPacket: break; // DEPRECATED.

14
libethereum/Executive.cpp

@ -146,9 +146,17 @@ bool Executive::create(Address _sender, u256 _endowment, u256 _gasPrice, u256 _g
m_s.m_cache[m_newAddress] = Account(m_s.balance(m_newAddress) + _endowment, Account::ContractConception); m_s.m_cache[m_newAddress] = Account(m_s.balance(m_newAddress) + _endowment, Account::ContractConception);
// Execute _init. // Execute _init.
m_vm = VMFactory::create(_gas); if (_init.empty())
m_ext = make_shared<ExtVM>(m_s, m_lastHashes, m_newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _init, m_depth); {
return _init.empty(); m_s.m_cache[m_newAddress].setCode({});
m_endGas = _gas;
}
else
{
m_vm = VMFactory::create(_gas);
m_ext = make_shared<ExtVM>(m_s, m_lastHashes, m_newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _init, m_depth);
}
return !m_ext;
} }
OnOpFunc Executive::simpleTrace() OnOpFunc Executive::simpleTrace()

15
libethereum/Interface.h

@ -86,15 +86,15 @@ public:
// [LOGS API] // [LOGS API]
virtual LogEntries logs(unsigned _watchId) const = 0; virtual LocalisedLogEntries logs(unsigned _watchId) const = 0;
virtual LogEntries logs(LogFilter const& _filter) const = 0; virtual LocalisedLogEntries logs(LogFilter const& _filter) const = 0;
/// Install, uninstall and query watches. /// Install, uninstall and query watches.
virtual unsigned installWatch(LogFilter const& _filter) = 0; virtual unsigned installWatch(LogFilter const& _filter) = 0;
virtual unsigned installWatch(h256 _filterId) = 0; virtual unsigned installWatch(h256 _filterId) = 0;
virtual void uninstallWatch(unsigned _watchId) = 0; virtual void uninstallWatch(unsigned _watchId) = 0;
virtual bool peekWatch(unsigned _watchId) const = 0; virtual LocalisedLogEntries peekWatch(unsigned _watchId) const = 0;
virtual bool checkWatch(unsigned _watchId) = 0; virtual LocalisedLogEntries checkWatch(unsigned _watchId) = 0;
// [BLOCK QUERY API] // [BLOCK QUERY API]
@ -178,10 +178,9 @@ public:
Watch(Interface& _c, LogFilter const& _tf): m_c(&_c), m_id(_c.installWatch(_tf)) {} Watch(Interface& _c, LogFilter const& _tf): m_c(&_c), m_id(_c.installWatch(_tf)) {}
~Watch() { if (m_c) m_c->uninstallWatch(m_id); } ~Watch() { if (m_c) m_c->uninstallWatch(m_id); }
bool check() { return m_c ? m_c->checkWatch(m_id) : false; } LocalisedLogEntries check() { return m_c ? m_c->checkWatch(m_id) : LocalisedLogEntries(); }
bool peek() { return m_c ? m_c->peekWatch(m_id) : false; } LocalisedLogEntries peek() { return m_c ? m_c->peekWatch(m_id) : LocalisedLogEntries(); }
// PastMessages messages() const { return m_c->messages(m_id); } LocalisedLogEntries logs() const { return m_c->logs(m_id); }
LogEntries logs() const { return m_c->logs(m_id); }
private: private:
Interface* m_c = nullptr; Interface* m_c = nullptr;

17
libethereum/LogFilter.cpp

@ -68,14 +68,15 @@ bool LogFilter::matches(State const& _s, unsigned _i) const
LogEntries LogFilter::matches(TransactionReceipt const& _m) const LogEntries LogFilter::matches(TransactionReceipt const& _m) const
{ {
LogEntries ret; LogEntries ret;
for (LogEntry const& e: _m.log()) if (matches(_m.bloom()))
{ for (LogEntry const& e: _m.log())
if (!m_addresses.empty() && !m_addresses.count(e.address)) {
continue; if (!m_addresses.empty() && !m_addresses.count(e.address))
for (auto const& t: m_topics)
if (!std::count(e.topics.begin(), e.topics.end(), t))
continue; continue;
ret.push_back(e); for (auto const& t: m_topics)
} if (!std::count(e.topics.begin(), e.topics.end(), t))
continue;
ret.push_back(e);
}
return ret; return ret;
} }

6
libethereum/State.cpp

@ -412,10 +412,10 @@ bool State::cull(TransactionQueue& _tq) const
return ret; return ret;
} }
h512s State::sync(BlockChain const& _bc, TransactionQueue& _tq, bool* o_transactionQueueChanged) TransactionReceipts State::sync(BlockChain const& _bc, TransactionQueue& _tq, bool* o_transactionQueueChanged)
{ {
// TRANSACTIONS // TRANSACTIONS
h512s ret; TransactionReceipts ret;
auto ts = _tq.transactions(); auto ts = _tq.transactions();
auto lh = getLastHashes(_bc); auto lh = getLastHashes(_bc);
@ -432,7 +432,7 @@ h512s State::sync(BlockChain const& _bc, TransactionQueue& _tq, bool* o_transact
uncommitToMine(); uncommitToMine();
// boost::timer t; // boost::timer t;
execute(lh, i.second); execute(lh, i.second);
ret.push_back(m_receipts.back().bloom()); ret.push_back(m_receipts.back());
_tq.noteGood(i); _tq.noteGood(i);
++goodTxs; ++goodTxs;
// cnote << "TX took:" << t.elapsed() * 1000; // cnote << "TX took:" << t.elapsed() * 1000;

4
libethereum/State.h

@ -140,10 +140,10 @@ public:
// TODO: Cleaner interface. // TODO: Cleaner interface.
/// Sync our transactions, killing those from the queue that we have and assimilating those that we don't. /// Sync our transactions, killing those from the queue that we have and assimilating those that we don't.
/// @returns a list of bloom filters one for each transaction placed from the queue into the state. /// @returns a list of receipts one for each transaction placed from the queue into the state.
/// @a o_transactionQueueChanged boolean pointer, the value of which will be set to true if the transaction queue /// @a o_transactionQueueChanged boolean pointer, the value of which will be set to true if the transaction queue
/// changed and the pointer is non-null /// changed and the pointer is non-null
h512s sync(BlockChain const& _bc, TransactionQueue& _tq, bool* o_transactionQueueChanged = nullptr); TransactionReceipts sync(BlockChain const& _bc, TransactionQueue& _tq, bool* o_transactionQueueChanged = nullptr);
/// Like sync but only operate on _tq, killing the invalid/old ones. /// Like sync but only operate on _tq, killing the invalid/old ones.
bool cull(TransactionQueue& _tq) const; bool cull(TransactionQueue& _tq) const;

17
libethereum/Utility.cpp

@ -23,6 +23,7 @@
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <libethcore/CommonEth.h> #include <libethcore/CommonEth.h>
#include <libdevcrypto/SHA3.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
@ -32,7 +33,7 @@ bytes dev::eth::parseData(string const& _args)
bytes m_data; bytes m_data;
boost::smatch what; boost::smatch what;
static const boost::regex r("(@|\\$)?\"([^\"]*)\"(\\s.*)?"); static const boost::regex r("(!|#|@|\\$)?\"([^\"]*)\"(\\s.*)?");
static const boost::regex d("(@|\\$)?([0-9]+)(\\s*(ether)|(finney)|(szabo))?(\\s.*)?"); static const boost::regex d("(@|\\$)?([0-9]+)(\\s*(ether)|(finney)|(szabo))?(\\s.*)?");
static const boost::regex h("(@|\\$)?(0x)?(([a-fA-F0-9])+)(\\s.*)?"); static const boost::regex h("(@|\\$)?(0x)?(([a-fA-F0-9])+)(\\s.*)?");
@ -67,13 +68,15 @@ bytes dev::eth::parseData(string const& _args)
} }
else if (boost::regex_match(s, what, r)) else if (boost::regex_match(s, what, r))
{ {
for (auto i: (string)what[2]) bytes d = asBytes(what[2]);
m_data.push_back((byte)i); if (what[1] == "!")
if (what[1] != "$") m_data += FixedHash<4>(sha3(d)).asBytes();
for (int i = what[2].length(); i < 32; ++i) else if (what[1] == "#")
m_data.push_back(0); m_data += sha3(d).asBytes();
else if (what[1] == "$")
m_data += d + bytes{0};
else else
m_data.push_back(0); m_data += d + bytes(32 - what[2].length() % 32, 0);
s = what[3]; s = what[3];
} }
else else

10
libevm/ExtVMFace.h

@ -60,6 +60,16 @@ struct LogEntry
using LogEntries = std::vector<LogEntry>; using LogEntries = std::vector<LogEntry>;
struct LocalisedLogEntry: public LogEntry
{
LocalisedLogEntry() {}
LocalisedLogEntry(LogEntry const& _le, unsigned _number): LogEntry(_le), number(_number) {}
unsigned number = 0;
};
using LocalisedLogEntries = std::vector<LocalisedLogEntry>;
inline LogBloom bloom(LogEntries const& _logs) inline LogBloom bloom(LogEntries const& _logs)
{ {
LogBloom ret; LogBloom ret;

760
libevm/VM.cpp

@ -31,3 +31,763 @@ void VM::reset(u256 _gas) noexcept
m_curPC = 0; m_curPC = 0;
m_jumpDests.clear(); m_jumpDests.clear();
} }
bytesConstRef VM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps)
{
auto memNeed = [](dev::u256 _offset, dev::u256 _size) { return _size ? (bigint)_offset + _size : (bigint)0; };
if (m_jumpDests.empty())
for (unsigned i = 0; i < _ext.code.size(); ++i)
if (_ext.code[i] == (byte)Instruction::JUMPDEST)
m_jumpDests.insert(i);
else if (_ext.code[i] >= (byte)Instruction::PUSH1 && _ext.code[i] <= (byte)Instruction::PUSH32)
i += _ext.code[i] - (unsigned)Instruction::PUSH1 + 1;
u256 nextPC = m_curPC + 1;
auto osteps = _steps;
for (bool stopped = false; !stopped && _steps--; m_curPC = nextPC, nextPC = m_curPC + 1)
{
// INSTRUCTION...
Instruction inst = (Instruction)_ext.getCode(m_curPC);
// FEES...
bigint runGas = c_stepGas;
bigint newTempSize = m_temp.size();
bigint copySize = 0;
auto onOperation = [&]()
{
if (_onOp)
_onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, this, &_ext);
};
// should work, but just seems to result in immediate errorless exit on initial execution. yeah. weird.
//m_onFail = std::function<void()>(onOperation);
switch (inst)
{
case Instruction::STOP:
runGas = 0;
break;
case Instruction::SUICIDE:
require(1);
runGas = 0;
break;
case Instruction::SSTORE:
require(2);
if (!_ext.store(m_stack.back()) && m_stack[m_stack.size() - 2])
runGas = c_sstoreSetGas;
else if (_ext.store(m_stack.back()) && !m_stack[m_stack.size() - 2])
{
runGas = 0;
_ext.sub.refunds += c_sstoreRefundGas;
}
else
runGas = c_sstoreResetGas;
break;
case Instruction::SLOAD:
require(1);
runGas = c_sloadGas;
break;
// These all operate on memory and therefore potentially expand it:
case Instruction::MSTORE:
require(2);
newTempSize = (bigint)m_stack.back() + 32;
break;
case Instruction::MSTORE8:
require(2);
newTempSize = (bigint)m_stack.back() + 1;
break;
case Instruction::MLOAD:
require(1);
newTempSize = (bigint)m_stack.back() + 32;
break;
case Instruction::RETURN:
require(2);
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 2]);
break;
case Instruction::SHA3:
require(2);
runGas = c_sha3Gas + (m_stack[m_stack.size() - 2] + 31) / 32 * c_sha3WordGas;
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 2]);
break;
case Instruction::CALLDATACOPY:
require(3);
copySize = m_stack[m_stack.size() - 3];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 3]);
break;
case Instruction::CODECOPY:
require(3);
copySize = m_stack[m_stack.size() - 3];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 3]);
break;
case Instruction::EXTCODECOPY:
require(4);
copySize = m_stack[m_stack.size() - 4];
newTempSize = memNeed(m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 4]);
break;
case Instruction::BALANCE:
require(1);
runGas = c_balanceGas;
break;
case Instruction::LOG0:
case Instruction::LOG1:
case Instruction::LOG2:
case Instruction::LOG3:
case Instruction::LOG4:
{
unsigned n = (unsigned)inst - (unsigned)Instruction::LOG0;
require(n + 2);
runGas = c_logGas + c_logTopicGas * n + (bigint)c_logDataGas * m_stack[m_stack.size() - 2];
newTempSize = memNeed(m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2]);
break;
}
case Instruction::CALL:
case Instruction::CALLCODE:
require(7);
runGas = (bigint)c_callGas + m_stack[m_stack.size() - 1];
newTempSize = std::max(memNeed(m_stack[m_stack.size() - 6], m_stack[m_stack.size() - 7]), memNeed(m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]));
break;
case Instruction::CREATE:
{
require(3);
newTempSize = memNeed(m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 3]);
runGas = c_createGas;
break;
}
case Instruction::EXP:
{
require(2);
auto expon = m_stack[m_stack.size() - 2];
runGas = c_expGas + c_expByteGas * (32 - (h256(expon).firstBitSet() / 8));
break;
}
case Instruction::BLOCKHASH:
require(1);
break;
case Instruction::PC:
case Instruction::MSIZE:
case Instruction::GAS:
case Instruction::JUMPDEST:
case Instruction::ADDRESS:
case Instruction::ORIGIN:
case Instruction::CALLER:
case Instruction::CALLVALUE:
case Instruction::CALLDATASIZE:
case Instruction::CODESIZE:
case Instruction::GASPRICE:
case Instruction::COINBASE:
case Instruction::TIMESTAMP:
case Instruction::NUMBER:
case Instruction::DIFFICULTY:
case Instruction::GASLIMIT:
case Instruction::PUSH1:
case Instruction::PUSH2:
case Instruction::PUSH3:
case Instruction::PUSH4:
case Instruction::PUSH5:
case Instruction::PUSH6:
case Instruction::PUSH7:
case Instruction::PUSH8:
case Instruction::PUSH9:
case Instruction::PUSH10:
case Instruction::PUSH11:
case Instruction::PUSH12:
case Instruction::PUSH13:
case Instruction::PUSH14:
case Instruction::PUSH15:
case Instruction::PUSH16:
case Instruction::PUSH17:
case Instruction::PUSH18:
case Instruction::PUSH19:
case Instruction::PUSH20:
case Instruction::PUSH21:
case Instruction::PUSH22:
case Instruction::PUSH23:
case Instruction::PUSH24:
case Instruction::PUSH25:
case Instruction::PUSH26:
case Instruction::PUSH27:
case Instruction::PUSH28:
case Instruction::PUSH29:
case Instruction::PUSH30:
case Instruction::PUSH31:
case Instruction::PUSH32:
break;
case Instruction::NOT:
case Instruction::ISZERO:
case Instruction::CALLDATALOAD:
case Instruction::EXTCODESIZE:
case Instruction::POP:
case Instruction::JUMP:
require(1);
break;
case Instruction::ADD:
case Instruction::MUL:
case Instruction::SUB:
case Instruction::DIV:
case Instruction::SDIV:
case Instruction::MOD:
case Instruction::SMOD:
case Instruction::LT:
case Instruction::GT:
case Instruction::SLT:
case Instruction::SGT:
case Instruction::EQ:
case Instruction::AND:
case Instruction::OR:
case Instruction::XOR:
case Instruction::BYTE:
case Instruction::JUMPI:
case Instruction::SIGNEXTEND:
require(2);
break;
case Instruction::ADDMOD:
case Instruction::MULMOD:
require(3);
break;
case Instruction::DUP1:
case Instruction::DUP2:
case Instruction::DUP3:
case Instruction::DUP4:
case Instruction::DUP5:
case Instruction::DUP6:
case Instruction::DUP7:
case Instruction::DUP8:
case Instruction::DUP9:
case Instruction::DUP10:
case Instruction::DUP11:
case Instruction::DUP12:
case Instruction::DUP13:
case Instruction::DUP14:
case Instruction::DUP15:
case Instruction::DUP16:
require(1 + (int)inst - (int)Instruction::DUP1);
break;
case Instruction::SWAP1:
case Instruction::SWAP2:
case Instruction::SWAP3:
case Instruction::SWAP4:
case Instruction::SWAP5:
case Instruction::SWAP6:
case Instruction::SWAP7:
case Instruction::SWAP8:
case Instruction::SWAP9:
case Instruction::SWAP10:
case Instruction::SWAP11:
case Instruction::SWAP12:
case Instruction::SWAP13:
case Instruction::SWAP14:
case Instruction::SWAP15:
case Instruction::SWAP16:
require((int)inst - (int)Instruction::SWAP1 + 2);
break;
default:
BOOST_THROW_EXCEPTION(BadInstruction());
}
newTempSize = (newTempSize + 31) / 32 * 32;
if (newTempSize > m_temp.size())
runGas += c_memoryGas * (newTempSize - m_temp.size()) / 32;
runGas += c_copyGas * (copySize + 31) / 32;
onOperation();
// if (_onOp)
// _onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, this, &_ext);
if (m_gas < runGas)
{
// Out of gas!
m_gas = 0;
BOOST_THROW_EXCEPTION(OutOfGas());
}
m_gas = (u256)((bigint)m_gas - runGas);
if (newTempSize > m_temp.size())
m_temp.resize((size_t)newTempSize);
// EXECUTE...
switch (inst)
{
case Instruction::ADD:
//pops two items and pushes S[-1] + S[-2] mod 2^256.
m_stack[m_stack.size() - 2] += m_stack.back();
m_stack.pop_back();
break;
case Instruction::MUL:
//pops two items and pushes S[-1] * S[-2] mod 2^256.
m_stack[m_stack.size() - 2] *= m_stack.back();
m_stack.pop_back();
break;
case Instruction::SUB:
m_stack[m_stack.size() - 2] = m_stack.back() - m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::DIV:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? m_stack.back() / m_stack[m_stack.size() - 2] : 0;
m_stack.pop_back();
break;
case Instruction::SDIV:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? s2u(u2s(m_stack.back()) / u2s(m_stack[m_stack.size() - 2])) : 0;
m_stack.pop_back();
break;
case Instruction::MOD:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? m_stack.back() % m_stack[m_stack.size() - 2] : 0;
m_stack.pop_back();
break;
case Instruction::SMOD:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? s2u(u2s(m_stack.back()) % u2s(m_stack[m_stack.size() - 2])) : 0;
m_stack.pop_back();
break;
case Instruction::EXP:
{
auto base = m_stack.back();
auto expon = m_stack[m_stack.size() - 2];
m_stack.pop_back();
m_stack.back() = (u256)boost::multiprecision::powm((bigint)base, (bigint)expon, bigint(2) << 256);
break;
}
case Instruction::NOT:
m_stack.back() = ~m_stack.back();
break;
case Instruction::LT:
m_stack[m_stack.size() - 2] = m_stack.back() < m_stack[m_stack.size() - 2] ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::GT:
m_stack[m_stack.size() - 2] = m_stack.back() > m_stack[m_stack.size() - 2] ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::SLT:
m_stack[m_stack.size() - 2] = u2s(m_stack.back()) < u2s(m_stack[m_stack.size() - 2]) ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::SGT:
m_stack[m_stack.size() - 2] = u2s(m_stack.back()) > u2s(m_stack[m_stack.size() - 2]) ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::EQ:
m_stack[m_stack.size() - 2] = m_stack.back() == m_stack[m_stack.size() - 2] ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::ISZERO:
m_stack.back() = m_stack.back() ? 0 : 1;
break;
case Instruction::AND:
m_stack[m_stack.size() - 2] = m_stack.back() & m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::OR:
m_stack[m_stack.size() - 2] = m_stack.back() | m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::XOR:
m_stack[m_stack.size() - 2] = m_stack.back() ^ m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::BYTE:
m_stack[m_stack.size() - 2] = m_stack.back() < 32 ? (m_stack[m_stack.size() - 2] >> (unsigned)(8 * (31 - m_stack.back()))) & 0xff : 0;
m_stack.pop_back();
break;
case Instruction::ADDMOD:
m_stack[m_stack.size() - 3] = u256((bigint(m_stack.back()) + bigint(m_stack[m_stack.size() - 2])) % m_stack[m_stack.size() - 3]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::MULMOD:
m_stack[m_stack.size() - 3] = u256((bigint(m_stack.back()) * bigint(m_stack[m_stack.size() - 2])) % m_stack[m_stack.size() - 3]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::SIGNEXTEND:
if (m_stack.back() < 31)
{
unsigned const testBit(m_stack.back() * 8 + 7);
u256& number = m_stack[m_stack.size() - 2];
u256 mask = ((u256(1) << testBit) - 1);
if (boost::multiprecision::bit_test(number, testBit))
number |= ~mask;
else
number &= mask;
}
m_stack.pop_back();
break;
case Instruction::SHA3:
{
unsigned inOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned inSize = (unsigned)m_stack.back();
m_stack.pop_back();
m_stack.push_back(sha3(bytesConstRef(m_temp.data() + inOff, inSize)));
break;
}
case Instruction::ADDRESS:
m_stack.push_back(fromAddress(_ext.myAddress));
break;
case Instruction::ORIGIN:
m_stack.push_back(fromAddress(_ext.origin));
break;
case Instruction::BALANCE:
{
m_stack.back() = _ext.balance(asAddress(m_stack.back()));
break;
}
case Instruction::CALLER:
m_stack.push_back(fromAddress(_ext.caller));
break;
case Instruction::CALLVALUE:
m_stack.push_back(_ext.value);
break;
case Instruction::CALLDATALOAD:
{
if ((unsigned)m_stack.back() + (uint64_t)31 < _ext.data.size())
m_stack.back() = (u256)*(h256 const*)(_ext.data.data() + (unsigned)m_stack.back());
else
{
h256 r;
for (uint64_t i = (unsigned)m_stack.back(), e = (unsigned)m_stack.back() + (uint64_t)32, j = 0; i < e; ++i, ++j)
r[j] = i < _ext.data.size() ? _ext.data[i] : 0;
m_stack.back() = (u256)r;
}
break;
}
case Instruction::CALLDATASIZE:
m_stack.push_back(_ext.data.size());
break;
case Instruction::CODESIZE:
m_stack.push_back(_ext.code.size());
break;
case Instruction::EXTCODESIZE:
m_stack.back() = _ext.codeAt(asAddress(m_stack.back())).size();
break;
case Instruction::CALLDATACOPY:
case Instruction::CODECOPY:
case Instruction::EXTCODECOPY:
{
Address a;
if (inst == Instruction::EXTCODECOPY)
{
a = asAddress(m_stack.back());
m_stack.pop_back();
}
unsigned offset = (unsigned)m_stack.back();
m_stack.pop_back();
u256 index = m_stack.back();
m_stack.pop_back();
unsigned size = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned sizeToBeCopied;
switch(inst)
{
case Instruction::CALLDATACOPY:
sizeToBeCopied = index + (bigint)size > (u256)_ext.data.size() ? (u256)_ext.data.size() < index ? 0 : _ext.data.size() - (unsigned)index : size;
memcpy(m_temp.data() + offset, _ext.data.data() + (unsigned)index, sizeToBeCopied);
break;
case Instruction::CODECOPY:
sizeToBeCopied = index + (bigint)size > (u256)_ext.code.size() ? (u256)_ext.code.size() < index ? 0 : _ext.code.size() - (unsigned)index : size;
memcpy(m_temp.data() + offset, _ext.code.data() + (unsigned)index, sizeToBeCopied);
break;
case Instruction::EXTCODECOPY:
sizeToBeCopied = index + (bigint)size > (u256)_ext.codeAt(a).size() ? (u256)_ext.codeAt(a).size() < index ? 0 : _ext.codeAt(a).size() - (unsigned)index : size;
memcpy(m_temp.data() + offset, _ext.codeAt(a).data() + (unsigned)index, sizeToBeCopied);
break;
default:
// this is unreachable, but if someone introduces a bug in the future, he may get here.
assert(false);
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("CALLDATACOPY, CODECOPY or EXTCODECOPY instruction requested."));
break;
}
memset(m_temp.data() + offset + sizeToBeCopied, 0, size - sizeToBeCopied);
break;
}
case Instruction::GASPRICE:
m_stack.push_back(_ext.gasPrice);
break;
case Instruction::BLOCKHASH:
m_stack.back() = (u256)_ext.blockhash(m_stack.back());
break;
case Instruction::COINBASE:
m_stack.push_back((u160)_ext.currentBlock.coinbaseAddress);
break;
case Instruction::TIMESTAMP:
m_stack.push_back(_ext.currentBlock.timestamp);
break;
case Instruction::NUMBER:
m_stack.push_back(_ext.currentBlock.number);
break;
case Instruction::DIFFICULTY:
m_stack.push_back(_ext.currentBlock.difficulty);
break;
case Instruction::GASLIMIT:
m_stack.push_back(1000000);
break;
case Instruction::PUSH1:
case Instruction::PUSH2:
case Instruction::PUSH3:
case Instruction::PUSH4:
case Instruction::PUSH5:
case Instruction::PUSH6:
case Instruction::PUSH7:
case Instruction::PUSH8:
case Instruction::PUSH9:
case Instruction::PUSH10:
case Instruction::PUSH11:
case Instruction::PUSH12:
case Instruction::PUSH13:
case Instruction::PUSH14:
case Instruction::PUSH15:
case Instruction::PUSH16:
case Instruction::PUSH17:
case Instruction::PUSH18:
case Instruction::PUSH19:
case Instruction::PUSH20:
case Instruction::PUSH21:
case Instruction::PUSH22:
case Instruction::PUSH23:
case Instruction::PUSH24:
case Instruction::PUSH25:
case Instruction::PUSH26:
case Instruction::PUSH27:
case Instruction::PUSH28:
case Instruction::PUSH29:
case Instruction::PUSH30:
case Instruction::PUSH31:
case Instruction::PUSH32:
{
int i = (int)inst - (int)Instruction::PUSH1 + 1;
nextPC = m_curPC + 1;
m_stack.push_back(0);
for (; i--; nextPC++)
m_stack.back() = (m_stack.back() << 8) | _ext.getCode(nextPC);
break;
}
case Instruction::POP:
m_stack.pop_back();
break;
case Instruction::DUP1:
case Instruction::DUP2:
case Instruction::DUP3:
case Instruction::DUP4:
case Instruction::DUP5:
case Instruction::DUP6:
case Instruction::DUP7:
case Instruction::DUP8:
case Instruction::DUP9:
case Instruction::DUP10:
case Instruction::DUP11:
case Instruction::DUP12:
case Instruction::DUP13:
case Instruction::DUP14:
case Instruction::DUP15:
case Instruction::DUP16:
{
auto n = 1 + (int)inst - (int)Instruction::DUP1;
m_stack.push_back(m_stack[m_stack.size() - n]);
break;
}
case Instruction::SWAP1:
case Instruction::SWAP2:
case Instruction::SWAP3:
case Instruction::SWAP4:
case Instruction::SWAP5:
case Instruction::SWAP6:
case Instruction::SWAP7:
case Instruction::SWAP8:
case Instruction::SWAP9:
case Instruction::SWAP10:
case Instruction::SWAP11:
case Instruction::SWAP12:
case Instruction::SWAP13:
case Instruction::SWAP14:
case Instruction::SWAP15:
case Instruction::SWAP16:
{
unsigned n = (int)inst - (int)Instruction::SWAP1 + 2;
auto d = m_stack.back();
m_stack.back() = m_stack[m_stack.size() - n];
m_stack[m_stack.size() - n] = d;
break;
}
case Instruction::MLOAD:
{
m_stack.back() = (u256)*(h256 const*)(m_temp.data() + (unsigned)m_stack.back());
break;
}
case Instruction::MSTORE:
{
*(h256*)&m_temp[(unsigned)m_stack.back()] = (h256)m_stack[m_stack.size() - 2];
m_stack.pop_back();
m_stack.pop_back();
break;
}
case Instruction::MSTORE8:
{
m_temp[(unsigned)m_stack.back()] = (byte)(m_stack[m_stack.size() - 2] & 0xff);
m_stack.pop_back();
m_stack.pop_back();
break;
}
case Instruction::SLOAD:
m_stack.back() = _ext.store(m_stack.back());
break;
case Instruction::SSTORE:
_ext.setStore(m_stack.back(), m_stack[m_stack.size() - 2]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::JUMP:
nextPC = m_stack.back();
if (!m_jumpDests.count(nextPC))
BOOST_THROW_EXCEPTION(BadJumpDestination());
m_stack.pop_back();
break;
case Instruction::JUMPI:
if (m_stack[m_stack.size() - 2])
{
nextPC = m_stack.back();
if (!m_jumpDests.count(nextPC))
BOOST_THROW_EXCEPTION(BadJumpDestination());
}
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::PC:
m_stack.push_back(m_curPC);
break;
case Instruction::MSIZE:
m_stack.push_back(m_temp.size());
break;
case Instruction::GAS:
m_stack.push_back(m_gas);
break;
case Instruction::JUMPDEST:
break;
/* case Instruction::LOG0:
_ext.log({}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
break;
case Instruction::LOG1:
_ext.log({m_stack[m_stack.size() - 1]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 2], (unsigned)m_stack[m_stack.size() - 3]));
break;
case Instruction::LOG2:
_ext.log({m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 3], (unsigned)m_stack[m_stack.size() - 4]));
break;
case Instruction::LOG3:
_ext.log({m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 3]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 4], (unsigned)m_stack[m_stack.size() - 5]));
break;
case Instruction::LOG4:
_ext.log({m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 5], (unsigned)m_stack[m_stack.size() - 6]));
break;*/
case Instruction::LOG0:
_ext.log({}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG1:
_ext.log({m_stack[m_stack.size() - 3]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG2:
_ext.log({m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG3:
_ext.log({m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG4:
_ext.log({m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5], m_stack[m_stack.size() - 6]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::CREATE:
{
u256 endowment = m_stack.back();
m_stack.pop_back();
unsigned initOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned initSize = (unsigned)m_stack.back();
m_stack.pop_back();
if (_ext.balance(_ext.myAddress) >= endowment && _ext.depth < 1024)
{
_ext.subBalance(endowment);
m_stack.push_back((u160)_ext.create(endowment, m_gas, bytesConstRef(m_temp.data() + initOff, initSize), _onOp));
}
else
m_stack.push_back(0);
break;
}
case Instruction::CALL:
case Instruction::CALLCODE:
{
u256 gas = m_stack.back();
m_stack.pop_back();
Address receiveAddress = asAddress(m_stack.back());
m_stack.pop_back();
u256 value = m_stack.back();
m_stack.pop_back();
unsigned inOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned inSize = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned outOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned outSize = (unsigned)m_stack.back();
m_stack.pop_back();
if (_ext.balance(_ext.myAddress) >= value && _ext.depth < 1024)
{
_ext.subBalance(value);
m_stack.push_back(_ext.call(inst == Instruction::CALL ? receiveAddress : _ext.myAddress, value, bytesConstRef(m_temp.data() + inOff, inSize), gas, bytesRef(m_temp.data() + outOff, outSize), _onOp, {}, receiveAddress));
}
else
m_stack.push_back(0);
m_gas += gas;
break;
}
case Instruction::RETURN:
{
unsigned b = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned s = (unsigned)m_stack.back();
m_stack.pop_back();
return bytesConstRef(m_temp.data() + b, s);
}
case Instruction::SUICIDE:
{
Address dest = asAddress(m_stack.back());
_ext.suicide(dest);
// ...follow through to...
}
case Instruction::STOP:
return bytesConstRef();
}
}
if (_steps == (uint64_t)-1)
BOOST_THROW_EXCEPTION(StepsDone());
return bytesConstRef();
}

763
libevm/VM.h

@ -77,768 +77,5 @@ private:
std::function<void()> m_onFail; std::function<void()> m_onFail;
}; };
// TODO: Move it to cpp file. Not done to make review easier.
inline bytesConstRef VM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps)
{
auto memNeed = [](dev::u256 _offset, dev::u256 _size) { return _size ? (bigint)_offset + _size : (bigint)0; };
if (m_jumpDests.empty())
for (unsigned i = 0; i < _ext.code.size(); ++i)
if (_ext.code[i] == (byte)Instruction::JUMPDEST)
m_jumpDests.insert(i);
else if (_ext.code[i] >= (byte)Instruction::PUSH1 && _ext.code[i] <= (byte)Instruction::PUSH32)
i += _ext.code[i] - (unsigned)Instruction::PUSH1 + 1;
u256 nextPC = m_curPC + 1;
auto osteps = _steps;
for (bool stopped = false; !stopped && _steps--; m_curPC = nextPC, nextPC = m_curPC + 1)
{
// INSTRUCTION...
Instruction inst = (Instruction)_ext.getCode(m_curPC);
// FEES...
bigint runGas = c_stepGas;
bigint newTempSize = m_temp.size();
bigint copySize = 0;
auto onOperation = [&]()
{
if (_onOp)
_onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, this, &_ext);
};
// should work, but just seems to result in immediate errorless exit on initial execution. yeah. weird.
//m_onFail = std::function<void()>(onOperation);
switch (inst)
{
case Instruction::STOP:
runGas = 0;
break;
case Instruction::SUICIDE:
require(1);
runGas = 0;
break;
case Instruction::SSTORE:
require(2);
if (!_ext.store(m_stack.back()) && m_stack[m_stack.size() - 2])
runGas = c_sstoreSetGas;
else if (_ext.store(m_stack.back()) && !m_stack[m_stack.size() - 2])
{
runGas = 0;
_ext.sub.refunds += c_sstoreRefundGas;
}
else
runGas = c_sstoreResetGas;
break;
case Instruction::SLOAD:
require(1);
runGas = c_sloadGas;
break;
// These all operate on memory and therefore potentially expand it:
case Instruction::MSTORE:
require(2);
newTempSize = (bigint)m_stack.back() + 32;
break;
case Instruction::MSTORE8:
require(2);
newTempSize = (bigint)m_stack.back() + 1;
break;
case Instruction::MLOAD:
require(1);
newTempSize = (bigint)m_stack.back() + 32;
break;
case Instruction::RETURN:
require(2);
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 2]);
break;
case Instruction::SHA3:
require(2);
runGas = c_sha3Gas + (m_stack[m_stack.size() - 2] + 31) / 32 * c_sha3WordGas;
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 2]);
break;
case Instruction::CALLDATACOPY:
require(3);
copySize = m_stack[m_stack.size() - 3];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 3]);
break;
case Instruction::CODECOPY:
require(3);
copySize = m_stack[m_stack.size() - 3];
newTempSize = memNeed(m_stack.back(), m_stack[m_stack.size() - 3]);
break;
case Instruction::EXTCODECOPY:
require(4);
copySize = m_stack[m_stack.size() - 4];
newTempSize = memNeed(m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 4]);
break;
case Instruction::BALANCE:
require(1);
runGas = c_balanceGas;
break;
case Instruction::LOG0:
case Instruction::LOG1:
case Instruction::LOG2:
case Instruction::LOG3:
case Instruction::LOG4:
{
unsigned n = (unsigned)inst - (unsigned)Instruction::LOG0;
require(n + 2);
runGas = c_logGas + c_logTopicGas * n + (bigint)c_logDataGas * m_stack[m_stack.size() - 2];
newTempSize = memNeed(m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2]);
break;
}
case Instruction::CALL:
case Instruction::CALLCODE:
require(7);
runGas = (bigint)c_callGas + m_stack[m_stack.size() - 1];
newTempSize = std::max(memNeed(m_stack[m_stack.size() - 6], m_stack[m_stack.size() - 7]), memNeed(m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]));
break;
case Instruction::CREATE:
{
require(3);
u256 inOff = m_stack[m_stack.size() - 2];
u256 inSize = m_stack[m_stack.size() - 3];
newTempSize = (bigint)inOff + inSize;
runGas = c_createGas;
break;
}
case Instruction::EXP:
{
require(2);
auto expon = m_stack[m_stack.size() - 2];
runGas = c_expGas + c_expByteGas * (32 - (h256(expon).firstBitSet() / 8));
break;
}
case Instruction::BLOCKHASH:
require(1);
break;
case Instruction::PC:
case Instruction::MSIZE:
case Instruction::GAS:
case Instruction::JUMPDEST:
case Instruction::ADDRESS:
case Instruction::ORIGIN:
case Instruction::CALLER:
case Instruction::CALLVALUE:
case Instruction::CALLDATASIZE:
case Instruction::CODESIZE:
case Instruction::GASPRICE:
case Instruction::COINBASE:
case Instruction::TIMESTAMP:
case Instruction::NUMBER:
case Instruction::DIFFICULTY:
case Instruction::GASLIMIT:
case Instruction::PUSH1:
case Instruction::PUSH2:
case Instruction::PUSH3:
case Instruction::PUSH4:
case Instruction::PUSH5:
case Instruction::PUSH6:
case Instruction::PUSH7:
case Instruction::PUSH8:
case Instruction::PUSH9:
case Instruction::PUSH10:
case Instruction::PUSH11:
case Instruction::PUSH12:
case Instruction::PUSH13:
case Instruction::PUSH14:
case Instruction::PUSH15:
case Instruction::PUSH16:
case Instruction::PUSH17:
case Instruction::PUSH18:
case Instruction::PUSH19:
case Instruction::PUSH20:
case Instruction::PUSH21:
case Instruction::PUSH22:
case Instruction::PUSH23:
case Instruction::PUSH24:
case Instruction::PUSH25:
case Instruction::PUSH26:
case Instruction::PUSH27:
case Instruction::PUSH28:
case Instruction::PUSH29:
case Instruction::PUSH30:
case Instruction::PUSH31:
case Instruction::PUSH32:
break;
case Instruction::NOT:
case Instruction::ISZERO:
case Instruction::CALLDATALOAD:
case Instruction::EXTCODESIZE:
case Instruction::POP:
case Instruction::JUMP:
require(1);
break;
case Instruction::ADD:
case Instruction::MUL:
case Instruction::SUB:
case Instruction::DIV:
case Instruction::SDIV:
case Instruction::MOD:
case Instruction::SMOD:
case Instruction::LT:
case Instruction::GT:
case Instruction::SLT:
case Instruction::SGT:
case Instruction::EQ:
case Instruction::AND:
case Instruction::OR:
case Instruction::XOR:
case Instruction::BYTE:
case Instruction::JUMPI:
case Instruction::SIGNEXTEND:
require(2);
break;
case Instruction::ADDMOD:
case Instruction::MULMOD:
require(3);
break;
case Instruction::DUP1:
case Instruction::DUP2:
case Instruction::DUP3:
case Instruction::DUP4:
case Instruction::DUP5:
case Instruction::DUP6:
case Instruction::DUP7:
case Instruction::DUP8:
case Instruction::DUP9:
case Instruction::DUP10:
case Instruction::DUP11:
case Instruction::DUP12:
case Instruction::DUP13:
case Instruction::DUP14:
case Instruction::DUP15:
case Instruction::DUP16:
require(1 + (int)inst - (int)Instruction::DUP1);
break;
case Instruction::SWAP1:
case Instruction::SWAP2:
case Instruction::SWAP3:
case Instruction::SWAP4:
case Instruction::SWAP5:
case Instruction::SWAP6:
case Instruction::SWAP7:
case Instruction::SWAP8:
case Instruction::SWAP9:
case Instruction::SWAP10:
case Instruction::SWAP11:
case Instruction::SWAP12:
case Instruction::SWAP13:
case Instruction::SWAP14:
case Instruction::SWAP15:
case Instruction::SWAP16:
require((int)inst - (int)Instruction::SWAP1 + 2);
break;
default:
BOOST_THROW_EXCEPTION(BadInstruction());
}
newTempSize = (newTempSize + 31) / 32 * 32;
if (newTempSize > m_temp.size())
runGas += c_memoryGas * (newTempSize - m_temp.size()) / 32;
runGas += c_copyGas * (copySize + 31) / 32;
onOperation();
// if (_onOp)
// _onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, this, &_ext);
if (m_gas < runGas)
{
// Out of gas!
m_gas = 0;
BOOST_THROW_EXCEPTION(OutOfGas());
}
m_gas = (u256)((bigint)m_gas - runGas);
if (newTempSize > m_temp.size())
m_temp.resize((size_t)newTempSize);
// EXECUTE...
switch (inst)
{
case Instruction::ADD:
//pops two items and pushes S[-1] + S[-2] mod 2^256.
m_stack[m_stack.size() - 2] += m_stack.back();
m_stack.pop_back();
break;
case Instruction::MUL:
//pops two items and pushes S[-1] * S[-2] mod 2^256.
m_stack[m_stack.size() - 2] *= m_stack.back();
m_stack.pop_back();
break;
case Instruction::SUB:
m_stack[m_stack.size() - 2] = m_stack.back() - m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::DIV:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? m_stack.back() / m_stack[m_stack.size() - 2] : 0;
m_stack.pop_back();
break;
case Instruction::SDIV:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? s2u(u2s(m_stack.back()) / u2s(m_stack[m_stack.size() - 2])) : 0;
m_stack.pop_back();
break;
case Instruction::MOD:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? m_stack.back() % m_stack[m_stack.size() - 2] : 0;
m_stack.pop_back();
break;
case Instruction::SMOD:
m_stack[m_stack.size() - 2] = m_stack[m_stack.size() - 2] ? s2u(u2s(m_stack.back()) % u2s(m_stack[m_stack.size() - 2])) : 0;
m_stack.pop_back();
break;
case Instruction::EXP:
{
auto base = m_stack.back();
auto expon = m_stack[m_stack.size() - 2];
m_stack.pop_back();
m_stack.back() = (u256)boost::multiprecision::powm((bigint)base, (bigint)expon, bigint(2) << 256);
break;
}
case Instruction::NOT:
m_stack.back() = ~m_stack.back();
break;
case Instruction::LT:
m_stack[m_stack.size() - 2] = m_stack.back() < m_stack[m_stack.size() - 2] ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::GT:
m_stack[m_stack.size() - 2] = m_stack.back() > m_stack[m_stack.size() - 2] ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::SLT:
m_stack[m_stack.size() - 2] = u2s(m_stack.back()) < u2s(m_stack[m_stack.size() - 2]) ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::SGT:
m_stack[m_stack.size() - 2] = u2s(m_stack.back()) > u2s(m_stack[m_stack.size() - 2]) ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::EQ:
m_stack[m_stack.size() - 2] = m_stack.back() == m_stack[m_stack.size() - 2] ? 1 : 0;
m_stack.pop_back();
break;
case Instruction::ISZERO:
m_stack.back() = m_stack.back() ? 0 : 1;
break;
case Instruction::AND:
m_stack[m_stack.size() - 2] = m_stack.back() & m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::OR:
m_stack[m_stack.size() - 2] = m_stack.back() | m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::XOR:
m_stack[m_stack.size() - 2] = m_stack.back() ^ m_stack[m_stack.size() - 2];
m_stack.pop_back();
break;
case Instruction::BYTE:
m_stack[m_stack.size() - 2] = m_stack.back() < 32 ? (m_stack[m_stack.size() - 2] >> (unsigned)(8 * (31 - m_stack.back()))) & 0xff : 0;
m_stack.pop_back();
break;
case Instruction::ADDMOD:
m_stack[m_stack.size() - 3] = u256((bigint(m_stack.back()) + bigint(m_stack[m_stack.size() - 2])) % m_stack[m_stack.size() - 3]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::MULMOD:
m_stack[m_stack.size() - 3] = u256((bigint(m_stack.back()) * bigint(m_stack[m_stack.size() - 2])) % m_stack[m_stack.size() - 3]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::SIGNEXTEND:
if (m_stack.back() < 31)
{
unsigned const testBit(m_stack.back() * 8 + 7);
u256& number = m_stack[m_stack.size() - 2];
u256 mask = ((u256(1) << testBit) - 1);
if (boost::multiprecision::bit_test(number, testBit))
number |= ~mask;
else
number &= mask;
}
m_stack.pop_back();
break;
case Instruction::SHA3:
{
unsigned inOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned inSize = (unsigned)m_stack.back();
m_stack.pop_back();
m_stack.push_back(sha3(bytesConstRef(m_temp.data() + inOff, inSize)));
break;
}
case Instruction::ADDRESS:
m_stack.push_back(fromAddress(_ext.myAddress));
break;
case Instruction::ORIGIN:
m_stack.push_back(fromAddress(_ext.origin));
break;
case Instruction::BALANCE:
{
m_stack.back() = _ext.balance(asAddress(m_stack.back()));
break;
}
case Instruction::CALLER:
m_stack.push_back(fromAddress(_ext.caller));
break;
case Instruction::CALLVALUE:
m_stack.push_back(_ext.value);
break;
case Instruction::CALLDATALOAD:
{
if ((unsigned)m_stack.back() + (uint64_t)31 < _ext.data.size())
m_stack.back() = (u256)*(h256 const*)(_ext.data.data() + (unsigned)m_stack.back());
else
{
h256 r;
for (uint64_t i = (unsigned)m_stack.back(), e = (unsigned)m_stack.back() + (uint64_t)32, j = 0; i < e; ++i, ++j)
r[j] = i < _ext.data.size() ? _ext.data[i] : 0;
m_stack.back() = (u256)r;
}
break;
}
case Instruction::CALLDATASIZE:
m_stack.push_back(_ext.data.size());
break;
case Instruction::CODESIZE:
m_stack.push_back(_ext.code.size());
break;
case Instruction::EXTCODESIZE:
m_stack.back() = _ext.codeAt(asAddress(m_stack.back())).size();
break;
case Instruction::CALLDATACOPY:
case Instruction::CODECOPY:
case Instruction::EXTCODECOPY:
{
Address a;
if (inst == Instruction::EXTCODECOPY)
{
a = asAddress(m_stack.back());
m_stack.pop_back();
}
unsigned offset = (unsigned)m_stack.back();
m_stack.pop_back();
u256 index = m_stack.back();
m_stack.pop_back();
unsigned size = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned sizeToBeCopied;
switch(inst)
{
case Instruction::CALLDATACOPY:
sizeToBeCopied = index + (bigint)size > (u256)_ext.data.size() ? (u256)_ext.data.size() < index ? 0 : _ext.data.size() - (unsigned)index : size;
memcpy(m_temp.data() + offset, _ext.data.data() + (unsigned)index, sizeToBeCopied);
break;
case Instruction::CODECOPY:
sizeToBeCopied = index + (bigint)size > (u256)_ext.code.size() ? (u256)_ext.code.size() < index ? 0 : _ext.code.size() - (unsigned)index : size;
memcpy(m_temp.data() + offset, _ext.code.data() + (unsigned)index, sizeToBeCopied);
break;
case Instruction::EXTCODECOPY:
sizeToBeCopied = index + (bigint)size > (u256)_ext.codeAt(a).size() ? (u256)_ext.codeAt(a).size() < index ? 0 : _ext.codeAt(a).size() - (unsigned)index : size;
memcpy(m_temp.data() + offset, _ext.codeAt(a).data() + (unsigned)index, sizeToBeCopied);
break;
default:
// this is unreachable, but if someone introduces a bug in the future, he may get here.
assert(false);
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("CALLDATACOPY, CODECOPY or EXTCODECOPY instruction requested."));
break;
}
memset(m_temp.data() + offset + sizeToBeCopied, 0, size - sizeToBeCopied);
break;
}
case Instruction::GASPRICE:
m_stack.push_back(_ext.gasPrice);
break;
case Instruction::BLOCKHASH:
m_stack.back() = (u256)_ext.blockhash(m_stack.back());
break;
case Instruction::COINBASE:
m_stack.push_back((u160)_ext.currentBlock.coinbaseAddress);
break;
case Instruction::TIMESTAMP:
m_stack.push_back(_ext.currentBlock.timestamp);
break;
case Instruction::NUMBER:
m_stack.push_back(_ext.currentBlock.number);
break;
case Instruction::DIFFICULTY:
m_stack.push_back(_ext.currentBlock.difficulty);
break;
case Instruction::GASLIMIT:
m_stack.push_back(1000000);
break;
case Instruction::PUSH1:
case Instruction::PUSH2:
case Instruction::PUSH3:
case Instruction::PUSH4:
case Instruction::PUSH5:
case Instruction::PUSH6:
case Instruction::PUSH7:
case Instruction::PUSH8:
case Instruction::PUSH9:
case Instruction::PUSH10:
case Instruction::PUSH11:
case Instruction::PUSH12:
case Instruction::PUSH13:
case Instruction::PUSH14:
case Instruction::PUSH15:
case Instruction::PUSH16:
case Instruction::PUSH17:
case Instruction::PUSH18:
case Instruction::PUSH19:
case Instruction::PUSH20:
case Instruction::PUSH21:
case Instruction::PUSH22:
case Instruction::PUSH23:
case Instruction::PUSH24:
case Instruction::PUSH25:
case Instruction::PUSH26:
case Instruction::PUSH27:
case Instruction::PUSH28:
case Instruction::PUSH29:
case Instruction::PUSH30:
case Instruction::PUSH31:
case Instruction::PUSH32:
{
int i = (int)inst - (int)Instruction::PUSH1 + 1;
nextPC = m_curPC + 1;
m_stack.push_back(0);
for (; i--; nextPC++)
m_stack.back() = (m_stack.back() << 8) | _ext.getCode(nextPC);
break;
}
case Instruction::POP:
m_stack.pop_back();
break;
case Instruction::DUP1:
case Instruction::DUP2:
case Instruction::DUP3:
case Instruction::DUP4:
case Instruction::DUP5:
case Instruction::DUP6:
case Instruction::DUP7:
case Instruction::DUP8:
case Instruction::DUP9:
case Instruction::DUP10:
case Instruction::DUP11:
case Instruction::DUP12:
case Instruction::DUP13:
case Instruction::DUP14:
case Instruction::DUP15:
case Instruction::DUP16:
{
auto n = 1 + (int)inst - (int)Instruction::DUP1;
m_stack.push_back(m_stack[m_stack.size() - n]);
break;
}
case Instruction::SWAP1:
case Instruction::SWAP2:
case Instruction::SWAP3:
case Instruction::SWAP4:
case Instruction::SWAP5:
case Instruction::SWAP6:
case Instruction::SWAP7:
case Instruction::SWAP8:
case Instruction::SWAP9:
case Instruction::SWAP10:
case Instruction::SWAP11:
case Instruction::SWAP12:
case Instruction::SWAP13:
case Instruction::SWAP14:
case Instruction::SWAP15:
case Instruction::SWAP16:
{
unsigned n = (int)inst - (int)Instruction::SWAP1 + 2;
auto d = m_stack.back();
m_stack.back() = m_stack[m_stack.size() - n];
m_stack[m_stack.size() - n] = d;
break;
}
case Instruction::MLOAD:
{
m_stack.back() = (u256)*(h256 const*)(m_temp.data() + (unsigned)m_stack.back());
break;
}
case Instruction::MSTORE:
{
*(h256*)&m_temp[(unsigned)m_stack.back()] = (h256)m_stack[m_stack.size() - 2];
m_stack.pop_back();
m_stack.pop_back();
break;
}
case Instruction::MSTORE8:
{
m_temp[(unsigned)m_stack.back()] = (byte)(m_stack[m_stack.size() - 2] & 0xff);
m_stack.pop_back();
m_stack.pop_back();
break;
}
case Instruction::SLOAD:
m_stack.back() = _ext.store(m_stack.back());
break;
case Instruction::SSTORE:
_ext.setStore(m_stack.back(), m_stack[m_stack.size() - 2]);
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::JUMP:
nextPC = m_stack.back();
if (!m_jumpDests.count(nextPC))
BOOST_THROW_EXCEPTION(BadJumpDestination());
m_stack.pop_back();
break;
case Instruction::JUMPI:
if (m_stack[m_stack.size() - 2])
{
nextPC = m_stack.back();
if (!m_jumpDests.count(nextPC))
BOOST_THROW_EXCEPTION(BadJumpDestination());
}
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::PC:
m_stack.push_back(m_curPC);
break;
case Instruction::MSIZE:
m_stack.push_back(m_temp.size());
break;
case Instruction::GAS:
m_stack.push_back(m_gas);
break;
case Instruction::JUMPDEST:
break;
/* case Instruction::LOG0:
_ext.log({}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
break;
case Instruction::LOG1:
_ext.log({m_stack[m_stack.size() - 1]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 2], (unsigned)m_stack[m_stack.size() - 3]));
break;
case Instruction::LOG2:
_ext.log({m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 3], (unsigned)m_stack[m_stack.size() - 4]));
break;
case Instruction::LOG3:
_ext.log({m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 3]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 4], (unsigned)m_stack[m_stack.size() - 5]));
break;
case Instruction::LOG4:
_ext.log({m_stack[m_stack.size() - 1], m_stack[m_stack.size() - 2], m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 5], (unsigned)m_stack[m_stack.size() - 6]));
break;*/
case Instruction::LOG0:
_ext.log({}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG1:
_ext.log({m_stack[m_stack.size() - 3]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG2:
_ext.log({m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG3:
_ext.log({m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::LOG4:
_ext.log({m_stack[m_stack.size() - 3], m_stack[m_stack.size() - 4], m_stack[m_stack.size() - 5], m_stack[m_stack.size() - 6]}, bytesConstRef(m_temp.data() + (unsigned)m_stack[m_stack.size() - 1], (unsigned)m_stack[m_stack.size() - 2]));
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
m_stack.pop_back();
break;
case Instruction::CREATE:
{
u256 endowment = m_stack.back();
m_stack.pop_back();
unsigned initOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned initSize = (unsigned)m_stack.back();
m_stack.pop_back();
if (_ext.balance(_ext.myAddress) >= endowment && _ext.depth < 1024)
{
_ext.subBalance(endowment);
m_stack.push_back((u160)_ext.create(endowment, m_gas, bytesConstRef(m_temp.data() + initOff, initSize), _onOp));
}
else
m_stack.push_back(0);
break;
}
case Instruction::CALL:
case Instruction::CALLCODE:
{
u256 gas = m_stack.back();
m_stack.pop_back();
Address receiveAddress = asAddress(m_stack.back());
m_stack.pop_back();
u256 value = m_stack.back();
m_stack.pop_back();
unsigned inOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned inSize = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned outOff = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned outSize = (unsigned)m_stack.back();
m_stack.pop_back();
if (_ext.balance(_ext.myAddress) >= value && _ext.depth < 1024)
{
_ext.subBalance(value);
m_stack.push_back(_ext.call(inst == Instruction::CALL ? receiveAddress : _ext.myAddress, value, bytesConstRef(m_temp.data() + inOff, inSize), gas, bytesRef(m_temp.data() + outOff, outSize), _onOp, {}, receiveAddress));
}
else
m_stack.push_back(0);
m_gas += gas;
break;
}
case Instruction::RETURN:
{
unsigned b = (unsigned)m_stack.back();
m_stack.pop_back();
unsigned s = (unsigned)m_stack.back();
m_stack.pop_back();
return bytesConstRef(m_temp.data() + b, s);
}
case Instruction::SUICIDE:
{
Address dest = asAddress(m_stack.back());
_ext.suicide(dest);
// ...follow through to...
}
case Instruction::STOP:
return bytesConstRef();
}
}
if (_steps == (uint64_t)-1)
BOOST_THROW_EXCEPTION(StepsDone());
return bytesConstRef();
}
} }
} }

8
libevmcore/Instruction.h

@ -215,6 +215,14 @@ inline Instruction swapInstruction(unsigned _number)
return Instruction(unsigned(Instruction::SWAP1) + _number - 1); return Instruction(unsigned(Instruction::SWAP1) + _number - 1);
} }
/// @returns the LOG<_number> instruction
inline Instruction logInstruction(unsigned _number)
{
if (asserts(_number <= 4))
BOOST_THROW_EXCEPTION(InvalidOpcode() << errinfo_comment("Invalid LOG instruction requested."));
return Instruction(unsigned(Instruction::LOG0) + _number);
}
/// Information structure for a particular instruction. /// Information structure for a particular instruction.
struct InstructionInfo struct InstructionInfo
{ {

10
libjsqrc/CMakeLists.txt

@ -12,4 +12,14 @@ qt5_add_resources(JSQRC js.qrc)
add_library(jsqrc STATIC ${JSQRC}) add_library(jsqrc STATIC ${JSQRC})
target_link_libraries(jsqrc Qt5::Core) target_link_libraries(jsqrc Qt5::Core)
if (ETH_NODE AND ETH_NPM)
add_custom_target(ethereumjs)
add_custom_command(TARGET ethereumjs
POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND bash compilejs.sh ${ETH_NPM_DIRECTORY} ${ETH_NODE_DIRECTORY}
)
add_dependencies(jsqrc ethereumjs)
endif()
install( TARGETS jsqrc ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( TARGETS jsqrc ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )

7
libjsqrc/compilejs.sh

@ -0,0 +1,7 @@
#!/bin/bash
cd ethereumjs
export PATH=$PATH:$1:$2
npm install
npm run-script build

4
libjsqrc/ethereumjs/.travis.yml

@ -8,4 +8,6 @@ before_script:
script: script:
- "jshint *.js lib" - "jshint *.js lib"
after_script: after_script:
- npm run-script gulp - npm run-script build
- npm test

24
libjsqrc/ethereumjs/README.md

@ -50,13 +50,35 @@ web3.eth.coinbase.then(function(result){
For another example see `example/index.html`. For another example see `example/index.html`.
## Contribute!
### Requirements
* Node.js
* npm
* gulp (build)
* mocha (tests)
```bash
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
sudo apt-get install nodejs-legacy
```
## Building ## Building
* `gulp build` ```bash (gulp)
npm run-script build
```
### Testing ### Testing
```bash (mocha)
npm test
```
**Please note this repo is in it's early stage.** **Please note this repo is in it's early stage.**
If you'd like to run a WebSocket ethereum node check out If you'd like to run a WebSocket ethereum node check out

152
libjsqrc/ethereumjs/dist/ethereum.js

@ -93,6 +93,12 @@ var setupInputTypes = function () {
} }
var padding = calcPadding(type, expected); var padding = calcPadding(type, expected);
if (padding > 32)
return false; // not allowed to be so big.
padding = 32; // override as per the new ABI.
if (prefix === "string")
return web3.fromAscii(value, padding).substr(2);
if (typeof value === "number") if (typeof value === "number")
value = value.toString(16); value = value.toString(16);
else if (typeof value === "string") else if (typeof value === "string")
@ -111,6 +117,8 @@ var setupInputTypes = function () {
return false; return false;
} }
padding = 32; //override as per the new ABI.
return padLeft(formatter ? formatter(value) : value, padding * 2); return padLeft(formatter ? formatter(value) : value, padding * 2);
}; };
}; };
@ -141,7 +149,6 @@ var toAbiInput = function (json, methodName, params) {
return; return;
} }
bytes = "0x" + padLeft(index.toString(16), 2);
var method = json[index]; var method = json[index];
for (var i = 0; i < method.inputs.length; i++) { for (var i = 0; i < method.inputs.length; i++) {
@ -167,12 +174,16 @@ var setupOutputTypes = function () {
} }
var padding = calcPadding(type, expected); var padding = calcPadding(type, expected);
if (padding > 32)
return -1; // not allowed to be so big.
padding = 32; // override as per the new ABI.
return padding * 2; return padding * 2;
}; };
}; };
var namedType = function (name, padding) { var namedType = function (name, padding) {
return function (type) { return function (type) {
padding = 32; // override as per the new ABI.
return name === type ? padding * 2 : -1; return name === type ? padding * 2 : -1;
}; };
}; };
@ -260,11 +271,25 @@ var outputParser = function (json) {
return parser; return parser;
}; };
var methodSignature = function (json, name) {
var method = json[findMethodIndex(json, name)];
var result = name + '(';
var inputTypes = method.inputs.map(function (inp) {
return inp.type;
});
result += inputTypes.join(',');
result += ')';
return web3.sha3(web3.fromAscii(result));
};
module.exports = { module.exports = {
inputParser: inputParser, inputParser: inputParser,
outputParser: outputParser outputParser: outputParser,
methodSignature: methodSignature
}; };
},{}],2:[function(require,module,exports){ },{}],2:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.
@ -298,7 +323,7 @@ module.exports = {
// TODO: is these line is supposed to be here? // TODO: is these line is supposed to be here?
if ("build" !== 'build') {/* if ("build" !== 'build') {/*
var WebSocket = require('ws'); // jshint ignore:line var WebSocket = require('ws'); // jshint ignore:line
var web3 = require('./main.js'); // jshint ignore:line var web3 = require('./web3'); // jshint ignore:line
*/} */}
var AutoProvider = function (userOptions) { var AutoProvider = function (userOptions) {
@ -399,6 +424,9 @@ if ("build" !== 'build') {/*
var abi = require('./abi'); var abi = require('./abi');
// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
var contract = function (address, desc) { var contract = function (address, desc) {
var inputParser = abi.inputParser(desc); var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc); var outputParser = abi.outputParser(desc);
@ -418,14 +446,18 @@ var contract = function (address, desc) {
call: function (extra) { call: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
extra.data = parsed; return abi.methodSignature(desc, method.name).then(function (signature) {
return web3.eth.call(extra).then(onSuccess); extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
return web3.eth.call(extra).then(onSuccess);
});
}, },
transact: function (extra) { transact: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
extra.data = parsed; return abi.methodSignature(desc, method.name).then(function (signature) {
return web3.eth.transact(extra).then(onSuccess); extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
return web3.eth.transact(extra).then(onSuccess);
});
} }
}; };
}; };
@ -549,6 +581,53 @@ module.exports = HttpRpcProvider;
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file qt.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var QtProvider = function() {
this.handlers = [];
var self = this;
navigator.qt.onmessage = function (message) {
self.handlers.forEach(function (handler) {
handler.call(self, JSON.parse(message.data));
});
};
};
QtProvider.prototype.send = function(payload) {
navigator.qt.postMessage(JSON.stringify(payload));
};
Object.defineProperty(QtProvider.prototype, "onmessage", {
set: function(handler) {
this.handlers.push(handler);
}
});
module.exports = QtProvider;
},{}],6:[function(require,module,exports){
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file main.js /** @file main.js
* @authors: * @authors:
* Jeffrey Wilcke <jeff@ethdev.com> * Jeffrey Wilcke <jeff@ethdev.com>
@ -791,7 +870,7 @@ var web3 = {
}, },
fromAscii: function(str, pad) { fromAscii: function(str, pad) {
pad = pad === undefined ? 32 : pad; pad = pad === undefined ? 0 : pad;
var hex = this.toHex(str); var hex = this.toHex(str);
while(hex.length < pad*2) while(hex.length < pad*2)
hex += "00"; hex += "00";
@ -1040,54 +1119,8 @@ function messageHandler(data) {
} }
} }
module.exports = web3; if (typeof(module) !== "undefined")
module.exports = web3;
},{}],6:[function(require,module,exports){
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file qt.js
* @authors:
* Jeffrey Wilcke <jeff@ethdev.com>
* Marek Kotewicz <marek@ethdev.com>
* @date 2014
*/
var QtProvider = function() {
this.handlers = [];
var self = this;
navigator.qt.onmessage = function (message) {
self.handlers.forEach(function (handler) {
handler.call(self, JSON.parse(message.data));
});
};
};
QtProvider.prototype.send = function(payload) {
navigator.qt.postMessage(JSON.stringify(payload));
};
Object.defineProperty(QtProvider.prototype, "onmessage", {
set: function(handler) {
this.handlers.push(handler);
}
});
module.exports = QtProvider;
},{}],7:[function(require,module,exports){ },{}],7:[function(require,module,exports){
/* /*
@ -1166,10 +1199,11 @@ Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
set: function(provider) { this.onMessage(provider); } set: function(provider) { this.onMessage(provider); }
}); });
module.exports = WebSocketProvider; if (typeof(module) !== "undefined")
module.exports = WebSocketProvider;
},{}],"web3":[function(require,module,exports){ },{}],"web3":[function(require,module,exports){
var web3 = require('./lib/main'); var web3 = require('./lib/web3');
web3.providers.WebSocketProvider = require('./lib/websocket'); web3.providers.WebSocketProvider = require('./lib/websocket');
web3.providers.HttpRpcProvider = require('./lib/httprpc'); web3.providers.HttpRpcProvider = require('./lib/httprpc');
web3.providers.QtProvider = require('./lib/qt'); web3.providers.QtProvider = require('./lib/qt');
@ -1178,7 +1212,7 @@ web3.contract = require('./lib/contract');
module.exports = web3; module.exports = web3;
},{"./lib/autoprovider":2,"./lib/contract":3,"./lib/httprpc":4,"./lib/main":5,"./lib/qt":6,"./lib/websocket":7}]},{},["web3"]) },{"./lib/autoprovider":2,"./lib/contract":3,"./lib/httprpc":4,"./lib/qt":5,"./lib/web3":6,"./lib/websocket":7}]},{},["web3"])
//# sourceMappingURL=ethereum.js.map //# sourceMappingURL=ethereum.js.map

16
libjsqrc/ethereumjs/dist/ethereum.js.map

File diff suppressed because one or more lines are too long

2
libjsqrc/ethereumjs/dist/ethereum.min.js

File diff suppressed because one or more lines are too long

2
libjsqrc/ethereumjs/example/contract.html

@ -50,7 +50,7 @@
function callExampleContract() { function callExampleContract() {
// this should be generated by ethereum // this should be generated by ethereum
var param = document.getElementById('value').value; var param = parseInt(document.getElementById('value').value);
// call the contract // call the contract
contract.multiply(param).call().then(function(res) { contract.multiply(param).call().then(function(res) {

2
libjsqrc/ethereumjs/index.js

@ -1,4 +1,4 @@
var web3 = require('./lib/main'); var web3 = require('./lib/web3');
web3.providers.WebSocketProvider = require('./lib/websocket'); web3.providers.WebSocketProvider = require('./lib/websocket');
web3.providers.HttpRpcProvider = require('./lib/httprpc'); web3.providers.HttpRpcProvider = require('./lib/httprpc');
web3.providers.QtProvider = require('./lib/qt'); web3.providers.QtProvider = require('./lib/qt');

5
libjsqrc/ethereumjs/index_qt.js

@ -1,5 +0,0 @@
var web3 = require('./lib/main');
web3.providers.QtProvider = require('./lib/qt');
web3.contract = require('./lib/contract');
module.exports = web3;

29
libjsqrc/ethereumjs/lib/abi.js

@ -92,6 +92,12 @@ var setupInputTypes = function () {
} }
var padding = calcPadding(type, expected); var padding = calcPadding(type, expected);
if (padding > 32)
return false; // not allowed to be so big.
padding = 32; // override as per the new ABI.
if (prefix === "string")
return web3.fromAscii(value, padding).substr(2);
if (typeof value === "number") if (typeof value === "number")
value = value.toString(16); value = value.toString(16);
else if (typeof value === "string") else if (typeof value === "string")
@ -110,6 +116,8 @@ var setupInputTypes = function () {
return false; return false;
} }
padding = 32; //override as per the new ABI.
return padLeft(formatter ? formatter(value) : value, padding * 2); return padLeft(formatter ? formatter(value) : value, padding * 2);
}; };
}; };
@ -140,7 +148,6 @@ var toAbiInput = function (json, methodName, params) {
return; return;
} }
bytes = "0x" + padLeft(index.toString(16), 2);
var method = json[index]; var method = json[index];
for (var i = 0; i < method.inputs.length; i++) { for (var i = 0; i < method.inputs.length; i++) {
@ -166,12 +173,16 @@ var setupOutputTypes = function () {
} }
var padding = calcPadding(type, expected); var padding = calcPadding(type, expected);
if (padding > 32)
return -1; // not allowed to be so big.
padding = 32; // override as per the new ABI.
return padding * 2; return padding * 2;
}; };
}; };
var namedType = function (name, padding) { var namedType = function (name, padding) {
return function (type) { return function (type) {
padding = 32; // override as per the new ABI.
return name === type ? padding * 2 : -1; return name === type ? padding * 2 : -1;
}; };
}; };
@ -259,7 +270,21 @@ var outputParser = function (json) {
return parser; return parser;
}; };
var methodSignature = function (json, name) {
var method = json[findMethodIndex(json, name)];
var result = name + '(';
var inputTypes = method.inputs.map(function (inp) {
return inp.type;
});
result += inputTypes.join(',');
result += ')';
return web3.sha3(web3.fromAscii(result));
};
module.exports = { module.exports = {
inputParser: inputParser, inputParser: inputParser,
outputParser: outputParser outputParser: outputParser,
methodSignature: methodSignature
}; };

2
libjsqrc/ethereumjs/lib/autoprovider.js

@ -30,7 +30,7 @@
// TODO: is these line is supposed to be here? // TODO: is these line is supposed to be here?
if (process.env.NODE_ENV !== 'build') { if (process.env.NODE_ENV !== 'build') {
var WebSocket = require('ws'); // jshint ignore:line var WebSocket = require('ws'); // jshint ignore:line
var web3 = require('./main.js'); // jshint ignore:line var web3 = require('./web3'); // jshint ignore:line
} }
var AutoProvider = function (userOptions) { var AutoProvider = function (userOptions) {

15
libjsqrc/ethereumjs/lib/contract.js

@ -27,6 +27,9 @@ if (process.env.NODE_ENV !== 'build') {
var abi = require('./abi'); var abi = require('./abi');
// method signature length in bytes
var ETH_METHOD_SIGNATURE_LENGTH = 4;
var contract = function (address, desc) { var contract = function (address, desc) {
var inputParser = abi.inputParser(desc); var inputParser = abi.inputParser(desc);
var outputParser = abi.outputParser(desc); var outputParser = abi.outputParser(desc);
@ -46,14 +49,18 @@ var contract = function (address, desc) {
call: function (extra) { call: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
extra.data = parsed; return abi.methodSignature(desc, method.name).then(function (signature) {
return web3.eth.call(extra).then(onSuccess); extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
return web3.eth.call(extra).then(onSuccess);
});
}, },
transact: function (extra) { transact: function (extra) {
extra = extra || {}; extra = extra || {};
extra.to = address; extra.to = address;
extra.data = parsed; return abi.methodSignature(desc, method.name).then(function (signature) {
return web3.eth.transact(extra).then(onSuccess); extra.data = signature.slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2) + parsed;
return web3.eth.transact(extra).then(onSuccess);
});
} }
}; };
}; };

5
libjsqrc/ethereumjs/lib/main.js → libjsqrc/ethereumjs/lib/web3.js

@ -256,7 +256,7 @@ var web3 = {
}, },
fromAscii: function(str, pad) { fromAscii: function(str, pad) {
pad = pad === undefined ? 32 : pad; pad = pad === undefined ? 0 : pad;
var hex = this.toHex(str); var hex = this.toHex(str);
while(hex.length < pad*2) while(hex.length < pad*2)
hex += "00"; hex += "00";
@ -505,4 +505,5 @@ function messageHandler(data) {
} }
} }
module.exports = web3; if (typeof(module) !== "undefined")
module.exports = web3;

3
libjsqrc/ethereumjs/lib/websocket.js

@ -74,4 +74,5 @@ Object.defineProperty(WebSocketProvider.prototype, "onmessage", {
set: function(provider) { this.onMessage(provider); } set: function(provider) { this.onMessage(provider); }
}); });
module.exports = WebSocketProvider; if (typeof(module) !== "undefined")
module.exports = WebSocketProvider;

8
libjsqrc/ethereumjs/package.json

@ -1,7 +1,7 @@
{ {
"name": "ethereum.js", "name": "ethereum.js",
"namespace": "ethereum", "namespace": "ethereum",
"version": "0.0.5", "version": "0.0.7",
"description": "Ethereum Compatible JavaScript API", "description": "Ethereum Compatible JavaScript API",
"main": "./index.js", "main": "./index.js",
"directories": { "directories": {
@ -25,12 +25,14 @@
"jshint": ">=2.5.0", "jshint": ">=2.5.0",
"uglifyify": "^2.6.0", "uglifyify": "^2.6.0",
"unreachable-branch-transform": "^0.1.0", "unreachable-branch-transform": "^0.1.0",
"vinyl-source-stream": "^1.0.0" "vinyl-source-stream": "^1.0.0",
"mocha": ">=2.1.0"
}, },
"scripts": { "scripts": {
"build": "gulp", "build": "gulp",
"watch": "gulp watch", "watch": "gulp watch",
"lint": "gulp lint" "lint": "gulp lint",
"test": "mocha"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

37
libjsqrc/ethereumjs/test/abi.parsers.js

@ -0,0 +1,37 @@
var assert = require('assert');
var abi = require('../lib/abi.js');
describe('abi', function() {
describe('inputParser', function() {
it('should parse ...', function() {
var desc = [{
"name": "multiply",
"inputs": [
{
"name": "a",
"type": "uint256"
}
],
"outputs": [
{
"name": "d",
"type": "uint256"
}
]
}];
var iParser = abi.inputParser(desc);
assert.equal(iParser.multiply(1), "0x000000000000000000000000000000000000000000000000000000000000000001");
});
});
describe('outputParser', function() {
it('parse ...', function() {
});
});
});

18
libjsqrc/ethereumjs/test/db.methods.js

@ -0,0 +1,18 @@
require('es6-promise').polyfill();
var assert = require('assert');
var web3 = require('../index.js');
var u = require('./utils.js');
web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider
describe('web3', function() {
describe('db', function() {
it('should have all methods implemented', function() {
u.methodExists(web3.db, 'put');
u.methodExists(web3.db, 'get');
u.methodExists(web3.db, 'putString');
u.methodExists(web3.db, 'getString');
});
});
});

42
libjsqrc/ethereumjs/test/eth.methods.js

@ -0,0 +1,42 @@
require('es6-promise').polyfill();
var assert = require('assert');
var web3 = require('../index.js');
var u = require('./utils.js');
web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider
describe('web3', function() {
describe('eth', function() {
it('should have all methods implemented', function() {
u.methodExists(web3.eth, 'balanceAt');
u.methodExists(web3.eth, 'stateAt');
u.methodExists(web3.eth, 'storageAt');
u.methodExists(web3.eth, 'countAt');
u.methodExists(web3.eth, 'codeAt');
u.methodExists(web3.eth, 'transact');
u.methodExists(web3.eth, 'call');
u.methodExists(web3.eth, 'block');
u.methodExists(web3.eth, 'transaction');
u.methodExists(web3.eth, 'uncle');
u.methodExists(web3.eth, 'compilers');
u.methodExists(web3.eth, 'lll');
u.methodExists(web3.eth, 'solidity');
u.methodExists(web3.eth, 'serpent');
u.methodExists(web3.eth, 'logs');
});
it('should have all properties implemented', function () {
u.propertyExists(web3.eth, 'coinbase');
u.propertyExists(web3.eth, 'listening');
u.propertyExists(web3.eth, 'mining');
u.propertyExists(web3.eth, 'gasPrice');
u.propertyExists(web3.eth, 'account');
u.propertyExists(web3.eth, 'accounts');
u.propertyExists(web3.eth, 'peerCount');
u.propertyExists(web3.eth, 'defaultBlock');
u.propertyExists(web3.eth, 'number');
});
});
});

2
libjsqrc/ethereumjs/test/mocha.opts

@ -0,0 +1,2 @@
--reporter Spec

19
libjsqrc/ethereumjs/test/shh.methods.js

@ -0,0 +1,19 @@
require('es6-promise').polyfill();
var assert = require('assert');
var web3 = require('../index.js');
var u = require('./utils.js');
web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider
describe('web3', function() {
describe('shh', function() {
it('should have all methods implemented', function() {
u.methodExists(web3.shh, 'post');
u.methodExists(web3.shh, 'newIdentity');
u.methodExists(web3.shh, 'haveIdentity');
u.methodExists(web3.shh, 'newGroup');
u.methodExists(web3.shh, 'addToGroup');
});
});
});

15
libjsqrc/ethereumjs/test/utils.js

@ -0,0 +1,15 @@
var assert = require('assert');
var methodExists = function (object, method) {
assert.equal('function', typeof object[method], 'method ' + method + ' is not implemented');
};
var propertyExists = function (object, property) {
assert.equal('object', typeof object[property], 'property ' + property + ' is not implemented');
};
module.exports = {
methodExists: methodExists,
propertyExists: propertyExists
};

18
libjsqrc/ethereumjs/test/web3.methods.js

@ -0,0 +1,18 @@
require('es6-promise').polyfill();
var assert = require('assert');
var web3 = require('../index.js');
var u = require('./utils.js');
web3.setProvider(new web3.providers.WebSocketProvider('http://localhost:8080')); // TODO: create some mock provider
describe('web3', function() {
it('should have all methods implemented', function() {
u.methodExists(web3, 'sha3');
u.methodExists(web3, 'toAscii');
u.methodExists(web3, 'fromAscii');
u.methodExists(web3, 'toFixed');
u.methodExists(web3, 'fromFixed');
u.methodExists(web3, 'offset');
});
});

12
libjsqrc/js.qrc

@ -1,7 +1,7 @@
<RCC version="1.0"> <RCC>
<qresource prefix="/js"> <qresource prefix="/js">
<file>es6-promise-2.0.0.js</file> <file>es6-promise-2.0.0.js</file>
<file>setup.js</file> <file>setup.js</file>
<file alias="ethereum.js">ethereumjs/dist/ethereum.js</file> <file alias="webthree.js">ethereumjs/dist/ethereum.js</file>
</qresource> </qresource>
</RCC> </RCC>

2
libp2p/Host.cpp

@ -558,7 +558,7 @@ void Host::prunePeers()
for (auto i: m_peers) for (auto i: m_peers)
if (!dc.count(i.first)) if (!dc.count(i.first))
if (auto p = i.second.lock()) if (auto p = i.second.lock())
if (/*(m_mode != NodeMode::Host || p->m_caps != 0x01) &&*/ chrono::steady_clock::now() > p->m_connect + chrono::milliseconds(old)) // don't throw off new peers; peer-servers should never kick off other peer-servers. if (chrono::steady_clock::now() > p->m_connect + chrono::milliseconds(old)) // don't throw off new peers; peer-servers should never kick off other peer-servers.
{ {
++agedPeers; ++agedPeers;
if ((!worst || p->rating() < worst->rating() || (p->rating() == worst->rating() && p->m_connect > worst->m_connect))) // kill older ones if ((!worst || p->rating() < worst->rating() || (p->rating() == worst->rating() && p->m_connect > worst->m_connect))) // kill older ones

2
libqethereum/CMakeLists.txt → libqwebthree/CMakeLists.txt

@ -15,7 +15,7 @@ aux_source_directory(. SRC_LIST)
include_directories(${JSON_RPC_CPP_INCLUDE_DIRS}) include_directories(${JSON_RPC_CPP_INCLUDE_DIRS})
include_directories(..) include_directories(..)
set(EXECUTABLE qethereum) set(EXECUTABLE qwebthree)
file(GLOB HEADERS "*.h") file(GLOB HEADERS "*.h")

30
libqethereum/QEthereum.cpp → libqwebthree/QWebThree.cpp

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file QEthereum.cpp /** @file QWebThree.cpp
* @authors: * @authors:
* Gav Wood <i@gavwood.com> * Gav Wood <i@gavwood.com>
* Marek Kotewicz <marek@ethdev.com> * Marek Kotewicz <marek@ethdev.com>
@ -22,7 +22,7 @@
*/ */
#include <QtCore/QtCore> #include <QtCore/QtCore>
#include "QEthereum.h" #include "QWebThree.h"
using namespace std; using namespace std;
@ -134,24 +134,7 @@ void QWebThree::onDataProcessed(QString _json, QString _addInfo)
if (!_addInfo.compare("internal")) if (!_addInfo.compare("internal"))
return; return;
if (!_addInfo.compare("eth_changed")) if (!_addInfo.compare("shh_changed") || !_addInfo.compare("eth_changed"))
{
QJsonArray resultsArray = QJsonDocument::fromJson(_json.toUtf8()).array();
for (int i = 0; i < resultsArray.size(); i++)
{
QJsonObject elem = resultsArray[i].toObject();
if (elem.contains("result") && elem["result"].toBool() == true)
{
QJsonObject res;
res["_event"] = _addInfo;
res["_id"] = (int)m_watches[i]; // we can do that couse poll is synchronous
response(QString::fromUtf8(QJsonDocument(res).toJson()));
}
}
return;
}
if (!_addInfo.compare("shh_changed"))
{ {
QJsonArray resultsArray = QJsonDocument::fromJson(_json.toUtf8()).array(); QJsonArray resultsArray = QJsonDocument::fromJson(_json.toUtf8()).array();
for (int i = 0; i < resultsArray.size(); i++) for (int i = 0; i < resultsArray.size(); i++)
@ -161,7 +144,12 @@ void QWebThree::onDataProcessed(QString _json, QString _addInfo)
{ {
QJsonObject res; QJsonObject res;
res["_event"] = _addInfo; res["_event"] = _addInfo;
res["_id"] = (int)m_shhWatches[i];
if (!_addInfo.compare("shh_changed"))
res["_id"] = (int)m_shhWatches[i]; // we can do that couse poll is synchronous
else
res["_id"] = (int)m_watches[i];
res["data"] = elem["result"].toArray(); res["data"] = elem["result"].toArray();
response(QString::fromUtf8(QJsonDocument(res).toJson())); response(QString::fromUtf8(QJsonDocument(res).toJson()));
} }

4
libqethereum/QEthereum.h → libqwebthree/QWebThree.h

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file QEthereum.h /** @file QWebThree.h
* @authors: * @authors:
* Gav Wood <i@gavwood.com> * Gav Wood <i@gavwood.com>
* Marek Kotewicz <marek@ethdev.com> * Marek Kotewicz <marek@ethdev.com>
@ -85,7 +85,7 @@ private:
_frame->addToJavaScriptWindowObject("_web3", qweb, QWebFrame::ScriptOwnership); \ _frame->addToJavaScriptWindowObject("_web3", qweb, QWebFrame::ScriptOwnership); \
_frame->addToJavaScriptWindowObject("env", _env, QWebFrame::QtOwnership); \ _frame->addToJavaScriptWindowObject("env", _env, QWebFrame::QtOwnership); \
_frame->evaluateJavaScript(contentsOfQResource(":/js/es6-promise-2.0.0.js")); \ _frame->evaluateJavaScript(contentsOfQResource(":/js/es6-promise-2.0.0.js")); \
_frame->evaluateJavaScript(contentsOfQResource(":/js/ethereum.js")); \ _frame->evaluateJavaScript(contentsOfQResource(":/js/webthree.js")); \
_frame->evaluateJavaScript(contentsOfQResource(":/js/setup.js")); \ _frame->evaluateJavaScript(contentsOfQResource(":/js/setup.js")); \
} }

57
libsolidity/AST.cpp

@ -27,6 +27,8 @@
#include <libsolidity/Exceptions.h> #include <libsolidity/Exceptions.h>
#include <libsolidity/AST_accept.h> #include <libsolidity/AST_accept.h>
#include <libdevcrypto/SHA3.h>
using namespace std; using namespace std;
namespace dev namespace dev
@ -50,18 +52,17 @@ void ContractDefinition::checkTypeRequirements()
function->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) for (ASTPointer<FunctionDefinition> const& f: m_definedFunctions)
if (f->isPublic() && f->getName() != getName()) if (f->isPublic() && f->getName() != getName())
exportedFunctions.push_back(f.get()); {
auto compareNames = [](FunctionDefinition const* _a, FunctionDefinition const* _b) FixedHash<4> hash(dev::sha3(f->getCanonicalSignature()));
{ auto res = exportedFunctions.insert(std::make_pair(hash,f.get()));
return _a->getName().compare(_b->getName()) < 0; solAssert(res.second, "Hash collision at Function Definition Hash calculation");
}; }
sort(exportedFunctions.begin(), exportedFunctions.end(), compareNames);
return exportedFunctions; return exportedFunctions;
} }
@ -173,7 +174,15 @@ void VariableDefinition::checkTypeRequirements()
{ {
// no type declared and no previous assignment, infer the type // no type declared and no previous assignment, infer the type
m_value->checkTypeRequirements(); 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 // compound assignment
m_rightHandSide->checkTypeRequirements(); m_rightHandSide->checkTypeRequirements();
TypePointer resultType = Type::binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator), TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
m_type, m_rightHandSide->getType()); m_rightHandSide->getType());
if (!resultType || *resultType != *m_type) 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() void ExpressionStatement::checkTypeRequirements()
{ {
m_expression->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) void Expression::expectType(Type const& _expectedType)
@ -227,8 +242,8 @@ void UnaryOperation::checkTypeRequirements()
m_subExpression->checkTypeRequirements(); m_subExpression->checkTypeRequirements();
if (m_operator == Token::Value::INC || m_operator == Token::Value::DEC || m_operator == Token::Value::DELETE) if (m_operator == Token::Value::INC || m_operator == Token::Value::DEC || m_operator == Token::Value::DELETE)
m_subExpression->requireLValue(); m_subExpression->requireLValue();
m_type = m_subExpression->getType(); m_type = m_subExpression->getType()->unaryOperatorResult(m_operator);
if (!m_type->acceptsUnaryOperator(m_operator)) if (!m_type)
BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type.")); BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
} }
@ -236,7 +251,7 @@ void BinaryOperation::checkTypeRequirements()
{ {
m_left->checkTypeRequirements(); m_left->checkTypeRequirements();
m_right->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) if (!m_commonType)
BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_operator)) + BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_operator)) +
" not compatible with types " + " not compatible with types " +
@ -300,7 +315,7 @@ void NewExpression::checkTypeRequirements()
m_contract = dynamic_cast<ContractDefinition const*>(m_contractName->getReferencedDeclaration()); m_contract = dynamic_cast<ContractDefinition const*>(m_contractName->getReferencedDeclaration());
if (!m_contract) if (!m_contract)
BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a 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; m_type = type;
TypePointers const& parameterTypes = type->getConstructorType()->getParameterTypes(); TypePointers const& parameterTypes = type->getConstructorType()->getParameterTypes();
if (parameterTypes.size() != m_arguments.size()) if (parameterTypes.size() != m_arguments.size())
@ -351,7 +366,7 @@ void Identifier::checkTypeRequirements()
if (structDef) if (structDef)
{ {
// note that we do not have a struct type here // 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; return;
} }
FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(m_referencedDeclaration); 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. // 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 // Calling a function (e.g. function(12), otherContract.function(34)) does not do a type
// conversion. // conversion.
m_type = make_shared<FunctionType const>(*functionDef); m_type = make_shared<FunctionType>(*functionDef);
return; return;
} }
ContractDefinition const* contractDef = dynamic_cast<ContractDefinition const*>(m_referencedDeclaration); ContractDefinition const* contractDef = dynamic_cast<ContractDefinition const*>(m_referencedDeclaration);
if (contractDef) if (contractDef)
{ {
m_type = make_shared<TypeType const>(make_shared<ContractType>(*contractDef)); m_type = make_shared<TypeType>(make_shared<ContractType>(*contractDef));
return; return;
} }
MagicVariableDeclaration const* magicVariable = dynamic_cast<MagicVariableDeclaration const*>(m_referencedDeclaration); MagicVariableDeclaration const* magicVariable = dynamic_cast<MagicVariableDeclaration const*>(m_referencedDeclaration);
@ -380,14 +395,14 @@ void Identifier::checkTypeRequirements()
void ElementaryTypeNameExpression::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() void Literal::checkTypeRequirements()
{ {
m_type = Type::forLiteral(*this); m_type = Type::forLiteral(*this);
if (!m_type) 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 /// Can contain a nullptr in which case indicates absence of documentation
ASTPointer<ASTString> const& getDocumentation() const { return m_documentation; } ASTPointer<ASTString> const& getDocumentation() const { return m_documentation; }
/// Returns the functions that make up the calling interface in the intended order. /// @returns a map of canonical function signatures to FunctionDefinitions
std::vector<FunctionDefinition const*> getInterfaceFunctions() const; /// 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 /// Returns the constructor or nullptr if no constructor was specified
FunctionDefinition const* getConstructor() const; FunctionDefinition const* getConstructor() const;

1
libsolidity/CMakeLists.txt

@ -28,6 +28,7 @@ endif()
target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES}) target_link_libraries(${EXECUTABLE} ${JSONCPP_LIBRARIES})
target_link_libraries(${EXECUTABLE} evmcore) target_link_libraries(${EXECUTABLE} evmcore)
target_link_libraries(${EXECUTABLE} devcore) target_link_libraries(${EXECUTABLE} devcore)
target_link_libraries(${EXECUTABLE} devcrypto)
install( TARGETS ${EXECUTABLE} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib ) install( TARGETS ${EXECUTABLE} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib )
install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} ) install( FILES ${HEADERS} DESTINATION include/${EXECUTABLE} )

78
libsolidity/Compiler.cpp

@ -95,12 +95,12 @@ void Compiler::appendConstructorCall(FunctionDefinition const& _constructor)
// copy constructor arguments from code to memory and then to stack, they are supplied after the actual program // copy constructor arguments from code to memory and then to stack, they are supplied after the actual program
unsigned argumentSize = 0; unsigned argumentSize = 0;
for (ASTPointer<VariableDeclaration> const& var: _constructor.getParameters()) for (ASTPointer<VariableDeclaration> const& var: _constructor.getParameters())
argumentSize += var->getType()->getCalldataEncodedSize(); argumentSize += CompilerUtils::getPaddedSize(var->getType()->getCalldataEncodedSize());
if (argumentSize > 0) if (argumentSize > 0)
{ {
m_context << u256(argumentSize); m_context << u256(argumentSize);
m_context.appendProgramSize(); 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; m_context << eth::Instruction::CODECOPY;
appendCalldataUnpacker(_constructor, true); appendCalldataUnpacker(_constructor, true);
} }
@ -118,35 +118,27 @@ set<FunctionDefinition const*> Compiler::getFunctionsNeededByConstructor(Functio
void Compiler::appendFunctionSelector(ContractDefinition const& _contract) void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
{ {
vector<FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions(); map<FixedHash<4>, FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions();
vector<eth::AssemblyItem> callDataUnpackerEntryPoints; map<FixedHash<4>, const eth::AssemblyItem> callDataUnpackerEntryPoints;
if (interfaceFunctions.size() > 255) // retrieve the function signature hash from the calldata
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 255 public functions for contract.")); m_context << u256(1) << u256(0);
CompilerUtils(m_context).loadFromMemory(0, 4, false, true);
// 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 // stack now is: 1 0 <funhash>
// error prone (due to the optimizer and variable length tag addresses) // for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
m_context << u256(1) << u256(0) // some constants for (auto const& it: interfaceFunctions)
<< 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)
{ {
callDataUnpackerEntryPoints.push_back(m_context.newTag()); callDataUnpackerEntryPoints.insert(std::make_pair(it.first, m_context.newTag()));
m_context << eth::dupInstruction(2) << eth::dupInstruction(2) << eth::Instruction::EQ; m_context << eth::dupInstruction(1) << u256(FixedHash<4>::Arith(it.first)) << eth::Instruction::EQ;
m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.back()); m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.at(it.first));
if (funid < interfaceFunctions.size() - 1)
m_context << eth::dupInstruction(4) << eth::Instruction::ADD;
} }
m_context << eth::Instruction::STOP; // function not found 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]; FunctionDefinition const& function = *it.second;
m_context << callDataUnpackerEntryPoints[funid]; m_context << callDataUnpackerEntryPoints.at(it.first);
eth::AssemblyItem returnTag = m_context.pushNewTag(); eth::AssemblyItem returnTag = m_context.pushNewTag();
appendCalldataUnpacker(function); appendCalldataUnpacker(function);
m_context.appendJumpTo(m_context.getFunctionEntryLabel(function)); m_context.appendJumpTo(m_context.getFunctionEntryLabel(function));
@ -158,18 +150,19 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory) unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory)
{ {
// We do not check the calldata size, everything is zero-padded. // 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 //@todo this can be done more efficiently, saving some CALLDATALOAD calls
for (ASTPointer<VariableDeclaration> const& var: _function.getParameters()) for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
{ {
unsigned const numBytes = var->getType()->getCalldataEncodedSize(); unsigned const c_numBytes = var->getType()->getCalldataEncodedSize();
if (numBytes > 32) if (c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError() BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(var->getLocation()) << errinfo_sourceLocation(var->getLocation())
<< errinfo_comment("Type " + var->getType()->toString() + " not yet supported.")); << errinfo_comment("Type " + var->getType()->toString() + " not yet supported."));
bool leftAligned = var->getType()->getCategory() == Type::Category::STRING; bool const c_leftAligned = var->getType()->getCategory() == Type::Category::STRING;
CompilerUtils(m_context).loadFromMemory(dataOffset, numBytes, leftAligned, !_fromMemory); bool const c_padToWords = true;
dataOffset += numBytes; dataOffset += CompilerUtils(m_context).loadFromMemory(dataOffset, c_numBytes, c_leftAligned,
!_fromMemory, c_padToWords);
} }
return dataOffset; return dataOffset;
} }
@ -189,10 +182,11 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function)
<< errinfo_sourceLocation(parameters[i]->getLocation()) << errinfo_sourceLocation(parameters[i]->getLocation())
<< errinfo_comment("Type " + paramType.toString() + " not yet supported.")); << errinfo_comment("Type " + paramType.toString() + " not yet supported."));
CompilerUtils(m_context).copyToStackTop(stackDepth, paramType); CompilerUtils(m_context).copyToStackTop(stackDepth, paramType);
bool const leftAligned = paramType.getCategory() == Type::Category::STRING; ExpressionCompiler::appendTypeConversion(m_context, paramType, paramType, true);
CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); bool const c_leftAligned = paramType.getCategory() == Type::Category::STRING;
bool const c_padToWords = true;
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, c_leftAligned, c_padToWords);
stackDepth -= paramType.getSizeOnStack(); stackDepth -= paramType.getSizeOnStack();
dataOffset += numBytes;
} }
// note that the stack is not cleaned up here // note that the stack is not cleaned up here
m_context << u256(dataOffset) << u256(0) << eth::Instruction::RETURN; m_context << u256(dataOffset) << u256(0) << eth::Instruction::RETURN;
@ -238,16 +232,16 @@ bool Compiler::visit(FunctionDefinition const& _function)
// Note that the fact that the return arguments are of increasing index is vital for this // Note that the fact that the return arguments are of increasing index is vital for this
// algorithm to work. // algorithm to work.
unsigned const argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters()); unsigned const c_argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters());
unsigned const returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters()); unsigned const c_returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters());
unsigned const localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables()); unsigned const c_localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables());
vector<int> stackLayout; vector<int> stackLayout;
stackLayout.push_back(returnValuesSize); // target of return address stackLayout.push_back(c_returnValuesSize); // target of return address
stackLayout += vector<int>(argumentsSize, -1); // discard all arguments stackLayout += vector<int>(c_argumentsSize, -1); // discard all arguments
for (unsigned i = 0; i < returnValuesSize; ++i) for (unsigned i = 0; i < c_returnValuesSize; ++i)
stackLayout.push_back(i); stackLayout.push_back(i);
stackLayout += vector<int>(localVariablesSize, -1); stackLayout += vector<int>(c_localVariablesSize, -1);
while (stackLayout.back() != int(stackLayout.size() - 1)) while (stackLayout.back() != int(stackLayout.size() - 1))
if (stackLayout.back() < 0) if (stackLayout.back() < 0)

13
libsolidity/CompilerStack.cpp

@ -16,6 +16,7 @@
*/ */
/** /**
* @author Christian <c@ethdev.com> * @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014 * @date 2014
* Full-stack compiler that converts a source code string to bytecode. * 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 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) if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); 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: case DocumentationType::ABI_INTERFACE:
doc = &contract.interface; doc = &contract.interface;
break; break;
case DocumentationType::ABI_SOLIDITY_INTERFACE:
doc = &contract.solidityInterface;
break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal documentation type.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal documentation type."));
} }

12
libsolidity/CompilerStack.h

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

21
libsolidity/CompilerUtils.cpp

@ -31,17 +31,23 @@ namespace dev
namespace solidity namespace solidity
{ {
void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned, bool _fromCalldata) const unsigned int CompilerUtils::dataStartOffset = 4;
unsigned CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _leftAligned,
bool _fromCalldata, bool _padToWordBoundaries)
{ {
if (_bytes == 0) if (_bytes == 0)
{ {
m_context << u256(0); m_context << u256(0);
return; return 0;
} }
eth::Instruction load = _fromCalldata ? eth::Instruction::CALLDATALOAD : eth::Instruction::MLOAD; eth::Instruction load = _fromCalldata ? eth::Instruction::CALLDATALOAD : eth::Instruction::MLOAD;
solAssert(_bytes <= 32, "Memory load of more than 32 bytes requested."); solAssert(_bytes <= 32, "Memory load of more than 32 bytes requested.");
if (_bytes == 32) if (_bytes == 32 || _padToWordBoundaries)
{
m_context << u256(_offset) << load; m_context << u256(_offset) << load;
return 32;
}
else else
{ {
// load data and add leading or trailing zeros by dividing/multiplying depending on alignment // load data and add leading or trailing zeros by dividing/multiplying depending on alignment
@ -52,21 +58,24 @@ void CompilerUtils::loadFromMemory(unsigned _offset, unsigned _bytes, bool _left
m_context << u256(_offset) << load << eth::Instruction::DIV; m_context << u256(_offset) << load << eth::Instruction::DIV;
if (_leftAligned) if (_leftAligned)
m_context << eth::Instruction::MUL; m_context << eth::Instruction::MUL;
return _bytes;
} }
} }
void CompilerUtils::storeInMemory(unsigned _offset, unsigned _bytes, bool _leftAligned) unsigned CompilerUtils::storeInMemory(unsigned _offset, unsigned _bytes, bool _leftAligned,
bool _padToWordBoundaries)
{ {
if (_bytes == 0) if (_bytes == 0)
{ {
m_context << eth::Instruction::POP; m_context << eth::Instruction::POP;
return; return 0;
} }
solAssert(_bytes <= 32, "Memory store of more than 32 bytes requested."); solAssert(_bytes <= 32, "Memory store of more than 32 bytes requested.");
if (_bytes != 32 && !_leftAligned) if (_bytes != 32 && !_leftAligned && !_padToWordBoundaries)
// shift the value accordingly before storing // shift the value accordingly before storing
m_context << (u256(1) << ((32 - _bytes) * 8)) << eth::Instruction::MUL; m_context << (u256(1) << ((32 - _bytes) * 8)) << eth::Instruction::MUL;
m_context << u256(_offset) << eth::Instruction::MSTORE; m_context << u256(_offset) << eth::Instruction::MSTORE;
return _padToWordBoundaries ? 32 : _bytes;
} }
void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable)

19
libsolidity/CompilerUtils.h

@ -40,12 +40,23 @@ public:
/// @param _bytes number of bytes to load /// @param _bytes number of bytes to load
/// @param _leftAligned if true, store left aligned on stack (otherwise right aligned) /// @param _leftAligned if true, store left aligned on stack (otherwise right aligned)
/// @param _fromCalldata if true, load from calldata, not from memory /// @param _fromCalldata if true, load from calldata, not from memory
void loadFromMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false, bool _fromCalldata = false); /// @param _padToWordBoundaries if true, assume the data is padded to word (32 byte) boundaries
/// @returns the number of bytes consumed in memory (can be different from _bytes if
/// _padToWordBoundaries is true)
unsigned loadFromMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false,
bool _fromCalldata = false, bool _padToWordBoundaries = false);
/// Stores data from stack in memory. /// Stores data from stack in memory.
/// @param _offset offset in memory /// @param _offset offset in memory
/// @param _bytes number of bytes to store /// @param _bytes number of bytes to store
/// @param _leftAligned if true, data is left aligned on stack (otherwise right aligned) /// @param _leftAligned if true, data is left aligned on stack (otherwise right aligned)
void storeInMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false); /// @param _padToWordBoundaries if true, pad the data to word (32 byte) boundaries
/// @returns the number of bytes written to memory (can be different from _bytes if
/// _padToWordBoundaries is true)
unsigned storeInMemory(unsigned _offset, unsigned _bytes = 32, bool _leftAligned = false,
bool _padToWordBoundaries = false);
/// @returns _size rounded up to the next multiple of 32 (the number of bytes occupied in the
/// padded calldata)
static unsigned getPaddedSize(unsigned _size) { return ((_size + 31) / 32) * 32; }
/// Moves the value that is at the top of the stack to a stack variable. /// Moves the value that is at the top of the stack to a stack variable.
void moveToStackVariable(VariableDeclaration const& _variable); void moveToStackVariable(VariableDeclaration const& _variable);
@ -58,10 +69,14 @@ public:
static unsigned getSizeOnStack(std::vector<T> const& _variables); static unsigned getSizeOnStack(std::vector<T> const& _variables);
static unsigned getSizeOnStack(std::vector<std::shared_ptr<Type const>> const& _variableTypes); 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: private:
CompilerContext& m_context; CompilerContext& m_context;
}; };
template <class T> template <class T>
unsigned CompilerUtils::getSizeOnStack(std::vector<T> const& _variables) unsigned CompilerUtils::getSizeOnStack(std::vector<T> const& _variables)
{ {

206
libsolidity/ExpressionCompiler.cpp

@ -41,11 +41,11 @@ void ExpressionCompiler::compileExpression(CompilerContext& _context, Expression
_expression.accept(compiler); _expression.accept(compiler);
} }
void ExpressionCompiler::appendTypeConversion(CompilerContext& _context, void ExpressionCompiler::appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack,
Type const& _typeOnStack, Type const& _targetType) Type const& _targetType, bool _cleanupNeeded)
{ {
ExpressionCompiler compiler(_context); ExpressionCompiler compiler(_context);
compiler.appendTypeConversion(_typeOnStack, _targetType); compiler.appendTypeConversion(_typeOnStack, _targetType, _cleanupNeeded);
} }
bool ExpressionCompiler::visit(Assignment const& _assignment) bool ExpressionCompiler::visit(Assignment const& _assignment)
@ -71,12 +71,20 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
return false; 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: //@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 // 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 // put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
// represents the operator // 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()) switch (_unaryOperation.getOperator())
{ {
case Token::NOT: // ! case Token::NOT: // !
@ -128,6 +136,7 @@ void ExpressionCompiler::endVisit(UnaryOperation const& _unaryOperation)
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " +
string(Token::toString(_unaryOperation.getOperator())))); string(Token::toString(_unaryOperation.getOperator()))));
} }
return false;
} }
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation) bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
@ -135,21 +144,23 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
Expression const& leftExpression = _binaryOperation.getLeftExpression(); Expression const& leftExpression = _binaryOperation.getLeftExpression();
Expression const& rightExpression = _binaryOperation.getRightExpression(); Expression const& rightExpression = _binaryOperation.getRightExpression();
Type const& commonType = _binaryOperation.getCommonType(); Type const& commonType = _binaryOperation.getCommonType();
Token::Value const op = _binaryOperation.getOperator(); Token::Value const c_op = _binaryOperation.getOperator();
if (op == Token::AND || op == Token::OR) // special case: short-circuiting if (c_op == Token::AND || c_op == Token::OR) // special case: short-circuiting
appendAndOrOperatorCode(_binaryOperation); appendAndOrOperatorCode(_binaryOperation);
else if (commonType.getCategory() == Type::Category::INTEGER_CONSTANT)
m_context << commonType.literalValue(nullptr);
else else
{ {
bool cleanupNeeded = false; bool cleanupNeeded = commonType.getCategory() == Type::Category::INTEGER &&
if (commonType.getCategory() == Type::Category::INTEGER) (Token::isCompareOp(c_op) || c_op == Token::DIV || c_op == Token::MOD);
if (Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD)
cleanupNeeded = true;
// for commutative operators, push the literal as late as possible to allow improved optimization // for commutative operators, push the literal as late as possible to allow improved optimization
//@todo this has to be extended for literal expressions auto isLiteral = [](Expression const& _e)
bool swap = (m_optimize && Token::isCommutativeOp(op) && dynamic_cast<Literal const*>(&rightExpression) {
&& !dynamic_cast<Literal const*>(&leftExpression)); return dynamic_cast<Literal const*>(&_e) || _e.getType()->getCategory() == Type::Category::INTEGER_CONSTANT;
};
bool swap = m_optimize && Token::isCommutativeOp(c_op) && isLiteral(rightExpression) && !isLiteral(leftExpression);
if (swap) if (swap)
{ {
leftExpression.accept(*this); leftExpression.accept(*this);
@ -164,10 +175,10 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
leftExpression.accept(*this); leftExpression.accept(*this);
appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded); appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded);
} }
if (Token::isCompareOp(op)) if (Token::isCompareOp(c_op))
appendCompareOperatorCode(op, commonType); appendCompareOperatorCode(c_op, commonType);
else else
appendOrdinaryBinaryOperatorCode(op, commonType); appendOrdinaryBinaryOperatorCode(c_op, commonType);
} }
// do not visit the child nodes, we already did that explicitly // do not visit the child nodes, we already did that explicitly
@ -183,13 +194,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
solAssert(_functionCall.getArguments().size() == 1, ""); solAssert(_functionCall.getArguments().size() == 1, "");
Expression const& firstArgument = *_functionCall.getArguments().front(); Expression const& firstArgument = *_functionCall.getArguments().front();
firstArgument.accept(*this); firstArgument.accept(*this);
if (firstArgument.getType()->getCategory() == Type::Category::CONTRACT && appendTypeConversion(*firstArgument.getType(), *_functionCall.getType());
_functionCall.getType()->getCategory() == Type::Category::INTEGER)
{
// explicit type conversion contract -> address, nothing to do.
}
else
appendTypeConversion(*firstArgument.getType(), *_functionCall.getType());
} }
else else
{ {
@ -239,13 +244,17 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
FunctionCallOptions options; FunctionCallOptions options;
options.bare = true; options.bare = true;
options.obtainAddress = [&]() { _functionCall.getExpression().accept(*this); }; options.obtainAddress = [&]() { _functionCall.getExpression().accept(*this); };
options.obtainValue = [&]() { arguments.front()->accept(*this); }; options.obtainValue = [&]()
appendExternalFunctionCall(FunctionType({}, {}, Location::EXTERNAL), {}, options); {
arguments.front()->accept(*this);
appendTypeConversion(*arguments.front()->getType(),
*function.getParameterTypes().front(), true);
};
appendExternalFunctionCall(FunctionType(TypePointers{}, TypePointers{}, Location::EXTERNAL), {}, options);
break; break;
} }
case Location::SUICIDE: case Location::SUICIDE:
arguments.front()->accept(*this); arguments.front()->accept(*this);
//@todo might not be necessary
appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true); appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
m_context << eth::Instruction::SUICIDE; m_context << eth::Instruction::SUICIDE;
break; break;
@ -256,6 +265,23 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
CompilerUtils(m_context).storeInMemory(0); CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::Instruction::SHA3; m_context << u256(32) << u256(0) << eth::Instruction::SHA3;
break; break;
case Location::LOG0:
case Location::LOG1:
case Location::LOG2:
case Location::LOG3:
case Location::LOG4:
{
unsigned logNumber = int(function.getLocation()) - int(Location::LOG0);
for (int arg = logNumber; arg >= 0; --arg)
{
arguments[arg]->accept(*this);
appendTypeConversion(*arguments[arg]->getType(), *function.getParameterTypes()[arg], true);
}
// @todo move this once we actually use memory
CompilerUtils(m_context).storeInMemory(0);
m_context << u256(32) << u256(0) << eth::logInstruction(logNumber);
break;
}
case Location::ECRECOVER: case Location::ECRECOVER:
case Location::SHA256: case Location::SHA256:
case Location::RIPEMD160: case Location::RIPEMD160:
@ -267,7 +293,6 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
FunctionCallOptions options; FunctionCallOptions options;
options.bare = true; options.bare = true;
options.obtainAddress = [&]() { m_context << contractAddress; }; options.obtainAddress = [&]() { m_context << contractAddress; };
options.packDensely = false;
appendExternalFunctionCall(function, arguments, options); appendExternalFunctionCall(function, arguments, options);
break; break;
} }
@ -299,15 +324,16 @@ bool ExpressionCompiler::visit(NewExpression const& _newExpression)
for (unsigned i = 0; i < arguments.size(); ++i) for (unsigned i = 0; i < arguments.size(); ++i)
{ {
arguments[i]->accept(*this); arguments[i]->accept(*this);
appendTypeConversion(*arguments[i]->getType(), *types[i]); appendTypeConversion(*arguments[i]->getType(), *types[i], true);
unsigned const numBytes = types[i]->getCalldataEncodedSize(); unsigned const c_numBytes = types[i]->getCalldataEncodedSize();
if (numBytes > 32) if (c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError() BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(arguments[i]->getLocation()) << errinfo_sourceLocation(arguments[i]->getLocation())
<< errinfo_comment("Type " + types[i]->toString() + " not yet supported.")); << errinfo_comment("Type " + types[i]->toString() + " not yet supported."));
bool const leftAligned = types[i]->getCategory() == Type::Category::STRING; bool const c_leftAligned = types[i]->getCategory() == Type::Category::STRING;
CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); bool const c_padToWords = true;
dataOffset += numBytes; dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, c_numBytes,
c_leftAligned, c_padToWords);
} }
// size, offset, endowment // size, offset, endowment
m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE; m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE;
@ -319,6 +345,18 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
ASTString const& member = _memberAccess.getMemberName(); ASTString const& member = _memberAccess.getMemberName();
switch (_memberAccess.getExpression().getType()->getCategory()) switch (_memberAccess.getExpression().getType()->getCategory())
{ {
case Type::Category::CONTRACT:
{
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
u256 identifier = type.getFunctionIdentifier(member);
if (identifier != Invalid256)
{
appendTypeConversion(type, IntegerType(0, IntegerType::Modifier::ADDRESS), true);
m_context << identifier;
break;
}
// fall-through to "integer" otherwise (address)
}
case Type::Category::INTEGER: case Type::Category::INTEGER:
if (member == "balance") if (member == "balance")
{ {
@ -332,12 +370,6 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
else else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer."));
break; break;
case Type::Category::CONTRACT:
{
ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
m_context << type.getFunctionIndex(member);
break;
}
case Type::Category::MAGIC: case Type::Category::MAGIC:
// we can ignore the kind of magic and only look at the name of the member // we can ignore the kind of magic and only look at the name of the member
if (member == "coinbase") if (member == "coinbase")
@ -423,10 +455,10 @@ void ExpressionCompiler::endVisit(Literal const& _literal)
{ {
switch (_literal.getType()->getCategory()) switch (_literal.getType()->getCategory())
{ {
case Type::Category::INTEGER: case Type::Category::INTEGER_CONSTANT:
case Type::Category::BOOL: case Type::Category::BOOL:
case Type::Category::STRING: case Type::Category::STRING:
m_context << _literal.getType()->literalValue(_literal); m_context << _literal.getType()->literalValue(&_literal);
break; break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now."));
@ -435,12 +467,12 @@ void ExpressionCompiler::endVisit(Literal const& _literal)
void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation) void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation)
{ {
Token::Value const op = _binaryOperation.getOperator(); Token::Value const c_op = _binaryOperation.getOperator();
solAssert(op == Token::OR || op == Token::AND, ""); solAssert(c_op == Token::OR || c_op == Token::AND, "");
_binaryOperation.getLeftExpression().accept(*this); _binaryOperation.getLeftExpression().accept(*this);
m_context << eth::Instruction::DUP1; m_context << eth::Instruction::DUP1;
if (op == Token::AND) if (c_op == Token::AND)
m_context << eth::Instruction::ISZERO; m_context << eth::Instruction::ISZERO;
eth::AssemblyItem endLabel = m_context.appendConditionalJump(); eth::AssemblyItem endLabel = m_context.appendConditionalJump();
m_context << eth::Instruction::POP; m_context << eth::Instruction::POP;
@ -459,23 +491,23 @@ void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type
else else
{ {
IntegerType const& type = dynamic_cast<IntegerType const&>(_type); IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
bool const isSigned = type.isSigned(); bool const c_isSigned = type.isSigned();
switch (_operator) switch (_operator)
{ {
case Token::GTE: case Token::GTE:
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT) m_context << (c_isSigned ? eth::Instruction::SLT : eth::Instruction::LT)
<< eth::Instruction::ISZERO; << eth::Instruction::ISZERO;
break; break;
case Token::LTE: case Token::LTE:
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT) m_context << (c_isSigned ? eth::Instruction::SGT : eth::Instruction::GT)
<< eth::Instruction::ISZERO; << eth::Instruction::ISZERO;
break; break;
case Token::GT: case Token::GT:
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT); m_context << (c_isSigned ? eth::Instruction::SGT : eth::Instruction::GT);
break; break;
case Token::LT: case Token::LT:
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT); m_context << (c_isSigned ? eth::Instruction::SLT : eth::Instruction::LT);
break; break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown comparison operator.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown comparison operator."));
@ -498,7 +530,7 @@ void ExpressionCompiler::appendOrdinaryBinaryOperatorCode(Token::Value _operator
void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Type const& _type) void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Type const& _type)
{ {
IntegerType const& type = dynamic_cast<IntegerType const&>(_type); IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
bool const isSigned = type.isSigned(); bool const c_isSigned = type.isSigned();
switch (_operator) switch (_operator)
{ {
@ -512,10 +544,10 @@ void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Ty
m_context << eth::Instruction::MUL; m_context << eth::Instruction::MUL;
break; break;
case Token::DIV: case Token::DIV:
m_context << (isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV); m_context << (c_isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV);
break; break;
case Token::MOD: case Token::MOD:
m_context << (isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD); m_context << (c_isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD);
break; break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown arithmetic operator.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown arithmetic operator."));
@ -562,10 +594,38 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con
if (_typeOnStack == _targetType && !_cleanupNeeded) if (_typeOnStack == _targetType && !_cleanupNeeded)
return; return;
if (_typeOnStack.getCategory() == Type::Category::INTEGER) Type::Category stackTypeCategory = _typeOnStack.getCategory();
appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack)); Type::Category targetTypeCategory = _targetType.getCategory();
else if (_typeOnStack.getCategory() == Type::Category::STRING) if (stackTypeCategory == Type::Category::INTEGER || stackTypeCategory == Type::Category::CONTRACT ||
stackTypeCategory == Type::Category::INTEGER_CONSTANT)
{
solAssert(targetTypeCategory == Type::Category::INTEGER || targetTypeCategory == Type::Category::CONTRACT, "");
IntegerType addressType(0, IntegerType::Modifier::ADDRESS);
IntegerType const& targetType = targetTypeCategory == Type::Category::INTEGER
? dynamic_cast<IntegerType const&>(_targetType) : addressType;
if (stackTypeCategory == Type::Category::INTEGER_CONSTANT)
{
IntegerConstantType const& constType = dynamic_cast<IntegerConstantType const&>(_typeOnStack);
// We know that the stack is clean, we only have to clean for a narrowing conversion
// where cleanup is forced.
if (targetType.getNumBits() < constType.getIntegerType()->getNumBits() && _cleanupNeeded)
appendHighBitsCleanup(targetType);
}
else
{
IntegerType const& typeOnStack = stackTypeCategory == Type::Category::INTEGER
? dynamic_cast<IntegerType const&>(_typeOnStack) : addressType;
// Widening: clean up according to source type width
// Non-widening and force: clean up according to target type bits
if (targetType.getNumBits() > typeOnStack.getNumBits())
appendHighBitsCleanup(typeOnStack);
else if (_cleanupNeeded)
appendHighBitsCleanup(targetType);
}
}
else if (stackTypeCategory == Type::Category::STRING)
{ {
solAssert(targetTypeCategory == Type::Category::STRING, "");
// nothing to do, strings are high-order-bit-aligned // nothing to do, strings are high-order-bit-aligned
//@todo clear lower-order bytes if we allow explicit conversion to shorter strings //@todo clear lower-order bytes if we allow explicit conversion to shorter strings
} }
@ -590,43 +650,47 @@ void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functio
{ {
solAssert(_arguments.size() == _functionType.getParameterTypes().size(), ""); 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) for (unsigned i = 0; i < _arguments.size(); ++i)
{ {
_arguments[i]->accept(*this); _arguments[i]->accept(*this);
Type const& type = *_functionType.getParameterTypes()[i]; Type const& type = *_functionType.getParameterTypes()[i];
appendTypeConversion(*_arguments[i]->getType(), type); appendTypeConversion(*_arguments[i]->getType(), type, true);
unsigned const numBytes = _options.packDensely ? type.getCalldataEncodedSize() : 32; unsigned const c_numBytes = type.getCalldataEncodedSize();
if (numBytes == 0 || numBytes > 32) if (c_numBytes == 0 || c_numBytes > 32)
BOOST_THROW_EXCEPTION(CompilerError() BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_arguments[i]->getLocation()) << errinfo_sourceLocation(_arguments[i]->getLocation())
<< errinfo_comment("Type " + type.toString() + " not yet supported.")); << errinfo_comment("Type " + type.toString() + " not yet supported."));
bool const leftAligned = type.getCategory() == Type::Category::STRING; bool const c_leftAligned = type.getCategory() == Type::Category::STRING;
CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned); bool const c_padToWords = true;
dataOffset += numBytes; dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, c_numBytes,
c_leftAligned, c_padToWords);
} }
//@todo only return the first return value for now //@todo only return the first return value for now
Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr : Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
_functionType.getReturnParameterTypes().front().get(); _functionType.getReturnParameterTypes().front().get();
unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0; unsigned retSize = firstType ? CompilerUtils::getPaddedSize(firstType->getCalldataEncodedSize()) : 0;
if (!_options.packDensely && retSize > 0)
retSize = 32;
// CALL arguments: outSize, outOff, inSize, inOff, value, addr, gas (stack top) // CALL arguments: outSize, outOff, inSize, inOff, value, addr, gas (stack top)
m_context << u256(retSize) << u256(0) << u256(dataOffset) << u256(0); m_context << u256(retSize) << u256(0) << u256(dataOffset) << u256(0);
if (_options.obtainValue) if (_options.obtainValue)
_options.obtainValue(); _options.obtainValue();
else else
m_context << u256(0); m_context << u256(0);
_options.obtainAddress(); m_context << eth::dupInstruction(6); //copy contract address
if (!_options.bare)
m_context << u256(0) << eth::Instruction::MSTORE8;
m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB
<< eth::Instruction::CALL << 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) if (retSize > 0)
{ {
bool const leftAligned = firstType->getCategory() == Type::Category::STRING; bool const c_leftAligned = firstType->getCategory() == Type::Category::STRING;
CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned); CompilerUtils(m_context).loadFromMemory(0, retSize, c_leftAligned, false, true);
} }
} }

9
libsolidity/ExpressionCompiler.h

@ -16,6 +16,7 @@
*/ */
/** /**
* @author Christian <c@ethdev.com> * @author Christian <c@ethdev.com>
* @author Gav Wood <g@ethdev.com>
* @date 2014 * @date 2014
* Solidity AST to EVM bytecode compiler for expressions. * Solidity AST to EVM bytecode compiler for expressions.
*/ */
@ -50,14 +51,15 @@ public:
static void compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize = false); static void compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize = false);
/// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type. /// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type.
static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack, Type const& _targetType); static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack,
Type const& _targetType, bool _cleanupNeeded = false);
private: private:
explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false): explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false):
m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {} m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {}
virtual bool visit(Assignment const& _assignment) override; 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(BinaryOperation const& _binaryOperation) override;
virtual bool visit(FunctionCall const& _functionCall) override; virtual bool visit(FunctionCall const& _functionCall) override;
virtual bool visit(NewExpression const& _newExpression) override; virtual bool visit(NewExpression const& _newExpression) override;
@ -95,9 +97,6 @@ private:
std::function<void()> obtainValue; std::function<void()> obtainValue;
/// If true, do not prepend function index to call data /// If true, do not prepend function index to call data
bool bare = false; bool bare = false;
/// If false, use calling convention that all arguments and return values are packed as
/// 32 byte values with padding.
bool packDensely = true;
}; };
/// Appends code to call a function of the given type with the given arguments. /// Appends code to call a function of the given type with the given arguments.

49
libsolidity/GlobalContext.cpp

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

55
libsolidity/InterfaceHandler.cpp

@ -2,6 +2,7 @@
#include <libsolidity/InterfaceHandler.h> #include <libsolidity/InterfaceHandler.h>
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
using namespace std;
namespace dev namespace dev
{ {
@ -26,6 +27,8 @@ std::unique_ptr<std::string> InterfaceHandler::getDocumentation(ContractDefiniti
return getDevDocumentation(_contractDef); return getDevDocumentation(_contractDef);
case DocumentationType::ABI_INTERFACE: case DocumentationType::ABI_INTERFACE:
return getABIInterface(_contractDef); return getABIInterface(_contractDef);
case DocumentationType::ABI_SOLIDITY_INTERFACE:
return getABISolidityInterface(_contractDef);
} }
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation type")); 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); Json::Value methods(Json::arrayValue);
for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) for (auto const& it: _contractDef.getInterfaceFunctions())
{ {
Json::Value method; Json::Value method;
Json::Value inputs(Json::arrayValue); Json::Value inputs(Json::arrayValue);
@ -55,24 +58,47 @@ std::unique_ptr<std::string> InterfaceHandler::getABIInterface(ContractDefinitio
return params; return params;
}; };
method["name"] = f->getName(); method["name"] = it.second->getName();
method["constant"] = f->isDeclaredConst(); method["constant"] = it.second->isDeclaredConst();
method["inputs"] = populateParameters(f->getParameters()); method["inputs"] = populateParameters(it.second->getParameters());
method["outputs"] = populateParameters(f->getReturnParameters()); method["outputs"] = populateParameters(it.second->getReturnParameters());
methods.append(method); methods.append(method);
} }
return std::unique_ptr<std::string>(new std::string(m_writer.write(methods))); 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) std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(ContractDefinition const& _contractDef)
{ {
Json::Value doc; Json::Value doc;
Json::Value methods(Json::objectValue); Json::Value methods(Json::objectValue);
for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) for (auto const& it: _contractDef.getInterfaceFunctions())
{ {
Json::Value user; Json::Value user;
auto strPtr = f->getDocumentation(); auto strPtr = it.second->getDocumentation();
if (strPtr) if (strPtr)
{ {
resetUser(); resetUser();
@ -80,7 +106,7 @@ std::unique_ptr<std::string> InterfaceHandler::getUserDocumentation(ContractDefi
if (!m_notice.empty()) if (!m_notice.empty())
{// since @notice is the only user tag if missing function should not appear {// since @notice is the only user tag if missing function should not appear
user["notice"] = Json::Value(m_notice); 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; doc["title"] = m_title;
} }
for (FunctionDefinition const* f: _contractDef.getInterfaceFunctions()) for (auto const& it: _contractDef.getInterfaceFunctions())
{ {
Json::Value method; Json::Value method;
auto strPtr = f->getDocumentation(); auto strPtr = it.second->getDocumentation();
if (strPtr) if (strPtr)
{ {
resetDev(); resetDev();
@ -136,7 +162,7 @@ std::unique_ptr<std::string> InterfaceHandler::getDevDocumentation(ContractDefin
method["return"] = m_return; method["return"] = m_return;
if (!method.empty()) // add the function, only if we have any documentation to add 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; doc["methods"] = methods;
@ -323,8 +349,13 @@ void InterfaceHandler::parseDocString(std::string const& _string, CommentOwner _
} }
else if (m_lastTag != DocTagType::NONE) // continuation of the previous tag else if (m_lastTag != DocTagType::NONE) // continuation of the previous tag
currPos = appendDocTag(currPos, end, _owner); currPos = appendDocTag(currPos, end, _owner);
else if (currPos != end) // skip the line if a newline was found else if (currPos != end)
{
if (nlPos == end) //end of text
return;
// else skip the line if a newline was found
currPos = nlPos + 1; currPos = nlPos + 1;
}
} }
} }

1
libsolidity/InterfaceHandler.h

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

27
libsolidity/Scanner.cpp

@ -455,7 +455,7 @@ void Scanner::scanToken()
token = Token::ADD; token = Token::ADD;
break; break;
case '-': case '-':
// - -- -= Number // - -- -=
advance(); advance();
if (m_char == '-') if (m_char == '-')
{ {
@ -464,8 +464,6 @@ void Scanner::scanToken()
} }
else if (m_char == '=') else if (m_char == '=')
token = selectToken(Token::ASSIGN_SUB); token = selectToken(Token::ASSIGN_SUB);
else if (m_char == '.' || isDecimalDigit(m_char))
token = scanNumber('-');
else else
token = Token::SUB; token = Token::SUB;
break; break;
@ -650,8 +648,7 @@ Token::Value Scanner::scanNumber(char _charSeen)
} }
else else
{ {
if (_charSeen == '-') solAssert(_charSeen == 0, "");
addLiteralChar('-');
// if the first character is '0' we must check for octals and hex // if the first character is '0' we must check for octals and hex
if (m_char == '0') if (m_char == '0')
{ {
@ -703,24 +700,6 @@ Token::Value Scanner::scanNumber(char _charSeen)
return Token::NUMBER; return Token::NUMBER;
} }
// ----------------------------------------------------------------------------
// Keyword Matcher
static Token::Value keywordOrIdentifierToken(string const& _input)
{
// The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored
// and keywords to be put inside the keywords variable.
#define KEYWORD(name, string, precedence) {string, Token::name},
#define TOKEN(name, string, precedence)
static const map<string, Token::Value> keywords({TOKEN_LIST(TOKEN, KEYWORD)});
#undef KEYWORD
#undef TOKEN
auto it = keywords.find(_input);
return it == keywords.end() ? Token::IDENTIFIER : it->second;
}
Token::Value Scanner::scanIdentifierOrKeyword() Token::Value Scanner::scanIdentifierOrKeyword()
{ {
solAssert(isIdentifierStart(m_char), ""); solAssert(isIdentifierStart(m_char), "");
@ -730,7 +709,7 @@ Token::Value Scanner::scanIdentifierOrKeyword()
while (isIdentifierPart(m_char)) while (isIdentifierPart(m_char))
addLiteralCharAndAdvance(); addLiteralCharAndAdvance();
literal.complete(); literal.complete();
return keywordOrIdentifierToken(m_nextToken.literal); return Token::fromIdentifierOrKeyword(m_nextToken.literal);
} }
char CharStream::advanceAndGet(size_t _chars) char CharStream::advanceAndGet(size_t _chars)

17
libsolidity/Token.cpp

@ -40,8 +40,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. // along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#include <map>
#include <libsolidity/Token.h> #include <libsolidity/Token.h>
using namespace std;
namespace dev namespace dev
{ {
namespace solidity namespace solidity
@ -77,6 +80,20 @@ char const Token::m_tokenType[] =
{ {
TOKEN_LIST(KT, KK) TOKEN_LIST(KT, KK)
}; };
Token::Value Token::fromIdentifierOrKeyword(const std::string& _name)
{
// The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored
// and keywords to be put inside the keywords variable.
#define KEYWORD(name, string, precedence) {string, Token::name},
#define TOKEN(name, string, precedence)
static const map<string, Token::Value> keywords({TOKEN_LIST(TOKEN, KEYWORD)});
#undef KEYWORD
#undef TOKEN
auto it = keywords.find(_name);
return it == keywords.end() ? Token::IDENTIFIER : it->second;
}
#undef KT #undef KT
#undef KK #undef KK

2
libsolidity/Token.h

@ -386,6 +386,8 @@ public:
return m_precedence[tok]; return m_precedence[tok];
} }
static Token::Value fromIdentifierOrKeyword(std::string const& _name);
private: private:
static char const* const m_name[NUM_TOKENS]; static char const* const m_name[NUM_TOKENS];
static char const* const m_string[NUM_TOKENS]; static char const* const m_string[NUM_TOKENS];

316
libsolidity/Types.cpp

@ -35,7 +35,7 @@ namespace solidity
shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken) shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken)
{ {
solAssert(Token::isElementaryTypeName(_typeToken), ""); solAssert(Token::isElementaryTypeName(_typeToken), "Elementary type name expected.");
if (Token::INT <= _typeToken && _typeToken <= Token::HASH256) if (Token::INT <= _typeToken && _typeToken <= Token::HASH256)
{ {
@ -44,17 +44,17 @@ shared_ptr<Type const> Type::fromElementaryTypeName(Token::Value _typeToken)
if (bytes == 0) if (bytes == 0)
bytes = 32; bytes = 32;
int modifier = offset / 33; int modifier = offset / 33;
return make_shared<IntegerType const>(bytes * 8, return make_shared<IntegerType>(bytes * 8,
modifier == 0 ? IntegerType::Modifier::SIGNED : modifier == 0 ? IntegerType::Modifier::SIGNED :
modifier == 1 ? IntegerType::Modifier::UNSIGNED : modifier == 1 ? IntegerType::Modifier::UNSIGNED :
IntegerType::Modifier::HASH); IntegerType::Modifier::HASH);
} }
else if (_typeToken == Token::ADDRESS) 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) else if (_typeToken == Token::BOOL)
return make_shared<BoolType const>(); return make_shared<BoolType>();
else if (Token::STRING0 <= _typeToken && _typeToken <= Token::STRING32) 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 else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unable to convert elementary typename " + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unable to convert elementary typename " +
std::string(Token::toString(_typeToken)) + " to type.")); std::string(Token::toString(_typeToken)) + " to type."));
@ -64,11 +64,11 @@ shared_ptr<Type const> Type::fromUserDefinedTypeName(UserDefinedTypeName const&
{ {
Declaration const* declaration = _typeName.getReferencedDeclaration(); Declaration const* declaration = _typeName.getReferencedDeclaration();
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration)) 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)) 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)) 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>(); 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(); shared_ptr<Type const> valueType = _typeName.getValueType().toType();
if (!valueType) if (!valueType)
BOOST_THROW_EXCEPTION(_typeName.getValueType().createTypeError("Invalid type name")); 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) 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::TRUE_LITERAL:
case Token::FALSE_LITERAL: case Token::FALSE_LITERAL:
return make_shared<BoolType const>(); return make_shared<BoolType>();
case Token::NUMBER: case Token::NUMBER:
return IntegerType::smallestTypeForLiteral(_literal.getValue()); return IntegerConstantType::fromLiteral(_literal.getValue());
case Token::STRING_LITERAL: case Token::STRING_LITERAL:
//@todo put larger strings into dynamic strings //@todo put larger strings into dynamic strings
return StaticStringType::smallestTypeForLiteral(_literal.getValue()); return StaticStringType::smallestTypeForLiteral(_literal.getValue());
default: 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(); 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): IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
m_bits(_bits), m_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; 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) if (_operator == Token::DELETE)
return true; return shared_from_this();
if (isAddress()) // no further unary operators for addresses
return false; else if (isAddress())
if (_operator == Token::BIT_NOT) return TypePointer();
return true; // "~" is ok for all other types
if (isHash()) else if (_operator == Token::BIT_NOT)
return false; return shared_from_this();
return _operator == Token::ADD || _operator == Token::SUB || // nothing else for hashes
_operator == Token::INC || _operator == Token::DEC; 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 bool IntegerType::operator==(Type const& _other) const
@ -186,17 +181,11 @@ string IntegerType::toString() const
return prefix + dev::toString(m_bits); return prefix + dev::toString(m_bits);
} }
u256 IntegerType::literalValue(Literal const& _literal) const TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointer const& _other) const
{
bigint value(_literal.getValue());
return u256(value);
}
TypePointer IntegerType::binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const
{ {
if (getCategory() != _other->getCategory()) if (_other->getCategory() != Category::INTEGER_CONSTANT && _other->getCategory() != getCategory())
return TypePointer(); 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) if (!commonType)
return TypePointer(); return TypePointer();
@ -215,18 +204,147 @@ TypePointer IntegerType::binaryOperatorResultImpl(Token::Value _operator, TypePo
} }
const MemberList IntegerType::AddressMemberList = const MemberList IntegerType::AddressMemberList =
MemberList({{"balance", MemberList({{"balance", make_shared<IntegerType >(256)},
make_shared<IntegerType const>(256)}, {"callstring32", make_shared<FunctionType>(strings{"string32"}, strings{},
{"callstring32", FunctionType::Location::BARE)},
make_shared<FunctionType const>(TypePointers({make_shared<StaticStringType const>(32)}), {"callstring32string32", make_shared<FunctionType>(strings{"string32", "string32"},
TypePointers(), FunctionType::Location::BARE)}, strings{}, FunctionType::Location::BARE)},
{"callstring32string32", {"send", make_shared<FunctionType>(strings{"uint"}, strings{}, FunctionType::Location::SEND)}});
make_shared<FunctionType const>(TypePointers({make_shared<StaticStringType const>(32),
make_shared<StaticStringType const>(32)}), shared_ptr<IntegerConstantType const> IntegerConstantType::fromLiteral(string const& _literal)
TypePointers(), FunctionType::Location::BARE)}, {
{"send", return make_shared<IntegerConstantType>(bigint(_literal));
make_shared<FunctionType const>(TypePointers({make_shared<IntegerType const>(256)}), }
TypePointers(), FunctionType::Location::SEND)}});
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) shared_ptr<StaticStringType> StaticStringType::smallestTypeForLiteral(string const& _literal)
{ {
@ -257,12 +375,13 @@ bool StaticStringType::operator==(Type const& _other) const
return other.m_bytes == m_bytes; 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; u256 value = 0;
for (char c: _literal.getValue()) for (char c: _literal->getValue())
value = (value << 8) | byte(c); 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 bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
@ -278,35 +397,41 @@ bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
return isImplicitlyConvertibleTo(_convertTo); 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); return u256(1);
else if (_literal.getToken() == Token::FALSE_LITERAL) else if (_literal->getToken() == Token::FALSE_LITERAL)
return u256(0); return u256(0);
else else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Bool type constructed from non-boolean literal.")); 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()) if (getCategory() != _other->getCategory())
return TypePointer(); return TypePointer();
if (Token::isCompareOp(_operator) || _operator == Token::AND || _operator == Token::OR) if (Token::isCompareOp(_operator) || _operator == Token::AND || _operator == Token::OR)
return _this; return _other;
else else
return TypePointer(); return TypePointer();
} }
bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const bool ContractType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{ {
if (isImplicitlyConvertibleTo(_convertTo)) if (*this == _convertTo)
return true; return true;
if (_convertTo.getCategory() == Category::INTEGER) if (_convertTo.getCategory() == Category::INTEGER)
return dynamic_cast<IntegerType const&>(_convertTo).isAddress(); return dynamic_cast<IntegerType const&>(_convertTo).isAddress();
return false; return false;
} }
bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{
return isImplicitlyConvertibleTo(_convertTo) || _convertTo.getCategory() == Category::INTEGER;
}
bool ContractType::operator==(Type const& _other) const bool ContractType::operator==(Type const& _other) const
{ {
if (_other.getCategory() != getCategory()) if (_other.getCategory() != getCategory())
@ -333,9 +458,11 @@ MemberList const& ContractType::getMembers() const
// We need to lazy-initialize it because of recursive references. // We need to lazy-initialize it because of recursive references.
if (!m_members) if (!m_members)
{ {
map<string, shared_ptr<Type const>> members; // All address members and all interface functions
for (FunctionDefinition const* function: m_contract.getInterfaceFunctions()) map<string, shared_ptr<Type const>> members(IntegerType::AddressMemberList.begin(),
members[function->getName()] = make_shared<FunctionType>(*function, false); IntegerType::AddressMemberList.end());
for (auto const& it: m_contract.getInterfaceFunctions())
members[it.second->getName()] = make_shared<FunctionType>(*it.second, false);
m_members.reset(new MemberList(members)); m_members.reset(new MemberList(members));
} }
return *m_members; return *m_members;
@ -347,23 +474,21 @@ shared_ptr<FunctionType const> const& ContractType::getConstructorType() const
{ {
FunctionDefinition const* constructor = m_contract.getConstructor(); FunctionDefinition const* constructor = m_contract.getConstructor();
if (constructor) if (constructor)
m_constructorType = make_shared<FunctionType const>(*constructor); m_constructorType = make_shared<FunctionType>(*constructor);
else else
m_constructorType = make_shared<FunctionType const>(TypePointers(), TypePointers()); m_constructorType = make_shared<FunctionType>(TypePointers(), TypePointers());
} }
return m_constructorType; return m_constructorType;
} }
unsigned ContractType::getFunctionIndex(string const& _functionName) const u256 ContractType::getFunctionIdentifier(string const& _functionName) const
{ {
unsigned index = 0; auto interfaceFunctions = m_contract.getInterfaceFunctions();
for (FunctionDefinition const* function: m_contract.getInterfaceFunctions()) for (auto it = interfaceFunctions.cbegin(); it != interfaceFunctions.cend(); ++it)
{ if (it->second->getName() == _functionName)
if (function->getName() == _functionName) return FixedHash<4>::Arith(it->first);
return index;
++index; return Invalid256;
}
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index of non-existing contract function requested."));
} }
bool StructType::operator==(Type const& _other) const bool StructType::operator==(Type const& _other) const
@ -494,6 +619,15 @@ string FunctionType::getCanonicalSignature() const
return ret + ")"; return ret + ")";
} }
TypePointers FunctionType::parseElementaryTypeVector(strings const& _types)
{
TypePointers pointers;
pointers.reserve(_types.size());
for (string const& type: _types)
pointers.push_back(Type::fromElementaryTypeName(Token::fromIdentifierOrKeyword(type)));
return pointers;
}
bool MappingType::operator==(Type const& _other) const bool MappingType::operator==(Type const& _other) const
{ {
if (_other.getCategory() != getCategory()) if (_other.getCategory() != getCategory())
@ -521,21 +655,21 @@ MagicType::MagicType(MagicType::Kind _kind):
switch (m_kind) switch (m_kind)
{ {
case Kind::BLOCK: case Kind::BLOCK:
m_members = MemberList({{"coinbase", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)}, m_members = MemberList({{"coinbase", make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)},
{"timestamp", make_shared<IntegerType const>(256)}, {"timestamp", make_shared<IntegerType >(256)},
{"prevhash", make_shared<IntegerType const>(256, IntegerType::Modifier::HASH)}, {"prevhash", make_shared<IntegerType>(256, IntegerType::Modifier::HASH)},
{"difficulty", make_shared<IntegerType const>(256)}, {"difficulty", make_shared<IntegerType>(256)},
{"number", make_shared<IntegerType const>(256)}, {"number", make_shared<IntegerType>(256)},
{"gaslimit", make_shared<IntegerType const>(256)}}); {"gaslimit", make_shared<IntegerType>(256)}});
break; break;
case Kind::MSG: case Kind::MSG:
m_members = MemberList({{"sender", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)}, m_members = MemberList({{"sender", make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)},
{"gas", make_shared<IntegerType const>(256)}, {"gas", make_shared<IntegerType>(256)},
{"value", make_shared<IntegerType const>(256)}}); {"value", make_shared<IntegerType>(256)}});
break; break;
case Kind::TX: case Kind::TX:
m_members = MemberList({{"origin", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)}, m_members = MemberList({{"origin", make_shared<IntegerType>(0, IntegerType::Modifier::ADDRESS)},
{"gasprice", make_shared<IntegerType const>(256)}}); {"gasprice", make_shared<IntegerType>(256)}});
break; break;
default: default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of magic.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of magic."));

146
libsolidity/Types.h

@ -70,12 +70,12 @@ private:
/** /**
* Abstract base class that forms the root of the type hierarchy. * 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: public:
enum class Category 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); static TypePointer forLiteral(Literal const& _literal);
/// @returns a pointer to _a or _b if the other is implicitly convertible to it or nullptr otherwise /// @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); 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 Category getCategory() const = 0;
virtual bool isImplicitlyConvertibleTo(Type const& _other) const { return *this == _other; } virtual bool isImplicitlyConvertibleTo(Type const& _other) const { return *this == _other; }
@ -105,13 +99,24 @@ public:
{ {
return isImplicitlyConvertibleTo(_convertTo); 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 getCategory() == _other.getCategory(); }
virtual bool operator!=(Type const& _other) const { return !this->operator ==(_other); } virtual bool operator!=(Type const& _other) const { return !this->operator ==(_other); }
/// @returns number of bytes used by this type when encoded for CALL, or 0 if the encoding /// @returns number of bytes used by this type when encoded for CALL, or 0 if the encoding
/// is not a simple big-endian encoding or the type cannot be stored on the stack. /// is not a simple big-endian encoding or the type cannot be stored in calldata.
/// Note that irrespective of this size, each calldata element is padded to a multiple of 32 bytes.
virtual unsigned getCalldataEncodedSize() const { return 0; } virtual unsigned getCalldataEncodedSize() const { return 0; }
/// @returns number of bytes required to hold this value in storage. /// @returns number of bytes required to hold this value in storage.
/// For dynamically "allocated" types, it returns the size of the statically allocated head, /// For dynamically "allocated" types, it returns the size of the statically allocated head,
@ -131,18 +136,13 @@ public:
TypePointer getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); } TypePointer getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); }
virtual std::string toString() const = 0; 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 " BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested "
"for type without literals.")); "for type without literals."));
} }
protected: 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. /// Convenience object used when returning an empty member list.
static const MemberList EmptyMemberList; static const MemberList EmptyMemberList;
}; };
@ -159,15 +159,12 @@ public:
}; };
virtual Category getCategory() const override { return Category::INTEGER; } 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); explicit IntegerType(int _bits, Modifier _modifier = Modifier::UNSIGNED);
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override; virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool isExplicitlyConvertibleTo(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; virtual bool operator==(Type const& _other) const override;
@ -177,20 +174,51 @@ public:
virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; } virtual MemberList const& getMembers() const { return isAddress() ? AddressMemberList : EmptyMemberList; }
virtual std::string toString() const override; virtual std::string toString() const override;
virtual u256 literalValue(Literal const& _literal) const override;
int getNumBits() const { return m_bits; } int getNumBits() const { return m_bits; }
bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; } bool isHash() const { return m_modifier == Modifier::HASH || m_modifier == Modifier::ADDRESS; }
bool isAddress() const { return m_modifier == Modifier::ADDRESS; } bool isAddress() const { return m_modifier == Modifier::ADDRESS; }
int isSigned() const { return m_modifier == Modifier::SIGNED; } int isSigned() const { return m_modifier == Modifier::SIGNED; }
protected: static const MemberList AddressMemberList;
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override;
private: private:
int m_bits; int m_bits;
Modifier m_modifier; 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;
}; };
/** /**
@ -214,7 +242,7 @@ public:
virtual bool isValueType() const override { return true; } virtual bool isValueType() const override { return true; }
virtual std::string toString() const override { return "string" + dev::toString(m_bytes); } 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; } int getNumBytes() const { return m_bytes; }
@ -231,19 +259,17 @@ public:
BoolType() {} BoolType() {}
virtual Category getCategory() const { return Category::BOOL; } virtual Category getCategory() const { return Category::BOOL; }
virtual bool isExplicitlyConvertibleTo(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
{ {
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 unsigned getCalldataEncodedSize() const { return 1; }
virtual bool isValueType() const override { return true; } virtual bool isValueType() const override { return true; }
virtual std::string toString() const override { return "bool"; } virtual std::string toString() const override { return "bool"; }
virtual u256 literalValue(Literal const& _literal) const override; virtual u256 literalValue(Literal const* _literal) const override;
protected:
virtual TypePointer binaryOperatorResultImpl(Token::Value _operator, TypePointer const& _this, TypePointer const& _other) const override;
}; };
/** /**
@ -254,7 +280,9 @@ class ContractType: public Type
public: public:
virtual Category getCategory() const override { return Category::CONTRACT; } virtual Category getCategory() const override { return Category::CONTRACT; }
ContractType(ContractDefinition const& _contract): m_contract(_contract) {} ContractType(ContractDefinition const& _contract): m_contract(_contract) {}
/// Contracts can be converted to themselves and to addresses. /// Contracts can be implicitly converted to super classes and to addresses.
virtual bool isImplicitlyConvertibleTo(Type const& _convertTo) const override;
/// Contracts can be converted to themselves and to integers.
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override; virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
virtual bool operator==(Type const& _other) const override; virtual bool operator==(Type const& _other) const override;
virtual u256 getStorageSize() const override; virtual u256 getStorageSize() const override;
@ -267,7 +295,9 @@ public:
/// is not used, as this type cannot be the type of a variable or expression. /// is not used, as this type cannot be the type of a variable or expression.
std::shared_ptr<FunctionType const> const& getConstructorType() const; std::shared_ptr<FunctionType const> const& getConstructorType() const;
unsigned getFunctionIndex(std::string const& _functionName) const; /// @returns the identifier of the function with the given name or Invalid256 if such a name does
/// not exist.
u256 getFunctionIdentifier(std::string const& _functionName) const;
private: private:
ContractDefinition const& m_contract; ContractDefinition const& m_contract;
@ -285,9 +315,9 @@ class StructType: public Type
public: public:
virtual Category getCategory() const override { return Category::STRUCT; } virtual Category getCategory() const override { return Category::STRUCT; }
StructType(StructDefinition const& _struct): m_struct(_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; virtual bool operator==(Type const& _other) const override;
@ -318,10 +348,14 @@ public:
/// INTERNAL: jump tag, EXTERNAL: contract address + function index, /// INTERNAL: jump tag, EXTERNAL: contract address + function index,
/// BARE: contract address (non-abi contract call) /// BARE: contract address (non-abi contract call)
/// OTHERS: special virtual function, nothing on the stack /// 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; } virtual Category getCategory() const override { return Category::FUNCTION; }
explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true); explicit FunctionType(FunctionDefinition const& _function, bool _isInternal = true);
FunctionType(strings const& _parameterTypes, strings const& _returnParameterTypes,
Location _location = Location::INTERNAL):
FunctionType(parseElementaryTypeVector(_parameterTypes), parseElementaryTypeVector(_returnParameterTypes),
_location) {}
FunctionType(TypePointers const& _parameterTypes, TypePointers const& _returnParameterTypes, FunctionType(TypePointers const& _parameterTypes, TypePointers const& _returnParameterTypes,
Location _location = Location::INTERNAL): Location _location = Location::INTERNAL):
m_parameterTypes(_parameterTypes), m_returnParameterTypes(_returnParameterTypes), m_parameterTypes(_parameterTypes), m_returnParameterTypes(_returnParameterTypes),
@ -341,6 +375,8 @@ public:
std::string getCanonicalSignature() const; std::string getCanonicalSignature() const;
private: private:
static TypePointers parseElementaryTypeVector(strings const& _types);
TypePointers m_parameterTypes; TypePointers m_parameterTypes;
TypePointers m_returnParameterTypes; TypePointers m_returnParameterTypes;
Location m_location; Location m_location;
@ -378,20 +414,12 @@ public:
virtual Category getCategory() const override { return Category::VOID; } virtual Category getCategory() const override { return Category::VOID; }
VoidType() {} VoidType() {}
virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override { return TypePointer(); }
virtual std::string toString() const override { return "void"; } virtual std::string toString() const override { return "void"; }
virtual bool canBeStored() const override { return false; } 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 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 bool canLiveOutsideStorage() const override { return false; }
virtual unsigned getSizeOnStack() const override { return 0; } 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 +431,15 @@ class TypeType: public Type
public: public:
virtual Category getCategory() const override { return Category::TYPE; } virtual Category getCategory() const override { return Category::TYPE; }
TypeType(TypePointer const& _actualType): m_actualType(_actualType) {} TypeType(TypePointer const& _actualType): m_actualType(_actualType) {}
TypePointer const& getActualType() const { return m_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 operator==(Type const& _other) const override;
virtual bool canBeStored() const override { return false; } 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 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 bool canLiveOutsideStorage() const override { return false; }
virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; } 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: private:
TypePointer m_actualType; TypePointer m_actualType;
}; };
@ -437,6 +456,12 @@ public:
virtual Category getCategory() const override { return Category::MAGIC; } virtual Category getCategory() const override { return Category::MAGIC; }
MagicType(Kind _kind); MagicType(Kind _kind);
virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override
{
return TypePointer();
}
virtual bool operator==(Type const& _other) const; virtual bool operator==(Type const& _other) const;
virtual bool canBeStored() const override { return false; } virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const override { return true; } virtual bool canLiveOutsideStorage() const override { return true; }
@ -445,15 +470,6 @@ public:
virtual std::string toString() const override; 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: private:
Kind m_kind; Kind m_kind;

637
libweb3jsonrpc/WebThreeStubServer.cpp

@ -21,198 +21,19 @@
* @date 2014 * @date 2014
*/ */
#include <libsolidity/CompilerStack.h>
#include <libsolidity/Scanner.h>
#include <libsolidity/SourceReferenceFormatter.h>
#include "WebThreeStubServer.h"
#include <libevmcore/Instruction.h>
#include <liblll/Compiler.h>
#include <libethereum/Client.h>
#include <libwebthree/WebThree.h>
#include <libdevcore/CommonJS.h>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <libwebthree/WebThree.h>
#include <libdevcrypto/FileSystem.h> #include <libdevcrypto/FileSystem.h>
#include <libwhisper/Message.h> #include "WebThreeStubServer.h"
#include <libwhisper/WhisperHost.h>
#include <libserpent/funcs.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
static Json::Value toJson(dev::eth::BlockInfo const& _bi)
{
Json::Value res;
res["hash"] = boost::lexical_cast<string>(_bi.hash);
res["parentHash"] = toJS(_bi.parentHash);
res["sha3Uncles"] = toJS(_bi.sha3Uncles);
res["miner"] = toJS(_bi.coinbaseAddress);
res["stateRoot"] = toJS(_bi.stateRoot);
res["transactionsRoot"] = toJS(_bi.transactionsRoot);
res["difficulty"] = toJS(_bi.difficulty);
res["number"] = (int)_bi.number;
res["gasLimit"] = (int)_bi.gasLimit;
res["timestamp"] = (int)_bi.timestamp;
res["extraData"] = jsFromBinary(_bi.extraData);
res["nonce"] = toJS(_bi.nonce);
return res;
}
static Json::Value toJson(dev::eth::Transaction const& _t)
{
Json::Value res;
res["hash"] = toJS(_t.sha3());
res["input"] = jsFromBinary(_t.data());
res["to"] = toJS(_t.receiveAddress());
res["from"] = toJS(_t.sender());
res["gas"] = (int)_t.gas();
res["gasPrice"] = toJS(_t.gasPrice());
res["nonce"] = toJS(_t.nonce());
res["value"] = toJS(_t.value());
return res;
}
static Json::Value toJson(dev::eth::LogEntry const& _e)
{
Json::Value res;
res["data"] = jsFromBinary(_e.data);
res["address"] = toJS(_e.address);
for (auto const& t: _e.topics)
res["topics"].append(toJS(t));
return res;
}
static Json::Value toJson(dev::eth::LogEntries const& _es) // commented to avoid warning. Uncomment once in use @ poC-7.
{
Json::Value res;
for (dev::eth::LogEntry const& e: _es)
res.append(toJson(e));
return res;
}
static Json::Value toJson(std::map<u256, u256> const& _storage)
{
Json::Value res(Json::objectValue);
for (auto i: _storage)
res[toJS(i.first)] = toJS(i.second);
return res;
}
static dev::eth::LogFilter toLogFilter(Json::Value const& _json) // commented to avoid warning. Uncomment once in use @ PoC-7.
{
dev::eth::LogFilter filter;
if (!_json.isObject() || _json.empty())
return filter;
if (_json["earliest"].isInt())
filter.withEarliest(_json["earliest"].asInt());
if (_json["latest"].isInt())
filter.withLatest(_json["lastest"].asInt());
if (_json["max"].isInt())
filter.withMax(_json["max"].asInt());
if (_json["skip"].isInt())
filter.withSkip(_json["skip"].asInt());
if (!_json["address"].empty())
{
if (_json["address"].isArray())
{
for (auto i : _json["address"])
if (i.isString())
filter.address(jsToAddress(i.asString()));
}
else if (_json["address"].isString())
filter.address(jsToAddress(_json["address"].asString()));
}
if (!_json["topics"].empty())
{
if (_json["topics"].isArray())
{
for (auto i: _json["topics"])
if (i.isString())
filter.topic(jsToU256(i.asString()));
}
else if(_json["topics"].isString())
filter.topic(jsToU256(_json["topics"].asString()));
}
return filter;
}
static shh::Message toMessage(Json::Value const& _json)
{
shh::Message ret;
if (_json["from"].isString())
ret.setFrom(jsToPublic(_json["from"].asString()));
if (_json["to"].isString())
ret.setTo(jsToPublic(_json["to"].asString()));
if (_json["payload"].isString())
ret.setPayload(jsToBytes(_json["payload"].asString()));
return ret;
}
static shh::Envelope toSealed(Json::Value const& _json, shh::Message const& _m, Secret _from)
{
unsigned ttl = 50;
unsigned workToProve = 50;
shh::BuildTopic bt;
if (_json["ttl"].isInt())
ttl = _json["ttl"].asInt();
if (_json["workToProve"].isInt())
workToProve = _json["workToProve"].asInt();
if (!_json["topic"].empty())
{
if (_json["topic"].isString())
bt.shift(jsToBytes(_json["topic"].asString()));
else if (_json["topic"].isArray())
for (auto i: _json["topic"])
if (i.isString())
bt.shift(jsToBytes(i.asString()));
}
return _m.seal(_from, bt, ttl, workToProve);
}
static pair<shh::TopicMask, Public> toWatch(Json::Value const& _json)
{
shh::BuildTopicMask bt;
Public to;
if (_json["to"].isString())
to = jsToPublic(_json["to"].asString());
if (!_json["topic"].empty())
{
if (_json["topic"].isString())
bt.shift(jsToBytes(_json["topic"].asString()));
else if (_json["topic"].isArray())
for (auto i: _json["topic"])
if (i.isString())
bt.shift(jsToBytes(i.asString()));
}
return make_pair(bt.toTopicMask(), to);
}
static Json::Value toJson(h256 const& _h, shh::Envelope const& _e, shh::Message const& _m)
{
Json::Value res;
res["hash"] = toJS(_h);
res["expiry"] = (int)_e.expiry();
res["sent"] = (int)_e.sent();
res["ttl"] = (int)_e.ttl();
res["workProved"] = (int)_e.workProved();
for (auto const& t: _e.topics())
res["topics"].append(toJS(t));
res["payload"] = toJS(_m.payload());
res["from"] = toJS(_m.from());
res["to"] = toJS(_m.to());
return res;
}
WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector& _conn, WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts): WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector& _conn, WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts):
AbstractWebThreeStubServer(_conn), WebThreeStubServerBase(_conn, _accounts),
m_web3(_web3) m_web3(_web3)
{ {
setAccounts(_accounts);
auto path = getDataDir() + "/.web3"; auto path = getDataDir() + "/.web3";
boost::filesystem::create_directories(path); boost::filesystem::create_directories(path);
ldb::Options o; ldb::Options o;
@ -220,471 +41,37 @@ WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector& _conn,
ldb::DB::Open(o, path, &m_db); ldb::DB::Open(o, path, &m_db);
} }
void WebThreeStubServer::setAccounts(std::vector<dev::KeyPair> const& _accounts) dev::eth::Interface* WebThreeStubServer::client()
{
m_accounts.clear();
for (auto i: _accounts)
m_accounts[i.address()] = i.secret();
}
void WebThreeStubServer::setIdentities(std::vector<dev::KeyPair> const& _ids)
{
m_ids.clear();
for (auto i: _ids)
m_ids[i.pub()] = i.secret();
}
dev::eth::Interface* WebThreeStubServer::client() const
{ {
return m_web3.ethereum(); return m_web3.ethereum();
} }
std::shared_ptr<dev::shh::Interface> WebThreeStubServer::face() const std::shared_ptr<dev::shh::Interface> WebThreeStubServer::face()
{ {
return m_web3.whisper(); return m_web3.whisper();
} }
std::string WebThreeStubServer::web3_sha3(std::string const& _param1) dev::WebThreeNetworkFace* WebThreeStubServer::network()
{
return toJS(sha3(jsToBytes(_param1)));
}
Json::Value WebThreeStubServer::eth_accounts()
{ {
Json::Value ret(Json::arrayValue); return &m_web3;
for (auto i: m_accounts)
ret.append(toJS(i.first));
return ret;
} }
std::string WebThreeStubServer::shh_addToGroup(std::string const& _group, std::string const& _who) dev::WebThreeStubDatabaseFace* WebThreeStubServer::db()
{ {
(void)_group; return this;
(void)_who;
return "";
} }
std::string WebThreeStubServer::eth_balanceAt(string const& _address) std::string WebThreeStubServer::get(std::string const& _name, std::string const& _key)
{
return toJS(client()->balanceAt(jsToAddress(_address), client()->getDefault()));
}
Json::Value WebThreeStubServer::eth_blockByHash(std::string const& _hash)
{
return toJson(client()->blockInfo(jsToFixed<32>(_hash)));
}
Json::Value WebThreeStubServer::eth_blockByNumber(int const& _number)
{
return toJson(client()->blockInfo(client()->hashFromNumber(_number)));
}
static TransactionSkeleton toTransaction(Json::Value const& _json)
{
TransactionSkeleton ret;
if (!_json.isObject() || _json.empty())
return ret;
if (_json["from"].isString())
ret.from = jsToAddress(_json["from"].asString());
if (_json["to"].isString())
ret.to = jsToAddress(_json["to"].asString());
if (!_json["value"].empty())
{
if (_json["value"].isString())
ret.value = jsToU256(_json["value"].asString());
else if (_json["value"].isInt())
ret.value = u256(_json["value"].asInt());
}
if (!_json["gas"].empty())
{
if (_json["gas"].isString())
ret.gas = jsToU256(_json["gas"].asString());
else if (_json["gas"].isInt())
ret.gas = u256(_json["gas"].asInt());
}
if (!_json["gasPrice"].empty())
{
if (_json["gasPrice"].isString())
ret.gasPrice = jsToU256(_json["gasPrice"].asString());
else if (_json["gasPrice"].isInt())
ret.gas = u256(_json["gas"].asInt());
}
if (!_json["data"].empty())
{
if (_json["data"].isString()) // ethereum.js has preconstructed the data array
ret.data = jsToBytes(_json["data"].asString());
else if (_json["data"].isArray()) // old style: array of 32-byte-padded values. TODO: remove PoC-8
for (auto i: _json["data"])
dev::operator +=(ret.data, padded(jsToBytes(i.asString()), 32));
}
if (_json["code"].isString())
ret.data = jsToBytes(_json["code"].asString());
return ret;
}
std::string WebThreeStubServer::eth_call(Json::Value const& _json)
{
std::string ret;
TransactionSkeleton t = toTransaction(_json);
if (!t.from && m_accounts.size())
{
auto b = m_accounts.begin()->first;
for (auto a: m_accounts)
if (client()->balanceAt(a.first) > client()->balanceAt(b))
b = a.first;
t.from = b;
}
if (!m_accounts.count(t.from))
return ret;
if (!t.gasPrice)
t.gasPrice = 10 * dev::eth::szabo;
if (!t.gas)
t.gas = min<u256>(client()->gasLimitRemaining(), client()->balanceAt(t.from) / t.gasPrice);
ret = toJS(client()->call(m_accounts[t.from].secret(), t.value, t.to, t.data, t.gas, t.gasPrice));
return ret;
}
bool WebThreeStubServer::eth_changed(int const& _id)
{
return client()->checkWatch(_id);
}
std::string WebThreeStubServer::eth_codeAt(string const& _address)
{
return jsFromBinary(client()->codeAt(jsToAddress(_address), client()->getDefault()));
}
std::string WebThreeStubServer::eth_coinbase()
{
return toJS(client()->address());
}
double WebThreeStubServer::eth_countAt(string const& _address)
{
return (double)(uint64_t)client()->countAt(jsToAddress(_address), client()->getDefault());
}
int WebThreeStubServer::eth_defaultBlock()
{
return client()->getDefault();
}
std::string WebThreeStubServer::eth_gasPrice()
{
return toJS(10 * dev::eth::szabo);
}
std::string WebThreeStubServer::db_get(std::string const& _name, std::string const& _key)
{ {
bytes k = sha3(_name).asBytes() + sha3(_key).asBytes(); bytes k = sha3(_name).asBytes() + sha3(_key).asBytes();
string ret; string ret;
m_db->Get(m_readOptions, ldb::Slice((char const*)k.data(), k.size()), &ret); m_db->Get(m_readOptions, ldb::Slice((char const*)k.data(), k.size()), &ret);
return toJS(dev::asBytes(ret));
}
Json::Value WebThreeStubServer::eth_filterLogs(int const& _id)
{
return toJson(client()->logs(_id));
}
Json::Value WebThreeStubServer::eth_logs(Json::Value const& _json)
{
return toJson(client()->logs(toLogFilter(_json)));
}
std::string WebThreeStubServer::db_getString(std::string const& _name, std::string const& _key)
{
bytes k = sha3(_name).asBytes() + sha3(_key).asBytes();
string ret;
m_db->Get(m_readOptions, ldb::Slice((char const*)k.data(), k.size()), &ret);
return ret;
}
bool WebThreeStubServer::shh_haveIdentity(std::string const& _id)
{
return m_ids.count(jsToPublic(_id)) > 0;
}
bool WebThreeStubServer::eth_listening()
{
return m_web3.isNetworkStarted();
}
bool WebThreeStubServer::eth_mining()
{
return client()->isMining();
}
int WebThreeStubServer::eth_newFilter(Json::Value const& _json)
{
unsigned ret = -1;
ret = client()->installWatch(toLogFilter(_json));
return ret;
}
int WebThreeStubServer::eth_newFilterString(std::string const& _filter)
{
unsigned ret = -1;
if (_filter.compare("chain") == 0)
ret = client()->installWatch(dev::eth::ChainChangedFilter);
else if (_filter.compare("pending") == 0)
ret = client()->installWatch(dev::eth::PendingChangedFilter);
return ret; return ret;
} }
std::string WebThreeStubServer::shh_newGroup(std::string const& _id, std::string const& _who) void WebThreeStubServer::put(std::string const& _name, std::string const& _key, std::string const& _value)
{
(void)_id;
(void)_who;
return "";
}
std::string WebThreeStubServer::shh_newIdentity()
{
// cnote << this << m_ids;
KeyPair kp = KeyPair::create();
m_ids[kp.pub()] = kp.secret();
return toJS(kp.pub());
}
Json::Value WebThreeStubServer::eth_compilers()
{
Json::Value ret(Json::arrayValue);
ret.append("lll");
ret.append("solidity");
ret.append("serpent");
return ret;
}
std::string WebThreeStubServer::eth_lll(std::string const& _code)
{
string res;
vector<string> errors;
res = toJS(dev::eth::compileLLL(_code, true, &errors));
cwarn << "LLL compilation errors: " << errors;
return res;
}
std::string WebThreeStubServer::eth_serpent(std::string const& _code)
{
string res;
try
{
res = toJS(dev::asBytes(::compile(_code)));
}
catch (string err)
{
cwarn << "Solidity compilation error: " << err;
}
catch (...)
{
cwarn << "Uncought serpent compilation exception";
}
return res;
}
std::string WebThreeStubServer::eth_solidity(std::string const& _code)
{
string res;
dev::solidity::CompilerStack compiler;
try
{
res = toJS(compiler.compile(_code, true));
}
catch (dev::Exception const& exception)
{
ostringstream error;
solidity::SourceReferenceFormatter::printExceptionInformation(error, exception, "Error", compiler);
cwarn << "Solidity compilation error: " << error.str();
}
catch (...)
{
cwarn << "Uncought solidity compilation exception";
}
return res;
}
int WebThreeStubServer::eth_number()
{
return client()->number() + 1;
}
int WebThreeStubServer::eth_peerCount()
{
return m_web3.peerCount();
}
bool WebThreeStubServer::shh_post(Json::Value const& _json)
{
shh::Message m = toMessage(_json);
Secret from;
if (m.from() && m_ids.count(m.from()))
{
cwarn << "Silently signing message from identity" << m.from().abridged() << ": User validation hook goes here.";
// TODO: insert validification hook here.
from = m_ids[m.from()];
}
face()->inject(toSealed(_json, m, from));
return true;
}
bool WebThreeStubServer::db_put(std::string const& _name, std::string const& _key, std::string const& _value)
{ {
bytes k = sha3(_name).asBytes() + sha3(_key).asBytes(); bytes k = sha3(_name).asBytes() + sha3(_key).asBytes();
bytes v = jsToBytes(_value); m_db->Put(m_writeOptions, ldb::Slice((char const*)k.data(), k.size()), ldb::Slice((char const*)_value.data(), _value.size()));
m_db->Put(m_writeOptions, ldb::Slice((char const*)k.data(), k.size()), ldb::Slice((char const*)v.data(), v.size()));
return true;
}
bool WebThreeStubServer::db_putString(std::string const& _name, std::string const& _key, std::string const& _value)
{
bytes k = sha3(_name).asBytes() + sha3(_key).asBytes();
string v = _value;
m_db->Put(m_writeOptions, ldb::Slice((char const*)k.data(), k.size()), ldb::Slice((char const*)v.data(), v.size()));
return true;
}
bool WebThreeStubServer::eth_setCoinbase(std::string const& _address)
{
client()->setAddress(jsToAddress(_address));
return true;
}
bool WebThreeStubServer::eth_setDefaultBlock(int const& _block)
{
client()->setDefault(_block);
return true;
}
bool WebThreeStubServer::eth_setListening(bool const& _listening)
{
if (_listening)
m_web3.startNetwork();
else
m_web3.stopNetwork();
return true;
}
bool WebThreeStubServer::eth_setMining(bool const& _mining)
{
if (_mining)
client()->startMining();
else
client()->stopMining();
return true;
}
Json::Value WebThreeStubServer::shh_changed(int const& _id)
{
Json::Value ret(Json::arrayValue);
auto pub = m_shhWatches[_id];
if (!pub || m_ids.count(pub))
for (h256 const& h: face()->checkWatch(_id))
{
auto e = face()->envelope(h);
shh::Message m;
if (pub)
{
cwarn << "Silently decrypting message from identity" << pub.abridged() << ": User validation hook goes here.";
m = e.open(m_ids[pub]);
if (!m)
continue;
}
else
m = e.open();
ret.append(toJson(h, e, m));
}
return ret;
}
int WebThreeStubServer::shh_newFilter(Json::Value const& _json)
{
auto w = toWatch(_json);
auto ret = face()->installWatch(w.first);
m_shhWatches.insert(make_pair(ret, w.second));
return ret;
}
bool WebThreeStubServer::shh_uninstallFilter(int const& _id)
{
face()->uninstallWatch(_id);
return true;
}
std::string WebThreeStubServer::eth_stateAt(string const& _address, string const& _storage)
{
return toJS(client()->stateAt(jsToAddress(_address), jsToU256(_storage), client()->getDefault()));
}
Json::Value WebThreeStubServer::eth_storageAt(string const& _address)
{
return toJson(client()->storageAt(jsToAddress(_address)));
}
std::string WebThreeStubServer::eth_transact(Json::Value const& _json)
{
std::string ret;
TransactionSkeleton t = toTransaction(_json);
if (!t.from && m_accounts.size())
{
auto b = m_accounts.begin()->first;
for (auto a: m_accounts)
if (client()->balanceAt(a.first) > client()->balanceAt(b))
b = a.first;
t.from = b;
}
if (!m_accounts.count(t.from))
return ret;
if (!t.gasPrice)
t.gasPrice = 10 * dev::eth::szabo;
if (!t.gas)
t.gas = min<u256>(client()->gasLimitRemaining(), client()->balanceAt(t.from) / t.gasPrice);
if (authenticate(t))
{
if (t.to)
// TODO: from qethereum, insert validification hook here.
client()->transact(m_accounts[t.from].secret(), t.value, t.to, t.data, t.gas, t.gasPrice);
else
ret = toJS(client()->transact(m_accounts[t.from].secret(), t.value, t.data, t.gas, t.gasPrice));
client()->flushTransactions();
}
return ret;
}
bool WebThreeStubServer::authenticate(TransactionSkeleton const& _t) const
{
cwarn << "Silently signing transaction from address" << _t.from.abridged() << ": User validation hook goes here.";
return true;
}
Json::Value WebThreeStubServer::eth_transactionByHash(std::string const& _hash, int const& _i)
{
return toJson(client()->transaction(jsToFixed<32>(_hash), _i));
}
Json::Value WebThreeStubServer::eth_transactionByNumber(int const& _number, int const& _i)
{
return toJson(client()->transaction(client()->hashFromNumber(_number), _i));
}
Json::Value WebThreeStubServer::eth_uncleByHash(std::string const& _hash, int const& _i)
{
return toJson(client()->uncle(jsToFixed<32>(_hash), _i));
}
Json::Value WebThreeStubServer::eth_uncleByNumber(int const& _number, int const& _i)
{
return toJson(client()->uncle(client()->hashFromNumber(_number), _i));
}
bool WebThreeStubServer::eth_uninstallFilter(int const& _id)
{
client()->uninstallWatch(_id);
return true;
} }

104
libweb3jsonrpc/WebThreeStubServer.h

@ -28,111 +28,33 @@
#include <leveldb/db.h> #include <leveldb/db.h>
#pragma warning(pop) #pragma warning(pop)
#include <iostream> #include "WebThreeStubServerBase.h"
#include <jsonrpccpp/server.h>
#include <libdevcrypto/Common.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include "abstractwebthreestubserver.h"
#pragma GCC diagnostic pop
namespace ldb = leveldb;
namespace dev namespace dev
{ {
class WebThreeDirect; class WebThreeDirect;
class KeyPair;
class TransactionSkeleton;
namespace eth
{
class Interface;
}
namespace shh
{
class Interface;
}
} }
/** /**
* @brief JSON-RPC api implementation * @brief JSON-RPC api implementation for WebThreeDirect
* @todo filters should work on unsigned instead of int
* unsigned are not supported in json-rpc-cpp and there are bugs with double in json-rpc-cpp version 0.2.1
* @todo split these up according to subprotocol (eth, shh, db, p2p, web3) and make it /very/ clear about how to add other subprotocols.
* @todo modularise everything so additional subprotocols don't need to change this file.
*/ */
class WebThreeStubServer: public AbstractWebThreeStubServer class WebThreeStubServer: public dev::WebThreeStubServerBase, public dev::WebThreeStubDatabaseFace
{ {
public: public:
WebThreeStubServer(jsonrpc::AbstractServerConnector& _conn, dev::WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts); WebThreeStubServer(jsonrpc::AbstractServerConnector& _conn, dev::WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts);
virtual std::string web3_sha3(std::string const& _param1); private:
virtual Json::Value eth_accounts(); dev::eth::Interface* client() override;
virtual std::string eth_balanceAt(std::string const& _address); std::shared_ptr<dev::shh::Interface> face() override;
virtual Json::Value eth_blockByHash(std::string const& _hash); dev::WebThreeNetworkFace* network() override;
virtual Json::Value eth_blockByNumber(int const& _number); dev::WebThreeStubDatabaseFace* db() override;
virtual std::string eth_call(Json::Value const& _json);
virtual bool eth_changed(int const& _id);
virtual std::string eth_codeAt(std::string const& _address);
virtual std::string eth_coinbase();
virtual Json::Value eth_compilers();
virtual double eth_countAt(std::string const& _address);
virtual int eth_defaultBlock();
virtual std::string eth_gasPrice();
virtual Json::Value eth_filterLogs(int const& _id);
virtual Json::Value eth_logs(Json::Value const& _json);
virtual bool eth_listening();
virtual bool eth_mining();
virtual int eth_newFilter(Json::Value const& _json);
virtual int eth_newFilterString(std::string const& _filter);
virtual int eth_number();
virtual int eth_peerCount();
virtual bool eth_setCoinbase(std::string const& _address);
virtual bool eth_setDefaultBlock(int const& _block);
virtual bool eth_setListening(bool const& _listening);
virtual std::string eth_lll(std::string const& _s);
virtual std::string eth_serpent(std::string const& _s);
virtual bool eth_setMining(bool const& _mining);
virtual std::string eth_solidity(std::string const& _code);
virtual std::string eth_stateAt(std::string const& _address, std::string const& _storage);
virtual Json::Value eth_storageAt(std::string const& _address);
virtual std::string eth_transact(Json::Value const& _json);
virtual Json::Value eth_transactionByHash(std::string const& _hash, int const& _i);
virtual Json::Value eth_transactionByNumber(int const& _number, int const& _i);
virtual Json::Value eth_uncleByHash(std::string const& _hash, int const& _i);
virtual Json::Value eth_uncleByNumber(int const& _number, int const& _i);
virtual bool eth_uninstallFilter(int const& _id);
virtual std::string db_get(std::string const& _name, std::string const& _key);
virtual std::string db_getString(std::string const& _name, std::string const& _key);
virtual bool db_put(std::string const& _name, std::string const& _key, std::string const& _value);
virtual bool db_putString(std::string const& _name, std::string const& _key, std::string const& _value);
virtual std::string shh_addToGroup(std::string const& _group, std::string const& _who);
virtual Json::Value shh_changed(int const& _id);
virtual bool shh_haveIdentity(std::string const& _id);
virtual int shh_newFilter(Json::Value const& _json);
virtual std::string shh_newGroup(std::string const& _id, std::string const& _who);
virtual std::string shh_newIdentity();
virtual bool shh_post(Json::Value const& _json);
virtual bool shh_uninstallFilter(int const& _id);
void setAccounts(std::vector<dev::KeyPair> const& _accounts);
void setIdentities(std::vector<dev::KeyPair> const& _ids);
std::map<dev::Public, dev::Secret> const& ids() const { return m_ids; }
protected: std::string get(std::string const& _name, std::string const& _key) override;
virtual bool authenticate(dev::TransactionSkeleton const& _t) const; void put(std::string const& _name, std::string const& _key, std::string const& _value) override;
private: private:
dev::eth::Interface* client() const;
std::shared_ptr<dev::shh::Interface> face() const;
dev::WebThreeDirect& m_web3; dev::WebThreeDirect& m_web3;
std::map<dev::Address, dev::KeyPair> m_accounts; leveldb::ReadOptions m_readOptions;
leveldb::WriteOptions m_writeOptions;
ldb::ReadOptions m_readOptions; leveldb::DB* m_db;
ldb::WriteOptions m_writeOptions;
ldb::DB* m_db;
std::map<dev::Public, dev::Secret> m_ids;
std::map<unsigned, dev::Public> m_shhWatches;
}; };

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save