Fabcoin Core  0.16.2
P2P Digital Currency
ExtVMFace.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 <set>
25 #include <functional>
26 #include <boost/optional.hpp>
27 #include <libdevcore/Common.h>
28 #include <libdevcore/CommonData.h>
29 #include <libdevcore/RLP.h>
30 #include <libdevcore/SHA3.h>
31 #include <libevmcore/Instruction.h>
32 #include <libethcore/Common.h>
33 #include <libethcore/BlockHeader.h>
35 
36 namespace dev
37 {
38 namespace eth
39 {
40 
56 class owning_bytes_ref: public vector_ref<byte const>
57 {
58 public:
59  owning_bytes_ref() = default;
60 
64  owning_bytes_ref(bytes&& _bytes, size_t _begin, size_t _size):
65  m_bytes(std::move(_bytes))
66  {
67  // Set the reference *after* the buffer is moved to avoid
68  // pointer invalidation.
69  retarget(&m_bytes[_begin], _size);
70  }
71 
72  owning_bytes_ref(owning_bytes_ref const&) = delete;
76 
77 private:
79 };
80 
81 enum class BlockPolarity
82 {
83  Unknown,
84  Dead,
85  Live
86 };
87 
88 struct LogEntry
89 {
90  LogEntry() {}
91  LogEntry(RLP const& _r) { address = (Address)_r[0]; topics = _r[1].toVector<h256>(); data = _r[2].toBytes(); }
92  LogEntry(Address const& _address, h256s const& _ts, bytes&& _d): address(_address), topics(_ts), data(std::move(_d)) {}
93 
94  void streamRLP(RLPStream& _s) const { _s.appendList(3) << address << topics << data; }
95 
96  LogBloom bloom() const
97  {
98  LogBloom ret;
99  ret.shiftBloom<3>(sha3(address.ref()));
100  for (auto t: topics)
101  ret.shiftBloom<3>(sha3(t.ref()));
102  return ret;
103  }
104 
108 };
109 
110 using LogEntries = std::vector<LogEntry>;
111 
113 {
115  explicit LocalisedLogEntry(LogEntry const& _le): LogEntry(_le) {}
116 
118  LogEntry const& _le,
119  h256 _special
120  ):
121  LogEntry(_le),
122  isSpecial(true),
123  special(_special)
124  {}
125 
127  LogEntry const& _le,
128  h256 const& _blockHash,
129  BlockNumber _blockNumber,
130  h256 const& _transactionHash,
131  unsigned _transactionIndex,
132  unsigned _logIndex,
134  ):
135  LogEntry(_le),
136  blockHash(_blockHash),
137  blockNumber(_blockNumber),
138  transactionHash(_transactionHash),
139  transactionIndex(_transactionIndex),
140  logIndex(_logIndex),
141  polarity(_polarity),
142  mined(true)
143  {}
144 
146  BlockNumber blockNumber = 0;
148  unsigned transactionIndex = 0;
149  unsigned logIndex = 0;
151  bool mined = false;
152  bool isSpecial = false;
154 };
155 
156 using LocalisedLogEntries = std::vector<LocalisedLogEntry>;
157 
158 inline LogBloom bloom(LogEntries const& _logs)
159 {
160  LogBloom ret;
161  for (auto const& l: _logs)
162  ret |= l.bloom();
163  return ret;
164 }
165 
166 struct SubState
167 {
168  std::set<Address> suicides;
171 
173  {
174  suicides += _s.suicides;
175  refunds += _s.refunds;
176  logs += _s.logs;
177  return *this;
178  }
179 
180  void clear()
181  {
182  suicides.clear();
183  logs.clear();
184  refunds = 0;
185  }
186 };
187 
188 class ExtVMFace;
189 class VM;
190 
191 using LastHashes = std::vector<h256>;
192 
193 using OnOpFunc = std::function<void(uint64_t /*steps*/, uint64_t /* PC */, Instruction /*instr*/, bigint /*newMemSize*/, bigint /*gasCost*/, bigint /*gas*/, VM*, ExtVMFace const*)>;
194 
196 {
205 };
206 
207 class EnvInfo
208 {
209 public:
210  EnvInfo() {}
211  EnvInfo(BlockHeader const& _current, LastHashes const& _lh = LastHashes(), u256 const& _gasUsed = u256()):
212  m_number(_current.number()),
213  m_author(_current.author()),
214  m_timestamp(_current.timestamp()),
215  m_difficulty(_current.difficulty()),
216  // Trim gas limit to int64. convert_to used explicitly instead of
217  // static_cast to be noticed when BlockHeader::gasLimit() will be
218  // changed to int64 too.
219  m_gasLimit(_current.gasLimit().convert_to<int64_t>()),
220  m_lastHashes(_lh),
221  m_gasUsed(_gasUsed)
222  {}
223 
224  EnvInfo(BlockHeader const& _current, LastHashes&& _lh, u256 const& _gasUsed = u256()):
225  m_number(_current.number()),
226  m_author(_current.author()),
227  m_timestamp(_current.timestamp()),
228  m_difficulty(_current.difficulty()),
229  // Trim gas limit to int64. convert_to used explicitly instead of
230  // static_cast to be noticed when BlockHeader::gasLimit() will be
231  // changed to int64 too.
232  m_gasLimit(_current.gasLimit().convert_to<int64_t>()),
233  m_lastHashes(_lh),
234  m_gasUsed(_gasUsed)
235  {}
236 
237  u256 const& number() const { return m_number; }
238  Address const& author() const { return m_author; }
239  u256 const& timestamp() const { return m_timestamp; }
240  u256 const& difficulty() const { return m_difficulty; }
241  int64_t gasLimit() const { return m_gasLimit; }
242  LastHashes const& lastHashes() const { return m_lastHashes; }
243  u256 const& gasUsed() const { return m_gasUsed; }
244 
245  void setNumber(u256 const& _v) { m_number = _v; }
246  void setAuthor(Address const& _v) { m_author = _v; }
247  void setTimestamp(u256 const& _v) { m_timestamp = _v; }
248  void setDifficulty(u256 const& _v) { m_difficulty = _v; }
249  void setGasLimit(int64_t _v) { m_gasLimit = _v; }
250  void setLastHashes(LastHashes&& _lh) { m_lastHashes = _lh; }
251 
252 private:
257  int64_t m_gasLimit;
260 };
261 
266 {
267 public:
269  ExtVMFace() = default;
270 
272  ExtVMFace(EnvInfo const& _envInfo, Address _myAddress, Address _caller, Address _origin, u256 _value, u256 _gasPrice, bytesConstRef _data, bytes _code, h256 const& _codeHash, unsigned _depth);
273 
274  virtual ~ExtVMFace() = default;
275 
276  ExtVMFace(ExtVMFace const&) = delete;
277  ExtVMFace& operator=(ExtVMFace const&) = delete;
278 
280  virtual u256 store(u256) { return 0; }
281 
283  virtual void setStore(u256, u256) {}
284 
286  virtual u256 balance(Address) { return 0; }
287 
289  virtual bytes const& codeAt(Address) { return NullBytes; }
290 
292  virtual size_t codeSizeAt(Address) { return 0; }
293 
295  virtual bool exists(Address) { return false; }
296 
298  virtual void suicide(Address) { sub.suicides.insert(myAddress); }
299 
301  virtual h160 create(u256, u256&, bytesConstRef, OnOpFunc const&) { return h160(); }
302 
304  virtual boost::optional<owning_bytes_ref> call(CallParameters&) = 0;
305 
307  virtual void log(h256s&& _topics, bytesConstRef _data) { sub.logs.push_back(LogEntry(myAddress, std::move(_topics), _data.toBytes())); }
308 
310  h256 blockHash(u256 _number) { return _number < envInfo().number() && _number >= (std::max<u256>(256, envInfo().number()) - 256) ? envInfo().lastHashes()[(unsigned)(envInfo().number() - 1 - _number)] : h256(); }
311 
313  EnvInfo const& envInfo() const { return m_envInfo; }
314 
316  virtual EVMSchedule const& evmSchedule() const { return DefaultSchedule; }
317 
318 private:
320 
321 public:
322  // TODO: make private
332  unsigned depth = 0;
333 };
334 
335 }
336 }
h256 blockHash
Definition: ExtVMFace.h:145
LastHashes const & lastHashes() const
Definition: ExtVMFace.h:242
virtual bytes const & codeAt(Address)
Read address&#39;s code.
Definition: ExtVMFace.h:289
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define function(a, b, c, d, k, s)
SubState & operator+=(SubState const &_s)
Definition: ExtVMFace.h:172
A modifiable reference to an existing object or vector in memory.
Definition: vector_ref.h:20
LogEntry(RLP const &_r)
Definition: ExtVMFace.h:91
u256 const & timestamp() const
Definition: ExtVMFace.h:239
LocalisedLogEntry(LogEntry const &_le, h256 const &_blockHash, BlockNumber _blockNumber, h256 const &_transactionHash, unsigned _transactionIndex, unsigned _logIndex, BlockPolarity _polarity=BlockPolarity::Unknown)
Definition: ExtVMFace.h:126
std::vector< T > toVector(int _flags=LaissezFaire) const
Definition: RLP.h:204
Encapsulation of a block header.
Definition: BlockHeader.h:95
LogEntries logs
Any logs.
Definition: ExtVMFace.h:169
int64_t m_gasLimit
Definition: ExtVMFace.h:257
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
virtual size_t codeSizeAt(Address)
Definition: ExtVMFace.h:292
LastHashes m_lastHashes
Definition: ExtVMFace.h:258
int64_t gasLimit() const
Definition: ExtVMFace.h:241
u256 value
Value (in Wei) that was passed to this address.
Definition: ExtVMFace.h:326
owning_bytes_ref(bytes &&_bytes, size_t _begin, size_t _size)
Definition: ExtVMFace.h:64
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
Address const & author() const
Definition: ExtVMFace.h:238
EnvInfo const & envInfo() const
Get the execution environment information.
Definition: ExtVMFace.h:313
h256 special
Definition: ExtVMFace.h:153
std::hash for asio::adress
Definition: Common.h:323
u256 const & gasUsed() const
Definition: ExtVMFace.h:243
void setGasLimit(int64_t _v)
Definition: ExtVMFace.h:249
h256 codeHash
SHA3 hash of the executing code.
Definition: ExtVMFace.h:330
u256 const & number() const
Definition: ExtVMFace.h:237
FixedHash & shiftBloom(FixedHash< M > const &_h)
Definition: FixedHash.h:170
LocalisedLogEntry(LogEntry const &_le, h256 _special)
Definition: ExtVMFace.h:117
unsigned BlockNumber
Definition: Common.h:72
virtual h160 create(u256, u256 &, bytesConstRef, OnOpFunc const &)
Create a new (contract) account.
Definition: ExtVMFace.h:301
std::vector< LocalisedLogEntry > LocalisedLogEntries
Definition: ExtVMFace.h:156
u256 refunds
Refund counter of SSTORE nonzero->zero.
Definition: ExtVMFace.h:170
void retarget(byte const *_d, size_t _s)
Definition: vector_ref.h:65
LogEntry(Address const &_address, h256s const &_ts, bytes &&_d)
Definition: ExtVMFace.h:92
std::vector< h256 > LastHashes
Definition: ExtVMFace.h:191
Address caller
Address which sent the message (either equal to origin or a contract).
Definition: ExtVMFace.h:324
virtual bool exists(Address)
Does the account exist?
Definition: ExtVMFace.h:295
EnvInfo const & m_envInfo
Definition: ExtVMFace.h:319
h256 blockHash(u256 _number)
Hash of a block if within the last 256 blocks, or h256() otherwise.
Definition: ExtVMFace.h:310
h256 transactionHash
Definition: ExtVMFace.h:147
BlockPolarity
Definition: ExtVMFace.h:81
void setLastHashes(LastHashes &&_lh)
Definition: ExtVMFace.h:250
void setTimestamp(u256 const &_v)
Definition: ExtVMFace.h:247
std::vector< byte > bytes
Definition: Common.h:75
std::vector< unsigned char > toBytes() const
Definition: vector_ref.h:45
h256s topics
Definition: ExtVMFace.h:106
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
RLPStream & appendList(size_t _items)
Appends a list.
Definition: RLP.cpp:276
u256 const & difficulty() const
Definition: ExtVMFace.h:240
void setNumber(u256 const &_v)
Definition: ExtVMFace.h:245
FixedHash< 32 > h256
Definition: FixedHash.h:340
void streamRLP(RLPStream &_s) const
Definition: ExtVMFace.h:94
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Address address
Definition: ExtVMFace.h:105
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:39
EnvInfo(BlockHeader const &_current, LastHashes &&_lh, u256 const &_gasUsed=u256())
Definition: ExtVMFace.h:224
virtual void setStore(u256, u256)
Write a value in storage.
Definition: ExtVMFace.h:283
u256 gasPrice
Price of gas (that we already paid).
Definition: ExtVMFace.h:327
std::set< Address > suicides
Any accounts that have suicided.
Definition: ExtVMFace.h:168
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:265
Address m_author
Definition: ExtVMFace.h:254
bytes code
Current code that is executing.
Definition: ExtVMFace.h:329
Address myAddress
Address associated with executing code (a contract, or contract-to-be).
Definition: ExtVMFace.h:323
byte const * data() const
Definition: vector_ref.h:51
bytes data
Definition: ExtVMFace.h:107
Address origin
Original transactor.
Definition: ExtVMFace.h:325
Reference to a slice of buffer that also owns the buffer.
Definition: ExtVMFace.h:56
LogEntry()
Definition: ExtVMFace.h:90
virtual EVMSchedule const & evmSchedule() const
Return the EVM gas-price schedule for this execution context.
Definition: ExtVMFace.h:316
LocalisedLogEntry(LogEntry const &_le)
Definition: ExtVMFace.h:115
owning_bytes_ref & operator=(owning_bytes_ref const &)=delete
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
virtual void suicide(Address)
Suicide the associated contract and give proceeds to the given address.
Definition: ExtVMFace.h:298
EnvInfo(BlockHeader const &_current, LastHashes const &_lh=LastHashes(), u256 const &_gasUsed=u256())
Definition: ExtVMFace.h:211
void setAuthor(Address const &_v)
Definition: ExtVMFace.h:246
SubState sub
Sub-band VM state (suicides, refund counter, logs).
Definition: ExtVMFace.h:331
Definition: ExtVMFace.h:112
virtual void log(h256s &&_topics, bytesConstRef _data)
Revert any changes made (by any of the other calls).
Definition: ExtVMFace.h:307
bytesConstRef data
Definition: ExtVMFace.h:203
FixedHash< 20 > h160
Definition: FixedHash.h:341
virtual u256 balance(Address)
Read address&#39;s balance.
Definition: ExtVMFace.h:286
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
bytesConstRef data
Current input data.
Definition: ExtVMFace.h:328
void setDifficulty(u256 const &_v)
Definition: ExtVMFace.h:248
LogBloom bloom() const
Definition: ExtVMFace.h:96
virtual u256 store(u256)
Read storage location.
Definition: ExtVMFace.h:280
std::vector< h256 > h256s
Definition: FixedHash.h:345
Class for writing to an RLP bytestream.
Definition: RLP.h:383
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
LocalisedLogEntry()
Definition: ExtVMFace.h:114
bytes toBytes(int _flags=LaissezFaire) const
Converts to bytearray.
Definition: RLP.h:195
std::vector< LogEntry > LogEntries
Definition: ExtVMFace.h:110
LogBloom bloom(LogEntries const &_logs)
Definition: ExtVMFace.h:158
Definition: ExtVMFace.h:88