forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovidertx.cpp
More file actions
263 lines (234 loc) · 11.6 KB
/
providertx.cpp
File metadata and controls
263 lines (234 loc) · 11.6 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
// Copyright (c) 2018-2025 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 <evo/providertx.h>
#include <evo/dmn_types.h>
#include <chainparams.h>
#include <consensus/validation.h>
#include <deploymentstatus.h>
#include <hash.h>
#include <script/standard.h>
#include <tinyformat.h>
#include <util/underlying.h>
#include <validation.h>
namespace ProTxVersion {
template <typename T>
[[nodiscard]] uint16_t GetMaxFromDeployment(gsl::not_null<const CBlockIndex*> pindexPrev,
const ChainstateManager& chainman, std::optional<bool> is_basic_override)
{
constexpr bool is_extaddr_eligible{std::is_same_v<std::decay_t<T>, CProRegTx> || std::is_same_v<std::decay_t<T>, CProUpServTx>};
return ProTxVersion::GetMax(
is_basic_override ? *is_basic_override
: DeploymentActiveAfter(pindexPrev, chainman.GetConsensus(), Consensus::DEPLOYMENT_V19),
is_extaddr_eligible ? DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_V24) : false);
}
template uint16_t GetMaxFromDeployment<CProRegTx>(gsl::not_null<const CBlockIndex*> pindexPrev,
const ChainstateManager& chainman,
std::optional<bool> is_basic_override);
template uint16_t GetMaxFromDeployment<CProUpServTx>(gsl::not_null<const CBlockIndex*> pindexPrev,
const ChainstateManager& chainman,
std::optional<bool> is_basic_override);
template uint16_t GetMaxFromDeployment<CProUpRegTx>(gsl::not_null<const CBlockIndex*> pindexPrev,
const ChainstateManager& chainman,
std::optional<bool> is_basic_override);
template uint16_t GetMaxFromDeployment<CProUpRevTx>(gsl::not_null<const CBlockIndex*> pindexPrev,
const ChainstateManager& chainman,
std::optional<bool> is_basic_override);
} // namespace ProTxVersion
template <typename ProTx>
bool IsNetInfoTriviallyValid(const ProTx& proTx, TxValidationState& state)
{
if (!proTx.netInfo->HasEntries(NetInfoPurpose::CORE_P2P)) {
// Mandatory for all nodes
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-empty");
}
if (proTx.nType == MnType::Regular) {
// Regular nodes shouldn't populate Platform-specific fields
if (proTx.netInfo->HasEntries(NetInfoPurpose::PLATFORM_HTTPS) ||
proTx.netInfo->HasEntries(NetInfoPurpose::PLATFORM_P2P)) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-bad");
}
}
if (proTx.netInfo->CanStorePlatform() && proTx.nType == MnType::Evo) {
// Platform fields are mandatory for EvoNodes
if (!proTx.netInfo->HasEntries(NetInfoPurpose::PLATFORM_HTTPS) ||
!proTx.netInfo->HasEntries(NetInfoPurpose::PLATFORM_P2P)) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-empty");
}
}
return true;
}
bool CProRegTx::IsTriviallyValid(gsl::not_null<const CBlockIndex*> pindexPrev, const ChainstateManager& chainman,
TxValidationState& state) const
{
if (nVersion == 0 || nVersion > ProTxVersion::GetMaxFromDeployment<decltype(*this)>(pindexPrev, chainman)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version");
}
if (nVersion < ProTxVersion::BasicBLS && nType == MnType::Evo) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-evo-version");
}
if (!IsValidMnType(nType)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-type");
}
if (nMode != 0) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-mode");
}
if (keyIDOwner.IsNull() || !pubKeyOperator.Get().IsValid() || keyIDVoting.IsNull()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-key-null");
}
if (pubKeyOperator.IsLegacy() != (nVersion == ProTxVersion::LegacyBLS)) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-pubkey");
}
if (!scriptPayout.IsPayToPublicKeyHash() && !scriptPayout.IsPayToScriptHash()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-payee");
}
if (netInfo->CanStorePlatform() != (nVersion == ProTxVersion::ExtAddr)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-netinfo-version");
}
if (!netInfo->IsEmpty() && !IsNetInfoTriviallyValid(*this, state)) {
// pass the state returned by the function above
return false;
}
for (const auto& entry : netInfo->GetEntries()) {
if (!entry.IsTriviallyValid()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-bad");
}
}
CTxDestination payoutDest;
if (!ExtractDestination(scriptPayout, payoutDest)) {
// should not happen as we checked script types before
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-payee-dest");
}
// don't allow reuse of payout key for other keys (don't allow people to put the payee key onto an online server)
if (payoutDest == CTxDestination(PKHash(keyIDOwner)) || payoutDest == CTxDestination(PKHash(keyIDVoting))) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-payee-reuse");
}
if (nOperatorReward > 10000) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-reward");
}
return true;
}
std::string CProRegTx::MakeSignString() const
{
std::string s;
// We only include the important stuff in the string form...
CTxDestination destPayout;
std::string strPayout;
if (ExtractDestination(scriptPayout, destPayout)) {
strPayout = EncodeDestination(destPayout);
} else {
strPayout = HexStr(scriptPayout);
}
s += strPayout + "|";
s += strprintf("%d", nOperatorReward) + "|";
s += EncodeDestination(PKHash(keyIDOwner)) + "|";
s += EncodeDestination(PKHash(keyIDVoting)) + "|";
// ... and also the full hash of the payload as a protection against malleability and replays
s += ::SerializeHash(*this).ToString();
return s;
}
std::string CProRegTx::ToString() const
{
CTxDestination dest;
std::string payee = "unknown";
if (ExtractDestination(scriptPayout, dest)) {
payee = EncodeDestination(dest);
}
return strprintf("CProRegTx(nVersion=%d, nType=%d, collateralOutpoint=%s, netInfo=%s, nOperatorReward=%f, "
"ownerAddress=%s, pubKeyOperator=%s, votingAddress=%s, scriptPayout=%s, platformNodeID=%s%s)\n",
nVersion, ToUnderlying(nType), collateralOutpoint.ToStringShort(), netInfo->ToString(),
(double)nOperatorReward / 100, EncodeDestination(PKHash(keyIDOwner)), pubKeyOperator.ToString(),
EncodeDestination(PKHash(keyIDVoting)), payee, platformNodeID.ToString(),
(nVersion >= ProTxVersion::ExtAddr
? ""
: strprintf(", platformP2PPort=%d, platformHTTPPort=%d", platformP2PPort, platformHTTPPort)));
}
bool CProUpServTx::IsTriviallyValid(gsl::not_null<const CBlockIndex*> pindexPrev, const ChainstateManager& chainman,
TxValidationState& state) const
{
if (nVersion == 0 || nVersion > ProTxVersion::GetMaxFromDeployment<decltype(*this)>(pindexPrev, chainman)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version");
}
if (nVersion < ProTxVersion::BasicBLS && nType == MnType::Evo) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-evo-version");
}
if (netInfo->CanStorePlatform() != (nVersion == ProTxVersion::ExtAddr)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-netinfo-version");
}
if (netInfo->IsEmpty()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-empty");
}
if (!IsNetInfoTriviallyValid(*this, state)) {
// pass the state returned by the function above
return false;
}
for (const auto& entry : netInfo->GetEntries()) {
if (!entry.IsTriviallyValid()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-netinfo-bad");
}
}
return true;
}
std::string CProUpServTx::ToString() const
{
CTxDestination dest;
std::string payee = "unknown";
if (ExtractDestination(scriptOperatorPayout, dest)) {
payee = EncodeDestination(dest);
}
return strprintf("CProUpServTx(nVersion=%d, nType=%d, proTxHash=%s, netInfo=%s, operatorPayoutAddress=%s, "
"platformNodeID=%s%s)\n",
nVersion, ToUnderlying(nType), proTxHash.ToString(), netInfo->ToString(), payee,
platformNodeID.ToString(),
(nVersion >= ProTxVersion::ExtAddr
? ""
: strprintf(", platformP2PPort=%d, platformHTTPPort=%d", platformP2PPort, platformHTTPPort)));
}
bool CProUpRegTx::IsTriviallyValid(gsl::not_null<const CBlockIndex*> pindexPrev, const ChainstateManager& chainman,
TxValidationState& state) const
{
if (nVersion == 0 || nVersion > ProTxVersion::GetMaxFromDeployment<decltype(*this)>(pindexPrev, chainman)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version");
}
if (nMode != 0) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-mode");
}
if (!pubKeyOperator.Get().IsValid() || keyIDVoting.IsNull()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-key-null");
}
if (pubKeyOperator.IsLegacy() != (nVersion == ProTxVersion::LegacyBLS)) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-pubkey");
}
if (!scriptPayout.IsPayToPublicKeyHash() && !scriptPayout.IsPayToScriptHash()) {
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-payee");
}
return true;
}
std::string CProUpRegTx::ToString() const
{
CTxDestination dest;
std::string payee = "unknown";
if (ExtractDestination(scriptPayout, dest)) {
payee = EncodeDestination(dest);
}
return strprintf("CProUpRegTx(nVersion=%d, proTxHash=%s, pubKeyOperator=%s, votingAddress=%s, payoutAddress=%s)",
nVersion, proTxHash.ToString(), pubKeyOperator.ToString(), EncodeDestination(PKHash(keyIDVoting)), payee);
}
bool CProUpRevTx::IsTriviallyValid(gsl::not_null<const CBlockIndex*> pindexPrev, const ChainstateManager& chainman,
TxValidationState& state) const
{
if (nVersion == 0 || nVersion > ProTxVersion::GetMaxFromDeployment<decltype(*this)>(pindexPrev, chainman)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version");
}
// nReason < CProUpRevTx::REASON_NOT_SPECIFIED is always `false` since
// nReason is unsigned and CProUpRevTx::REASON_NOT_SPECIFIED == 0
if (nReason > CProUpRevTx::REASON_LAST) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-reason");
}
return true;
}
std::string CProUpRevTx::ToString() const
{
return strprintf("CProUpRevTx(nVersion=%d, proTxHash=%s, nReason=%d)",
nVersion, proTxHash.ToString(), nReason);
}