Browse Source

New client API.

cl-refactor
Gav Wood 11 years ago
parent
commit
3d3f99229e
  1. 1
      alethzero/MainWin.cpp
  2. 9
      libethereum/BlockChain.cpp
  3. 3
      libethereum/BlockChain.h
  4. 46
      libethereum/Client.cpp
  5. 9
      libethereum/Client.h
  6. 44
      libqethereum/QEthereum.cpp
  7. 14
      libqethereum/QEthereum.h
  8. 4
      test/vm.cpp

1
alethzero/MainWin.cpp

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

9
libethereum/BlockChain.cpp

@ -322,6 +322,15 @@ eth::uint BlockChain::number(h256 _hash) const
return details(_hash).number;
}
h256 BlockChain::numberHash(unsigned _n) const
{
if (!_n)
return genesisHash();
h256 ret = currentHash();
for (; _n < details().number; ++_n, ret = details(ret).parent) {}
return ret;
}
BlockDetails const& BlockChain::details(h256 _h) const
{
BlockDetailsHash::const_iterator it;

3
libethereum/BlockChain.h

@ -105,6 +105,9 @@ public:
/// Get the hash of the genesis block.
h256 genesisHash() const { return m_genesisHash; }
/// Get the hash of a block of a given number.
h256 numberHash(unsigned _n) const;
std::vector<std::pair<Address, AddressState>> interestQueue() { std::vector<std::pair<Address, AddressState>> ret; swap(ret, m_interestQueue); return ret; }
void pushInterest(Address _a) { m_interest[_a]++; }
void popInterest(Address _a) { if (m_interest[_a] > 1) m_interest[_a]--; else if (m_interest[_a]) m_interest.erase(_a); }

46
libethereum/Client.cpp

@ -286,3 +286,49 @@ void Client::unlock()
{
m_lock.unlock();
}
unsigned Client::numberOf(int _n) const
{
if (_n > 0)
return _n;
else if (_n == GenesisBlock)
return 0;
else
return m_bc.details().number + 1 + _n;
}
State Client::asOf(int _h) const
{
if (_h == 0)
return m_postMine;
else if (_h == -1)
return m_preMine;
else
return State(m_stateDB, m_bc, m_bc.numberHash(numberOf(_h)));
}
u256 Client::balanceAt(Address _a, int _block) const
{
return asOf(_block).balance(_a);
}
u256 Client::countAt(Address _a, int _block) const
{
return asOf(_block).transactionsFrom(_a);
}
u256 Client::stateAt(Address _a, u256 _l, int _block) const
{
return asOf(_block).storage(_a, _l);
}
bytes Client::codeAt(Address _a, int _block) const
{
return asOf(_block).code(_a);
}
Transactions Client::transactions(TransactionFilter const& _f) const
{
Transactions ret;
return ret;
}

9
libethereum/Client.h

@ -98,6 +98,8 @@ private:
unsigned m_max;
};
static const int GenesisBlock = INT_MIN;
/**
* @brief Main API hub for interfacing with Ethereum.
*/
@ -216,8 +218,11 @@ public:
private:
void work();
/// Return the actual block number of the block with the given int-number (positive/zero is the same, -(1 << 31) is next to be mined, < 0 is negative age, thus -1 is most recently mined, 0 is genesis.
unsigned blockNumber(int _b) const;
/// Return the actual block number of the block with the given int-number (positive is the same, INT_MIN is genesis block, < 0 is negative age, thus -1 is most recently mined, 0 is pending.
unsigned numberOf(int _b) const;
State asOf(int _h) const;
State asOf(unsigned _h) const;
std::string m_clientVersion; ///< Our end-application client's name/version.
VersionChecker m_vc; ///< Dummy object to check & update the protocol version.

44
libqethereum/QEthereum.cpp

