Skip to content

Commit

Permalink
Fix get address mempool rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed May 8, 2024
1 parent a31d9dc commit ad3e4b9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/addresstype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ bool ExtractDestination(const COutPoint& prevout, const CScript& scriptPubKey, C
}
return false;
}

int GetAddressIndexType(const CTxDestination &dest)
{
if(dest.index() > 1)
return dest.index() - 1; // PubKeyDestination and PKHash are considered the same
return dest.index();
}
2 changes: 2 additions & 0 deletions src/addresstype.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@ struct DataVisitor

bool ExtractDestination(const COutPoint& prevout, const CScript& scriptPubKey, CTxDestination& addressRet, TxoutType* typeRet = NULL);

int GetAddressIndexType(const CTxDestination& dest);

#endif // BITCOIN_ADDRESSTYPE_H
7 changes: 7 additions & 0 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,13 @@ bool DecodeIndexKey(const std::string &str, uint256 &hashBytes, int &type)
type = 4;
return true;
}

if (std::holds_alternative<WitnessV1Taproot>(dest)) {
const WitnessV1Taproot witnessV1Taproot = std::get<WitnessV1Taproot>(dest);
memcpy(hashBytes.data(), &witnessV1Taproot, 32);
type = 5;
return true;
}
}

return false;
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ bool getAddressFromIndex(const int &type, const uint256 &hash, std::string &addr
} else if (type == 4) {
std::vector<unsigned char> addressBytes(hash.begin(), hash.begin() + 20);
address = EncodeDestination(WitnessV0KeyHash(uint160(addressBytes)));
} else if (type == 5) {
WitnessV1Taproot tap;
std::copy(hash.begin(), hash.end(), tap.begin());
address = EncodeDestination(tap);
} else {
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,8 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
CMempoolAddressDeltaKey key(dest.index(), uint256(addressBytes), txhash, j, 1);
int addressIndexType = GetAddressIndexType(dest);
CMempoolAddressDeltaKey key(addressIndexType, uint256(addressBytes), txhash, j, 1);
CMempoolAddressDelta delta(entry.GetTime().count(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
mapAddress.insert(std::make_pair(key, delta));
inserted.push_back(key);
Expand All @@ -1310,7 +1311,8 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
CMempoolAddressDeltaKey key(dest.index(), uint256(addressBytes), txhash, k, 0);
int addressIndexType = GetAddressIndexType(dest);
CMempoolAddressDeltaKey key(addressIndexType, uint256(addressBytes), txhash, k, 0);
mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime().count(), out.nValue)));
inserted.push_back(key);
}
Expand Down
22 changes: 13 additions & 9 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2282,10 +2282,11 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
int addressIndexType = GetAddressIndexType(dest);
// undo receiving activity
addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, hash, k, false), out.nValue));
addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, hash, k, false), out.nValue));
// undo unspent index
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), hash, k), CAddressUnspentValue()));
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), hash, k), CAddressUnspentValue()));
}
}
}
Expand Down Expand Up @@ -2319,10 +2320,11 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
int addressIndexType = GetAddressIndexType(dest);
// undo spending activity
addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1));
addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1));
// restore unspent index
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight, isTxCoinStake)));
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight, isTxCoinStake)));
}
}
}
Expand Down Expand Up @@ -3472,11 +3474,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), j, true), prevout.nValue * -1));
int addressIndexType = GetAddressIndexType(dest);
addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), j, true), prevout.nValue * -1));

// remove address from unspent index
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue()));
spentIndex.push_back(std::make_pair(CSpentIndexKey(input.prevout.hash, input.prevout.n), CSpentIndexValue(tx.GetHash(), j, pindex->nHeight, prevout.nValue, dest.index(), uint256(addressBytes))));
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue()));
spentIndex.push_back(std::make_pair(CSpentIndexKey(input.prevout.hash, input.prevout.n), CSpentIndexValue(tx.GetHash(), j, pindex->nHeight, prevout.nValue, addressIndexType, uint256(addressBytes))));
}
}
}
Expand Down Expand Up @@ -3697,10 +3700,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
}
valtype addressBytes(32);
std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin());
int addressIndexType = GetAddressIndexType(dest);
// record receiving activity
addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), k, false), out.nValue));
addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), k, false), out.nValue));
// record unspent output
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), tx.GetHash(), k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight, isTxCoinStake)));
addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), tx.GetHash(), k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight, isTxCoinStake)));
}
}
}
Expand Down

0 comments on commit ad3e4b9

Please sign in to comment.