-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathreconstruct_won_block_test.cpp
More file actions
405 lines (356 loc) · 14.9 KB
/
Copy pathreconstruct_won_block_test.cpp
File metadata and controls
405 lines (356 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// ---------------------------------------------------------------------------
// dgb_reconstruct_won_block_test -- pins coin/reconstruct_won_block.hpp, the
// won-block reconstructor's (#82) BODY: the single composition the dispatch
// handler injects as its WonBlockReconstructor. Proves the three landed
// sub-slices compose into a faithful p2pool Share.as_block, end to end, against
// a SYNTHETIC ancestry + known-tx set (no live ShareTracker / mempool):
// * refs --resolve--> ordered hashes --assemble--> txs --frame--> {bytes,hex}
// * the composed bytes are IDENTICAL to assemble_won_block fed the manually
// resolved other_txs (no step reorders / drops / duplicates), hex==HexStr;
// * block tx layout is [gentx] ++ other_txs in transaction_hash_refs order;
// * empty refs => block carries the gentx only;
// * a malformed ref (walk past chain end) and an unknown other_tx hash each
// PROPAGATE as std::out_of_range -- the reconstructor never emits a partial
// or wrong block.
//
// Links the dgb OBJECT lib like dgb_block_assembly_test (block_assembly.hpp pulls
// BlockType / MutableTransaction / check_merkle_link). MUST also appear in the
// build.yml --target allowlist (#143 NOT_BUILT trap).
// ---------------------------------------------------------------------------
#include <gtest/gtest.h>
#include <functional>
#include <map>
#include <span>
#include <stdexcept>
#include <vector>
#include <core/pack.hpp>
#include <core/uint256.hpp>
#include <util/strencodings.h>
#include "../coin/reconstruct_won_block.hpp"
#include "../coin/won_share_inputs.hpp"
using dgb::coin::reconstruct_won_block;
using dgb::coin::reconstruct_won_block_from_template;
using dgb::coin::assemble_won_block;
using dgb::coin::resolve_other_tx_hashes;
using dgb::coin::assemble_other_txs;
using dgb::coin::ReconstructedWonBlock;
using dgb::coin::BlockType;
using dgb::coin::SmallBlockHeaderType;
using dgb::coin::MutableTransaction;
using dgb::TxHashRefs;
using dgb::coin::won_share_inputs;
using dgb::coin::WonShareInputs;
namespace {
uint256 H(const char* two) // 0x"two" repeated to 64 hex chars
{
std::string s;
for (int i = 0; i < 32; ++i) s += two;
uint256 h; h.SetHex(s);
return h;
}
MutableTransaction make_tx(int64_t value)
{
MutableTransaction tx;
tx.version = 1;
tx.locktime = 0;
dgb::coin::TxIn in;
in.prevout.hash.SetNull();
in.prevout.index = 0;
in.sequence = 0xffffffff;
tx.vin.push_back(in);
dgb::coin::TxOut out;
out.value = value;
tx.vout.push_back(out);
return tx;
}
MutableTransaction make_gentx()
{
MutableTransaction tx;
tx.version = 1;
tx.locktime = 0;
dgb::coin::TxIn in;
in.prevout.hash.SetNull();
in.prevout.index = 0xffffffff; // coinbase
in.sequence = 0xffffffff;
tx.vin.push_back(in);
dgb::coin::TxOut out;
out.value = 5000000000LL;
tx.vout.push_back(out);
return tx;
}
SmallBlockHeaderType make_small_header()
{
SmallBlockHeaderType h;
h.m_version = 0x20000000;
h.m_previous_block.SetHex("00000000000000000000000000000000000000000000000000000000deadbeef");
h.m_timestamp = 1718700000;
h.m_bits = 0x1a0fffff;
h.m_nonce = 0x12345678;
return h;
}
uint256 fixed_gentx_hash()
{
uint256 h;
h.SetHex("1111111111111111111111111111111111111111111111111111111111111111");
return h;
}
// Synthetic ancestry won <- p1 <- p2, each share carrying new_transaction_hashes,
// plus a known_txs store mapping those hashes to distinct MutableTransactions.
struct Fixture
{
uint256 won = H("a0"), p1 = H("a1"), p2 = H("a2");
std::map<uint256, uint256> parent;
std::map<uint256, std::vector<uint256>> nths;
std::map<uint256, MutableTransaction> known;
Fixture()
{
parent[won] = p1;
parent[p1] = p2;
nths[won] = { H("c0"), H("c1") };
nths[p1] = { H("d0") };
nths[p2] = { H("e0"), H("e1") };
// distinct values so order is observable downstream
known[H("c1")] = make_tx(11);
known[H("d0")] = make_tx(22);
known[H("c0")] = make_tx(33);
}
std::function<uint256(const uint256&, uint64_t)> nth_parent() const
{
auto p = parent;
return [p](const uint256& start, uint64_t n) {
uint256 cur = start;
for (uint64_t i = 0; i < n; ++i) {
auto it = p.find(cur);
if (it == p.end()) return uint256();
cur = it->second;
}
return cur;
};
}
std::function<const std::vector<uint256>&(const uint256&)> new_tx() const
{
return [this](const uint256& h) -> const std::vector<uint256>& { return nths.at(h); };
}
std::function<const MutableTransaction*(const uint256&)> known_fn() const
{
return [this](const uint256& h) -> const MutableTransaction* {
auto it = known.find(h);
return it == known.end() ? nullptr : &it->second;
};
}
};
// --- Test 1: end-to-end composition == manual resolve+assemble+frame ----------
TEST(DgbReconstructWonBlock, ComposesToIdenticalBlock)
{
Fixture f;
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link; // empty branch => root == gentx_hash
// refs: (0,1) -> won.nths[1]=c1 ; (1,0) -> p1.nths[0]=d0
std::vector<TxHashRefs> refs = { TxHashRefs(0, 1), TxHashRefs(1, 0) };
auto got = reconstruct_won_block(sh, link, gentx, gh, f.won, refs,
f.nth_parent(), f.new_tx(), f.known_fn());
// Independently drive the three SSOT steps and frame directly.
auto hashes = resolve_other_tx_hashes(f.won, refs, f.nth_parent(), f.new_tx());
auto others = assemble_other_txs(hashes, f.known_fn());
auto direct = assemble_won_block(sh, gentx, gh, link, others);
EXPECT_EQ(got.bytes, direct.first);
EXPECT_EQ(got.hex, direct.second);
auto sp = std::span<const std::byte>(
reinterpret_cast<const std::byte*>(got.bytes.data()), got.bytes.size());
EXPECT_EQ(got.hex, HexStr(sp));
}
// --- Test 2: tx layout is [gentx] ++ other_txs in ref order -------------------
TEST(DgbReconstructWonBlock, CoinbaseFirstThenRefOrder)
{
Fixture f;
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link;
std::vector<TxHashRefs> refs = { TxHashRefs(0, 1), TxHashRefs(1, 0) };
auto got = reconstruct_won_block(sh, link, gentx, gh, f.won, refs,
f.nth_parent(), f.new_tx(), f.known_fn());
PackStream ps(got.bytes);
BlockType blk;
ps >> blk;
ASSERT_EQ(blk.m_txs.size(), 3u); // gentx + 2 others
// Header merkle_root is recomputed over the ACTUAL block tx vector
// ([gentx_hash] ++ each other-tx txid), per the #303 bad-txnmrklroot fix --
// NOT the empty-merkle_link walk (which would yield gentx_hash alone). Pin
// header/body consistency via the same build_block_merkle_root SSOT.
{
std::vector<uint256> expect_txids = {
gh,
dgb::coin::compute_txid(blk.m_txs[1]),
dgb::coin::compute_txid(blk.m_txs[2]),
};
EXPECT_EQ(blk.m_merkle_root,
dgb::coin::build_block_merkle_root(expect_txids));
}
EXPECT_EQ(blk.m_txs[0].vin[0].prevout.index, 0xffffffffu); // coinbase at 0
EXPECT_TRUE(blk.m_txs[0].vin[0].prevout.hash.IsNull());
EXPECT_EQ(blk.m_txs[1].vout[0].value, 11); // c1 -> make_tx(11), ref order
EXPECT_EQ(blk.m_txs[2].vout[0].value, 22); // d0 -> make_tx(22)
}
// --- Test 3: empty refs => block carries only the gentx -----------------------
TEST(DgbReconstructWonBlock, EmptyRefsGentxOnly)
{
Fixture f;
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link;
std::vector<TxHashRefs> refs; // none
auto got = reconstruct_won_block(sh, link, gentx, gh, f.won, refs,
f.nth_parent(), f.new_tx(), f.known_fn());
PackStream ps(got.bytes);
BlockType blk;
ps >> blk;
ASSERT_EQ(blk.m_txs.size(), 1u);
EXPECT_EQ(blk.m_txs[0].vin[0].prevout.index, 0xffffffffu);
}
// --- Test 4: malformed ref (walk past chain end) propagates as throw ----------
TEST(DgbReconstructWonBlock, RefPastChainThrows)
{
Fixture f;
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link;
// share_count 9 walks far past p2 (the chain end) -> null ancestor.
std::vector<TxHashRefs> refs = { TxHashRefs(9, 0) };
EXPECT_THROW(
reconstruct_won_block(sh, link, gentx, gh, f.won, refs,
f.nth_parent(), f.new_tx(), f.known_fn()),
std::out_of_range);
}
// --- Test 5: unknown other_tx hash propagates as throw ------------------------
TEST(DgbReconstructWonBlock, UnknownKnownTxThrows)
{
Fixture f;
f.known.erase(H("c1")); // resolves but is absent from known_txs
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link;
std::vector<TxHashRefs> refs = { TxHashRefs(0, 1) }; // -> c1, now missing
EXPECT_THROW(
reconstruct_won_block(sh, link, gentx, gh, f.won, refs,
f.nth_parent(), f.new_tx(), f.known_fn()),
std::out_of_range);
}
// --- Test 5: template path == ref path for the SAME tx set (equivalence) ------
// Proves reconstruct_won_block_from_template (the CORRECT block-broadcast
// source: captured GBT template txs) frames a byte-identical block to the
// ref-walk path when fed the same non-coinbase tx set -- i.e. the re-scope is a
// pure SOURCE swap, the framing/merkle/codec are unchanged.
TEST(DgbReconstructWonBlock, TemplatePathMatchesRefPathForSameTxSet)
{
Fixture f;
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link;
std::vector<TxHashRefs> refs = { TxHashRefs(0, 1), TxHashRefs(1, 0) };
// Ref-walk path: resolve hashes -> assemble the other_txs the share refs to.
auto hashes = resolve_other_tx_hashes(f.won, refs, f.nth_parent(), f.new_tx());
auto template_txs = assemble_other_txs(hashes, f.known_fn());
auto ref_got = reconstruct_won_block(sh, link, gentx, gh, f.won, refs,
f.nth_parent(), f.new_tx(), f.known_fn());
auto tmpl_got = reconstruct_won_block_from_template(sh, link, gentx, gh,
template_txs);
EXPECT_EQ(tmpl_got.bytes, ref_got.bytes);
EXPECT_EQ(tmpl_got.hex, ref_got.hex);
PackStream ps(tmpl_got.bytes);
BlockType blk;
ps >> blk;
ASSERT_EQ(blk.m_txs.size(), 3u); // gentx + 2 template txs
EXPECT_EQ(blk.m_txs[0].vin[0].prevout.index, 0xffffffffu);
EXPECT_EQ(blk.m_txs[1].vout[0].value, 11); // template order preserved
EXPECT_EQ(blk.m_txs[2].vout[0].value, 22);
}
// --- Test 6: empty captured template => valid coinbase-only block -------------
// Today's embedded path emits transactions[]==[]; reconstruct must yield a
// valid gentx-only block (correct-and-empty), never throw / fail closed.
TEST(DgbReconstructWonBlock, EmptyTemplateGentxOnly)
{
auto sh = make_small_header();
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
::dgb::MerkleLink link;
std::vector<MutableTransaction> template_txs; // captured template was empty
auto got = reconstruct_won_block_from_template(sh, link, gentx, gh, template_txs);
PackStream ps(got.bytes);
BlockType blk;
ps >> blk;
ASSERT_EQ(blk.m_txs.size(), 1u);
EXPECT_EQ(blk.m_txs[0].vin[0].prevout.index, 0xffffffffu);
EXPECT_EQ(blk.m_merkle_root, gh); // empty link => root == gentx_hash
auto sp = std::span<const std::byte>(
reinterpret_cast<const std::byte*>(got.bytes.data()), got.bytes.size());
EXPECT_EQ(got.hex, HexStr(sp));
}
// --- Test 7: won_share_inputs extracts the two share-carried inputs verbatim --
// The share-side half of the faithful reconstruct closure: a won share carries
// small_header (m_min_header) + merkle_link (m_merkle_link) verbatim. The seam
// is a pure read -- no field is transformed, reordered, or dropped.
TEST(DgbReconstructWonBlock, WonShareInputsExtractsHeaderAndMerkleLink)
{
// Minimal duck-typed stand-in for dgb::Share (keeps this TU off share.hpp's
// tracker/base_uint TU, per won_share_inputs.hpp's #143 note). Exposes the
// two members won_share_inputs reads by name.
struct FakeShare {
SmallBlockHeaderType m_min_header;
::dgb::MerkleLink m_merkle_link;
};
FakeShare s;
s.m_min_header = make_small_header();
s.m_merkle_link.m_branch = { H("b0"), H("b1") };
s.m_merkle_link.m_index = 0;
WonShareInputs got = won_share_inputs(s);
EXPECT_EQ(got.small_header.m_version, s.m_min_header.m_version);
EXPECT_EQ(got.small_header.m_previous_block, s.m_min_header.m_previous_block);
EXPECT_EQ(got.small_header.m_timestamp, s.m_min_header.m_timestamp);
EXPECT_EQ(got.small_header.m_bits, s.m_min_header.m_bits);
EXPECT_EQ(got.small_header.m_nonce, s.m_min_header.m_nonce);
ASSERT_EQ(got.merkle_link.m_branch.size(), 2u);
EXPECT_EQ(got.merkle_link.m_branch[0], H("b0"));
EXPECT_EQ(got.merkle_link.m_branch[1], H("b1"));
EXPECT_EQ(got.merkle_link.m_index, 0u);
}
// --- Test 8: won_share_inputs feeds reconstruct_won_block_from_template -------
// End-to-end seam check: the extracted {small_header, merkle_link} drive a block
// byte-identical to passing those same fields directly. Proves the share-side
// seam is a faithful pass-through into the won-block framing path -- the run-loop
// closure may source small_header/merkle_link via won_share_inputs(share) with
// no change to the reconstructed block.
TEST(DgbReconstructWonBlock, WonShareInputsDrivesTemplateReconstructIdentically)
{
struct FakeShare {
SmallBlockHeaderType m_min_header;
::dgb::MerkleLink m_merkle_link;
};
FakeShare s;
s.m_min_header = make_small_header();
// Non-empty link so the merkle-root math actually consumes it.
s.m_merkle_link.m_branch = { H("b0") };
s.m_merkle_link.m_index = 0;
auto gentx = make_gentx();
auto gh = fixed_gentx_hash();
std::vector<MutableTransaction> template_txs = { make_tx(11), make_tx(22) };
WonShareInputs in = won_share_inputs(s);
auto via_seam = reconstruct_won_block_from_template(
in.small_header, in.merkle_link, gentx, gh, template_txs);
auto direct = reconstruct_won_block_from_template(
s.m_min_header, s.m_merkle_link, gentx, gh, template_txs);
EXPECT_EQ(via_seam.bytes, direct.bytes);
EXPECT_EQ(via_seam.hex, direct.hex);
PackStream ps(via_seam.bytes);
BlockType blk;
ps >> blk;
ASSERT_EQ(blk.m_txs.size(), 3u); // gentx + 2 template txs
EXPECT_EQ(blk.m_txs[0].vin[0].prevout.index, 0xffffffffu);
}
} // namespace