Skip to content

Commit 714fb3a

Browse files
kwvgknst
andcommitted
refactor: consolidate ProTx versioning, make logic friendly to upgrades
Co-authored-by: Konstantin Akimov <[email protected]>
1 parent 2f9d65d commit 714fb3a

File tree

6 files changed

+42
-45
lines changed

6 files changed

+42
-45
lines changed

src/evo/dmnstate.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CDeterministicMNState
3333
friend class CDeterministicMNStateDiff;
3434

3535
public:
36-
int nVersion{CProRegTx::LEGACY_BLS_VERSION};
36+
int nVersion{ProTxVersion::LegacyBLS};
3737

3838
int nRegisteredHeight{-1};
3939
int nLastPaidHeight{0};
@@ -94,7 +94,7 @@ class CDeterministicMNState
9494
obj.confirmedHash,
9595
obj.confirmedHashWithProRegTxHash,
9696
obj.keyIDOwner);
97-
READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), obj.nVersion == CProRegTx::LEGACY_BLS_VERSION));
97+
READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), obj.nVersion == ProTxVersion::LegacyBLS));
9898
READWRITE(
9999
obj.keyIDVoting,
100100
obj.addr,
@@ -107,7 +107,7 @@ class CDeterministicMNState
107107

108108
void ResetOperatorFields()
109109
{
110-
nVersion = CProRegTx::LEGACY_BLS_VERSION;
110+
nVersion = ProTxVersion::LegacyBLS;
111111
pubKeyOperator = CBLSLazyPublicKey();
112112
addr = CService();
113113
scriptOperatorPayout = CScript();
@@ -219,14 +219,14 @@ class CDeterministicMNStateDiff
219219
#define DMN_STATE_DIFF_LINE(f) \
220220
if (strcmp(#f, "pubKeyOperator") == 0 && (obj.fields & Field_pubKeyOperator)) {\
221221
SER_READ(obj, read_pubkey = true); \
222-
READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.state.pubKeyOperator), obj.state.nVersion == CProRegTx::LEGACY_BLS_VERSION)); \
222+
READWRITE(CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.state.pubKeyOperator), obj.state.nVersion == ProTxVersion::LegacyBLS)); \
223223
} else if (obj.fields & Field_##f) READWRITE(obj.state.f);
224224

225225
DMN_STATE_DIFF_ALL_FIELDS
226226
#undef DMN_STATE_DIFF_LINE
227227
if (read_pubkey) {
228228
SER_READ(obj, obj.fields |= Field_nVersion);
229-
SER_READ(obj, obj.state.pubKeyOperator.SetLegacy(obj.state.nVersion == CProRegTx::LEGACY_BLS_VERSION));
229+
SER_READ(obj, obj.state.pubKeyOperator.SetLegacy(obj.state.nVersion == ProTxVersion::LegacyBLS));
230230
}
231231
}
232232

