Skip to content

Commit

Permalink
Replace direct use of 0 with SetNull and IsNull
Browse files Browse the repository at this point in the history
Replace x=0 with .SetNull(),
x==0 with IsNull(), x!=0 with !IsNull().
Replace uses of uint256(0) with uint256().
  • Loading branch information
laanwj committed Jan 5, 2015
1 parent 5d3064b commit 4f15249
Show file tree
Hide file tree
Showing 28 changed files with 68 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/bitcoin-tx.cpp
Expand Up @@ -315,7 +315,7 @@ static bool findSighashFlags(int& flags, const string& flagStr)
uint256 ParseHashUO(map<string,UniValue>& o, string strKey)
{
if (!o.count(strKey))
return 0;
return uint256();
return ParseHashUV(o[strKey], strKey);
}

Expand Down Expand Up @@ -485,7 +485,7 @@ static void MutateTx(CMutableTransaction& tx, const string& command,
static void OutputTxJSON(const CTransaction& tx)
{
UniValue entry(UniValue::VOBJ);
TxToUniv(tx, 0, entry);
TxToUniv(tx, uint256(), entry);

string jsonOutput = entry.write(4);
fprintf(stdout, "%s\n", jsonOutput.c_str());
Expand Down
8 changes: 4 additions & 4 deletions src/chain.h
Expand Up @@ -150,14 +150,14 @@ class CBlockIndex
nFile = 0;
nDataPos = 0;
nUndoPos = 0;
nChainWork = 0;
nChainWork = uint256();
nTx = 0;
nChainTx = 0;
nStatus = 0;
nSequenceId = 0;

nVersion = 0;
hashMerkleRoot = 0;
hashMerkleRoot = uint256();
nTime = 0;
nBits = 0;
nNonce = 0;
Expand Down Expand Up @@ -282,11 +282,11 @@ class CDiskBlockIndex : public CBlockIndex
uint256 hashPrev;

CDiskBlockIndex() {
hashPrev = 0;
hashPrev = uint256();
}

explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
hashPrev = (pprev ? pprev->GetBlockHash() : 0);
hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
}

ADD_SERIALIZE_METHODS;
Expand Down
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Expand Up @@ -141,7 +141,7 @@ class CMainParams : public CChainParams {
txNew.vout[0].nValue = 50 * COIN;
txNew.vout[0].scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
genesis.vtx.push_back(txNew);
genesis.hashPrevBlock = 0;
genesis.hashPrevBlock.SetNull();
genesis.hashMerkleRoot = genesis.BuildMerkleTree();
genesis.nVersion = 1;
genesis.nTime = 1231006505;
Expand Down
6 changes: 3 additions & 3 deletions src/coins.cpp
Expand Up @@ -42,7 +42,7 @@ bool CCoins::Spend(uint32_t nPos)

bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
uint256 CCoinsView::GetBestBlock() const { return uint256(0); }
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }

Expand All @@ -57,7 +57,7 @@ bool CCoinsViewBacked::GetStats(CCoinsStats &stats) const { return base->GetStat

CCoinsKeyHasher::CCoinsKeyHasher() : salt(GetRandHash()) {}

CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), hashBlock(0) { }
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false) { }

CCoinsViewCache::~CCoinsViewCache()
{
Expand Down Expand Up @@ -128,7 +128,7 @@ bool CCoinsViewCache::HaveCoins(const uint256 &txid) const {
}

uint256 CCoinsViewCache::GetBestBlock() const {
if (hashBlock == uint256(0))
if (hashBlock.IsNull())
hashBlock = base->GetBestBlock();
return hashBlock;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coins.h
Expand Up @@ -297,7 +297,7 @@ struct CCoinsStats
uint256 hashSerialized;
CAmount nTotalAmount;

CCoinsStats() : nHeight(0), hashBlock(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), hashSerialized(0), nTotalAmount(0) {}
CCoinsStats() : nHeight(0), nTransactions(0), nTransactionOutputs(0), nSerializedSize(0), nTotalAmount(0) {}
};


Expand Down
2 changes: 1 addition & 1 deletion src/core_write.cpp
Expand Up @@ -127,7 +127,7 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry)
}
entry.pushKV("vout", vout);

if (hashBlock != 0)
if (!hashBlock.IsNull())
entry.pushKV("blockhash", hashBlock.GetHex());

