Browse Source

Latest API updates.

cl-refactor
Gav Wood 11 years ago
parent
commit
485828ea30
  1. 2
      alethzero/MainWin.cpp
  2. 2
      libethcore/CommonEth.cpp
  3. 61
      libethereum/Client.cpp
  4. 29
      libethereum/Client.h
  5. 8
      liblll/Parser.cpp
  6. 42
      libqethereum/QEthereum.cpp
  7. 7
      libqethereum/QEthereum.h

2
alethzero/MainWin.cpp

@ -789,7 +789,7 @@ void Main::ourAccountsRowsMoved()
myKeys.push_back(i); myKeys.push_back(i);
} }
m_myKeys = myKeys; m_myKeys = myKeys;
changed(); m_ethereum->setAccounts(m_myKeys);
} }
void Main::on_inject_triggered() void Main::on_inject_triggered()

2
libethcore/CommonEth.cpp

@ -29,7 +29,7 @@ using namespace eth;
//#define ETH_ADDRESS_DEBUG 1 //#define ETH_ADDRESS_DEBUG 1
const unsigned eth::c_protocolVersion = 21; const unsigned eth::c_protocolVersion = 22;
static const vector<pair<u256, string>> g_units = static const vector<pair<u256, string>> g_units =
{ {

61
libethereum/Client.cpp

@ -277,12 +277,12 @@ void Client::work()
m_changed = m_changed || changed; m_changed = m_changed || changed;
} }
void Client::lock() void Client::lock() const
{ {
m_lock.lock(); m_lock.lock();
} }
void Client::unlock() void Client::unlock() const
{ {
m_lock.unlock(); m_lock.unlock();
} }
@ -309,21 +309,25 @@ State Client::asOf(int _h) const
u256 Client::balanceAt(Address _a, int _block) const u256 Client::balanceAt(Address _a, int _block) const
{ {
ClientGuard l(this);
return asOf(_block).balance(_a); return asOf(_block).balance(_a);
} }
u256 Client::countAt(Address _a, int _block) const u256 Client::countAt(Address _a, int _block) const
{ {
ClientGuard l(this);
return asOf(_block).transactionsFrom(_a); return asOf(_block).transactionsFrom(_a);
} }
u256 Client::stateAt(Address _a, u256 _l, int _block) const u256 Client::stateAt(Address _a, u256 _l, int _block) const
{ {
ClientGuard l(this);
return asOf(_block).storage(_a, _l); return asOf(_block).storage(_a, _l);
} }
bytes Client::codeAt(Address _a, int _block) const bytes Client::codeAt(Address _a, int _block) const
{ {
ClientGuard l(this);
return asOf(_block).code(_a); return asOf(_block).code(_a);
} }
@ -337,28 +341,28 @@ bool TransactionFilter::matches(State const& _s, unsigned _i) const
if (m_stateAltered.empty() && m_altered.empty()) if (m_stateAltered.empty() && m_altered.empty())
return true; return true;
StateDiff d = _s.pendingDiff(_i); StateDiff d = _s.pendingDiff(_i);
if (!m_stateAltered.empty())
{
for (auto const& s: m_stateAltered)
if (d.accounts.count(s.first) && d.accounts.at(s.first).storage.count(s.second))
goto OK;
return false;
OK:;
}
if (!m_altered.empty()) if (!m_altered.empty())
{ {
for (auto const& s: m_altered) for (auto const& s: m_altered)
if (d.accounts.count(s)) if (d.accounts.count(s))
goto OK2; return true;
return false;
}
if (!m_stateAltered.empty())
{
for (auto const& s: m_stateAltered)
if (d.accounts.count(s.first) && d.accounts.at(s.first).storage.count(s.second))
return true;
return false; return false;
OK2:;
} }
return true; return true;
} }
Transactions Client::transactions(TransactionFilter const& _f) const PastTransactions Client::transactions(TransactionFilter const& _f) const
{ {
Transactions ret; ClientGuard l(this);
PastTransactions ret;
unsigned begin = numberOf(_f.latest()); unsigned begin = numberOf(_f.latest());
unsigned end = min(begin, numberOf(_f.earliest())); unsigned end = min(begin, numberOf(_f.earliest()));
unsigned m = _f.max(); unsigned m = _f.max();
@ -373,25 +377,34 @@ Transactions Client::transactions(TransactionFilter const& _f) const
if (s) if (s)
s--; s--;
else else
ret.insert(ret.begin(), m_postMine.pending()[i]); ret.insert(ret.begin(), PastTransaction(m_postMine.pending()[i], h256(), i, time(0), 0));
} }
// Early exit here since we can't rely on begin/end, being out of the blockchain as we are. // Early exit here since we can't rely on begin/end, being out of the blockchain as we are.
if (_f.earliest() == 0) if (_f.earliest() == 0)
return ret; return ret;
} }
auto cn = m_bc.number();
auto h = m_bc.numberHash(begin); auto h = m_bc.numberHash(begin);
for (unsigned n = begin; ret.size() != m; n--, h = m_bc.details(h).parent) for (unsigned n = begin; ret.size() != m; n--, h = m_bc.details(h).parent)
{ {
State st(m_stateDB, m_bc, h); try
for (unsigned i = st.pending().size(); i--;) {
if (_f.matches(st, i)) State st(m_stateDB, m_bc, h);
{ for (unsigned i = st.pending().size(); i--;)
if (s) if (_f.matches(st, i))
s--; {
else if (s)
ret.insert(ret.begin(), st.pending()[i]); s--;
} else
ret.insert(ret.begin(), PastTransaction(st.pending()[i], h, i, BlockInfo(m_bc.block(h)).timestamp, cn - n + 2));
}
}
catch (...)
{
// Gaa. bad state. not good at all. bury head in sand for now.
}
if (n == end) if (n == end)
break; break;
} }

