Fabcoin Core  0.16.2
P2P Digital Currency
Interface.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <libdevcore/Common.h>
25 #include <libdevcore/CommonIO.h>
26 #include <libdevcore/Guards.h>
27 #include <libdevcrypto/Common.h>
28 #include <libethcore/SealEngine.h>
29 #include "GasPricer.h"
30 #include "LogFilter.h"
31 #include "Transaction.h"
32 #include "BlockDetails.h"
33 
34 namespace dev
35 {
36 namespace eth
37 {
38 
39 struct SyncStatus;
40 
41 using TransactionHashes = h256s;
42 using UncleHashes = h256s;
43 
44 enum class Reaping
45 {
46  Automatic,
47  Manual
48 };
49 
50 enum class FudgeFactor
51 {
52  Strict,
53  Lenient
54 };
55 
57 {
60 };
61 
62 using GasEstimationCallback = std::function<void(GasEstimationProgress const&)>;
63 
67 class Interface
68 {
69 public:
71  Interface() {}
72 
74  virtual ~Interface() {}
75 
76  // [TRANSACTION API]
77 
80  virtual std::pair<h256, Address> submitTransaction(TransactionSkeleton const& _t, Secret const& _secret) = 0;
81 
83  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);
84 
87  Address submitTransaction(Secret const& _secret, u256 const& _endowment, bytes const& _init, u256 const& _gas = 1000000, u256 const& _gasPrice = DefaultGasPrice, u256 const& _nonce = Invalid256);
88 
90  virtual void flushTransactions() = 0;
91 
93  virtual ExecutionResult call(Address const& _from, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) = 0;
94  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); }
95  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); }
96  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); }
97 
100  virtual ExecutionResult create(Address const& _from, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) = 0;
101  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); }
102  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); }
103  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); }
104 
106  virtual ImportResult injectTransaction(bytes const& _rlp, IfDropped _id = IfDropped::Ignore) = 0;
107 
109  virtual ImportResult injectBlock(bytes const& _block) = 0;
110 
114  virtual std::pair<u256, ExecutionResult> estimateGas(Address const& _from, u256 _value, Address _dest, bytes const& _data, int64_t _maxGas, u256 _gasPrice, BlockNumber _blockNumber, GasEstimationCallback const& _callback = GasEstimationCallback()) = 0;
115 
116  // [STATE-QUERY API]
117 
118  int getDefault() const { return m_default; }
119  void setDefault(BlockNumber _block) { m_default = _block; }
120 
121  u256 balanceAt(Address _a) const { return balanceAt(_a, m_default); }
122  u256 countAt(Address _a) const { return countAt(_a, m_default); }
123  u256 stateAt(Address _a, u256 _l) const { return stateAt(_a, _l, m_default); }
124  bytes codeAt(Address _a) const { return codeAt(_a, m_default); }
125  h256 codeHashAt(Address _a) const { return codeHashAt(_a, m_default); }
126  std::map<h256, std::pair<u256, u256>> storageAt(Address _a) const { return storageAt(_a, m_default); }
127 
128  virtual u256 balanceAt(Address _a, BlockNumber _block) const = 0;
129  virtual u256 countAt(Address _a, BlockNumber _block) const = 0;
130  virtual u256 stateAt(Address _a, u256 _l, BlockNumber _block) const = 0;
131  virtual h256 stateRootAt(Address _a, BlockNumber _block) const = 0;
132  virtual bytes codeAt(Address _a, BlockNumber _block) const = 0;
133  virtual h256 codeHashAt(Address _a, BlockNumber _block) const = 0;
134  virtual std::map<h256, std::pair<u256, u256>> storageAt(Address _a, BlockNumber _block) const = 0;
135 
136  // [LOGS API]
137 
138  virtual LocalisedLogEntries logs(unsigned _watchId) const = 0;
139  virtual LocalisedLogEntries logs(LogFilter const& _filter) const = 0;
140 
142  virtual unsigned installWatch(LogFilter const& _filter, Reaping _r = Reaping::Automatic) = 0;
143  virtual unsigned installWatch(h256 _filterId, Reaping _r = Reaping::Automatic) = 0;
144  virtual bool uninstallWatch(unsigned _watchId) = 0;
145  LocalisedLogEntries peekWatchSafe(unsigned _watchId) const { try { return peekWatch(_watchId); } catch (...) { return LocalisedLogEntries(); } }
146  LocalisedLogEntries checkWatchSafe(unsigned _watchId) { try { return checkWatch(_watchId); } catch (...) { return LocalisedLogEntries(); } }
147  virtual LocalisedLogEntries peekWatch(unsigned _watchId) const = 0;
148  virtual LocalisedLogEntries checkWatch(unsigned _watchId) = 0;
149 
150  // [BLOCK QUERY API]
151 
152  virtual bool isKnownTransaction(h256 const& _transactionHash) const = 0;
153  virtual bool isKnownTransaction(h256 const& _blockHash, unsigned _i) const = 0;
154  virtual Transaction transaction(h256 _transactionHash) const = 0;
155  virtual LocalisedTransaction localisedTransaction(h256 const& _transactionHash) const = 0;
156  virtual TransactionReceipt transactionReceipt(h256 const& _transactionHash) const = 0;
157  virtual LocalisedTransactionReceipt localisedTransactionReceipt(h256 const& _transactionHash) const = 0;
158  virtual std::pair<h256, unsigned> transactionLocation(h256 const& _transactionHash) const = 0;
159  virtual h256 hashFromNumber(BlockNumber _number) const = 0;
160  virtual BlockNumber numberFromHash(h256 _blockHash) const = 0;
161  virtual int compareBlockHashes(h256 _h1, h256 _h2) const = 0;
162 
163  virtual bool isKnown(BlockNumber _block) const = 0;
164  virtual bool isKnown(h256 const& _hash) const = 0;
165  virtual BlockHeader blockInfo(h256 _hash) const = 0;
166  virtual BlockDetails blockDetails(h256 _hash) const = 0;
167  virtual Transaction transaction(h256 _blockHash, unsigned _i) const = 0;
168  virtual LocalisedTransaction localisedTransaction(h256 const& _blockHash, unsigned _i) const = 0;
169  virtual BlockHeader uncle(h256 _blockHash, unsigned _i) const = 0;
170  virtual UncleHashes uncleHashes(h256 _blockHash) const = 0;
171  virtual unsigned transactionCount(h256 _blockHash) const = 0;
172  virtual unsigned uncleCount(h256 _blockHash) const = 0;
173  virtual Transactions transactions(h256 _blockHash) const = 0;
174  virtual TransactionHashes transactionHashes(h256 _blockHash) const = 0;
175 
176  virtual BlockHeader pendingInfo() const { return BlockHeader(); }
177  virtual BlockDetails pendingDetails() const { return BlockDetails(); }
179  virtual EVMSchedule evmSchedule() const { return EVMSchedule(); }
180 
181  BlockHeader blockInfo(BlockNumber _block) const;
182  BlockDetails blockDetails(BlockNumber _block) const;
183  Transaction transaction(BlockNumber _block, unsigned _i) const { auto p = transactions(_block); return _i < p.size() ? p[_i] : Transaction(); }
184  unsigned transactionCount(BlockNumber _block) const { if (_block == PendingBlock) { auto p = pending(); return p.size(); } return transactionCount(hashFromNumber(_block)); }
185  Transactions transactions(BlockNumber _block) const { if (_block == PendingBlock) return pending(); return transactions(hashFromNumber(_block)); }
186  TransactionHashes transactionHashes(BlockNumber _block) const { if (_block == PendingBlock) return pendingHashes(); return transactionHashes(hashFromNumber(_block)); }
187  BlockHeader uncle(BlockNumber _block, unsigned _i) const { return uncle(hashFromNumber(_block), _i); }
188  UncleHashes uncleHashes(BlockNumber _block) const { return uncleHashes(hashFromNumber(_block)); }
189  unsigned uncleCount(BlockNumber _block) const { return uncleCount(hashFromNumber(_block)); }
190 
191  // [EXTRA API]:
192 
194  virtual unsigned number() const = 0;
195 
198  virtual Transactions pending() const = 0;
199  virtual h256s pendingHashes() const = 0;
200 
203  virtual Addresses addresses() const { return addresses(m_default); }
204  virtual Addresses addresses(BlockNumber _block) const = 0;
205 
207  virtual u256 gasLimitRemaining() const = 0;
208  // Get the gas bidding price
209  virtual u256 gasBidPrice() const = 0;
210 
212  virtual SyncStatus syncStatus() const = 0;
213 
214  // [SEALING API]:
215 
217  virtual void setAuthor(Address const& _us) = 0;
219  virtual Address author() const = 0;
220 
223  virtual void startSealing() = 0;
226  virtual void stopSealing() = 0;
228  virtual bool wouldSeal() const = 0;
229 
231  virtual bool isSyncing() const { return false; }
233  virtual bool isMajorSyncing() const { return false; }
234 
236  virtual u256 networkId() const { return 0; }
238  virtual void setNetworkId(u256 const&) {}
239 
241  virtual SealEngineFace* sealEngine() const { return nullptr; }
242 
243 protected:
244  int m_default = PendingBlock;
245 };
246 
247 class Watch;
248 
249 }
250 }
251 
252 namespace std { void swap(dev::eth::Watch& _a, dev::eth::Watch& _b); }
253 
254 namespace dev
255 {
256 namespace eth
257 {
258 
259 class Watch: public boost::noncopyable
260 {
261  friend void std::swap(Watch& _a, Watch& _b);
262 
263 public:
264  Watch() {}
265  Watch(Interface& _c, h256 _f): m_c(&_c), m_id(_c.installWatch(_f)) {}
266  Watch(Interface& _c, LogFilter const& _tf): m_c(&_c), m_id(_c.installWatch(_tf)) {}
267  ~Watch() { if (m_c) m_c->uninstallWatch(m_id); }
268 
269  LocalisedLogEntries check() { return m_c ? m_c->checkWatch(m_id) : LocalisedLogEntries(); }
270  LocalisedLogEntries peek() { return m_c ? m_c->peekWatch(m_id) : LocalisedLogEntries(); }
271  LocalisedLogEntries logs() const { return m_c->logs(m_id); }
272 
273 private:
274  Interface* m_c = nullptr;
275  unsigned m_id = 0;
276 };
277 
278 }
279 }
280 
281 namespace std
282 {
283 
284 inline void swap(dev::eth::Watch& _a, dev::eth::Watch& _b)
285 {
286  swap(_a.m_c, _b.m_c);
287  swap(_a.m_id, _b.m_id);
288 }
289 
290 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
ExecutionResult create(Secret const &_secret, u256 _value, bytes const &_data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff=FudgeFactor::Strict)
Definition: Interface.h:102
Watch(Interface &_c, LogFilter const &_tf)
Definition: Interface.h:266
h256s TransactionHashes
Definition: BlockChain.h:81
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
u256 countAt(Address _a) const
Definition: Interface.h:122
Encapsulation of a block header.
Definition: BlockHeader.h:95
virtual EVMSchedule evmSchedule() const
Definition: Interface.h:179
BlockHeader uncle(BlockNumber _block, unsigned _i) const
Definition: Interface.h:187
std::vector< Transaction > Transactions
Nice name for vector of Transaction.
Definition: Transaction.h:121
Transaction transaction(BlockNumber _block, unsigned _i) const
Definition: Interface.h:183
virtual BlockDetails pendingDetails() const
Definition: Interface.h:177
std::hash for asio::adress
Definition: Common.h:323
LocalisedLogEntries check()
Definition: Interface.h:269
virtual bool isSyncing() const
Are we updating the chain (syncing or importing a new block)?
Definition: Interface.h:231
LocalisedLogEntries checkWatchSafe(unsigned _watchId)
Definition: Interface.h:146
ExecutionResult create(Secret const &_secret, u256 _value, bytes const &_data, u256 _gas, u256 _gasPrice, FudgeFactor _ff=FudgeFactor::Strict)
Definition: Interface.h:103
Description of the result of executing a transaction.
Definition: Transaction.h:69
h256 codeHashAt(Address _a) const
Definition: Interface.h:125
unsigned BlockNumber
Definition: Common.h:72
std::vector< LocalisedLogEntry > LocalisedLogEntries
Definition: ExtVMFace.h:156
h256s UncleHashes
Definition: BlockChain.h:82
ImportResult
Definition: Common.h:97
void swap(dev::shh::Watch &_a, dev::shh::Watch &_b)
Definition: Interface.h:119
virtual void setNetworkId(u256 const &)
Sets the network id.
Definition: Interface.h:238
UncleHashes uncleHashes(BlockNumber _block) const
Definition: Interface.h:188
virtual Addresses addresses() const
Get a list of all active addresses.
Definition: Interface.h:203
bytes codeAt(Address _a) const
Definition: Interface.h:124
ExecutionResult create(Address const &_from, u256 _value, bytes const &_data=bytes(), u256 _gas=1000000, u256 _gasPrice=DefaultGasPrice, FudgeFactor _ff=FudgeFactor::Strict)
Definition: Interface.h:101
IfDropped
Import transaction policy.
Definition: Common.h:214
virtual bool isMajorSyncing() const
Are we syncing the chain?
Definition: Interface.h:233
ExecutionResult call(Secret const &_secret, u256 _value, Address _dest, bytes const &_data, u256 _gas, u256 _gasPrice, FudgeFactor _ff=FudgeFactor::Strict)
Definition: Interface.h:96
virtual ~Interface()
Destructor.
Definition: Interface.h:74
u256 balanceAt(Address _a) const
Definition: Interface.h:121
int getDefault() const
Definition: Interface.h:118
std::vector< byte > bytes
Definition: Common.h:75
const u256 Invalid256
Definition: Common.cpp:38
Don&#39;t import transaction that was previously dropped.
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
virtual u256 networkId() const
Gets the network id.
Definition: Interface.h:236
ExecutionResult call(Address const &_from, u256 _value, Address _dest, bytes const &_data=bytes(), u256 _gas=1000000, u256 _gasPrice=DefaultGasPrice, FudgeFactor _ff=FudgeFactor::Strict)
Definition: Interface.h:94
ExecutionResult call(Secret const &_secret, u256 _value, Address _dest, bytes const &_data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff=FudgeFactor::Strict)
Definition: Interface.h:95
Transactions transactions(BlockNumber _block) const
Definition: Interface.h:185
Address toAddress(std::string const &_s)
Convert the given string into an address.
Definition: Common.cpp:56
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Interface * m_c
Definition: Interface.h:274
h160s Addresses
A vector of Ethereum addresses.
Definition: Common.h:68
unsigned uncleCount(BlockNumber _block) const
Definition: Interface.h:189
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
LocalisedLogEntries peekWatchSafe(unsigned _watchId) const
Definition: Interface.h:145
LocalisedLogEntries peek()
Definition: Interface.h:270
std::function< void(GasEstimationProgress const &)> GasEstimationCallback
Definition: Interface.h:62
u256 stateAt(Address _a, u256 _l) const
Definition: Interface.h:123
unsigned transactionCount(BlockNumber _block) const
Definition: Interface.h:184
Interface()
Constructor.
Definition: Interface.h:71
virtual SealEngineFace * sealEngine() const
Get the seal engine.
Definition: Interface.h:241
Watch(Interface &_c, h256 _f)
Definition: Interface.h:265
std::map< h256, std::pair< u256, u256 > > storageAt(Address _a) const
Definition: Interface.h:126
TransactionHashes transactionHashes(BlockNumber _block) const
Definition: Interface.h:186
std::vector< h256 > h256s
Definition: FixedHash.h:345
void setDefault(BlockNumber _block)
Definition: Interface.h:119
LocalisedLogEntries logs() const
Definition: Interface.h:271
unsigned m_id
Definition: Interface.h:275
virtual BlockHeader pendingInfo() const
Definition: Interface.h:176