Browse Source

Merge branch 'develop' of github.com:ethereum/cpp-ethereum into develop

cl-refactor
Gav Wood 10 years ago
parent
commit
34aa7bc9ad
  1. 3
      libdevcore/Worker.cpp
  2. 18
      libdevcore/Worker.h
  3. 1
      libethereum/EthereumHost.h
  4. 14
      libp2p/Common.cpp
  5. 1
      libp2p/Common.h
  6. 644
      libp2p/Host.cpp
  7. 68
      libp2p/Host.h
  8. 2
      libp2p/Session.cpp
  9. 63
      libsolidity/AST.cpp
  10. 43
      libsolidity/AST.h
  11. 1
      libsolidity/ASTForward.h
  12. 15
      libsolidity/Compiler.cpp
  13. 5
      libsolidity/Compiler.h
  14. 5
      libsolidity/CompilerContext.cpp
  15. 4
      libsolidity/CompilerContext.h
  16. 7
      libsolidity/CompilerStack.cpp
  17. 9
      libsolidity/CompilerStack.h
  18. 125
      libsolidity/ExpressionCompiler.cpp
  19. 13
      libsolidity/ExpressionCompiler.h
  20. 84
      libsolidity/GlobalContext.cpp
  21. 62
      libsolidity/GlobalContext.h
  22. 43
      libsolidity/NameAndTypeResolver.cpp
  23. 3
      libsolidity/NameAndTypeResolver.h
  24. 1
      libsolidity/Scanner.cpp
  25. 1
      libsolidity/Token.h
  26. 131
      libsolidity/Types.cpp
  27. 113
      libsolidity/Types.h
  28. 31
      test/TestHelper.cpp
  29. 2
      test/TestHelper.h
  30. 4
      test/solidityCompiler.cpp
  31. 70
      test/solidityEndToEndTest.cpp
  32. 2
      test/solidityExpressionCompiler.cpp
  33. 22
      test/solidityNameAndTypeResolution.cpp
  34. 68
      test/stPreCompiledContractsFiller.json
  35. 41
      test/stSpecialTestFiller.json
  36. 5
      test/state.cpp
  37. 47
      test/vm.cpp
  38. 2
      test/vm.h
  39. 4
      test/vmIOandFlowOperationsTestFiller.json
  40. 1266
      test/vmLogTestFiller.json
  41. 31
      test/vmPushDupSwapTestFiller.json

3
libdevcore/Worker.cpp

@ -41,7 +41,8 @@ void Worker::startWorking()
startedWorking(); startedWorking();
while (!m_stop) while (!m_stop)
{ {
this_thread::sleep_for(chrono::milliseconds(30)); if (m_idleWaitMs)
this_thread::sleep_for(chrono::milliseconds(m_idleWaitMs));
doWork(); doWork();
} }
cdebug << "Finishing up worker thread"; cdebug << "Finishing up worker thread";

18
libdevcore/Worker.h

@ -31,7 +31,7 @@ namespace dev
class Worker class Worker
{ {
protected: protected:
Worker(std::string const& _name = "anon"): m_name(_name) {} Worker(std::string const& _name = "anon", unsigned _idleWaitMs = 30): m_name(_name), m_idleWaitMs(_idleWaitMs) {}
/// Move-constructor. /// Move-constructor.
Worker(Worker&& _m) { std::swap(m_name, _m.m_name); } Worker(Worker&& _m) { std::swap(m_name, _m.m_name); }
@ -41,20 +41,34 @@ protected:
virtual ~Worker() { stopWorking(); } virtual ~Worker() { stopWorking(); }
/// Allows changing worker name if work is stopped.
void setName(std::string _n) { if (!isWorking()) m_name = _n; } void setName(std::string _n) { if (!isWorking()) m_name = _n; }
/// Starts worker thread; causes startedWorking() to be called.
void startWorking(); void startWorking();
/// Stop worker thread; causes call to stopWorking().
void stopWorking(); void stopWorking();
/// Returns if worker thread is present.
bool isWorking() const { Guard l(x_work); return !!m_work; } bool isWorking() const { Guard l(x_work); return !!m_work; }
/// Called after thread is started from startWorking().
virtual void startedWorking() {} virtual void startedWorking() {}
/// Called continuously following sleep for m_idleWaitMs.
virtual void doWork() = 0; virtual void doWork() = 0;
/// Called when is to be stopped, just prior to thread being joined.
virtual void doneWorking() {} virtual void doneWorking() {}
private: private:
std::string m_name;
unsigned m_idleWaitMs;
mutable Mutex x_work; ///< Lock for the network existance. mutable Mutex x_work; ///< Lock for the network existance.
std::unique_ptr<std::thread> m_work; ///< The network thread. std::unique_ptr<std::thread> m_work; ///< The network thread.
bool m_stop = false; bool m_stop = false;
std::string m_name;
}; };
} }

1
libethereum/EthereumHost.h

