Browse Source

Various fixes for the JS API.

cl-refactor
Gav Wood 10 years ago
parent
commit
89031ff638
  1. 3
      libdevcrypto/Common.cpp
  2. 17
      libdevcrypto/CryptoPP.cpp
  3. 25
      libethereum/Client.cpp
  4. 12
      libethereum/Client.h
  5. 7
      libethereum/Interface.h
  6. 10
      libevm/ExtVMFace.h
  7. 7
      libweb3jsonrpc/WebThreeStubServer.cpp

3
libdevcrypto/Common.cpp

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

17
libdevcrypto/CryptoPP.cpp

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

25
libethereum/Client.cpp

@ -185,7 +185,7 @@ unsigned Client::installWatch(h256 _h)
unsigned Client::installWatch(LogFilter const& _f) unsigned Client::installWatch(LogFilter const& _f)
{ {
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
h256 h = _f.sha3(); h256 h = _f.sha3();
@ -199,7 +199,7 @@ void Client::uninstallWatch(unsigned _i)
{ {
cwatch << "XXX" << _i; cwatch << "XXX" << _i;
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
auto it = m_watches.find(_i); auto it = m_watches.find(_i);
if (it == m_watches.end()) if (it == m_watches.end())
@ -215,7 +215,7 @@ void Client::uninstallWatch(unsigned _i)
void Client::noteChanged(h256Set const& _filters) void Client::noteChanged(h256Set const& _filters)
{ {
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
for (auto& i: m_watches) for (auto& i: m_watches)
if (_filters.count(i.second.id)) if (_filters.count(i.second.id))
{ {
@ -227,7 +227,7 @@ void Client::noteChanged(h256Set const& _filters)
void Client::appendFromNewPending(LogBloom _bloom, h256Set& o_changed) const void Client::appendFromNewPending(LogBloom _bloom, h256Set& o_changed) const
{ {
// TODO: more precise check on whether the txs match. // TODO: more precise check on whether the txs match.
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
for (pair<h256, InstalledFilter> const& i: m_filters) for (pair<h256, InstalledFilter> const& i: m_filters)
if ((unsigned)i.second.filter.latest() > m_bc.number() && i.second.filter.matches(_bloom)) if ((unsigned)i.second.filter.latest() > m_bc.number() && i.second.filter.matches(_bloom))
o_changed.insert(i.first); o_changed.insert(i.first);
@ -238,7 +238,7 @@ void Client::appendFromNewBlock(h256 _block, h256Set& o_changed) const
// TODO: more precise check on whether the txs match. // TODO: more precise check on whether the txs match.
auto d = m_bc.info(_block); auto d = m_bc.info(_block);
lock_guard<mutex> l(m_filterLock); Guard l(m_filterLock);
for (pair<h256, InstalledFilter> const& i: m_filters) for (pair<h256, InstalledFilter> const& i: m_filters)
if ((unsigned)i.second.filter.latest() >= d.number && (unsigned)i.second.filter.earliest() <= d.number && i.second.filter.matches(d.logBloom)) if ((unsigned)i.second.filter.latest() >= d.number && (unsigned)i.second.filter.earliest() <= d.number && i.second.filter.matches(d.logBloom))
o_changed.insert(i.first); o_changed.insert(i.first);
@ -592,16 +592,16 @@ BlockInfo Client::uncle(h256 _blockHash, unsigned _i) const
return BlockInfo::fromHeader(b[2][_i].data()); return BlockInfo::fromHeader(b[2][_i].data());
} }
LogEntries Client::logs(LogFilter const& _f) const LocalisedLogEntries Client::logs(LogFilter const& _f) const
{ {
LogEntries ret; LocalisedLogEntries ret;
unsigned begin = min<unsigned>(m_bc.number(), (unsigned)_f.latest()); unsigned begin = min<unsigned>(m_bc.number() + 1, (unsigned)_f.latest());
unsigned end = min(begin, (unsigned)_f.earliest()); unsigned end = min(m_bc.number(), min(begin, (unsigned)_f.earliest()));
unsigned m = _f.max(); unsigned m = _f.max();
unsigned s = _f.skip(); unsigned s = _f.skip();
// Handle pending transactions differently as they're not on the block chain. // Handle pending transactions differently as they're not on the block chain.
if (begin == m_bc.number()) if (begin > m_bc.number())
{ {
ReadGuard l(x_stateDB); ReadGuard l(x_stateDB);
for (unsigned i = 0; i < m_postMine.pending().size(); ++i) for (unsigned i = 0; i < m_postMine.pending().size(); ++i)
@ -615,9 +615,10 @@ LogEntries Client::logs(LogFilter const& _f) const
if (s) if (s)
s--; s--;
else else
ret.insert(ret.begin(), le[j]); ret.insert(ret.begin(), LocalisedLogEntry(le[j], begin));
} }
} }
begin = m_bc.number();
} }
#if ETH_DEBUG #if ETH_DEBUG
@ -649,7 +650,7 @@ LogEntries Client::logs(LogFilter const& _f) const
if (s) if (s)
s--; s--;
else else
ret.insert(ret.begin(), le[j]); ret.insert(ret.begin(), LocalisedLogEntry(le[j], n));
} }
} }
} }

