2016-04-04 22:37:43 +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:16:04 +02:00
|
|
|
// Copyright (c) 2023 The Dash Core developers
|
2016-04-04 22:37:43 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_ADDRESSINDEX_H
|
|
|
|
#define BITCOIN_ADDRESSINDEX_H
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <amount.h>
|
2023-09-25 17:16:43 +02:00
|
|
|
#include <serialize.h>
|
|
|
|
#include <uint256.h>
|
2023-09-25 10:16:15 +02:00
|
|
|
#include <util/underlying.h>
|
2016-04-04 22:37:43 +02:00
|
|
|
|
2019-10-02 16:55:03 +02:00
|
|
|
#include <chrono>
|
2023-09-25 16:49:30 +02:00
|
|
|
#include <tuple>
|
2019-10-02 16:55:03 +02:00
|
|
|
|
2023-09-24 22:54:01 +02:00
|
|
|
class CScript;
|
|
|
|
|
2023-09-25 10:16:15 +02:00
|
|
|
enum class AddressType : uint8_t {
|
2023-09-18 20:39:11 +02:00
|
|
|
P2PK = 1,
|
|
|
|
P2PKH = 1,
|
|
|
|
P2SH = 2,
|
|
|
|
|
|
|
|
UNKNOWN = 0
|
|
|
|
};
|
2023-09-25 10:16:15 +02:00
|
|
|
template<> struct is_serializable_enum<AddressType> : std::true_type {};
|
2023-09-18 20:39:11 +02:00
|
|
|
|
2016-04-04 22:37:43 +02:00
|
|
|
struct CMempoolAddressDelta
|
|
|
|
{
|
2023-09-18 19:16:04 +02:00
|
|
|
public:
|
|
|
|
std::chrono::seconds m_time;
|
|
|
|
CAmount m_amount;
|
|
|
|
uint256 m_prev_hash;
|
|
|
|
uint32_t m_prev_out{0};
|
2016-04-04 22:37:43 +02:00
|
|
|
|
2023-09-18 19:16:04 +02:00
|
|
|
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} {}
|
2016-04-04 22:37:43 +02:00
|
|
|
|
2023-09-18 19:16:04 +02:00
|
|
|
CMempoolAddressDelta(std::chrono::seconds time, CAmount amount) :
|
|
|
|
m_time{time}, m_amount{amount} {}
|
2016-04-04 22:37:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CMempoolAddressDeltaKey
|
|
|
|
{
|
2023-09-18 19:16:04 +02:00
|
|
|
public:
|
2023-09-25 10:16:15 +02:00
|
|
|
AddressType m_address_type{AddressType::UNKNOWN};
|
2023-09-18 19:16:04 +02:00
|
|
|
uint160 m_address_bytes;
|
|
|
|
uint256 m_tx_hash;
|
|
|
|
uint32_t m_tx_index{0};
|
|
|
|
bool m_tx_spent{false};
|
2016-04-04 22:37:43 +02:00
|
|
|
|
2023-09-18 19:16:04 +02:00
|
|
|
public:
|
2023-09-25 10:16:15 +02:00
|
|
|
CMempoolAddressDeltaKey(AddressType address_type, uint160 address_bytes, uint256 tx_hash, uint32_t tx_index, bool tx_spent) :
|
2023-09-18 19:16:04 +02:00
|
|
|
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} {};
|
2016-04-04 22:37:43 +02:00
|
|
|
|
2023-09-25 10:16:15 +02:00
|
|
|
CMempoolAddressDeltaKey(AddressType address_type, uint160 address_bytes) :
|
2023-09-18 19:16:04 +02:00
|
|
|
m_address_type{address_type},
|
|
|
|
m_address_bytes{address_bytes} {};
|
2016-04-04 22:37:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CMempoolAddressDeltaKeyCompare
|
|
|
|
{
|
2016-04-13 05:25:55 +02:00
|
|
|
bool operator()(const CMempoolAddressDeltaKey& a, const CMempoolAddressDeltaKey& b) const {
|
2023-09-25 16:49:30 +02:00
|
|
|
auto to_tuple = [](const CMempoolAddressDeltaKey& obj) {
|
2023-09-18 19:16:04 +02:00
|
|
|
return std::tie(obj.m_address_type, obj.m_address_bytes, obj.m_tx_hash, obj.m_tx_index, obj.m_tx_spent);
|
2023-09-25 16:49:30 +02:00
|
|
|
};
|
|
|
|
return to_tuple(a) < to_tuple(b);
|
2016-04-04 22:37:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-25 16:55:34 +02:00
|
|
|
struct CAddressIndexKey {
|
|
|
|
public:
|
2023-09-25 10:16:15 +02:00
|
|
|
AddressType m_address_type{AddressType::UNKNOWN};
|
2023-09-25 16:55:34 +02:00
|
|
|
uint160 m_address_bytes;
|
|
|
|
int32_t m_block_height{0};
|
|
|
|
uint32_t m_block_tx_pos{0};
|
|
|
|
uint256 m_tx_hash;
|
|
|
|
uint32_t m_tx_index{0};
|
|
|
|
bool m_tx_spent{false};
|
|
|
|
|
|
|
|
public:
|
|
|
|
CAddressIndexKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-25 10:16:15 +02:00
|
|
|
CAddressIndexKey(AddressType address_type, uint160 address_bytes, int32_t block_height, uint32_t block_tx_pos, uint256 tx_hash,
|
2023-09-25 16:55:34 +02:00
|
|
|
uint32_t tx_index, bool tx_spent) :
|
|
|
|
m_address_type{address_type},
|
|
|
|
m_address_bytes{address_bytes},
|
|
|
|
m_block_height{block_height},
|
|
|
|
m_block_tx_pos{block_tx_pos},
|
|
|
|
m_tx_hash{tx_hash},
|
|
|
|
m_tx_index{tx_index},
|
|
|
|
m_tx_spent{tx_spent} {};
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
m_address_type = AddressType::UNKNOWN;
|
|
|
|
m_address_bytes.SetNull();
|
|
|
|
m_block_height = 0;
|
|
|
|
m_block_tx_pos = 0;
|
|
|
|
m_tx_hash.SetNull();
|
|
|
|
m_tx_index = 0;
|
|
|
|
m_tx_spent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
|
|
|
return 66;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
2023-09-25 10:16:15 +02:00
|
|
|
ser_writedata8(s, ToUnderlying(m_address_type));
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_bytes.Serialize(s);
|
|
|
|
// Heights are stored big-endian for key sorting in LevelDB
|
|
|
|
ser_writedata32be(s, m_block_height);
|
|
|
|
ser_writedata32be(s, m_block_tx_pos);
|
|
|
|
m_tx_hash.Serialize(s);
|
|
|
|
ser_writedata32(s, m_tx_index);
|
2023-09-25 16:58:55 +02:00
|
|
|
ser_writedata8(s, static_cast<uint8_t>(m_tx_spent));
|
2023-09-25 16:55:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
2023-09-25 10:16:15 +02:00
|
|
|
m_address_type = static_cast<AddressType>(ser_readdata8(s));
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_bytes.Unserialize(s);
|
|
|
|
m_block_height = ser_readdata32be(s);
|
|
|
|
m_block_tx_pos = ser_readdata32be(s);
|
|
|
|
m_tx_hash.Unserialize(s);
|
|
|
|
m_tx_index = ser_readdata32(s);
|
2023-09-25 16:58:55 +02:00
|
|
|
m_tx_spent = static_cast<bool>(ser_readdata8(s));
|
2023-09-25 16:55:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressIndexIteratorKey {
|
|
|
|
public:
|
2023-09-25 10:16:15 +02:00
|
|
|
AddressType m_address_type{AddressType::UNKNOWN};
|
2023-09-25 16:55:34 +02:00
|
|
|
uint160 m_address_bytes;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CAddressIndexIteratorKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-25 10:16:15 +02:00
|
|
|
CAddressIndexIteratorKey(AddressType address_type, uint160 address_bytes) :
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_type{address_type}, m_address_bytes{address_bytes} {};
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
m_address_type = AddressType::UNKNOWN;
|
|
|
|
m_address_bytes.SetNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
|
|
|
return 21;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
2023-09-25 10:16:15 +02:00
|
|
|
ser_writedata8(s, ToUnderlying(m_address_type));
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_bytes.Serialize(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
2023-09-25 10:16:15 +02:00
|
|
|
m_address_type = static_cast<AddressType>(ser_readdata8(s));
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_bytes.Unserialize(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CAddressIndexIteratorHeightKey {
|
|
|
|
public:
|
2023-09-25 10:16:15 +02:00
|
|
|
AddressType m_address_type{AddressType::UNKNOWN};
|
2023-09-25 16:55:34 +02:00
|
|
|
uint160 m_address_bytes;
|
|
|
|
int32_t m_block_height{0};
|
|
|
|
|
|
|
|
public:
|
|
|
|
CAddressIndexIteratorHeightKey() {
|
|
|
|
SetNull();
|
|
|
|
}
|
|
|
|
|
2023-09-25 10:16:15 +02:00
|
|
|
CAddressIndexIteratorHeightKey(AddressType address_type, uint160 address_bytes, int32_t block_height) :
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_type{address_type}, m_address_bytes{address_bytes}, m_block_height{block_height} {};
|
|
|
|
|
|
|
|
void SetNull() {
|
|
|
|
m_address_type = AddressType::UNKNOWN;
|
|
|
|
m_address_bytes.SetNull();
|
|
|
|
m_block_height = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
size_t GetSerializeSize(int nType, int nVersion) const {
|
|
|
|
return 25;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
2023-09-25 10:16:15 +02:00
|
|
|
ser_writedata8(s, ToUnderlying(m_address_type));
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_bytes.Serialize(s);
|
|
|
|
ser_writedata32be(s, m_block_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
2023-09-25 10:16:15 +02:00
|
|
|
m_address_type = static_cast<AddressType>(ser_readdata8(s));
|
2023-09-25 16:55:34 +02:00
|
|
|
m_address_bytes.Unserialize(s);
|
|
|
|
m_block_height = ser_readdata32be(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-25 10:16:15 +02:00
|
|
|
bool AddressBytesFromScript(const CScript& script, AddressType& address_type, uint160& address_bytes);
|
2023-09-14 20:59:48 +02:00
|
|
|
|
2016-04-04 22:37:43 +02:00
|
|
|
#endif // BITCOIN_ADDRESSINDEX_H
|