@ -51,6 +51,7 @@ class BlockQueue;
/** /**
* @brief The EthereumHost class * @brief The EthereumHost class
* @warning None of this is thread-safe. You have been warned. * @warning None of this is thread-safe. You have been warned.
* @doWork Syncs to peers and sends new blocks and transactions.
*/ */
class EthereumHost: public p2p::HostCapability<EthereumPeer>, Worker class EthereumHost: public p2p::HostCapability<EthereumPeer>, Worker
{ {

14
libp2p/Common.cpp

@ -53,6 +53,20 @@ bool p2p::isPrivateAddress(bi::address const& _addressToCheck)
return false; return false;
} }
// Helper function to determine if an address is localhost
bool p2p::isLocalHostAddress(bi::address const& _addressToCheck)
{
// @todo: ivp6 link-local adresses (macos), ex: fe80::1%lo0
static const set<bi::address> c_rejectAddresses = {
{bi::address_v4::from_string("127.0.0.1")},
{bi::address_v4::from_string("0.0.0.0")},
{bi::address_v6::from_string("::1")},
{bi::address_v6::from_string("::")}
};
return find(c_rejectAddresses.begin(), c_rejectAddresses.end(), _addressToCheck) != c_rejectAddresses.end();
}
std::string p2p::reasonOf(DisconnectReason _r) std::string p2p::reasonOf(DisconnectReason _r)
{ {
switch (_r) switch (_r)

1
libp2p/Common.h

@ -47,6 +47,7 @@ namespace p2p
using NodeId = h512; using NodeId = h512;
bool isPrivateAddress(bi::address const& _addressToCheck); bool isPrivateAddress(bi::address const& _addressToCheck);
bool isLocalHostAddress(bi::address const& _addressToCheck);
class UPnP; class UPnP;
class Capability; class Capability;

644
libp2p/Host.cpp

@ -46,103 +46,196 @@ using namespace std;
using namespace dev; using namespace dev;
using namespace dev::p2p; using namespace dev::p2p;
// Addresses we will skip during network interface discovery std::vector<bi::address> Host::getInterfaceAddresses()
// Use a vector as the list is small
// Why this and not names?
// Under MacOSX loopback (127.0.0.1) can be named lo0 and br0 are bridges (0.0.0.0)
static const set<bi::address> c_rejectAddresses = {
{bi::address_v4::from_string("127.0.0.1")},
{bi::address_v6::from_string("::1")},
{bi::address_v4::from_string("0.0.0.0")},
{bi::address_v6::from_string("::")}
};
Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool _start):
Worker("p2p"),
m_clientVersion(_clientVersion),
m_netPrefs(_n),
m_ioService(new ba::io_service),
m_acceptor(new bi::tcp::acceptor(*m_ioService)),
m_socket(new bi::tcp::socket(*m_ioService)),
m_key(KeyPair::create())
{ {
populateAddresses(); std::vector<bi::address> addresses;
clog(NetNote) << "Id:" << id().abridged();
if (_start)
start();
}
Host::~Host() #ifdef _WIN32
{ WSAData wsaData;
quit(); if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
} BOOST_THROW_EXCEPTION(NoNetworking());
void Host::start() char ac[80];
{ if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here. {
if (!m_ioService) clog(NetWarn) << "Error " << WSAGetLastError() << " when getting local host name.";
return; WSACleanup();
BOOST_THROW_EXCEPTION(NoNetworking());
}
if (isWorking()) struct hostent* phe = gethostbyname(ac);
stop(); if (phe == 0)
{
clog(NetWarn) << "Bad host lookup.";
WSACleanup();
BOOST_THROW_EXCEPTION(NoNetworking());
}
for (int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
char *addrStr = inet_ntoa(addr);
bi::address address(bi::address::from_string(addrStr));
if (!isLocalHostAddress(address))
addresses.push_back(ad.to_v4());
}
WSACleanup();
#else
ifaddrs* ifaddr;
if (getifaddrs(&ifaddr) == -1)
BOOST_THROW_EXCEPTION(NoNetworking());
for (auto ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr || string(ifa->ifa_name) == "lo0")
continue;
if (ifa->ifa_addr->sa_family == AF_INET)
{
in_addr addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
boost::asio::ip::address_v4 address(boost::asio::detail::socket_ops::network_to_host_long(addr.s_addr));
if (!isLocalHostAddress(address))
addresses.push_back(address);
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
sockaddr_in6* sockaddr = ((struct sockaddr_in6 *)ifa->ifa_addr);
in6_addr addr = sockaddr->sin6_addr;
boost::asio::ip::address_v6::bytes_type bytes;
memcpy(&bytes[0], addr.s6_addr, 16);
boost::asio::ip::address_v6 address(bytes, sockaddr->sin6_scope_id);
if (!isLocalHostAddress(address))
addresses.push_back(address);
}
}
if (ifaddr!=NULL)
freeifaddrs(ifaddr);
#endif
return std::move(addresses);
}
int Host::listen4(bi::tcp::acceptor* _acceptor, unsigned short _listenPort)
{
int retport = -1;
for (unsigned i = 0; i < 2; ++i) for (unsigned i = 0; i < 2; ++i)
{ {
bi::tcp::endpoint endpoint(bi::tcp::v4(), i ? 0 : m_netPrefs.listenPort); // try to connect w/listenPort, else attempt net-allocated port
bi::tcp::endpoint endpoint(bi::tcp::v4(), i ? 0 : _listenPort);
try try
{ {
m_acceptor->open(endpoint.protocol()); _acceptor->open(endpoint.protocol());
m_acceptor->set_option(ba::socket_base::reuse_address(true)); _acceptor->set_option(ba::socket_base::reuse_address(true));
m_acceptor->bind(endpoint); _acceptor->bind(endpoint);
m_acceptor->listen(); _acceptor->listen();
m_listenPort = i ? m_acceptor->local_endpoint().port() : m_netPrefs.listenPort; retport = _acceptor->local_endpoint().port();
break; break;
} }
catch (...) catch (...)
{ {
if (i) if (i)
{ {
// both attempts failed
cwarn << "Couldn't start accepting connections on host. Something very wrong with network?\n" << boost::current_exception_diagnostic_information(); cwarn << "Couldn't start accepting connections on host. Something very wrong with network?\n" << boost::current_exception_diagnostic_information();
return;
} }
m_acceptor->close();
// first attempt failed
_acceptor->close();
continue; continue;
} }
} }
return retport;
}
for (auto const& h: m_capabilities) bi::tcp::endpoint Host::traverseNAT(std::vector<bi::address> const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpifaddr)
h.second->onStarting(); {
asserts(_listenPort);
startWorking(); UPnP* upnp;
try
{
upnp = new UPnP;
}
// let m_upnp continue as null - we handle it properly.
catch (NoUPnPDevice) {}
bi::tcp::endpoint upnpep;
if (upnp && upnp->isValid())
{
bi::address paddr;
int extPort = 0;
for (auto const& addr: _ifAddresses)
if (addr.is_v4() && isPrivateAddress(addr) && (extPort = upnp->addRedirect(addr.to_string().c_str(), _listenPort)))
{
paddr = addr;
break;
}
auto eip = upnp->externalIP();
bi::address eipaddr(bi::address::from_string(eip));
if (extPort && eip != string("0.0.0.0") && !isPrivateAddress(eipaddr))
{
clog(NetNote) << "Punched through NAT and mapped local port" << _listenPort << "onto external port" << extPort << ".";
clog(NetNote) << "External addr:" << eip;
o_upnpifaddr = paddr;
upnpep = bi::tcp::endpoint(eipaddr, (unsigned short)extPort);
}
else
clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place).";
if (upnp)
delete upnp;
}
return upnpep;
} }
void Host::stop() Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool _start):
Worker("p2p", 0),
m_clientVersion(_clientVersion),
m_netPrefs(_n),
m_ifAddresses(getInterfaceAddresses()),
m_ioService(new ba::io_service),
m_acceptor(new bi::tcp::acceptor(*m_ioService)),
m_socket(new bi::tcp::socket(*m_ioService)),
m_key(KeyPair::create())
{ {
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here. for (auto address: m_ifAddresses)
if (!m_ioService) if (address.is_v4())
return; clog(NetNote) << "IP Address: " << address << " = " << (isPrivateAddress(address) ? "[LOCAL]" : "[PEER]");
for (auto const& h: m_capabilities) clog(NetNote) << "Id:" << id().abridged();
h.second->onStopping(); if (_start)
start();
}
stopWorking(); Host::~Host()
{
quit();
}
if (m_acceptor->is_open()) void Host::start()
{ {
if (m_accepting) startWorking();
m_acceptor->cancel(); }
m_acceptor->close();
m_accepting = false;
}
if (m_socket->is_open())
m_socket->close();
disconnectPeers();
if (!!m_ioService) void Host::stop()
{
{ {
m_ioService->stop(); // prevent m_run from being set to false at same time as set to true by start()
m_ioService->reset(); lock_guard<mutex> l(x_runtimer);
// once m_run is false the scheduler will shutdown network and stopWorking()
m_run = false;
} }
// we know shutdown is complete when m_timer is reset
while (m_timer)
this_thread::sleep_for(chrono::milliseconds(50));
stopWorking();
} }
void Host::quit() void Host::quit()
@ -150,7 +243,8 @@ void Host::quit()
// called to force io_service to kill any remaining tasks it might have - // called to force io_service to kill any remaining tasks it might have -
// such tasks may involve socket reads from Capabilities that maintain references // such tasks may involve socket reads from Capabilities that maintain references
// to resources we're about to free. // to resources we're about to free.
stop(); if (isWorking())
stop();
m_acceptor.reset(); m_acceptor.reset();
m_socket.reset(); m_socket.reset();
m_ioService.reset(); m_ioService.reset();
@ -183,33 +277,6 @@ void Host::registerPeer(std::shared_ptr<Session> _s, CapDescs const& _caps)
} }
} }
void Host::disconnectPeers()
{
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here.
if (!m_ioService)
return;
for (unsigned n = 0;; n = 0)
{
{
RecursiveGuard l(x_peers);
for (auto i: m_peers)
if (auto p = i.second.lock())
{
p->disconnect(ClientQuit);
n++;
}
}
if (!n)
break;
m_ioService->poll();
this_thread::sleep_for(chrono::milliseconds(100));
}
delete m_upnp;
m_upnp = nullptr;
}
void Host::seal(bytes& _b) void Host::seal(bytes& _b)
{ {
_b[0] = 0x22; _b[0] = 0x22;
@ -223,165 +290,6 @@ void Host::seal(bytes& _b)
_b[7] = len & 0xff; _b[7] = len & 0xff;
} }
void Host::determinePublic(string const& _publicAddress, bool _upnp)
{
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here.
if (!m_ioService)
return;
if (_upnp)
try
{
m_upnp = new UPnP;
}
catch (NoUPnPDevice) {} // let m_upnp continue as null - we handle it properly.
bi::tcp::resolver r(*m_ioService);
if (m_upnp && m_upnp->isValid() && m_peerAddresses.size())
{
clog(NetNote) << "External addr:" << m_upnp->externalIP();
int p;
for (auto const& addr : m_peerAddresses)
if ((p = m_upnp->addRedirect(addr.to_string().c_str(), m_listenPort)))
break;
if (p)
clog(NetNote) << "Punched through NAT and mapped local port" << m_listenPort << "onto external port" << p << ".";
else
{
// couldn't map
clog(NetWarn) << "Couldn't punch through NAT (or no NAT in place). Assuming" << m_listenPort << "is local & external port.";
p = m_listenPort;
}
auto eip = m_upnp->externalIP();
if (eip == string("0.0.0.0") && _publicAddress.empty())
m_public = bi::tcp::endpoint(bi::address(), (unsigned short)p);
else
{
bi::address adr = adr = bi::address::from_string(eip);
try
{
adr = bi::address::from_string(_publicAddress);
}
catch (...) {}
m_public = bi::tcp::endpoint(adr, (unsigned short)p);
m_addresses.push_back(m_public.address());
}
}
else
{
// No UPnP - fallback on given public address or, if empty, the assumed peer address.
bi::address adr = m_peerAddresses.size() ? m_peerAddresses[0] : bi::address();
try
{
adr = bi::address::from_string(_publicAddress);
}
catch (...) {}
m_public = bi::tcp::endpoint(adr, m_listenPort);
m_addresses.push_back(adr);
}
}
void Host::populateAddresses()
{
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here.
if (!m_ioService)
return;
#ifdef _WIN32
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
BOOST_THROW_EXCEPTION(NoNetworking());
char ac[80];
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
{
clog(NetWarn) << "Error " << WSAGetLastError() << " when getting local host name.";
WSACleanup();
BOOST_THROW_EXCEPTION(NoNetworking());
}
struct hostent* phe = gethostbyname(ac);
if (phe == 0)
{
clog(NetWarn) << "Bad host lookup.";
WSACleanup();
BOOST_THROW_EXCEPTION(NoNetworking());
}
for (int i = 0; phe->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
char *addrStr = inet_ntoa(addr);
bi::address ad(bi::address::from_string(addrStr));
m_addresses.push_back(ad.to_v4());
bool isLocal = std::find(c_rejectAddresses.begin(), c_rejectAddresses.end(), ad) != c_rejectAddresses.end();
if (!isLocal)
m_peerAddresses.push_back(ad.to_v4());
clog(NetNote) << "Address: " << ac << " = " << m_addresses.back() << (isLocal ? " [LOCAL]" : " [PEER]");
}
WSACleanup();
#else
ifaddrs* ifaddr;
if (getifaddrs(&ifaddr) == -1)
BOOST_THROW_EXCEPTION(NoNetworking());
bi::tcp::resolver r(*m_ioService);
for (ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr)
continue;
if (ifa->ifa_addr->sa_family == AF_INET)
{
char host[NI_MAXHOST];
if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
continue;
try
{
auto it = r.resolve({host, "30303"});
bi::tcp::endpoint ep = it->endpoint();
bi::address ad = ep.address();
m_addresses.push_back(ad.to_v4());
bool isLocal = std::find(c_rejectAddresses.begin(), c_rejectAddresses.end(), ad) != c_rejectAddresses.end();
if (!isLocal)
m_peerAddresses.push_back(ad.to_v4());
clog(NetNote) << "Address: " << host << " = " << m_addresses.back() << (isLocal ? " [LOCAL]" : " [PEER]");
}
catch (...)
{
clog(NetNote) << "Couldn't resolve: " << host;
}
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
char host[NI_MAXHOST];
if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
continue;
try
{
auto it = r.resolve({host, "30303"});
bi::tcp::endpoint ep = it->endpoint();
bi::address ad = ep.address();
m_addresses.push_back(ad.to_v6());
bool isLocal = std::find(c_rejectAddresses.begin(), c_rejectAddresses.end(), ad) != c_rejectAddresses.end();
if (!isLocal)
m_peerAddresses.push_back(ad);
clog(NetNote) << "Address: " << host << " = " << m_addresses.back() << (isLocal ? " [LOCAL]" : " [PEER]");
}
catch (...)
{
clog(NetNote) << "Couldn't resolve: " << host;
}
}
}
freeifaddrs(ifaddr);
#endif
}
shared_ptr<Node> Host::noteNode(NodeId _id, bi::tcp::endpoint _a, Origin _o, bool _ready, NodeId _oldId) shared_ptr<Node> Host::noteNode(NodeId _id, bi::tcp::endpoint _a, Origin _o, bool _ready, NodeId _oldId)
{ {
RecursiveGuard l(x_peers); RecursiveGuard l(x_peers);
@ -459,10 +367,73 @@ Nodes Host::potentialPeers(RangeMask<unsigned> const& _known)
return ret; return ret;
} }
void Host::determinePublic(string const& _publicAddress, bool _upnp)
{
m_peerAddresses.clear();
// no point continuing if there are no interface addresses or valid listen port
if (!m_ifAddresses.size() || m_listenPort < 1)
return;
// populate interfaces we'll listen on (eth listens on all interfaces); ignores local
for (auto addr: m_ifAddresses)
if ((m_netPrefs.localNetworking || !isPrivateAddress(addr)) && !isLocalHostAddress(addr))
m_peerAddresses.insert(addr);
// if user supplied address is a public address then we use it
// if user supplied address is private, and localnetworking is enabled, we use it
bi::address reqpublicaddr(bi::address(_publicAddress.empty() ? bi::address() : bi::address::from_string(_publicAddress)));
bi::tcp::endpoint reqpublic(reqpublicaddr, m_listenPort);
bool isprivate = isPrivateAddress(reqpublicaddr);
bool ispublic = !isprivate && !isLocalHostAddress(reqpublicaddr);
if (!reqpublicaddr.is_unspecified() && (ispublic || (isprivate && m_netPrefs.localNetworking)))
{
if (!m_peerAddresses.count(reqpublicaddr))
m_peerAddresses.insert(reqpublicaddr);
m_public = reqpublic;
return;
}
// if address wasn't provided, then use first public ipv4 address found
for (auto addr: m_peerAddresses)
if (addr.is_v4() && !isPrivateAddress(addr))
{
m_public = bi::tcp::endpoint(*m_peerAddresses.begin(), m_listenPort);
return;
}
// or find address via upnp
if (_upnp)
{
bi::address upnpifaddr;
bi::tcp::endpoint upnpep = traverseNAT(m_ifAddresses, m_listenPort, upnpifaddr);
if (!upnpep.address().is_unspecified() && !upnpifaddr.is_unspecified())
{
if (!m_peerAddresses.count(upnpep.address()))
m_peerAddresses.insert(upnpep.address());
m_public = upnpep;
return;
}
}
// or if no address provided, use private ipv4 address if local networking is enabled
if (reqpublicaddr.is_unspecified())
if (m_netPrefs.localNetworking)
for (auto addr: m_peerAddresses)
if (addr.is_v4() && isPrivateAddress(addr))
{
m_public = bi::tcp::endpoint(addr, m_listenPort);
return;
}
// otherwise address is unspecified
m_public = bi::tcp::endpoint(bi::address(), m_listenPort);
}
void Host::ensureAccepting() void Host::ensureAccepting()
{ {
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here. // return if there's no io-server (quit called) or we're not listening
if (!m_ioService) if (!m_ioService || m_listenPort < 1)
return; return;
if (!m_accepting) if (!m_accepting)
@ -654,12 +625,9 @@ void Host::growPeers()
return; return;
} }
else else
{
ensureAccepting();
for (auto const& i: m_peers) for (auto const& i: m_peers)
if (auto p = i.second.lock()) if (auto p = i.second.lock())
p->ensureNodesRequested(); p->ensureNodesRequested();
}
} }
} }
@ -718,45 +686,153 @@ PeerInfos Host::peers(bool _updatePing) const
return ret; return ret;
} }
void Host::startedWorking() void Host::run(boost::system::error_code const& error)
{ {
determinePublic(m_netPrefs.publicIP, m_netPrefs.upnp); static unsigned s_lasttick = 0;
ensureAccepting(); s_lasttick += c_timerInterval;
if (error || !m_ioService)
{
// timer died or io service went away, so stop here
m_timer.reset();
return;
}
if (!m_public.address().is_unspecified() && (m_nodes.empty() || m_nodes[m_nodesList[0]]->id != id())) // network running
noteNode(id(), m_public, Origin::Perfect, false); if (m_run)
{
if (s_lasttick >= c_timerInterval * 50)
{
growPeers();
prunePeers();
s_lasttick = 0;
}
clog(NetNote) << "Id:" << id().abridged(); if (m_hadNewNodes)
} {
for (auto p: m_peers)
if (auto pp = p.second.lock())
pp->serviceNodesRequest();
void Host::doWork() m_hadNewNodes = false;
{ }
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here.
if (asserts(!!m_ioService))
return;
growPeers(); if (chrono::steady_clock::now() - m_lastPing > chrono::seconds(30)) // ping every 30s.
prunePeers(); {
for (auto p: m_peers)
if (auto pp = p.second.lock())
if (chrono::steady_clock::now() - pp->m_lastReceived > chrono::seconds(60))
pp->disconnect(PingTimeout);
pingAll();
}
auto runcb = [this](boost::system::error_code const& error) -> void { run(error); };
m_timer->expires_from_now(boost::posix_time::milliseconds(c_timerInterval));
m_timer->async_wait(runcb);
return;
}
if (m_hadNewNodes) // network stopping
if (!m_run)
{ {
for (auto p: m_peers) // close acceptor
if (auto pp = p.second.lock()) if (m_acceptor->is_open())
pp->serviceNodesRequest(); {
if (m_accepting)
m_acceptor->cancel();
m_acceptor->close();
m_accepting = false;
}
// stop capabilities (eth: stops syncing or block/tx broadcast)
for (auto const& h: m_capabilities)
h.second->onStopping();
// disconnect peers
for (unsigned n = 0;; n = 0)
{
{
RecursiveGuard l(x_peers);
for (auto i: m_peers)
if (auto p = i.second.lock())
if (p->isOpen())
{
p->disconnect(ClientQuit);
n++;
}
}
if (!n)
break;
this_thread::sleep_for(chrono::milliseconds(100));
}
m_hadNewNodes = false; if (m_socket->is_open())
m_socket->close();
// m_run is false, so we're stopping; kill timer
s_lasttick = 0;
// causes parent thread's stop() to continue which calls stopWorking()
m_timer.reset();
// stop ioservice (stops blocking worker thread, allowing thread to join)
if (!!m_ioService)
m_ioService->stop();
return;
} }
}
if (chrono::steady_clock::now() - m_lastPing > chrono::seconds(30)) // ping every 30s. void Host::startedWorking()
{
if (!m_timer)
{ {
for (auto p: m_peers) // no timer means this is first run and network must be started
if (auto pp = p.second.lock()) // (run once when host worker thread calls startedWorking())
if (chrono::steady_clock::now() - pp->m_lastReceived > chrono::seconds(60))
pp->disconnect(PingTimeout); {
pingAll(); // prevent m_run from being set to true at same time as set to false by stop()
// don't release mutex until m_timer is set so in case stop() is called at same
// time, stop will wait on m_timer and graceful network shutdown.
lock_guard<mutex> l(x_runtimer);
// reset io service and create deadline timer
m_timer.reset(new boost::asio::deadline_timer(*m_ioService));
m_run = true;
}
m_ioService->reset();
// try to open acceptor (todo: ipv6)
m_listenPort = listen4(m_acceptor.get(), m_netPrefs.listenPort);
// start capability threads
for (auto const& h: m_capabilities)
h.second->onStarting();
// determine public IP, but only if we're able to listen for connections
// todo: GUI when listen is unavailable in UI
if (m_listenPort)
{
determinePublic(m_netPrefs.publicIP, m_netPrefs.upnp);
ensureAccepting();
}
// if m_public address is valid then add us to node list
// todo: abstract empty() and emplace logic
if (!m_public.address().is_unspecified() && (m_nodes.empty() || m_nodes[m_nodesList[0]]->id != id()))
noteNode(id(), m_public, Origin::Perfect, false);
clog(NetNote) << "Id:" << id().abridged();
} }
m_ioService->poll(); run(boost::system::error_code());
}
void Host::doWork()
{
// no ioService means we've had quit() called - bomb out - we're not allowed in here.
if (asserts(!!m_ioService))
return;
m_ioService->run();
} }
void Host::pingAll() void Host::pingAll()

68
libp2p/Host.h

