VMFace.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <memory>
  16. #include <libdevcore/Exceptions.h>
  17. #include "ExtVMFace.h"
  18. namespace dev
  19. {
  20. namespace eth
  21. {
  22. struct VMException: virtual Exception {};
  23. #define ETH_SIMPLE_EXCEPTION_VM(X) struct X: virtual VMException { const char* what() const noexcept override { return #X; } }
  24. ETH_SIMPLE_EXCEPTION_VM(BreakPointHit);
  25. ETH_SIMPLE_EXCEPTION_VM(BadInstruction);
  26. ETH_SIMPLE_EXCEPTION_VM(BadJumpDestination);
  27. ETH_SIMPLE_EXCEPTION_VM(OutOfGas);
  28. ETH_SIMPLE_EXCEPTION_VM(OutOfStack);
  29. ETH_SIMPLE_EXCEPTION_VM(StackUnderflow);
  30. /// EVM Virtual Machine interface
  31. class VMFace
  32. {
  33. public:
  34. VMFace() = default;
  35. virtual ~VMFace() = default;
  36. VMFace(VMFace const&) = delete;
  37. VMFace& operator=(VMFace const&) = delete;
  38. /// Execute EVM code by VM.
  39. ///
  40. /// @param _out Expected output
  41. void exec(u256& io_gas, ExtVMFace& _ext, bytesRef _out, OnOpFunc const& _onOp = {})
  42. {
  43. execImpl(io_gas, _ext, _onOp).copyTo(_out);
  44. }
  45. /// The same as above but returns a copy of full output.
  46. bytes exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {})
  47. {
  48. return execImpl(io_gas, _ext, _onOp).toVector();
  49. }
  50. /// VM implementation
  51. virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) = 0;
  52. };
  53. }
  54. }