From 4515ac0bca2dd44fca7ae4b1ff31fbda5a78e230 Mon Sep 17 00:00:00 2001 From: Scott Schurr Date: Fri, 29 May 2015 10:31:13 -0700 Subject: [PATCH] Replace base_uint(string) with from_hex_text<> (RIPD-897) Removes the base_uint constructor that took a string. Replaces that functionality with two free functions named from_hex_text<>. Use of from_hex_text<> looks like this: auto v = from_hex_text("AAA555"); static_assert (std::is_same::value, "Huh!"); from_hex_text<> only operates on base_uint types. At the moment the list of those types include: o uint128, o uint160, o uint256, o Directory, o Account, o Currency, and o NodeID. Using from_hex_text<> with any other types will not compile due to an enable_if. --- src/ripple/app/ledger/tests/Ledger_test.cpp | 4 +-- src/ripple/basics/base_uint.h | 28 ++++++++++++++----- .../nodestore/backend/RocksDBFactory.cpp | 3 +- .../nodestore/backend/RocksDBQuickFactory.cpp | 11 ++++---- src/ripple/protocol/impl/Indexes.cpp | 7 +++-- src/ripple/protocol/impl/UintTypes.cpp | 8 ++++-- src/ripple/protocol/tests/STAmount.test.cpp | 3 +- src/ripple/rpc/handlers/CanDelete.cpp | 6 ++-- src/ripple/rpc/handlers/Tx.cpp | 3 +- 9 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/ripple/app/ledger/tests/Ledger_test.cpp b/src/ripple/app/ledger/tests/Ledger_test.cpp index e6534b11c..94c0d3aed 100644 --- a/src/ripple/app/ledger/tests/Ledger_test.cpp +++ b/src/ripple/app/ledger/tests/Ledger_test.cpp @@ -29,7 +29,7 @@ class Ledger_test : public beast::unit_test::suite std::uint64_t const xrp = std::mega::num; auto master = createAccount ("masterpassphrase", keyType); - + Ledger::pointer LCL; Ledger::pointer ledger; std::tie(LCL, ledger) = createGenesisLedger(100000*xrp, master); @@ -120,7 +120,7 @@ class Ledger_test : public beast::unit_test::suite void test_getQuality () { - uint256 uBig ( + uint256 uBig = from_hex_text ( "D2DC44E5DC189318DB36EF87D2104CDF0A0FE3A4B698BEEE55038D7EA4C68000"); expect (6125895493223874560 == getQuality (uBig)); diff --git a/src/ripple/basics/base_uint.h b/src/ripple/basics/base_uint.h index ad750baf7..9f0af6b32 100644 --- a/src/ripple/basics/base_uint.h +++ b/src/ripple/basics/base_uint.h @@ -141,13 +141,6 @@ public: *this = b; } - // NIKB TODO remove the need for this constructor - have a free function - // to handle the hex string parsing. - explicit base_uint (std::string const& str) - { - SetHex (str); - } - base_uint (base_uint const& other) = default; template @@ -513,6 +506,27 @@ inline std::string to_string (base_uint const& a) return strHex (a.begin (), a.size ()); } +// Function templates that return a base_uint given text in hexadecimal. +// Invoke like: +// auto i = from_hex_text("AAAAA"); +template +auto from_hex_text (char const* text) -> std::enable_if_t< + std::is_same>::value, T> +{ + T ret; + ret.SetHex (text); + return ret; +} + +template +auto from_hex_text (std::string const& text) -> std::enable_if_t< + std::is_same>::value, T> +{ + T ret; + ret.SetHex (text); + return ret; +} + template inline std::ostream& operator<< ( std::ostream& out, base_uint const& u) diff --git a/src/ripple/nodestore/backend/RocksDBFactory.cpp b/src/ripple/nodestore/backend/RocksDBFactory.cpp index d17bc2200..09ef4e626 100644 --- a/src/ripple/nodestore/backend/RocksDBFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBFactory.cpp @@ -334,7 +334,8 @@ public: { // Uh oh, corrupted data! if (m_journal.fatal) m_journal.fatal << - "Corrupt NodeObject #" << uint256 (it->key ().data ()); + "Corrupt NodeObject #" << + from_hex_text(it->key ().data ()); } } else diff --git a/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp b/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp index c2c7e4e9e..8215df99b 100644 --- a/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBQuickFactory.cpp @@ -136,7 +136,7 @@ public: // overrride OptimizeLevelStyleCompaction options.min_write_buffer_number_to_merge = 1; - + rocksdb::BlockBasedTableOptions table_options; // Use hash index table_options.index_type = @@ -145,7 +145,7 @@ public: rocksdb::NewBloomFilterPolicy(10)); options.table_factory.reset( NewBlockBasedTableFactory(table_options)); - + // Higher values make reads slower // table_options.block_size = 4096; @@ -273,7 +273,7 @@ public: storeBatch (Batch const& batch) { rocksdb::WriteBatch wb; - + EncodedBlob encoded; for (auto const& e : batch) @@ -291,7 +291,7 @@ public: // Crucial to ensure good write speed and non-blocking writes to memtable options.disableWAL = true; - + auto ret = m_db->Write (options, &wb); if (!ret.ok ()) @@ -321,7 +321,8 @@ public: { // Uh oh, corrupted data! if (m_journal.fatal) m_journal.fatal << - "Corrupt NodeObject #" << uint256 (it->key ().data ()); + "Corrupt NodeObject #" << + from_hex_text(it->key ().data ()); } } else diff --git a/src/ripple/protocol/impl/Indexes.cpp b/src/ripple/protocol/impl/Indexes.cpp index e97429fb3..516746eec 100644 --- a/src/ripple/protocol/impl/Indexes.cpp +++ b/src/ripple/protocol/impl/Indexes.cpp @@ -19,6 +19,7 @@ #include #include +#include namespace ripple { @@ -168,8 +169,10 @@ getQualityIndex (uint256 const& uBase, const std::uint64_t uNodeDir) uint256 getQualityNext (uint256 const& uBase) { - static uint256 uNext ("10000000000000000"); - return uBase + uNext; + static beast::static_initializer const uNext ( + from_hex_text("10000000000000000")); + + return uBase + *uNext; } std::uint64_t diff --git a/src/ripple/protocol/impl/UintTypes.cpp b/src/ripple/protocol/impl/UintTypes.cpp index 718778b38..ef5b154d6 100644 --- a/src/ripple/protocol/impl/UintTypes.cpp +++ b/src/ripple/protocol/impl/UintTypes.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace ripple { @@ -32,8 +33,6 @@ std::string to_string(Account const& account) std::string to_string(Currency const& currency) { - static Currency const sIsoBits ("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF"); - // Characters we are willing to allow in the ASCII representation of a // three-letter currency code. static std::string const allowed_characters = @@ -48,7 +47,10 @@ std::string to_string(Currency const& currency) if (currency == noCurrency()) return "1"; - if ((currency & sIsoBits).isZero ()) + static beast::static_initializer const sIsoBits ( + from_hex_text("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF")); + + if ((currency & *sIsoBits).isZero ()) { // The offset of the 3 character ISO code in the currency descriptor int const isoOffset = 12; diff --git a/src/ripple/protocol/tests/STAmount.test.cpp b/src/ripple/protocol/tests/STAmount.test.cpp index 11b7272ca..359057b07 100644 --- a/src/ripple/protocol/tests/STAmount.test.cpp +++ b/src/ripple/protocol/tests/STAmount.test.cpp @@ -281,7 +281,8 @@ public: const std::string cur = "015841551A748AD2C1F76FF6ECB0CCCD00000000"; unexpected (!to_currency (c, cur), "create custom currency"); unexpected (to_string (c) != cur, "check custom currency"); - unexpected (c != Currency (cur), "check custom currency"); + unexpected (c != Currency ( + from_hex_text(cur)), "check custom currency"); } //-------------------------------------------------------------------------- diff --git a/src/ripple/rpc/handlers/CanDelete.cpp b/src/ripple/rpc/handlers/CanDelete.cpp index e4e9050d0..8144da4c9 100644 --- a/src/ripple/rpc/handlers/CanDelete.cpp +++ b/src/ripple/rpc/handlers/CanDelete.cpp @@ -70,9 +70,9 @@ Json::Value doCanDelete (RPC::Context& context) canDeleteStr.find_first_not_of("0123456789abcdef") == std::string::npos) { - uint256 ledgerHash (canDeleteStr); - Ledger::pointer ledger = - context.netOps.getLedgerByHash (ledgerHash); + Ledger::pointer ledger = context.netOps.getLedgerByHash ( + from_hex_text(canDeleteStr)); + if (!ledger) return RPC::make_error(rpcLGR_NOT_FOUND, "ledgerNotFound"); diff --git a/src/ripple/rpc/handlers/Tx.cpp b/src/ripple/rpc/handlers/Tx.cpp index a874578dd..d0f5021cd 100644 --- a/src/ripple/rpc/handlers/Tx.cpp +++ b/src/ripple/rpc/handlers/Tx.cpp @@ -55,7 +55,8 @@ Json::Value doTx (RPC::Context& context) if (!isHexTxID (txid)) return rpcError (rpcNOT_IMPL); - auto txn = getApp().getMasterTransaction ().fetch (uint256 (txid), true); + auto txn = getApp().getMasterTransaction ().fetch ( + from_hex_text(txid), true); if (!txn) return rpcError (rpcTXN_NOT_FOUND);