Fabcoin Core  0.16.2
P2P Digital Currency
ClientBase.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  */
23 #pragma once
24 
25 #include <chrono>
26 #include "Interface.h"
27 #include "LogFilter.h"
28 #include "TransactionQueue.h"
29 #include "Block.h"
30 #include "CommonNet.h"
31 
32 namespace dev
33 {
34 
35 namespace eth
36 {
37 
39 {
40  InstalledFilter(LogFilter const& _f): filter(_f) {}
41 
43  unsigned refCount = 1;
45 };
46 
47 static const h256 PendingChangedFilter = u256(0);
48 static const h256 ChainChangedFilter = u256(1);
49 
50 static const LogEntry SpecialLogEntry = LogEntry(Address(), h256s(), bytes());
51 static const LocalisedLogEntry InitialChange(SpecialLogEntry);
52 
54 {
55  ClientWatch(): lastPoll(std::chrono::system_clock::now()) {}
56  explicit ClientWatch(h256 _id, Reaping _r): id(_id), lastPoll(_r == Reaping::Automatic ? std::chrono::system_clock::now() : std::chrono::system_clock::time_point::max()) {}
57 
59 #if INITIAL_STATE_AS_CHANGES
61 #else
63 #endif
64  mutable std::chrono::system_clock::time_point lastPoll = std::chrono::system_clock::now();
65 };
66 
67 struct WatchChannel: public LogChannel { static const char* name(); static const int verbosity = 7; };
68 #define cwatch LogOutputStream<WatchChannel, true>()
69 struct WorkInChannel: public LogChannel { static const char* name(); static const int verbosity = 16; };
70 struct WorkOutChannel: public LogChannel { static const char* name(); static const int verbosity = 16; };
71 struct WorkChannel: public LogChannel { static const char* name(); static const int verbosity = 21; };
72 #define cwork LogOutputStream<WorkChannel, true>()
73 #define cworkin LogOutputStream<WorkInChannel, true>()
74 #define cworkout LogOutputStream<WorkOutChannel, true>()
75 
76 class ClientBase: public Interface
77 {
78 public:
79  ClientBase(TransactionQueue::Limits const& _l = TransactionQueue::Limits{1024, 1024}): m_tq(_l) {}
80  virtual ~ClientBase() {}
81 
84  virtual std::pair<h256, Address> submitTransaction(TransactionSkeleton const& _t, Secret const& _secret) override;
86 
88  virtual ExecutionResult call(Address const& _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) override;
89  using Interface::call;
90 
92  virtual ExecutionResult create(Address const& _secret, u256 _value, bytes const& _data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff = FudgeFactor::Strict) override;
93 
97  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) override;
98 
99  using Interface::create;
100 
101  using Interface::balanceAt;
102  using Interface::countAt;
103  using Interface::stateAt;
104  using Interface::codeAt;
105  using Interface::codeHashAt;
106  using Interface::storageAt;
107 
108  virtual u256 balanceAt(Address _a, BlockNumber _block) const override;
109  virtual u256 countAt(Address _a, BlockNumber _block) const override;
110  virtual u256 stateAt(Address _a, u256 _l, BlockNumber _block) const override;
111  virtual h256 stateRootAt(Address _a, BlockNumber _block) const override;
112  virtual bytes codeAt(Address _a, BlockNumber _block) const override;
113  virtual h256 codeHashAt(Address _a, BlockNumber _block) const override;
114  virtual std::map<h256, std::pair<u256, u256>> storageAt(Address _a, BlockNumber _block) const override;
115 
116  virtual LocalisedLogEntries logs(unsigned _watchId) const override;
117  virtual LocalisedLogEntries logs(LogFilter const& _filter) const override;
118  virtual void prependLogsFromBlock(LogFilter const& _filter, h256 const& _blockHash, BlockPolarity _polarity, LocalisedLogEntries& io_logs) const;
119 
121  virtual unsigned installWatch(LogFilter const& _filter, Reaping _r = Reaping::Automatic) override;
122  virtual unsigned installWatch(h256 _filterId, Reaping _r = Reaping::Automatic) override;
123  virtual bool uninstallWatch(unsigned _watchId) override;
124  virtual LocalisedLogEntries peekWatch(unsigned _watchId) const override;
125  virtual LocalisedLogEntries checkWatch(unsigned _watchId) override;
126 
127  virtual h256 hashFromNumber(BlockNumber _number) const override;
128  virtual BlockNumber numberFromHash(h256 _blockHash) const override;
129  virtual int compareBlockHashes(h256 _h1, h256 _h2) const override;
130  virtual BlockHeader blockInfo(h256 _hash) const override;
131  virtual BlockDetails blockDetails(h256 _hash) const override;
132  virtual Transaction transaction(h256 _transactionHash) const override;
133  virtual LocalisedTransaction localisedTransaction(h256 const& _transactionHash) const override;
134  virtual Transaction transaction(h256 _blockHash, unsigned _i) const override;
135  virtual LocalisedTransaction localisedTransaction(h256 const& _blockHash, unsigned _i) const override;
136  virtual TransactionReceipt transactionReceipt(h256 const& _transactionHash) const override;
137  virtual LocalisedTransactionReceipt localisedTransactionReceipt(h256 const& _transactionHash) const override;
138  virtual std::pair<h256, unsigned> transactionLocation(h256 const& _transactionHash) const override;
139  virtual Transactions transactions(h256 _blockHash) const override;
140  virtual TransactionHashes transactionHashes(h256 _blockHash) const override;
141  virtual BlockHeader uncle(h256 _blockHash, unsigned _i) const override;
142  virtual UncleHashes uncleHashes(h256 _blockHash) const override;
143  virtual unsigned transactionCount(h256 _blockHash) const override;
144  virtual unsigned uncleCount(h256 _blockHash) const override;
145  virtual unsigned number() const override;
146  virtual Transactions pending() const override;
147  virtual h256s pendingHashes() const override;
148  virtual BlockHeader pendingInfo() const override;
149  virtual BlockDetails pendingDetails() const override;
150 
151  virtual EVMSchedule evmSchedule() const override { return sealEngine()->evmSchedule(EnvInfo(pendingInfo())); }
152 
153  virtual ImportResult injectTransaction(bytes const& _rlp, IfDropped _id = IfDropped::Ignore) override { prepareForTransaction(); return m_tq.import(_rlp, _id); }
154  virtual ImportResult injectBlock(bytes const& _block) override;
155 
156  using Interface::addresses;
157  virtual Addresses addresses(BlockNumber _block) const override;
158  virtual u256 gasLimitRemaining() const override;
159  virtual u256 gasBidPrice() const override { return DefaultGasPrice; }
160 
162  virtual Address author() const override;
163 
164  virtual bool isKnown(h256 const& _hash) const override;
165  virtual bool isKnown(BlockNumber _block) const override;
166  virtual bool isKnownTransaction(h256 const& _transactionHash) const override;
167  virtual bool isKnownTransaction(h256 const& _blockHash, unsigned _i) const override;
168 
169  virtual void startSealing() override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::startSealing")); }
170  virtual void stopSealing() override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::stopSealing")); }
171  virtual bool wouldSeal() const override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::wouldSeal")); }
172 
173  virtual SyncStatus syncStatus() const override { BOOST_THROW_EXCEPTION(InterfaceNotSupported("ClientBase::syncStatus")); }
174 
175  Block block(BlockNumber _h) const;
176 
177 protected:
180  virtual BlockChain& bc() = 0;
181  virtual BlockChain const& bc() const = 0;
182  virtual Block block(h256 const& _h) const = 0;
183  virtual Block preSeal() const = 0;
184  virtual Block postSeal() const = 0;
185  virtual void prepareForTransaction() = 0;
187 
189 
190  // filters
192  std::unordered_map<h256, InstalledFilter> m_filters;
193  std::unordered_map<h256, h256s> m_specialFilters = std::unordered_map<h256, std::vector<h256>>{{PendingChangedFilter, {}}, {ChainChangedFilter, {}}};
195  std::map<unsigned, ClientWatch> m_watches;
196 };
197 
198 }}
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
LocalisedLogEntries changes
Definition: ClientBase.h:62
A queue of Transactions, each stored as RLP.
h256s TransactionHashes
Definition: BlockChain.h:81
u256 countAt(Address _a) const
Definition: Interface.h:122
Implements the blockchain database.
Definition: BlockChain.h:105
virtual EVMSchedule evmSchedule() const override
Definition: ClientBase.h:151
Encapsulation of a block header.
Definition: BlockHeader.h:95
InstalledFilter(LogFilter const &_f)
Definition: ClientBase.h:40
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
std::vector< Transaction > Transactions
Nice name for vector of Transaction.
Definition: Transaction.h:121
virtual std::pair< h256, Address > submitTransaction(TransactionSkeleton const &_t, Secret const &_secret)=0
Submits a new transaction.
LocalisedLogEntries changes
Definition: ClientBase.h:44
ClientWatch(h256 _id, Reaping _r)
Definition: ClientBase.h:56
std::hash for asio::adress
Definition: Common.h:323
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
Active model of a block within the block chain.
Definition: Block.h:73
virtual Addresses addresses() const
Get a list of all active addresses.
Definition: Interface.h:203
bytes codeAt(Address _a) const
Definition: Interface.h:124
virtual ExecutionResult create(Address const &_from, u256 _value, bytes const &_data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff=FudgeFactor::Strict)=0
Does the given creation.
IfDropped
Import transaction policy.
Definition: Common.h:214
virtual ExecutionResult call(Address const &_from, u256 _value, Address _dest, bytes const &_data, u256 _gas, u256 _gasPrice, BlockNumber _blockNumber, FudgeFactor _ff=FudgeFactor::Strict)=0
Makes the given call. Nothing is recorded into the state.
BlockPolarity
Definition: ExtVMFace.h:81
ClientBase(TransactionQueue::Limits const &_l=TransactionQueue::Limits{1024, 1024})
Definition: ClientBase.h:79
const char * name
Definition: rest.cpp:36
ExecStats::duration max
Definition: ExecStats.cpp:36
u256 balanceAt(Address _a) const
Definition: Interface.h:121
std::vector< byte > bytes
Definition: Common.h:75
Don&#39;t import transaction that was previously dropped.
virtual ~ClientBase()
Definition: ClientBase.h:80
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
h160s Addresses
A vector of Ethereum addresses.
Definition: Common.h:68
virtual bool wouldSeal() const override
Would we like to be sealing now?
Definition: ClientBase.h:171
virtual void stopSealing() override
Stop sealing.
Definition: ClientBase.h:170
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
std::function< void(GasEstimationProgress const &)> GasEstimationCallback
Definition: Interface.h:62
u256 stateAt(Address _a, u256 _l) const
Definition: Interface.h:123
virtual SyncStatus syncStatus() const override
Get some information on the block queue.
Definition: ClientBase.h:173
clock::time_point time_point
Definition: bench.h:49
virtual ImportResult injectTransaction(bytes const &_rlp, IfDropped _id=IfDropped::Ignore) override
Injects the RLP-encoded transaction given by the _rlp into the transaction queue directly.
Definition: ClientBase.h:153
TransactionQueue m_tq
}
Definition: ClientBase.h:188
std::map< h256, std::pair< u256, u256 > > storageAt(Address _a) const
Definition: Interface.h:126
virtual u256 gasBidPrice() const override
Definition: ClientBase.h:159
Definition: ExtVMFace.h:112
The default logging channels.
Definition: Log.h:130
virtual void startSealing() override
Start sealing.
Definition: ClientBase.h:169
std::mutex Mutex
Definition: Guards.h:37
Mutex x_filtersWatches
Our lock.
Definition: ClientBase.h:191
std::vector< h256 > h256s
Definition: FixedHash.h:345
std::map< unsigned, ClientWatch > m_watches
Each and every watch - these reference a filter.
Definition: ClientBase.h:195
std::unordered_map< h256, InstalledFilter > m_filters
The dictionary of filters that are active.
Definition: ClientBase.h:192
Definition: ExtVMFace.h:88