29
libethereum/Client.h

@ -49,11 +49,11 @@ class Client;
class ClientGuard class ClientGuard
{ {
public: public:
inline ClientGuard(Client* _c); inline ClientGuard(Client const* _c);
inline ~ClientGuard(); inline ~ClientGuard();
private: private:
Client* m_client; Client const* m_client;
}; };
enum ClientWorkState enum ClientWorkState
@ -92,8 +92,8 @@ public:
TransactionFilter from(Address _a) { m_from.insert(_a); return *this; } TransactionFilter from(Address _a) { m_from.insert(_a); return *this; }
TransactionFilter to(Address _a) { m_to.insert(_a); return *this; } TransactionFilter to(Address _a) { m_to.insert(_a); return *this; }
TransactionFilter stateAltered(Address _a, u256 _l) { m_stateAltered.insert(std::make_pair(_a, _l)); return *this; } TransactionFilter altered(Address _a, u256 _l) { m_stateAltered.insert(std::make_pair(_a, _l)); return *this; }
TransactionFilter stateAltered(Address _a) { m_altered.insert(_a); return *this; } TransactionFilter altered(Address _a) { m_altered.insert(_a); return *this; }
TransactionFilter withMax(unsigned _m) { m_max = _m; return *this; } TransactionFilter withMax(unsigned _m) { m_max = _m; return *this; }
TransactionFilter withSkip(unsigned _m) { m_skip = _m; return *this; } TransactionFilter withSkip(unsigned _m) { m_skip = _m; return *this; }
TransactionFilter withEarliest(int _e) { m_earliest = _e; return *this; } TransactionFilter withEarliest(int _e) { m_earliest = _e; return *this; }
@ -110,6 +110,17 @@ private:
unsigned m_skip; unsigned m_skip;
}; };
struct PastTransaction: public Transaction
{
PastTransaction(Transaction const& _t, h256 _b, u256 _i, u256 _ts, int _age): Transaction(_t), block(_b), index(_i), timestamp(_ts), age(_age) {}
h256 block;
u256 index;
u256 timestamp;
int age;
};
typedef std::vector<PastTransaction> PastTransactions;
/** /**
* @brief Main API hub for interfacing with Ethereum. * @brief Main API hub for interfacing with Ethereum.
*/ */
@ -162,8 +173,8 @@ public:
// [OLD API]: // [OLD API]:
/// Locks/unlocks the state/blockChain/transactionQueue for access. /// Locks/unlocks the state/blockChain/transactionQueue for access.
void lock(); void lock() const;
void unlock(); void unlock() const;
/// Get the object representing the current state of Ethereum. /// Get the object representing the current state of Ethereum.
State const& state() const { return m_preMine; } State const& state() const { return m_preMine; }
@ -178,7 +189,7 @@ public:
u256 countAt(Address _a, int _block = -1) const; u256 countAt(Address _a, int _block = -1) const;
u256 stateAt(Address _a, u256 _l, int _block = -1) const; u256 stateAt(Address _a, u256 _l, int _block = -1) const;
bytes codeAt(Address _a, int _block = -1) const; bytes codeAt(Address _a, int _block = -1) const;
Transactions transactions(TransactionFilter const& _f) const; PastTransactions transactions(TransactionFilter const& _f) const;
// Misc stuff: // Misc stuff:
@ -245,7 +256,7 @@ private:
std::unique_ptr<std::thread> m_work;///< The work thread. std::unique_ptr<std::thread> m_work;///< The work thread.
std::recursive_mutex m_lock; mutable std::recursive_mutex m_lock;
std::atomic<ClientWorkState> m_workState; std::atomic<ClientWorkState> m_workState;
bool m_paranoia = false; bool m_paranoia = false;
bool m_doMine = false; ///< Are we supposed to be mining? bool m_doMine = false; ///< Are we supposed to be mining?
@ -256,7 +267,7 @@ private:
mutable bool m_changed; mutable bool m_changed;
}; };
inline ClientGuard::ClientGuard(Client* _c): m_client(_c) inline ClientGuard::ClientGuard(Client const* _c): m_client(_c)
{ {
m_client->lock(); m_client->lock();
} }