src/evo/providertx.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bool CProRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState&
1717
if (nVersion == 0 || nVersion > GetVersion(is_basic_scheme_active)) {
1818
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version");
1919
}
20-
if (nVersion != BASIC_BLS_VERSION && nType == MnType::Evo) {
20+
if (nVersion < ProTxVersion::BasicBLS && nType == MnType::Evo) {
2121
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-evo-version");
2222
}
2323
if (!IsValidMnType(nType)) {
@@ -30,7 +30,7 @@ bool CProRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationState&
3030
if (keyIDOwner.IsNull() || !pubKeyOperator.Get().IsValid() || keyIDVoting.IsNull()) {
3131
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-key-null");
3232
}
33-
if (pubKeyOperator.IsLegacy() != (nVersion == LEGACY_BLS_VERSION)) {
33+
if (pubKeyOperator.IsLegacy() != (nVersion == ProTxVersion::LegacyBLS)) {
3434
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-pubkey");
3535
}
3636
if (!scriptPayout.IsPayToPublicKeyHash() && !scriptPayout.IsPayToScriptHash()) {
@@ -101,7 +101,7 @@ bool CProUpServTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationSta
101101
if (nVersion == 0 || nVersion > GetVersion(is_basic_scheme_active)) {
102102
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-version");
103103
}
104-
if (nVersion != BASIC_BLS_VERSION && nType == MnType::Evo) {
104+
if (nVersion < ProTxVersion::BasicBLS && nType == MnType::Evo) {
105105
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-protx-evo-version");
106106
}
107107

@@ -134,7 +134,7 @@ bool CProUpRegTx::IsTriviallyValid(bool is_basic_scheme_active, TxValidationStat
134134
if (!pubKeyOperator.Get().IsValid() || keyIDVoting.IsNull()) {
135135
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-key-null");
136136
}
137-
if (pubKeyOperator.IsLegacy() != (nVersion == LEGACY_BLS_VERSION)) {
137+
if (pubKeyOperator.IsLegacy() != (nVersion == ProTxVersion::LegacyBLS)) {
138138
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-protx-operator-pubkey");
139139
}
140140
if (!scriptPayout.IsPayToPublicKeyHash() && !scriptPayout.IsPayToScriptHash()) {

src/evo/providertx.h

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,24 @@
1919

2020
class TxValidationState;
2121

22+
namespace ProTxVersion {
23+
enum : uint16_t {
24+
LegacyBLS = 1,
25+
BasicBLS = 2,
26+
};
27+
} // namespace ProTxVersion
28+
2229
class CProRegTx
2330
{
2431
public:
2532
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_REGISTER;
26-
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
27-
static constexpr uint16_t BASIC_BLS_VERSION = 2;
2833

2934
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
3035
{
31-
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
36+
return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS;
3237
}
3338

34-
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
39+
uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version
3540
MnType nType{MnType::Regular};
3641
uint16_t nMode{0}; // only 0 supported for now
3742
COutPoint collateralOutpoint{uint256(), (uint32_t)-1}; // if hash is null, we refer to a ProRegTx output
@@ -52,7 +57,7 @@ class CProRegTx
5257
READWRITE(
5358
obj.nVersion
5459
);
55-
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
60+
if (obj.nVersion == 0 || obj.nVersion > ProTxVersion::BasicBLS) {
5661
// unknown version, bail out early
5762
return;
5863
}
@@ -63,7 +68,7 @@ class CProRegTx
6368
obj.collateralOutpoint,
6469
obj.addr,
6570
obj.keyIDOwner,
66-
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)),
71+
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), (obj.nVersion == ProTxVersion::LegacyBLS)),
6772
obj.keyIDVoting,
6873
obj.nOperatorReward,
6974
obj.scriptPayout,
@@ -119,15 +124,13 @@ class CProUpServTx
119124
{
120125
public:
121126
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_SERVICE;
122-
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
123-
static constexpr uint16_t BASIC_BLS_VERSION = 2;
124127

125128
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
126129
{
127-
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
130+
return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS;
128131
}
129132

130-
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
133+
uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version
131134
MnType nType{MnType::Regular};
132135
uint256 proTxHash;
133136
CService addr;
@@ -143,11 +146,11 @@ class CProUpServTx
143146
READWRITE(
144147
obj.nVersion
145148
);
146-
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
149+
if (obj.nVersion == 0 || obj.nVersion > ProTxVersion::BasicBLS) {
147150
// unknown version, bail out early
148151
return;
149152
}
150-
if (obj.nVersion == BASIC_BLS_VERSION) {
153+
if (obj.nVersion >= ProTxVersion::BasicBLS) {
151154
READWRITE(
152155
obj.nType);
153156
}
@@ -165,7 +168,7 @@ class CProUpServTx
165168
}
166169
if (!(s.GetType() & SER_GETHASH)) {
167170
READWRITE(
168-
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == LEGACY_BLS_VERSION))
171+
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == ProTxVersion::LegacyBLS))
169172
);
170173
}
171174
}
@@ -199,15 +202,13 @@ class CProUpRegTx
199202
{
200203
public:
201204
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_REGISTRAR;
202-
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
203-
static constexpr uint16_t BASIC_BLS_VERSION = 2;
204205

205206
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
206207
{
207-
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
208+
return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS;
208209
}
209210

210-
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
211+
uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version
211212
uint256 proTxHash;
212213
uint16_t nMode{0}; // only 0 supported for now
213214
CBLSLazyPublicKey pubKeyOperator;
@@ -221,14 +222,14 @@ class CProUpRegTx
221222
READWRITE(
222223
obj.nVersion
223224
);
224-
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
225+
if (obj.nVersion == 0 || obj.nVersion > ProTxVersion::BasicBLS) {
225226
// unknown version, bail out early
226227
return;
227228
}
228229
READWRITE(
229230
obj.proTxHash,
230231
obj.nMode,
231-
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)),
232+
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), (obj.nVersion == ProTxVersion::LegacyBLS)),
232233
obj.keyIDVoting,
233234
obj.scriptPayout,
234235
obj.inputsHash
@@ -264,12 +265,10 @@ class CProUpRevTx
264265
{
265266
public:
266267
static constexpr auto SPECIALTX_TYPE = TRANSACTION_PROVIDER_UPDATE_REVOKE;
267-
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
268-
static constexpr uint16_t BASIC_BLS_VERSION = 2;
269268

270269
[[nodiscard]] static constexpr auto GetVersion(const bool is_basic_scheme_active) -> uint16_t
271270
{
272-
return is_basic_scheme_active ? BASIC_BLS_VERSION : LEGACY_BLS_VERSION;
271+
return is_basic_scheme_active ? ProTxVersion::BasicBLS : ProTxVersion::LegacyBLS;
273272
}
274273

275274
// these are just informational and do not have any effect on the revocation
@@ -281,7 +280,7 @@ class CProUpRevTx
281280
REASON_LAST = REASON_CHANGE_OF_KEYS
282281
};
283282

