Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1/3] refactor: port some misc changes from upstream #2485

Merged
merged 5 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ GRIDCOIN_CORE_H = \
node/ui_interface.h \
uint256.h \
util/check.h \
util/hash_type.h \
util/macros.h \
util/overflow.h \
util/reverse_iterator.h \
util/settings.h \
util/strencodings.h \
Expand Down Expand Up @@ -247,6 +249,7 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
gridcoin/voting/registry.cpp \
gridcoin/voting/result.cpp \
gridcoin/voting/vote.cpp \
hash.cpp \
init.cpp \
key.cpp \
keystore.cpp \
Expand Down
10 changes: 5 additions & 5 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ using namespace std;

int CAddrInfo::GetTriedBucket(const uint256& nKey) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetHash().GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetHash().GetCheapHash();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
}

int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src) const
{
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey).GetHash().GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetHash().GetCheapHash();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
}

int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetHash().GetCheapHash();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash();
return hash1 % ADDRMAN_BUCKET_SIZE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool CAlert::IsNull() const

uint256 CAlert::GetHash() const
{
return Hash(this->vchMsg.begin(), this->vchMsg.end());
return Hash(this->vchMsg);
}

bool CAlert::IsInEffect() const
Expand Down Expand Up @@ -148,7 +148,7 @@ bool CAlert::CheckSignature() const
CKey key;
if (!key.SetPubKey(Params().AlertKey()))
return error("CAlert::CheckSignature() : SetPubKey failed");
if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
if (!key.Verify(Hash(vchMsg), vchSig))
return error("CAlert::CheckSignature() : verify signature failed");

// Now unserialize the data
Expand Down
8 changes: 3 additions & 5 deletions src/base58.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2014-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
// file COPYING or https://opensource.org/licenses/mit-license.php.

#include <base58.h>

