ClientBase.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file ClientBase.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @author Marek Kotewicz <marek@ethdev.com>
  17. * @date 2015
  18. */
  19. #pragma once
  20. #include <chrono>
  21. #include "Interface.h"
  22. #include "LogFilter.h"
  23. #include "TransactionQueue.h"
  24. #include "Block.h"
  25. #include "CommonNet.h"
  26. namespace dev
  27. {
  28. namespace eth
  29. {
  30. struct InstalledFilter
  31. {
  32. InstalledFilter(LogFilter const& _f): filter(_f) {}
  33. LogFilter filter;
  34. unsigned refCount = 1;
  35. LocalisedLogEntries changes;
  36. };
  37. static const h256 PendingChangedFilter = u256(0);
  38. static const h256 ChainChangedFilter = u256(1);
  39. static const LogEntry SpecialLogEntry = LogEntry(Address(), h256s(), bytes());
  40. static const LocalisedLogEntry InitialChange(SpecialLogEntry);
  41. struct ClientWatch
  42. {
  43. ClientWatch(): lastPoll(std::chrono::system_clock::now()) {}
  44. explicit ClientWatch(h256 _id, Reaping _r): id(_id), lastPoll(_r == Reaping::Automatic ? std::chrono::system_clock::now() : std::chrono::system_clock::time_point::max()) {}
  45. h256 id;
  46. #if INITIAL_STATE_AS_CHANGES
  47. LocalisedLogEntries changes = LocalisedLogEntries{ InitialChange };
  48. #else
  49. LocalisedLogEntries changes;
  50. #endif
  51. mutable std::chrono::system_clock::time_point lastPoll = std::chrono::system_clock::now();
  52. };
  53. struct WatchChannel: public LogChannel { static const char* name(); static const int verbosity = 7; };
  54. #define cwatch LogOutputStream<WatchChannel, true>()
  55. struct WorkInChannel: public LogChannel { static const char* name(); static const int verbosity = 16; };
  56. struct WorkOutChannel: public LogChannel { static const char* name(); static const int verbosity = 16; };
  57. struct WorkChannel: public LogChannel { static const char* name(); static const int verbosity = 21; };
  58. #define cwork LogOutputStream<WorkChannel, true>()
  59. #define cworkin LogOutputStream<WorkInChannel, true>()
  60. #define cworkout LogOutputStream<WorkOutChannel, true>()
  61. class ClientBase: public Interface
  62. {
  63. public:
  64. ClientBase(TransactionQueue::Limits const& _l = TransactionQueue::Limits{1024, 1024}): m_tq(_l) {}
  65. virtual ~ClientBase() {}
  66. /// Submits the given transaction.
  67. /// @returns the new transaction's hash.
  68. virtual std::pair<h256, Address> submitTransaction(TransactionSkeleton const& _t, Secret const& _secret) override;
  69. using Interface::submitTransaction;
  70. /// Makes the given call. Nothing is recorded into the state.
  71. virtual ExecutionResult call(Address const& _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) override;
  72. using Interface::call;
  73. /// Makes the given create. Nothing is recorded into the state.
  74. virtual ExecutionResult create(Address const& _secret, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) override;
  75. /// Estimate gas usage for call/create.
  76. /// @param _maxGas An upper bound value for estimation, if not provided default value of c_maxGasEstimate will be used.
  77. /// @param _callback Optional callback function for progress reporting
  78. virtual std::pair<u256, ExecutionResult> estimateGas(Address const& _from, u256 _value, Address _dest, bytes const& _data, u256 _maxGas, u256 _gasPrice, BlockNumber _blockNumber, GasEstimationCallback const& _callback) override;
  79. using Interface::create;
  80. using Interface::balanceAt;
  81. using Interface::countAt;
  82. using Interface::stateAt;
  83. using Interface::codeAt;
  84. using Interface::codeHashAt;
  85. using Interface::storageAt;
  86. virtual u256 balanceAt(Address _a, BlockNumber _block) const override;
  87. virtual u256 countAt(Address _a, BlockNumber _block) const override;
  88. virtual u256 stateAt(Address _a, u256 _l, BlockNumber _block) const override;
  89. virtual h256 stateRootAt(Address _a, BlockNumber _block) const override;
  90. virtual bytes codeAt(Address _a, BlockNumber _block) const override;
  91. virtual h256 codeHashAt(Address _a, BlockNumber _block) const override;
  92. virtual std::map<u256, u256> storageAt(Address _a, BlockNumber _block) const override;
  93. virtual LocalisedLogEntries logs(unsigned _watchId) const override;
  94. virtual LocalisedLogEntries logs(LogFilter const& _filter) const override;
  95. virtual void prependLogsFromBlock(LogFilter const& _filter, h256 const& _blockHash, BlockPolarity _polarity, LocalisedLogEntries& io_logs) const;
  96. /// Install, uninstall and query watches.
  97. virtual unsigned installWatch(LogFilter const& _filter, Reaping _r = Reaping::Automatic) override;
  98. virtual unsigned installWatch(h256 _filterId, Reaping _r = Reaping::Automatic) override;
  99. virtual bool uninstallWatch(unsigned _watchId) override;
  100. virtual LocalisedLogEntries peekWatch(unsigned _watchId) const override;
  101. virtual LocalisedLogEntries checkWatch(unsigned _watchId) override;
  102. virtual h256 hashFromNumber(BlockNumber _number) const override;
  103. virtual BlockNumber numberFromHash(h256 _blockHash) const override;
  104. virtual int compareBlockHashes(h256 _h1, h256 _h2) const override;
  105. virtual BlockHeader blockInfo(h256 _hash) const override;
  106. virtual BlockDetails blockDetails(h256 _hash) const override;
  107. virtual Transaction transaction(h256 _transactionHash) const override;
  108. virtual LocalisedTransaction localisedTransaction(h256 const& _transactionHash) const override;
  109. virtual Transaction transaction(h256 _blockHash, unsigned _i) const override;
  110. virtual LocalisedTransaction localisedTransaction(h256 const& _blockHash, unsigned _i) const override;
  111. virtual TransactionReceipt transactionReceipt(h256 const& _transactionHash) const override;
  112. virtual LocalisedTransactionReceipt localisedTransactionReceipt(h256 const& _transactionHash) const override;
  113. virtual std::pair<h256, unsigned> transactionLocation(h256 const& _transactionHash) const override;
  114. virtual Transactions transactions(h256 _blockHash) const override;
  115. virtual TransactionHashes transactionHashes(h256 _blockHash) const override;
  116. virtual BlockHeader uncle(h256 _blockHash, unsigned _i) const override;
  117. virtual UncleHashes uncleHashes(h256 _blockHash) const override;
  118. virtual unsigned transactionCount(h256 _blockHash) const override;
  119. virtual unsigned uncleCount(h256 _blockHash) const override;
  120. virtual unsigned number() const override;
  121. virtual Transactions pending() const override;
  122. virtual h256s pendingHashes() const override;
  123. virtual BlockHeader pendingInfo() const override;
  124. virtual BlockDetails pendingDetails() const override;
  125. virtual EVMSchedule evmSchedule() const override { return sealEngine()->evmSchedule(EnvInfo(pendingInfo())); }
  126. virtual ImportResult injectTransaction(bytes const& _rlp, IfDropped _id = IfDropped::Ignore) override { prepareForTransaction(); return m_tq.import(_rlp, _id); }
  127. virtual ImportResult injectBlock(bytes const& _block) override;
  128. using Interface::addresses;
  129. virtual Addresses addresses(BlockNumber _block) const override;
  130. virtual u256 gasLimitRemaining() const override;
  131. virtual u256 gasBidPrice() const override { return DefaultGasPrice; }
  132. /// Get the block author
  133. virtual Address author() const override;
  134. virtual bool isKnown(h256 const& _hash) const override;
  135. virtual bool isKnown(BlockNumber _block) const override;
  136. virtual bool isKnownTransaction(h256 const& _transactionHash) const override;
  137. virtual bool isKnownTransaction(h256 const& _blockHash, unsigned _i) const override;
  138. virtual void startSealing() override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::startSealing")); }
  139. virtual void stopSealing() override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::stopSealing")); }
  140. virtual bool wouldSeal() const override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::wouldSeal")); }
  141. virtual SyncStatus syncStatus() const override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::syncStatus")); }
  142. Block block(BlockNumber _h) const;
  143. protected:
  144. /// The interface that must be implemented in any class deriving this.
  145. /// {
  146. virtual BlockChain& bc() = 0;
  147. virtual BlockChain const& bc() const = 0;
  148. virtual Block block(h256 const& _h) const = 0;
  149. virtual Block preSeal() const = 0;
  150. virtual Block postSeal() const = 0;
  151. virtual void prepareForTransaction() = 0;
  152. /// }
  153. TransactionQueue m_tq; ///< Maintains a list of incoming transactions not yet in a block on the blockchain.
  154. // filters
  155. mutable Mutex x_filtersWatches; ///< Our lock.
  156. std::unordered_map<h256, InstalledFilter> m_filters; ///< The dictionary of filters that are active.
  157. std::unordered_map<h256, h256s> m_specialFilters = std::unordered_map<h256, std::vector<h256>>{{PendingChangedFilter, {}}, {ChainChangedFilter, {}}};
  158. ///< The dictionary of special filters and their additional data
  159. std::map<unsigned, ClientWatch> m_watches; ///< Each and every watch - these reference a filter.
  160. };
  161. }}