Browse Source

NameRegNamer for NameReg integration.

cl-refactor
Gav Wood 10 years ago
parent
commit
58545a4589
  1. 1
      alethzero/MainFace.h
  2. 11
      alethzero/MainWin.cpp
  3. 2
      alethzero/MainWin.h
  4. 106
      alethzero/NameRegNamer.cpp
  5. 59
      alethzero/NameRegNamer.h
  6. 25
      libethcore/ABI.h

1
alethzero/MainFace.h

@ -93,6 +93,7 @@ public:
virtual unsigned installWatch(dev::eth::LogFilter const& _tf, WatchHandler const& _f) = 0;
virtual unsigned installWatch(dev::h256 const& _tf, WatchHandler const& _f) = 0;
virtual void uninstallWatch(unsigned _id) = 0;
// Account naming API
virtual void install(AccountNamer* _adopt) = 0;

11
alethzero/MainWin.cpp

@ -94,8 +94,8 @@ QString dev::az::contentsOfQResource(string const& res)
}
//Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f");
Address c_newConfig = Address("c6d9d2cd449a754c494264e1809c50e34d64562b");
//Address c_nameReg = Address("ddd1cea741d548f90d86fb87a3ae6492e18c03a1");
//Address c_newConfig = Address("c6d9d2cd449a754c494264e1809c50e34d64562b");
Address c_nameReg = Address("96d76ae3397b52d9f61215270df65d72358709e3");
Main::Main(QWidget* _parent):
MainFace(_parent),
@ -486,8 +486,8 @@ void Main::installWatches()
cdebug << "newBlock watch ID: " << newBlockId;
cdebug << "newPending watch ID: " << newPendingId;
installWatch(LogFilter().address(c_newConfig), [=](LocalisedLogEntries const&) { installNameRegWatch(); });
installWatch(LogFilter().address(c_newConfig), [=](LocalisedLogEntries const&) { installCurrenciesWatch(); });
// installWatch(LogFilter().address(c_newConfig), [=](LocalisedLogEntries const&) { installNameRegWatch(); });
// installWatch(LogFilter().address(c_newConfig), [=](LocalisedLogEntries const&) { installCurrenciesWatch(); });
}
Address Main::getNameReg() const
@ -498,7 +498,8 @@ Address Main::getNameReg() const
Address Main::getCurrencies() const
{
return abiOut<Address>(ethereum()->call(c_newConfig, abiIn("lookup(uint256)", (u256)3)).output);
// return abiOut<Address>(ethereum()->call(c_newConfig, abiIn("lookup(uint256)", (u256)3)).output);
return Address();
}
bool Main::doConfirm()

2
alethzero/MainWin.h

@ -240,7 +240,7 @@ private:
unsigned installWatch(eth::LogFilter const& _tf, WatchHandler const& _f) override;
unsigned installWatch(h256 const& _tf, WatchHandler const& _f) override;
void uninstallWatch(unsigned _w);
void uninstallWatch(unsigned _w) override;
void keysChanged();

106
alethzero/NameRegNamer.cpp

@ -0,0 +1,106 @@
/*
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/>.
*/
/** @file NameRegNamer.h
* @author Gav Wood <i@gavwood.com>
* @date 2015
*/
#include "NameRegNamer.h"
#include <libdevcore/Log.h>
#include <libethereum/Client.h>
using namespace std;
using namespace dev;
using namespace az;
using namespace eth;
DEV_AZ_NOTE_PLUGIN(NameRegNamer);
NameRegNamer::NameRegNamer(MainFace* _m):
AccountNamerPlugin(_m, "NameRegNamer")
{
}
NameRegNamer::~NameRegNamer()
{
}
string NameRegNamer::toName(Address const& _a) const
{
for (auto const& r: m_registrars)
{
string n = abiOut<string>(main()->ethereum()->call(r, abiIn("name(address)", _a)).output);
if (!n.empty())
return n;
}
return string();
}
Address NameRegNamer::toAddress(std::string const& _n) const
{
for (auto const& r: m_registrars)
if (Address a = abiOut<Address>(main()->ethereum()->call(r, abiIn("addr(string)", _n)).output))
return a;
return Address();
}
Addresses NameRegNamer::knownAddresses() const
{
return m_knownCache;
}
void NameRegNamer::killRegistrar(Address const& _r)
{
if (m_filters.count(_r))
{
main()->uninstallWatch(m_filters.at(_r));
m_filters.erase(_r);
}
for (auto i = m_registrars.begin(); i != m_registrars.end();)
if (*i == _r)
i = m_registrars.erase(i);
else
++i;
}
void NameRegNamer::updateCache()
{
// m_forwardCache.clear();
// m_reverseCache.clear();
m_knownCache.clear();
#if ETH_FATDB || !ETH_TRUE
for (auto const& r: m_registrars)
for (u256 const& a: keysOf(ethereum()->storageAt(r)))
if (a < u256(1) << 160)
m_knownCache.push_back(Address((u160)a - 1));
#endif
}
void NameRegNamer::readSettings(QSettings const& _s)
{
(void)_s;
while (!m_registrars.empty())
killRegistrar(m_registrars.back());
Address a("96d76ae3397b52d9f61215270df65d72358709e3");
m_filters[a] = main()->installWatch(LogFilter().address(a), [=](LocalisedLogEntries const&){ updateCache(); });
noteKnownChanged();
}
void NameRegNamer::writeSettings(QSettings&)
{
}