@ -121,6 +121,17 @@ class Host: public Worker
friend class HostCapabilityFace; friend class HostCapabilityFace;
friend struct Node; friend struct Node;
/// Static network interface methods
public:
/// @returns public and private interface addresses
static std::vector<bi::address> getInterfaceAddresses();
/// Try to bind and listen on _listenPort, else attempt net-allocated port.
static int listen4(bi::tcp::acceptor* _acceptor, unsigned short _listenPort);
/// Return public endpoint of upnp interface. If successful o_upnpifaddr will be a private interface address and endpoint will contain public address and port.
static bi::tcp::endpoint traverseNAT(std::vector<bi::address> const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpifaddr);
public: public:
/// Start server, listening for connections on the given port. /// Start server, listening for connections on the given port.
Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences(), bool _start = false); Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences(), bool _start = false);
@ -128,9 +139,6 @@ public:
/// Will block on network process events. /// Will block on network process events.
virtual ~Host(); virtual ~Host();
/// Closes all peers.
void disconnectPeers();
/// Basic peer network protocol version. /// Basic peer network protocol version.
unsigned protocolVersion() const; unsigned protocolVersion() const;
@ -147,7 +155,7 @@ public:
void connect(bi::tcp::endpoint const& _ep); void connect(bi::tcp::endpoint const& _ep);
void connect(std::shared_ptr<Node> const& _n); void connect(std::shared_ptr<Node> const& _n);
/// @returns true iff we have the a peer of the given id. /// @returns true iff we have a peer of the given id.
bool havePeer(NodeId _id) const; bool havePeer(NodeId _id) const;
/// Set ideal number of peers. /// Set ideal number of peers.
@ -175,10 +183,16 @@ public:
void setNetworkPreferences(NetworkPreferences const& _p) { auto had = isStarted(); if (had) stop(); m_netPrefs = _p; if (had) start(); } void setNetworkPreferences(NetworkPreferences const& _p) { auto had = isStarted(); if (had) stop(); m_netPrefs = _p; if (had) start(); }
/// Start network. @threadsafe
void start(); void start();
/// Stop network. @threadsafe
void stop(); void stop();
bool isStarted() const { return isWorking(); }
/// @returns if network is running.
bool isStarted() const { return m_run; }
/// Reset acceptor, socket, and IO service. Called by deallocator. Maybe called by implementation when ordered deallocation is required.
void quit(); void quit();
NodeId id() const { return m_key.pub(); } NodeId id() const { return m_key.pub(); }
@ -188,38 +202,48 @@ public:
std::shared_ptr<Node> node(NodeId _id) const { if (m_nodes.count(_id)) return m_nodes.at(_id); return std::shared_ptr<Node>(); } std::shared_ptr<Node> node(NodeId _id) const { if (m_nodes.count(_id)) return m_nodes.at(_id); return std::shared_ptr<Node>(); }
private: private:
void seal(bytes& _b); /// Populate m_peerAddresses with available public addresses.
void populateAddresses();
void determinePublic(std::string const& _publicAddress, bool _upnp); void determinePublic(std::string const& _publicAddress, bool _upnp);
void ensureAccepting(); void ensureAccepting();
void seal(bytes& _b);
void growPeers(); void growPeers();
void prunePeers(); void prunePeers();
/// Called by Worker. Not thread-safe; to be called only by worker.
virtual void startedWorking(); virtual void startedWorking();
/// Called by startedWorking. Not thread-safe; to be called only be worker callback.
void run(boost::system::error_code const& error); ///< Run network. Called serially via ASIO deadline timer. Manages connection state transitions.
/// Conduct I/O, polling, syncing, whatever. /// Run network
/// Ideally all time-consuming I/O is done in a background thread or otherwise asynchronously, but you get this call every 100ms or so anyway.
/// This won't touch alter the blockchain.
virtual void doWork(); virtual void doWork();
std::shared_ptr<Node> noteNode(NodeId _id, bi::tcp::endpoint _a, Origin _o, bool _ready, NodeId _oldId = NodeId()); std::shared_ptr<Node> noteNode(NodeId _id, bi::tcp::endpoint _a, Origin _o, bool _ready, NodeId _oldId = NodeId());
Nodes potentialPeers(RangeMask<unsigned> const& _known); Nodes potentialPeers(RangeMask<unsigned> const& _known);
bool m_run = false; ///< Whether network is running.
std::mutex x_runtimer; ///< Start/stop mutex.
std::string m_clientVersion; ///< Our version string. std::string m_clientVersion; ///< Our version string.
NetworkPreferences m_netPrefs; ///< Network settings. NetworkPreferences m_netPrefs; ///< Network settings.
/// Interface addresses (private, public)
std::vector<bi::address> m_ifAddresses; ///< Interface addresses.
static const int NetworkStopped = -1; ///< The value meaning we're not actually listening. int m_listenPort = -1; ///< What port are we listening on. -1 means binding failed or acceptor hasn't been initialized.
int m_listenPort = NetworkStopped; ///< What port are we listening on?
std::unique_ptr<ba::io_service> m_ioService; ///< IOService for network stuff. std::unique_ptr<ba::io_service> m_ioService; ///< IOService for network stuff.
std::unique_ptr<bi::tcp::acceptor> m_acceptor; ///< Listening acceptor. std::unique_ptr<bi::tcp::acceptor> m_acceptor; ///< Listening acceptor.
std::unique_ptr<bi::tcp::socket> m_socket; ///< Listening socket. std::unique_ptr<bi::tcp::socket> m_socket; ///< Listening socket.
std::unique_ptr<boost::asio::deadline_timer> m_timer; ///< Timer which, when network is running, calls scheduler() every c_timerInterval ms.
static const unsigned c_timerInterval = 100; ///< Interval which m_timer is run when network is connected.
UPnP* m_upnp = nullptr; ///< UPnP helper. bi::tcp::endpoint m_public; ///< Our public listening endpoint.
bi::tcp::endpoint m_public; ///< Our public listening endpoint. KeyPair m_key; ///< Our unique ID.
KeyPair m_key; ///< Our unique ID.
bool m_hadNewNodes = false; bool m_hadNewNodes = false;
@ -237,13 +261,11 @@ private:
std::vector<NodeId> m_nodesList; std::vector<NodeId> m_nodesList;
RangeMask<unsigned> m_ready; ///< Indices into m_nodesList over to which nodes we are not currently connected, connecting or otherwise ignoring. RangeMask<unsigned> m_ready; ///< Indices into m_nodesList over to which nodes we are not currently connected, connecting or otherwise ignoring.
RangeMask<unsigned> m_private; ///< Indices into m_nodesList over to which nodes are private. RangeMask<unsigned> m_private; ///< Indices into m_nodesList over to which nodes are private.
unsigned m_idealPeerCount = 5; ///< Ideal number of peers to be connected to. unsigned m_idealPeerCount = 5; ///< Ideal number of peers to be connected to.
// Our addresses. std::set<bi::address> m_peerAddresses; ///< Public addresses that peers (can) know us by.
std::vector<bi::address> m_addresses; ///< Addresses for us.
std::vector<bi::address> m_peerAddresses; ///< Addresses that peers (can) know us by.
// Our capabilities. // Our capabilities.
std::map<CapDesc, std::shared_ptr<HostCapabilityFace>> m_capabilities; ///< Each of the capabilities we support. std::map<CapDesc, std::shared_ptr<HostCapabilityFace>> m_capabilities; ///< Each of the capabilities we support.

2
libp2p/Session.cpp

@ -344,7 +344,7 @@ bool Session::interpret(RLP const& _r)
// goto CONTINUE; // Wierd port. // goto CONTINUE; // Wierd port.
// Avoid our random other addresses that they might end up giving us. // Avoid our random other addresses that they might end up giving us.
for (auto i: m_server->m_addresses) for (auto i: m_server->m_peerAddresses)
if (ep.address() == i && ep.port() == m_server->listenPort()) if (ep.address() == i && ep.port() == m_server->listenPort())
goto CONTINUE; goto CONTINUE;

63
libsolidity/AST.cpp

@ -51,6 +51,40 @@ void StructDefinition::accept(ASTVisitor& _visitor)
_visitor.endVisit(*this); _visitor.endVisit(*this);
} }
void StructDefinition::checkValidityOfMembers()
{
checkMemberTypes();
checkRecursion();
}
void StructDefinition::checkMemberTypes()
{
for (ASTPointer<VariableDeclaration> const& member: getMembers())
if (!member->getType()->canBeStored())
BOOST_THROW_EXCEPTION(member->createTypeError("Type cannot be used in struct."));
}
void StructDefinition::checkRecursion()
{
set<StructDefinition const*> definitionsSeen;
vector<StructDefinition const*> queue = {this};
while (!queue.empty())
{
StructDefinition const* def = queue.back();
queue.pop_back();
if (definitionsSeen.count(def))
BOOST_THROW_EXCEPTION(ParserError() << errinfo_sourceLocation(def->getLocation())
<< errinfo_comment("Recursive struct definition."));
definitionsSeen.insert(def);
for (ASTPointer<VariableDeclaration> const& member: def->getMembers())
if (member->getType()->getCategory() == Type::Category::STRUCT)
{
UserDefinedTypeName const& typeName = dynamic_cast<UserDefinedTypeName&>(*member->getTypeName());
queue.push_back(&dynamic_cast<StructDefinition const&>(*typeName.getReferencedDeclaration()));
}
}
}
void ParameterList::accept(ASTVisitor& _visitor) void ParameterList::accept(ASTVisitor& _visitor)
{ {
if (_visitor.visit(*this)) if (_visitor.visit(*this))
@ -258,7 +292,7 @@ void Literal::accept(ASTVisitor& _visitor)
_visitor.endVisit(*this); _visitor.endVisit(*this);
} }
TypeError ASTNode::createTypeError(string const& _description) TypeError ASTNode::createTypeError(string const& _description) const
{ {
return TypeError() << errinfo_sourceLocation(getLocation()) << errinfo_comment(_description); return TypeError() << errinfo_sourceLocation(getLocation()) << errinfo_comment(_description);
} }
@ -338,8 +372,6 @@ void VariableDefinition::checkTypeRequirements()
m_variable->setType(m_value->getType()); m_variable->setType(m_value->getType());
} }
} }
if (m_variable->getType() && !m_variable->getType()->canLiveOutsideStorage())
BOOST_THROW_EXCEPTION(m_variable->createTypeError("Type is required to live outside storage."));
} }
void Assignment::checkTypeRequirements() void Assignment::checkTypeRequirements()
@ -432,8 +464,6 @@ void FunctionCall::checkTypeRequirements()
} }
else else
{ {
m_expression->requireLValue();
//@todo would be nice to create a struct type from the arguments //@todo would be nice to create a struct type from the arguments
// and then ask if that is implicitly convertible to the struct represented by the // and then ask if that is implicitly convertible to the struct represented by the
// function parameters // function parameters
@ -461,29 +491,23 @@ bool FunctionCall::isTypeConversion() const
void MemberAccess::checkTypeRequirements() void MemberAccess::checkTypeRequirements()
{ {
m_expression->checkTypeRequirements(); m_expression->checkTypeRequirements();
m_expression->requireLValue(); Type const& type = *m_expression->getType();
if (m_expression->getType()->getCategory() != Type::Category::STRUCT) m_type = type.getMemberType(*m_memberName);
BOOST_THROW_EXCEPTION(createTypeError("Member access to a non-struct (is " + if (!m_type)
m_expression->getType()->toString() + ")"));
StructType const& type = dynamic_cast<StructType const&>(*m_expression->getType());
unsigned memberIndex = type.memberNameToIndex(*m_memberName);
if (memberIndex >= type.getMemberCount())
BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found in " + type.toString())); BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found in " + type.toString()));
m_type = type.getMemberByIndex(memberIndex).getType(); m_isLvalue = (type.getCategory() == Type::Category::STRUCT && m_type->getCategory() != Type::Category::MAPPING);
m_isLvalue = true;
} }
void IndexAccess::checkTypeRequirements() void IndexAccess::checkTypeRequirements()
{ {
m_base->checkTypeRequirements(); m_base->checkTypeRequirements();
m_base->requireLValue();
if (m_base->getType()->getCategory() != Type::Category::MAPPING) if (m_base->getType()->getCategory() != Type::Category::MAPPING)
BOOST_THROW_EXCEPTION(m_base->createTypeError("Indexed expression has to be a mapping (is " + BOOST_THROW_EXCEPTION(m_base->createTypeError("Indexed expression has to be a mapping (is " +
m_base->getType()->toString() + ")")); m_base->getType()->toString() + ")"));
MappingType const& type = dynamic_cast<MappingType const&>(*m_base->getType()); MappingType const& type = dynamic_cast<MappingType const&>(*m_base->getType());
m_index->expectType(*type.getKeyType()); m_index->expectType(*type.getKeyType());
m_type = type.getValueType(); m_type = type.getValueType();
m_isLvalue = true; m_isLvalue = m_type->getCategory() != Type::Category::MAPPING;
} }
void Identifier::checkTypeRequirements() void Identifier::checkTypeRequirements()
@ -515,7 +539,6 @@ void Identifier::checkTypeRequirements()
// 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>(*functionDef); m_type = make_shared<FunctionType>(*functionDef);
m_isLvalue = true;
return; return;
} }
ContractDefinition* contractDef = dynamic_cast<ContractDefinition*>(m_referencedDeclaration); ContractDefinition* contractDef = dynamic_cast<ContractDefinition*>(m_referencedDeclaration);
@ -524,6 +547,12 @@ void Identifier::checkTypeRequirements()
m_type = make_shared<TypeType>(make_shared<ContractType>(*contractDef)); m_type = make_shared<TypeType>(make_shared<ContractType>(*contractDef));
return; return;
} }
MagicVariableDeclaration* magicVariable = dynamic_cast<MagicVariableDeclaration*>(m_referencedDeclaration);
if (magicVariable)
{
m_type = magicVariable->getType();
return;
}
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Declaration reference of unknown/forbidden type.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Declaration reference of unknown/forbidden type."));
} }

43
libsolidity/AST.h

