Browse Source

State reworked slightly.

Additional experimentation for State to demo how easily it is to create
states.
cl-refactor
Gav Wood 10 years ago
parent
commit
a068543f21
  1. 80
      exp/main.cpp
  2. 16
      libethereum/State.h

80
exp/main.cpp

@ -28,6 +28,7 @@
#include <libp2p/All.h> #include <libp2p/All.h>
#include <libdevcore/RangeMask.h> #include <libdevcore/RangeMask.h>
#include <libethereum/DownloadMan.h> #include <libethereum/DownloadMan.h>
#include <libethereum/All.h>
#include <libwhisper/WhisperPeer.h> #include <libwhisper/WhisperPeer.h>
#include <libwhisper/WhisperHost.h> #include <libwhisper/WhisperHost.h>
using namespace std; using namespace std;
@ -36,7 +37,7 @@ using namespace dev::eth;
using namespace dev::p2p; using namespace dev::p2p;
using namespace dev::shh; using namespace dev::shh;
#if 1 #if 0
int main() int main()
{ {
DownloadMan man; DownloadMan man;
@ -72,72 +73,17 @@ int main()
cnote << i;*/ cnote << i;*/
return 0; return 0;
} }
#endif #else
int main()
/*int other(bool& o_started)
{ {
setThreadName("other"); KeyPair u = KeyPair::create();
KeyPair cb = KeyPair::create();
short listenPort = 30300; OverlayDB db;
State s(cb.address(), db);
Host ph("Test", NetworkPreferences(listenPort, "", false, true)); cnote << s.rootHash();
auto wh = ph.registerCapability(new WhisperHost()); s.addBalance(u.address(), 1000 * ether);
s.commit();
ph.start(); cnote << s.rootHash();
o_started = true;
/// Only interested in odd packets
auto w = wh->installWatch(BuildTopicMask()("odd"));
unsigned last = 0;
unsigned total = 0;
for (int i = 0; i < 100 && last < 81; ++i)
{
for (auto i: wh->checkWatch(w))
{
Message msg = wh->envelope(i).open();
last = RLP(msg.payload()).toInt<unsigned>();
cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt<unsigned>();
total += last;
}
this_thread::sleep_for(chrono::milliseconds(50));
}
return total;
} }
#endif
int main(int, char**)
{
g_logVerbosity = 0;
bool started = false;
unsigned result;
std::thread listener([&](){ return (result = other(started)); });
while (!started)
this_thread::sleep_for(chrono::milliseconds(50));
short listenPort = 30303;
string remoteHost = "127.0.0.1";
short remotePort = 30300;
Host ph("Test", NetworkPreferences(listenPort, "", false, true));
auto wh = ph.registerCapability(new WhisperHost());
ph.start();
if (!remoteHost.empty())
ph.connect(remoteHost, remotePort);
KeyPair us = KeyPair::create();
for (int i = 0; i < 10; ++i)
{
wh->post(us.sec(), RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even"));
this_thread::sleep_for(chrono::milliseconds(250));
}
listener.join();
assert(result == 1 + 9 + 25 + 49 + 81);
return 0;
}*/

16
libethereum/State.h

@ -244,6 +244,12 @@ public:
/// the block since all state changes are ultimately reversed. /// the block since all state changes are ultimately reversed.
void cleanup(bool _fullCommit); void cleanup(bool _fullCommit);
/// Commit all changes waiting in the address cache to the DB.
void commit();
/// Sets m_currentBlock to a clean state, (i.e. no change from m_previousBlock).
void resetCurrent();
private: private:
/// Undo the changes to the state for committing to mine. /// Undo the changes to the state for committing to mine.
void uncommitToMine(); void uncommitToMine();
@ -257,25 +263,19 @@ private:
/// Retrieve all information about a given address into a cache. /// Retrieve all information about a given address into a cache.
void ensureCached(std::map<Address, Account>& _cache, Address _a, bool _requireCode, bool _forceCreate) const; void ensureCached(std::map<Address, Account>& _cache, Address _a, bool _requireCode, bool _forceCreate) const;
/// Commit all changes waiting in the address cache to the DB.
void commit();
/// Execute the given block, assuming it corresponds to m_currentBlock. If _bc is passed, it will be used to check the uncles. /// Execute the given block, assuming it corresponds to m_currentBlock. If _bc is passed, it will be used to check the uncles.
/// Throws on failure. /// Throws on failure.
u256 enact(bytesConstRef _block, BlockChain const* _bc = nullptr, bool _checkNonce = true); u256 enact(bytesConstRef _block, BlockChain const* _bc = nullptr, bool _checkNonce = true);
/// Sets m_currentBlock to a clean state, (i.e. no change from m_previousBlock).
void resetCurrent();
/// Finalise the block, applying the earned rewards. /// Finalise the block, applying the earned rewards.
void applyRewards(Addresses const& _uncleAddresses); void applyRewards(Addresses const& _uncleAddresses);
void refreshManifest(RLPStream* _txs = nullptr);
/// @returns gas used by transactions thus far executed. /// @returns gas used by transactions thus far executed.
u256 gasUsed() const { return m_receipts.size() ? m_receipts.back().gasUsed() : 0; } u256 gasUsed() const { return m_receipts.size() ? m_receipts.back().gasUsed() : 0; }
/// Debugging only. Good for checking the Trie is in shape.
bool isTrieGood(bool _enforceRefs, bool _requireNoLeftOvers) const; bool isTrieGood(bool _enforceRefs, bool _requireNoLeftOvers) const;
/// Debugging only. Good for checking the Trie is in shape.
void paranoia(std::string const& _when, bool _enforceRefs = false) const; void paranoia(std::string const& _when, bool _enforceRefs = false) const;
OverlayDB m_db; ///< Our overlay for the state tree. OverlayDB m_db; ///< Our overlay for the state tree.

Loading…
Cancel
Save