Skip to content

Commit

Permalink
State and Account on smartPointers WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Aug 21, 2021
1 parent 6926c17 commit a6241fc
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 134 deletions.
21 changes: 11 additions & 10 deletions retesteth/session/RPCImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ FH32 RPCImpl::eth_sendRawTransaction(BYTES const& _rlp, VALUE const& _secret)
return FH32(result.getCContent());
}

VALUE RPCImpl::eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber)
spVALUE RPCImpl::eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber)
{
try
{
spDataObject response = rpcCall("eth_getTransactionCount", {quote(_address.asString()), quote(_blockNumber.asString())});
(*response).performModifier(mod_valueToCompactEvenHexPrefixed);
if (response->type() == DataType::String)
return VALUE(response);
return VALUE(response->asInt());
return spVALUE(new VALUE(response));
return spVALUE(new VALUE(response->asInt()));
}
catch(std::exception const& _ex)
{
ETH_FAIL_MESSAGE(string("RPC eth_getTransactionCount Exception: ") + _ex.what());
}
return VALUE(0);
return spVALUE(0);
}

VALUE RPCImpl::eth_blockNumber()
Expand All @@ -65,20 +65,21 @@ EthGetBlockBy RPCImpl::eth_getBlockByNumber(VALUE const& _blockNumber, Request _
return EthGetBlockBy(response);
}

BYTES RPCImpl::eth_getCode(FH20 const& _address, VALUE const& _blockNumber)
spBYTES RPCImpl::eth_getCode(FH20 const& _address, VALUE const& _blockNumber)
{
spDataObject const res(rpcCall("eth_getCode", {quote(_address.asString()), quote(_blockNumber.asString())}));
spDataObject res = rpcCall("eth_getCode", {quote(_address.asString()), quote(_blockNumber.asString())});
if (res->asString().empty())
{
ETH_WARNING_TEST("eth_getCode return `` empty string, correct to `0x` empty bytes ", 6);
return BYTES(DataObject("0x"));
return spBYTES(new BYTES(DataObject("0x")));
}
return BYTES(res);
return spBYTES(new BYTES(res));
}

VALUE RPCImpl::eth_getBalance(FH20 const& _address, VALUE const& _blockNumber)
spVALUE RPCImpl::eth_getBalance(FH20 const& _address, VALUE const& _blockNumber)
{
return VALUE(rpcCall("eth_getBalance", {quote(_address.asString()), quote(_blockNumber.asString())}));
auto ret = rpcCall("eth_getBalance", {quote(_address.asString()), quote(_blockNumber.asString())});
return spVALUE(new VALUE(ret));
}


Expand Down
7 changes: 4 additions & 3 deletions retesteth/session/RPCImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class RPCImpl : public SessionInterface

// ETH Methods
FH32 eth_sendRawTransaction(BYTES const& _rlp, VALUE const& _secret) override;
VALUE eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber) override;
VALUE eth_blockNumber() override;
EthGetBlockBy eth_getBlockByHash(FH32 const& _hash, Request _fullObjects) override;
EthGetBlockBy eth_getBlockByNumber(VALUE const& _blockNumber, Request _fullObjects) override;

BYTES eth_getCode(FH20 const& _address, VALUE const& _blockNumber) override;
VALUE eth_getBalance(FH20 const& _address, VALUE const& _blockNumber) override;
// Account functions
spVALUE eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber) override;
spBYTES eth_getCode(FH20 const& _address, VALUE const& _blockNumber) override;
spVALUE eth_getBalance(FH20 const& _address, VALUE const& _blockNumber) override;

// Debug
DebugAccountRange debug_accountRange(
Expand Down
8 changes: 5 additions & 3 deletions retesteth/session/SessionInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ class SessionInterface

// ETH Methods
virtual FH32 eth_sendRawTransaction(BYTES const& _rlp, VALUE const& _secret) = 0;
virtual VALUE eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber) = 0;

virtual VALUE eth_blockNumber() = 0;
virtual EthGetBlockBy eth_getBlockByHash(FH32 const& _blockHash, Request _fullObjects) = 0;
virtual EthGetBlockBy eth_getBlockByNumber(VALUE const& _blockNumber, Request _fullObjects) = 0;
virtual BYTES eth_getCode(FH20 const& _address, VALUE const& _blockNumber) = 0;
virtual VALUE eth_getBalance(FH20 const& _address, VALUE const& _blockNumber) = 0;