entry.pushKV("hex", EncodeHexTx(tx)); // the hex-encoded transaction. used the name "hex" to be consistent with the verbose output of "getrawtransaction".
Expand Down
4 changes: 2 additions & 2 deletions src/key.cpp
Expand Up @@ -84,7 +84,7 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_
nonce += test_case;
int nSigLen = 72;
int ret = secp256k1_ecdsa_sign((const unsigned char*)&hash, (unsigned char*)&vchSig[0], &nSigLen, begin(), (unsigned char*)&nonce);
nonce = 0;
nonce = uint256();
if (ret) {
vchSig.resize(nSigLen);
return true;
Expand Down Expand Up @@ -116,7 +116,7 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
uint256 nonce;
prng.Generate((unsigned char*)&nonce, 32);
int ret = secp256k1_ecdsa_sign_compact((const unsigned char*)&hash, &vchSig[1], begin(), (unsigned char*)&nonce, &rec);
nonce = 0;
nonce = uint256();
if (ret)
break;
} while(true);
Expand Down
22 changes: 11 additions & 11 deletions src/main.cpp
Expand Up @@ -261,7 +261,7 @@ struct CNodeState {
nMisbehavior = 0;
fShouldBan = false;
pindexBestKnownBlock = NULL;
hashLastUnknownBlock = uint256(0);
hashLastUnknownBlock.SetNull();
pindexLastCommonBlock = NULL;
fSyncStarted = false;
nStallingSince = 0;
Expand Down Expand Up @@ -349,12 +349,12 @@ void ProcessBlockAvailability(NodeId nodeid) {
CNodeState *state = State(nodeid);
assert(state != NULL);

if (state->hashLastUnknownBlock != 0) {
if (!state->hashLastUnknownBlock.IsNull()) {
BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
state->pindexBestKnownBlock = itOld->second;
state->hashLastUnknownBlock = uint256(0);
state->hashLastUnknownBlock.SetNull();
}
}
}
Expand Down Expand Up @@ -1712,7 +1712,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
return false;

// verify that the view's current state corresponds to the previous block
uint256 hashPrevBlock = pindex->pprev == NULL ? uint256(0) : pindex->pprev->GetBlockHash();
uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
assert(hashPrevBlock == view.GetBestBlock());

// Special case for the genesis block, skipping connection of its transactions
Expand Down Expand Up @@ -2835,7 +2835,7 @@ boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char

CBlockIndex * InsertBlockIndex(uint256 hash)
{
if (hash == 0)
if (hash.IsNull())
return NULL;

// Return existing
Expand Down Expand Up @@ -3369,7 +3369,7 @@ void static ProcessGetData(CNode* pfrom)
vector<CInv> vInv;
vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
pfrom->PushMessage("inv", vInv);
pfrom->hashContinue = 0;
pfrom->hashContinue.SetNull();
}
}
}
Expand Down Expand Up @@ -3604,7 +3604,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
// Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the setAddrKnowns of the chosen nodes prevent repeats
static uint256 hashSalt;
if (hashSalt == 0)
if (hashSalt.IsNull())
hashSalt = GetRandHash();
uint64_t hashAddr = addr.GetHash();
uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60));
Expand Down Expand Up @@ -3738,7 +3738,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (pindex)
pindex = chainActive.Next(pindex);
int nLimit = 500;
LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop==uint256(0) ? "end" : hashStop.ToString(), nLimit, pfrom->id);
LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
for (; pindex; pindex = chainActive.Next(pindex))
{
if (pindex->GetBlockHash() == hashStop)
Expand Down Expand Up @@ -3954,7 +3954,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
// TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
// from there instead.
LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256(0));
pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256());
}
}

Expand Down Expand Up @@ -4452,7 +4452,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
nSyncStarted++;
CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader;
LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256(0));
pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256());
}
}

Expand Down Expand Up @@ -4483,7 +4483,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
{
// 1/4 of tx invs blast to all immediately
static uint256 hashSalt;
if (hashSalt == 0)
if (hashSalt.IsNull())
hashSalt = GetRandHash();
uint256 hashRand = inv.hash ^ hashSalt;
hashRand = Hash(BEGIN(hashRand), END(hashRand));
Expand Down
18 changes: 9 additions & 9 deletions src/merkleblock.cpp
Expand Up @@ -76,15 +76,15 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
if (nBitsUsed >= vBits.size()) {
// overflowed the bits array - failure
fBad = true;
return 0;
return uint256();
}
bool fParentOfMatch = vBits[nBitsUsed++];
if (height==0 || !fParentOfMatch) {
// if at height 0, or nothing interesting below, use stored hash and do not descend
if (nHashUsed >= vHash.size()) {
// overflowed the hash array - failure
fBad = true;
return 0;
return uint256();
}
const uint256 &hash = vHash[nHashUsed++];
if (height==0 && fParentOfMatch) // in case of height 0, we have a matched txid
Expand Down Expand Up @@ -128,16 +128,16 @@ uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch) {
vMatch.clear();
// An empty set will not work
if (nTransactions == 0)
return 0;
return uint256();
// check for excessively high numbers of transactions
if (nTransactions > MAX_BLOCK_SIZE / 60) // 60 is the lower bound for the size of a serialized CTransaction
return 0;
return uint256();
// there can never be more hashes provided than one for every txid
if (vHash.size() > nTransactions)
return 0;
return uint256();
// there must be at least one bit per node in the partial tree, and at least one node per hash
if (vBits.size() < vHash.size())
return 0;
return uint256();
// calculate height of tree
int nHeight = 0;
while (CalcTreeWidth(nHeight) > 1)
Expand All @@ -147,12 +147,12 @@ uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch) {
uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch);
// verify that no problems occured during the tree traversal
if (fBad)
return 0;
return uint256();
// verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
return 0;
return uint256();
// verify that all hashes were consumed
if (nHashUsed != vHash.size())
return 0;
return uint256();
return hashMerkleRoot;
}
2 changes: 1 addition & 1 deletion src/net.cpp
Expand Up @@ -1949,7 +1949,7 @@ CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fIn
nRefCount = 0;
nSendSize = 0;
nSendOffset = 0;
hashContinue = 0;
hashContinue = uint256();
nStartingHeight = -1;
fGetAddr = false;
fRelayTxes = false;
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/block.cpp
Expand Up @@ -74,7 +74,7 @@ uint256 CBlock::BuildMerkleTree(bool* fMutated) const
if (fMutated) {
*fMutated = mutated;
}
return (vMerkleTree.empty() ? 0 : vMerkleTree.back());
return (vMerkleTree.empty() ? uint256() : vMerkleTree.back());
}