@ -66,7 +66,7 @@ public:
/// Creates a @ref TypeError exception and decorates it with the location of the node and /// Creates a @ref TypeError exception and decorates it with the location of the node and
/// the given description /// the given description
TypeError createTypeError(std::string const& _description); TypeError createTypeError(std::string const& _description) const;
///@{ ///@{
///@name equality operators ///@name equality operators
@ -122,6 +122,7 @@ public:
/// Returns the functions that make up the calling interface in the intended order. /// Returns the functions that make up the calling interface in the intended order.
std::vector<FunctionDefinition const*> getInterfaceFunctions() const; std::vector<FunctionDefinition const*> getInterfaceFunctions() const;
private: private:
std::vector<ASTPointer<StructDefinition>> m_definedStructs; std::vector<ASTPointer<StructDefinition>> m_definedStructs;
std::vector<ASTPointer<VariableDeclaration>> m_stateVariables; std::vector<ASTPointer<VariableDeclaration>> m_stateVariables;
@ -139,7 +140,14 @@ public:
std::vector<ASTPointer<VariableDeclaration>> const& getMembers() const { return m_members; } std::vector<ASTPointer<VariableDeclaration>> const& getMembers() const { return m_members; }
/// Checks that the members do not include any recursive structs and have valid types
/// (e.g. no functions).
void checkValidityOfMembers();
private: private:
void checkMemberTypes();
void checkRecursion();
std::vector<ASTPointer<VariableDeclaration>> m_members; std::vector<ASTPointer<VariableDeclaration>> m_members;
}; };
@ -188,6 +196,7 @@ public:
/// Checks that all parameters have allowed types and calls checkTypeRequirements on the body. /// Checks that all parameters have allowed types and calls checkTypeRequirements on the body.
void checkTypeRequirements(); void checkTypeRequirements();
private: private:
bool m_isPublic; bool m_isPublic;
ASTPointer<ParameterList> m_parameters; ASTPointer<ParameterList> m_parameters;
@ -210,7 +219,6 @@ public:
Declaration(_location, _name), m_typeName(_type) {} Declaration(_location, _name), m_typeName(_type) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
bool isTypeGivenExplicitly() const { return bool(m_typeName); }
TypeName* getTypeName() const { return m_typeName.get(); } TypeName* getTypeName() const { return m_typeName.get(); }
/// Returns the declared or inferred type. Can be an empty pointer if no type was explicitly /// Returns the declared or inferred type. Can be an empty pointer if no type was explicitly
@ -224,6 +232,27 @@ private:
std::shared_ptr<Type const> m_type; ///< derived type, initially empty std::shared_ptr<Type const> m_type; ///< derived type, initially empty
}; };
/**
* Pseudo AST node that is used as declaration for "this", "msg", "tx" and "block" when the
* identifier is encountered. Will never have a valid location in the source code.
*/
class MagicVariableDeclaration: public Declaration
{
public:
enum class VariableKind { THIS, MSG, TX, BLOCK };
MagicVariableDeclaration(VariableKind _kind, ASTString const& _name, std::shared_ptr<Type const> const& _type):
Declaration(Location(), std::make_shared<ASTString>(_name)), m_kind(_kind), m_type(_type) {}
virtual void accept(ASTVisitor&) override { BOOST_THROW_EXCEPTION(InternalCompilerError()
<< errinfo_comment("MagicVariableDeclaration used inside real AST.")); }
std::shared_ptr<Type const> const& getType() const { return m_type; }
VariableKind getKind() const { return m_kind; }
private:
VariableKind m_kind;
std::shared_ptr<Type const> m_type;
};
/// Types /// Types
/// @{ /// @{
@ -238,6 +267,7 @@ public:
/// Retrieve the element of the type hierarchy this node refers to. Can return an empty shared /// Retrieve the element of the type hierarchy this node refers to. Can return an empty shared
/// pointer until the types have been resolved using the @ref NameAndTypeResolver. /// pointer until the types have been resolved using the @ref NameAndTypeResolver.
/// If it returns an empty shared pointer after that, this indicates that the type was not found.
virtual std::shared_ptr<Type> toType() const = 0; virtual std::shared_ptr<Type> toType() const = 0;
}; };
@ -263,8 +293,7 @@ private:
}; };
/** /**
* Name referring to a user-defined type (i.e. a struct). * Name referring to a user-defined type (i.e. a struct, contract, etc.).
* @todo some changes are necessary if this is also used to refer to contract types later
*/ */
class UserDefinedTypeName: public TypeName class UserDefinedTypeName: public TypeName
{ {
@ -275,13 +304,13 @@ public:
virtual std::shared_ptr<Type> toType() const override { return Type::fromUserDefinedTypeName(*this); } virtual std::shared_ptr<Type> toType() const override { return Type::fromUserDefinedTypeName(*this); }
ASTString const& getName() const { return *m_name; } ASTString const& getName() const { return *m_name; }
void setReferencedStruct(StructDefinition& _referencedStruct) { m_referencedStruct = &_referencedStruct; } void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; }
StructDefinition const* getReferencedStruct() const { return m_referencedStruct; } Declaration const* getReferencedDeclaration() const { return m_referencedDeclaration; }
private: private:
ASTPointer<ASTString> m_name; ASTPointer<ASTString> m_name;
StructDefinition* m_referencedStruct; Declaration* m_referencedDeclaration;
}; };
/** /**

1
libsolidity/ASTForward.h

@ -40,6 +40,7 @@ class StructDefinition;
class ParameterList; class ParameterList;
class FunctionDefinition; class FunctionDefinition;
class VariableDeclaration; class VariableDeclaration;
class MagicVariableDeclaration;
class TypeName; class TypeName;
class ElementaryTypeName; class ElementaryTypeName;
class UserDefinedTypeName; class UserDefinedTypeName;

15
libsolidity/Compiler.cpp

@ -32,17 +32,13 @@ using namespace std;
namespace dev { namespace dev {
namespace solidity { namespace solidity {
bytes Compiler::compile(ContractDefinition& _contract, bool _optimize) void Compiler::compileContract(ContractDefinition& _contract, vector<MagicVariableDeclaration const*> const& _magicGlobals)
{
Compiler compiler;
compiler.compileContract(_contract);
return compiler.m_context.getAssembledBytecode(_optimize);
}
void Compiler::compileContract(ContractDefinition& _contract)
{ {
m_context = CompilerContext(); // clear it just in case m_context = CompilerContext(); // clear it just in case
for (MagicVariableDeclaration const* variable: _magicGlobals)
m_context.addMagicGlobal(*variable);
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions()) for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
if (function->getName() != _contract.getName()) // don't add the constructor here if (function->getName() != _contract.getName()) // don't add the constructor here
m_context.addFunction(*function); m_context.addFunction(*function);
@ -328,7 +324,8 @@ bool Compiler::visit(ExpressionStatement& _expressionStatement)
{ {
Expression& expression = _expressionStatement.getExpression(); Expression& expression = _expressionStatement.getExpression();
ExpressionCompiler::compileExpression(m_context, expression); ExpressionCompiler::compileExpression(m_context, expression);
if (expression.getType()->getCategory() != Type::Category::VOID) Type::Category category = expression.getType()->getCategory();
if (category != Type::Category::VOID && category != Type::Category::MAGIC)
m_context << eth::Instruction::POP; m_context << eth::Instruction::POP;
return false; return false;
} }

5
libsolidity/Compiler.h

@ -32,13 +32,10 @@ class Compiler: private ASTVisitor
public: public:
Compiler(): m_returnTag(m_context.newTag()) {} Compiler(): m_returnTag(m_context.newTag()) {}
void compileContract(ContractDefinition& _contract); void compileContract(ContractDefinition& _contract, std::vector<MagicVariableDeclaration const*> const& _magicGlobals);
bytes getAssembledBytecode(bool _optimize = false) { return m_context.getAssembledBytecode(_optimize); } bytes getAssembledBytecode(bool _optimize = false) { return m_context.getAssembledBytecode(_optimize); }
void streamAssembly(std::ostream& _stream) const { m_context.streamAssembly(_stream); } void streamAssembly(std::ostream& _stream) const { m_context.streamAssembly(_stream); }
/// Compile the given contract and return the EVM bytecode.
static bytes compile(ContractDefinition& _contract, bool _optimize);
private: private:
/// Creates a new compiler context / assembly, packs the current code into the data part and /// Creates a new compiler context / assembly, packs the current code into the data part and
/// adds the constructor code. /// adds the constructor code.

5
libsolidity/CompilerContext.cpp

@ -30,6 +30,11 @@ using namespace std;
namespace dev { namespace dev {
namespace solidity { namespace solidity {
void CompilerContext::addMagicGlobal(MagicVariableDeclaration const& _declaration)
{
m_magicGlobals.insert(&_declaration);
}
void CompilerContext::addStateVariable(VariableDeclaration const& _declaration) void CompilerContext::addStateVariable(VariableDeclaration const& _declaration)
{ {
m_stateVariables[&_declaration] = m_stateVariablesSize; m_stateVariables[&_declaration] = m_stateVariablesSize;

4
libsolidity/CompilerContext.h

@ -40,6 +40,7 @@ class CompilerContext
public: public:
CompilerContext(): m_stateVariablesSize(0) {} CompilerContext(): m_stateVariablesSize(0) {}
void addMagicGlobal(MagicVariableDeclaration const& _declaration);
void addStateVariable(VariableDeclaration const& _declaration); void addStateVariable(VariableDeclaration const& _declaration);
void startNewFunction() { m_localVariables.clear(); m_asm.setDeposit(0); } void startNewFunction() { m_localVariables.clear(); m_asm.setDeposit(0); }
void initializeLocalVariables(unsigned _numVariables); void initializeLocalVariables(unsigned _numVariables);
@ -48,6 +49,7 @@ public:
void adjustStackOffset(int _adjustment) { m_asm.adjustDeposit(_adjustment); } void adjustStackOffset(int _adjustment) { m_asm.adjustDeposit(_adjustment); }
bool isMagicGlobal(Declaration const* _declaration) const { return m_magicGlobals.count(_declaration); }
bool isFunctionDefinition(Declaration const* _declaration) const { return m_functionEntryLabels.count(_declaration); } bool isFunctionDefinition(Declaration const* _declaration) const { return m_functionEntryLabels.count(_declaration); }
bool isLocalVariable(Declaration const* _declaration) const; bool isLocalVariable(Declaration const* _declaration) const;
bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration); } bool isStateVariable(Declaration const* _declaration) const { return m_stateVariables.count(_declaration); }
@ -90,6 +92,8 @@ public:
private: private:
eth::Assembly m_asm; eth::Assembly m_asm;
/// Magic global variables like msg, tx or this, distinguished by type.
std::set<Declaration const*> m_magicGlobals;
/// Size of the state variables, offset of next variable to be added. /// Size of the state variables, offset of next variable to be added.
u256 m_stateVariablesSize; u256 m_stateVariablesSize;
/// Storage offsets of state variables /// Storage offsets of state variables

7
libsolidity/CompilerStack.cpp

@ -23,6 +23,7 @@
#include <libsolidity/AST.h> #include <libsolidity/AST.h>
#include <libsolidity/Scanner.h> #include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/GlobalContext.h>
#include <libsolidity/NameAndTypeResolver.h> #include <libsolidity/NameAndTypeResolver.h>
#include <libsolidity/Compiler.h> #include <libsolidity/Compiler.h>
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
@ -45,7 +46,9 @@ void CompilerStack::parse()
if (!m_scanner) if (!m_scanner)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Source not available.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Source not available."));
m_contractASTNode = Parser().parse(m_scanner); m_contractASTNode = Parser().parse(m_scanner);
NameAndTypeResolver().resolveNamesAndTypes(*m_contractASTNode); m_globalContext = make_shared<GlobalContext>();
m_globalContext->setCurrentContract(*m_contractASTNode);
NameAndTypeResolver(m_globalContext->getDeclarations()).resolveNamesAndTypes(*m_contractASTNode);
m_parseSuccessful = true; m_parseSuccessful = true;
} }
@ -61,7 +64,7 @@ bytes const& CompilerStack::compile(bool _optimize)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful.")); BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
m_bytecode.clear(); m_bytecode.clear();
m_compiler = make_shared<Compiler>(); m_compiler = make_shared<Compiler>();
m_compiler->compileContract(*m_contractASTNode); m_compiler->compileContract(*m_contractASTNode, m_globalContext->getMagicVariables());
return m_bytecode = m_compiler->getAssembledBytecode(_optimize); return m_bytecode = m_compiler->getAssembledBytecode(_optimize);
} }

9
libsolidity/CompilerStack.h

@ -30,9 +30,11 @@
namespace dev { namespace dev {
namespace solidity { namespace solidity {
class Scanner; // forward // forward declarations
class ContractDefinition; // forward class Scanner;
class Compiler; // forward class ContractDefinition;
class Compiler;
class GlobalContext;
/** /**
* Easy to use and self-contained Solidity compiler with as few header dependencies as possible. * Easy to use and self-contained Solidity compiler with as few header dependencies as possible.
@ -71,6 +73,7 @@ public:
private: private:
std::shared_ptr<Scanner> m_scanner; std::shared_ptr<Scanner> m_scanner;
std::shared_ptr<GlobalContext> m_globalContext;
std::shared_ptr<ContractDefinition> m_contractASTNode; std::shared_ptr<ContractDefinition> m_contractASTNode;
bool m_parseSuccessful; bool m_parseSuccessful;
std::string m_interface; std::string m_interface;

125
libsolidity/ExpressionCompiler.cpp

@ -162,15 +162,25 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall)
{ {
if (_functionCall.isTypeConversion()) if (_functionCall.isTypeConversion())
{ {
//@todo we only have integers and bools for now which cannot be explicitly converted //@todo struct construction
if (asserts(_functionCall.getArguments().size() == 1)) if (asserts(_functionCall.getArguments().size() == 1))
BOOST_THROW_EXCEPTION(InternalCompilerError()); BOOST_THROW_EXCEPTION(InternalCompilerError());
Expression& firstArgument = *_functionCall.getArguments().front(); Expression& firstArgument = *_functionCall.getArguments().front();
firstArgument.accept(*this); firstArgument.accept(*this);
appendTypeConversion(*firstArgument.getType(), *_functionCall.getType()); if (firstArgument.getType()->getCategory() == Type::Category::CONTRACT &&
_functionCall.getType()->getCategory() == Type::Category::INTEGER)
{
// explicit type conversion contract -> address, nothing to do.
}
else
{
appendTypeConversion(*firstArgument.getType(), *_functionCall.getType());
}
} }
else else
{ {
//@todo: check for "external call" (to be stored in type)
// Calling convention: Caller pushes return address and arguments // Calling convention: Caller pushes return address and arguments
// Callee removes them and pushes return values // Callee removes them and pushes return values
FunctionDefinition const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType()).getFunction(); FunctionDefinition const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType()).getFunction();
@ -185,9 +195,6 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall)
appendTypeConversion(*arguments[i]->getType(), *function.getParameters()[i]->getType()); appendTypeConversion(*arguments[i]->getType(), *function.getParameters()[i]->getType());
} }
_functionCall.getExpression().accept(*this); _functionCall.getExpression().accept(*this);
if (asserts(m_currentLValue.isInCode()))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Code reference expected."));
m_currentLValue.reset();
m_context.appendJump(); m_context.appendJump();
m_context << returnLabel; m_context << returnLabel;
@ -205,22 +212,61 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall)
void ExpressionCompiler::endVisit(MemberAccess& _memberAccess) void ExpressionCompiler::endVisit(MemberAccess& _memberAccess)
{ {
if (asserts(m_currentLValue.isInStorage())) ASTString const& member = _memberAccess.getMemberName();
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member access to a non-storage value.")); switch (_memberAccess.getExpression().getType()->getCategory())
StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType()); {
unsigned memberIndex = type.memberNameToIndex(_memberAccess.getMemberName()); case Type::Category::INTEGER:
if (asserts(memberIndex <= type.getMemberCount())) if (asserts(member == "balance"))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member not found in struct during compilation.")); BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer."));
m_context << type.getStorageOffsetOfMember(memberIndex) << eth::Instruction::ADD; m_context << eth::Instruction::BALANCE;
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess); break;
case Type::Category::CONTRACT:
// call function
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Contract variables not yet implemented."));
break;
case Type::Category::MAGIC:
// we can ignore the kind of magic and only look at the name of the member
if (member == "coinbase")
m_context << eth::Instruction::COINBASE;
else if (member == "timestamp")
m_context << eth::Instruction::TIMESTAMP;
else if (member == "prevhash")
m_context << eth::Instruction::PREVHASH;
else if (member == "difficulty")
m_context << eth::Instruction::DIFFICULTY;
else if (member == "number")
m_context << eth::Instruction::NUMBER;
else if (member == "gaslimit")
m_context << eth::Instruction::GASLIMIT;
else if (member == "sender")
m_context << eth::Instruction::CALLER;
else if (member == "value")
m_context << eth::Instruction::CALLVALUE;
else if (member == "origin")
m_context << eth::Instruction::ORIGIN;
else if (member == "gas")
m_context << eth::Instruction::GAS;
else if (member == "gasprice")
m_context << eth::Instruction::GASPRICE;
else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown magic member."));
break;
case Type::Category::STRUCT:
{
StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType());
m_context << type.getStorageOffsetOfMember(member) << eth::Instruction::ADD;
m_currentLValue = LValue(m_context, LValue::STORAGE);
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
break;
}
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member access to unknown type."));
}
} }
bool ExpressionCompiler::visit(IndexAccess& _indexAccess) bool ExpressionCompiler::visit(IndexAccess& _indexAccess)
{ {
m_currentLValue.reset();
_indexAccess.getBaseExpression().accept(*this); _indexAccess.getBaseExpression().accept(*this);
if (asserts(m_currentLValue.isInStorage()))
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Index access to a non-storage value."));
_indexAccess.getIndexExpression().accept(*this); _indexAccess.getIndexExpression().accept(*this);
appendTypeConversion(*_indexAccess.getIndexExpression().getType(), appendTypeConversion(*_indexAccess.getIndexExpression().getType(),
*dynamic_cast<MappingType const&>(*_indexAccess.getBaseExpression().getType()).getKeyType(), *dynamic_cast<MappingType const&>(*_indexAccess.getBaseExpression().getType()).getKeyType(),
@ -237,8 +283,25 @@ bool ExpressionCompiler::visit(IndexAccess& _indexAccess)
void ExpressionCompiler::endVisit(Identifier& _identifier) void ExpressionCompiler::endVisit(Identifier& _identifier)
{ {
m_currentLValue.fromDeclaration(_identifier, *_identifier.getReferencedDeclaration()); Declaration* declaration = _identifier.getReferencedDeclaration();
m_currentLValue.retrieveValueIfLValueNotRequested(_identifier); if (MagicVariableDeclaration* magicVar = dynamic_cast<MagicVariableDeclaration*>(declaration))
{
if (magicVar->getKind() == MagicVariableDeclaration::VariableKind::THIS)
m_context << eth::Instruction::ADDRESS;
return;
}
if (FunctionDefinition* functionDef = dynamic_cast<FunctionDefinition*>(declaration))
{
m_context << m_context.getFunctionEntryLabel(*functionDef).pushTag();
return;
}
if (VariableDeclaration* varDef = dynamic_cast<VariableDeclaration*>(declaration))
{
m_currentLValue.fromIdentifier(_identifier, *_identifier.getReferencedDeclaration());
m_currentLValue.retrieveValueIfLValueNotRequested(_identifier);
return;
}
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Identifier type not expected in expression context."));
} }
void ExpressionCompiler::endVisit(Literal& _literal) void ExpressionCompiler::endVisit(Literal& _literal)
@ -405,9 +468,6 @@ void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bo
{ {
switch (m_type) switch (m_type)
{ {
case CODE:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Tried to retrieve value of a function."));
break;
case STACK: case STACK:
{ {
unsigned stackPos = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset)); unsigned stackPos = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset));
@ -418,11 +478,15 @@ void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bo
break; break;
} }
case STORAGE: case STORAGE:
if (!_expression.getType()->isValueType())
break; // no distinction between value and reference for non-value types
if (!_remove) if (!_remove)
*m_context << eth::Instruction::DUP1; *m_context << eth::Instruction::DUP1;
*m_context << eth::Instruction::SLOAD; *m_context << eth::Instruction::SLOAD;
break; break;
case MEMORY: case MEMORY:
if (!_expression.getType()->isValueType())
break; // no distinction between value and reference for non-value types
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation()) BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
<< errinfo_comment("Location type not yet implemented.")); << errinfo_comment("Location type not yet implemented."));
break; break;
@ -450,15 +514,15 @@ void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool
break; break;
} }
case LValue::STORAGE: case LValue::STORAGE:
if (!_expression.getType()->isValueType())
break; // no distinction between value and reference for non-value types
if (!_move) if (!_move)
*m_context << eth::Instruction::DUP2 << eth::Instruction::SWAP1; *m_context << eth::Instruction::DUP2 << eth::Instruction::SWAP1;
*m_context << eth::Instruction::SSTORE; *m_context << eth::Instruction::SSTORE;
break; break;
case LValue::CODE:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
<< errinfo_comment("Location type does not support assignment."));
break;
case LValue::MEMORY: case LValue::MEMORY:
if (!_expression.getType()->isValueType())
break; // no distinction between value and reference for non-value types
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation()) BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
<< errinfo_comment("Location type not yet implemented.")); << errinfo_comment("Location type not yet implemented."));
break; break;
@ -469,7 +533,7 @@ void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool
} }
} }
void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(const Expression& _expression) void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(Expression const& _expression)
{ {
if (!_expression.lvalueRequested()) if (!_expression.lvalueRequested())
{ {
@ -478,7 +542,7 @@ void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(const Express
} }
} }
void ExpressionCompiler::LValue::fromDeclaration( Expression const& _expression, Declaration const& _declaration) void ExpressionCompiler::LValue::fromIdentifier(Identifier const& _identifier, Declaration const& _declaration)
{ {
if (m_context->isLocalVariable(&_declaration)) if (m_context->isLocalVariable(&_declaration))
{ {
@ -490,13 +554,8 @@ void ExpressionCompiler::LValue::fromDeclaration( Expression const& _expression,
m_type = STORAGE; m_type = STORAGE;
*m_context << m_context->getStorageLocationOfVariable(_declaration); *m_context << m_context->getStorageLocationOfVariable(_declaration);
} }
else if (m_context->isFunctionDefinition(&_declaration))
{
m_type = CODE;
*m_context << m_context->getFunctionEntryLabel(dynamic_cast<FunctionDefinition const&>(_declaration)).pushTag();
}
else else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation()) BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_identifier.getLocation())
<< errinfo_comment("Identifier type not supported or identifier not found.")); << errinfo_comment("Identifier type not supported or identifier not found."));
} }

13
libsolidity/ExpressionCompiler.h

@ -83,31 +83,30 @@ private:
/** /**
* Helper class to store and retrieve lvalues to and from various locations. * Helper class to store and retrieve lvalues to and from various locations.
* All types except STACK store a reference in a slot on the stack, STACK just stores the * All types except STACK store a reference in a slot on the stack, STACK just
* base stack offset of the variable in @a m_baseStackOffset. * stores the base stack offset of the variable in @a m_baseStackOffset.
*/ */
class LValue class LValue
{ {
public: public:
enum LValueType { NONE, CODE, STACK, MEMORY, STORAGE }; enum LValueType { NONE, STACK, MEMORY, STORAGE };
explicit LValue(CompilerContext& _compilerContext): m_context(&_compilerContext) { reset(); } explicit LValue(CompilerContext& _compilerContext): m_context(&_compilerContext) { reset(); }
LValue(CompilerContext& _compilerContext, LValueType _type, unsigned _baseStackOffset = 0): LValue(CompilerContext& _compilerContext, LValueType _type, unsigned _baseStackOffset = 0):
m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset) {} m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset) {}
/// Set type according to the declaration and retrieve the reference. /// Set type according to the declaration and retrieve the reference.
/// @a _expression is the current expression, used for error reporting. /// @a _expression is the current expression
void fromDeclaration(Expression const& _expression, Declaration const& _declaration); void fromIdentifier(Identifier const& _identifier, Declaration const& _declaration);
void reset() { m_type = NONE; m_baseStackOffset = 0; } void reset() { m_type = NONE; m_baseStackOffset = 0; }
bool isValid() const { return m_type != NONE; } bool isValid() const { return m_type != NONE; }
bool isInCode() const { return m_type == CODE; }
bool isInOnStack() const { return m_type == STACK; } bool isInOnStack() const { return m_type == STACK; }
bool isInMemory() const { return m_type == MEMORY; } bool isInMemory() const { return m_type == MEMORY; }
bool isInStorage() const { return m_type == STORAGE; } bool isInStorage() const { return m_type == STORAGE; }
/// @returns true if this lvalue reference type occupies a slot on the stack. /// @returns true if this lvalue reference type occupies a slot on the stack.
bool storesReferenceOnStack() const { return m_type == STORAGE || m_type == MEMORY || m_type == CODE; } bool storesReferenceOnStack() const { return m_type == STORAGE || m_type == MEMORY; }
/// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true, /// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true,
/// also removes the reference from the stack (note that is does not reset the type to @a NONE). /// also removes the reference from the stack (note that is does not reset the type to @a NONE).

