Browse Source

BlockWeight: toCompactHex

refactoring
cl-refactor
winsvega 10 years ago
parent
commit
0e758af143
  1. 19
      libdevcore/CommonData.h
  2. 50
      test/TestHelper.cpp
  3. 7
      test/TestHelper.h
  4. 4
      test/fuzzTesting/createRandomVMTest.cpp
  5. 38
      test/libethereum/blockchain.cpp
  6. 2
      test/libethereum/transaction.cpp
  7. 51
      test/libevm/vm.cpp

19
libdevcore/CommonData.h

@ -41,6 +41,11 @@ enum class WhenError
Throw = 1, Throw = 1,
}; };
enum class HexPrefix
{
DontAdd = 0,
Add = 1,
};
/// Convert a series of bytes to the corresponding string of hex duplets. /// Convert a series of bytes to the corresponding string of hex duplets.
/// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte. /// @param _w specifies the width of the first of the elements. Defaults to two - enough to represent a byte.
/// @example toHex("A\x69") == "4169" /// @example toHex("A\x69") == "4169"
@ -128,9 +133,6 @@ inline std::string toBigEndianString(u160 _val) { std::string ret(20, '\0'); toB
inline bytes toBigEndian(u256 _val) { bytes ret(32); toBigEndian(_val, ret); return ret; } inline bytes toBigEndian(u256 _val) { bytes ret(32); toBigEndian(_val, ret); return ret; }
inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); return ret; } inline bytes toBigEndian(u160 _val) { bytes ret(20); toBigEndian(_val, ret); return ret; }
/// Convenience function for conversion of a u256 to hex
inline std::string toHex(u256 val) { return toHex(toBigEndian(val)); }
/// Convenience function for toBigEndian. /// Convenience function for toBigEndian.
/// @returns a byte array just big enough to represent @a _val. /// @returns a byte array just big enough to represent @a _val.
template <class _T> template <class _T>
@ -159,6 +161,17 @@ inline std::string toCompactBigEndianString(_T _val)
return ret; return ret;
} }
/// Convenience function for conversion of a u256 to hex
inline std::string toHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
{
std::string str = toHex(toBigEndian(val));
return (prefix == HexPrefix::Add) ? "0x" + str : str;
}
inline std::string toCompactHex(u256 val, HexPrefix prefix = HexPrefix::DontAdd)
{
std::string str = toHex(toCompactBigEndian(val, 1)); //1 means val=0 would be '0x00' instead of '0x'
return (prefix == HexPrefix::Add) ? "0x" + str : str;
}
// Algorithms for string and string-like collections. // Algorithms for string and string-like collections.

50
test/TestHelper.cpp