59
alethzero/NameRegNamer.h

@ -0,0 +1,59 @@
/*
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/>.
*/
/** @file NameRegNamer.h
* @author Gav Wood <i@gavwood.com>
* @date 2015
*/
#pragma once
#include "MainFace.h"
namespace dev
{
namespace az
{
class NameRegNamer: public QObject, public AccountNamerPlugin
{
Q_OBJECT
public:
NameRegNamer(MainFace* _m);
~NameRegNamer();
private:
void readSettings(QSettings const&) override;
void writeSettings(QSettings&) override;
std::string toName(Address const&) const override;
Address toAddress(std::string const&) const override;
Addresses knownAddresses() const override;
void updateCache();
void killRegistrar(Address const& _r);
Addresses m_registrars;
std::unordered_map<Address, unsigned> m_filters;
mutable Addresses m_knownCache;
// mutable std::unordered_map<Address, std::string> m_forwardCache;
// mutable std::unordered_map<std::string, Address> m_reverseCache;
};
}
}

25
libethcore/ABI.h

@ -43,7 +43,17 @@ template <class T> struct ABISerialiser {};
template <unsigned N> struct ABISerialiser<FixedHash<N>> { static bytes serialise(FixedHash<N> const& _t) { static_assert(N <= 32, "Cannot serialise hash > 32 bytes."); static_assert(N > 0, "Cannot serialise zero-length hash."); return bytes(32 - N, 0) + _t.asBytes(); } };
template <> struct ABISerialiser<u256> { static bytes serialise(u256 const& _t) { return h256(_t).asBytes(); } };
template <> struct ABISerialiser<u160> { static bytes serialise(u160 const& _t) { return bytes(12, 0) + h160(_t).asBytes(); } };
template <> struct ABISerialiser<string32> { static bytes serialise(string32 const& _t) { return bytesConstRef((byte const*)_t.data(), 32).toBytes(); } };
template <> struct ABISerialiser<string32> { static bytes serialise(string32 const& _t) { bytes ret; bytesConstRef((byte const*)_t.data(), 32).populate(bytesRef(&ret)); return ret; } };
template <> struct ABISerialiser<std::string>
{
static bytes serialise(std::string const& _t)
{
bytes ret = h256(u256(32)).asBytes() + h256(u256(_t.size())).asBytes();
ret.resize(ret.size() + (_t.size() + 31) / 32 * 32);
bytesConstRef(&_t).populate(bytesRef(&ret).cropped(64));
return ret;
}
};
inline bytes abiInAux() { return {}; }
template <class T, class ... U> bytes abiInAux(T const& _t, U const& ... _u)
@ -61,6 +71,19 @@ template <unsigned N> struct ABIDeserialiser<FixedHash<N>> { static FixedHash<N>
template <> struct ABIDeserialiser<u256> { static u256 deserialise(bytesConstRef& io_t) { u256 ret = fromBigEndian<u256>(io_t.cropped(0, 32)); io_t = io_t.cropped(32); return ret; } };
template <> struct ABIDeserialiser<u160> { static u160 deserialise(bytesConstRef& io_t) { u160 ret = fromBigEndian<u160>(io_t.cropped(12, 20)); io_t = io_t.cropped(32); return ret; } };
template <> struct ABIDeserialiser<string32> { static string32 deserialise(bytesConstRef& io_t) { string32 ret; io_t.cropped(0, 32).populate(bytesRef((byte*)ret.data(), 32)); io_t = io_t.cropped(32); return ret; } };
template <> struct ABIDeserialiser<std::string>
{
static std::string deserialise(bytesConstRef& io_t)
{
unsigned o = (uint16_t)u256(h256(io_t.cropped(0, 32)));
unsigned s = (uint16_t)u256(h256(io_t.cropped(o, 32)));
std::string ret;
ret.resize(s);
io_t.cropped(o + 32, s).populate(bytesRef((byte*)ret.data(), s));
io_t = io_t.cropped(32);
return ret;
}
};
template <class T> T abiOut(bytes const& _data)
{

Loading…
Cancel
Save