84
libsolidity/GlobalContext.cpp

@ -0,0 +1,84 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Container of the (implicit and explicit) global objects.
*/
#include <memory>
#include <libsolidity/GlobalContext.h>
#include <libsolidity/AST.h>
#include <libsolidity/Types.h>
using namespace std;
namespace dev
{
namespace solidity
{
GlobalContext::GlobalContext():
m_magicVariables{make_shared<MagicVariableDeclaration>(MagicVariableDeclaration::VariableKind::BLOCK,
"block",
make_shared<MagicType>(MagicType::Kind::BLOCK)),
make_shared<MagicVariableDeclaration>(MagicVariableDeclaration::VariableKind::MSG,
"msg",
make_shared<MagicType>(MagicType::Kind::MSG)),
make_shared<MagicVariableDeclaration>(MagicVariableDeclaration::VariableKind::TX,
"tx",
make_shared<MagicType>(MagicType::Kind::TX))}
{
}
void GlobalContext::setCurrentContract(ContractDefinition const& _contract)
{
m_currentContract = &_contract;
}
vector<Declaration*> GlobalContext::getDeclarations() const
{
vector<Declaration*> declarations;
declarations.reserve(m_magicVariables.size() + 1);
for (ASTPointer<Declaration> const& variable: m_magicVariables)
declarations.push_back(variable.get());
declarations.push_back(getCurrentThis());
return declarations;
}
MagicVariableDeclaration*GlobalContext::getCurrentThis() const
{
if (!m_thisPointer[m_currentContract])
m_thisPointer[m_currentContract] = make_shared<MagicVariableDeclaration>(
MagicVariableDeclaration::VariableKind::THIS,
"this", make_shared<ContractType>(*m_currentContract));
return m_thisPointer[m_currentContract].get();
}
vector<MagicVariableDeclaration const*> GlobalContext::getMagicVariables() const
{
vector<MagicVariableDeclaration const*> declarations;
declarations.reserve(m_magicVariables.size() + 1);
for (ASTPointer<MagicVariableDeclaration const> const& variable: m_magicVariables)
declarations.push_back(variable.get());
declarations.push_back(getCurrentThis());
return declarations;
}
}
}

62
libsolidity/GlobalContext.h

@ -0,0 +1,62 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Container of the (implicit and explicit) global objects.
*/
#pragma once
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <boost/noncopyable.hpp>
#include <libsolidity/ASTForward.h>
namespace dev
{
namespace solidity
{
class Type; // forward
/**
* Container for all global objects which look like AST nodes, but are not part of the AST
* that is currently being compiled.
* @note must not be destroyed or moved during compilation as its objects can be referenced from
* other objects.
*/
class GlobalContext: private boost::noncopyable
{
public:
GlobalContext();
void setCurrentContract(ContractDefinition const& _contract);
std::vector<MagicVariableDeclaration const*> getMagicVariables() const;
std::vector<Declaration*> getDeclarations() const;
private:
MagicVariableDeclaration* getCurrentThis() const;
std::vector<std::shared_ptr<MagicVariableDeclaration>> m_magicVariables;
ContractDefinition const* m_currentContract;
std::map<ContractDefinition const*, std::shared_ptr<MagicVariableDeclaration>> mutable m_thisPointer;
};
}
}

