refactor: use boolean for transaction state, rename variable

there are two problems with "int spending;"
- the integer, which implies that there are gradient states when
  there is none, only boolean (vin is spent, vout is UTXO)
- the name, "spending" implies the existence of a middle state,
  there is none

a reason why int may have been used is due to needing it in the
comparison struct CMempoolAddressDeltaKeyCompare, though, using
an int isn't necessary as when used with a comparison operator,
a bool is implicitly converted to an int.

see, https://en.cppreference.com/w/cpp/language/implicit_conversion
(Integral promotion)
This commit is contained in:
Kittywhiskers Van Gogh 2023-09-13 22:54:19 +05:30
parent 5950f7ce35
commit 12fd3571e3
3 changed files with 16 additions and 16 deletions

View File

@ -50,14 +50,14 @@ struct CMempoolAddressDeltaKey
uint160 addressBytes; uint160 addressBytes;
uint256 txhash; uint256 txhash;
unsigned int index; unsigned int index;
int spending; bool is_spent;
CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, int s) { CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, bool s) {
type = addressType; type = addressType;
addressBytes = addressHash; addressBytes = addressHash;
txhash = hash; txhash = hash;
index = i; index = i;
spending = s; is_spent = s;
} }
CMempoolAddressDeltaKey(int addressType, uint160 addressHash) { CMempoolAddressDeltaKey(int addressType, uint160 addressHash) {
@ -65,7 +65,7 @@ struct CMempoolAddressDeltaKey
addressBytes = addressHash; addressBytes = addressHash;
txhash.SetNull(); txhash.SetNull();
index = 0; index = 0;
spending = 0; is_spent = false;
} }
}; };
@ -73,7 +73,7 @@ struct CMempoolAddressDeltaKeyCompare
{ {
bool operator()(const CMempoolAddressDeltaKey& a, const CMempoolAddressDeltaKey& b) const { bool operator()(const CMempoolAddressDeltaKey& a, const CMempoolAddressDeltaKey& b) const {
auto to_tuple = [](const CMempoolAddressDeltaKey& obj) { auto to_tuple = [](const CMempoolAddressDeltaKey& obj) {
return std::tie(obj.type, obj.addressBytes, obj.txhash, obj.index, obj.spending); return std::tie(obj.type, obj.addressBytes, obj.txhash, obj.index, obj.is_spent);
}; };
return to_tuple(a) < to_tuple(b); return to_tuple(a) < to_tuple(b);
} }

View File

@ -236,7 +236,7 @@ struct CAddressIndexKey {
unsigned int txindex; unsigned int txindex;
uint256 txhash; uint256 txhash;
size_t index; size_t index;
bool spending; bool is_spent;
size_t GetSerializeSize(int nType, int nVersion) const { size_t GetSerializeSize(int nType, int nVersion) const {
return 66; return 66;
@ -250,7 +250,7 @@ struct CAddressIndexKey {
ser_writedata32be(s, txindex); ser_writedata32be(s, txindex);
txhash.Serialize(s); txhash.Serialize(s);
ser_writedata32(s, index); ser_writedata32(s, index);
char f = spending; char f = is_spent;
ser_writedata8(s, f); ser_writedata8(s, f);
} }
template<typename Stream> template<typename Stream>
@ -262,7 +262,7 @@ struct CAddressIndexKey {
txhash.Unserialize(s); txhash.Unserialize(s);
index = ser_readdata32(s); index = ser_readdata32(s);
char f = ser_readdata8(s); char f = ser_readdata8(s);
spending = f; is_spent = f;
} }
CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex, CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex,
@ -273,7 +273,7 @@ struct CAddressIndexKey {
txindex = blockindex; txindex = blockindex;
txhash = txid; txhash = txid;
index = indexValue; index = indexValue;
spending = isSpending; is_spent = isSpending;
} }
CAddressIndexKey() { CAddressIndexKey() {
@ -287,7 +287,7 @@ struct CAddressIndexKey {
txindex = 0; txindex = 0;
txhash.SetNull(); txhash.SetNull();
index = 0; index = 0;
spending = false; is_spent = false;
} }
}; };

View File

@ -476,19 +476,19 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
const CTxOut &prevout = coin.out; const CTxOut &prevout = coin.out;
if (prevout.scriptPubKey.IsPayToScriptHash()) { if (prevout.scriptPubKey.IsPayToScriptHash()) {
std::vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22); std::vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22);
CMempoolAddressDeltaKey key(AddressType::P2SH, uint160(hashBytes), txhash, j, 1); CMempoolAddressDeltaKey key(AddressType::P2SH, uint160(hashBytes), txhash, j, /* is_spent */ true);
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n); CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(std::make_pair(key, delta)); mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key); inserted.push_back(key);
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) { } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
std::vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23); std::vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23);
CMempoolAddressDeltaKey key(AddressType::P2PKH, uint160(hashBytes), txhash, j, 1); CMempoolAddressDeltaKey key(AddressType::P2PKH, uint160(hashBytes), txhash, j, /* is_spent */ true);
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n); CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(std::make_pair(key, delta)); mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key); inserted.push_back(key);
} else if (prevout.scriptPubKey.IsPayToPublicKey()) { } else if (prevout.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes{Hash160(Span{prevout.scriptPubKey.data()+1, prevout.scriptPubKey.size() - 2})}; uint160 hashBytes{Hash160(Span{prevout.scriptPubKey.data()+1, prevout.scriptPubKey.size() - 2})};
CMempoolAddressDeltaKey key(AddressType::P2PK, hashBytes, txhash, j, 1); CMempoolAddressDeltaKey key(AddressType::P2PK, hashBytes, txhash, j, /* is_spent */ true);
CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n); CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(std::make_pair(key, delta)); mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key); inserted.push_back(key);
@ -499,19 +499,19 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
const CTxOut &out = tx.vout[k]; const CTxOut &out = tx.vout[k];
if (out.scriptPubKey.IsPayToScriptHash()) { if (out.scriptPubKey.IsPayToScriptHash()) {
std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22); std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
CMempoolAddressDeltaKey key(AddressType::P2SH, uint160(hashBytes), txhash, k, 0); CMempoolAddressDeltaKey key(AddressType::P2SH, uint160(hashBytes), txhash, k, /* is_spent */ false);
mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue))); mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
inserted.push_back(key); inserted.push_back(key);
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) { } else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23); std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
std::pair<addressDeltaMap::iterator,bool> ret; std::pair<addressDeltaMap::iterator,bool> ret;
CMempoolAddressDeltaKey key(AddressType::P2PKH, uint160(hashBytes), txhash, k, 0); CMempoolAddressDeltaKey key(AddressType::P2PKH, uint160(hashBytes), txhash, k, /* is_spent */ false);
mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue))); mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
inserted.push_back(key); inserted.push_back(key);
} else if (out.scriptPubKey.IsPayToPublicKey()) { } else if (out.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes{Hash160(Span{out.scriptPubKey.data()+1, out.scriptPubKey.size() - 2})}; uint160 hashBytes{Hash160(Span{out.scriptPubKey.data()+1, out.scriptPubKey.size() - 2})};
std::pair<addressDeltaMap::iterator,bool> ret; std::pair<addressDeltaMap::iterator,bool> ret;
CMempoolAddressDeltaKey key(AddressType::P2PK, hashBytes, txhash, k, 0); CMempoolAddressDeltaKey key(AddressType::P2PK, hashBytes, txhash, k, /* is_spent */ false);
mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue))); mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
inserted.push_back(key); inserted.push_back(key);
} }