8
liblll/Parser.cpp

@ -94,15 +94,9 @@ void eth::parseTreeLLL(string const& _s, sp::utree& o_out)
qi::rule<it, qi::ascii::space_type, sp::utree::list_type()> sstore = qi::lit("[[") > element > qi::lit("]]") > -qi::lit(":") > element; qi::rule<it, qi::ascii::space_type, sp::utree::list_type()> sstore = qi::lit("[[") > element > qi::lit("]]") > -qi::lit(":") > element;
qi::rule<it, qi::ascii::space_type, sp::utree::list_type()> list = '(' > *element > ')'; qi::rule<it, qi::ascii::space_type, sp::utree::list_type()> list = '(' > *element > ')';
// todo: fix compound compile errors in this line for Visual Studio 2013
//#ifndef _MSC_VER
auto x = [](int a) { return [=](sp::utree& n, typename qi::rule<it, qi::ascii::space_type, sp::utree()>::context_type& c) { (boost::fusion::at_c<0>(c.attributes) = n).tag(a); }; }; auto x = [](int a) { return [=](sp::utree& n, typename qi::rule<it, qi::ascii::space_type, sp::utree()>::context_type& c) { (boost::fusion::at_c<0>(c.attributes) = n).tag(a); }; };
qi::rule<it, qi::ascii::space_type, sp::utree()> extra = mload[x(1)] | sload[x(2)] | mstore[x(3)] | sstore[x(4)] | seq[x(5)]; qi::rule<it, qi::ascii::space_type, sp::utree()> extra = sload[x(2)] | mload[x(1)] | sstore[x(4)] | mstore[x(3)] | seq[x(5)];
element = atom | list | extra; element = atom | list | extra;
/*#else
element = atom | list;
#endif*/
string s; string s;
s.reserve(_s.size()); s.reserve(_s.size());

42
libqethereum/QEthereum.cpp