// Account functions
virtual spBYTES eth_getCode(FH20 const& _address, VALUE const& _blockNumber) = 0;
virtual spVALUE eth_getBalance(FH20 const& _address, VALUE const& _blockNumber) = 0;
virtual spVALUE eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber) = 0;

// Debug
virtual DebugAccountRange debug_accountRange(
Expand Down
25 changes: 13 additions & 12 deletions retesteth/session/ToolImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ FH32 ToolImpl::eth_sendRawTransaction(BYTES const& _rlp, VALUE const& _secret)
return FH32::zero();
}

VALUE ToolImpl::eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber)
spVALUE ToolImpl::eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber)
{
rpcCall("", {});
ETH_TEST_MESSAGE("\nRequest: eth_getTransactionCount " + _blockNumber.asString() + " " + _address.asString());
TRYCATCHCALL(
VALUE const& nonce = blockchain().blockByNumber(_blockNumber).state()->getAccount(_address).nonce();
ETH_TEST_MESSAGE("Response: eth_getTransactionCount " + nonce.asDecString());
spVALUE nonce (blockchain().blockByNumber(_blockNumber).state()->getAccount(_address).nonce().copy());
ETH_TEST_MESSAGE("Response: eth_getTransactionCount " + nonce->asDecString());
return nonce;
, "eth_getTransactionCount", CallType::FAILEVERYTHING)
return VALUE(0);
return spVALUE(0);
}

VALUE ToolImpl::eth_blockNumber()
Expand Down Expand Up @@ -125,28 +125,29 @@ EthGetBlockBy ToolImpl::eth_getBlockByNumber(VALUE const& _blockNumber, Request
return EthGetBlockBy(DataObject());
}

BYTES ToolImpl::eth_getCode(FH20 const& _address, VALUE const& _blockNumber)
spBYTES ToolImpl::eth_getCode(FH20 const& _address, VALUE const& _blockNumber)
{
rpcCall("", {});
ETH_TEST_MESSAGE("\nRequest: eth_getCode " + _blockNumber.asString() + " " + _address.asString());
TRYCATCHCALL(
BYTES const& code = blockchain().blockByNumber(_blockNumber).state()->getAccount(_address).code();
ETH_TEST_MESSAGE("Response: eth_getCode " + code.asString());
spBYTES code(blockchain().blockByNumber(_blockNumber).state()->getAccount(_address).code().copy());
ETH_TEST_MESSAGE("Response: eth_getCode " + code->asString());
return code;
, "eth_getCode", CallType::FAILEVERYTHING)
return BYTES(DataObject());
return spBYTES(0);
}

VALUE ToolImpl::eth_getBalance(FH20 const& _address, VALUE const& _blockNumber)
spVALUE ToolImpl::eth_getBalance(FH20 const& _address, VALUE const& _blockNumber)
{
rpcCall("", {});
ETH_TEST_MESSAGE("\nRequest: eth_getBalance " + _blockNumber.asString() + " " + _address.asString());
TRYCATCHCALL(
VALUE const& balance = blockchain().blockByNumber(_blockNumber).state()->getAccount(_address).balance();
ETH_TEST_MESSAGE("Response: eth_getBalance " + balance.asDecString());
// Make a copy here because we do not expose the memory of the tool backend
spVALUE balance(blockchain().blockByNumber(_blockNumber).state()->getAccount(_address).balance().copy());
ETH_TEST_MESSAGE("Response: eth_getBalance " + balance->asDecString());
return balance;
, "eth_getBalance", CallType::FAILEVERYTHING)
return VALUE(0);
return spVALUE(0);
}

// Debug
Expand Down
7 changes: 4 additions & 3 deletions retesteth/session/ToolImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class ToolImpl : public SessionInterface

// ETH Methods
FH32 eth_sendRawTransaction(BYTES const& _rlp, VALUE const& _secret) override;
VALUE eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber) override;
VALUE eth_blockNumber() override;
EthGetBlockBy eth_getBlockByHash(FH32 const& _hash, Request _fullObjects) override;
EthGetBlockBy eth_getBlockByNumber(VALUE const& _blockNumber, Request _fullObjects) override;

BYTES eth_getCode(FH20 const& _address, VALUE const& _blockNumber) override;
VALUE eth_getBalance(FH20 const& _address, VALUE const& _blockNumber) override;
// Account functions
spVALUE eth_getTransactionCount(FH20 const& _address, VALUE const& _blockNumber) override;
spBYTES eth_getCode(FH20 const& _address, VALUE const& _blockNumber) override;
spVALUE eth_getBalance(FH20 const& _address, VALUE const& _blockNumber) override;