@ -137,7 +137,7 @@ json_spirit::mObject& ImportTest::makeAllFieldsHex(json_spirit::mObject& _o)
str = value.get_str(); str = value.get_str();
else continue; else continue;
_o[key] = (str.substr(0, 2) == "0x") ? str : jsonHex(toInt(str)); _o[key] = (str.substr(0, 2) == "0x") ? str : toHex(toInt(str), HexPrefix::Add);
} }
return _o; return _o;
} }
@ -327,7 +327,7 @@ void ImportTest::checkExpectedState(State const& _stateExpect, State const& _sta
void ImportTest::exportTest(bytes const& _output, State const& _statePost) void ImportTest::exportTest(bytes const& _output, State const& _statePost)
{ {
// export output // export output
m_TestObject["out"] = jsonHex(_output); m_TestObject["out"] = "0x" + toString(_output);
// export logs // export logs
m_TestObject["logs"] = exportLog(_statePost.pending().size() ? _statePost.log(0) : LogEntries()); m_TestObject["logs"] = exportLog(_statePost.pending().size() ? _statePost.log(0) : LogEntries());
@ -344,7 +344,7 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost)
// export post state // export post state
m_TestObject["post"] = fillJsonWithState(_statePost); m_TestObject["post"] = fillJsonWithState(_statePost);
m_TestObject["postStateRoot"] = jsonHex(_statePost.rootHash().asBytes()); m_TestObject["postStateRoot"] = toString(_statePost.rootHash().asBytes());
// export pre state // export pre state
m_TestObject["pre"] = fillJsonWithState(m_statePre); m_TestObject["pre"] = fillJsonWithState(m_statePre);
@ -352,29 +352,18 @@ void ImportTest::exportTest(bytes const& _output, State const& _statePost)
m_TestObject["transaction"] = makeAllFieldsHex(m_TestObject["transaction"].get_obj()); m_TestObject["transaction"] = makeAllFieldsHex(m_TestObject["transaction"].get_obj());
} }
std::string jsonHash(h256 const& _value) { return toString(_value); }
std::string jsonHash(Nonce const& _value) { return toString(_value); }
std::string jsonHash(LogBloom const& _value) { return toString(_value); }
std::string jsonHash(Address const& _value) { return toString(_value); }
std::string jsonHex(bytesConstRef _code) { return "0x" + toHex(_code); }
std::string jsonHex(bytes const& _code) { return "0x" + toHex(_code); }
std::string jsonHex(u256 const& _value, bool _nonempty)
{
return "0x" + toHex(toCompactBigEndian(_value, _nonempty ? 1 : 0));
}
json_spirit::mObject fillJsonWithTransaction(Transaction _txn) json_spirit::mObject fillJsonWithTransaction(Transaction _txn)
{ {
json_spirit::mObject txObject; json_spirit::mObject txObject;
txObject["nonce"] = jsonHex(_txn.nonce()); txObject["nonce"] = toCompactHex(_txn.nonce(), HexPrefix::Add);
txObject["data"] = jsonHex(_txn.data()); txObject["data"] = "0x"+toString(_txn.data());
txObject["gasLimit"] = jsonHex(_txn.gas()); txObject["gasLimit"] = toCompactHex(_txn.gas(), HexPrefix::Add);
txObject["gasPrice"] = jsonHex(_txn.gasPrice()); txObject["gasPrice"] = toCompactHex(_txn.gasPrice(), HexPrefix::Add);
txObject["r"] = jsonHex(_txn.signature().r); txObject["r"] = toCompactHex(_txn.signature().r, HexPrefix::Add);
txObject["s"] = jsonHex(_txn.signature().s); txObject["s"] = toCompactHex(_txn.signature().s, HexPrefix::Add);
txObject["v"] = jsonHex(_txn.signature().v + 27); txObject["v"] = toCompactHex(_txn.signature().v + 27, HexPrefix::Add);
txObject["to"] = _txn.isCreation() ? "" : jsonHash(_txn.receiveAddress()); txObject["to"] = _txn.isCreation() ? "" : toString(_txn.receiveAddress());
txObject["value"] = jsonHex(_txn.value()); txObject["value"] = toCompactHex(_txn.value(), HexPrefix::Add);
return txObject; return txObject;
} }
@ -384,16 +373,15 @@ json_spirit::mObject fillJsonWithState(State _state)
for (auto const& a: _state.addresses()) for (auto const& a: _state.addresses())
{ {
json_spirit::mObject o; json_spirit::mObject o;
o["balance"] = jsonHex(_state.balance(a.first)); o["balance"] = toCompactHex(_state.balance(a.first), HexPrefix::Add);
o["nonce"] = jsonHex(_state.transactionsFrom(a.first)); o["nonce"] = toCompactHex(_state.transactionsFrom(a.first), HexPrefix::Add);
{ {
json_spirit::mObject store; json_spirit::mObject store;
for (auto const& s: _state.storage(a.first)) for (auto const& s: _state.storage(a.first))
store[jsonHex(s.first)] = jsonHex(s.second); store[toCompactHex(s.first, HexPrefix::Add)] = toCompactHex(s.second, HexPrefix::Add);
o["storage"] = store; o["storage"] = store;
} }
o["code"] = jsonHex(_state.code(a.first)); o["code"] = "0x" + toString(_state.code(a.first));
oState[toString(a.first)] = o; oState[toString(a.first)] = o;
} }
return oState; return oState;
@ -406,13 +394,13 @@ json_spirit::mArray exportLog(eth::LogEntries _logs)
for (LogEntry const& l: _logs) for (LogEntry const& l: _logs)
{ {
json_spirit::mObject o; json_spirit::mObject o;
o["address"] = jsonHash(l.address); o["address"] = toString(l.address);
json_spirit::mArray topics; json_spirit::mArray topics;
for (auto const& t: l.topics) for (auto const& t: l.topics)
topics.push_back(toString(t)); topics.push_back(toString(t));
o["topics"] = topics; o["topics"] = topics;
o["data"] = jsonHex(l.data); o["data"] = "0x" + toString(l.data);
o["bloom"] = jsonHash(l.bloom()); o["bloom"] = toString(l.bloom());
ret.push_back(o); ret.push_back(o);
} }
return ret; return ret;

