Fabcoin Core  0.16.2
P2P Digital Currency
Block.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 <array>
25 #include <unordered_map>
26 #include <libdevcore/Common.h>
27 #include <libdevcore/RLP.h>
28 #include <libdevcore/TrieDB.h>
29 #include <libdevcore/OverlayDB.h>
30 #include <libethcore/Exceptions.h>
31 #include <libethcore/BlockHeader.h>
33 #include <libevm/ExtVMFace.h>
34 #include "Account.h"
35 #include "Transaction.h"
36 #include "TransactionReceipt.h"
37 #include "GasPricer.h"
38 #include "State.h"
39 
40 namespace dev
41 {
42 
43 namespace test { class ImportTest; class StateLoader; }
44 
45 namespace eth
46 {
47 
48 class SealEngineFace;
49 class BlockChain;
50 class State;
51 class TransactionQueue;
52 struct VerifiedBlockRef;
53 
54 struct BlockChat: public LogChannel { static const char* name(); static const int verbosity = 4; };
55 struct BlockTrace: public LogChannel { static const char* name(); static const int verbosity = 5; };
56 struct BlockDetail: public LogChannel { static const char* name(); static const int verbosity = 14; };
57 struct BlockSafeExceptions: public LogChannel { static const char* name(); static const int verbosity = 21; };
58 
60 {
61  double verify;
62  double enact;
63 };
64 
65 DEV_SIMPLE_EXCEPTION(ChainOperationWithUnknownBlockChain);
66 DEV_SIMPLE_EXCEPTION(InvalidOperationOnSealedBlock);
67 
73 class Block
74 {
75  friend class ExtVM;
76  friend class dev::test::ImportTest;
77  friend class dev::test::StateLoader;
78  friend class Executive;
79  friend class BlockChain;
80 
81 public:
82  // TODO: pass in ChainOperationParams rather than u256
83 
85  Block(u256 const& _accountStartNonce): m_state(_accountStartNonce, OverlayDB(), BaseState::Empty), m_precommit(_accountStartNonce) {}
86 
92  Block(BlockChain const& _bc, OverlayDB const& _db, BaseState _bs = BaseState::PreExisting, Address const& _author = Address());
93 
99  Block(BlockChain const& _bc, OverlayDB const& _db, h256 const& _root, Address const& _author = Address());
100 
101  enum NullType { Null };
102  Block(NullType): m_state(0, OverlayDB(), BaseState::Empty), m_precommit(0) {}
103 
105  explicit Block(BlockChain const& _bc): Block(Null) { noteChain(_bc); }
106 
108  Block(Block const& _s);
109 
111  Block& operator=(Block const& _s);
112 
114  Address author() const { return m_author; }
115 
118  void setAuthor(Address const& _id) { m_author = _id; resetCurrent(); }
119 
122  void noteChain(BlockChain const& _bc);
123 
124  // Account-getters. All operate on the final state.
125 
128  u256 balance(Address const& _address) const { return m_state.balance(_address); }
129 
132  u256 transactionsFrom(Address const& _address) const { return m_state.getNonce(_address); }
133 
135  bool addressInUse(Address const& _address) const { return m_state.addressInUse(_address); }
136 
138  bool addressHasCode(Address const& _address) const { return m_state.addressHasCode(_address); }
139 
141  h256 storageRoot(Address const& _contract) const { return m_state.storageRoot(_contract); }
142 
145  u256 storage(Address const& _contract, u256 const& _memory) const { return m_state.storage(_contract, _memory); }
146 
150  std::map<h256, std::pair<u256, u256>> storage(Address const& _contract) const { return m_state.storage(_contract); }
151 
154  bytes const& code(Address const& _contract) const { return m_state.code(_contract); }
155 
158  h256 codeHash(Address const& _contract) const { return m_state.codeHash(_contract); }
159 
160  // General information from state
161 
163  State const& state() const { return m_state; }
164 
166  OverlayDB const& db() const { return m_state.db(); }
167 
169  h256 rootHash() const { return m_state.rootHash(); }
170 
173  std::unordered_map<Address, u256> addresses() const { return m_state.addresses(); }
174 
175  // For altering accounts behind-the-scenes
176 
180  State& mutableState() { return m_state; }
181 
182  // Information concerning ongoing transactions
183 
185  u256 gasLimitRemaining() const { return m_currentBlock.gasLimit() - gasUsed(); }
186 
188  Transactions const& pending() const { return m_transactions; }
189 
191  h256Hash const& pendingHashes() const { return m_transactionSet; }
192 
194  TransactionReceipt const& receipt(unsigned _i) const { return m_receipts[_i]; }
195 
197  LogEntries const& log(unsigned _i) const { return m_receipts[_i].log(); }
198 
200  LogBloom logBloom() const;
201 
203  LogBloom const& logBloom(unsigned _i) const { return m_receipts[_i].bloom(); }
204 
208  State fromPending(unsigned _i) const;
209 
210  // State-change operations
211 
213  PopulationStatistics populateFromChain(BlockChain const& _bc, h256 const& _hash, ImportRequirements::value _ir = ImportRequirements::None);
214 
217  ExecutionResult execute(LastHashes const& _lh, Transaction const& _t, Permanence _p = Permanence::Committed, OnOpFunc const& _onOp = OnOpFunc());
218 
221  std::pair<TransactionReceipts, bool> sync(BlockChain const& _bc, TransactionQueue& _tq, GasPricer const& _gp, unsigned _msTimeout = 100);
222 
225  bool sync(BlockChain const& _bc);
226 
228  bool sync(BlockChain const& _bc, h256 const& _blockHash, BlockHeader const& _bi = BlockHeader());
229 
232  u256 enactOn(VerifiedBlockRef const& _block, BlockChain const& _bc);
233 
237  void cleanup(bool _fullCommit);
238 
241  void resetCurrent(u256 const& _timestamp = u256(utcTime()));
242 
243  // Sealing
244 
251  void commitToSeal(BlockChain const& _bc, bytes const& _extraData = {});
252 
257 
268  bool sealBlock(bytes const& _header) { return sealBlock(&_header); }
269  bool sealBlock(bytesConstRef _header);
270 
272  bool isSealed() const { return !m_currentBytes.empty(); }
273 
276  bytes const& blockData() const { return m_currentBytes; }
277 
279  BlockHeader const& info() const { return m_currentBlock; }
280 
281 private:
282  SealEngineFace* sealEngine() const;
283 
285  void uncommitToSeal();
286 
291  void ensureCached(Address const& _a, bool _requireCode, bool _forceCreate) const;
292 
294  void ensureCached(std::unordered_map<Address, Account>& _cache, Address const& _a, bool _requireCode, bool _forceCreate) const;
295 
298  u256 enact(VerifiedBlockRef const& _block, BlockChain const& _bc);
299 
301  void applyRewards(std::vector<BlockHeader> const& _uncleBlockHeaders, u256 const& _blockReward);
302 
304  u256 gasUsed() const { return m_receipts.size() ? m_receipts.back().gasUsed() : 0; }
305 
307  void performIrregularModifications();
308 
310  std::string vmTrace(bytesConstRef _block, BlockChain const& _bc, ImportRequirements::value _ir);
311 
317 
321  bool m_committedToSeal = false;
322 
325 
327 
328  SealEngineFace* m_sealEngine = nullptr;
329 
330  friend std::ostream& operator<<(std::ostream& _out, Block const& _s);
331 };
332 
333 std::ostream& operator<<(std::ostream& _out, Block const& _s);
334 
335 
336 }
337 
338 }
bytes const & blockData() const
Get the complete current block, including valid nonce.
Definition: Block.h:276
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
State const & state() const
Get the backing state object.
Definition: Block.h:163
std::function< void(uint64_t, uint64_t, dev::eth::Instruction, dev::bigint, dev::bigint, dev::bigint, dev::eth::VM *, dev::eth::ExtVMFace const *)> OnOpFunc
Definition: fascstate.h:15
bool isSealed() const
Definition: Block.h:272
A queue of Transactions, each stored as RLP.
Transactions m_transactions
The current list of transactions that we&#39;ve included in the state.
Definition: Block.h:313
h256Hash const & pendingHashes() const
Get the list of hashes of pending transactions.
Definition: Block.h:191
bool addressInUse(Address const &_address) const
Check if the address is in use.
Definition: Block.h:135
bytes const & code(Address const &_contract) const
Get the code of an account.
Definition: Block.h:154
Implements the blockchain database.
Definition: BlockChain.h:105
uint64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:64
Encapsulation of a block header.
Definition: BlockHeader.h:95
bool addressHasCode(Address const &_address) const
Check if the address contains executable code.
Definition: Block.h:138
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
bytes m_currentBytes
The current block&#39;s bytes.
Definition: Block.h:320
u256 transactionsFrom(Address const &_address) const
Get the number of transactions a particular address has sent (used for the transaction nonce)...
Definition: Block.h:132
Block(NullType)
Definition: Block.h:102
Block(u256 const &_accountStartNonce)
Default constructor; creates with a blank database prepopulated with the genesis block.
Definition: Block.h:85
TransactionReceipts m_receipts
The corresponding list of transaction receipts.
Definition: Block.h:314
Description of the result of executing a transaction.
Definition: Transaction.h:69
u256 gasUsed() const
Definition: Block.h:304
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
TransactionReceipt const & receipt(unsigned _i) const
Get the transaction receipt for the transaction of the given index.
Definition: Block.h:194
LogEntries const & log(unsigned _i) const
Get the list of pending transactions.
Definition: Block.h:197
u256 storage(Address const &_contract, u256 const &_memory) const
Get the value of a storage position of an account.
Definition: Block.h:145
h256 codeHash(Address const &_contract) const
Get the code hash of an account.
Definition: Block.h:158
Active model of a block within the block chain.
Definition: Block.h:73
std::vector< TransactionReceipt > TransactionReceipts
std::vector< h256 > LastHashes
Definition: ExtVMFace.h:191
u256 gasLimitRemaining() const
Get the remaining gas limit in this block.
Definition: Block.h:185
Address m_author
Our address (i.e. the address to which fees go).
Definition: Block.h:326
BlockHeader m_currentBlock
The current block&#39;s information.
Definition: Block.h:319
std::unordered_map< Address, u256 > addresses() const
Definition: Block.h:173
BaseState
Definition: State.h:76
const char * name
Definition: rest.cpp:36
BlockGetAndPut< word32, BigEndian > Block
Definition: cast.cpp:34
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
BlockHeader m_previousBlock
The previous block&#39;s information.
Definition: Block.h:318
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
Message-call/contract-creation executor; useful for executing transactions.
Definition: Executive.h:106
bool sealBlock(bytes const &_header)
Pass in a properly sealed header matching this block.
Definition: Block.h:268
ldb::DB * db() const
Definition: OverlayDB.h:39
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Transactions const & pending() const
Get the list of pending transactions.
Definition: Block.h:188
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
OverlayDB const & db() const
Open a DB - useful for passing into the constructor & keeping for other states that are necessary...
Definition: Block.h:166
DEV_SIMPLE_EXCEPTION(BadHexCharacter)
Address author() const
Get the author address for any transactions we do and rewards we get.
Definition: Block.h:114
std::map< h256, std::pair< u256, u256 > > storage(Address const &_contract) const
Get the storage of an account.
Definition: Block.h:150
State m_precommit
State at the point immediately prior to rewards.
Definition: Block.h:316
void setAuthor(Address const &_id)
Set the author address for any transactions we do and rewards we get.
Definition: Block.h:118
u256 balance(Address const &_address) const
Get an account&#39;s balance.
Definition: Block.h:128
State & mutableState()
Get a mutable State object which is backing this block.
Definition: Block.h:180
Verified block info, does not hold block data, but a reference instead.
Definition: VerifiedBlock.h:36
h256Hash m_transactionSet
The set of transaction hashes that we&#39;ve included in the state.
Definition: Block.h:315
bytes m_currentUncles
The RLP-encoded block of uncles.
Definition: Block.h:324
An Empty class.
Definition: misc.h:184
Block(BlockChain const &_bc)
Construct from a given blockchain. Empty, but associated with _bc &#39;s chain params.
Definition: Block.h:105
h256 rootHash() const
The hash of the root of our state tree.
Definition: Block.h:169
Externality interface for the Virtual Machine providing access to world state.
Definition: ExtVM.h:42
LogBloom const & logBloom(unsigned _i) const
Get the bloom filter of a particular transaction that happened in the block.
Definition: Block.h:203
The default logging channels.
Definition: Log.h:130
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:349
Permanence
Definition: State.h:82
bytes m_currentTxs
The RLP-encoded block of transactions.
Definition: Block.h:323
std::ostream & operator<<(std::ostream &out, Stats::clock::duration const &d)
Definition: Stats.cpp:51
State m_state
Our state tree, as an OverlayDB DB.
Definition: Block.h:312
h256 storageRoot(Address const &_contract) const
Get the root of the storage of an account.
Definition: Block.h:141
BlockHeader const & info() const
Get the header information on the present block.
Definition: Block.h:279
std::vector< LogEntry > LogEntries
Definition: ExtVMFace.h:110