43
libsolidity/NameAndTypeResolver.cpp

@ -32,15 +32,20 @@ namespace solidity
{ {
NameAndTypeResolver::NameAndTypeResolver(std::vector<Declaration*> const& _globals)
{
for (Declaration* declaration: _globals)
m_scopes[nullptr].registerDeclaration(*declaration);
}
void NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract) void NameAndTypeResolver::resolveNamesAndTypes(ContractDefinition& _contract)
{ {
reset();
DeclarationRegistrationHelper registrar(m_scopes, _contract); DeclarationRegistrationHelper registrar(m_scopes, _contract);
m_currentScope = &m_scopes[&_contract]; m_currentScope = &m_scopes[&_contract];
for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs()) for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs())
ReferencesResolver resolver(*structDef, *this, nullptr); ReferencesResolver resolver(*structDef, *this, nullptr);
for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs()) for (ASTPointer<StructDefinition> const& structDef: _contract.getDefinedStructs())
checkForRecursion(*structDef); structDef->checkValidityOfMembers();
for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables()) for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
ReferencesResolver resolver(*variable, *this, nullptr); ReferencesResolver resolver(*variable, *this, nullptr);
for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions()) for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
@ -73,30 +78,6 @@ Declaration* NameAndTypeResolver::getNameFromCurrentScope(ASTString const& _name
return m_currentScope->resolveName(_name, _recursive); return m_currentScope->resolveName(_name, _recursive);
} }
void NameAndTypeResolver::checkForRecursion(StructDefinition const& _struct)
{
set<StructDefinition const*> definitionsSeen;
vector<StructDefinition const*> queue = {&_struct};
while (!queue.empty())
{
StructDefinition const* def = queue.back();
queue.pop_back();
if (definitionsSeen.count(def))
BOOST_THROW_EXCEPTION(ParserError() << errinfo_sourceLocation(def->getLocation())
<< errinfo_comment("Recursive struct definition."));
definitionsSeen.insert(def);
for (ASTPointer<VariableDeclaration> const& member: def->getMembers())
if (member->getType()->getCategory() == Type::Category::STRUCT)
queue.push_back(dynamic_cast<UserDefinedTypeName&>(*member->getTypeName()).getReferencedStruct());
}
}
void NameAndTypeResolver::reset()
{
m_scopes.clear();
m_currentScope = nullptr;
}
DeclarationRegistrationHelper::DeclarationRegistrationHelper(map<ASTNode const*, Scope>& _scopes, DeclarationRegistrationHelper::DeclarationRegistrationHelper(map<ASTNode const*, Scope>& _scopes,
ASTNode& _astRoot): ASTNode& _astRoot):
m_scopes(_scopes), m_currentScope(&m_scopes[nullptr]) m_scopes(_scopes), m_currentScope(&m_scopes[nullptr])
@ -195,7 +176,11 @@ void ReferencesResolver::endVisit(VariableDeclaration& _variable)
// endVisit because the internal type needs resolving if it is a user defined type // endVisit because the internal type needs resolving if it is a user defined type
// or mapping // or mapping
if (_variable.getTypeName()) if (_variable.getTypeName())
{
_variable.setType(_variable.getTypeName()->toType()); _variable.setType(_variable.getTypeName()->toType());
if (!_variable.getType())
BOOST_THROW_EXCEPTION(_variable.getTypeName()->createTypeError("Invalid type name"));
}
else if (!m_allowLazyTypes) else if (!m_allowLazyTypes)
BOOST_THROW_EXCEPTION(_variable.createTypeError("Explicit type needed.")); BOOST_THROW_EXCEPTION(_variable.createTypeError("Explicit type needed."));
// otherwise we have a "var"-declaration whose type is resolved by the first assignment // otherwise we have a "var"-declaration whose type is resolved by the first assignment
@ -221,11 +206,7 @@ bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
if (!declaration) if (!declaration)
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_typeName.getLocation()) BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_typeName.getLocation())
<< errinfo_comment("Undeclared identifier.")); << errinfo_comment("Undeclared identifier."));
StructDefinition* referencedStruct = dynamic_cast<StructDefinition*>(declaration); _typeName.setReferencedDeclaration(*declaration);
//@todo later, contracts are also valid types
if (!referencedStruct)
BOOST_THROW_EXCEPTION(_typeName.createTypeError("Identifier does not name a type name."));
_typeName.setReferencedStruct(*referencedStruct);
return false; return false;
} }

3
libsolidity/NameAndTypeResolver.h

@ -41,8 +41,7 @@ namespace solidity
class NameAndTypeResolver: private boost::noncopyable class NameAndTypeResolver: private boost::noncopyable
{ {
public: public:
NameAndTypeResolver() {} explicit NameAndTypeResolver(std::vector<Declaration*> const& _globals);
void resolveNamesAndTypes(ContractDefinition& _contract); void resolveNamesAndTypes(ContractDefinition& _contract);
/// Resolves the given @a _name inside the scope @a _scope. If @a _scope is omitted, /// Resolves the given @a _name inside the scope @a _scope. If @a _scope is omitted,

1
libsolidity/Scanner.cpp

@ -683,7 +683,6 @@ Token::Value Scanner::scanNumber(char _charSeen)
KEYWORD("switch", Token::SWITCH) \ KEYWORD("switch", Token::SWITCH) \
KEYWORD_GROUP('t') \ KEYWORD_GROUP('t') \
KEYWORD("text", Token::TEXT) \ KEYWORD("text", Token::TEXT) \
KEYWORD("this", Token::THIS) \
KEYWORD("true", Token::TRUE_LITERAL) \ KEYWORD("true", Token::TRUE_LITERAL) \
KEYWORD_GROUP('u') \ KEYWORD_GROUP('u') \
KEYWORD("uint", Token::UINT) \ KEYWORD("uint", Token::UINT) \

1
libsolidity/Token.h

@ -160,7 +160,6 @@ namespace solidity
K(RETURNS, "returns", 0) \ K(RETURNS, "returns", 0) \
K(STRUCT, "struct", 0) \ K(STRUCT, "struct", 0) \
K(SWITCH, "switch", 0) \ K(SWITCH, "switch", 0) \
K(THIS, "this", 0) \
K(VAR, "var", 0) \ K(VAR, "var", 0) \
K(WHILE, "while", 0) \ K(WHILE, "while", 0) \
\ \

131
libsolidity/Types.cpp

@ -60,13 +60,24 @@ shared_ptr<Type> Type::fromElementaryTypeName(Token::Value _typeToken)
shared_ptr<Type> Type::fromUserDefinedTypeName(UserDefinedTypeName const& _typeName) shared_ptr<Type> Type::fromUserDefinedTypeName(UserDefinedTypeName const& _typeName)
{ {
return make_shared<StructType>(*_typeName.getReferencedStruct()); Declaration const* declaration = _typeName.getReferencedDeclaration();
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration))
return make_shared<StructType>(*structDef);
else if (FunctionDefinition const* function = dynamic_cast<FunctionDefinition const*>(declaration))
return make_shared<FunctionType>(*function);
else if (ContractDefinition const* contract = dynamic_cast<ContractDefinition const*>(declaration))
return make_shared<ContractType>(*contract);
return shared_ptr<Type>();
} }
shared_ptr<Type> Type::fromMapping(Mapping const& _typeName) shared_ptr<Type> Type::fromMapping(Mapping const& _typeName)
{ {
shared_ptr<Type const> keyType = _typeName.getKeyType().toType(); shared_ptr<Type const> keyType = _typeName.getKeyType().toType();
if (!keyType)
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Error resolving type name."));
shared_ptr<Type const> valueType = _typeName.getValueType().toType(); shared_ptr<Type const> valueType = _typeName.getValueType().toType();
if (!valueType)
BOOST_THROW_EXCEPTION(_typeName.getValueType().createTypeError("Invalid type name"));
return make_shared<MappingType>(keyType, valueType); return make_shared<MappingType>(keyType, valueType);
} }
@ -86,6 +97,8 @@ shared_ptr<Type> Type::forLiteral(Literal const& _literal)
} }
} }
const MemberList Type::EmptyMemberList = MemberList();
shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(string const& _literal) shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(string const& _literal)
{ {
bigint value(_literal); bigint value(_literal);
@ -176,6 +189,8 @@ u256 IntegerType::literalValue(Literal const& _literal) const
return u256(value); return u256(value);
} }
const MemberList IntegerType::AddressMemberList = MemberList({{"balance", std::make_shared<IntegerType const>(256)}});
bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{ {
// conversion to integer is fine, but not to address // conversion to integer is fine, but not to address
@ -199,6 +214,15 @@ u256 BoolType::literalValue(Literal const& _literal) const
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."));
} }
bool ContractType::isExplicitlyConvertibleTo(Type const& _convertTo) const
{
if (isImplicitlyConvertibleTo(_convertTo))
return true;
if (_convertTo.getCategory() == Category::INTEGER)
return dynamic_cast<IntegerType const&>(_convertTo).isAddress();
return false;
}
bool ContractType::operator==(Type const& _other) const bool ContractType::operator==(Type const& _other) const
{ {
if (_other.getCategory() != getCategory()) if (_other.getCategory() != getCategory())
@ -215,6 +239,11 @@ u256 ContractType::getStorageSize() const
return max<u256>(1, size); return max<u256>(1, size);
} }
string ContractType::toString() const
{
return "contract " + m_contract.getName();
}
bool StructType::operator==(Type const& _other) const bool StructType::operator==(Type const& _other) const
{ {
if (_other.getCategory() != getCategory()) if (_other.getCategory() != getCategory())
@ -226,15 +255,15 @@ bool StructType::operator==(Type const& _other) const
u256 StructType::getStorageSize() const u256 StructType::getStorageSize() const
{ {
u256 size = 0; u256 size = 0;
for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers()) for (pair<string, shared_ptr<Type const>> const& member: getMembers())
size += variable->getType()->getStorageSize(); size += member.second->getStorageSize();
return max<u256>(1, size); return max<u256>(1, size);
} }
bool StructType::canLiveOutsideStorage() const bool StructType::canLiveOutsideStorage() const
{ {
for (unsigned i = 0; i < getMemberCount(); ++i) for (pair<string, shared_ptr<Type const>> const& member: getMembers())
if (!getMemberByIndex(i).getType()->canLiveOutsideStorage()) if (!member.second->canLiveOutsideStorage())
return false; return false;
return true; return true;
} }
@ -244,33 +273,30 @@ string StructType::toString() const
return string("struct ") + m_struct.getName(); return string("struct ") + m_struct.getName();
} }
unsigned StructType::getMemberCount() const MemberList const& StructType::getMembers() const
{ {
return m_struct.getMembers().size(); // We need to lazy-initialize it because of recursive references.
} if (!m_members)
{
unsigned StructType::memberNameToIndex(string const& _name) const map<string, shared_ptr<Type const>> members;
{ for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers())
vector<ASTPointer<VariableDeclaration>> const& members = m_struct.getMembers(); members[variable->getName()] = variable->getType();
for (unsigned index = 0; index < members.size(); ++index) m_members.reset(new MemberList(members));
if (members[index]->getName() == _name) }
return index; return *m_members;
return unsigned(-1);
}
VariableDeclaration const& StructType::getMemberByIndex(unsigned _index) const
{
return *m_struct.getMembers()[_index];
} }
u256 StructType::getStorageOffsetOfMember(unsigned _index) const u256 StructType::getStorageOffsetOfMember(string const& _name) const
{ {
//@todo cache member offset? //@todo cache member offset?
u256 offset; u256 offset;
// vector<ASTPointer<VariableDeclaration>> const& members = m_struct.getMembers(); for (ASTPointer<VariableDeclaration> variable: m_struct.getMembers())
for (unsigned index = 0; index < _index; ++index) {
offset += getMemberByIndex(index).getType()->getStorageSize(); offset += variable->getType()->getStorageSize();
return offset; if (variable->getName() == _name)
return offset;
}
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage offset of non-existing member requested."));
} }
bool FunctionType::operator==(Type const& _other) const bool FunctionType::operator==(Type const& _other) const
@ -283,8 +309,7 @@ bool FunctionType::operator==(Type const& _other) const
string FunctionType::toString() const string FunctionType::toString() const
{ {
//@todo nice string for function types return "function " + m_function.getName();
return "function(...)returns(...)";
} }
bool MappingType::operator==(Type const& _other) const bool MappingType::operator==(Type const& _other) const
@ -308,5 +333,55 @@ bool TypeType::operator==(Type const& _other) const
return *getActualType() == *other.getActualType(); return *getActualType() == *other.getActualType();
} }
MagicType::MagicType(MagicType::Kind _kind):
m_kind(_kind)
{
switch (m_kind)
{
case Kind::BLOCK:
m_members = MemberList({{"coinbase", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)},
{"timestamp", make_shared<IntegerType const>(256)},
{"prevhash", make_shared<IntegerType const>(256, IntegerType::Modifier::HASH)},
{"difficulty", make_shared<IntegerType const>(256)},
{"number", make_shared<IntegerType const>(256)},
{"gaslimit", make_shared<IntegerType const>(256)}});
break;
case Kind::MSG:
m_members = MemberList({{"sender", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)},
{"value", make_shared<IntegerType const>(256)}});
break;
case Kind::TX:
m_members = MemberList({{"origin", make_shared<IntegerType const>(0, IntegerType::Modifier::ADDRESS)},
{"gas", make_shared<IntegerType const>(256)},
{"gasprice", make_shared<IntegerType const>(256)}});
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of magic."));
}
}
bool MagicType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
MagicType const& other = dynamic_cast<MagicType const&>(_other);
return other.m_kind == m_kind;
}
string MagicType::toString() const
{
switch (m_kind)
{
case Kind::BLOCK:
return "block";
case Kind::MSG:
return "msg";
case Kind::TX:
return "tx";
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown kind of magic."));
}
}
} }
} }

113
libsolidity/Types.h