7
test/TestHelper.h

@ -162,13 +162,6 @@ RLPStream createRLPStreamFromTransactionFields(json_spirit::mObject& _tObj);
eth::LastHashes lastHashes(u256 _currentBlockNumber); eth::LastHashes lastHashes(u256 _currentBlockNumber);
json_spirit::mObject fillJsonWithState(eth::State _state); json_spirit::mObject fillJsonWithState(eth::State _state);
json_spirit::mObject fillJsonWithTransaction(eth::Transaction _txn); json_spirit::mObject fillJsonWithTransaction(eth::Transaction _txn);
std::string jsonHash(dev::eth::LogBloom const& _value);
std::string jsonHash(Address const& _value);
std::string jsonHash(dev::eth::Nonce const& _value);
std::string jsonHash(h256 const& _value);
std::string jsonHex(u256 const& _value, bool _nonempty = true);
std::string jsonHex(bytesConstRef _code);
std::string jsonHex(bytes const& _code);
template<typename mapType> template<typename mapType>
void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs) void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs)

4
test/fuzzTesting/createRandomVMTest.cpp

@ -200,8 +200,8 @@ void doMyTests(json_spirit::mValue& _v)
{ {
o["post"] = mValue(fev.exportState()); o["post"] = mValue(fev.exportState());
o["callcreates"] = fev.exportCallCreates(); o["callcreates"] = fev.exportCallCreates();
o["out"] = dev::test::jsonHex(output); o["out"] = "0x" + toString(output);
o["gas"] = dev::test::jsonHex(gas); o["gas"] = toCompactHex(gas, HexPrefix::Add);
o["logs"] = test::exportLog(fev.sub.logs); o["logs"] = test::exportLog(fev.sub.logs);
} }
} }

38
test/libethereum/blockchain.cpp

