refactor: cleanup addressindex structs, use fixed-length integers

This commit is contained in:
Kittywhiskers Van Gogh 2023-09-18 22:46:04 +05:30
parent 12fd3571e3
commit 53977827ff
3 changed files with 44 additions and 52 deletions

View File

@ -1,5 +1,6 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers // Copyright (c) 2009-2015 The Bitcoin Core developers
// Copyright (c) 2023 The Dash Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -24,56 +25,47 @@ enum AddressType {
struct CMempoolAddressDelta struct CMempoolAddressDelta
{ {
std::chrono::seconds time; public:
CAmount amount; std::chrono::seconds m_time;
uint256 prevhash; CAmount m_amount;
unsigned int prevout; uint256 m_prev_hash;
uint32_t m_prev_out{0};
CMempoolAddressDelta(std::chrono::seconds t, CAmount a, uint256 hash, unsigned int out) { public:
time = t; CMempoolAddressDelta(std::chrono::seconds time, CAmount amount, uint256 prev_hash, uint32_t prev_out) :
amount = a; m_time{time}, m_amount{amount}, m_prev_hash{prev_hash}, m_prev_out{prev_out} {}
prevhash = hash;
prevout = out;
}
CMempoolAddressDelta(std::chrono::seconds t, CAmount a) { CMempoolAddressDelta(std::chrono::seconds time, CAmount amount) :
time = t; m_time{time}, m_amount{amount} {}
amount = a;
prevhash.SetNull();
prevout = 0;
}
}; };
struct CMempoolAddressDeltaKey struct CMempoolAddressDeltaKey
{ {
int type; public:
uint160 addressBytes; int32_t m_address_type;
uint256 txhash; uint160 m_address_bytes;
unsigned int index; uint256 m_tx_hash;
bool is_spent; uint32_t m_tx_index{0};
bool m_tx_spent{false};
CMempoolAddressDeltaKey(int addressType, uint160 addressHash, uint256 hash, unsigned int i, bool s) { public:
type = addressType; CMempoolAddressDeltaKey(int32_t address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index, bool tx_spent) :
addressBytes = addressHash; m_address_type{address_type},
txhash = hash; m_address_bytes{address_bytes},
index = i; m_tx_hash{tx_hash},
is_spent = s; m_tx_index{tx_index},
} m_tx_spent{tx_spent} {};
CMempoolAddressDeltaKey(int addressType, uint160 addressHash) { CMempoolAddressDeltaKey(int32_t address_type, uint160 address_bytes) :
type = addressType; m_address_type{address_type},
addressBytes = addressHash; m_address_bytes{address_bytes} {};
txhash.SetNull();
index = 0;
is_spent = false;
}
}; };
struct CMempoolAddressDeltaKeyCompare 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.is_spent); return std::tie(obj.m_address_type, obj.m_address_bytes, obj.m_tx_hash, obj.m_tx_index, obj.m_tx_spent);
}; };
return to_tuple(a) < to_tuple(b); return to_tuple(a) < to_tuple(b);
} }

View File

@ -660,7 +660,7 @@ static bool heightSort(std::pair<CAddressUnspentKey, CAddressUnspentValue> a,
static bool timestampSort(std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> a, static bool timestampSort(std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> a,
std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> b) { std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> b) {
return a.second.time < b.second.time; return a.second.m_time < b.second.m_time;
} }
static UniValue getaddressmempool(const JSONRPCRequest& request) static UniValue getaddressmempool(const JSONRPCRequest& request)
@ -715,19 +715,19 @@ static UniValue getaddressmempool(const JSONRPCRequest& request)
it != indexes.end(); it++) { it != indexes.end(); it++) {
std::string address; std::string address;
if (!getAddressFromIndex(it->first.type, it->first.addressBytes, address)) { if (!getAddressFromIndex(it->first.m_address_type, it->first.m_address_bytes, address)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unknown address type");
} }
UniValue delta(UniValue::VOBJ); UniValue delta(UniValue::VOBJ);
delta.pushKV("address", address); delta.pushKV("address", address);
delta.pushKV("txid", it->first.txhash.GetHex()); delta.pushKV("txid", it->first.m_tx_hash.GetHex());
delta.pushKV("index", (int)it->first.index); delta.pushKV("index", (int)it->first.m_tx_index);
delta.pushKV("satoshis", it->second.amount); delta.pushKV("satoshis", it->second.m_amount);
delta.pushKV("timestamp", count_seconds(it->second.time)); delta.pushKV("timestamp", count_seconds(it->second.m_time));
if (it->second.amount < 0) { if (it->second.m_amount < 0) {
delta.pushKV("prevtxid", it->second.prevhash.GetHex()); delta.pushKV("prevtxid", it->second.m_prev_hash.GetHex());
delta.pushKV("prevout", (int)it->second.prevout); delta.pushKV("prevout", (int)it->second.m_prev_out);
} }
result.push_back(delta); result.push_back(delta);
} }

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, /* is_spent */ true); CMempoolAddressDeltaKey key(AddressType::P2SH, uint160(hashBytes), txhash, j, /* tx_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, /* is_spent */ true); CMempoolAddressDeltaKey key(AddressType::P2PKH, uint160(hashBytes), txhash, j, /* tx_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, /* is_spent */ true); CMempoolAddressDeltaKey key(AddressType::P2PK, hashBytes, txhash, j, /* tx_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, /* is_spent */ false); CMempoolAddressDeltaKey key(AddressType::P2SH, uint160(hashBytes), txhash, k, /* tx_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, /* is_spent */ false); CMempoolAddressDeltaKey key(AddressType::P2PKH, uint160(hashBytes), txhash, k, /* tx_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, /* is_spent */ false); CMempoolAddressDeltaKey key(AddressType::P2PK, hashBytes, txhash, k, /* tx_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);
} }
@ -526,7 +526,7 @@ bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint160, int> > &addresse
LOCK(cs); LOCK(cs);
for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) { for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) {
addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first)); addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first));
while (ait != mapAddress.end() && (*ait).first.addressBytes == (*it).first && (*ait).first.type == (*it).second) { while (ait != mapAddress.end() && (*ait).first.m_address_bytes == (*it).first && (*ait).first.m_address_type == (*it).second) {
results.push_back(*ait); results.push_back(*ait);
ait++; ait++;
} }