Fabcoin Core  0.16.2
P2P Digital Currency
ClientBase.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #include <boost/test/unit_test.hpp>
23 #include <libdevcore/CommonJS.h>
24 #include <libethashseal/Ethash.h>
28 
29 using namespace std;
30 using namespace dev;
31 using namespace dev::eth;
32 using namespace dev::test;
33 
35 
37 {
38  test::TestOutputHelper::initTest();
39  enumerateClients([](Json::Value const& _json, dev::eth::ClientBase& _client) -> void
40  {
41  auto compareState = [&_client](Json::Value const& _o, string const& _name, BlockNumber _blockNumber) -> void
42  {
43  Address address(_name);
44 
45  // balanceAt
46  u256 expectedBalance = u256(_o["balance"].asString());
47  u256 balance = _client.balanceAt(address, _blockNumber);
48  ETH_CHECK_EQUAL(expectedBalance, balance);
49 
50  // countAt
51  u256 expectedCount = u256(_o["nonce"].asString());
52  u256 count = _client.countAt(address, _blockNumber);
53  ETH_CHECK_EQUAL(expectedCount, count);
54 
55  // stateAt
56  for (string const& pos: _o["storage"].getMemberNames())
57  {
58  u256 expectedState = u256(_o["storage"][pos].asString());
59  u256 state = _client.stateAt(address, u256(pos), _blockNumber);
60  ETH_CHECK_EQUAL(expectedState, state);
61  }
62 
63  // codeAt
64  bytes expectedCode = fromHex(_o["code"].asString());
65  bytes code = _client.codeAt(address, _blockNumber);
66  ETH_CHECK_EQUAL_COLLECTIONS(expectedCode.begin(), expectedCode.end(),
67  code.begin(), code.end());
68  };
69 
70  for (string const& name: _json["postState"].getMemberNames())
71  {
72  Json::Value o = _json["postState"][name];
73  compareState(o, name, PendingBlock);
74  }
75 
76  for (string const& name: _json["pre"].getMemberNames())
77  {
78  Json::Value o = _json["pre"][name];
79  compareState(o, name, 0);
80  }
81 
82  // number
83  unsigned expectedNumber = _json["blocks"].size();
84  unsigned number = _client.number();
85  ETH_CHECK_EQUAL(expectedNumber, number);
86 
87  u256 totalDifficulty = u256(_json["genesisBlockHeader"]["difficulty"].asString());
88  for (Json::Value const& block: _json["blocks"])
89  {
90  Json::Value blockHeader = block["blockHeader"];
91  Json::Value uncles = block["uncleHeaders"];
92  Json::Value transactions = block["transactions"];
93  h256 blockHash = h256(fromHex(blockHeader["hash"].asString()));
94 
95  // just update the difficulty
96  for (Json::Value const& uncle: uncles)
97  {
98  totalDifficulty += u256(uncle["difficulty"].asString());
99  }
100 
101  // hashFromNumber
102  h256 expectedHashFromNumber = h256(fromHex(blockHeader["hash"].asString()));
103  h256 hashFromNumber = _client.hashFromNumber(jsToInt(blockHeader["number"].asString()));
104  ETH_CHECK_EQUAL(expectedHashFromNumber, hashFromNumber);
105 
106  // blockInfo
107  auto compareBlockInfos = [](Json::Value const& _b, BlockHeader _blockInfo) -> void
108  {
109  LogBloom expectedBlockInfoBloom = LogBloom(fromHex(_b["bloom"].asString()));
110  Address expectedBlockInfoCoinbase = Address(fromHex(_b["coinbase"].asString()));
111  u256 expectedBlockInfoDifficulty = u256(_b["difficulty"].asString());
112  bytes expectedBlockInfoExtraData = fromHex(_b["extraData"].asString());
113  u256 expectedBlockInfoGasLimit = u256(_b["gasLimit"].asString());
114  u256 expectedBlockInfoGasUsed = u256(_b["gasUsed"].asString());
115  h256 expectedBlockInfoHash = h256(fromHex(_b["hash"].asString()));
116  h256 expectedBlockInfoMixHash = h256(fromHex(_b["mixHash"].asString()));
117  Nonce expectedBlockInfoNonce = Nonce(fromHex(_b["nonce"].asString()));
118  u256 expectedBlockInfoNumber = u256(_b["number"].asString());
119  h256 expectedBlockInfoParentHash = h256(fromHex(_b["parentHash"].asString()));
120  h256 expectedBlockInfoReceiptsRoot = h256(fromHex(_b["receiptTrie"].asString()));
121  u256 expectedBlockInfoTimestamp = u256(_b["timestamp"].asString());
122  h256 expectedBlockInfoTransactionsRoot = h256(fromHex(_b["transactionsTrie"].asString()));
123  h256 expectedBlockInfoUncldeHash = h256(fromHex(_b["uncleHash"].asString()));
124  ETH_CHECK_EQUAL(expectedBlockInfoBloom, _blockInfo.logBloom());
125  ETH_CHECK_EQUAL(expectedBlockInfoCoinbase, _blockInfo.author());
126  ETH_CHECK_EQUAL(expectedBlockInfoDifficulty, _blockInfo.difficulty());
128  expectedBlockInfoExtraData.begin(),
129  expectedBlockInfoExtraData.end(),
130  _blockInfo.extraData().begin(),
131  _blockInfo.extraData().end()
132  );
133  ETH_CHECK_EQUAL(expectedBlockInfoGasLimit, _blockInfo.gasLimit());
134  ETH_CHECK_EQUAL(expectedBlockInfoGasUsed, _blockInfo.gasUsed());
135  ETH_CHECK_EQUAL(expectedBlockInfoHash, _blockInfo.hash());
136  ETH_CHECK_EQUAL(expectedBlockInfoMixHash, Ethash::mixHash(_blockInfo));
137  ETH_CHECK_EQUAL(expectedBlockInfoNonce, Ethash::nonce(_blockInfo));
138  ETH_CHECK_EQUAL(expectedBlockInfoNumber, _blockInfo.number());
139  ETH_CHECK_EQUAL(expectedBlockInfoParentHash, _blockInfo.parentHash());
140  ETH_CHECK_EQUAL(expectedBlockInfoReceiptsRoot, _blockInfo.receiptsRoot());
141  ETH_CHECK_EQUAL(expectedBlockInfoTimestamp, _blockInfo.timestamp());
142  ETH_CHECK_EQUAL(expectedBlockInfoTransactionsRoot, _blockInfo.transactionsRoot());
143  ETH_CHECK_EQUAL(expectedBlockInfoUncldeHash, _blockInfo.sha3Uncles());
144  };
145 
146  BlockHeader blockInfo((static_cast<FixedClient&>(_client)).bc().headerData(blockHash), HeaderData);
147  compareBlockInfos(blockHeader, blockInfo);
148 
149  // blockDetails
150  unsigned expectedBlockDetailsNumber = jsToInt(blockHeader["number"].asString());
151  totalDifficulty += u256(blockHeader["difficulty"].asString());
152  BlockDetails blockDetails = _client.blockDetails(blockHash);
153  ETH_CHECK_EQUAL(expectedBlockDetailsNumber, blockDetails.number);
154  ETH_CHECK_EQUAL(totalDifficulty, blockDetails.totalDifficulty);
155 
156  auto compareTransactions = [](Json::Value const& _t, Transaction _transaction) -> void
157  {
158  bytes expectedTransactionData = fromHex(_t["data"].asString());
159  u256 expectedTransactionGasLimit = u256(_t["gasLimit"].asString());
160  u256 expectedTransactionGasPrice = u256(_t["gasPrice"].asString());
161  u256 expectedTransactionNonce = u256(_t["nonce"].asString());
162  u256 expectedTransactionSignatureR = h256(fromHex(_t["r"].asString()));
163  u256 expectedTransactionSignatureS = h256(fromHex(_t["s"].asString()));
164 // unsigned expectedTransactionSignatureV = jsToInt(t["v"].asString());
165 
167  expectedTransactionData.begin(),
168  expectedTransactionData.end(),
169  _transaction.data().begin(),
170  _transaction.data().end()
171  );
172  ETH_CHECK_EQUAL(expectedTransactionGasLimit, _transaction.gas());
173  ETH_CHECK_EQUAL(expectedTransactionGasPrice, _transaction.gasPrice());
174  ETH_CHECK_EQUAL(expectedTransactionNonce, _transaction.nonce());
175  ETH_CHECK_EQUAL(expectedTransactionSignatureR, _transaction.signature().r);
176  ETH_CHECK_EQUAL(expectedTransactionSignatureS, _transaction.signature().s);
177 // ETH_CHECK_EQUAL(expectedTransactionSignatureV, _transaction.signature().v); // 27 === 0x0, 28 === 0x1, not sure why
178  };
179 
180  Transactions ts = _client.transactions(blockHash);
181  TransactionHashes tHashes = _client.transactionHashes(blockHash);
182  unsigned tsCount = _client.transactionCount(blockHash);
183 
184  ETH_REQUIRE(transactions.size() == ts.size());
185  ETH_REQUIRE(transactions.size() == tHashes.size());
186 
187  // transactionCount
188  ETH_CHECK_EQUAL(transactions.size(), tsCount);
189 
190  for (unsigned i = 0; i < tsCount; i++)
191  {
192  Json::Value t = transactions[i];
193 
194  // transaction (by block hash and transaction index)
195  Transaction transaction = _client.transaction(blockHash, i);
196  compareTransactions(t, transaction);
197 
198  // transaction (by hash)
199  Transaction transactionByHash = _client.transaction(transaction.sha3());
200  compareTransactions(t, transactionByHash);
201 
202  // transactions
203  compareTransactions(t, ts[i]);
204 
205  // transactionHashes
206  ETH_CHECK_EQUAL(transaction.sha3(), tHashes[i]);
207  }
208 
209  // uncleCount
210  unsigned usCount = _client.uncleCount(blockHash);
211  ETH_CHECK_EQUAL(uncles.size(), usCount);
212 
213  for (unsigned i = 0; i < usCount; i++)
214  {
215  Json::Value u = uncles[i];
216 
217  // uncle (by hash)
218  BlockHeader uncle = _client.uncle(blockHash, i);
219  compareBlockInfos(u, uncle);
220  }
221  }
222  });
223 }
224 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
h256s TransactionHashes
Definition: BlockChain.h:81
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
Definition: capi.c:7
Encapsulation of a block header.
Definition: BlockHeader.h:95
size_t count
Definition: ExecStats.cpp:37
virtual u256 countAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:168
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
std::vector< Transaction > Transactions
Nice name for vector of Transaction.
Definition: Transaction.h:121
virtual bytes codeAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:183
#define ETH_REQUIRE(x)
Definition: TestUtils.h:38
virtual BlockDetails blockDetails(h256 _hash) const override
Definition: ClientBase.cpp:381
virtual unsigned transactionCount(h256 _blockHash) const override
Definition: ClientBase.cpp:467
std::hash for asio::adress
Definition: Common.h:323
virtual u256 balanceAt(Address _a, BlockNumber _block) const override
Definition: ClientBase.cpp:163
#define ETH_CHECK_EQUAL(x, y)
Definition: TestUtils.h:36
bytes code
Definition: SmartVM.cpp:45
unsigned BlockNumber
Definition: Common.h:72
virtual Transactions transactions(h256 _blockHash) const override
Definition: ClientBase.cpp:437
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
Config::Value_type Value
const char * name
Definition: rest.cpp:36
virtual u256 stateAt(Address _a, u256 _l, BlockNumber _block) const override
Definition: ClientBase.cpp:173
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
BOOST_AUTO_TEST_CASE(blocks)
Definition: ClientBase.cpp:36
h256 sha3(IncludeSignature _sig=WithSignature) const
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
#define ETH_CHECK_EQUAL_COLLECTIONS(xb, xe, yb, ye)
Definition: TestUtils.h:37
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
virtual h256 hashFromNumber(BlockNumber _number) const override
Definition: ClientBase.cpp:526
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
virtual TransactionHashes transactionHashes(h256 _blockHash) const override
Definition: ClientBase.cpp:447
virtual unsigned uncleCount(h256 _blockHash) const override
Definition: ClientBase.cpp:474
h2048 LogBloom
The log bloom&#39;s size (2048-bit).
Definition: Common.h:58
virtual unsigned number() const override
Definition: ClientBase.cpp:481
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > jsToInt(std::string const &_s)
Convert a string representation of a number to an int String can be a normal decimal number...
Definition: CommonJS.h:97
virtual Transaction transaction(h256 _transactionHash) const override
Definition: ClientBase.cpp:386
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Definition: CommonData.h:79
h64 Nonce
Definition: Common.h:70
virtual BlockHeader uncle(h256 _blockHash, unsigned _i) const override
Definition: ClientBase.cpp:452
Helper functions to work with json::spirit and test files.