mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
refactor: remove more code duplication from (dis)connect operations
This commit is contained in:
parent
b29ef2eb4a
commit
f47021a536
@ -386,6 +386,7 @@ libbitcoin_server_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CP
|
||||
libbitcoin_server_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
libbitcoin_server_a_SOURCES = \
|
||||
addrdb.cpp \
|
||||
addressindex.cpp \
|
||||
addrman.cpp \
|
||||
banman.cpp \
|
||||
batchedlogger.cpp \
|
||||
|
43
src/addressindex.cpp
Normal file
43
src/addressindex.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright (c) 2016 BitPay, Inc.
|
||||
// Copyright (c) 2023 The Dash Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <addressindex.h>
|
||||
|
||||
#include <key_io.h>
|
||||
#include <hash.h>
|
||||
#include <script/script.h>
|
||||
|
||||
template <typename T1>
|
||||
inline std::vector<uint8_t> TrimScriptP2PKH(const T1& input) {
|
||||
return std::vector<uint8_t>(input.begin() + 3, input.begin() + 23);
|
||||
};
|
||||
|
||||
template <typename T1>
|
||||
inline std::vector<uint8_t> TrimScriptP2SH(const T1& input) {
|
||||
return std::vector<uint8_t>(input.begin() + 2, input.begin() + 22);
|
||||
};
|
||||
|
||||
template <typename T1>
|
||||
inline std::vector<uint8_t> TrimScriptP2PK(const T1& input) {
|
||||
return std::vector<uint8_t>(input.begin() + 1, input.end() - 1);
|
||||
};
|
||||
|
||||
bool AddressBytesFromScript(const CScript& script, uint8_t& address_type, uint160& address_bytes) {
|
||||
if (script.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(script));
|
||||
} else if (script.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(script));
|
||||
} else if (script.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(script));
|
||||
} else {
|
||||
address_type = AddressType::UNKNOWN;
|
||||
address_bytes.SetNull();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
@ -14,6 +14,8 @@
|
||||
#include <chrono>
|
||||
#include <tuple>
|
||||
|
||||
class CScript;
|
||||
|
||||
namespace AddressType {
|
||||
enum AddressType {
|
||||
P2PK = 1,
|
||||
@ -212,19 +214,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T1>
|
||||
inline std::vector<uint8_t> TrimScriptP2PK(const T1& input) {
|
||||
return std::vector<uint8_t>(input.begin() + 1, input.end() - 1);
|
||||
};
|
||||
|
||||
template <typename T1>
|
||||
inline std::vector<uint8_t> TrimScriptP2PKH(const T1& input) {
|
||||
return std::vector<uint8_t>(input.begin() + 3, input.begin() + 23);
|
||||
};
|
||||
|
||||
template <typename T1>
|
||||
inline std::vector<uint8_t> TrimScriptP2SH(const T1& input) {
|
||||
return std::vector<uint8_t>(input.begin() + 2, input.begin() + 22);
|
||||
};
|
||||
bool AddressBytesFromScript(const CScript& script, uint8_t& address_type, uint160& address_bytes);
|
||||
|
||||
#endif // BITCOIN_ADDRESSINDEX_H
|
||||
|
@ -478,16 +478,7 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (prevout.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(prevout.scriptPubKey));
|
||||
} else {
|
||||
if (!AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -503,16 +494,7 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (out.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(out.scriptPubKey));
|
||||
} else {
|
||||
if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -570,18 +552,8 @@ void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCac
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (prevout.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(prevout.scriptPubKey));
|
||||
} else {
|
||||
address_type = AddressType::UNKNOWN;
|
||||
address_bytes.SetNull();
|
||||
if (!AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
CSpentIndexKey key = CSpentIndexKey(input.prevout.hash, input.prevout.n);
|
||||
|
@ -1709,16 +1709,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (out.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(out.scriptPubKey));
|
||||
} else {
|
||||
if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -1771,16 +1762,7 @@ DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockI
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (prevout.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(prevout.scriptPubKey));
|
||||
} else {
|
||||
if (!AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -2245,19 +2227,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (prevout.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(prevout.scriptPubKey));
|
||||
} else {
|
||||
address_type = AddressType::UNKNOWN;
|
||||
address_bytes.SetNull();
|
||||
}
|
||||
AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes);
|
||||
|
||||
if (fAddressIndex && address_type != AddressType::UNKNOWN) {
|
||||
// record spending activity
|
||||
@ -2311,16 +2281,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (out.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(out.scriptPubKey));
|
||||
} else {
|
||||
if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -4847,19 +4808,7 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (prevout.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(prevout.scriptPubKey));
|
||||
} else if (prevout.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(prevout.scriptPubKey));
|
||||
} else {
|
||||
address_type = AddressType::UNKNOWN;
|
||||
address_bytes.SetNull();
|
||||
}
|
||||
AddressBytesFromScript(prevout.scriptPubKey, address_type, address_bytes);
|
||||
|
||||
if (fAddressIndex && address_type != AddressType::UNKNOWN) {
|
||||
// record spending activity
|
||||
@ -4884,16 +4833,7 @@ bool CChainState::RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& i
|
||||
uint8_t address_type{0};
|
||||
uint160 address_bytes;
|
||||
|
||||
if (out.scriptPubKey.IsPayToScriptHash()) {
|
||||
address_type = AddressType::P2SH;
|
||||
address_bytes = uint160(TrimScriptP2SH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
|
||||
address_type = AddressType::P2PKH;
|
||||
address_bytes = uint160(TrimScriptP2PKH(out.scriptPubKey));
|
||||
} else if (out.scriptPubKey.IsPayToPublicKey()) {
|
||||
address_type = AddressType::P2PK;
|
||||
address_bytes = Hash160(TrimScriptP2PK(out.scriptPubKey));
|
||||
} else {
|
||||
if (!AddressBytesFromScript(out.scriptPubKey, address_type, address_bytes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user