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

View File

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

View File

@ -476,19 +476,19 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
const CTxOut &prevout = coin.out;
if (prevout.scriptPubKey.IsPayToScriptHash()) {
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);
mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key);
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
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);
mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key);
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
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);
mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key);
@ -499,19 +499,19 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
const CTxOut &out = tx.vout[k];
if (out.scriptPubKey.IsPayToScriptHash()) {
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)));
inserted.push_back(key);
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
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)));
inserted.push_back(key);
} else if (out.scriptPubKey.IsPayToPublicKey()) {
uint160 hashBytes{Hash160(Span{out.scriptPubKey.data()+1, out.scriptPubKey.size() - 2})};
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)));
inserted.push_back(key);
}
@ -526,7 +526,7 @@ bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint160, int> > &addresse
LOCK(cs);
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));
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);
ait++;
}