@ -252,7 +252,7 @@ void QEthereum::setup(QWebFrame* _e)
_e->evaluateJavaScript("eth.watch = function(a, s, f) { eth.changed.connect(f ? f : s) }"); _e->evaluateJavaScript("eth.watch = function(a, s, f) { eth.changed.connect(f ? f : s) }");
_e->evaluateJavaScript("eth.create = function(s, v, c, g, p, f) { var v = eth.doCreate(s, v, c, g, p); if (f) f(v) }"); _e->evaluateJavaScript("eth.create = function(s, v, c, g, p, f) { var v = eth.doCreate(s, v, c, g, p); if (f) f(v) }");
_e->evaluateJavaScript("eth.transact = function(s, v, t, d, g, p, f) { eth.doTransact(s, v, t, d, g, p); if (f) f() }"); _e->evaluateJavaScript("eth.transact = function(s, v, t, d, g, p, f) { eth.doTransact(s, v, t, d, g, p); if (f) f() }");
_e->evaluateJavaScript("eth.transactions = function(a) { return eval(eth.getTransactions(a)); }"); _e->evaluateJavaScript("eth.transactions = function(a) { return JSON.parse(eth.getTransactions(JSON.stringify(a))); }");
_e->evaluateJavaScript("String.prototype.pad = function(l, r) { return eth.pad(this, l, r) }"); _e->evaluateJavaScript("String.prototype.pad = function(l, r) { return eth.pad(this, l, r) }");
_e->evaluateJavaScript("String.prototype.bin = function() { return eth.toBinary(this) }"); _e->evaluateJavaScript("String.prototype.bin = function() { return eth.toBinary(this) }");
_e->evaluateJavaScript("String.prototype.unbin = function(l) { return eth.fromBinary(this) }"); _e->evaluateJavaScript("String.prototype.unbin = function(l) { return eth.fromBinary(this) }");
@ -398,18 +398,52 @@ QString QEthereum::getTransactions(QString _a) const
filter.withMax(f["max"].toInt()); filter.withMax(f["max"].toInt());
if (f.contains("skip")) if (f.contains("skip"))
filter.withSkip(f["skip"].toInt()); filter.withSkip(f["skip"].toInt());
if (f.contains("from"))
{
if (f["from"].isArray())
for (auto i: f["from"].toArray())
filter.from(toAddress(i.toString()));
else
filter.from(toAddress(f["from"].toString()));
}
if (f.contains("to"))
{
if (f["to"].isArray())
for (auto i: f["to"].toArray())
filter.to(toAddress(i.toString()));
else
filter.to(toAddress(f["to"].toString()));
}
if (f.contains("altered"))
{
if (f["altered"].isArray())
for (auto i: f["altered"].toArray())
if (i.isObject())
filter.altered(toAddress(i.toObject()["id"].toString()), toU256(i.toObject()["at"].toString()));
else
filter.altered(toAddress(i.toString()));
else
if (f["altered"].isObject())
filter.altered(toAddress(f["altered"].toObject()["id"].toString()), toU256(f["altered"].toObject()["at"].toString()));
else
filter.altered(toAddress(f["altered"].toString()));
}
QJsonArray ret; QJsonArray ret;
for (Transaction const& t: m_client->transactions(filter)) for (eth::PastTransaction const& t: m_client->transactions(filter))
{ {
QJsonObject v; QJsonObject v;
v["data"] = ::fromBinary(t.data); v["data"] = ::fromBinary(t.data);
v["gas"] = toQJS(t.gas); v["gas"] = toQJS(t.gas);
v["gasPrice"] = toQJS(t.gasPrice); v["gasPrice"] = toQJS(t.gasPrice);
v["nonce"] = toQJS(t.nonce); v["nonce"] = (int)t.nonce;
v["to"] = toQJS(t.receiveAddress); v["to"] = toQJS(t.receiveAddress);
v["value"] = toQJS(t.value); v["value"] = toQJS(t.value);
v["sender"] = toQJS(t.sender()); v["from"] = toQJS(t.sender());
v["timestamp"] = (int)t.timestamp;
v["block"] = toQJS(t.block);
v["index"] = (int)t.index;
v["age"] = (int)t.age;
ret.append(v); ret.append(v);
} }
return QString::fromUtf8(QJsonDocument(ret).toJson()); return QString::fromUtf8(QJsonDocument(ret).toJson());

7
libqethereum/QEthereum.h

@ -376,6 +376,8 @@ public:
void setup(QWebFrame* _e); void setup(QWebFrame* _e);
void teardown(QWebFrame* _e); void teardown(QWebFrame* _e);
void setAccounts(QList<eth::KeyPair> const& _l) { m_accounts = _l; changed(); }
Q_INVOKABLE QString ethTest() const { return "Hello world!"; } Q_INVOKABLE QString ethTest() const { return "Hello world!"; }
Q_INVOKABLE QEthereum* self() { return this; } Q_INVOKABLE QEthereum* self() { return this; }
@ -401,11 +403,6 @@ public:
Q_INVOKABLE double countAt(QString/*eth::Address*/ _a, int _block) const; Q_INVOKABLE double countAt(QString/*eth::Address*/ _a, int _block) const;
Q_INVOKABLE QString/*eth::u256*/ stateAt(QString/*eth::Address*/ _a, QString/*eth::u256*/ _p, int _block) const; Q_INVOKABLE QString/*eth::u256*/ stateAt(QString/*eth::Address*/ _a, QString/*eth::u256*/ _p, int _block) const;
Q_INVOKABLE QString/*eth::u256*/ codeAt(QString/*eth::Address*/ _a, int _block) const; Q_INVOKABLE QString/*eth::u256*/ codeAt(QString/*eth::Address*/ _a, int _block) const;
/*TransactionFilter from(Address _a) { m_from.insert(_a); return *this; }
TransactionFilter to(Address _a) { m_to.insert(_a); return *this; }
TransactionFilter stateAltered(Address _a, u256 _l) { m_stateAltered.insert(std::make_pair(_a, _l)); return *this; }
TransactionFilter stateAltered(Address _a) { m_altered.insert(_a); return *this; }*/
Q_INVOKABLE QString getTransactions(QString _attribs) const; Q_INVOKABLE QString getTransactions(QString _attribs) const;
Q_INVOKABLE QString doCreate(QString _secret, QString _amount, QString _init, QString _gas, QString _gasPrice); Q_INVOKABLE QString doCreate(QString _secret, QString _amount, QString _init, QString _gas, QString _gasPrice);

Loading…
Cancel
Save