vm.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /** @file vm.h
  15. * @author Christoph Jentzsch <jentzsch.simulationsoftware@gmail.com>
  16. * @author Gav Wood <i@gavwood.com>
  17. * @date 2014
  18. * vm test functions.
  19. */
  20. #pragma once
  21. #include <fstream>
  22. #include <cstdint>
  23. #include <test/TestHelper.h>
  24. #include <boost/test/unit_test.hpp>
  25. #include <json_spirit/json_spirit.h>
  26. #include <libdevcore/Log.h>
  27. #include <libdevcore/CommonIO.h>
  28. #include <libevmcore/Instruction.h>
  29. #include <libevm/ExtVMFace.h>
  30. #include <libevm/VM.h>
  31. #include <libethereum/Transaction.h>
  32. #include <libethereum/ExtVM.h>
  33. #include <libethereum/State.h>
  34. namespace dev { namespace test {
  35. struct FakeExtVMFailure : virtual Exception {};
  36. class FakeExtVM: public eth::ExtVMFace
  37. {
  38. public:
  39. FakeExtVM() = delete;
  40. FakeExtVM(eth::EnvInfo const& _envInfo, unsigned _depth = 0);
  41. virtual u256 store(u256 _n) override { return std::get<2>(addresses[myAddress])[_n]; }
  42. virtual void setStore(u256 _n, u256 _v) override { std::get<2>(addresses[myAddress])[_n] = _v; }
  43. virtual bool exists(Address _a) override { return !!addresses.count(_a); }
  44. virtual u256 balance(Address _a) override { return std::get<0>(addresses[_a]); }
  45. virtual void suicide(Address _a) override { std::get<0>(addresses[_a]) += std::get<0>(addresses[myAddress]); addresses.erase(myAddress); }
  46. virtual bytes const& codeAt(Address _a) override { return std::get<3>(addresses[_a]); }
  47. virtual size_t codeSizeAt(Address _a) override { return std::get<3>(addresses[_a]).size(); }
  48. virtual h160 create(u256 _endowment, u256& io_gas, bytesConstRef _init, eth::OnOpFunc const&) override;
  49. virtual bool call(eth::CallParameters&) override;
  50. void setTransaction(Address _caller, u256 _value, u256 _gasPrice, bytes const& _data);
  51. void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage, bytes const& _code);
  52. void set(Address _a, u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage, bytes const& _code);
  53. void reset(u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage);
  54. u256 doPosts();
  55. json_spirit::mObject exportEnv();
  56. static dev::eth::EnvInfo importEnv(json_spirit::mObject& _o);
  57. json_spirit::mObject exportState();
  58. void importState(json_spirit::mObject& _object);
  59. json_spirit::mObject exportExec();
  60. void importExec(json_spirit::mObject& _o);
  61. json_spirit::mArray exportCallCreates();
  62. void importCallCreates(json_spirit::mArray& _callcreates);
  63. eth::OnOpFunc simpleTrace() const;
  64. std::map<Address, std::tuple<u256, u256, std::map<u256, u256>, bytes>> addresses;
  65. eth::Transactions callcreates;
  66. bytes thisTxData;
  67. bytes thisTxCode;
  68. u256 gas;
  69. u256 execGas;
  70. };
  71. } } // Namespace Close