Slice improvements:

* Rename to makeSlice
* Well defined default construction
* Add `empty`
* Add operator[]
* Buffer arithmetic
This commit is contained in:
Vinnie Falco
2015-07-08 08:53:08 -07:00
parent 5962a4817a
commit 362726de4a
18 changed files with 75 additions and 39 deletions

View File

@@ -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<STTx> (std::ref (txnIt));
mRawMeta = sit.getVL ();

View File

@@ -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 (

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<STTx> (it);
std::string reason;

View File

@@ -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<Transaction> (std::make_shared<STTx> (sit),
@@ -130,7 +130,7 @@ Transaction::pointer Transaction::transactionFromSQL (
std::uint32_t const inLedger =
rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or (0));
SerialIter it (make_Slice(rawTxn));
SerialIter it (makeSlice(rawTxn));
auto txn = std::make_shared<STTx> (it);
std::string reason;
auto tr = std::make_shared<Transaction> (txn, validate, reason);

View File

@@ -26,6 +26,7 @@
#include <cassert>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <string>
#include <vector>
#include <beast/cxx14/type_traits.h> // <type_traits>
@@ -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 <class Hasher>
inline
void
@@ -131,14 +167,14 @@ std::enable_if_t<
std::is_same<T, unsigned char>::value,
Slice
>
make_Slice (std::vector<T, Alloc> const& v)
makeSlice (std::vector<T, Alloc> const& v)
{
return Slice(v.data(), v.size());
}
template <class Traits, class Alloc>
Slice
make_Slice (std::basic_string<char, Traits, Alloc> const& s)
makeSlice (std::basic_string<char, Traits, Alloc> const& s)
{
return Slice(s.data(), s.size());
}

View File

@@ -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<NodeObject> object = NodeObject::createObject(

View File

@@ -1010,7 +1010,7 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMTransaction> const& m)
return;
}
SerialIter sit (make_Slice(m->rawtransaction()));
SerialIter sit (makeSlice(m->rawtransaction()));
try
{
@@ -1424,7 +1424,7 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMValidation> 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 <protocol::TMValidation> const& m)
}
if (! getApp().getHashRouter ().addSuppressionPeer(
sha512Half(make_Slice(m->validation())), id_))
sha512Half(makeSlice(m->validation())), id_))
{
p_journal_.trace << "Validation: duplicate";
return;

View File

@@ -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)

View File

@@ -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)

View File

@@ -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);

View File

@@ -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;

View File

@@ -197,7 +197,7 @@ SHAMapAbstractNode::make(Blob const& rawNode, std::uint32_t seq, SHANodeFormat f
if (prefix == HashPrefix::transactionID)
{
auto item = std::make_shared<SHAMapItem const>(
sha512Half(make_Slice(rawNode)),
sha512Half(makeSlice(rawNode)),
s.peekData ());
if (hashValid)
return std::make_shared<SHAMapTreeNode>(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

View File

@@ -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);
}

View File

@@ -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())));
}

View File

@@ -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())));
}