284-
uint16_t nVersion{LEGACY_BLS_VERSION}; // message version
283+
uint16_t nVersion{ProTxVersion::LegacyBLS}; // message version
285284
uint256 proTxHash;
286285
uint16_t nReason{REASON_NOT_SPECIFIED};
287286
uint256 inputsHash; // replay protection
@@ -292,7 +291,7 @@ class CProUpRevTx
292291
READWRITE(
293292
obj.nVersion
294293
);
295-
if (obj.nVersion == 0 || obj.nVersion > BASIC_BLS_VERSION) {
294+
if (obj.nVersion == 0 || obj.nVersion > ProTxVersion::BasicBLS) {
296295
// unknown version, bail out early
297296
return;
298297
}
@@ -303,7 +302,7 @@ class CProUpRevTx
303302
);
304303
if (!(s.GetType() & SER_GETHASH)) {
305304
READWRITE(
306-
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == LEGACY_BLS_VERSION))
305+
CBLSSignatureVersionWrapper(const_cast<CBLSSignature&>(obj.sig), (obj.nVersion == ProTxVersion::LegacyBLS))
307306
);
308307
}
309308
}

src/evo/simplifiedmns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ CSimplifiedMNListEntry::CSimplifiedMNListEntry(const CDeterministicMN& dmn) :
3838
platformNodeID(dmn.pdmnState->platformNodeID),
3939
scriptPayout(dmn.pdmnState->scriptPayout),
4040
scriptOperatorPayout(dmn.pdmnState->scriptOperatorPayout),
41-
nVersion(dmn.pdmnState->nVersion == CProRegTx::LEGACY_BLS_VERSION ? LEGACY_BLS_VERSION : BASIC_BLS_VERSION),
41+
nVersion(dmn.pdmnState->nVersion),
4242
nType(dmn.nType)
4343
{
4444
}

