Interface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 Interface.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <libdevcore/Common.h>
  20. #include <libdevcore/CommonIO.h>
  21. #include <libdevcore/Guards.h>
  22. #include <libdevcrypto/Common.h>
  23. #include <libethcore/SealEngine.h>
  24. #include "GasPricer.h"
  25. #include "LogFilter.h"
  26. #include "Transaction.h"
  27. #include "BlockDetails.h"
  28. namespace dev
  29. {
  30. namespace eth
  31. {
  32. struct SyncStatus;
  33. using TransactionHashes = h256s;
  34. using UncleHashes = h256s;
  35. enum class Reaping
  36. {
  37. Automatic,
  38. Manual
  39. };
  40. enum class FudgeFactor
  41. {
  42. Strict,
  43. Lenient
  44. };
  45. struct GasEstimationProgress
  46. {
  47. u256 lowerBound;
  48. u256 upperBound;
  49. };
  50. using GasEstimationCallback = std::function<void(GasEstimationProgress const&)>;
  51. extern const u256 c_maxGasEstimate;
  52. /**
  53. * @brief Main API hub for interfacing with Ethereum.
  54. */
  55. class Interface
  56. {
  57. public:
  58. /// Constructor.
  59. Interface() {}
  60. /// Destructor.
  61. virtual ~Interface() {}
  62. // [TRANSACTION API]
  63. /// Submits a new transaction.
  64. /// @returns the transaction's hash.
  65. virtual std::pair<h256, Address> submitTransaction(TransactionSkeleton const& _t, Secret const& _secret) = 0;
  66. /// Submits the given message-call transaction.
  67. void submitTransaction(Secret const& _secret, u256 const& _value, Address const& _dest, bytes const& _data = bytes(), u256 const& _gas = 1000000, u256 const& _gasPrice = DefaultGasPrice, u256 const& _nonce = Invalid256);
  68. /// Submits a new contract-creation transaction.
  69. /// @returns the new contract's address (assuming it all goes through).
  70. Address submitTransaction(Secret const& _secret, u256 const& _endowment, bytes const& _init, u256 const& _gas = 1000000, u256 const& _gasPrice = DefaultGasPrice, u256 const& _nonce = Invalid256);
  71. /// Blocks until all pending transactions have been processed.
  72. virtual void flushTransactions() = 0;
  73. /// Makes the given call. Nothing is recorded into the state.
  74. virtual ExecutionResult call(Address const& _from, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) = 0;
  75. ExecutionResult call(Address const& _from, u256 _value, Address _dest, bytes const& _data = bytes(), u256 _gas = 1000000, u256 _gasPrice = DefaultGasPrice, FudgeFactor _ff = FudgeFactor::Strict) { return call(_from, _value, _dest, _data, _gas, _gasPrice, m_default, _ff); }
  76. ExecutionResult call(Secret const& _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) { return call(toAddress(_secret), _value, _dest, _data, _gas, _gasPrice, _blockNumber, _ff); }
  77. ExecutionResult call(Secret const& _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, FudgeFactor _ff = FudgeFactor::Strict) { return call(toAddress(_secret), _value, _dest, _data, _gas, _gasPrice, _ff); }
  78. /// Does the given creation. Nothing is recorded into the state.
  79. /// @returns the pair of the Address of the created contract together with its code.
  80. virtual ExecutionResult create(Address const& _from, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) = 0;
  81. ExecutionResult create(Address const& _from, u256 _value, bytes const& _data = bytes(), u256 _gas = 1000000, u256 _gasPrice = DefaultGasPrice, FudgeFactor _ff = FudgeFactor::Strict) { return create(_from, _value, _data, _gas, _gasPrice, m_default, _ff); }
  82. ExecutionResult create(Secret const& _secret, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) { return create(toAddress(_secret), _value, _data, _gas, _gasPrice, _blockNumber, _ff); }
  83. ExecutionResult create(Secret const& _secret, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, FudgeFactor _ff = FudgeFactor::Strict) { return create(toAddress(_secret), _value, _data, _gas, _gasPrice, _ff); }
  84. /// Injects the RLP-encoded transaction given by the _rlp into the transaction queue directly.
  85. virtual ImportResult injectTransaction(bytes const& _rlp, IfDropped _id = IfDropped::Ignore) = 0;
  86. /// Injects the RLP-encoded block given by the _rlp into the block queue directly.
  87. virtual ImportResult injectBlock(bytes const& _block) = 0;
  88. /// Estimate gas usage for call/create.
  89. /// @param _maxGas An upper bound value for estimation, if not provided default value of c_maxGasEstimate will be used.
  90. /// @param _callback Optional callback function for progress reporting
  91. 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 = GasEstimationCallback()) = 0;
  92. // [STATE-QUERY API]
  93. int getDefault() const { return m_default; }
  94. void setDefault(BlockNumber _block) { m_default = _block; }
  95. u256 balanceAt(Address _a) const { return balanceAt(_a, m_default); }
  96. u256 countAt(Address _a) const { return countAt(_a, m_default); }
  97. u256 stateAt(Address _a, u256 _l) const { return stateAt(_a, _l, m_default); }
  98. bytes codeAt(Address _a) const { return codeAt(_a, m_default); }
  99. h256 codeHashAt(Address _a) const { return codeHashAt(_a, m_default); }
  100. std::map<u256, u256> storageAt(Address _a) const { return storageAt(_a, m_default); }
  101. virtual u256 balanceAt(Address _a, BlockNumber _block) const = 0;
  102. virtual u256 countAt(Address _a, BlockNumber _block) const = 0;
  103. virtual u256 stateAt(Address _a, u256 _l, BlockNumber _block) const = 0;
  104. virtual h256 stateRootAt(Address _a, BlockNumber _block) const = 0;
  105. virtual bytes codeAt(Address _a, BlockNumber _block) const = 0;
  106. virtual h256 codeHashAt(Address _a, BlockNumber _block) const = 0;
  107. virtual std::map<u256, u256> storageAt(Address _a, BlockNumber _block) const = 0;
  108. // [LOGS API]
  109. virtual LocalisedLogEntries logs(unsigned _watchId) const = 0;
  110. virtual LocalisedLogEntries logs(LogFilter const& _filter) const = 0;
  111. /// Install, uninstall and query watches.
  112. virtual unsigned installWatch(LogFilter const& _filter, Reaping _r = Reaping::Automatic) = 0;
  113. virtual unsigned installWatch(h256 _filterId, Reaping _r = Reaping::Automatic) = 0;
  114. virtual bool uninstallWatch(unsigned _watchId) = 0;
  115. LocalisedLogEntries peekWatchSafe(unsigned _watchId) const { try { return peekWatch(_watchId); } catch (...) { return LocalisedLogEntries(); } }
  116. LocalisedLogEntries checkWatchSafe(unsigned _watchId) { try { return checkWatch(_watchId); } catch (...) { return LocalisedLogEntries(); } }
  117. virtual LocalisedLogEntries peekWatch(unsigned _watchId) const = 0;
  118. virtual LocalisedLogEntries checkWatch(unsigned _watchId) = 0;
  119. // [BLOCK QUERY API]
  120. virtual bool isKnownTransaction(h256 const& _transactionHash) const = 0;
  121. virtual bool isKnownTransaction(h256 const& _blockHash, unsigned _i) const = 0;
  122. virtual Transaction transaction(h256 _transactionHash) const = 0;
  123. virtual LocalisedTransaction localisedTransaction(h256 const& _transactionHash) const = 0;
  124. virtual TransactionReceipt transactionReceipt(h256 const& _transactionHash) const = 0;
  125. virtual LocalisedTransactionReceipt localisedTransactionReceipt(h256 const& _transactionHash) const = 0;
  126. virtual std::pair<h256, unsigned> transactionLocation(h256 const& _transactionHash) const = 0;
  127. virtual h256 hashFromNumber(BlockNumber _number) const = 0;
  128. virtual BlockNumber numberFromHash(h256 _blockHash) const = 0;
  129. virtual int compareBlockHashes(h256 _h1, h256 _h2) const = 0;
  130. virtual bool isKnown(BlockNumber _block) const = 0;
  131. virtual bool isKnown(h256 const& _hash) const = 0;
  132. virtual BlockHeader blockInfo(h256 _hash) const = 0;
  133. virtual BlockDetails blockDetails(h256 _hash) const = 0;
  134. virtual Transaction transaction(h256 _blockHash, unsigned _i) const = 0;
  135. virtual LocalisedTransaction localisedTransaction(h256 const& _blockHash, unsigned _i) const = 0;
  136. virtual BlockHeader uncle(h256 _blockHash, unsigned _i) const = 0;
  137. virtual UncleHashes uncleHashes(h256 _blockHash) const = 0;
  138. virtual unsigned transactionCount(h256 _blockHash) const = 0;
  139. virtual unsigned uncleCount(h256 _blockHash) const = 0;
  140. virtual Transactions transactions(h256 _blockHash) const = 0;
  141. virtual TransactionHashes transactionHashes(h256 _blockHash) const = 0;
  142. virtual BlockHeader pendingInfo() const { return BlockHeader(); }
  143. virtual BlockDetails pendingDetails() const { return BlockDetails(); }
  144. /// @returns the EVMSchedule in the context of the pending block.
  145. virtual EVMSchedule evmSchedule() const { return EVMSchedule(); }
  146. BlockHeader blockInfo(BlockNumber _block) const;
  147. BlockDetails blockDetails(BlockNumber _block) const;
  148. Transaction transaction(BlockNumber _block, unsigned _i) const { auto p = transactions(_block); return _i < p.size() ? p[_i] : Transaction(); }
  149. unsigned transactionCount(BlockNumber _block) const { if (_block == PendingBlock) { auto p = pending(); return p.size(); } return transactionCount(hashFromNumber(_block)); }
  150. Transactions transactions(BlockNumber _block) const { if (_block == PendingBlock) return pending(); return transactions(hashFromNumber(_block)); }
  151. TransactionHashes transactionHashes(BlockNumber _block) const { if (_block == PendingBlock) return pendingHashes(); return transactionHashes(hashFromNumber(_block)); }
  152. BlockHeader uncle(BlockNumber _block, unsigned _i) const { return uncle(hashFromNumber(_block), _i); }
  153. UncleHashes uncleHashes(BlockNumber _block) const { return uncleHashes(hashFromNumber(_block)); }
  154. unsigned uncleCount(BlockNumber _block) const { return uncleCount(hashFromNumber(_block)); }
  155. // [EXTRA API]:
  156. /// @returns The height of the chain.
  157. virtual unsigned number() const = 0;
  158. /// Get a map containing each of the pending transactions.
  159. /// @TODO: Remove in favour of transactions().
  160. virtual Transactions pending() const = 0;
  161. virtual h256s pendingHashes() const = 0;
  162. /// Get a list of all active addresses.
  163. /// NOTE: This only works when compiled with ETH_FATDB; otherwise will throw InterfaceNotSupported.
  164. virtual Addresses addresses() const { return addresses(m_default); }
  165. virtual Addresses addresses(BlockNumber _block) const = 0;
  166. /// Get the remaining gas limit in this block.
  167. virtual u256 gasLimitRemaining() const = 0;
  168. // Get the gas bidding price
  169. virtual u256 gasBidPrice() const = 0;
  170. /// Get some information on the block queue.
  171. virtual SyncStatus syncStatus() const = 0;
  172. // [SEALING API]:
  173. /// Set the block author address.
  174. virtual void setAuthor(Address const& _us) = 0;
  175. /// Get the block author address.
  176. virtual Address author() const = 0;
  177. /// Start sealing.
  178. /// NOT thread-safe - call it & stopSealing only from a single thread
  179. virtual void startSealing() = 0;
  180. /// Stop sealing.
  181. /// NOT thread-safe
  182. virtual void stopSealing() = 0;
  183. /// Would we like to be sealing now?
  184. virtual bool wouldSeal() const = 0;
  185. /// Are we updating the chain (syncing or importing a new block)?
  186. virtual bool isSyncing() const { return false; }
  187. /// Are we syncing the chain?
  188. virtual bool isMajorSyncing() const { return false; }
  189. /// Gets the network id.
  190. virtual u256 networkId() const { return 0; }
  191. /// Sets the network id.
  192. virtual void setNetworkId(u256 const&) {}
  193. /// Get the seal engine.
  194. virtual SealEngineFace* sealEngine() const { return nullptr; }
  195. protected:
  196. int m_default = PendingBlock;
  197. };
  198. class Watch;
  199. }
  200. }
  201. namespace std { void swap(dev::eth::Watch& _a, dev::eth::Watch& _b); }
  202. namespace dev
  203. {
  204. namespace eth
  205. {
  206. class Watch: public boost::noncopyable
  207. {
  208. friend void std::swap(Watch& _a, Watch& _b);
  209. public:
  210. Watch() {}
  211. Watch(Interface& _c, h256 _f): m_c(&_c), m_id(_c.installWatch(_f)) {}
  212. Watch(Interface& _c, LogFilter const& _tf): m_c(&_c), m_id(_c.installWatch(_tf)) {}
  213. ~Watch() { if (m_c) m_c->uninstallWatch(m_id); }
  214. LocalisedLogEntries check() { return m_c ? m_c->checkWatch(m_id) : LocalisedLogEntries(); }
  215. LocalisedLogEntries peek() { return m_c ? m_c->peekWatch(m_id) : LocalisedLogEntries(); }
  216. LocalisedLogEntries logs() const { return m_c->logs(m_id); }
  217. private:
  218. Interface* m_c = nullptr;
  219. unsigned m_id = 0;
  220. };
  221. }
  222. }
  223. namespace std
  224. {
  225. inline void swap(dev::eth::Watch& _a, dev::eth::Watch& _b)
  226. {
  227. swap(_a.m_c, _b.m_c);
  228. swap(_a.m_id, _b.m_id);
  229. }
  230. }