corrected CALLSTATELESS test, style fixes, code length independent call in FakeExtVM

This commit is contained in:
Christoph Jentzsch 2014-10-01 13:59:07 +02:00
parent 244b787111
commit 4d7a55f8e2
3 changed files with 30 additions and 31 deletions

34
vm.cpp
View File

@ -22,7 +22,7 @@
#include "vm.h" #include "vm.h"
//#define FILL_TESTS #define FILL_TESTS
using namespace std; using namespace std;
using namespace json_spirit; using namespace json_spirit;
@ -30,12 +30,15 @@ using namespace dev;
using namespace dev::eth; using namespace dev::eth;
using namespace dev::test; using namespace dev::test;
FakeExtVM::FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock):
ExtVMFace(Address(), Address(), Address(), 0, 1, bytesConstRef(), bytesConstRef(), _previousBlock, _currentBlock) {}
h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFunc) h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFunc)
{ {
m_s.noteSending(myAddress); m_s.noteSending(myAddress);
m_ms.internal.resize(m_ms.internal.size() + 1); m_ms.internal.resize(m_ms.internal.size() + 1);
auto ret = m_s.create(myAddress, _endowment, gasPrice, _gas, _init, origin, &suicides, &posts, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1); auto ret = m_s.create(myAddress, _endowment, gasPrice, _gas, _init, origin, &suicides, &posts, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1);
if ( !m_ms.internal.back().from) if (!m_ms.internal.back().from)
m_ms.internal.pop_back(); m_ms.internal.pop_back();
if (get<0>(addresses[myAddress]) >= _endowment) if (get<0>(addresses[myAddress]) >= _endowment)
@ -58,18 +61,14 @@ h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFun
bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, OnOpFunc, Address, Address) bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, OnOpFunc, Address, Address)
{ {
string codeOf_receiveAddress = toHex(get<3>(addresses[_receiveAddress]) ); string codeOf_receiveAddress = toHex(get<3>(addresses[_receiveAddress]) );
string sizeOfCode = toHex(toCompactBigEndian(codeOf_receiveAddress.size())); string sizeOfCode = toHex(toCompactBigEndian((codeOf_receiveAddress.size()+1)/2));
if (codeOf_receiveAddress.size() > 255) if (codeOf_receiveAddress.size())
{ {
cnote << "codesize too large for FakeExtVM\n"; // create init code that returns given contract code
return false; string initStringHex = "{ (CODECOPY 0 (- (CODESIZE) 0x" + sizeOfCode + " ) 0x" + sizeOfCode + ") (RETURN 0 0x" + sizeOfCode +")}";
} bytes initBytes = compileLLL(initStringHex, true, NULL);
else if (codeOf_receiveAddress.size()) initBytes += fromHex(codeOf_receiveAddress);
{
string initStringHex = "0x60" + sizeOfCode + "80600c6000396000f200" + codeOf_receiveAddress;
bytes initBytes = fromHex(initStringHex.substr(2));
bytesConstRef init(&initBytes); bytesConstRef init(&initBytes);
if (!m_s.addresses().count(_receiveAddress)) if (!m_s.addresses().count(_receiveAddress))
@ -77,7 +76,7 @@ bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data,
m_s.noteSending(myAddress); m_s.noteSending(myAddress);
m_ms.internal.resize(m_ms.internal.size() + 1); m_ms.internal.resize(m_ms.internal.size() + 1);
auto na = m_s.create(myAddress, 0, gasPrice, _gas, init, origin, &suicides, &posts, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1); auto na = m_s.create(myAddress, 0, gasPrice, _gas, init, origin, &suicides, &posts, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1);
if ( !m_ms.internal.back().from) if (!m_ms.internal.back().from)
m_ms.internal.pop_back(); m_ms.internal.pop_back();
if (!m_s.addresses().count(_receiveAddress)) if (!m_s.addresses().count(_receiveAddress))
{ {
@ -130,11 +129,13 @@ void FakeExtVM::setTransaction(Address _caller, u256 _value, u256 _gasPrice, byt
data = &(thisTxData = _data); data = &(thisTxData = _data);
gasPrice = _gasPrice; gasPrice = _gasPrice;
} }
void FakeExtVM::setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, map<u256, u256> const& _storage, bytes const& _code) void FakeExtVM::setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, map<u256, u256> const& _storage, bytes const& _code)
{ {
myAddress = _myAddress; myAddress = _myAddress;
set(myAddress, _myBalance, _myNonce, _storage, _code); set(myAddress, _myBalance, _myNonce, _storage, _code);
} }
void FakeExtVM::set(Address _a, u256 _myBalance, u256 _myNonce, map<u256, u256> const& _storage, bytes const& _code) void FakeExtVM::set(Address _a, u256 _myBalance, u256 _myNonce, map<u256, u256> const& _storage, bytes const& _code)
{ {
get<0>(addresses[_a]) = _myBalance; get<0>(addresses[_a]) = _myBalance;
@ -400,7 +401,6 @@ void FakeExtVM::importCallCreates(mArray& _callcreates)
} }
namespace dev { namespace test { namespace dev { namespace test {
void doTests(json_spirit::mValue& v, bool _fillin) void doTests(json_spirit::mValue& v, bool _fillin)
@ -428,6 +428,7 @@ void doTests(json_spirit::mValue& v, bool _fillin)
fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress)); fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress));
fev.code = &fev.thisTxCode; fev.code = &fev.thisTxCode;
} }
vm.reset(fev.gas); vm.reset(fev.gas);
bytes output; bytes output;
try try
@ -523,7 +524,8 @@ void doTests(json_spirit::mValue& v, bool _fillin)
return json_spirit::write_string(json_spirit::mValue(o), true); return json_spirit::write_string(json_spirit::mValue(o), true);
}*/ }*/
void executeTests(const string& _name){ void executeTests(const string& _name)
{
#ifdef FILL_TESTS #ifdef FILL_TESTS
try try
{ {
@ -603,5 +605,3 @@ BOOST_AUTO_TEST_CASE(vmSystemOperationsTest)
{ {
dev::test::executeTests("vmSystemOperationsTest"); dev::test::executeTests("vmSystemOperationsTest");
} }

9
vm.h
View File

@ -23,9 +23,10 @@ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#pragma once #pragma once
#include <libethereum/State.h>
#include <fstream> #include <fstream>
#include <cstdint> #include <cstdint>
#include <boost/test/unit_test.hpp>
#include "JsonSpiritHeaders.h"
#include <libdevcore/Log.h> #include <libdevcore/Log.h>
#include <libevmface/Instruction.h> #include <libevmface/Instruction.h>
#include <libevm/ExtVMFace.h> #include <libevm/ExtVMFace.h>
@ -33,8 +34,7 @@ along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
#include <liblll/Compiler.h> #include <liblll/Compiler.h>
#include <libethereum/Transaction.h> #include <libethereum/Transaction.h>
#include <libethereum/ExtVM.h> #include <libethereum/ExtVM.h>
#include "JsonSpiritHeaders.h" #include <libethereum/State.h>
#include <boost/test/unit_test.hpp>
namespace dev { namespace test { namespace dev { namespace test {
@ -42,8 +42,7 @@ class FakeExtVM: public eth::ExtVMFace
{ {
public: public:
FakeExtVM() {} FakeExtVM() {}
FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock): FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock);
ExtVMFace(Address(), Address(), Address(), 0, 1, bytesConstRef(), bytesConstRef(), _previousBlock, _currentBlock) {}
u256 store(u256 _n) { return std::get<2>(addresses[myAddress])[_n]; } u256 store(u256 _n) { return std::get<2>(addresses[myAddress])[_n]; }
void setStore(u256 _n, u256 _v) { std::get<2>(addresses[myAddress])[_n] = _v; } void setStore(u256 _n, u256 _v) { std::get<2>(addresses[myAddress])[_n] = _v; }

View File

@ -165,7 +165,7 @@
}, },
"945304eb96065b2a98b57a48a06ae28d285a71b5" : { "945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "23", "balance" : "23",
"code" : "0x603760005560026000f2", "code" : "0x6001600157603760005560026000f2",
"nonce" : "0", "nonce" : "0",
"storage" : { "storage" : {
} }
@ -196,7 +196,7 @@
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000", "balance" : "1000000000000000000",
"nonce" : 0, "nonce" : 0,
"code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) [[ 0 ]] (POST 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 ) }", "code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) (POST 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 ) }",
"storage": {} "storage": {}
}, },
"945304eb96065b2a98b57a48a06ae28d285a71b5" : { "945304eb96065b2a98b57a48a06ae28d285a71b5" : {
@ -232,12 +232,12 @@
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000", "balance" : "1000000000000000000",
"nonce" : 0, "nonce" : 0,
"code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) [[ 0 ]] (CALLSTATELESS 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 0 2 ) }", "code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) [[ 0 ]] (CALLSTATELESS 500 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 0 2 ) }",
"storage": {} "storage": {}
}, },
"945304eb96065b2a98b57a48a06ae28d285a71b5" : { "945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "23", "balance" : "23",
"code" : "0x603760005560026000f2", "code" : "0x6001600157603760005560026000f2",
"nonce" : "0", "nonce" : "0",
"storage" : { "storage" : {
} }
@ -473,7 +473,7 @@
} }
}, },
"CallToNameRegistratorForeverLoop": { "CallRecursiveBomb": {
"env" : { "env" : {
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6",
"currentNumber" : "0", "currentNumber" : "0",
@ -486,13 +486,13 @@
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "20000000", "balance" : "20000000",
"nonce" : 0, "nonce" : 0,
"code" : "{ (MSTORE 0 0x601c80600c6000396000f2006001600056016000576000600060006000601730) (MSTORE 32 0x620f4240f1600157000000000000000000000000000000000000000000000000) [[ 0 ]] (CREATE 10000000 0 40) (CALL 10000000 (SLOAD 0) 9000000 0 0 0 0) }", "code" : "{ (CALL 100000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 0 0 0) }",
"storage": {} "storage": {}
}, },
"945304eb96065b2a98b57a48a06ae28d285a71aa" : { "945304eb96065b2a98b57a48a06ae28d285a71b5" : {
"balance" : "1000000000000000000", "balance" : "1000000000000000000",
"nonce" : 0, "nonce" : 0,
"code" : "{ [[ 0 ]] (+ (SLOAD 0) 1) [[ 1 ]] (CALL 1000000 (ADDRESS) 23 0 0 0 0) }", "code" : "{ [[ 0 ]] (+ (SLOAD 0) 1) [[ 1 ]] (CALL (- (GAS) 224) (ADDRESS) 0 0 0 0 0) } ",
"storage": {} "storage": {}
} }
}, },
@ -718,7 +718,7 @@
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000", "balance" : "1000000000000000000",
"nonce" : 0, "nonce" : 0,
"code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) [[ 0 ]] (POST 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 ) }", "code" : "{ (MSTORE 0 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) (MSTORE 32 0xaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffaa ) (POST 1000000 0x945304eb96065b2a98b57a48a06ae28d285a71b5 23 0 64 ) }",
"storage": {} "storage": {}
}, },
"945304eb96065b2a98b57a48a06ae28d285a71b5" : { "945304eb96065b2a98b57a48a06ae28d285a71b5" : {