Skip to content

Commit

Permalink
[Core] Prevector type
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 17, 2020
1 parent cc84157 commit 9811a68
Show file tree
Hide file tree
Showing 22 changed files with 907 additions and 76 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ BITCOIN_CORE_H = \
net.h \
noui.h \
pow.h \
prevector.h \
protocol.h \
pubkey.h \
random.h \
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ BITCOIN_TESTS =\
test/net_tests.cpp \
test/netbase_tests.cpp \
test/pmt_tests.cpp \
test/prevector_tests.cpp \
test/random_tests.cpp \
test/reverselock_tests.cpp \
test/rpc_tests.cpp \
Expand Down
3 changes: 1 addition & 2 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ class CCoins
size_t DynamicMemoryUsage() const {
size_t ret = memusage::DynamicUsage(vout);
for(const CTxOut &out : vout) {
const std::vector<unsigned char> *script = &out.scriptPubKey;
ret += memusage::DynamicUsage(*script);
ret += memusage::DynamicUsage(*static_cast<const CScriptBase*>(&out.scriptPubKey));
}
return ret;
}
Expand Down
8 changes: 8 additions & 0 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "crypto/ripemd160.h"
#include "crypto/sha256.h"
#include "prevector.h"
#include "serialize.h"
#include "uint256.h"
#include "version.h"
Expand Down Expand Up @@ -265,6 +266,13 @@ inline uint160 Hash160(const std::vector<unsigned char>& vch)
return Hash160(vch.begin(), vch.end());
}

/** Compute the 160-bit hash of a vector. */
template<unsigned int N>
inline uint160 Hash160(const prevector<N, unsigned char>& vch)
{
return Hash160(vch.begin(), vch.end());
}

/** A writer stream (for serialization) that computes a 256-bit hash. */
class CHashWriter
{
Expand Down
8 changes: 4 additions & 4 deletions src/masternode-budget.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class CTxBudgetPayment
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(payee);
READWRITE(*(CScriptBase*)(&payee));
READWRITE(nAmount);
READWRITE(nProposalHash);
}
Expand Down Expand Up @@ -519,7 +519,7 @@ class CBudgetProposal
ss << nBlockStart;
ss << nBlockEnd;
ss << nAmount;
ss << address;
ss << std::vector<unsigned char>(address.begin(), address.end());
uint256 h1 = ss.GetHash();

return h1;
Expand All @@ -537,7 +537,7 @@ class CBudgetProposal
READWRITE(nBlockStart);
READWRITE(nBlockEnd);
READWRITE(nAmount);
READWRITE(address);
READWRITE(*(CScriptBase*)(&address));
READWRITE(nTime);
READWRITE(nFeeTXHash);

Expand Down Expand Up @@ -594,7 +594,7 @@ class CBudgetProposalBroadcast : public CBudgetProposal
READWRITE(nBlockStart);
READWRITE(nBlockEnd);
READWRITE(nAmount);
READWRITE(address);
READWRITE(*(CScriptBase*)(&address));
READWRITE(nFeeTXHash);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ CMasternodePaymentDB::ReadResult CMasternodePaymentDB::Read(CMasternodePayments&
uint256 CMasternodePaymentWinner::GetHash() const
{
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
ss << payee;
ss << std::vector<unsigned char>(payee.begin(), payee.end());
ss << nBlockHeight;
ss << vinMasternode.prevout;
return ss.GetHash();
Expand Down
4 changes: 2 additions & 2 deletions src/masternode-payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CMasternodePayee
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
{
READWRITE(scriptPubKey);
READWRITE(*(CScriptBase*)(&scriptPubKey));
READWRITE(nVotes);
}
};
Expand Down Expand Up @@ -200,7 +200,7 @@ class CMasternodePaymentWinner : public CSignedMessage
{
READWRITE(vinMasternode);
READWRITE(nBlockHeight);
READWRITE(payee);
READWRITE(*(CScriptBase*)(&payee));
READWRITE(vchSig);
try
{
Expand Down
10 changes: 9 additions & 1 deletion src/memusage.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ template<typename X> static size_t DynamicUsage(const X& x);
static inline size_t MallocUsage(size_t alloc)
{
// Measured on libc6 2.19 on Linux.
if (sizeof(void*) == 8) {
if (alloc == 0) {
return 0;
} else if (sizeof(void*) == 8) {
return ((alloc + 31) >> 4) << 4;
} else if (sizeof(void*) == 4) {
return ((alloc + 15) >> 3) << 3;
Expand Down Expand Up @@ -65,6 +67,12 @@ static inline size_t DynamicUsage(const std::vector<X>& v)
return MallocUsage(v.capacity() * sizeof(X));
}

template<unsigned int N, typename X, typename S, typename D>
static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
{
return MallocUsage(v.allocated_memory());
}

template<typename X>
static inline size_t DynamicUsage(const std::set<X>& s)
{
Expand Down
Loading

0 comments on commit 9811a68

Please sign in to comment.