2016-05-16 20:23:01 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2023-09-14 11:40:35 +02:00
|
|
|
// Copyright (c) 2016 BitPay, Inc.
|
2023-09-18 19:17:33 +02:00
|
|
|
// Copyright (c) 2023 The Dash Core developers
|
2016-05-16 20:23:01 +02:00
|
|
|
// 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:
|
2023-09-18 19:17:33 +02:00
|
|
|
uint256 m_tx_hash;
|
|
|
|
uint32_t m_tx_index{0};
|
2016-05-16 20:23:01 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2016-05-16 20:23:01 +02:00
|
|
|
CSpentIndexKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:17:33 +02:00
|
|
|
CSpentIndexKey(uint256 txout_hash, uint32_t txout_index) :
|
|
|
|
m_tx_hash{txout_hash}, m_tx_index{txout_index} {};
|
|
|
|
|
2016-05-16 20:23:01 +02:00
|
|
|
void SetNull() {
|
2023-09-18 19:17:33 +02:00
|
|
|
m_tx_hash.SetNull();
|
|
|
|
m_tx_index = 0;
|
2016-05-16 20:23:01 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
|
|
|
SERIALIZE_METHODS(CSpentIndexKey, obj)
|
|
|
|
{
|
2023-09-18 19:17:33 +02:00
|
|
|
READWRITE(obj.m_tx_hash, obj.m_tx_index);
|
2023-09-13 18:25:32 +02:00
|
|
|
}
|
2016-05-16 20:23:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CSpentIndexValue {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2023-09-18 19:17:33 +02:00
|
|
|
uint256 m_tx_hash;
|
|
|
|
uint32_t m_tx_index{0};
|
|
|
|
int32_t m_block_height{0};
|
|
|
|
CAmount m_amount{0};
|
2023-09-13 18:52:54 +02:00
|
|
|
uint8_t m_address_type{AddressType::UNKNOWN};
|
2023-09-18 19:17:33 +02:00
|
|
|
uint160 m_address_bytes;
|
2016-05-16 20:23:01 +02:00
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2016-05-16 20:23:01 +02:00
|
|
|
CSpentIndexValue() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:52:54 +02:00
|
|
|
CSpentIndexValue(uint256 txin_hash, uint32_t txin_index, int32_t block_height, CAmount amount, uint8_t address_type,
|
2023-09-18 19:17:33 +02:00
|
|
|
uint160 address_bytes) :
|
|
|
|
m_tx_hash{txin_hash},
|
|
|
|
m_tx_index{txin_index},
|
|
|
|
m_block_height{block_height},
|
|
|
|
m_amount{amount},
|
|
|
|
m_address_type{address_type},
|
|
|
|
m_address_bytes{address_bytes} {};
|
|
|
|
|
2016-05-16 20:23:01 +02:00
|
|
|
void SetNull() {
|
2023-09-18 19:17:33 +02:00
|
|
|
m_tx_hash.SetNull();
|
|
|
|
m_tx_index = 0;
|
|
|
|
m_block_height = 0;
|
|
|
|
m_amount = 0;
|
|
|
|
m_address_type = AddressType::UNKNOWN;
|
|
|
|
m_address_bytes.SetNull();
|
2016-05-16 20:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNull() const {
|
2023-09-18 19:17:33 +02:00
|
|
|
return m_tx_hash.IsNull();
|
2016-05-16 20:23:01 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
SERIALIZE_METHODS(CSpentIndexValue, obj)
|
|
|
|
{
|
2023-09-18 19:17:33 +02:00
|
|
|
READWRITE(obj.m_tx_hash, obj.m_tx_index, obj.m_block_height, obj.m_amount, obj.m_address_type, obj.m_address_bytes);
|
2023-09-13 18:25:32 +02:00
|
|
|
}
|
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) {
|
2023-09-18 19:17:33 +02:00
|
|
|
return std::tie(obj.m_tx_hash, obj.m_tx_index);
|
2023-09-25 16:49:30 +02:00
|
|
|
};
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-09-13 18:25:32 +02:00
|
|
|
struct CAddressUnspentKey {
|
|
|
|
public:
|
2023-09-13 18:52:54 +02:00
|
|
|
uint8_t m_address_type{AddressType::UNKNOWN};
|
2023-09-18 19:17:33 +02:00
|
|
|
uint160 m_address_bytes;
|
|
|
|
uint256 m_tx_hash;
|
2023-09-13 19:28:06 +02:00
|
|
|
uint32_t m_tx_index{0};
|
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() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-13 19:28:06 +02:00
|
|
|
CAddressUnspentKey(uint8_t address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index) :
|
2023-09-18 19:17:33 +02:00
|
|
|
m_address_type{address_type}, m_address_bytes{address_bytes}, m_tx_hash{tx_hash}, m_tx_index{tx_index} {};
|
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
void SetNull() {
|
2023-09-18 19:17:33 +02:00
|
|
|
m_address_type = AddressType::UNKNOWN;
|
|
|
|
m_address_bytes.SetNull();
|
|
|
|
m_tx_hash.SetNull();
|
|
|
|
m_tx_index = 0;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
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 {
|
2023-09-18 19:17:33 +02:00
|
|
|
ser_writedata8(s, m_address_type);
|
|
|
|
m_address_bytes.Serialize(s);
|
|
|
|
m_tx_hash.Serialize(s);
|
|
|
|
ser_writedata32(s, m_tx_index);
|
2023-09-13 18:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
2023-09-18 19:17:33 +02:00
|
|
|
m_address_type = ser_readdata8(s);
|
|
|
|
m_address_bytes.Unserialize(s);
|
|
|
|
m_tx_hash.Unserialize(s);
|
|
|
|
m_tx_index = ser_readdata32(s);
|
2023-09-13 18:25:32 +02:00
|
|
|
}
|
2017-09-15 18:34:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressUnspentValue {
|
2023-09-13 18:25:32 +02:00
|
|
|
public:
|
2023-09-18 19:17:33 +02:00
|
|
|
CAmount m_amount{-1};
|
|
|
|
CScript m_tx_script;
|
|
|
|
int32_t m_block_height;
|
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
|
|
|
CAddressUnspentValue() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:17:33 +02:00
|
|
|
CAddressUnspentValue(CAmount amount, CScript tx_script, int32_t block_height) :
|
|
|
|
m_amount{amount}, m_tx_script{tx_script}, m_block_height{block_height} {};
|
|
|
|
|
2017-09-15 18:34:04 +02:00
|
|
|
void SetNull() {
|
2023-09-18 19:17:33 +02:00
|
|
|
m_amount = -1;
|
|
|
|
m_tx_script.clear();
|
|
|
|
m_block_height = 0;
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNull() const {
|
2023-09-18 19:17:33 +02:00
|
|
|
return (m_amount == -1);
|
2017-09-15 18:34:04 +02:00
|
|
|
}
|
2023-09-13 18:25:32 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
SERIALIZE_METHODS(CAddressUnspentValue, obj)
|
|
|
|
{
|
2023-09-18 19:17:33 +02:00
|
|
|
READWRITE(obj.m_amount, obj.m_tx_script, obj.m_block_height);
|
2023-09-13 18:25:32 +02:00
|
|
|
}
|
2017-09-15 18:34:04 +02:00
|
|
|
};
|
|
|
|
|
2016-05-16 20:23:01 +02:00
|
|
|
#endif // BITCOIN_SPENTINDEX_H
|