@ -24,6 +24,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <map>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libsolidity/Exceptions.h> #include <libsolidity/Exceptions.h>
@ -37,6 +38,33 @@ namespace solidity
// @todo realMxN, string<N> // @todo realMxN, string<N>
class Type; // forward
/**
* List of members of a type.
*/
class MemberList
{
public:
using TypePointer = std::shared_ptr<Type const>;
using MemberMap = std::map<std::string, TypePointer>;
MemberList() {}
explicit MemberList(MemberMap const& _members): m_memberTypes(_members) {}
TypePointer getMemberType(std::string const& _name) const
{
auto it = m_memberTypes.find(_name);
return it != m_memberTypes.end() ? it->second : std::shared_ptr<Type const>();
}
MemberMap::const_iterator begin() const { return m_memberTypes.begin(); }
MemberMap::const_iterator end() const { return m_memberTypes.end(); }
private:
MemberMap m_memberTypes;
};
/** /**
* Abstract base class that forms the root of the type hierarchy. * Abstract base class that forms the root of the type hierarchy.
*/ */
@ -45,7 +73,7 @@ class Type: private boost::noncopyable
public: public:
enum class Category enum class Category
{ {
INTEGER, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE INTEGER, BOOL, REAL, STRING, CONTRACT, STRUCT, FUNCTION, MAPPING, VOID, TYPE, MAGIC
}; };
///@{ ///@{
@ -78,8 +106,18 @@ public:
/// @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,
virtual u256 getStorageSize() const { return 1; } virtual u256 getStorageSize() const { return 1; }
/// Returns true if the type can be stored in storage.
virtual bool canBeStored() const { return true; }
/// Returns false if the type cannot live outside the storage, i.e. if it includes some mapping. /// Returns false if the type cannot live outside the storage, i.e. if it includes some mapping.
virtual bool canLiveOutsideStorage() const { return true; } virtual bool canLiveOutsideStorage() const { return true; }
/// Returns true if the type can be stored as a value (as opposed to a reference) on the stack,
/// i.e. it behaves differently in lvalue context and in value context.
virtual bool isValueType() const { return false; }
/// Returns the list of all members of this type. Default implementation: no members.
virtual MemberList const& getMembers() const { return EmptyMemberList; }
/// Convenience method, returns the type of the given named member or an empty pointer if no such member exists.
std::shared_ptr<Type const> 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
@ -87,6 +125,10 @@ public:
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:
/// Convenience object used when returning an empty member list.
static const MemberList EmptyMemberList;
}; };
/** /**
@ -114,7 +156,10 @@ public:
virtual bool operator==(Type const& _other) const override; virtual bool operator==(Type const& _other) const override;
virtual unsigned getCalldataEncodedSize() const { return m_bits / 8; } virtual unsigned getCalldataEncodedSize() const override { return m_bits / 8; }
virtual bool isValueType() const override { return true; }
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; virtual u256 literalValue(Literal const& _literal) const override;
@ -127,6 +172,7 @@ public:
private: private:
int m_bits; int m_bits;
Modifier m_modifier; Modifier m_modifier;
static const MemberList AddressMemberList;
}; };
/** /**
@ -147,6 +193,7 @@ public:
} }
virtual unsigned getCalldataEncodedSize() const { return 1; } virtual unsigned getCalldataEncodedSize() const { return 1; }
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;
@ -160,10 +207,11 @@ 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.
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; virtual u256 getStorageSize() const override;
virtual std::string toString() const override { return "contract{...}"; } virtual std::string toString() const override;
private: private:
ContractDefinition const& m_contract; ContractDefinition const& m_contract;
@ -183,18 +231,18 @@ public:
} }
virtual bool operator==(Type const& _other) const override; virtual bool operator==(Type const& _other) const override;
virtual u256 getStorageSize() const; virtual u256 getStorageSize() const override;
virtual bool canLiveOutsideStorage() const; virtual bool canLiveOutsideStorage() const override;
virtual std::string toString() const override; virtual std::string toString() const override;
unsigned getMemberCount() const; virtual MemberList const& getMembers() const override;
/// Returns the index of the member with name @a _name or unsigned(-1) if it does not exist.
unsigned memberNameToIndex(std::string const& _name) const; u256 getStorageOffsetOfMember(std::string const& _name) const;
VariableDeclaration const& getMemberByIndex(unsigned _index) const;
u256 getStorageOffsetOfMember(unsigned _index) const;
private: private:
StructDefinition const& m_struct; StructDefinition const& m_struct;
/// List of member types, will be lazy-initialized because of recursive references.
mutable std::unique_ptr<MemberList> m_members;
}; };
/** /**
@ -210,8 +258,9 @@ public:
virtual bool operator==(Type const& _other) const override; virtual bool operator==(Type const& _other) const override;
virtual std::string toString() const override; virtual std::string toString() const override;
virtual u256 getStorageSize() const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable function type requested.")); } virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const { return false; } virtual u256 getStorageSize() const override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable function type requested.")); }
virtual bool canLiveOutsideStorage() const override { return false; }
private: private:
FunctionDefinition const& m_function; FunctionDefinition const& m_function;
@ -229,7 +278,7 @@ public:
virtual bool operator==(Type const& _other) const override; virtual bool operator==(Type const& _other) const override;
virtual std::string toString() const override; virtual std::string toString() const override;
virtual bool canLiveOutsideStorage() const { return false; } virtual bool canLiveOutsideStorage() const override { return false; }
std::shared_ptr<Type const> getKeyType() const { return m_keyType; } std::shared_ptr<Type const> getKeyType() const { return m_keyType; }
std::shared_ptr<Type const> getValueType() const { return m_valueType; } std::shared_ptr<Type const> getValueType() const { return m_valueType; }
@ -250,8 +299,9 @@ public:
VoidType() {} VoidType() {}
virtual std::string toString() const override { return "void"; } virtual std::string toString() const override { return "void"; }
virtual u256 getStorageSize() const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable void type requested.")); } virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const { return false; } virtual u256 getStorageSize() const override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable void type requested.")); }
virtual bool canLiveOutsideStorage() const override { return false; }
}; };
/** /**
@ -267,8 +317,9 @@ public:
std::shared_ptr<Type const> const& getActualType() const { return m_actualType; } std::shared_ptr<Type const> const& getActualType() const { return m_actualType; }
virtual bool operator==(Type const& _other) const override; virtual bool operator==(Type const& _other) const override;
virtual u256 getStorageSize() const { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable type type requested.")); } virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const { return false; } virtual u256 getStorageSize() const override { BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Storage size of non-storable type type requested.")); }
virtual bool canLiveOutsideStorage() const override { return false; }
virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; } virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; }
private: private:
@ -276,5 +327,29 @@ private:
}; };
/**
* Special type for magic variables (block, msg, tx), similar to a struct but without any reference
* (it always references a global singleton by name).
*/
class MagicType: public Type
{
public:
enum class Kind { BLOCK, MSG, TX }; //@todo should be unified with MagicVariableDeclaration::VariableKind;
virtual Category getCategory() const override { return Category::MAGIC; }
MagicType(Kind _kind);
virtual bool operator==(Type const& _other) const;
virtual bool canBeStored() const override { return false; }
virtual bool canLiveOutsideStorage() const override { return true; }
virtual MemberList const& getMembers() const override { return m_members; }
virtual std::string toString() const override;
private:
Kind m_kind;
MemberList m_members;
};
} }
} }

31
test/TestHelper.cpp

@ -287,6 +287,18 @@ void checkStorage(map<u256, u256> _expectedStore, map<u256, u256> _resultStore,
} }
} }
void checkLog(LogEntries _resultLogs, LogEntries _expectedLogs)
{
BOOST_REQUIRE_EQUAL(_resultLogs.size(), _expectedLogs.size());
for (size_t i = 0; i < _resultLogs.size(); ++i)
{
BOOST_CHECK_EQUAL(_resultLogs[i].address, _expectedLogs[i].address);
BOOST_CHECK_EQUAL(_resultLogs[i].topics, _expectedLogs[i].topics);
BOOST_CHECK(_resultLogs[i].data == _expectedLogs[i].data);
}
}
std::string getTestPath() std::string getTestPath()
{ {
string testPath; string testPath;
@ -310,12 +322,13 @@ void userDefinedTest(string testTypeFlag, std::function<void(json_spirit::mValue
string arg = boost::unit_test::framework::master_test_suite().argv[i]; string arg = boost::unit_test::framework::master_test_suite().argv[i];
if (arg == testTypeFlag) if (arg == testTypeFlag)
{ {
if (i + 1 >= boost::unit_test::framework::master_test_suite().argc) if (boost::unit_test::framework::master_test_suite().argc <= i + 2)
{ {
cnote << "Missing filename\nUsage: testeth " << testTypeFlag << " <filename>\n"; cnote << "Missing filename\nUsage: testeth " << testTypeFlag << " <filename> <testname>\n";
return; return;
} }
string filename = boost::unit_test::framework::master_test_suite().argv[i + 1]; string filename = boost::unit_test::framework::master_test_suite().argv[i + 1];
string testname = boost::unit_test::framework::master_test_suite().argv[i + 2];
int currentVerbosity = g_logVerbosity; int currentVerbosity = g_logVerbosity;
g_logVerbosity = 12; g_logVerbosity = 12;
try try
@ -325,7 +338,19 @@ void userDefinedTest(string testTypeFlag, std::function<void(json_spirit::mValue
string s = asString(contents(filename)); string s = asString(contents(filename));
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + filename + " is empty. "); BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + filename + " is empty. ");
json_spirit::read_string(s, v); json_spirit::read_string(s, v);
doTests(v, false); json_spirit::mObject oSingleTest;
json_spirit::mObject::const_iterator pos = v.get_obj().find(testname);
if (pos == v.get_obj().end())
{
cnote << "Could not find test: " << testname << " in " << filename << "\n";
return;
}
else
oSingleTest[pos->first] = pos->second;
json_spirit::mValue v_singleTest(oSingleTest);
doTests(v_singleTest, false);
} }
catch (Exception const& _e) catch (Exception const& _e)
{ {

2
test/TestHelper.h

@ -25,6 +25,7 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include "JsonSpiritHeaders.h" #include "JsonSpiritHeaders.h"
#include <libethereum/State.h> #include <libethereum/State.h>
#include <libevm/ExtVMFace.h>
namespace dev namespace dev
{ {
@ -69,6 +70,7 @@ bytes importCode(json_spirit::mObject& _o);
bytes importData(json_spirit::mObject& _o); bytes importData(json_spirit::mObject& _o);
void checkOutput(bytes const& _output, json_spirit::mObject& _o); void checkOutput(bytes const& _output, json_spirit::mObject& _o);
void checkStorage(std::map<u256, u256> _expectedStore, std::map<u256, u256> _resultStore, Address _expectedAddr); void checkStorage(std::map<u256, u256> _expectedStore, std::map<u256, u256> _resultStore, Address _expectedAddr);
void checkLog(eth::LogEntries _resultLogs, eth::LogEntries _expectedLogs);
void executeTests(const std::string& _name, const std::string& _testPathAppendix, std::function<void(json_spirit::mValue&, bool)> doTests); void executeTests(const std::string& _name, const std::string& _testPathAppendix, std::function<void(json_spirit::mValue&, bool)> doTests);
std::string getTestPath(); std::string getTestPath();
void userDefinedTest(std::string testTypeFlag, std::function<void(json_spirit::mValue&, bool)> doTests); void userDefinedTest(std::string testTypeFlag, std::function<void(json_spirit::mValue&, bool)> doTests);

4
test/solidityCompiler.cpp

@ -48,11 +48,11 @@ bytes compileContract(const string& _sourceCode)
Parser parser; Parser parser;
ASTPointer<ContractDefinition> contract; ASTPointer<ContractDefinition> contract;
BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode)))); BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
NameAndTypeResolver resolver; NameAndTypeResolver resolver({});
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract)); BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
Compiler compiler; Compiler compiler;
compiler.compileContract(*contract); compiler.compileContract(*contract, {});
// debug // debug
//compiler.streamAssembly(cout); //compiler.streamAssembly(cout);
return compiler.getAssembledBytecode(); return compiler.getAssembledBytecode();

70
test/solidityEndToEndTest.cpp

@ -45,17 +45,17 @@ class ExecutionFramework
public: public:
ExecutionFramework() { g_logVerbosity = 0; } ExecutionFramework() { g_logVerbosity = 0; }
bytes const& compileAndRun(string const& _sourceCode) bytes const& compileAndRun(string const& _sourceCode, u256 const& _value = 0)
{ {
bytes code = dev::solidity::CompilerStack::staticCompile(_sourceCode); bytes code = dev::solidity::CompilerStack::staticCompile(_sourceCode);
sendMessage(code, true); sendMessage(code, true, _value);
BOOST_REQUIRE(!m_output.empty()); BOOST_REQUIRE(!m_output.empty());
return m_output; return m_output;
} }
bytes const& callContractFunction(byte _index, bytes const& _data = bytes()) bytes const& callContractFunction(byte _index, bytes const& _data = bytes(), u256 const& _value = 0)
{ {
sendMessage(bytes(1, _index) + _data, false); sendMessage(bytes(1, _index) + _data, false, _value);
return m_output; return m_output;
} }
@ -111,11 +111,11 @@ private:
return toBigEndian(_cppFunction(_arguments...)); return toBigEndian(_cppFunction(_arguments...));
} }
void sendMessage(bytes const& _data, bool _isCreation) void sendMessage(bytes const& _data, bool _isCreation, u256 const& _value = 0)
{ {
eth::Executive executive(m_state); eth::Executive executive(m_state);
eth::Transaction t = _isCreation ? eth::Transaction(0, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec())
: eth::Transaction(0, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec());
bytes transactionRLP = t.rlp(); bytes transactionRLP = t.rlp();
try try
{ {
@ -125,7 +125,7 @@ private:
catch (...) {} catch (...) {}
if (_isCreation) if (_isCreation)
{ {
BOOST_REQUIRE(!executive.create(Address(), 0, m_gasPrice, m_gas, &_data, Address())); BOOST_REQUIRE(!executive.create(Address(), _value, m_gasPrice, m_gas, &_data, Address()));
m_contractAddress = executive.newAddress(); m_contractAddress = executive.newAddress();
BOOST_REQUIRE(m_contractAddress); BOOST_REQUIRE(m_contractAddress);
BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
@ -133,7 +133,7 @@ private:
else else
{ {
BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress)); BOOST_REQUIRE(m_state.addressHasCode(m_contractAddress));
BOOST_REQUIRE(!executive.call(m_contractAddress, Address(), 0, m_gasPrice, &_data, m_gas, Address())); BOOST_REQUIRE(!executive.call(m_contractAddress, Address(), _value, m_gasPrice, &_data, m_gas, Address()));
} }
BOOST_REQUIRE(executive.go()); BOOST_REQUIRE(executive.go());
executive.finalize(); executive.finalize();
@ -700,6 +700,34 @@ BOOST_AUTO_TEST_CASE(structs)
BOOST_CHECK(callContractFunction(0) == bytes({0x01})); BOOST_CHECK(callContractFunction(0) == bytes({0x01}));
} }
BOOST_AUTO_TEST_CASE(struct_reference)
{
char const* sourceCode = "contract test {\n"
" struct s2 {\n"
" uint32 z;\n"
" mapping(uint8 => s2) recursive;\n"
" }\n"
" s2 data;\n"
" function check() returns (bool ok) {\n"
" return data.z == 2 && \n"
" data.recursive[0].z == 3 && \n"
" data.recursive[0].recursive[1].z == 0 && \n"
" data.recursive[0].recursive[0].z == 1;\n"
" }\n"
" function set() {\n"
" data.z = 2;\n"
" var map = data.recursive;\n"
" s2 inner = map[0];\n"
" inner.z = 3;\n"
" inner.recursive[0].z = inner.recursive[1].z + 1;\n"
" }\n"
"}\n";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction(0) == bytes({0x00}));
BOOST_CHECK(callContractFunction(1) == bytes());
BOOST_CHECK(callContractFunction(0) == bytes({0x01}));
}
BOOST_AUTO_TEST_CASE(constructor) BOOST_AUTO_TEST_CASE(constructor)
{ {
char const* sourceCode = "contract test {\n" char const* sourceCode = "contract test {\n"
@ -722,6 +750,30 @@ BOOST_AUTO_TEST_CASE(constructor)
testSolidityAgainstCpp(0, get, u256(7)); testSolidityAgainstCpp(0, get, u256(7));
} }
BOOST_AUTO_TEST_CASE(balance)
{
char const* sourceCode = "contract test {\n"
" function getBalance() returns (uint256 balance) {\n"
" return address(this).balance;\n"
" }\n"
"}\n";
compileAndRun(sourceCode, 23);
BOOST_CHECK(callContractFunction(0) == toBigEndian(u256(23)));
}
BOOST_AUTO_TEST_CASE(blockchain)
{
char const* sourceCode = "contract test {\n"
" function someInfo() returns (uint256 value, address coinbase, uint256 blockNumber) {\n"
" value = msg.value;\n"
" coinbase = block.coinbase;\n"
" blockNumber = block.number;\n"
" }\n"
"}\n";
compileAndRun(sourceCode, 27);
BOOST_CHECK(callContractFunction(0, bytes{0}, u256(28)) == toBigEndian(u256(28)) + bytes(20, 0) + toBigEndian(u256(1)));
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

2
test/solidityExpressionCompiler.cpp

@ -88,7 +88,7 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
Parser parser; Parser parser;
ASTPointer<ContractDefinition> contract; ASTPointer<ContractDefinition> contract;
BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode)))); BOOST_REQUIRE_NO_THROW(contract = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
NameAndTypeResolver resolver; NameAndTypeResolver resolver({});
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract)); BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
FirstExpressionExtractor extractor(*contract); FirstExpressionExtractor extractor(*contract);
BOOST_REQUIRE(extractor.getExpression() != nullptr); BOOST_REQUIRE(extractor.getExpression() != nullptr);

