2016-05-16 20:23:01 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_SPENTINDEX_H
|
|
|
|
#define BITCOIN_SPENTINDEX_H
|
|
|
|
|
2023-09-18 20:39:11 +02:00
|
|
|
#include <addressindex.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <amount.h>
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <serialize.h>
|
2023-09-18 20:39:11 +02:00
|
|
|
#include <uint256.h>
|
2016-05-16 20:23:01 +02:00
|
|
|
|
2023-09-25 16:49:30 +02:00
|
|
|
#include <tuple>
|
|
|
|
|
2016-05-16 20:23:01 +02:00
|
|
|
struct CSpentIndexKey {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2016-05-16 20:23:01 +02:00
|
|
|
uint256 txid;
|
|
|
|
unsigned int outputIndex;
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2016-05-16 20:23:01 +02:00
|
|
|
CSpentIndexKey(uint256 t, unsigned int i) {
|
|
|
|
txid = t;
|
|
|
|
outputIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpentIndexKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
txid.SetNull();
|
|
|
|
outputIndex = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
|
|
|
SERIALIZE_METHODS(CSpentIndexKey, obj)
|
|
|
|
{
|
|
|
|
READWRITE(obj.txid, obj.outputIndex);
|
|
|
|
}
|
2016-05-16 20:23:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CSpentIndexValue {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2016-05-16 20:23:01 +02:00
|
|
|
uint256 txid;
|
|
|
|
unsigned int inputIndex;
|
|
|
|
int blockHeight;
|
|
|
|
CAmount satoshis;
|
|
|
|
int addressType;
|
|
|
|
uint160 addressHash;
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2016-05-16 20:23:01 +02:00
|
|
|
CSpentIndexValue(uint256 t, unsigned int i, int h, CAmount s, int type, uint160 a) {
|
|
|
|
txid = t;
|
|
|
|
inputIndex = i;
|
|
|
|
blockHeight = h;
|
|
|
|
satoshis = s;
|
|
|
|
addressType = type;
|
|
|
|
addressHash = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpentIndexValue() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
txid.SetNull();
|
|
|
|
inputIndex = 0;
|
|
|
|
blockHeight = 0;
|
|
|
|
satoshis = 0;
|
2023-09-18 20:39:11 +02:00
|
|
|
addressType = AddressType::UNKNOWN;
|
2016-05-16 20:23:01 +02:00
|
|
|
addressHash.SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNull() const {
|
|
|
|
return txid.IsNull();
|
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
SERIALIZE_METHODS(CSpentIndexValue, obj)
|
|
|
|
{
|
|
|
|
READWRITE(obj.txid, obj.inputIndex, obj.blockHeight, obj.satoshis, obj.addressType, obj.addressHash);
|
|
|
|
}
|
2016-05-16 20:23:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CSpentIndexKeyCompare
|
|
|
|
{
|
|
|
|
bool operator()(const CSpentIndexKey& a, const CSpentIndexKey& b) const {
|
2023-09-25 16:49:30 +02:00
|
|
|
auto to_tuple = [](const CSpentIndexKey& obj) {
|
|
|
|
return std::tie(obj.txid, obj.outputIndex);
|
|
|
|
};
|
|
|
|
return to_tuple(a) < to_tuple(b);
|
2016-05-16 20:23:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-11 13:42:17 +02:00
|
|
|
struct CSpentIndexTxInfo
|
|
|
|
{
|
|
|
|
std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mSpentInfo;
|
|
|
|
};
|
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
struct CTimestampIndexIteratorKey {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
unsigned int timestamp;
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CTimestampIndexIteratorKey(unsigned int time) {
|
|
|
|
timestamp = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTimestampIndexIteratorKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
timestamp = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
2023-09-13 18:25:32 +02:00
|
|
|
return 4;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Serialize(Stream& s) const {
|
2017-09-15 18:34:04 +02:00
|
|
|
ser_writedata32be(s, timestamp);
|
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Unserialize(Stream& s) {
|
2017-09-15 18:34:04 +02:00
|
|
|
timestamp = ser_readdata32be(s);
|
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CTimestampIndexKey {
|
|
|
|
public:
|
|
|
|
unsigned int timestamp;
|
|
|
|
uint256 blockHash;
|
2017-09-15 18:34:04 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CTimestampIndexKey(unsigned int time, uint256 hash) {
|
|
|
|
timestamp = time;
|
|
|
|
blockHash = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
CTimestampIndexKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
timestamp = 0;
|
|
|
|
blockHash.SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
2023-09-13 18:25:32 +02:00
|
|
|
return 36;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Serialize(Stream& s) const {
|
2023-09-13 18:25:32 +02:00
|
|
|
ser_writedata32be(s, timestamp);
|
|
|
|
blockHash.Serialize(s);
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Unserialize(Stream& s) {
|
2023-09-13 18:25:32 +02:00
|
|
|
timestamp = ser_readdata32be(s);
|
|
|
|
blockHash.Unserialize(s);
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressUnspentKey {
|
|
|
|
public:
|
|
|
|
unsigned int type;
|
|
|
|
uint160 hashBytes;
|
|
|
|
uint256 txhash;
|
|
|
|
size_t index;
|
2017-09-15 18:34:04 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CAddressUnspentKey(unsigned int addressType, uint160 addressHash, uint256 txid, size_t indexValue) {
|
|
|
|
type = addressType;
|
|
|
|
hashBytes = addressHash;
|
|
|
|
txhash = txid;
|
|
|
|
index = indexValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddressUnspentKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
2023-09-18 20:39:11 +02:00
|
|
|
type = AddressType::UNKNOWN;
|
2017-09-15 18:34:04 +02:00
|
|
|
hashBytes.SetNull();
|
|
|
|
txhash.SetNull();
|
|
|
|
index = 0;
|
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
|
|
|
return 57;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
|
|
|
ser_writedata8(s, type);
|
|
|
|
hashBytes.Serialize(s);
|
|
|
|
txhash.Serialize(s);
|
|
|
|
ser_writedata32(s, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
|
|
|
type = ser_readdata8(s);
|
|
|
|
hashBytes.Unserialize(s);
|
|
|
|
txhash.Unserialize(s);
|
|
|
|
index = ser_readdata32(s);
|
|
|
|
}
|
2017-09-15 18:34:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressUnspentValue {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CAmount satoshis;
|
|
|
|
CScript script;
|
|
|
|
int blockHeight;
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CAddressUnspentValue(CAmount sats, CScript scriptPubKey, int height) {
|
|
|
|
satoshis = sats;
|
|
|
|
script = scriptPubKey;
|
|
|
|
blockHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddressUnspentValue() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
satoshis = -1;
|
|
|
|
script.clear();
|
|
|
|
blockHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNull() const {
|
|
|
|
return (satoshis == -1);
|
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
SERIALIZE_METHODS(CAddressUnspentValue, obj)
|
|
|
|
{
|
|
|
|
READWRITE(obj.satoshis, obj.script, obj.blockHeight);
|
|
|
|
}
|
2017-09-15 18:34:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressIndexKey {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
unsigned int type;
|
|
|
|
uint160 hashBytes;
|
|
|
|
int blockHeight;
|
|
|
|
unsigned int txindex;
|
|
|
|
uint256 txhash;
|
|
|
|
size_t index;
|
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)
2023-09-13 19:24:19 +02:00
|
|
|
bool is_spent;
|
2017-09-15 18:34:04 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CAddressIndexKey(unsigned int addressType, uint160 addressHash, int height, int blockindex,
|
|
|
|
uint256 txid, size_t indexValue, bool isSpending) {
|
|
|
|
type = addressType;
|
|
|
|
hashBytes = addressHash;
|
|
|
|
blockHeight = height;
|
|
|
|
txindex = blockindex;
|
|
|
|
txhash = txid;
|
|
|
|
index = indexValue;
|
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)
2023-09-13 19:24:19 +02:00
|
|
|
is_spent = isSpending;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CAddressIndexKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
2023-09-18 20:39:11 +02:00
|
|
|
type = AddressType::UNKNOWN;
|
2017-09-15 18:34:04 +02:00
|
|
|
hashBytes.SetNull();
|
|
|
|
blockHeight = 0;
|
|
|
|
txindex = 0;
|
|
|
|
txhash.SetNull();
|
|
|
|
index = 0;
|
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)
2023-09-13 19:24:19 +02:00
|
|
|
is_spent = false;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
2023-09-13 18:25:32 +02:00
|
|
|
return 66;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Serialize(Stream& s) const {
|
2017-09-15 18:34:04 +02:00
|
|
|
ser_writedata8(s, type);
|
2017-09-19 21:36:55 +02:00
|
|
|
hashBytes.Serialize(s);
|
2023-09-13 18:25:32 +02:00
|
|
|
// Heights are stored big-endian for key sorting in LevelDB
|
|
|
|
ser_writedata32be(s, blockHeight);
|
|
|
|
ser_writedata32be(s, txindex);
|
|
|
|
txhash.Serialize(s);
|
|
|
|
ser_writedata32(s, index);
|
|
|
|
char f = is_spent;
|
|
|
|
ser_writedata8(s, f);
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Unserialize(Stream& s) {
|
2017-09-15 18:34:04 +02:00
|
|
|
type = ser_readdata8(s);
|
2017-09-19 21:36:55 +02:00
|
|
|
hashBytes.Unserialize(s);
|
2023-09-13 18:25:32 +02:00
|
|
|
blockHeight = ser_readdata32be(s);
|
|
|
|
txindex = ser_readdata32be(s);
|
|
|
|
txhash.Unserialize(s);
|
|
|
|
index = ser_readdata32(s);
|
|
|
|
char f = ser_readdata8(s);
|
|
|
|
is_spent = f;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressIndexIteratorKey {
|
|
|
|
public:
|
|
|
|
unsigned int type;
|
|
|
|
uint160 hashBytes;
|
2017-09-15 18:34:04 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CAddressIndexIteratorKey(unsigned int addressType, uint160 addressHash) {
|
|
|
|
type = addressType;
|
|
|
|
hashBytes = addressHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddressIndexIteratorKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
2023-09-18 20:39:11 +02:00
|
|
|
type = AddressType::UNKNOWN;
|
2017-09-15 18:34:04 +02:00
|
|
|
hashBytes.SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
2023-09-13 18:25:32 +02:00
|
|
|
return 21;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Serialize(Stream& s) const {
|
2017-09-15 18:34:04 +02:00
|
|
|
ser_writedata8(s, type);
|
2017-09-19 21:36:55 +02:00
|
|
|
hashBytes.Serialize(s);
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
template<typename Stream>
|
2017-09-19 21:36:55 +02:00
|
|
|
void Unserialize(Stream& s) {
|
2017-09-15 18:34:04 +02:00
|
|
|
type = ser_readdata8(s);
|
2017-09-19 21:36:55 +02:00
|
|
|
hashBytes.Unserialize(s);
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
};
|
2017-09-15 18:34:04 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
struct CAddressIndexIteratorHeightKey {
|
|
|
|
public:
|
|
|
|
unsigned int type;
|
|
|
|
uint160 hashBytes;
|
|
|
|
int blockHeight;
|
|
|
|
|
|
|
|
public:
|
2017-09-15 18:34:04 +02:00
|
|
|
CAddressIndexIteratorHeightKey(unsigned int addressType, uint160 addressHash, int height) {
|
|
|
|
type = addressType;
|
|
|
|
hashBytes = addressHash;
|
|
|
|
blockHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddressIndexIteratorHeightKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNull() {
|
2023-09-18 20:39:11 +02:00
|
|
|
type = AddressType::UNKNOWN;
|
2017-09-15 18:34:04 +02:00
|
|
|
hashBytes.SetNull();
|
|
|
|
blockHeight = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
|
|
|
return 25;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
|
|
|
ser_writedata8(s, type);
|
|
|
|
hashBytes.Serialize(s);
|
|
|
|
ser_writedata32be(s, blockHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
|
|
|
type = ser_readdata8(s);
|
|
|
|
hashBytes.Unserialize(s);
|
|
|
|
blockHeight = ser_readdata32be(s);
|
|
|
|
}
|
|
|
|
};
|
2017-09-15 18:34:04 +02:00
|
|
|
|
2016-05-16 20:23:01 +02:00
|
|
|
#endif // BITCOIN_SPENTINDEX_H
|