From 1f17c569b961123ef97def7bfebc1266a2e4cd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 27 May 2015 13:23:00 +0200 Subject: [PATCH 01/11] Change VM interface to return a copy of output. --- evmjit/libevmjit-cpp/JitVM.cpp | 4 ++-- evmjit/libevmjit-cpp/JitVM.h | 2 +- libethereum/Executive.cpp | 12 +++++------- libethereum/Executive.h | 5 ++--- libevm/SmartVM.cpp | 4 ++-- libevm/SmartVM.h | 2 +- libevm/VM.cpp | 2 +- libevm/VM.h | 2 +- libevm/VMFace.h | 7 ++++++- test/fuzzTesting/checkRandomVMTest.cpp | 4 ++-- test/fuzzTesting/createRandomVMTest.cpp | 2 +- test/libevm/vm.cpp | 4 +--- 12 files changed, 25 insertions(+), 25 deletions(-) diff --git a/evmjit/libevmjit-cpp/JitVM.cpp b/evmjit/libevmjit-cpp/JitVM.cpp index 5193168a4..ac8df545f 100644 --- a/evmjit/libevmjit-cpp/JitVM.cpp +++ b/evmjit/libevmjit-cpp/JitVM.cpp @@ -18,7 +18,7 @@ namespace eth extern "C" void env_sload(); // fake declaration for linker symbol stripping workaround, see a call below -bytesConstRef JitVM::go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step) +bytesConstRef JitVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step) { using namespace jit; @@ -33,7 +33,7 @@ bytesConstRef JitVM::go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, ui { cwarn << "Execution rejected by EVM JIT (gas limit: " << io_gas << "), executing with interpreter"; m_fallbackVM = VMFactory::create(VMKind::Interpreter); - return m_fallbackVM->go(io_gas, _ext, _onOp, _step); + return m_fallbackVM->execImpl(io_gas, _ext, _onOp, _step); } m_data.gas = static_cast(io_gas); diff --git a/evmjit/libevmjit-cpp/JitVM.h b/evmjit/libevmjit-cpp/JitVM.h index e6864f885..7c8b4ceb8 100644 --- a/evmjit/libevmjit-cpp/JitVM.h +++ b/evmjit/libevmjit-cpp/JitVM.h @@ -11,7 +11,7 @@ namespace eth class JitVM: public VMFace { public: - virtual bytesConstRef go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; private: jit::RuntimeData m_data; diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index d7bad0ff9..122432d85 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -46,7 +46,7 @@ u256 Executive::gasUsed() const ExecutionResult Executive::executionResult() const { - return ExecutionResult(gasUsed(), m_excepted, m_newAddress, m_out, m_codeDeposit, m_ext ? m_ext->sub.refunds : 0, m_depositSize, m_gasForDeposit); + return ExecutionResult(gasUsed(), m_excepted, m_newAddress, out(), m_codeDeposit, m_ext ? m_ext->sub.refunds : 0, m_depositSize, m_gasForDeposit); } void Executive::accrueSubState(SubState& _parentContext) @@ -145,8 +145,7 @@ bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address co else { m_gas = (u256)(_p.gas - g); - m_precompiledOut = it->second.exec(_p.data); - m_out = &m_precompiledOut; + m_out = it->second.exec(_p.data); } } else @@ -219,7 +218,7 @@ bool Executive::go(OnOpFunc const& _onOp) #endif try { - m_out = m_vm->go(m_gas, *m_ext, _onOp); + m_out = m_vm->exec(m_gas, *m_ext, _onOp); if (m_isCreation) { @@ -232,11 +231,10 @@ bool Executive::go(OnOpFunc const& _onOp) } else { - m_codeDeposit = CodeDeposit::Failed; - m_out.reset(); + m_out.clear(); } - m_s.m_cache[m_newAddress].setCode(m_out.toBytes()); + m_s.m_cache[m_newAddress].setCode(std::move(m_out)); } } catch (StepsDone const&) diff --git a/libethereum/Executive.h b/libethereum/Executive.h index 04d5857b3..bd9fb178e 100644 --- a/libethereum/Executive.h +++ b/libethereum/Executive.h @@ -109,7 +109,7 @@ public: /// @returns gas remaining after the transaction/operation. Valid after the transaction has been executed. u256 gas() const { return m_gas; } /// @returns output data of the transaction/operation. - bytesConstRef out() const { return m_out; } + bytesConstRef out() const { return {m_out.data(), m_out.size()}; } /// @returns the new address for the created contract in the CREATE operation. h160 newAddress() const { return m_newAddress; } /// @returns true iff the operation ended with a VM exception. @@ -123,8 +123,7 @@ private: LastHashes m_lastHashes; std::shared_ptr m_ext; ///< The VM externality object for the VM execution or null if no VM is required. std::unique_ptr m_vm; ///< The VM object or null if no VM is required. - bytes m_precompiledOut; ///< Used for the output when there is no VM for a contract (i.e. precompiled). - bytesConstRef m_out; ///< The copyable output. + bytes m_out; ///< The VM execution output. Address m_newAddress; ///< The address of the created contract in the case of create() being called. unsigned m_depth = 0; ///< The context's call-depth. diff --git a/libevm/SmartVM.cpp b/libevm/SmartVM.cpp index d37abc1bf..27263fc2d 100644 --- a/libevm/SmartVM.cpp +++ b/libevm/SmartVM.cpp @@ -41,7 +41,7 @@ namespace } } -bytesConstRef SmartVM::go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) +bytesConstRef SmartVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) { auto codeHash = sha3(_ext.code); auto vmKind = VMKind::Interpreter; // default VM @@ -68,7 +68,7 @@ bytesConstRef SmartVM::go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, // TODO: Selected VM must be kept only because it returns reference to its internal memory. // VM implementations should be stateless, without escaping memory reference. m_selectedVM = VMFactory::create(vmKind); - return m_selectedVM->go(io_gas, _ext, _onOp, _steps); + return m_selectedVM->execImpl(io_gas, _ext, _onOp, _steps); } } diff --git a/libevm/SmartVM.h b/libevm/SmartVM.h index cc198c0c2..01e163cd2 100644 --- a/libevm/SmartVM.h +++ b/libevm/SmartVM.h @@ -31,7 +31,7 @@ namespace eth class SmartVM: public VMFace { public: - virtual bytesConstRef go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; private: std::unique_ptr m_selectedVM; diff --git a/libevm/VM.cpp b/libevm/VM.cpp index df8432ac6..3a4124d67 100644 --- a/libevm/VM.cpp +++ b/libevm/VM.cpp @@ -45,7 +45,7 @@ static array metrics() return s_ret; } -bytesConstRef VM::go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) +bytesConstRef VM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) { // Reset leftovers from possible previous run m_curPC = 0; diff --git a/libevm/VM.h b/libevm/VM.h index 99f608227..0a33e9fae 100644 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -52,7 +52,7 @@ inline u256 fromAddress(Address _a) class VM: public VMFace { public: - virtual bytesConstRef go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; u256 curPC() const { return m_curPC; } diff --git a/libevm/VMFace.h b/libevm/VMFace.h index d2a12e0ca..8fd7ebcb3 100644 --- a/libevm/VMFace.h +++ b/libevm/VMFace.h @@ -43,7 +43,12 @@ public: VMFace(VMFace const&) = delete; VMFace& operator=(VMFace const&) = delete; - virtual bytesConstRef go(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) = 0; + bytes exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) + { + return execImpl(io_gas, _ext, _onOp, _steps).toVector(); + } + + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) = 0; }; } diff --git a/test/fuzzTesting/checkRandomVMTest.cpp b/test/fuzzTesting/checkRandomVMTest.cpp index 13d677e17..a6ade07f1 100644 --- a/test/fuzzTesting/checkRandomVMTest.cpp +++ b/test/fuzzTesting/checkRandomVMTest.cpp @@ -98,9 +98,9 @@ bool doVMTest(mValue& _v) try { auto vm = eth::VMFactory::create(); - output = vm->go(fev.gas, fev, fev.simpleTrace()).toBytes(); + output = vm->exec(fev.gas, fev, fev.simpleTrace()); } - catch (eth::VMException) + catch (eth::VMException const&) { cnote << "Safe VM Exception"; vmExceptionOccured = true; diff --git a/test/fuzzTesting/createRandomVMTest.cpp b/test/fuzzTesting/createRandomVMTest.cpp index 3196590d4..93040d6de 100644 --- a/test/fuzzTesting/createRandomVMTest.cpp +++ b/test/fuzzTesting/createRandomVMTest.cpp @@ -160,7 +160,7 @@ void doMyTests(json_spirit::mValue& _v) bool vmExceptionOccured = false; try { - output = vm->go(fev.gas, fev, fev.simpleTrace()).toBytes(); + output = vm->exec(fev.gas, fev, fev.simpleTrace()); } catch (eth::VMException const& _e) { diff --git a/test/libevm/vm.cpp b/test/libevm/vm.cpp index cb63e993c..c407c8184 100644 --- a/test/libevm/vm.cpp +++ b/test/libevm/vm.cpp @@ -329,12 +329,10 @@ void doVMTests(json_spirit::mValue& v, bool _fillin) { auto vm = eth::VMFactory::create(); auto vmtrace = Options::get().vmtrace ? fev.simpleTrace() : OnOpFunc{}; - auto outputRef = bytesConstRef{}; { Listener::ExecTimeGuard guard{i.first}; - outputRef = vm->go(fev.gas, fev, vmtrace); + output = vm->exec(fev.gas, fev, vmtrace); } - output = outputRef.toBytes(); } catch (VMException const&) { From 72856a8af658852206f6e117a71b4eb0f6375a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 May 2015 08:56:21 +0200 Subject: [PATCH 02/11] Change the way execution results are collected. Changes handling ExecutionResult by Executive. From now execution results are collected on if a storage for results (ExecutionResult) is provided to an Executiove instance up front. This change allow better output management for calls - VM interface improved. --- evmjit/libevmjit-cpp/JitVM.h | 2 +- libethereum/Client.cpp | 3 +- libethereum/Executive.cpp | 60 ++++++++++++------- libethereum/Executive.h | 14 ++--- libethereum/ExtVM.cpp | 1 - libethereum/State.cpp | 4 +- libethereum/Transaction.h | 17 +----- libevm/SmartVM.h | 2 +- libevm/VM.h | 2 +- libevm/VMFace.h | 12 +++- mix/MixClient.cpp | 5 +- test/libsolidity/solidityExecutionFramework.h | 4 +- 12 files changed, 72 insertions(+), 54 deletions(-) diff --git a/evmjit/libevmjit-cpp/JitVM.h b/evmjit/libevmjit-cpp/JitVM.h index 7c8b4ceb8..03c3c8880 100644 --- a/evmjit/libevmjit-cpp/JitVM.h +++ b/evmjit/libevmjit-cpp/JitVM.h @@ -11,7 +11,7 @@ namespace eth class JitVM: public VMFace { public: - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) override final; private: jit::RuntimeData m_data; diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 6dec81b38..d9813c725 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -440,9 +440,10 @@ ExecutionResult Client::call(Address _dest, bytes const& _data, u256 _gas, u256 temp = m_postMine; temp.addBalance(_from, _value + _gasPrice * _gas); Executive e(temp, LastHashes(), 0); + e.setResultRef(ret); if (!e.call(_dest, _from, _value, _gasPrice, &_data, _gas)) e.go(); - ret = e.executionResult(); + e.finalize(); } catch (...) { diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index 122432d85..fc8ba22a3 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -44,11 +44,6 @@ u256 Executive::gasUsed() const return m_t.gas() - m_gas; } -ExecutionResult Executive::executionResult() const -{ - return ExecutionResult(gasUsed(), m_excepted, m_newAddress, out(), m_codeDeposit, m_ext ? m_ext->sub.refunds : 0, m_depositSize, m_gasForDeposit); -} - void Executive::accrueSubState(SubState& _parentContext) { if (m_ext) @@ -145,7 +140,8 @@ bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address co else { m_gas = (u256)(_p.gas - g); - m_out = it->second.exec(_p.data); + auto out = it->second.exec(_p.data); // FIXME: Avoid double copy + bytesConstRef{&out}.copyTo(_p.out); } } else @@ -153,7 +149,7 @@ bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address co m_gas = _p.gas; if (m_s.addressHasCode(_p.codeAddress)) { - m_vm = VMFactory::create(); + m_outRef = _p.out; // Save ref to expected output buffer to be used in go() bytes const& c = m_s.code(_p.codeAddress); m_ext = make_shared(m_s, m_lastHashes, _p.receiveAddress, _p.senderAddress, _origin, _p.value, _gasPrice, _p.data, &c, m_depth); } @@ -175,10 +171,7 @@ bool Executive::create(Address _sender, u256 _endowment, u256 _gasPrice, u256 _g // Execute _init. if (!_init.empty()) - { - m_vm = VMFactory::create(); m_ext = make_shared(m_s, m_lastHashes, m_newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _init, m_depth); - } m_s.m_cache[m_newAddress] = Account(m_s.balance(m_newAddress), Account::ContractConception); m_s.transferBalance(_sender, m_newAddress, _endowment); @@ -211,30 +204,47 @@ OnOpFunc Executive::simpleTrace() bool Executive::go(OnOpFunc const& _onOp) { - if (m_vm) + if (m_ext) { #if ETH_TIMED_EXECUTIONS boost::timer t; #endif try { - m_out = m_vm->exec(m_gas, *m_ext, _onOp); - + auto vm = VMFactory::create(); if (m_isCreation) { - m_gasForDeposit = m_gas; - m_depositSize = m_out.size(); - if (m_out.size() * c_createDataGas <= m_gas) + auto out = vm->exec(m_gas, *m_ext, _onOp); + if (m_res) + { + m_res->gasForDeposit = m_gas; + m_res->depositSize = out.size(); + } + if (out.size() * c_createDataGas <= m_gas) { - m_codeDeposit = CodeDeposit::Success; - m_gas -= m_out.size() * c_createDataGas; + if (m_res) + m_res->codeDeposit = CodeDeposit::Success; + m_gas -= out.size() * c_createDataGas; } else { - m_codeDeposit = CodeDeposit::Failed; - m_out.clear(); + if (m_res) + m_res->codeDeposit = CodeDeposit::Failed; + out.clear(); } - m_s.m_cache[m_newAddress].setCode(std::move(m_out)); + if (m_res) + m_res->output = out; // copy output to execution result + m_s.m_cache[m_newAddress].setCode(std::move(out)); // FIXME: Set only if Success? + } + else + { + if (m_res) + { + m_res->output = vm->exec(m_gas, *m_ext, _onOp); // take full output + bytesConstRef{&m_res->output}.copyTo(m_outRef); + } + else + vm->exec(m_gas, *m_ext, m_outRef, _onOp); // take only expected output } } catch (StepsDone const&) @@ -293,4 +303,12 @@ void Executive::finalize() // Logs.. if (m_ext) m_logs = m_ext->sub.logs; + + if (m_res) // Collect results + { + m_res->gasUsed = gasUsed(); + m_res->excepted = m_excepted; // TODO: m_except is used only in ExtVM::call + m_res->newAddress = m_newAddress; + m_res->gasRefunded = m_ext ? m_ext->sub.refunds : 0; + } } diff --git a/libethereum/Executive.h b/libethereum/Executive.h index bd9fb178e..8b2906b9b 100644 --- a/libethereum/Executive.h +++ b/libethereum/Executive.h @@ -108,29 +108,25 @@ public: /// @returns gas remaining after the transaction/operation. Valid after the transaction has been executed. u256 gas() const { return m_gas; } - /// @returns output data of the transaction/operation. - bytesConstRef out() const { return {m_out.data(), m_out.size()}; } + /// @returns the new address for the created contract in the CREATE operation. h160 newAddress() const { return m_newAddress; } /// @returns true iff the operation ended with a VM exception. bool excepted() const { return m_excepted != TransactionException::None; } - /// Get the above in an amalgamated fashion. - ExecutionResult executionResult() const; + /// Collect execution results in the result storage provided. + void setResultRef(ExecutionResult& _res) { m_res = &_res; } private: State& m_s; ///< The state to which this operation/transaction is applied. LastHashes m_lastHashes; std::shared_ptr m_ext; ///< The VM externality object for the VM execution or null if no VM is required. - std::unique_ptr m_vm; ///< The VM object or null if no VM is required. - bytes m_out; ///< The VM execution output. + bytesRef m_outRef; ///< Reference to "expected output" buffer. + ExecutionResult* m_res = nullptr; ///< Optional storage for execution results. Address m_newAddress; ///< The address of the created contract in the case of create() being called. unsigned m_depth = 0; ///< The context's call-depth. bool m_isCreation = false; ///< True if the transaction creates a contract, or if create() is called. - unsigned m_depositSize = 0; ///< Amount of code of the creation's attempted deposit. - u256 m_gasForDeposit; ///< Amount of gas remaining for the code deposit phase. - CodeDeposit m_codeDeposit = CodeDeposit::None; ///< True if an attempted deposit failed due to lack of gas. TransactionException m_excepted = TransactionException::None; ///< Details if the VM's execution resulted in an exception. u256 m_gas = 0; ///< The gas for EVM code execution. Initial amount before go() execution, final amount after go() execution. diff --git a/libethereum/ExtVM.cpp b/libethereum/ExtVM.cpp index 488d8e4b7..782d4d79c 100644 --- a/libethereum/ExtVM.cpp +++ b/libethereum/ExtVM.cpp @@ -35,7 +35,6 @@ bool ExtVM::call(CallParameters& _p) e.accrueSubState(sub); } _p.gas = e.gas(); - e.out().copyTo(_p.out); return !e.excepted(); } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index c753f57ea..277e9a649 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1125,6 +1125,8 @@ ExecutionResult State::execute(LastHashes const& _lh, Transaction const& _t, Per // Create and initialize the executive. This will throw fairly cheaply and quickly if the // transaction is bad in any way. Executive e(*this, _lh, 0); + ExecutionResult res; + e.setResultRef(res); e.initialize(_t); // Uncommitting is a non-trivial operation - only do it once we've verified as much of the @@ -1181,7 +1183,7 @@ ExecutionResult State::execute(LastHashes const& _lh, Transaction const& _t, Per m_transactionSet.insert(e.t().sha3()); } - return e.executionResult(); + return res; } State State::fromPending(unsigned _i) const diff --git a/libethereum/Transaction.h b/libethereum/Transaction.h index 09d6cd54c..3a78ced26 100644 --- a/libethereum/Transaction.h +++ b/libethereum/Transaction.h @@ -74,25 +74,14 @@ TransactionException toTransactionException(VMException const& _e); /// Description of the result of executing a transaction. struct ExecutionResult { - ExecutionResult() = default; - ExecutionResult(u256 const& _gasUsed, TransactionException _excepted, Address const& _newAddress, bytesConstRef _output, CodeDeposit _codeDeposit, u256 const& _gasRefund, unsigned _depositSize, u256 const& _gasForDeposit): - gasUsed(_gasUsed), - excepted(_excepted), - newAddress(_newAddress), - output(_output.toBytes()), - codeDeposit(_codeDeposit), - gasRefunded(_gasRefund), - depositSize(_depositSize), - gasForDeposit(_gasForDeposit) - {} u256 gasUsed = 0; TransactionException excepted = TransactionException::Unknown; Address newAddress; bytes output; - CodeDeposit codeDeposit = CodeDeposit::None; + CodeDeposit codeDeposit = CodeDeposit::None; ///< Failed if an attempted deposit failed due to lack of gas. u256 gasRefunded = 0; - unsigned depositSize = 0; - u256 gasForDeposit; + unsigned depositSize = 0; ///< Amount of code of the creation's attempted deposit. + u256 gasForDeposit; ///< Amount of gas remaining for the code deposit phase. }; std::ostream& operator<<(std::ostream& _out, ExecutionResult const& _er); diff --git a/libevm/SmartVM.h b/libevm/SmartVM.h index 01e163cd2..73d515f29 100644 --- a/libevm/SmartVM.h +++ b/libevm/SmartVM.h @@ -31,7 +31,7 @@ namespace eth class SmartVM: public VMFace { public: - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) override final; private: std::unique_ptr m_selectedVM; diff --git a/libevm/VM.h b/libevm/VM.h index 0a33e9fae..da3a843ed 100644 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -52,7 +52,7 @@ inline u256 fromAddress(Address _a) class VM: public VMFace { public: - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) override final; u256 curPC() const { return m_curPC; } diff --git a/libevm/VMFace.h b/libevm/VMFace.h index 8fd7ebcb3..7a344a072 100644 --- a/libevm/VMFace.h +++ b/libevm/VMFace.h @@ -43,12 +43,22 @@ public: VMFace(VMFace const&) = delete; VMFace& operator=(VMFace const&) = delete; + /// Execute EVM code by VM. + /// + /// @param _out Expected output + void exec(u256& io_gas, ExtVMFace& _ext, bytesRef _out, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) + { + execImpl(io_gas, _ext, _onOp, _steps).copyTo(_out); + } + + /// The same as above but returns a copy of full output. bytes exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) { return execImpl(io_gas, _ext, _onOp, _steps).toVector(); } - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) = 0; + /// VM implementation + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) = 0; }; } diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index b011e0cec..2644a8c65 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -130,7 +130,9 @@ void MixClient::executeTransaction(Transaction const& _t, State& _state, bool _c State execState = _state; execState.addBalance(t.sender(), t.gas() * t.gasPrice()); //give it enough balance for gas estimation + eth::ExecutionResult er; Executive execution(execState, lastHashes, 0); + execution.setResultRef(er); execution.initialize(t); execution.execute(); std::vector machineStates; @@ -186,7 +188,6 @@ void MixClient::executeTransaction(Transaction const& _t, State& _state, bool _c execution.go(onOp); execution.finalize(); - dev::eth::ExecutionResult er = execution.executionResult(); switch (er.excepted) { @@ -213,7 +214,7 @@ void MixClient::executeTransaction(Transaction const& _t, State& _state, bool _c }; ExecutionResult d; - d.result = execution.executionResult(); + d.result = er; d.machineStates = machineStates; d.executionCode = std::move(codes); d.transactionData = std::move(data); diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index 0f80e9f59..ea277421a 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -135,6 +135,8 @@ protected: { m_state.addBalance(m_sender, _value); // just in case eth::Executive executive(m_state, eth::LastHashes(), 0); + eth::ExecutionResult res; + executive.setResultRef(res); eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); bytes transactionRLP = t.rlp(); @@ -161,7 +163,7 @@ protected: m_state.noteSending(m_sender); executive.finalize(); m_gasUsed = executive.gasUsed(); - m_output = executive.out().toVector(); + m_output = std::move(res.output); // FIXME: Looks like Framework needs ExecutiveResult embedded m_logs = executive.logs(); } From 9891028cb8bed067559c2b44d9d4a1a0180676f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 May 2015 09:49:48 +0200 Subject: [PATCH 03/11] Improve output handling in precompiled contracts. --- libethereum/Executive.cpp | 3 +- libethereum/Precompiled.cpp | 56 ++++++++++++++++++++----------------- libethereum/Precompiled.h | 2 +- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index fc8ba22a3..e33195631 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -140,8 +140,7 @@ bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address co else { m_gas = (u256)(_p.gas - g); - auto out = it->second.exec(_p.data); // FIXME: Avoid double copy - bytesConstRef{&out}.copyTo(_p.out); + it->second.exec(_p.data, _p.out); } } else diff --git a/libethereum/Precompiled.cpp b/libethereum/Precompiled.cpp index 0e80949fe..ab0d4d83f 100644 --- a/libethereum/Precompiled.cpp +++ b/libethereum/Precompiled.cpp @@ -31,7 +31,10 @@ using namespace std; using namespace dev; using namespace dev::eth; -static bytes ecrecoverCode(bytesConstRef _in) +namespace +{ + +void ecrecoverCode(bytesConstRef _in, bytesRef _out) { struct inType { @@ -44,47 +47,48 @@ static bytes ecrecoverCode(bytesConstRef _in) memcpy(&in, _in.data(), min(_in.size(), sizeof(in))); h256 ret; - - if ((u256)in.v > 28) - return ret.asBytes(); - SignatureStruct sig(in.r, in.s, (byte)((int)(u256)in.v - 27)); - if (!sig.isValid()) - return ret.asBytes(); - - try + if ((u256)in.v <= 28) { - ret = dev::sha3(recover(sig, in.hash)); + SignatureStruct sig(in.r, in.s, (byte)((int)(u256)in.v - 27)); + if (sig.isValid()) + { + try + { + ret = sha3(recover(sig, in.hash)); + } + catch (...) {} + } } - catch (...) {} memset(ret.data(), 0, 12); - return ret.asBytes(); + ret.ref().copyTo(_out); } -static bytes sha256Code(bytesConstRef _in) +void sha256Code(bytesConstRef _in, bytesRef _out) { - return sha256(_in).asBytes(); + sha256(_in).ref().copyTo(_out); } -static bytes ripemd160Code(bytesConstRef _in) +void ripemd160Code(bytesConstRef _in, bytesRef _out) { - return h256(ripemd160(_in), h256::AlignRight).asBytes(); + h256(ripemd160(_in), h256::AlignRight).ref().copyTo(_out); } -static bytes identityCode(bytesConstRef _in) +void identityCode(bytesConstRef _in, bytesRef _out) { - return _in.toBytes(); + _in.copyTo(_out); } -static const std::unordered_map c_precompiled = -{ - { 1, { [](bytesConstRef) -> bigint { return c_ecrecoverGas; }, ecrecoverCode }}, - { 2, { [](bytesConstRef i) -> bigint { return c_sha256Gas + (i.size() + 31) / 32 * c_sha256WordGas; }, sha256Code }}, - { 3, { [](bytesConstRef i) -> bigint { return c_ripemd160Gas + (i.size() + 31) / 32 * c_ripemd160WordGas; }, ripemd160Code }}, - { 4, { [](bytesConstRef i) -> bigint { return c_identityGas + (i.size() + 31) / 32 * c_identityWordGas; }, identityCode }} -}; +} std::unordered_map const& dev::eth::precompiled() { - return c_precompiled; + static const std::unordered_map s_precompiled = + { + { 1, { [](bytesConstRef) -> bigint { return c_ecrecoverGas; }, ecrecoverCode }}, + { 2, { [](bytesConstRef i) -> bigint { return c_sha256Gas + (i.size() + 31) / 32 * c_sha256WordGas; }, sha256Code }}, + { 3, { [](bytesConstRef i) -> bigint { return c_ripemd160Gas + (i.size() + 31) / 32 * c_ripemd160WordGas; }, ripemd160Code }}, + { 4, { [](bytesConstRef i) -> bigint { return c_identityGas + (i.size() + 31) / 32 * c_identityWordGas; }, identityCode }} + }; + return s_precompiled; } diff --git a/libethereum/Precompiled.h b/libethereum/Precompiled.h index bded27386..b50e51ecd 100644 --- a/libethereum/Precompiled.h +++ b/libethereum/Precompiled.h @@ -34,7 +34,7 @@ namespace eth struct PrecompiledAddress { std::function gas; - std::function exec; + std::function exec; }; /// Info on precompiled contract accounts baked into the protocol. From f042abc3e71af41bd4ce6ca269ceb099ebf9a714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 May 2015 10:29:16 +0200 Subject: [PATCH 04/11] Kill steps limit option in VM. --- evmjit/libevmjit-cpp/JitVM.cpp | 4 ++-- evmjit/libevmjit-cpp/JitVM.h | 2 +- libethereum/Executive.cpp | 4 ---- libevm/SmartVM.cpp | 4 ++-- libevm/SmartVM.h | 2 +- libevm/VM.cpp | 9 +++------ libevm/VM.h | 2 +- libevm/VMFace.h | 11 +++++------ 8 files changed, 15 insertions(+), 23 deletions(-) diff --git a/evmjit/libevmjit-cpp/JitVM.cpp b/evmjit/libevmjit-cpp/JitVM.cpp index ac8df545f..0d6a6e00a 100644 --- a/evmjit/libevmjit-cpp/JitVM.cpp +++ b/evmjit/libevmjit-cpp/JitVM.cpp @@ -18,7 +18,7 @@ namespace eth extern "C" void env_sload(); // fake declaration for linker symbol stripping workaround, see a call below -bytesConstRef JitVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _step) +bytesConstRef JitVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) { using namespace jit; @@ -33,7 +33,7 @@ bytesConstRef JitVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _on { cwarn << "Execution rejected by EVM JIT (gas limit: " << io_gas << "), executing with interpreter"; m_fallbackVM = VMFactory::create(VMKind::Interpreter); - return m_fallbackVM->execImpl(io_gas, _ext, _onOp, _step); + return m_fallbackVM->execImpl(io_gas, _ext, _onOp); } m_data.gas = static_cast(io_gas); diff --git a/evmjit/libevmjit-cpp/JitVM.h b/evmjit/libevmjit-cpp/JitVM.h index 03c3c8880..e97abd83b 100644 --- a/evmjit/libevmjit-cpp/JitVM.h +++ b/evmjit/libevmjit-cpp/JitVM.h @@ -11,7 +11,7 @@ namespace eth class JitVM: public VMFace { public: - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) override final; private: jit::RuntimeData m_data; diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp index e33195631..f3e45cbef 100644 --- a/libethereum/Executive.cpp +++ b/libethereum/Executive.cpp @@ -246,10 +246,6 @@ bool Executive::go(OnOpFunc const& _onOp) vm->exec(m_gas, *m_ext, m_outRef, _onOp); // take only expected output } } - catch (StepsDone const&) - { - return false; - } catch (VMException const& _e) { clog(StateSafeExceptions) << "Safe VM Exception. " << diagnostic_information(_e); diff --git a/libevm/SmartVM.cpp b/libevm/SmartVM.cpp index 27263fc2d..12f2f7078 100644 --- a/libevm/SmartVM.cpp +++ b/libevm/SmartVM.cpp @@ -41,7 +41,7 @@ namespace } } -bytesConstRef SmartVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) +bytesConstRef SmartVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) { auto codeHash = sha3(_ext.code); auto vmKind = VMKind::Interpreter; // default VM @@ -68,7 +68,7 @@ bytesConstRef SmartVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _ // TODO: Selected VM must be kept only because it returns reference to its internal memory. // VM implementations should be stateless, without escaping memory reference. m_selectedVM = VMFactory::create(vmKind); - return m_selectedVM->execImpl(io_gas, _ext, _onOp, _steps); + return m_selectedVM->execImpl(io_gas, _ext, _onOp); } } diff --git a/libevm/SmartVM.h b/libevm/SmartVM.h index 73d515f29..fce7be21e 100644 --- a/libevm/SmartVM.h +++ b/libevm/SmartVM.h @@ -31,7 +31,7 @@ namespace eth class SmartVM: public VMFace { public: - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) override final; private: std::unique_ptr m_selectedVM; diff --git a/libevm/VM.cpp b/libevm/VM.cpp index 3a4124d67..dedb30220 100644 --- a/libevm/VM.cpp +++ b/libevm/VM.cpp @@ -45,7 +45,7 @@ static array metrics() return s_ret; } -bytesConstRef VM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) +bytesConstRef VM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) { // Reset leftovers from possible previous run m_curPC = 0; @@ -73,8 +73,7 @@ bytesConstRef VM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, i += _ext.code[i] - (unsigned)Instruction::PUSH1 + 1; } u256 nextPC = m_curPC + 1; - auto osteps = _steps; - for (bool stopped = false; !stopped && _steps--; m_curPC = nextPC, nextPC = m_curPC + 1) + for (uint64_t steps = 0; true; m_curPC = nextPC, nextPC = m_curPC + 1, ++steps) { // INSTRUCTION... Instruction inst = (Instruction)_ext.getCode(m_curPC); @@ -97,7 +96,7 @@ bytesConstRef VM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, auto onOperation = [&]() { if (_onOp) - _onOp(osteps - _steps - 1, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, io_gas, this, &_ext); + _onOp(steps, inst, newTempSize > m_temp.size() ? (newTempSize - m_temp.size()) / 32 : bigint(0), runGas, io_gas, this, &_ext); }; switch (inst) @@ -670,7 +669,5 @@ bytesConstRef VM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, } } - if (_steps == (uint64_t)-1) - BOOST_THROW_EXCEPTION(StepsDone()); return bytesConstRef(); } diff --git a/libevm/VM.h b/libevm/VM.h index da3a843ed..d87f7e353 100644 --- a/libevm/VM.h +++ b/libevm/VM.h @@ -52,7 +52,7 @@ inline u256 fromAddress(Address _a) class VM: public VMFace { public: - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) override final; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) override final; u256 curPC() const { return m_curPC; } diff --git a/libevm/VMFace.h b/libevm/VMFace.h index 7a344a072..2a9ed808e 100644 --- a/libevm/VMFace.h +++ b/libevm/VMFace.h @@ -26,7 +26,6 @@ namespace eth { struct VMException: virtual Exception {}; -struct StepsDone: virtual VMException {}; struct BreakPointHit: virtual VMException {}; struct BadInstruction: virtual VMException {}; struct BadJumpDestination: virtual VMException {}; @@ -46,19 +45,19 @@ public: /// Execute EVM code by VM. /// /// @param _out Expected output - void exec(u256& io_gas, ExtVMFace& _ext, bytesRef _out, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) + void exec(u256& io_gas, ExtVMFace& _ext, bytesRef _out, OnOpFunc const& _onOp = {}) { - execImpl(io_gas, _ext, _onOp, _steps).copyTo(_out); + execImpl(io_gas, _ext, _onOp).copyTo(_out); } /// The same as above but returns a copy of full output. - bytes exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) + bytes exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp = {}) { - return execImpl(io_gas, _ext, _onOp, _steps).toVector(); + return execImpl(io_gas, _ext, _onOp).toVector(); } /// VM implementation - virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps) = 0; + virtual bytesConstRef execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) = 0; }; } From a10f7d5dd2ac282263f8cb0c5e6b9756525a2f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 28 May 2015 11:56:28 +0200 Subject: [PATCH 05/11] Rename Executive::setResultRef -> collectResult. --- libethereum/Client.cpp | 2 +- libethereum/Executive.h | 2 +- libethereum/State.cpp | 2 +- mix/MixClient.cpp | 2 +- test/libsolidity/solidityExecutionFramework.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index d9813c725..14b50e9ba 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -440,7 +440,7 @@ ExecutionResult Client::call(Address _dest, bytes const& _data, u256 _gas, u256 temp = m_postMine; temp.addBalance(_from, _value + _gasPrice * _gas); Executive e(temp, LastHashes(), 0); - e.setResultRef(ret); + e.collectResult(ret); if (!e.call(_dest, _from, _value, _gasPrice, &_data, _gas)) e.go(); e.finalize(); diff --git a/libethereum/Executive.h b/libethereum/Executive.h index 8b2906b9b..668e4b197 100644 --- a/libethereum/Executive.h +++ b/libethereum/Executive.h @@ -115,7 +115,7 @@ public: bool excepted() const { return m_excepted != TransactionException::None; } /// Collect execution results in the result storage provided. - void setResultRef(ExecutionResult& _res) { m_res = &_res; } + void collectResult(ExecutionResult& _res) { m_res = &_res; } private: State& m_s; ///< The state to which this operation/transaction is applied. diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 277e9a649..d96465dd9 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1126,7 +1126,7 @@ ExecutionResult State::execute(LastHashes const& _lh, Transaction const& _t, Per // transaction is bad in any way. Executive e(*this, _lh, 0); ExecutionResult res; - e.setResultRef(res); + e.collectResult(res); e.initialize(_t); // Uncommitting is a non-trivial operation - only do it once we've verified as much of the diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index 2644a8c65..634cf278c 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -132,7 +132,7 @@ void MixClient::executeTransaction(Transaction const& _t, State& _state, bool _c execState.addBalance(t.sender(), t.gas() * t.gasPrice()); //give it enough balance for gas estimation eth::ExecutionResult er; Executive execution(execState, lastHashes, 0); - execution.setResultRef(er); + execution.collectResult(er); execution.initialize(t); execution.execute(); std::vector machineStates; diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index ea277421a..8b8d851a2 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -136,7 +136,7 @@ protected: m_state.addBalance(m_sender, _value); // just in case eth::Executive executive(m_state, eth::LastHashes(), 0); eth::ExecutionResult res; - executive.setResultRef(res); + executive.collectResult(res); eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : eth::Transaction(_value, m_gasPrice, m_gas, m_contractAddress, _data, 0, KeyPair::create().sec()); bytes transactionRLP = t.rlp(); From fdb06f08508df1b9358222d0785f3da41e014a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 3 Jun 2015 17:17:41 +0200 Subject: [PATCH 06/11] Style correction. --- libethereum/Precompiled.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libethereum/Precompiled.cpp b/libethereum/Precompiled.cpp index ab0d4d83f..20f89c452 100644 --- a/libethereum/Precompiled.cpp +++ b/libethereum/Precompiled.cpp @@ -83,12 +83,12 @@ void identityCode(bytesConstRef _in, bytesRef _out) std::unordered_map const& dev::eth::precompiled() { - static const std::unordered_map s_precompiled = + static const std::unordered_map c_precompiled = { { 1, { [](bytesConstRef) -> bigint { return c_ecrecoverGas; }, ecrecoverCode }}, { 2, { [](bytesConstRef i) -> bigint { return c_sha256Gas + (i.size() + 31) / 32 * c_sha256WordGas; }, sha256Code }}, { 3, { [](bytesConstRef i) -> bigint { return c_ripemd160Gas + (i.size() + 31) / 32 * c_ripemd160WordGas; }, ripemd160Code }}, { 4, { [](bytesConstRef i) -> bigint { return c_identityGas + (i.size() + 31) / 32 * c_identityWordGas; }, identityCode }} }; - return s_precompiled; + return c_precompiled; } From 3a550d6ac777d6961466e5471faa39dffa0dfa69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 8 Jun 2015 10:27:37 +0200 Subject: [PATCH 07/11] Fix input data validation for ECRECOVER precompiled contract. --- libdevcrypto/Common.cpp | 2 +- libdevcrypto/Common.h | 2 +- libethereum/Precompiled.cpp | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libdevcrypto/Common.cpp b/libdevcrypto/Common.cpp index 814f8309e..e68381427 100644 --- a/libdevcrypto/Common.cpp +++ b/libdevcrypto/Common.cpp @@ -37,7 +37,7 @@ using namespace dev::crypto; static Secp256k1 s_secp256k1; -bool dev::SignatureStruct::isValid() const +bool dev::SignatureStruct::isValid() const noexcept { if (v > 1 || r >= h256("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141") || diff --git a/libdevcrypto/Common.h b/libdevcrypto/Common.h index 10bcdd067..7bb51e563 100644 --- a/libdevcrypto/Common.h +++ b/libdevcrypto/Common.h @@ -51,7 +51,7 @@ struct SignatureStruct operator Signature() const { return *(h520 const*)this; } /// @returns true if r,s,v values are valid, otherwise false - bool isValid() const; + bool isValid() const noexcept; h256 r; h256 s; diff --git a/libethereum/Precompiled.cpp b/libethereum/Precompiled.cpp index 20f89c452..0ae9bf3eb 100644 --- a/libethereum/Precompiled.cpp +++ b/libethereum/Precompiled.cpp @@ -47,9 +47,10 @@ void ecrecoverCode(bytesConstRef _in, bytesRef _out) memcpy(&in, _in.data(), min(_in.size(), sizeof(in))); h256 ret; - if ((u256)in.v <= 28) + u256 v = (u256)in.v; + if (v >= 27 && v <= 28) { - SignatureStruct sig(in.r, in.s, (byte)((int)(u256)in.v - 27)); + SignatureStruct sig(in.r, in.s, (byte)((int)v - 27)); if (sig.isValid()) { try From f7ed7ed4defbf89758ed017c59ff2eb3c66f01f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 8 Jun 2015 10:45:42 +0200 Subject: [PATCH 08/11] Rename Executive::collectResult -> setResultRecipient. --- libethereum/Client.cpp | 2 +- libethereum/Executive.h | 2 +- libethereum/State.cpp | 2 +- mix/MixClient.cpp | 2 +- test/libsolidity/solidityExecutionFramework.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 1e01a3a8c..f410e6eca 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -423,7 +423,7 @@ ExecutionResult Client::call(Address _dest, bytes const& _data, u256 _gas, u256 temp = m_postMine; temp.addBalance(_from, _value + _gasPrice * _gas); Executive e(temp, LastHashes(), 0); - e.collectResult(ret); + e.setResultRecipient(ret); if (!e.call(_dest, _from, _value, _gasPrice, &_data, _gas)) e.go(); e.finalize(); diff --git a/libethereum/Executive.h b/libethereum/Executive.h index 400c04298..beeee3331 100644 --- a/libethereum/Executive.h +++ b/libethereum/Executive.h @@ -119,7 +119,7 @@ public: bool excepted() const { return m_excepted != TransactionException::None; } /// Collect execution results in the result storage provided. - void collectResult(ExecutionResult& _res) { m_res = &_res; } + void setResultRecipient(ExecutionResult& _res) { m_res = &_res; } private: State& m_s; ///< The state to which this operation/transaction is applied. diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 5d88405ac..20f080fdc 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -1190,7 +1190,7 @@ ExecutionResult State::execute(LastHashes const& _lh, Transaction const& _t, Per // transaction is bad in any way. Executive e(*this, _lh, 0); ExecutionResult res; - e.collectResult(res); + e.setResultRecipient(res); e.initialize(_t); // Uncommitting is a non-trivial operation - only do it once we've verified as much of the diff --git a/mix/MixClient.cpp b/mix/MixClient.cpp index 32875f572..7c0498ef0 100644 --- a/mix/MixClient.cpp +++ b/mix/MixClient.cpp @@ -132,7 +132,7 @@ void MixClient::executeTransaction(Transaction const& _t, State& _state, bool _c execState.addBalance(t.sender(), t.gas() * t.gasPrice()); //give it enough balance for gas estimation eth::ExecutionResult er; Executive execution(execState, lastHashes, 0); - execution.collectResult(er); + execution.setResultRecipient(er); execution.initialize(t); execution.execute(); std::vector machineStates; diff --git a/test/libsolidity/solidityExecutionFramework.h b/test/libsolidity/solidityExecutionFramework.h index 133b23604..a36e0b4ed 100644 --- a/test/libsolidity/solidityExecutionFramework.h +++ b/test/libsolidity/solidityExecutionFramework.h @@ -149,7 +149,7 @@ protected: m_state.addBalance(m_sender, _value); // just in case eth::Executive executive(m_state, eth::LastHashes(), 0); eth::ExecutionResult res; - executive.collectResult(res); + executive.setResultRecipient(res); eth::Transaction t = _isCreation ? eth::Transaction(_value, m_gasPrice, m_gas, _data, 0, KeyPair::create().sec()) : From e81fc1e68fd8fe233b1b0a88c64bf58d3677c9aa Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 8 Jun 2015 23:28:46 +0900 Subject: [PATCH 09/11] Minor debug alterations/fixes for blockchain downloading to make it play marginally better with the braindead Go strategy. Import key without address. --- alethzero/MainWin.cpp | 8 +++--- ethkey/KeyAux.h | 39 ++++++++++++++++++++++++++- libdevcore/CommonData.cpp | 52 +++++++++++++++++------------------- libdevcore/CommonData.h | 2 +- libdevcrypto/SecretStore.cpp | 2 +- libethcore/KeyManager.cpp | 23 +++++++++------- libethcore/KeyManager.h | 11 +++++--- libethereum/CommonNet.h | 6 +++-- libethereum/EthereumHost.cpp | 4 ++- libethereum/EthereumPeer.cpp | 4 +-- 10 files changed, 98 insertions(+), 53 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 10038a80e..93054fd67 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -2114,14 +2114,14 @@ void Main::on_reencryptKey_triggered() auto pw = [&](){ auto p = QInputDialog::getText(this, "Re-Encrypt Key", "Enter the original password for this key.\nHint: " + QString::fromStdString(m_keyManager.hint(a)), QLineEdit::Password, QString()).toStdString(); if (p.empty()) - throw UnknownPassword(); + throw PasswordUnknown(); return p; }; while (!(password.empty() ? m_keyManager.recode(a, SemanticPassword::Master, pw, kdf) : m_keyManager.recode(a, password, hint, pw, kdf))) if (QMessageBox::question(this, "Re-Encrypt Key", "Password given is incorrect. Would you like to try again?", QMessageBox::Retry, QMessageBox::Cancel) == QMessageBox::Cancel) return; } - catch (UnknownPassword&) {} + catch (PasswordUnknown&) {} } } @@ -2137,13 +2137,13 @@ void Main::on_reencryptAll_triggered() while (!m_keyManager.recode(a, SemanticPassword::Existing, [&](){ auto p = QInputDialog::getText(nullptr, "Re-Encrypt Key", QString("Enter the original password for key %1.\nHint: %2").arg(QString::fromStdString(pretty(a))).arg(QString::fromStdString(m_keyManager.hint(a))), QLineEdit::Password, QString()).toStdString(); if (p.empty()) - throw UnknownPassword(); + throw PasswordUnknown(); return p; }, (KDF)kdfs.indexOf(kdf))) if (QMessageBox::question(this, "Re-Encrypt Key", "Password given is incorrect. Would you like to try again?", QMessageBox::Retry, QMessageBox::Cancel) == QMessageBox::Cancel) return; } - catch (UnknownPassword&) {} + catch (PasswordUnknown&) {} } void Main::on_go_triggered() diff --git a/ethkey/KeyAux.h b/ethkey/KeyAux.h index 639e1d4f4..d2ec13b2a 100644 --- a/ethkey/KeyAux.h +++ b/ethkey/KeyAux.h @@ -102,6 +102,7 @@ public: List, New, Import, + ImportWithAddress, Export, Recode, Kill @@ -159,6 +160,13 @@ public: m_inputs = strings(1, argv[++i]); m_name = argv[++i]; } + else if ((arg == "-i" || arg == "--import-with-address") && i + 3 < argc) + { + m_mode = OperationMode::ImportWithAddress; + m_inputs = strings(1, argv[++i]); + m_address = Address(argv[++i]); + m_name = argv[++i]; + } else if (arg == "--export") m_mode = OperationMode::Export; else if (arg == "--recode") @@ -314,6 +322,33 @@ public: cout << " ICAP: " << ICAP(k.address()).encoded() << endl; break; } + case OperationMode::ImportWithAddress: + { + string const& i = m_inputs[0]; + h128 u; + bytes b; + b = fromHex(i); + if (b.size() != 32) + { + std::string s = contentsString(i); + b = fromHex(s); + if (b.size() != 32) + u = wallet.store().importKey(i); + } + if (!u && b.size() == 32) + u = wallet.store().importSecret(b, lockPassword(toAddress(Secret(b)).abridged())); + if (!u) + { + cerr << "Cannot import " << i << " not a file or secret." << endl; + break; + } + wallet.importExisting(u, m_name, m_address); + cout << "Successfully imported " << i << ":" << endl; + cout << " Name: " << m_name << endl; + cout << " Address: " << m_address << endl; + cout << " UUID: " << toUUID(u) << endl; + break; + } case OperationMode::List: { vector bare; @@ -369,6 +404,7 @@ public: << " -l,--list List all keys available in wallet." << endl << " -n,--new Create a new key with given name and add it in the wallet." << endl << " -i,--import [||] Import keys from given source and place in wallet." << endl + << " --import-with-address [||]
Import keys from given source with given address and place in wallet." << endl << " -e,--export [
| , ... ] Export given keys." << endl << " -r,--recode [
|| , ... ] Decrypt and re-encrypt given keys." << endl << "Wallet configuration:" << endl @@ -418,8 +454,9 @@ private: string m_lockHint; bool m_icap = true; - /// Creating + /// Creating/importing string m_name; + Address m_address; /// Importing strings m_inputs; diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp index f8d8c172f..2d6333f26 100644 --- a/libdevcore/CommonData.cpp +++ b/libdevcore/CommonData.cpp @@ -67,7 +67,7 @@ std::string dev::randomWord() return ret; } -int dev::fromHex(char _i) +int dev::fromHex(char _i, WhenError _throw) { if (_i >= '0' && _i <= '9') return _i - '0'; @@ -75,7 +75,10 @@ int dev::fromHex(char _i) return _i - 'a' + 10; if (_i >= 'A' && _i <= 'F') return _i - 'A' + 10; - BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i)); + if (_throw == WhenError::Throw) + BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i)); + else + return -1; } bytes dev::fromHex(std::string const& _s, WhenError _throw) @@ -85,33 +88,26 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw) ret.reserve((_s.size() - s + 1) / 2); if (_s.size() % 2) - try - { - ret.push_back(fromHex(_s[s++])); - } - catch (...) - { - ret.push_back(0); - // msvc does not support it -#ifndef BOOST_NO_EXCEPTIONS - cwarn << boost::current_exception_diagnostic_information(); -#endif - if (_throw == WhenError::Throw) - throw; - } + { + int h = fromHex(_s[s++], WhenError::DontThrow); + if (h != -1) + ret.push_back(h); + else if (_throw == WhenError::Throw) + throw BadHexCharacter(); + else + return bytes(); + } for (unsigned i = s; i < _s.size(); i += 2) - try - { - ret.push_back((byte)(fromHex(_s[i]) * 16 + fromHex(_s[i + 1]))); - } - catch (...){ - ret.push_back(0); -#ifndef BOOST_NO_EXCEPTIONS - cwarn << boost::current_exception_diagnostic_information(); -#endif - if (_throw == WhenError::Throw) - throw; - } + { + int h = fromHex(_s[i], WhenError::DontThrow); + int l = fromHex(_s[i + 1], WhenError::DontThrow); + if (h != -1 && l != -1) + ret.push_back((byte)(h * 16 + l)); + else if (_throw == WhenError::Throw) + throw BadHexCharacter(); + else + return bytes(); + } return ret; } diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index e1d8d7bdb..ddc00e09f 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -61,7 +61,7 @@ std::string toHex(_T const& _data, int _w = 2, HexPrefix _prefix = HexPrefix::Do /// Converts a (printable) ASCII hex character into the correspnding integer value. /// @example fromHex('A') == 10 && fromHex('f') == 15 && fromHex('5') == 5 -int fromHex(char _i); +int fromHex(char _i, WhenError _throw); /// Converts a (printable) ASCII hex string into the corresponding byte stream. /// @example fromHex("41626261") == asBytes("Abba") diff --git a/libdevcrypto/SecretStore.cpp b/libdevcrypto/SecretStore.cpp index 11ff98bf6..b9d4ccfc6 100644 --- a/libdevcrypto/SecretStore.cpp +++ b/libdevcrypto/SecretStore.cpp @@ -164,7 +164,7 @@ void SecretStore::load(std::string const& _keysPath) h128 SecretStore::readKey(std::string const& _file, bool _deleteFile) { - cdebug << "Reading" << _file; + cnote << "Reading" << _file; return readKeyContent(contentsString(_file), _deleteFile ? _file : string()); } diff --git a/libethcore/KeyManager.cpp b/libethcore/KeyManager.cpp index 182201301..4430a588e 100644 --- a/libethcore/KeyManager.cpp +++ b/libethcore/KeyManager.cpp @@ -89,18 +89,18 @@ bool KeyManager::load(std::string const& _pass) for (auto const& i: s[1]) { m_keyInfo[m_addrLookup[(Address)i[0]] = (h128)i[1]] = KeyInfo((h256)i[2], (std::string)i[3]); - cdebug << toString((Address)i[0]) << toString((h128)i[1]) << toString((h256)i[2]) << (std::string)i[3]; +// cdebug << toString((Address)i[0]) << toString((h128)i[1]) << toString((h256)i[2]) << (std::string)i[3]; } for (auto const& i: s[2]) m_passwordInfo[(h256)i[0]] = (std::string)i[1]; m_password = (string)s[3]; } - cdebug << hashPassword(m_password) << toHex(m_password); +// cdebug << hashPassword(m_password) << toHex(m_password); m_cachedPasswords[hashPassword(m_password)] = m_password; - cdebug << hashPassword(asString(m_key.ref())) << m_key.hex(); +// cdebug << hashPassword(asString(m_key.ref())) << m_key.hex(); m_cachedPasswords[hashPassword(asString(m_key.ref()))] = asString(m_key.ref()); - cdebug << hashPassword(_pass) << _pass; +// cdebug << hashPassword(_pass) << _pass; m_cachedPasswords[m_master = hashPassword(_pass)] = _pass; return true; } @@ -141,7 +141,7 @@ std::string KeyManager::getPassword(h256 const& _passHash, function const& _pass = DontKnowThrow) const; Secret secret(h128 const& _uuid, std::function const& _pass = DontKnowThrow) const; diff --git a/libethereum/CommonNet.h b/libethereum/CommonNet.h index a2f4a2e7c..8cf2647cf 100644 --- a/libethereum/CommonNet.h +++ b/libethereum/CommonNet.h @@ -38,14 +38,16 @@ namespace eth #if ETH_DEBUG static const unsigned c_maxHashes = 2048; ///< Maximum number of hashes BlockHashes will ever send. -static const unsigned c_maxHashesAsk = 2048; ///< Maximum number of hashes GetBlockHashes will ever ask for. +static const unsigned c_maxHashesAsk = 256; ///< Maximum number of hashes GetBlockHashes will ever ask for. static const unsigned c_maxBlocks = 128; ///< Maximum number of blocks Blocks will ever send. -static const unsigned c_maxBlocksAsk = 128; ///< Maximum number of blocks we ask to receive in Blocks (when using GetChain). +static const unsigned c_maxBlocksAsk = 8; ///< Maximum number of blocks we ask to receive in Blocks (when using GetChain). +static const unsigned c_maxPayload = 262144; ///< Maximum size of packet for us to send. #else static const unsigned c_maxHashes = 2048; ///< Maximum number of hashes BlockHashes will ever send. static const unsigned c_maxHashesAsk = 2048; ///< Maximum number of hashes GetBlockHashes will ever ask for. static const unsigned c_maxBlocks = 128; ///< Maximum number of blocks Blocks will ever send. static const unsigned c_maxBlocksAsk = 128; ///< Maximum number of blocks we ask to receive in Blocks (when using GetChain). +static const unsigned c_maxPayload = 262144; ///< Maximum size of packet for us to send. #endif class BlockChain; diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp index 3965a214e..7c9b730ea 100644 --- a/libethereum/EthereumHost.cpp +++ b/libethereum/EthereumHost.cpp @@ -253,7 +253,9 @@ void EthereumHost::onPeerStatus(EthereumPeer* _peer) if (_peer->m_protocolVersion != protocolVersion()) estimatePeerHashes(_peer); else if (_peer->m_latestBlockNumber > m_chain.number()) - _peer->m_expectedHashes = (unsigned)_peer->m_latestBlockNumber - m_chain.number(); + _peer->m_expectedHashes = (unsigned)_peer->m_latestBlockNumber - m_chain.number() + 1000; + else + _peer->m_expectedHashes = 1000; if (m_hashMan.chainSize() < _peer->m_expectedHashes) m_hashMan.resetToRange(m_chain.number() + 1, _peer->m_expectedHashes); continueSync(_peer); diff --git a/libethereum/EthereumPeer.cpp b/libethereum/EthereumPeer.cpp index d6b0b50c3..a332e5b93 100644 --- a/libethereum/EthereumPeer.cpp +++ b/libethereum/EthereumPeer.cpp @@ -263,7 +263,7 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r) // return the requested blocks. bytes rlp; unsigned n = 0; - for (unsigned i = 0; i < min(count, c_maxBlocks); ++i) + for (unsigned i = 0; i < min(count, c_maxBlocks) && rlp.size() < c_maxPayload; ++i) { auto h = _r[i].toHash(); if (host()->chain().isKnown(h)) @@ -286,7 +286,7 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r) case BlocksPacket: { if (m_asking != Asking::Blocks) - clog(NetWarn) << "Peer giving us blocks when we didn't ask for them."; + clog(NetImpolite) << "Peer giving us blocks when we didn't ask for them."; else { setAsking(Asking::Nothing); From 1f9330a29c3791d4a4ae5e6e0561e4e6f8cd97c1 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 8 Jun 2015 23:54:16 +0900 Subject: [PATCH 10/11] Avoid divide by zero. --- alethzero/Transact.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alethzero/Transact.cpp b/alethzero/Transact.cpp index e61092f33..fd466e475 100644 --- a/alethzero/Transact.cpp +++ b/alethzero/Transact.cpp @@ -360,7 +360,7 @@ void Transact::rejigData() return; } else - gasNeeded = (qint64)min(ethereum()->gasLimitRemaining(), ((b - value()) / gasPrice())); + gasNeeded = (qint64)min(ethereum()->gasLimitRemaining(), ((b - value()) / max(gasPrice(), 1))); // Dry-run execution to determine gas requirement and any execution errors Address to; From 3589c73b75937a4cba202e872d7b0269f035448a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 8 Jun 2015 23:54:42 +0900 Subject: [PATCH 11/11] Version bump. --- libdevcore/Common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libdevcore/Common.cpp b/libdevcore/Common.cpp index cb9b94de8..3dc3fd280 100644 --- a/libdevcore/Common.cpp +++ b/libdevcore/Common.cpp @@ -28,7 +28,7 @@ using namespace dev; namespace dev { -char const* Version = "0.9.23"; +char const* Version = "0.9.24"; const u256 UndefinedU256 = ~(u256)0;