diff --git a/src/ripple/app/ledger/AcceptedLedgerTx.cpp b/src/ripple/app/ledger/AcceptedLedgerTx.cpp index 236c4a3d54..04fea22113 100644 --- a/src/ripple/app/ledger/AcceptedLedgerTx.cpp +++ b/src/ripple/app/ledger/AcceptedLedgerTx.cpp @@ -31,7 +31,7 @@ AcceptedLedgerTx::AcceptedLedgerTx (Ledger::ref ledger, SerialIter& sit) { // VFALCO This is making a needless copy auto const vl = sit.getVL(); - SerialIter txnIt (make_Slice(vl)); + SerialIter txnIt (makeSlice(vl)); mTxn = std::make_shared (std::ref (txnIt)); mRawMeta = sit.getVL (); diff --git a/src/ripple/app/ledger/LedgerToJson.h b/src/ripple/app/ledger/LedgerToJson.h index 83d2c0b989..74bbe9c91a 100644 --- a/src/ripple/app/ledger/LedgerToJson.h +++ b/src/ripple/app/ledger/LedgerToJson.h @@ -163,7 +163,7 @@ void fillJson (Object& json, LedgerFill const& fill) // VFALCO This is making a needless copy SerialIter sit (item->slice ()); auto const vl = sit.getVL(); - SerialIter tsit (make_Slice(vl)); + SerialIter tsit (makeSlice(vl)); STTx txn (tsit); TxMeta meta ( diff --git a/src/ripple/app/ledger/impl/TxMeta.cpp b/src/ripple/app/ledger/impl/TxMeta.cpp index 070d34168c..094982b2ff 100644 --- a/src/ripple/app/ledger/impl/TxMeta.cpp +++ b/src/ripple/app/ledger/impl/TxMeta.cpp @@ -35,7 +35,7 @@ TxMeta::TxMeta (uint256 const& txid, , mLedger (ledger) , mNodes (sfAffectedNodes, 32) { - SerialIter sit (make_Slice(data)); + SerialIter sit (makeSlice(data)); STObject obj(sit, sfMetadata); mResult = obj.getFieldU8 (sfTransactionResult); diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index 3445f2125e..b137d76cc3 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -3206,7 +3206,7 @@ bool NetworkOPsImp::getFetchPack (uint256 const& hash, Blob& data) mFetchPack.del (hash, false); - if (hash != sha512Half(make_Slice(data))) + if (hash != sha512Half(makeSlice(data))) { m_journal.warning << "Bad entry in fetch pack"; return false; diff --git a/src/ripple/app/misc/UniqueNodeList.cpp b/src/ripple/app/misc/UniqueNodeList.cpp index 5e489855ec..8e051d60bc 100644 --- a/src/ripple/app/misc/UniqueNodeList.cpp +++ b/src/ripple/app/misc/UniqueNodeList.cpp @@ -1599,7 +1599,7 @@ bool UniqueNodeListImp::responseFetch (std::string const& strDomain, const boost (void) bFound; uint256 iSha256 = - sha512Half(make_Slice(strSiteFile)); + sha512Half(makeSlice(strSiteFile)); bool bChangedB = sdCurrent.iSha256 != iSha256; diff --git a/src/ripple/app/misc/impl/AccountTxPaging.cpp b/src/ripple/app/misc/impl/AccountTxPaging.cpp index e22ad754e5..ebf1adf012 100644 --- a/src/ripple/app/misc/impl/AccountTxPaging.cpp +++ b/src/ripple/app/misc/impl/AccountTxPaging.cpp @@ -37,7 +37,7 @@ convertBlobsToTxResult ( Blob const& rawTxn, Blob const& rawMeta) { - SerialIter it (make_Slice(rawTxn)); + SerialIter it (makeSlice(rawTxn)); STTx::pointer txn = std::make_shared (it); std::string reason; diff --git a/src/ripple/app/tx/impl/Transaction.cpp b/src/ripple/app/tx/impl/Transaction.cpp index 8d164b13c9..e51e0bb6e7 100644 --- a/src/ripple/app/tx/impl/Transaction.cpp +++ b/src/ripple/app/tx/impl/Transaction.cpp @@ -61,7 +61,7 @@ Transaction::pointer Transaction::sharedTransaction ( { try { - SerialIter sit (make_Slice(vucTransaction)); + SerialIter sit (makeSlice(vucTransaction)); std::string reason; return std::make_shared (std::make_shared (sit), @@ -130,7 +130,7 @@ Transaction::pointer Transaction::transactionFromSQL ( std::uint32_t const inLedger = rangeCheckedCast(ledgerSeq.value_or (0)); - SerialIter it (make_Slice(rawTxn)); + SerialIter it (makeSlice(rawTxn)); auto txn = std::make_shared (it); std::string reason; auto tr = std::make_shared (txn, validate, reason); diff --git a/src/ripple/basics/Slice.h b/src/ripple/basics/Slice.h index 15c39305fc..2276eaf77d 100644 --- a/src/ripple/basics/Slice.h +++ b/src/ripple/basics/Slice.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include // @@ -35,22 +36,20 @@ namespace ripple { /** An immutable linear range of bytes. A fully constructed Slice is guaranteed to be in a valid state. - Default construction, construction from nullptr, and zero-byte - ranges are disallowed. A Slice is lightweight and copyable, it - retains no ownership of the underlying memory. + A Slice is lightweight and copyable, it retains no ownership + of the underlying memory. */ class Slice { private: std::uint8_t const* data_; - std::size_t size_; + std::size_t size_ = 0; public: - // Disallowed - Slice() = delete; + /** Default constructed Slice has length 0. */ + Slice() = default; Slice (Slice const&) = default; - Slice& operator= (Slice const&) = default; /** Create a slice pointing to existing memory. */ @@ -63,9 +62,16 @@ public: assert(size_ > 0); } + /** Return `true` if the byte range is empty. */ + bool + empty() const noexcept + { + return size_ == 0; + } + /** Returns the number of bytes in the storage. - This will never be zero. + This may be zero for an empty range. */ std::size_t size() const noexcept @@ -82,8 +88,38 @@ public: { return data_; } + + /** Access raw bytes. */ + std::uint8_t + operator[](std::size_t i) const noexcept + { + assert(i < size_); + return data_[i]; + } + + /** Advance the buffer. */ + /** @{ */ + Slice& + operator+= (std::size_t n) + { + if (n > size_) + throw std::domain_error("too small"); + data_ += n; + size_ -= n; + return *this; + } + + Slice + operator+ (std::size_t n) const + { + Slice temp = *this; + return temp += n; + } + /** @} */ }; +//------------------------------------------------------------------------------ + template inline void @@ -131,14 +167,14 @@ std::enable_if_t< std::is_same::value, Slice > -make_Slice (std::vector const& v) +makeSlice (std::vector const& v) { return Slice(v.data(), v.size()); } template Slice -make_Slice (std::basic_string const& s) +makeSlice (std::basic_string const& s) { return Slice(s.data(), s.size()); } diff --git a/src/ripple/nodestore/impl/DatabaseImp.h b/src/ripple/nodestore/impl/DatabaseImp.h index dfa4486789..3319e7efd3 100644 --- a/src/ripple/nodestore/impl/DatabaseImp.h +++ b/src/ripple/nodestore/impl/DatabaseImp.h @@ -288,7 +288,7 @@ public: Backend& backend) { #if RIPPLE_VERIFY_NODEOBJECT_KEYS - assert (hash == sha512Hash(make_Slice(data))); + assert (hash == sha512Hash(makeSlice(data))); #endif std::shared_ptr object = NodeObject::createObject( diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index 6cc05ad172..798c456086 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -1010,7 +1010,7 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - SerialIter sit (make_Slice(m->rawtransaction())); + SerialIter sit (makeSlice(m->rawtransaction())); try { @@ -1424,7 +1424,7 @@ PeerImp::onMessage (std::shared_ptr const& m) { STValidation::pointer val; { - SerialIter sit (make_Slice(m->validation())); + SerialIter sit (makeSlice(m->validation())); val = std::make_shared < STValidation> (std::ref (sit), false); } @@ -1437,7 +1437,7 @@ PeerImp::onMessage (std::shared_ptr const& m) } if (! getApp().getHashRouter ().addSuppressionPeer( - sha512Half(make_Slice(m->validation())), id_)) + sha512Half(makeSlice(m->validation())), id_)) { p_journal_.trace << "Validation: duplicate"; return; diff --git a/src/ripple/protocol/impl/RippleAddress.cpp b/src/ripple/protocol/impl/RippleAddress.cpp index a0c3db5407..db089ac286 100644 --- a/src/ripple/protocol/impl/RippleAddress.cpp +++ b/src/ripple/protocol/impl/RippleAddress.cpp @@ -67,7 +67,7 @@ static uint128 PassPhraseToKey (std::string const& passPhrase) { return uint128::fromVoid(sha512Half_s( - make_Slice(passPhrase)).data()); + makeSlice(passPhrase)).data()); } static @@ -414,7 +414,7 @@ bool RippleAddress::accountPublicVerify ( } return verifySignature (getAccountPublic(), - sha512Half(make_Slice(message)), vucSig, + sha512Half(makeSlice(message)), vucSig, fullyCanonical); } @@ -496,7 +496,7 @@ Blob RippleAddress::accountPrivateSign (Blob const& message) const } Blob result = ECDSASign( - sha512Half(make_Slice(message)), getAccountPrivate()); + sha512Half(makeSlice(message)), getAccountPrivate()); bool const ok = !result.empty(); CondLog (!ok, lsWARNING, RippleAddress) diff --git a/src/ripple/protocol/impl/Serializer.cpp b/src/ripple/protocol/impl/Serializer.cpp index d1d5e35bc7..71c34f8b68 100644 --- a/src/ripple/protocol/impl/Serializer.cpp +++ b/src/ripple/protocol/impl/Serializer.cpp @@ -230,7 +230,7 @@ Blob Serializer::getRaw (int offset, int length) const uint256 Serializer::getSHA512Half () const { - return sha512Half(make_Slice(mData)); + return sha512Half(makeSlice(mData)); } int Serializer::addVL (Blob const& vector) diff --git a/src/ripple/protocol/tests/RippleAddress.test.cpp b/src/ripple/protocol/tests/RippleAddress.test.cpp index d32bace4b3..73820c0759 100644 --- a/src/ripple/protocol/tests/RippleAddress.test.cpp +++ b/src/ripple/protocol/tests/RippleAddress.test.cpp @@ -48,7 +48,7 @@ public: // Check node signing. Blob vucTextSrc = strCopy ("Hello, nurse!"); - uint256 uHash = sha512Half(make_Slice(vucTextSrc)); + uint256 uHash = sha512Half(makeSlice(vucTextSrc)); Blob vucTextSig; naNodePrivate.signNodePrivate (uHash, vucTextSig); diff --git a/src/ripple/rpc/handlers/Submit.cpp b/src/ripple/rpc/handlers/Submit.cpp index bd5a3e953f..102d460d50 100644 --- a/src/ripple/rpc/handlers/Submit.cpp +++ b/src/ripple/rpc/handlers/Submit.cpp @@ -53,7 +53,7 @@ Json::Value doSubmit (RPC::Context& context) if (!ret.second || !ret.first.size ()) return rpcError (rpcINVALID_PARAMS); - SerialIter sitTrans (make_Slice(ret.first)); + SerialIter sitTrans (makeSlice(ret.first)); STTx::pointer stpTrans; diff --git a/src/ripple/shamap/impl/SHAMapTreeNode.cpp b/src/ripple/shamap/impl/SHAMapTreeNode.cpp index bb55b71486..f14407f213 100644 --- a/src/ripple/shamap/impl/SHAMapTreeNode.cpp +++ b/src/ripple/shamap/impl/SHAMapTreeNode.cpp @@ -197,7 +197,7 @@ SHAMapAbstractNode::make(Blob const& rawNode, std::uint32_t seq, SHANodeFormat f if (prefix == HashPrefix::transactionID) { auto item = std::make_shared( - sha512Half(make_Slice(rawNode)), + sha512Half(makeSlice(rawNode)), s.peekData ()); if (hashValid) return std::make_shared(item, tnTRANSACTION_NM, seq, hash); @@ -307,18 +307,18 @@ SHAMapTreeNode::updateHash() if (mType == tnTRANSACTION_NM) { nh = sha512Half(HashPrefix::transactionID, - make_Slice(mItem->peekData())); + makeSlice(mItem->peekData())); } else if (mType == tnACCOUNT_STATE) { nh = sha512Half(HashPrefix::leafNode, - make_Slice(mItem->peekData()), + makeSlice(mItem->peekData()), mItem->key()); } else if (mType == tnTRANSACTION_MD) { nh = sha512Half(HashPrefix::txNode, - make_Slice(mItem->peekData()), + makeSlice(mItem->peekData()), mItem->key()); } else diff --git a/src/ripple/shamap/tests/FetchPack.test.cpp b/src/ripple/shamap/tests/FetchPack.test.cpp index eb2e4ab51d..468695331e 100644 --- a/src/ripple/shamap/tests/FetchPack.test.cpp +++ b/src/ripple/shamap/tests/FetchPack.test.cpp @@ -108,7 +108,7 @@ public: void on_fetch (Map& map, uint256 const& hash, Blob const& blob) { - expect (sha512Half(make_Slice(blob)) == hash, + expect (sha512Half(makeSlice(blob)) == hash, "Hash mismatch"); map.emplace (hash, blob); } diff --git a/src/ripple/test/jtx/impl/multisign.cpp b/src/ripple/test/jtx/impl/multisign.cpp index e99ac8a114..00cecd7145 100644 --- a/src/ripple/test/jtx/impl/multisign.cpp +++ b/src/ripple/test/jtx/impl/multisign.cpp @@ -102,7 +102,7 @@ msig::operator()(Env const& env, JTx& jt) const auto const& e = accounts[i]; auto& jo = js[i]["SigningAccount"]; jo[jss::Account] = e.human(); - jo[jss::SigningPubKey] = strHex(make_Slice( + jo[jss::SigningPubKey] = strHex(makeSlice( e.pk().getAccountPublic())); Serializer ss; @@ -110,7 +110,7 @@ msig::operator()(Env const& env, JTx& jt) const st->addWithoutSigningFields(ss); ss.add160(*signFor); ss.add160(e.id()); - jo["MultiSignature"] = strHex(make_Slice( + jo["MultiSignature"] = strHex(makeSlice( e.sk().accountPrivateSign(ss.getData()))); } @@ -165,7 +165,7 @@ msig2_t::operator()(Env const& env, JTx& jt) const { auto& jj = js[j.first]["SigningAccount"]; jj[jss::Account] = j.second->human(); - jj[jss::SigningPubKey] = strHex(make_Slice( + jj[jss::SigningPubKey] = strHex(makeSlice( j.second->pk().getAccountPublic())); Serializer ss; @@ -173,7 +173,7 @@ msig2_t::operator()(Env const& env, JTx& jt) const st->addWithoutSigningFields(ss); ss.add160(sign_for.id()); ss.add160(j.second->id()); - jj["MultiSignature"] = strHex(make_Slice( + jj["MultiSignature"] = strHex(makeSlice( j.second->sk().accountPrivateSign( ss.getData()))); } diff --git a/src/ripple/test/jtx/impl/utility.cpp b/src/ripple/test/jtx/impl/utility.cpp index 3cf686e27d..822f0e942d 100644 --- a/src/ripple/test/jtx/impl/utility.cpp +++ b/src/ripple/test/jtx/impl/utility.cpp @@ -46,12 +46,12 @@ sign (Json::Value& jv, Account const& account) { jv[jss::SigningPubKey] = - strHex(make_Slice( + strHex(makeSlice( account.pk().getAccountPublic())); Serializer ss; ss.add32 (HashPrefix::txSign); parse(jv).add(ss); - jv[jss::TxnSignature] = strHex(make_Slice( + jv[jss::TxnSignature] = strHex(makeSlice( account.sk().accountPrivateSign( ss.getData()))); }