Expand Down Expand Up @@ -141,8 +141,7 @@ std::string EncodeBase58Check(Span<const unsigned char> input)
{
// add 4-byte hash check to the end
std::vector<unsigned char> vch(input.begin(), input.end());
// Can't use spans here until we teach hash.h about them.
uint256 hash = Hash(vch.begin(), vch.end());
uint256 hash = Hash(vch);
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
return EncodeBase58(vch);
}
Expand All @@ -160,8 +159,7 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
return false;
}
// re-calculate the checksum, ensure it matches the included 4-byte checksum
// Can't use spans here until we teach hash.h about them.
uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4);
uint256 hash = Hash(Span{vchRet}.first(vchRet.size() - 4));
if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
vchRet.clear();
return false;
Expand Down
10 changes: 5 additions & 5 deletions src/consensus/merkle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
}
}
mutated |= (inner[level] == h);
CHash256().Write(inner[level].begin(), 32).Write(h.begin(), 32).Finalize(h.begin());
CHash256().Write(inner[level]).Write(h).Finalize(h);
}
// Store the resulting hash at inner position level.
inner[level] = h;
Expand All @@ -101,7 +101,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
if (pbranch && matchh) {
pbranch->push_back(h);
}
CHash256().Write(h.begin(), 32).Write(h.begin(), 32).Finalize(h.begin());
CHash256().Write(h).Write(h).Finalize(h);
// Increment count to the value it would have if two entries at this
// level had existed.
count += (((uint32_t)1) << level);
Expand All @@ -116,7 +116,7 @@ static void MerkleComputation(const std::vector<uint256>& leaves, uint256* proot
matchh = true;
}
}
CHash256().Write(inner[level].begin(), 32).Write(h.begin(), 32).Finalize(h.begin());
CHash256().Write(inner[level]).Write(h).Finalize(h);
level++;
}
}
Expand All @@ -141,9 +141,9 @@ uint256 ComputeMerkleRootFromBranch(const uint256& leaf, const std::vector<uint2
uint256 hash = leaf;
for (std::vector<uint256>::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) {
if (nIndex & 1) {
hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
hash = Hash(*it, hash);
} else {
hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
hash = Hash(hash, *it);
}
nIndex >>= 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ bool CTxDB::LoadBlockIndex()
{
// Unpack keys and values.
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.write(iterator->key().data(), iterator->key().size());
ssKey.write(MakeByteSpan(iterator->key()));
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.write(iterator->value().data(), iterator->value().size());
ssValue.write(MakeByteSpan(iterator->value()));
string strType;
ssKey >> strType;
// Did we reach the end of the data to read?
Expand Down
13 changes: 6 additions & 7 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class CTxDB
}
// Unserialize value
try {
CDataStream ssValue(strValue.data(), strValue.data() + strValue.size(),
SER_DISK, CLIENT_VERSION);
CDataStream ssValue(MakeByteSpan(strValue), SER_DISK, CLIENT_VERSION);
ssValue >> value;
}
catch (std::exception &e) {
Expand Down Expand Up @@ -241,10 +240,10 @@ class CTxDB
{
// Unpack keys and values.
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.write(iterator->key().data(), iterator->key().size());
ssKey.write(MakeByteSpan(iterator->key()));

CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.write(iterator->value().data(), iterator->value().size());
ssValue.write(MakeByteSpan(iterator->value()));

T str_key_type;
ssKey >> str_key_type;
Expand Down Expand Up @@ -298,10 +297,10 @@ class CTxDB
{
// Unpack keys and values.
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.write(iterator->key().data(), iterator->key().size());
ssKey.write(MakeByteSpan(iterator->key()));

CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.write(iterator->value().data(), iterator->value().size());
ssValue.write(MakeByteSpan(iterator->value()));

T str_key_type;
ssKey >> str_key_type;
Expand Down Expand Up @@ -389,7 +388,7 @@ class CTxDB
{
// Unpack keys and values.
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.write(iterator->key().data(), iterator->key().size());
ssKey.write(MakeByteSpan(iterator->key()));

T str_key_type;
ssKey >> str_key_type;
Expand Down
4 changes: 2 additions & 2 deletions src/gridcoin/accrual/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -1044,14 +1044,14 @@ class AccrualSnapshotRegistry
//!
//! TODO: encapsulate this
//!
void write(const char* pch, size_t size)
void write(Span<const std::byte> src)
{
if (!m_file) {
throw std::ios_base::failure(
strprintf("%s: file handle is nullptr", __func__));
}

if (fwrite(pch, 1, size, m_file) != size) {
if (fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
throw std::ios_base::failure(
strprintf("%s: write failed", __func__));
}
Expand Down
22 changes: 4 additions & 18 deletions src/gridcoin/beacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,7 @@ void BeaconRegistry::ActivatePending(
// hash of the block hash, and the pending beacon that is being activated's hash is sufficient.
activated_beacon.m_status = BeaconStatusForStorage::ACTIVE;

activated_beacon.m_hash = Hash(block_hash.begin(),
block_hash.end(),
found_pending_beacon->m_hash.begin(),
found_pending_beacon->m_hash.end());
activated_beacon.m_hash = Hash(block_hash, found_pending_beacon->m_hash);

LogPrint(LogFlags::BEACON, "INFO: %s: Activating beacon for cpid %s, address %s, hash %s.",
__func__,
Expand Down Expand Up @@ -886,11 +883,7 @@ void BeaconRegistry::ActivatePending(
pending_beacon.m_status = BeaconStatusForStorage::EXPIRED_PENDING;

// Set the beacon entry's hash to a synthetic block hash similar to above.
pending_beacon.m_hash = Hash(block_hash.begin(),
block_hash.end(),
pending_beacon.m_hash.begin(),
pending_beacon.m_hash.end());

pending_beacon.m_hash = Hash(block_hash, pending_beacon.m_hash);
LogPrint(LogFlags::BEACON, "INFO: %s: Marking pending beacon expired for cpid %s, address %s, hash %s.",
__func__,
pending_beacon.m_cpid.ToString(),
Expand All @@ -917,11 +910,7 @@ void BeaconRegistry::Deactivate(const uint256 superblock_hash)
for (auto iter = m_beacons.begin(); iter != m_beacons.end();) {
Cpid cpid = iter->second->m_cpid;

uint256 activation_hash = Hash(superblock_hash.begin(),
superblock_hash.end(),
iter->second->m_prev_beacon_hash.begin(),
iter->second->m_prev_beacon_hash.end());

uint256 activation_hash = Hash(superblock_hash, iter->second->m_prev_beacon_hash);
// If we have an active beacon whose hash matches the composite hash assigned by ActivatePending...
if (iter->second->m_hash == activation_hash) {
// Find the pending beacon entry in the db before the activation. This is the previous state record.
Expand Down Expand Up @@ -965,10 +954,7 @@ void BeaconRegistry::Deactivate(const uint256 superblock_hash)
// The cpid in the historical beacon record to be matched.
Cpid cpid = iter->second->m_cpid;

uint256 match_hash = Hash(superblock_hash.begin(),
superblock_hash.end(),
iter->second->m_prev_beacon_hash.begin(),
iter->second->m_prev_beacon_hash.end());
uint256 match_hash = Hash(superblock_hash, iter->second->m_prev_beacon_hash);

// If the calculated match_hash matches the key (hash) of the historical beacon record, then
// restore the previous record pointed to by the historical beacon record to the pending map.
Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/claim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ uint256 GetClaimHash(
const std::string cpid_hex = cpid->ToString();
const std::string hash_hex = BlockHashToString(last_block_hash);

return Hash(cpid_hex.begin(), cpid_hex.end(), hash_hex.begin(), hash_hex.end());
return Hash(cpid_hex, hash_hex);
}
} // anonymous namespace

Expand Down
8 changes: 1 addition & 7 deletions src/gridcoin/contract/contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,7 @@ bool CheckLegacyContract(const Contract& contract, const CTransaction& tx)
const ContractPayload payload = contract.m_body.AssumeLegacy();
const auto& body = static_cast<const LegacyPayload&>(*payload);

const uint256 body_hash = Hash(
type_string.begin(),
type_string.end(),
body.m_key.begin(),
body.m_key.end(),
body.m_value.begin(),
body.m_value.end());
const uint256 body_hash = Hash(type_string, body.m_key, body.m_value);

CKey key;
key.SetPubKey(CWallet::MasterPublicKey());
Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/cpid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool Cpid::Matches(const std::string& internal, const std::string& email) const

std::string Cpid::ToString() const
{
return HexStr(m_bytes.begin(), m_bytes.end());
return HexStr(m_bytes);
}

// -----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/gridcoin/cpid.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Cpid
template<typename Stream>
void Serialize(Stream& stream) const
{
stream.write(CharCast(m_bytes.data()), m_bytes.size());
stream.write(MakeByteSpan(m_bytes));
}

//!
Expand All @@ -189,7 +189,7 @@ class Cpid
template<typename Stream>
void Unserialize(Stream& stream)
{
stream.read(CharCast(m_bytes.data()), m_bytes.size());
stream.read(MakeWritableByteSpan(m_bytes));
}

private:
Expand Down
8 changes: 4 additions & 4 deletions src/gridcoin/quorum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ class SuperblockValidator
const uint256& manifest_hash = hashes.second.first;
const uint256& content_hash = hashes.second.second;

if (content_hash.GetUint64() >> 32 == m_superblock->m_manifest_content_hint) {
if (content_hash.GetUint64(0) >> 32 == m_superblock->m_manifest_content_hint) {
if (!content_hash_tally.emplace(content_hash, 1).second) {
content_hash_tally[content_hash]++;
}
Expand Down Expand Up @@ -1478,7 +1478,7 @@ class SuperblockValidator
LOCK(CSplitBlob::cs_mapParts);

for (const auto& part_pair : CSplitBlob::mapParts) {
const uint32_t hint = part_pair.first.GetUint64() >> m_hint_shift;
const uint32_t hint = part_pair.first.GetUint64(0) >> m_hint_shift;
const auto hint_range = hints.equal_range(hint);

for (auto i = hint_range.first; i != hint_range.second; ++i) {
Expand Down Expand Up @@ -1642,11 +1642,11 @@ std::vector<ExplainMagnitudeProject> Quorum::ExplainMagnitude(const Cpid cpid)
CreateSuperblock();

const std::string cpid_str = cpid.ToString();
const Span<const char> cpid_span = MakeSpan(cpid_str);
const Span<const char> cpid_span = Span{cpid_str};

// Compare the stats entry CPID and return the project name if it matches:
const auto try_item = [&](const std::string& object_id) {
const Span<const char> id_span = MakeSpan(object_id);
const Span<const char> id_span = Span{object_id};
const Span<const char> cpid_subspan = id_span.last(32);

if (cpid_subspan != cpid_span) {
Expand Down
Loading