12
libethereum/Client.h

@ -188,11 +188,11 @@ public:
virtual unsigned installWatch(LogFilter const& _filter); virtual unsigned installWatch(LogFilter const& _filter);
virtual unsigned installWatch(h256 _filterId); virtual unsigned installWatch(h256 _filterId);
virtual void uninstallWatch(unsigned _watchId); virtual void uninstallWatch(unsigned _watchId);
virtual bool peekWatch(unsigned _watchId) const { std::lock_guard<std::mutex> l(m_filterLock); try { return m_watches.at(_watchId).changes != 0; } catch (...) { return false; } } virtual bool peekWatch(unsigned _watchId) const { Guard l(m_filterLock); try { return m_watches.at(_watchId).changes != 0; } catch (...) { return false; } }
virtual bool checkWatch(unsigned _watchId) { std::lock_guard<std::mutex> l(m_filterLock); bool ret = false; try { ret = m_watches.at(_watchId).changes != 0; m_watches.at(_watchId).changes = 0; } catch (...) {} return ret; } virtual bool checkWatch(unsigned _watchId) { Guard l(m_filterLock); bool ret = false; try { ret = m_watches.at(_watchId).changes != 0; m_watches.at(_watchId).changes = 0; } catch (...) {} return ret; }
virtual LogEntries logs(unsigned _watchId) const { try { std::lock_guard<std::mutex> l(m_filterLock); return logs(m_filters.at(m_watches.at(_watchId).id).filter); } catch (...) { return LogEntries(); } } virtual LocalisedLogEntries logs(unsigned _watchId) const { try { Guard l(m_filterLock); return logs(m_filters.at(m_watches.at(_watchId).id).filter); } catch (...) { return LocalisedLogEntries(); } }
virtual LogEntries logs(LogFilter const& _filter) const; virtual LocalisedLogEntries logs(LogFilter const& _filter) const;
// [EXTRA API]: // [EXTRA API]:
@ -313,7 +313,7 @@ private:
TransactionQueue m_tq; ///< Maintains a list of incoming transactions not yet in a block on the blockchain. TransactionQueue m_tq; ///< Maintains a list of incoming transactions not yet in a block on the blockchain.
BlockQueue m_bq; ///< Maintains a list of incoming blocks not yet on the blockchain (to be imported). BlockQueue m_bq; ///< Maintains a list of incoming blocks not yet on the blockchain (to be imported).
mutable boost::shared_mutex x_stateDB; ///< Lock on the state DB, effectively a lock on m_postMine. mutable SharedMutex x_stateDB; ///< Lock on the state DB, effectively a lock on m_postMine.
OverlayDB m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it. OverlayDB m_stateDB; ///< Acts as the central point for the state database, so multiple States can share it.
State m_preMine; ///< The present state of the client. State m_preMine; ///< The present state of the client.
State m_postMine; ///< The state of the client which we're mining (i.e. it'll have all the rewards added). State m_postMine; ///< The state of the client which we're mining (i.e. it'll have all the rewards added).
@ -321,7 +321,7 @@ private:
std::weak_ptr<EthereumHost> m_host; ///< Our Ethereum Host. Don't do anything if we can't lock. std::weak_ptr<EthereumHost> m_host; ///< Our Ethereum Host. Don't do anything if we can't lock.
std::vector<Miner> m_miners; std::vector<Miner> m_miners;
mutable boost::shared_mutex x_miners; mutable SharedMutex x_miners;
bool m_paranoia = false; ///< Should we be paranoid about our state? bool m_paranoia = false; ///< Should we be paranoid about our state?
bool m_turboMining = false; ///< Don't squander all of our time mining actually just sleeping. bool m_turboMining = false; ///< Don't squander all of our time mining actually just sleeping.
bool m_forceMining = false; ///< Mine even when there are no transactions pending? bool m_forceMining = false; ///< Mine even when there are no transactions pending?

