Ethereum  PoC-8
The C++ Implementation of Ethereum
VMFace.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 */
17 #pragma once
18 
19 #include "ExtVMFace.h"
20 #include <libdevcore/Exceptions.h>
21 #include <memory>
22 
23 namespace dev
24 {
25 namespace eth
26 {
27 
29 #define ETH_SIMPLE_EXCEPTION_VM(X) struct X: VMException { const char* what() const noexcept override { return #X; } }
30 ETH_SIMPLE_EXCEPTION_VM(BadInstruction);
31 ETH_SIMPLE_EXCEPTION_VM(BadJumpDestination);
34 ETH_SIMPLE_EXCEPTION_VM(StackUnderflow);
35 ETH_SIMPLE_EXCEPTION_VM(DisallowedStateChange);
36 ETH_SIMPLE_EXCEPTION_VM(BufferOverrun);
37 
41 
43 using errinfo_evmcStatusCode = boost::error_info<struct tag_evmcStatusCode, evmc_status_code>;
44 
46 {
47  explicit RevertInstruction(owning_bytes_ref&& _output) : m_output(std::move(_output)) {}
52 
53  char const* what() const noexcept override { return "Revert instruction"; }
54 
55  owning_bytes_ref&& output() { return std::move(m_output); }
56 
57 private:
58  owning_bytes_ref m_output;
59 };
60 
61 
63 class VMFace
64 {
65 public:
66  VMFace() = default;
67  virtual ~VMFace() = default;
68  VMFace(VMFace const&) = delete;
69  VMFace& operator=(VMFace const&) = delete;
70 
72  virtual owning_bytes_ref exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) = 0;
73 };
74 
76 
77 // Convert from a 256-bit integer stack/memory entry into a 160-bit Address hash.
78 // Currently we just pull out the right (low-order in BE) 160-bits.
79 inline Address asAddress(u256 _item)
80 {
81  return right160(h256(_item));
82 }
83 
85 {
86  return (u160)_a;
87 }
88 
89 }
90 }
dev::eth::OnOpFunc
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VMFace const *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:115
dev::eth::RevertInstruction::operator=
RevertInstruction & operator=(RevertInstruction const &)=delete
dev::eth::errinfo_evmcStatusCode
boost::error_info< struct tag_evmcStatusCode, evmc_status_code > errinfo_evmcStatusCode
Error info for EVMC status code.
Definition: VMFace.h:43
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::eth::RevertInstruction::RevertInstruction
RevertInstruction(owning_bytes_ref &&_output)
Definition: VMFace.h:47
dev::eth::ETH_SIMPLE_EXCEPTION_VM
ETH_SIMPLE_EXCEPTION_VM(BadInstruction)
dev::eth::RevertInstruction::operator=
RevertInstruction & operator=(RevertInstruction &&)=default
dev::eth::owning_bytes_ref
Definition: ExtVMFace.h:58
dev::eth::VMFace
EVM Virtual Machine interface.
Definition: VMFace.h:64
dev::eth::ExtVMFace
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:204
dev::FixedHash< 20 >
dev::eth::VMFace::operator=
VMFace & operator=(VMFace const &)=delete
dev::right160
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
Definition: FixedHash.h:369
dev::Exception
Base class for all exceptions.
Definition: Exceptions.h:39
ExtVMFace.h
dev::eth::VMException
Definition: VMFace.h:28
dev::eth::RevertInstruction::RevertInstruction
RevertInstruction(RevertInstruction const &)=delete
Exceptions.h
dev::u160
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u160
Definition: Common.h:123
dev::eth::VMFace::~VMFace
virtual ~VMFace()=default
dev::eth::RevertInstruction::what
char const * what() const noexcept override
Definition: VMFace.h:53
dev::eth::InternalVMError
Definition: VMFace.h:40
dev::eth::RevertInstruction::output
owning_bytes_ref && output()
Definition: VMFace.h:55
dev::eth::fromAddress
u256 fromAddress(Address _a)
Definition: VMFace.h:84
dev::eth::VMFace::exec
virtual owning_bytes_ref exec(u256 &io_gas, ExtVMFace &_ext, OnOpFunc const &_onOp)=0
VM implementation.
std
Definition: FixedHash.h:393
dev::eth::asAddress
Address asAddress(u256 _item)
Helpers:
Definition: VMFace.h:79
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev
Definition: Address.cpp:21
dev::eth::VMFace::VMFace
VMFace()=default
dev::eth::VMFace::VMFace
VMFace(VMFace const &)=delete
dev::eth::RevertInstruction::RevertInstruction
RevertInstruction(RevertInstruction &&)=default
dev::eth::RevertInstruction
Definition: VMFace.h:46