src/evo/simplifiedmns.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <bls/bls.h>
99
#include <evo/dmn_types.h>
10+
#include <evo/providertx.h>
1011
#include <merkleblock.h>
1112
#include <netaddress.h>
1213
#include <pubkey.h>
@@ -31,9 +32,6 @@ class CQuorumManager;
3132
class CSimplifiedMNListEntry
3233
{
3334
public:
34-
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
35-
static constexpr uint16_t BASIC_BLS_VERSION = 2;
36-
3735
uint256 proRegTxHash;
3836
uint256 confirmedHash;
3937
CService service;
@@ -44,7 +42,7 @@ class CSimplifiedMNListEntry
4442
uint160 platformNodeID{};
4543
CScript scriptPayout; // mem-only
4644
CScript scriptOperatorPayout; // mem-only
47-
uint16_t nVersion{LEGACY_BLS_VERSION};
45+
uint16_t nVersion{ProTxVersion::LegacyBLS};
4846
MnType nType{MnType::Regular};
4947

5048
CSimplifiedMNListEntry() = default;
@@ -78,14 +76,14 @@ class CSimplifiedMNListEntry
7876
obj.proRegTxHash,
7977
obj.confirmedHash,
8078
obj.service,
81-
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), (obj.nVersion == LEGACY_BLS_VERSION)),
79+
CBLSLazyPublicKeyVersionWrapper(const_cast<CBLSLazyPublicKey&>(obj.pubKeyOperator), (obj.nVersion == ProTxVersion::LegacyBLS)),
8280
obj.keyIDVoting,
8381
obj.isValid
8482
);
8583
if ((s.GetType() & SER_NETWORK) && s.GetVersion() < DMN_TYPE_PROTO_VERSION) {
8684
return;
8785
}
88-
if (obj.nVersion == BASIC_BLS_VERSION) {
86+
if (obj.nVersion >= ProTxVersion::BasicBLS) {
8987
READWRITE(obj.nType);
9088
if (obj.nType == MnType::Evo) {
9189
READWRITE(obj.platformHTTPPort);

src/rpc/evo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,8 @@ static UniValue protx_register_common_wrapper(const JSONRPCRequest& request,
689689

690690
ptx.keyIDOwner = ParsePubKeyIDFromAddress(request.params[paramIdx + 1].get_str(), "owner address");
691691
ptx.pubKeyOperator.Set(ParseBLSPubKey(request.params[paramIdx + 2].get_str(), "operator BLS address", use_legacy), use_legacy);
692-
ptx.nVersion = use_legacy ? CProRegTx::LEGACY_BLS_VERSION : CProRegTx::BASIC_BLS_VERSION;
693-
CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == CProRegTx::LEGACY_BLS_VERSION));
692+
ptx.nVersion = use_legacy ? ProTxVersion::LegacyBLS : ProTxVersion::BasicBLS;
693+
CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS));
694694

695695
CKeyID keyIDVoting = ptx.keyIDOwner;
696696

@@ -1111,8 +1111,8 @@ static RPCHelpMan protx_update_registrar_wrapper(const bool specific_legacy_bls_
11111111
ptx.pubKeyOperator = dmn->pdmnState->pubKeyOperator;
11121112
}
11131113

1114-
ptx.nVersion = use_legacy ? CProUpRegTx::LEGACY_BLS_VERSION : CProUpRegTx::BASIC_BLS_VERSION;
1115-
CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == CProUpRegTx::LEGACY_BLS_VERSION));
1114+
ptx.nVersion = use_legacy ? ProTxVersion::LegacyBLS : ProTxVersion::BasicBLS;
1115+
CHECK_NONFATAL(ptx.pubKeyOperator.IsLegacy() == (ptx.nVersion == ProTxVersion::LegacyBLS));
11161116

11171117
if (request.params[2].get_str() != "") {
11181118
ptx.keyIDVoting = ParsePubKeyIDFromAddress(request.params[2].get_str(), "voting address");

0 commit comments

Comments
 (0)