7
libethereum/Interface.h

@ -86,8 +86,8 @@ public:
// [LOGS API] // [LOGS API]
virtual LogEntries logs(unsigned _watchId) const = 0; virtual LocalisedLogEntries logs(unsigned _watchId) const = 0;
virtual LogEntries logs(LogFilter const& _filter) const = 0; virtual LocalisedLogEntries logs(LogFilter const& _filter) const = 0;
/// Install, uninstall and query watches. /// Install, uninstall and query watches.
virtual unsigned installWatch(LogFilter const& _filter) = 0; virtual unsigned installWatch(LogFilter const& _filter) = 0;
@ -180,8 +180,7 @@ public:
bool check() { return m_c ? m_c->checkWatch(m_id) : false; } bool check() { return m_c ? m_c->checkWatch(m_id) : false; }
bool peek() { return m_c ? m_c->peekWatch(m_id) : false; } bool peek() { return m_c ? m_c->peekWatch(m_id) : false; }
// PastMessages messages() const { return m_c->messages(m_id); } LocalisedLogEntries logs() const { return m_c->logs(m_id); }
LogEntries logs() const { return m_c->logs(m_id); }
private: private:
Interface* m_c = nullptr; Interface* m_c = nullptr;

10
libevm/ExtVMFace.h

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

7
libweb3jsonrpc/WebThreeStubServer.cpp

@ -72,7 +72,7 @@ static Json::Value toJson(dev::eth::Transaction const& _t)
return res; return res;
} }
static Json::Value toJson(dev::eth::LogEntry const& _e) static Json::Value toJson(dev::eth::LocalisedLogEntry const& _e)
{ {
Json::Value res; Json::Value res;
@ -80,13 +80,14 @@ static Json::Value toJson(dev::eth::LogEntry const& _e)
res["address"] = toJS(_e.address); res["address"] = toJS(_e.address);
for (auto const& t: _e.topics) for (auto const& t: _e.topics)
res["topics"].append(toJS(t)); res["topics"].append(toJS(t));
res["number"] = _e.number;
return res; return res;
} }
static Json::Value toJson(dev::eth::LogEntries const& _es) // commented to avoid warning. Uncomment once in use @ poC-7. static Json::Value toJson(dev::eth::LocalisedLogEntries const& _es) // commented to avoid warning. Uncomment once in use @ poC-7.
{ {
Json::Value res; Json::Value res;
for (dev::eth::LogEntry const& e: _es) for (dev::eth::LocalisedLogEntry const& e: _es)
res.append(toJson(e)); res.append(toJson(e));
return res; return res;
} }

Loading…
Cancel
Save