@ -81,7 +81,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin)
// create new "genesis" block // create new "genesis" block
RLPStream rlpGenesisBlock = createFullBlockFromHeader(biGenesisBlock); RLPStream rlpGenesisBlock = createFullBlockFromHeader(biGenesisBlock);
biGenesisBlock.verifyInternals(&rlpGenesisBlock.out()); biGenesisBlock.verifyInternals(&rlpGenesisBlock.out());
o["genesisRLP"] = jsonHex(rlpGenesisBlock.out()); o["genesisRLP"] = "0x" + toString(rlpGenesisBlock.out());
// construct blockchain // construct blockchain
TransientDirectory td; TransientDirectory td;
@ -149,7 +149,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin)
return; return;
} }
blObj["rlp"] = jsonHex(state.blockData()); blObj["rlp"] = "0x"+toString(state.blockData());
//get valid transactions //get valid transactions
Transactions txList; Transactions txList;
@ -188,7 +188,7 @@ void doBlockchainTests(json_spirit::mValue& _v, bool _fillin)
RLPStream block2 = createFullBlockFromHeader(current_BlockHeader, txStream.out(), uncleStream.out()); RLPStream block2 = createFullBlockFromHeader(current_BlockHeader, txStream.out(), uncleStream.out());
blObj["rlp"] = jsonHex(block2.out()); blObj["rlp"] = "0x" + toString(block2.out());
if (sha3(RLP(state.blockData())[0].data()) != sha3(RLP(block2.out())[0].data())) if (sha3(RLP(state.blockData())[0].data()) != sha3(RLP(block2.out())[0].data()))
cnote << "block header mismatch\n"; cnote << "block header mismatch\n";
@ -628,22 +628,22 @@ mArray writeTransactionsToJson(Transactions const& txs)
mObject writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi) mObject writeBlockHeaderToJson(mObject& _o, BlockInfo const& _bi)
{ {
_o["parentHash"] = jsonHash(_bi.parentHash); _o["parentHash"] = toString(_bi.parentHash);
_o["uncleHash"] = jsonHash(_bi.sha3Uncles); _o["uncleHash"] = toString(_bi.sha3Uncles);
_o["coinbase"] = jsonHash(_bi.coinbaseAddress); _o["coinbase"] = toString(_bi.coinbaseAddress);
_o["stateRoot"] = jsonHash(_bi.stateRoot); _o["stateRoot"] = toString(_bi.stateRoot);
_o["transactionsTrie"] = jsonHash(_bi.transactionsRoot); _o["transactionsTrie"] = toString(_bi.transactionsRoot);
_o["receiptTrie"] = jsonHash(_bi.receiptsRoot); _o["receiptTrie"] = toString(_bi.receiptsRoot);
_o["bloom"] = jsonHash(_bi.logBloom); _o["bloom"] = toString(_bi.logBloom);
_o["difficulty"] = jsonHex(_bi.difficulty); _o["difficulty"] = toCompactHex(_bi.difficulty, HexPrefix::Add);
_o["number"] = jsonHex(_bi.number); _o["number"] = toCompactHex(_bi.number, HexPrefix::Add);
_o["gasLimit"] = jsonHex(_bi.gasLimit); _o["gasLimit"] = toCompactHex(_bi.gasLimit, HexPrefix::Add);
_o["gasUsed"] = jsonHex(_bi.gasUsed); _o["gasUsed"] = toCompactHex(_bi.gasUsed, HexPrefix::Add);
_o["timestamp"] = jsonHex(_bi.timestamp); _o["timestamp"] = toCompactHex(_bi.timestamp, HexPrefix::Add);
_o["extraData"] = jsonHex(_bi.extraData); _o["extraData"] = "0x" + toString(_bi.extraData);
_o["mixHash"] = jsonHash(_bi.mixHash); _o["mixHash"] = toString(_bi.mixHash);
_o["nonce"] = jsonHash(_bi.nonce); _o["nonce"] = toString(_bi.nonce);
_o["hash"] = jsonHash(_bi.hash()); _o["hash"] = toString(_bi.hash());
return _o; return _o;
} }

2
test/libethereum/transaction.cpp

