Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
common.cpp
Go to the documentation of this file.
1// Copyright (c) The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4
5#include <test/util/common.h>
6
7#include <addresstype.h>
8#include <addrman.h>
9#include <core_io.h>
10#include <crypto/hex_base.h>
11#include <key.h>
12#include <netaddress.h>
13#include <netbase.h>
14#include <protocol.h>
15#include <pubkey.h>
16#include <script/keyorigin.h>
17#include <script/script.h>
18#include <streams.h>
19#include <tinyformat.h>
20#include <util/bip32.h>
21#include <util/string.h>
22
23#include <array>
24#include <ostream>
25#include <variant>
26
27std::ostream& operator<<(std::ostream& os, const AddressPosition& pos)
28{
29 return os << strprintf("AddressPosition(tried=%d, multiplicity=%d, bucket=%d, position=%d)", pos.tried, pos.multiplicity, pos.bucket, pos.position);
30}
31
32std::ostream& operator<<(std::ostream& os, const CScript& script)
33{
34 return os << strprintf("CScript(asm=%s, hex=%s)", ScriptToAsmStr(script), HexStr(script));
35}
36
37std::ostream& operator<<(std::ostream& os, const CService& service)
38{
39 return os << strprintf("CService(addr=%s, net=%s)", service.ToStringAddrPort(), GetNetworkName(service.GetNetwork()));
40}
41
42std::ostream& operator<<(std::ostream& os, const CAddress& addr)
43{
44 return os << strprintf("CAddress(addr=%s, nTime=%s, nServices=[%s])",
45 addr.ToStringAddrPort(),
46 addr.nTime,
48}
49
50std::ostream& operator<<(std::ostream& os, const CExtKey& k)
51{
52 if (!k.key.IsValid()) return os << "CExtKey(<invalid>)";
53 std::array<std::byte, BIP32_EXTKEY_SIZE> code;
54 SpanWriter{code} << k;
55 return os << strprintf("CExtKey(encoded=%s)", HexStr(code));
56}
57
58std::ostream& operator<<(std::ostream& os, const CExtPubKey& k)
59{
60 if (k.pubkey.size() != CPubKey::COMPRESSED_SIZE) return os << "CExtPubKey(<invalid>)";
61 std::array<std::byte, BIP32_EXTKEY_SIZE> code;
62 SpanWriter{code} << k;
63 return os << strprintf("CExtPubKey(encoded=%s)", HexStr(code));
64}
65
66std::ostream& operator<<(std::ostream& os, const FeeFrac& ff)
67{
68 return os << strprintf("FeeFrac(fee=%d, size=%d)", ff.fee, ff.size);
69}
70
71std::ostream& operator<<(std::ostream& os, const ByRatioNegSize<FeeFrac>& b)
72{
73 return os << "ByRatioNegSize(" << static_cast<const FeeFrac&>(b) << ")";
74}
75
76std::ostream& operator<<(std::ostream& os, const CNoDestination& dest)
77{
78 return os << strprintf("CNoDestination(script=%s)", dest.GetScript());
79}
80
81std::ostream& operator<<(std::ostream& os, const PubKeyDestination& dest)
82{
83 return os << strprintf("PubKeyDestination(pubkey=%s)", dest.GetPubKey());
84}
85
86std::ostream& operator<<(std::ostream& os, const PKHash& h)
87{
88 return os << strprintf("PKHash(hash=%s)", h.ToString());
89}
90
91std::ostream& operator<<(std::ostream& os, const ScriptHash& h)
92{
93 return os << strprintf("ScriptHash(hash=%s)", h.ToString());
94}
95
96std::ostream& operator<<(std::ostream& os, const WitnessV0KeyHash& h)
97{
98 return os << strprintf("WitnessV0KeyHash(hash=%s)", h.ToString());
99}
100
101std::ostream& operator<<(std::ostream& os, const WitnessV0ScriptHash& h)
102{
103 return os << strprintf("WitnessV0ScriptHash(hash=%s)", h.ToString());
104}
105
106std::ostream& operator<<(std::ostream& os, const WitnessUnknown& w)
107{
108 return os << strprintf("WitnessUnknown(version=%u, program=%s)", w.GetWitnessVersion(), HexStr(w.GetWitnessProgram()));
109}
110
111std::ostream& operator<<(std::ostream& os, const PayToAnchor& p)
112{
113 return os << strprintf("PayToAnchor(version=%u, program=%s)", p.GetWitnessVersion(), HexStr(p.GetWitnessProgram()));
114}
115
116std::ostream& operator<<(std::ostream& os, const WitnessV1Taproot& t)
117{
118 return os << strprintf("WitnessV1Taproot(program=%s)", HexStr(t));
119}
120
121std::ostream& operator<<(std::ostream& os, const CTxDestination& dest)
122{
123 std::visit([&os](const auto& alt) { os << alt; }, dest);
124 return os;
125}
126
127std::ostream& operator<<(std::ostream& os, const CPubKey& pk)
128{
129 return os << strprintf("CPubKey(pubkey=%s)", HexStr(pk));
130}
131
132std::ostream& operator<<(std::ostream& os, const XOnlyPubKey& pk)
133{
134 return os << strprintf("XOnlyPubKey(pubkey=%s)", HexStr(pk));
135}
136
137std::ostream& operator<<(std::ostream& os, const KeyOriginInfo& info)
138{
139 return os << strprintf("KeyOriginInfo(fingerprint=%s, path=%s)", HexStr(info.fingerprint), FormatHDKeypath(info.path));
140}
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
std::string FormatHDKeypath(const std::vector< uint32_t > &path, bool apostrophe)
Definition bip32.cpp:62
std::string ToString() const
Definition hash_type.h:43
Wrapper around FeeFrac & derived types, which adds a total ordering which first sorts by feerate and ...
Definition feefrac.h:289
A CService with information about it as peer.
Definition protocol.h:387
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
Definition protocol.h:479
NodeSeconds nTime
Always included in serialization. The behavior is unspecified if the value is not representable as ui...
Definition protocol.h:477
const CScript & GetScript() const LIFETIMEBOUND
Definition addresstype.h:28
An encapsulated public key.
Definition pubkey.h:43
static constexpr unsigned int COMPRESSED_SIZE
Definition pubkey.h:49
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:531
std::string ToStringAddrPort() const
Minimal stream for writing to an existing span of bytes.
Definition streams.h:130
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode)
Create the assembly string representation of a CScript object.
Definition core_io.cpp:359
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition hex_base.cpp:30
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition string.h:209
std::string GetNetworkName(enum Network net)
Definition netbase.cpp:114
std::vector< std::string > serviceFlagsToStr(uint64_t flags)
Convert service flags (a bitmask of NODE_*) to human readable strings.
Definition protocol.cpp:108
Location information for an address in AddrMan.
Definition addrman.h:59
const bool tried
Definition addrman.h:61
const int bucket
Definition addrman.h:72
const int position
Definition addrman.h:73
const int multiplicity
Definition addrman.h:66
Definition key.h:238
Data structure storing a fee and size.
Definition feefrac.h:21
KeyFingerprint fingerprint
First 32 bits of the Hash160 of the public key at the root of the path.
Definition keyorigin.h:14
std::vector< uint32_t > path
Definition keyorigin.h:15
const CPubKey & GetPubKey() const LIFETIMEBOUND
Definition addresstype.h:41
CTxDestination subtype to encode any future Witness version.
Definition addresstype.h:95
std::ostream & operator<<(std::ostream &os, const AddressPosition &pos)
Definition common.cpp:27
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82