Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
cluster_linearize_tests.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 http://www.opensource.org/licenses/mit-license.php.
4
5#include <cluster_linearize.h>
8#include <util/bitset.h>
9#include <util/strencodings.h>
10
11#include <vector>
12
13#include <boost/test/unit_test.hpp>
14
15BOOST_FIXTURE_TEST_SUITE(cluster_linearize_tests, BasicTestingSetup)
16
17using namespace cluster_linearize;
18
19namespace {
20
21template<typename SetType>
22void TestDepGraphSerialization(const Cluster<SetType>& cluster, const std::string& hexenc)
23{
24 DepGraph depgraph(cluster);
25
26 // Run normal sanity and correspondence checks, which includes a round-trip test.
27 VerifyDepGraphFromCluster(cluster, depgraph);
28
29 // There may be multiple serializations of the same graph, but DepGraphFormatter's serializer
30 // only produces one of those. Verify that hexenc matches that canonical serialization.
31 std::vector<unsigned char> encoding;
32 VectorWriter writer(encoding, 0);
33 writer << Using<DepGraphFormatter>(depgraph);
34 BOOST_CHECK_EQUAL(HexStr(encoding), hexenc);
35
36 // Test that deserializing that encoding yields depgraph. This is effectively already implied
37 // by the round-trip test above (if depgraph is acyclic), but verify it explicitly again here.
38 SpanReader reader(encoding);
39 DepGraph<SetType> depgraph_read;
40 reader >> Using<DepGraphFormatter>(depgraph_read);
41 BOOST_CHECK(depgraph == depgraph_read);
42}
43
44} // namespace
45
46BOOST_AUTO_TEST_CASE(depgraph_ser_tests)
47{
48 // Empty cluster.
49 TestDepGraphSerialization<TestBitSet>(
50 {},
51 "00" /* end of graph */);
52
53 // Transactions: A(fee=0,size=1).
54 TestDepGraphSerialization<TestBitSet>(
55 {{{0, 1}, {}}},
56 "01" /* A size */
57 "00" /* A fee */
58 "00" /* A insertion position (no skips): A */
59 "00" /* end of graph */);
60
61 // Transactions: A(fee=42,size=11), B(fee=-13,size=7), B depends on A.
62 TestDepGraphSerialization<TestBitSet>(
63 {{{42, 11}, {}}, {{-13, 7}, {0}}},
64 "0b" /* A size */
65 "54" /* A fee */
66 "00" /* A insertion position (no skips): A */
67 "07" /* B size */
68 "19" /* B fee */
69 "00" /* B->A dependency (no skips) */
70 "00" /* B insertion position (no skips): A,B */
71 "00" /* end of graph */);
72
73 // Transactions: A(64,128), B(128,256), C(1,1), C depends on A and B.
74 TestDepGraphSerialization<TestBitSet>(
75 {{{64, 128}, {}}, {{128, 256}, {}}, {{1, 1}, {0, 1}}},
76 "8000" /* A size */
77 "8000" /* A fee */
78 "00" /* A insertion position (no skips): A */
79 "8100" /* B size */
80 "8100" /* B fee */
81 "01" /* B insertion position (skip B->A dependency): A,B */
82 "01" /* C size */
83 "02" /* C fee */
84 "00" /* C->B dependency (no skips) */
85 "00" /* C->A dependency (no skips) */
86 "00" /* C insertion position (no skips): A,B,C */
87 "00" /* end of graph */);
88
89 // Transactions: A(-57,113), B(57,114), C(-58,115), D(58,116). Deps: B->A, C->A, D->C, in order
90 // [B,A,C,D]. This exercises non-topological ordering (internally serialized as A,B,C,D).
91 TestDepGraphSerialization<TestBitSet>(
92 {{{57, 114}, {1}}, {{-57, 113}, {}}, {{-58, 115}, {1}}, {{58, 116}, {2}}},
93 "71" /* A size */
94 "71" /* A fee */
95 "00" /* A insertion position (no skips): A */
96 "72" /* B size */
97 "72" /* B fee */
98 "00" /* B->A dependency (no skips) */
99 "01" /* B insertion position (skip A): B,A */
100 "73" /* C size */
101 "73" /* C fee */
102 "01" /* C->A dependency (skip C->B dependency) */
103 "00" /* C insertion position (no skips): B,A,C */
104 "74" /* D size */
105 "74" /* D fee */
106 "00" /* D->C dependency (no skips) */
107 "01" /* D insertion position (skip D->B dependency, D->A is implied): B,A,C,D */
108 "00" /* end of graph */);
109
110 // Transactions: A(1,2), B(3,1), C(2,1), D(1,3), E(1,1). Deps: C->A, D->A, D->B, E->D.
111 // In order: [D,A,B,E,C]. Internally serialized in order A,B,C,D,E.
112 TestDepGraphSerialization<TestBitSet>(
113 {{{1, 3}, {1, 2}}, {{1, 2}, {}}, {{3, 1}, {}}, {{1, 1}, {0}}, {{2, 1}, {1}}},
114 "02" /* A size */
115 "02" /* A fee */
116 "00" /* A insertion position (no skips): A */
117 "01" /* B size */
118 "06" /* B fee */
119 "01" /* B insertion position (skip B->A dependency): A,B */
120 "01" /* C size */
121 "04" /* C fee */
122 "01" /* C->A dependency (skip C->B dependency) */
123 "00" /* C insertion position (no skips): A,B,C */
124 "03" /* D size */
125 "02" /* D fee */
126 "01" /* D->B dependency (skip D->C dependency) */
127 "00" /* D->A dependency (no skips) */
128 "03" /* D insertion position (skip C,B,A): D,A,B,C */
129 "01" /* E size */
130 "02" /* E fee */
131 "00" /* E->D dependency (no skips) */
132 "02" /* E insertion position (skip E->C dependency, E->B and E->A are implied,
133 skip insertion C): D,A,B,E,C */
134 "00" /* end of graph */
135 );
136}
137
Minimal stream for reading from an existing byte array by Span.
Definition streams.h:101
Data structure that holds a transaction graph's preprocessed data (fee, size, ancestors,...
BOOST_AUTO_TEST_CASE(depgraph_ser_tests)
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition hex_base.cpp:29
std::vector< std::pair< FeeFrac, SetType > > Cluster
Data type to represent cluster input.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:17
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition serialize.h:495
Basic testing setup.