22
test/solidityNameAndTypeResolution.cpp

@ -43,7 +43,7 @@ void parseTextAndResolveNames(std::string const& _source)
Parser parser; Parser parser;
ASTPointer<ContractDefinition> contract = parser.parse( ASTPointer<ContractDefinition> contract = parser.parse(
std::make_shared<Scanner>(CharStream(_source))); std::make_shared<Scanner>(CharStream(_source)));
NameAndTypeResolver resolver; NameAndTypeResolver resolver({});
resolver.resolveNamesAndTypes(*contract); resolver.resolveNamesAndTypes(*contract);
} }
} }
@ -224,6 +224,26 @@ BOOST_AUTO_TEST_CASE(type_inference_explicit_conversion)
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text)); BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
} }
BOOST_AUTO_TEST_CASE(balance)
{
char const* text = "contract test {\n"
" function fun() {\n"
" uint256 x = address(0).balance;\n"
" }\n"
"}\n";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(balance_invalid)
{
char const* text = "contract test {\n"
" function fun() {\n"
" address(0).balance = 7;\n"
" }\n"
"}\n";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

68
test/stPreCompiledContractsFiller.json

@ -33,6 +33,40 @@
} }
}, },
"CallEcrecover0_completeReturnValue": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "10000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "20000000",
"nonce" : 0,
"code": "{ (MSTORE 0 0x18c547e4f7b0f325ad1e56f57e26c745b09a3e503d86e00e5255ff7f715d3d1c) (MSTORE 32 28) (MSTORE 64 0x73b1693892219d736caba55bdb67216e485557ea6b6af75f37096c9aa6a5a75f) (MSTORE 96 0xeeb940b1d03b21e36b0e47e79769f095fe2ab855bd91e3a38756b7d75a9c4549) [[ 2 ]] (CALL 1000 1 0 0 128 128 32) [[ 0 ]] (MLOAD 128) }",
"storage": {}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "",
"storage": {}
}
},
"transaction" : {
"nonce" : "0",
"gasPrice" : "1",
"gasLimit" : "365224",
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87",
"value" : "100000",
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
"data" : ""
}
},
"CallEcrecover0_gas500": { "CallEcrecover0_gas500": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
@ -305,6 +339,40 @@
} }
}, },
"CallSha256_1_nonzeroValue": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "10000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "20000000",
"nonce" : 0,
"code" : "{ [[ 2 ]] (CALL 500 2 0x13 0 0 0 32) [[ 0 ]] (MLOAD 0)}",
"storage": {}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "",
"storage": {}
}
},
"transaction" : {
"nonce" : "0",
"gasPrice" : "1",
"gasLimit" : "365224",
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87",
"value" : "100000",
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
"data" : ""
}
},
"CallSha256_2": { "CallSha256_2": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",

41
test/stSpecialTestFiller.json

@ -0,0 +1,41 @@
{
"makeMoney" : {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "1000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"095e7baea6a6c7c4c2dfeb977efac326af552d87" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "{ (MSTORE 0 0x601080600c6000396000f20060003554156009570060203560003555) (CALL 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec 0xaaaaaaaaace5edbc8e2a8697c15331677e6ebf0b 23 0 0 0 0) }",
"storage": {}
},
"aaaaaaaaace5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "0x600160015532600255",
"storage": {}
},
"a94f5374fce5edbc8e2a8697c15331677e6ebf0b" : {
"balance" : "1000",
"nonce" : 0,
"code" : "",
"storage": {}
}
},
"transaction" : {
"nonce" : "0",
"gasPrice" : "1",
"gasLimit" : "850",
"to" : "095e7baea6a6c7c4c2dfeb977efac326af552d87",
"value" : "10",
"secretKey" : "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8",
"data" : ""
}
}
}

5
test/state.cpp

@ -122,6 +122,11 @@ BOOST_AUTO_TEST_CASE(stPreCompiledContracts)
dev::test::executeTests("stPreCompiledContracts", "/StateTests", dev::test::doStateTests); dev::test::executeTests("stPreCompiledContracts", "/StateTests", dev::test::doStateTests);
} }
BOOST_AUTO_TEST_CASE(stSpecialTest)
{
dev::test::executeTests("stSpecialTest", "/StateTests", dev::test::doStateTests);
}
BOOST_AUTO_TEST_CASE(stCreateTest) BOOST_AUTO_TEST_CASE(stCreateTest)
{ {
for (int i = 1; i < boost::unit_test::framework::master_test_suite().argc; ++i) for (int i = 1; i < boost::unit_test::framework::master_test_suite().argc; ++i)

47
test/vm.cpp

@ -120,6 +120,41 @@ void FakeExtVM::importEnv(mObject& _o)
currentBlock.coinbaseAddress = Address(_o["currentCoinbase"].get_str()); currentBlock.coinbaseAddress = Address(_o["currentCoinbase"].get_str());
} }
mObject FakeExtVM::exportLog()
{
mObject ret;
for (LogEntry const& l: sub.logs)
{
mObject o;
o["address"] = toString(l.address);
mArray topics;
for (auto const& t: l.topics)
topics.push_back(toString(t));
o["topics"] = topics;
o["data"] = "0x" + toHex(l.data);
ret[toString(l.bloom())] = o;
}
return ret;
}
void FakeExtVM::importLog(mObject& _o)
{
for (auto const& l: _o)
{
mObject o = l.second.get_obj();
// cant use BOOST_REQUIRE, because this function is used outside boost test (createRandomTest)
assert(o.count("address") > 0);
assert(o.count("topics") > 0);
assert(o.count("data") > 0);
LogEntry log;
log.address = Address(o["address"].get_str());
for (auto const& t: o["topics"].get_array())
log.topics.insert(h256(t.get_str()));
log.data = importData(o);
sub.logs.push_back(log);
}
}
mObject FakeExtVM::exportState() mObject FakeExtVM::exportState()
{ {
mObject ret; mObject ret;
@ -303,7 +338,7 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
bool vmExceptionOccured = false; bool vmExceptionOccured = false;
try try
{ {
output = vm.go(fev, fev.simpleTrace()).toVector(); output = vm.go(fev, fev.simpleTrace()).toBytes();
gas = vm.gas(); gas = vm.gas();
} }
catch (VMException const& _e) catch (VMException const& _e)
@ -349,6 +384,7 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
o["callcreates"] = fev.exportCallCreates(); o["callcreates"] = fev.exportCallCreates();
o["out"] = "0x" + toHex(output); o["out"] = "0x" + toHex(output);
fev.push(o, "gas", gas); fev.push(o, "gas", gas);
o["logs"] = mValue(fev.exportLog());
} }
} }
else else
@ -361,10 +397,12 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
BOOST_REQUIRE(o.count("callcreates") > 0); BOOST_REQUIRE(o.count("callcreates") > 0);
BOOST_REQUIRE(o.count("out") > 0); BOOST_REQUIRE(o.count("out") > 0);
BOOST_REQUIRE(o.count("gas") > 0); BOOST_REQUIRE(o.count("gas") > 0);
BOOST_REQUIRE(o.count("logs") > 0);
dev::test::FakeExtVM test; dev::test::FakeExtVM test;
test.importState(o["post"].get_obj()); test.importState(o["post"].get_obj());
test.importCallCreates(o["callcreates"].get_array()); test.importCallCreates(o["callcreates"].get_array());
test.importLog(o["logs"].get_obj());
checkOutput(output, o); checkOutput(output, o);
@ -392,6 +430,8 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
checkAddresses<std::map<Address, std::tuple<u256, u256, std::map<u256, u256>, bytes> > >(test.addresses, fev.addresses); checkAddresses<std::map<Address, std::tuple<u256, u256, std::map<u256, u256>, bytes> > >(test.addresses, fev.addresses);
BOOST_CHECK(test.callcreates == fev.callcreates); BOOST_CHECK(test.callcreates == fev.callcreates);
checkLog(fev.sub.logs, test.sub.logs);
} }
else // Exception expected else // Exception expected
BOOST_CHECK(vmExceptionOccured); BOOST_CHECK(vmExceptionOccured);
@ -443,6 +483,11 @@ BOOST_AUTO_TEST_CASE(vmPushDupSwapTest)
dev::test::executeTests("vmPushDupSwapTest", "/VMTests", dev::test::doVMTests); dev::test::executeTests("vmPushDupSwapTest", "/VMTests", dev::test::doVMTests);
} }
BOOST_AUTO_TEST_CASE(vmLogTest)
{
dev::test::executeTests("vmLogTest", "/VMTests", dev::test::doVMTests);
}
BOOST_AUTO_TEST_CASE(vmRandom) BOOST_AUTO_TEST_CASE(vmRandom)
{ {
string testPath = getTestPath(); string testPath = getTestPath();

2
test/vm.h

@ -66,6 +66,8 @@ public:
u256 doPosts(); u256 doPosts();
json_spirit::mObject exportEnv(); json_spirit::mObject exportEnv();
void importEnv(json_spirit::mObject& _o); void importEnv(json_spirit::mObject& _o);
json_spirit::mObject exportLog();
void importLog(json_spirit::mObject& _o);
json_spirit::mObject exportState(); json_spirit::mObject exportState();
void importState(json_spirit::mObject& _object); void importState(json_spirit::mObject& _object);
json_spirit::mObject exportExec(); json_spirit::mObject exportExec();

4
test/vmIOandFlowOperationsTestFiller.json

@ -55,7 +55,7 @@
} }
}, },
"dupAt51doesNotExistAnymore": { "dupAt51becameMload": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0", "currentNumber" : "0",
@ -83,7 +83,7 @@
} }
}, },
"swapAt52doesNotExistAnymore": { "swapAt52becameMstore": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0", "currentNumber" : "0",

1266
test/vmLogTestFiller.json

File diff suppressed because it is too large

31
test/vmPushDupSwapTestFiller.json

@ -924,7 +924,7 @@
} }
}, },
"push32error": { "push32AndSuicide": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0", "currentNumber" : "0",
@ -952,6 +952,35 @@
} }
}, },
"push32FillUpInputWithZerosAtTheEnd": {
"env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0",
"currentGasLimit" : "1000000",
"currentDifficulty" : "256",
"currentTimestamp" : 1,
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba"
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"nonce" : 0,
"code" : "0x7fff10112233445566778899aabbccddeeff00112233445566778899aabbccdd",
"storage": {}
}
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000",
"data" : "",
"gasPrice" : "100000000000000",
"gas" : "10000"
}
},
"dup1": { "dup1": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",

Loading…
Cancel
Save