dash/src/spentindex.h

386 lines
9.3 KiB
C
Raw Normal View History

2016-05-16 20:23:01 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// 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
#include <addressindex.h>
Backport 11651 (#3358) * scripted-diff: Replace #include "" with #include <> (ryanofsky) -BEGIN VERIFY SCRIPT- for f in \ src/*.cpp \ src/*.h \ src/bench/*.cpp \ src/bench/*.h \ src/compat/*.cpp \ src/compat/*.h \ src/consensus/*.cpp \ src/consensus/*.h \ src/crypto/*.cpp \ src/crypto/*.h \ src/crypto/ctaes/*.h \ src/policy/*.cpp \ src/policy/*.h \ src/primitives/*.cpp \ src/primitives/*.h \ src/qt/*.cpp \ src/qt/*.h \ src/qt/test/*.cpp \ src/qt/test/*.h \ src/rpc/*.cpp \ src/rpc/*.h \ src/script/*.cpp \ src/script/*.h \ src/support/*.cpp \ src/support/*.h \ src/support/allocators/*.h \ src/test/*.cpp \ src/test/*.h \ src/wallet/*.cpp \ src/wallet/*.h \ src/wallet/test/*.cpp \ src/wallet/test/*.h \ src/zmq/*.cpp \ src/zmq/*.h do base=${f%/*}/ relbase=${base#src/} sed -i "s:#include \"\(.*\)\"\(.*\):if test -e \$base'\\1'; then echo \"#include <\"\$relbase\"\\1>\\2\"; else echo \"#include <\\1>\\2\"; fi:e" $f done -END VERIFY SCRIPT- Signed-off-by: Pasta <pasta@dashboost.org> * scripted-diff: Replace #include "" with #include <> (Dash Specific) -BEGIN VERIFY SCRIPT- for f in \ src/bls/*.cpp \ src/bls/*.h \ src/evo/*.cpp \ src/evo/*.h \ src/governance/*.cpp \ src/governance/*.h \ src/llmq/*.cpp \ src/llmq/*.h \ src/masternode/*.cpp \ src/masternode/*.h \ src/privatesend/*.cpp \ src/privatesend/*.h do base=${f%/*}/ relbase=${base#src/} sed -i "s:#include \"\(.*\)\"\(.*\):if test -e \$base'\\1'; then echo \"#include <\"\$relbase\"\\1>\\2\"; else echo \"#include <\\1>\\2\"; fi:e" $f done -END VERIFY SCRIPT- Signed-off-by: Pasta <pasta@dashboost.org> * build: Remove -I for everything but project root Remove -I from build system for everything but the project root, and built-in dependencies. Signed-off-by: Pasta <pasta@dashboost.org> # Conflicts: # src/Makefile.test.include * qt: refactor: Use absolute include paths in .ui files * qt: refactor: Changes to make include paths absolute This makes all include paths in the GUI absolute. Many changes are involved as every single source file in src/qt/ assumes to be able to use relative includes. Signed-off-by: Pasta <pasta@dashboost.org> # Conflicts: # src/qt/dash.cpp # src/qt/optionsmodel.cpp # src/qt/test/rpcnestedtests.cpp * test: refactor: Use absolute include paths for test data files * Recommend #include<> syntax in developer notes * refactor: Include obj/build.h instead of build.h * END BACKPORT #11651 Remove trailing whitespace causing travis failure * fix backport 11651 Signed-off-by: Pasta <pasta@dashboost.org> * More of 11651 * fix blockchain.cpp Signed-off-by: pasta <pasta@dashboost.org> * Add missing "qt/" in includes * Add missing "test/" in includes * Fix trailing whitespaces Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com> Co-authored-by: Russell Yanofsky <russ@yanofsky.org> Co-authored-by: MeshCollider <dobsonsa68@gmail.com> Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
2020-03-19 23:46:56 +01:00
#include <amount.h>
#include <script/script.h>
#include <serialize.h>
#include <uint256.h>
2016-05-16 20:23:01 +02:00
#include <tuple>
2016-05-16 20:23:01 +02:00
struct CSpentIndexKey {
public:
uint256 m_tx_hash;
uint32_t m_tx_index{0};
2016-05-16 20:23:01 +02:00
public:
2016-05-16 20:23:01 +02:00
CSpentIndexKey() {
SetNull();
}
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() {
m_tx_hash.SetNull();
m_tx_index = 0;
2016-05-16 20:23:01 +02:00
}
public:
SERIALIZE_METHODS(CSpentIndexKey, obj)
{
READWRITE(obj.m_tx_hash, obj.m_tx_index);
}
2016-05-16 20:23:01 +02:00
};
struct CSpentIndexValue {
public:
uint256 m_tx_hash;
uint32_t m_tx_index{0};
int32_t m_block_height{0};
CAmount m_amount{0};
uint8_t m_address_type{AddressType::UNKNOWN};
uint160 m_address_bytes;
2016-05-16 20:23:01 +02:00
public:
2016-05-16 20:23:01 +02:00
CSpentIndexValue() {
SetNull();
}
CSpentIndexValue(uint256 txin_hash, uint32_t txin_index, int32_t block_height, CAmount amount, uint8_t address_type,
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() {
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 {
return m_tx_hash.IsNull();
2016-05-16 20:23:01 +02:00
}
public:
SERIALIZE_METHODS(CSpentIndexValue, obj)
{
READWRITE(obj.m_tx_hash, obj.m_tx_index, obj.m_block_height, obj.m_amount, obj.m_address_type, obj.m_address_bytes);
}
2016-05-16 20:23:01 +02:00
};
struct CSpentIndexKeyCompare
{
bool operator()(const CSpentIndexKey& a, const CSpentIndexKey& b) const {
auto to_tuple = [](const CSpentIndexKey& obj) {
return std::tie(obj.m_tx_hash, obj.m_tx_index);
};
return to_tuple(a) < to_tuple(b);
2016-05-16 20:23:01 +02:00
}
};
struct CSpentIndexTxInfo
{
std::map<CSpentIndexKey, CSpentIndexValue, CSpentIndexKeyCompare> mSpentInfo;
};
struct CTimestampIndexIteratorKey {
public:
uint32_t m_time;
public:
CTimestampIndexIteratorKey() {
SetNull();
}
CTimestampIndexIteratorKey(uint32_t time) : m_time{time} {};
void SetNull() {
m_time = 0;
}
public:
size_t GetSerializeSize(int nType, int nVersion) const {
return 4;
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata32be(s, m_time);
}
template<typename Stream>
void Unserialize(Stream& s) {
m_time = ser_readdata32be(s);
}
};
struct CTimestampIndexKey {
public:
uint32_t m_block_time;
uint256 m_block_hash;
public:
CTimestampIndexKey() {
SetNull();
}
CTimestampIndexKey(uint32_t block_time, uint256 block_hash) :
m_block_time{block_time}, m_block_hash{block_hash} {};
void SetNull() {
m_block_time = 0;
m_block_hash.SetNull();
}
public:
size_t GetSerializeSize(int nType, int nVersion) const {
return 36;
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata32be(s, m_block_time);
m_block_hash.Serialize(s);
}
template<typename Stream>
void Unserialize(Stream& s) {
m_block_time = ser_readdata32be(s);
m_block_hash.Unserialize(s);
}
};
struct CAddressUnspentKey {
public:
uint8_t m_address_type{AddressType::UNKNOWN};
uint160 m_address_bytes;
uint256 m_tx_hash;
size_t m_tx_index{0};
public:
CAddressUnspentKey() {
SetNull();
}
CAddressUnspentKey(uint8_t address_type, uint160 address_bytes, uint256 tx_hash, size_t tx_index) :
m_address_type{address_type}, m_address_bytes{address_bytes}, m_tx_hash{tx_hash}, m_tx_index{tx_index} {};
void SetNull() {
m_address_type = AddressType::UNKNOWN;
m_address_bytes.SetNull();
m_tx_hash.SetNull();
m_tx_index = 0;
}
public:
size_t GetSerializeSize(int nType, int nVersion) const {
return 57;
}
template<typename Stream>
void Serialize(Stream& s) const {
ser_writedata8(s, m_address_type);
m_address_bytes.Serialize(s);
m_tx_hash.Serialize(s);
ser_writedata32(s, m_tx_index);
}
template<typename Stream>
void Unserialize(Stream& s) {
m_address_type = ser_readdata8(s);
m_address_bytes.Unserialize(s);
m_tx_hash.Unserialize(s);
m_tx_index = ser_readdata32(s);
}
};
struct CAddressUnspentValue {
public:
CAmount m_amount{-1};
CScript m_tx_script;
int32_t m_block_height;
public:
CAddressUnspentValue() {
SetNull();
}
CAddressUnspentValue(CAmount amount, CScript tx_script, int32_t block_height) :
m_amount{amount}, m_tx_script{tx_script}, m_block_height{block_height} {};
void SetNull() {
m_amount = -1;
m_tx_script.clear();
m_block_height = 0;
}
bool IsNull() const {
return (m_amount == -1);
}
public:
SERIALIZE_METHODS(CAddressUnspentValue, obj)
{
READWRITE(obj.m_amount, obj.m_tx_script, obj.m_block_height);
}
};
struct CAddressIndexKey {
public:
uint8_t m_address_type{AddressType::UNKNOWN};
uint160 m_address_bytes;
int32_t m_block_height{0};
uint32_t m_block_tx_pos{0};
uint256 m_tx_hash;
size_t m_tx_index{0};
bool m_tx_spent{false};
public:
CAddressIndexKey() {
SetNull();
}
CAddressIndexKey(uint8_t address_type, uint160 address_bytes, int32_t block_height, uint32_t block_tx_pos, uint256 tx_hash,
size_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 {
ser_writedata8(s, m_address_type);
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);
char f = m_tx_spent;
ser_writedata8(s, f);
}
template<typename Stream>
void Unserialize(Stream& s) {
m_address_type = ser_readdata8(s);
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);
char f = ser_readdata8(s);
m_tx_spent = f;
}
};
struct CAddressIndexIteratorKey {
public:
uint8_t m_address_type{AddressType::UNKNOWN};
uint160 m_address_bytes;
public:
CAddressIndexIteratorKey() {
SetNull();
}
CAddressIndexIteratorKey(uint8_t address_type, uint160 address_bytes) :
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 {
ser_writedata8(s, m_address_type);
m_address_bytes.Serialize(s);
}
template<typename Stream>
void Unserialize(Stream& s) {
m_address_type = ser_readdata8(s);
m_address_bytes.Unserialize(s);
}
};
struct CAddressIndexIteratorHeightKey {
public:
uint8_t m_address_type{AddressType::UNKNOWN};
uint160 m_address_bytes;
int32_t m_block_height{0};
public:
CAddressIndexIteratorHeightKey() {
SetNull();
}
CAddressIndexIteratorHeightKey(uint8_t address_type, uint160 address_bytes, int32_t block_height) :
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 {
ser_writedata8(s, m_address_type);
m_address_bytes.Serialize(s);
ser_writedata32be(s, m_block_height);
}
template<typename Stream>
void Unserialize(Stream& s) {
m_address_type = ser_readdata8(s);
m_address_bytes.Unserialize(s);
m_block_height = ser_readdata32be(s);
}
};
2016-05-16 20:23:01 +02:00
#endif // BITCOIN_SPENTINDEX_H