-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_dash_work_source.cpp
More file actions
131 lines (111 loc) · 3.93 KB
/
Copy pathtest_dash_work_source.cpp
File metadata and controls
131 lines (111 loc) · 3.93 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
// SPDX-License-Identifier: AGPL-3.0-or-later
///
/// KAT for dash::coin::select_dash_work — the embedded-vs-dashd work-source
/// selector (S8 embedded_gbt live-wire capstone). Proves the routing contract
/// and the RETAINED dashd fallback, without a live daemon or a populated
/// MN/mempool harness (the embedded builder is injected as a stub; its
/// oracle-parity output is already pinned by test_dash_embedded_gbt).
///
/// Contract under test:
/// 1. viable() bundle -> WorkSource::Embedded, embedded builder run,
/// fallback NEVER touched.
/// 2. has_state=false -> WorkSource::DashdFallback, fallback run.
/// 3. viable but null mnstates -> fallback (defensive null-guard).
/// 4. viable but null mempool -> fallback (defensive null-guard).
#include <impl/dash/coin/work_source.hpp>
#include <gtest/gtest.h>
using dash::coin::EmbeddedWorkInputs;
using dash::coin::WorkSource;
using dash::coin::WorkSelection;
using dash::coin::select_dash_work;
using dash::coin::DashWorkData;
using dash::coin::MnStateMachine;
using dash::coin::Mempool;
namespace {
// Distinguishable sentinels so we can prove WHICH closure produced the result.
constexpr uint32_t EMB_SENTINEL_HEIGHT = 0xE3BEDDEDu & 0xffffffu; // "embedded"
constexpr uint32_t DASHD_SENTINEL_HEIGHT = 999'999u;
DashWorkData embedded_stub(bool& ran) {
ran = true;
DashWorkData w;
w.m_height = EMB_SENTINEL_HEIGHT;
return w;
}
DashWorkData dashd_stub(bool& ran) {
ran = true;
DashWorkData w;
w.m_height = DASHD_SENTINEL_HEIGHT;
return w;
}
} // namespace
// 1) Viable bundle routes to the EMBEDDED builder; fallback is not invoked.
TEST(DashWorkSource, ViableRoutesEmbedded)
{
MnStateMachine mn;
Mempool mp;
EmbeddedWorkInputs emb;
emb.has_state = true;
emb.mnstates = &mn;
emb.mempool = ∓
ASSERT_TRUE(emb.viable());
bool emb_ran = false, fb_ran = false;
WorkSelection sel = select_dash_work(
emb,
[&] { return embedded_stub(emb_ran); },
[&] { return dashd_stub(fb_ran); });
EXPECT_EQ(sel.source, WorkSource::Embedded);
EXPECT_TRUE(emb_ran);
EXPECT_FALSE(fb_ran);
EXPECT_EQ(sel.work.m_height, EMB_SENTINEL_HEIGHT);
}
// 2) No embedded state -> the RETAINED dashd getblocktemplate fallback runs.
TEST(DashWorkSource, NoStateRoutesDashdFallback)
{
EmbeddedWorkInputs emb; // has_state defaults false
ASSERT_FALSE(emb.viable());
bool emb_ran = false, fb_ran = false;
WorkSelection sel = select_dash_work(
emb,
[&] { return embedded_stub(emb_ran); },
[&] { return dashd_stub(fb_ran); });
EXPECT_EQ(sel.source, WorkSource::DashdFallback);
EXPECT_FALSE(emb_ran);
EXPECT_TRUE(fb_ran);
EXPECT_EQ(sel.work.m_height, DASHD_SENTINEL_HEIGHT);
}
// 3) has_state true but mnstates null -> not viable -> fallback (null-guard).
TEST(DashWorkSource, NullMnStatesRoutesFallback)
{
Mempool mp;
EmbeddedWorkInputs emb;
emb.has_state = true;
emb.mnstates = nullptr;
emb.mempool = ∓
EXPECT_FALSE(emb.viable());
bool emb_ran = false, fb_ran = false;
WorkSelection sel = select_dash_work(
emb,
[&] { return embedded_stub(emb_ran); },
[&] { return dashd_stub(fb_ran); });
EXPECT_EQ(sel.source, WorkSource::DashdFallback);
EXPECT_FALSE(emb_ran);
EXPECT_TRUE(fb_ran);
}
// 4) has_state true but mempool null -> not viable -> fallback (null-guard).
TEST(DashWorkSource, NullMempoolRoutesFallback)
{
MnStateMachine mn;
EmbeddedWorkInputs emb;
emb.has_state = true;
emb.mnstates = &mn;
emb.mempool = nullptr;
EXPECT_FALSE(emb.viable());
bool emb_ran = false, fb_ran = false;
WorkSelection sel = select_dash_work(
emb,
[&] { return embedded_stub(emb_ran); },
[&] { return dashd_stub(fb_ran); });
EXPECT_EQ(sel.source, WorkSource::DashdFallback);
EXPECT_FALSE(emb_ran);
EXPECT_TRUE(fb_ran);
}