@ -43,7 +43,7 @@ void doTransactionTests(json_spirit::mValue& _v, bool _fillin)
//Construct Rlp of the given transaction //Construct Rlp of the given transaction
RLPStream rlpStream = createRLPStreamFromTransactionFields(tObj); RLPStream rlpStream = createRLPStreamFromTransactionFields(tObj);
o["rlp"] = jsonHex(rlpStream.out()); o["rlp"] = "0x"+toString(rlpStream.out());
try try
{ {

51
test/libevm/vm.cpp

@ -86,12 +86,12 @@ void FakeExtVM::reset(u256 _myBalance, u256 _myNonce, map<u256, u256> const& _st
mObject FakeExtVM::exportEnv() mObject FakeExtVM::exportEnv()
{ {
mObject ret; mObject ret;
ret["previousHash"] = jsonHash(currentBlock.parentHash); ret["previousHash"] = toString(currentBlock.parentHash);
ret["currentDifficulty"] = jsonHex(currentBlock.difficulty); ret["currentDifficulty"] = toCompactHex(currentBlock.difficulty, HexPrefix::Add);
ret["currentTimestamp"] = jsonHex(currentBlock.timestamp); ret["currentTimestamp"] = toCompactHex(currentBlock.timestamp, HexPrefix::Add);
ret["currentCoinbase"] = jsonHash(currentBlock.coinbaseAddress); ret["currentCoinbase"] = toString(currentBlock.coinbaseAddress);
ret["currentNumber"] = jsonHex(currentBlock.number); ret["currentNumber"] = toCompactHex(currentBlock.number, HexPrefix::Add);
ret["currentGasLimit"] = jsonHex(currentBlock.gasLimit); ret["currentGasLimit"] = toCompactHex(currentBlock.gasLimit, HexPrefix::Add);
return ret; return ret;
} }
@ -120,16 +120,15 @@ mObject FakeExtVM::exportState()
for (auto const& a: addresses) for (auto const& a: addresses)
{ {
mObject o; mObject o;
o["balance"] = jsonHex(get<0>(a.second)); o["balance"] = toCompactHex(get<0>(a.second), HexPrefix::Add);
o["nonce"] = jsonHex(get<1>(a.second)); o["nonce"] = toCompactHex(get<1>(a.second), HexPrefix::Add);
{ {
mObject store; mObject store;
for (auto const& s: get<2>(a.second)) for (auto const& s: get<2>(a.second))
store[jsonHex(s.first)] = jsonHex(s.second); store[toCompactHex(s.first, HexPrefix::Add)] = toCompactHex(s.second, HexPrefix::Add);
o["storage"] = store; o["storage"] = store;
} }
o["code"] = jsonHex(get<3>(a.second)); o["code"] = "0x"+toString(get<3>(a.second));
ret[toString(a.first)] = o; ret[toString(a.first)] = o;
} }
return ret; return ret;
@ -159,14 +158,14 @@ void FakeExtVM::importState(mObject& _object)
mObject FakeExtVM::exportExec() mObject FakeExtVM::exportExec()
{ {
mObject ret; mObject ret;
ret["address"] = jsonHash(myAddress); ret["address"] = toString(myAddress);
ret["caller"] = jsonHash(caller); ret["caller"] = toString(caller);
ret["origin"] = jsonHash(origin); ret["origin"] = toString(origin);
ret["value"] = jsonHex(value); ret["value"] = toCompactHex(value, HexPrefix::Add);
ret["gasPrice"] = jsonHex(gasPrice); ret["gasPrice"] = toCompactHex(gasPrice, HexPrefix::Add);
ret["gas"] = jsonHex(gas); ret["gas"] = toCompactHex(gas, HexPrefix::Add);
ret["data"] = jsonHex(data); ret["data"] = "0x" + toString(data);
ret["code"] = jsonHex(code); ret["code"] = "0x" + toString(code);
return ret; return ret;
} }
@ -207,10 +206,10 @@ mArray FakeExtVM::exportCallCreates()
for (Transaction const& tx: callcreates) for (Transaction const& tx: callcreates)
{ {
mObject o; mObject o;
o["destination"] = tx.isCreation() ? "" : jsonHash(tx.receiveAddress()); o["destination"] = tx.isCreation() ? "" : toString(tx.receiveAddress());
o["gasLimit"] = jsonHex(tx.gas()); o["gasLimit"] = toCompactHex(tx.gas(), HexPrefix::Add);
o["value"] = jsonHex(tx.value()); o["value"] = toCompactHex(tx.value(), HexPrefix::Add);
o["data"] = jsonHex(tx.data()); o["data"] = "0x" + toString(tx.data());
ret.push_back(o); ret.push_back(o);
} }
return ret; return ret;
@ -281,7 +280,7 @@ eth::OnOpFunc FakeExtVM::simpleTrace()
o_step.push_back(Pair("storage", storage)); o_step.push_back(Pair("storage", storage));
o_step.push_back(Pair("depth", to_string(ext.depth))); o_step.push_back(Pair("depth", to_string(ext.depth)));
o_step.push_back(Pair("gas", (string)vm.gas())); o_step.push_back(Pair("gas", (string)vm.gas()));
o_step.push_back(Pair("address", jsonHash(ext.myAddress ))); o_step.push_back(Pair("address", toString(ext.myAddress )));
o_step.push_back(Pair("step", steps )); o_step.push_back(Pair("step", steps ));
o_step.push_back(Pair("pc", (int)vm.curPC())); o_step.push_back(Pair("pc", (int)vm.curPC()));
o_step.push_back(Pair("opcode", instructionInfo(inst).name )); o_step.push_back(Pair("opcode", instructionInfo(inst).name ));
@ -389,8 +388,8 @@ void doVMTests(json_spirit::mValue& v, bool _fillin)
} }
o["callcreates"] = fev.exportCallCreates(); o["callcreates"] = fev.exportCallCreates();
o["out"] = jsonHex(output); o["out"] = "0x" + toString(output);
o["gas"] = jsonHex(gas); o["gas"] = toCompactHex(gas, HexPrefix::Add);
o["logs"] = exportLog(fev.sub.logs); o["logs"] = exportLog(fev.sub.logs);
} }
} }

Loading…
Cancel
Save