// Debug
DebugAccountRange debug_accountRange(
Expand Down
2 changes: 1 addition & 1 deletion retesteth/testStructures/basetypes/FH.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct FH : GCP_SPointerBase
{
FH(dev::RLP const& _rlp, size_t _scale);
FH(string const&, size_t _scale);
FH(DataObject const&, size_t _scale);
FH(DataObject const&, size_t _scale); // Does not require to move smart pointer here as this structure changes a lot
FH(dev::bigint const&, size_t _scale);

bool isBigInt() const { return m_bigint; }
Expand Down
2 changes: 1 addition & 1 deletion retesteth/testStructures/basetypes/VALUE.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct VALUE : GCP_SPointerBase
VALUE(dev::RLP const& _rlp);
VALUE(dev::bigint const&);
VALUE(int);
VALUE(DataObject const&);
VALUE(DataObject const&); // Does not require to move smart pointer here as this structure changes a lot
VALUE* copy() const { return new VALUE(m_data); }

bool operator<(int _rhs) const { return m_data < _rhs; }
Expand Down
41 changes: 27 additions & 14 deletions retesteth/testStructures/types/Ethereum/Account.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
#include "Account.h"
#include "State.h"
#include <retesteth/TestHelper.h>
#include <retesteth/testStructures/Common.h>

namespace test
{
namespace teststruct
{
Account::Account(FH20 const& _addr, VALUE const& _balance, VALUE const& _nonce, BYTES const& _code, Storage const& _storage)

// The Account is a part of State class and manages it's data
State::Account::Account(FH20 const& _addr, spVALUE& _balance, spVALUE& _nonce, spBYTES& _code, Storage const& _storage)
{
m_address = spFH20(_addr.copy());
m_balance = spVALUE(_balance.copy());
m_nonce = spVALUE(_nonce.copy());
m_code = spBYTES(_code.copy());
m_balance = _balance;
m_nonce = _nonce;
m_code = _code;
m_storage = spStorage(new Storage(_storage));
if (m_nonce.getCContent() > c_maxNonce)
ETH_ERROR_MESSAGE("Account `" + m_address->asString() + "` requires nonce <= (2**64)-1");

// Prepare output data
m_rawData = spDataObject(new DataObject());
(*m_rawData)["code"] = m_code->asString();
(*m_rawData)["nonce"] = m_nonce->asString();
(*m_rawData)["balance"] = m_balance->asString();
(*m_rawData).atKeyPointer("storage") = m_storage->asDataObject();
}

Account::Account(DataObject const& _data)
State::Account::Account(spDataObject& _data)
{
m_address = spFH20(new FH20(_data.getKey()));
m_balance = spVALUE(new VALUE(_data.atKey("balance")));
m_nonce = spVALUE(new VALUE(_data.atKey("nonce")));
m_code = spBYTES(new BYTES(_data.atKey("code")));
m_storage = spStorage(new Storage(_data.atKey("storage")));
m_rawData = _data;
m_address = spFH20(new FH20(_data->getKey()));
m_balance = spVALUE(new VALUE(_data->atKey("balance")));
m_nonce = spVALUE(new VALUE(_data->atKey("nonce")));
m_code = spBYTES(new BYTES(_data->atKey("code")));
m_storage = spStorage(new Storage(_data->atKey("storage")));
if (m_nonce.getCContent() > c_maxNonce)
ETH_ERROR_MESSAGE("Account `" + m_address->asString() + "` requires nonce <= (2**64)-1");
requireJsonFields(_data, "Account " + _data.getKey(),
requireJsonFields(_data, "Account " + _data->getKey(),
{{"balance", {{DataType::String}, jsonField::Required}},
{"code", {{DataType::String}, jsonField::Required}},
{"nonce", {{DataType::String}, jsonField::Required}},
{"storage", {{DataType::Object}, jsonField::Required}}});
}

spDataObject Account::asDataObject(ExportOrder _order) const
spDataObject const& State::Account::asDataObject(ExportOrder _order) const
{
return m_rawData;
(void) _order;
/*
spDataObject out(new DataObject());
string const& addr = m_address->asString();
(*out)["balance"] = m_balance->asString();
Expand All @@ -49,7 +62,7 @@ spDataObject Account::asDataObject(ExportOrder _order) const
(*out).setKeyPos("nonce", 1);
(*out).setKeyPos("balance", 2);
}
return out;
return out;*/
}

} // namespace teststruct
Expand Down
29 changes: 0 additions & 29 deletions retesteth/testStructures/types/Ethereum/Account.h

This file was deleted.

33 changes: 18 additions & 15 deletions retesteth/testStructures/types/Ethereum/AccountIncomplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@ namespace test
{
namespace teststruct
{
AccountIncomplete::AccountIncomplete(DataObject const& _data)
AccountIncomplete::AccountIncomplete(spDataObject& _data)
{
m_address = spFH20(new FH20(_data.getKey()));
m_shouldNotExist = _data.count("shouldnotexist");
if (_data.count("storage"))
m_storage = spStorage(new Storage(_data.atKey("storage")));
if (_data.count("balance"))
m_balance = spVALUE(new VALUE(_data.atKey("balance")));
if (_data.count("nonce"))
m_rawData = _data;
m_address = spFH20(new FH20(_data->getKey()));
m_shouldNotExist = _data->count("shouldnotexist");
if (_data->count("storage"))
m_storage = spStorage(new Storage(_data->atKey("storage")));
if (_data->count("balance"))
m_balance = spVALUE(new VALUE(_data->atKey("balance")));
if (_data->count("nonce"))
{
m_nonce = spVALUE(new VALUE(_data.atKey("nonce")));
m_nonce = spVALUE(new VALUE(_data->atKey("nonce")));
if (m_nonce.getCContent() > c_maxNonce)
ETH_ERROR_MESSAGE("AccountIncomplete `" + m_address->asString() + "` requires nonce <= (2**64)-1");
}
if (_data.count("code"))
m_code = spBYTES(new BYTES(_data.atKey("code")));
requireJsonFields(_data, "AccountIncomplete " + _data.getKey(),
if (_data->count("code"))
m_code = spBYTES(new BYTES(_data->atKey("code")));
requireJsonFields(_data, "AccountIncomplete " + _data->getKey(),
{{"shouldnotexist", {{DataType::String}, jsonField::Optional}},
{"storage", {{DataType::Object}, jsonField::Optional}},
{"balance", {{DataType::String}, jsonField::Optional}},
{"nonce", {{DataType::String}, jsonField::Optional}},
{"code", {{DataType::String}, jsonField::Optional}}});
ETH_ERROR_REQUIRE_MESSAGE(_data.getSubObjects().size() > 0, "AccountIncomplete must have at least one object!");
ETH_ERROR_REQUIRE_MESSAGE(_data->getSubObjects().size() > 0, "AccountIncomplete must have at least one object!");
}

spDataObject AccountIncomplete::asDataObject(ExportOrder) const
spDataObject const& AccountIncomplete::asDataObject(ExportOrder) const
{
return m_rawData;
/*
spDataObject _out(new DataObject());
DataObject& out = _out.getContent();
string const& addr = m_address->asString();
Expand All @@ -49,7 +52,7 @@ spDataObject AccountIncomplete::asDataObject(ExportOrder) const
if (m_shouldNotExist)
out["shouldnotexist"] = "1";
out.setKey(addr);
return _out;
return _out;*/
}


Expand Down
5 changes: 3 additions & 2 deletions retesteth/testStructures/types/Ethereum/AccountIncomplete.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ namespace teststruct
// It can be missing some of the fields defined
struct AccountIncomplete : AccountBase
{
AccountIncomplete(DataObject const&);
AccountIncomplete(spDataObject&);
void setBalance(VALUE const& _balance) { m_balance = spVALUE(new VALUE(_balance)); }
spDataObject asDataObject(ExportOrder) const override;
spDataObject const& asDataObject(ExportOrder) const override;
AccountType type() const override { return AccountType::Incomplete; }
};

typedef GCP_SPointer<AccountIncomplete> spAccountIncomplete;
Expand Down
10 changes: 9 additions & 1 deletion retesteth/testStructures/types/Ethereum/Base/AccountBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace test
namespace teststruct
{

enum class AccountType
{
Incomplete,
FullAccount
};

// Ethereum account description
struct AccountBase : GCP_SPointerBase
{
Expand All @@ -26,11 +32,13 @@ struct AccountBase : GCP_SPointerBase
BYTES const& code() const { return m_code; }
FH20 const& address() const { return m_address; }

virtual spDataObject asDataObject(ExportOrder order = ExportOrder::Default) const = 0;
virtual spDataObject const& asDataObject(ExportOrder order = ExportOrder::Default) const = 0;
virtual AccountType type() const = 0;
virtual ~AccountBase() {}

protected:
AccountBase() {}
spDataObject m_rawData;
bool m_shouldNotExist = false;
spFH20 m_address;
spVALUE m_balance;
Expand Down
Loading

0 comments on commit a6241fc

Please sign in to comment.