@ -339,9 +339,9 @@ QString QEthereum::storageAt(QString _a, QString _p) const
return toQJS(client()->postState().storage(toAddress(_a), toU256(_p)));
}
u256 QEthereum::balanceAt(Address _a) const
double QEthereum::txCountAt(QString _a) const
{
return client()->postState().balance(_a);
return (double)client()->postState().transactionsFrom(toAddress(_a));
}
bool QEthereum::isContractAt(QString _a) const
@ -349,11 +349,41 @@ bool QEthereum::isContractAt(QString _a) const
return client()->postState().addressHasCode(toAddress(_a));
}
u256 QEthereum::balanceAt(Address _a) const
{
return client()->postState().balance(_a);
}
double QEthereum::txCountAt(Address _a) const
{
return (double)client()->postState().transactionsFrom(_a);
}
bool QEthereum::isContractAt(Address _a) const
{
return client()->postState().addressHasCode(_a);
}
QString QEthereum::balanceAt(QString _a, int _block) const
{
return toQJS(client()->balanceAt(toAddress(_a), _block));
}
QString QEthereum::stateAt(QString _a, QString _p, int _block) const
{
return toQJS(client()->stateAt(toAddress(_a), toU256(_p), _block));
}
QString QEthereum::codeAt(QString _a, int _block) const
{
return ::fromBinary(client()->codeAt(toAddress(_a), _block));
}
double QEthereum::countAt(QString _a, int _block) const
{
return (double)(uint64_t)client()->countAt(toAddress(_a), _block);
}
bool QEthereum::isMining() const
{
return client()->isMining();
@ -380,16 +410,6 @@ void QEthereum::setListening(bool _l)
client()->stopNetwork();
}
double QEthereum::txCountAt(QString _a) const
{
return (double)client()->postState().transactionsFrom(toAddress(_a));
}
double QEthereum::txCountAt(Address _a) const
{
return (double)client()->postState().transactionsFrom(_a);
}
unsigned QEthereum::peerCount() const
{
return (unsigned)client()->peerCount();

14
libqethereum/QEthereum.h

@ -353,9 +353,14 @@ inline QString toDecimal(QString const& _s)
return QString::fromStdString(eth::toString(toU256(_s)));
}
inline QString fromBinary(eth::bytes const& _s)
{
return QString::fromStdString("0x" + eth::toHex(_s));
}
inline QString fromBinary(QString const& _s)
{
return QString::fromStdString("0x" + eth::toHex(asBytes(_s)));
return fromBinary(asBytes(_s));
}
class QEthereum: public QObject
@ -385,11 +390,18 @@ public:
Q_INVOKABLE QString fromBinary(QString _s) const { return ::fromBinary(_s); }
Q_INVOKABLE QString toDecimal(QString _s) const { return ::toDecimal(_s); }
// [OLD API] - Don't use this.
Q_INVOKABLE QString/*eth::u256*/ balanceAt(QString/*eth::Address*/ _a) const;
Q_INVOKABLE QString/*eth::u256*/ storageAt(QString/*eth::Address*/ _a, QString/*eth::u256*/ _p) const;
Q_INVOKABLE double txCountAt(QString/*eth::Address*/ _a) const;
Q_INVOKABLE bool isContractAt(QString/*eth::Address*/ _a) const;
// [NEW API] - Use this instead.
Q_INVOKABLE QString/*eth::u256*/ balanceAt(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*/ codeAt(QString/*eth::Address*/ _a, int _block) const;
Q_INVOKABLE QString doCreate(QString _secret, QString _amount, QString _init, QString _gas, QString _gasPrice);
Q_INVOKABLE void doTransact(QString _secret, QString _amount, QString _dest, QString _data, QString _gas, QString _gasPrice);

4
test/vm.cpp

@ -468,7 +468,7 @@ void doTests(json_spirit::mValue& v, bool _fillin)
BOOST_AUTO_TEST_CASE(vm_tests)
{
// Populate tests first:
try
/* try
{
cnote << "Populating VM tests...";
json_spirit::mValue v;
@ -482,7 +482,7 @@ BOOST_AUTO_TEST_CASE(vm_tests)
{
BOOST_ERROR("Failed VM Test with Exception: " << e.what());
}
*/
try
{
cnote << "Testing VM...";

Loading…
Cancel
Save