std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
Expand All @@ -96,7 +96,7 @@ std::vector<uint256> CBlock::GetMerkleBranch(int nIndex) const
uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
{
if (nIndex == -1)
return 0;
return uint256();
for (std::vector<uint256>::const_iterator it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
{
if (nIndex & 1)
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/block.h
Expand Up @@ -53,8 +53,8 @@ class CBlockHeader
void SetNull()
{
nVersion = CBlockHeader::CURRENT_VERSION;
hashPrevBlock = 0;
hashMerkleRoot = 0;
hashPrevBlock.SetNull();
hashMerkleRoot.SetNull();
nTime = 0;
nBits = 0;
nNonce = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/transaction.cpp
Expand Up @@ -72,7 +72,7 @@ void CTransaction::UpdateHash() const
*const_cast<uint256*>(&hash) = SerializeHash(*this);
}

CTransaction::CTransaction() : hash(0), nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }
CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }

CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {
UpdateHash();
Expand Down
4 changes: 2 additions & 2 deletions src/primitives/transaction.h
Expand Up @@ -28,8 +28,8 @@ class COutPoint
READWRITE(FLATDATA(*this));
}

void SetNull() { hash = 0; n = (uint32_t) -1; }
bool IsNull() const { return (hash == 0 && n == (uint32_t) -1); }
void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }

friend bool operator<(const COutPoint& a, const COutPoint& b)
{
Expand Down
2 changes: 1 addition & 1 deletion src/protocol.cpp
Expand Up @@ -96,7 +96,7 @@ void CAddress::Init()
CInv::CInv()
{
type = 0;
hash = 0;
hash.SetNull();
}

CInv::CInv(int typeIn, const uint256& hashIn)
Expand Down
2 changes: 1 addition & 1 deletion src/pubkey.h
Expand Up @@ -27,7 +27,7 @@
class CKeyID : public uint160
{
public:
CKeyID() : uint160(0) {}
CKeyID() : uint160() {}
CKeyID(const uint160& in) : uint160(in) {}
};

Expand Down
2 changes: 1 addition & 1 deletion src/rest.cpp
Expand Up @@ -240,7 +240,7 @@ static bool rest_tx(AcceptedConnection* conn,
throw RESTERR(HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);

CTransaction tx;
uint256 hashBlock = 0;
uint256 hashBlock = uint256();
if (!GetTransaction(hash, tx, hashBlock, true))
throw RESTERR(HTTP_NOT_FOUND, hashStr + " not found");

Expand Down
2 changes: 1 addition & 1 deletion src/rpcblockchain.cpp
Expand Up @@ -70,7 +70,7 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDe
if(txDetails)
{
Object objTx;
TxToJSON(tx, uint256(0), objTx);
TxToJSON(tx, uint256(), objTx);
txs.push_back(objTx);
}
else
Expand Down
6 changes: 3 additions & 3 deletions src/rpcrawtransaction.cpp
Expand Up @@ -89,7 +89,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry)
}
entry.push_back(Pair("vout", vout));

if (hashBlock != 0) {
if (!hashBlock.IsNull()) {
entry.push_back(Pair("blockhash", hashBlock.GetHex()));
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
if (mi != mapBlockIndex.end() && (*mi).second) {
Expand Down Expand Up @@ -178,7 +178,7 @@ Value getrawtransaction(const Array& params, bool fHelp)
fVerbose = (params[1].get_int() != 0);

CTransaction tx;
uint256 hashBlock = 0;
uint256 hashBlock;
if (!GetTransaction(hash, tx, hashBlock, true))
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available about transaction");

Expand Down Expand Up @@ -438,7 +438,7 @@ Value decoderawtransaction(const Array& params, bool fHelp)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");

Object result;
TxToJSON(tx, 0, result);
TxToJSON(tx, uint256(), result);

return result;
}
Expand Down

0 comments on commit 4f15249

Please sign in to comment.