Skip to content

Commit

Permalink
feat: store protx version in CSimplifiedMNListEntry and use it to ser…
Browse files Browse the repository at this point in the history
…/deser pubKeyOperator
  • Loading branch information
UdjinM6 committed May 27, 2023
1 parent 23770b5 commit 26f7e96
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
3 changes: 1 addition & 2 deletions src/evo/cbtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev
int64_t nTime2 = GetTimeMicros(); nTimeDMN += nTime2 - nTime1;
LogPrint(BCLog::BENCHMARK, " - BuildNewListFromBlock: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeDMN * 0.000001);

bool v19active = llmq::utils::IsV19Active(pindexPrev);
CSimplifiedMNList sml(tmpMNList, v19active);
CSimplifiedMNList sml(tmpMNList);

int64_t nTime3 = GetTimeMicros(); nTimeSMNL += nTime3 - nTime2;
LogPrint(BCLog::BENCHMARK, " - CSimplifiedMNList: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeSMNL * 0.000001);
Expand Down
14 changes: 4 additions & 10 deletions src/evo/simplifiedmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ CSimplifiedMNListEntry::CSimplifiedMNListEntry(const CDeterministicMN& dmn) :
isValid(!dmn.pdmnState->IsBanned()),
scriptPayout(dmn.pdmnState->scriptPayout),
scriptOperatorPayout(dmn.pdmnState->scriptOperatorPayout),
nVersion(dmn.pdmnState->nVersion == CProRegTx::LEGACY_BLS_VERSION ? LEGACY_BLS_VERSION : BASIC_BLS_VERSION),
nType(dmn.nType),
platformHTTPPort(dmn.pdmnState->platformHTTPPort),
platformNodeID(dmn.pdmnState->platformNodeID)
Expand Down Expand Up @@ -103,15 +104,13 @@ CSimplifiedMNList::CSimplifiedMNList(const std::vector<CSimplifiedMNListEntry>&
});
}

CSimplifiedMNList::CSimplifiedMNList(const CDeterministicMNList& dmnList, bool isV19Active)
CSimplifiedMNList::CSimplifiedMNList(const CDeterministicMNList& dmnList)
{
mnList.resize(dmnList.GetAllMNsCount());

size_t i = 0;
dmnList.ForEachMN(false, [this, &i, isV19Active](auto& dmn) {
auto sme = std::make_unique<CSimplifiedMNListEntry>(dmn);
sme->nVersion = isV19Active ? CSimplifiedMNListEntry::BASIC_BLS_VERSION : CSimplifiedMNListEntry::LEGACY_BLS_VERSION;
mnList[i++] = std::move(sme);
dmnList.ForEachMN(false, [this, &i](auto& dmn) {
mnList[i++] = std::make_unique<CSimplifiedMNListEntry>(dmn);
});

std::sort(mnList.begin(), mnList.end(), [&](const std::unique_ptr<CSimplifiedMNListEntry>& a, const std::unique_ptr<CSimplifiedMNListEntry>& b) {
Expand Down Expand Up @@ -238,23 +237,18 @@ void CSimplifiedMNListDiff::ToJson(UniValue& obj, bool extended) const

CSimplifiedMNListDiff BuildSimplifiedDiff(const CDeterministicMNList& from, const CDeterministicMNList& to, bool extended)
{
bool v19active = llmq::utils::IsV19Active(::ChainActive().Tip());
CSimplifiedMNListDiff diffRet;
diffRet.baseBlockHash = from.GetBlockHash();
diffRet.blockHash = to.GetBlockHash();
diffRet.nVersion = v19active ? CSimplifiedMNListDiff::BASIC_BLS_VERSION : CSimplifiedMNListDiff::LEGACY_BLS_VERSION;

to.ForEachMN(false, [&](const auto& toPtr) {
auto fromPtr = from.GetMN(toPtr.proTxHash);
if (fromPtr == nullptr) {
CSimplifiedMNListEntry sme(toPtr);
sme.nVersion = diffRet.nVersion;
diffRet.mnList.push_back(std::move(sme));
} else {
CSimplifiedMNListEntry sme1(toPtr);
CSimplifiedMNListEntry sme2(*fromPtr);
sme1.nVersion = diffRet.nVersion;
sme2.nVersion = diffRet.nVersion;
if ((sme1 != sme2) ||
(extended && (sme1.scriptPayout != sme2.scriptPayout || sme1.scriptOperatorPayout != sme2.scriptOperatorPayout))) {
diffRet.mnList.push_back(std::move(sme1));
Expand Down
12 changes: 7 additions & 5 deletions src/evo/simplifiedmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CSimplifiedMNListEntry
uint160 platformNodeID{};
CScript scriptPayout; // mem-only
CScript scriptOperatorPayout; // mem-only
uint16_t nVersion{LEGACY_BLS_VERSION}; // mem-only
uint16_t nVersion{LEGACY_BLS_VERSION};

CSimplifiedMNListEntry() = default;
explicit CSimplifiedMNListEntry(const CDeterministicMN& dmn);
Expand All @@ -65,6 +65,9 @@ class CSimplifiedMNListEntry

SERIALIZE_METHODS(CSimplifiedMNListEntry, obj)
{
if ((s.GetType() & SER_NETWORK) && s.GetVersion() >= SMNLE_VERSIONED_PROTO_VERSION) {
READWRITE(obj.nVersion);
}
READWRITE(
obj.proRegTxHash,
obj.confirmedHash,
Expand Down Expand Up @@ -98,7 +101,7 @@ class CSimplifiedMNList

CSimplifiedMNList() = default;
explicit CSimplifiedMNList(const std::vector<CSimplifiedMNListEntry>& smlEntries);
explicit CSimplifiedMNList(const CDeterministicMNList& dmnList, bool isV19Active);
explicit CSimplifiedMNList(const CDeterministicMNList& dmnList);

uint256 CalcMerkleRoot(bool* pmutated = nullptr) const;
bool operator==(const CSimplifiedMNList& rhs) const;
Expand All @@ -121,16 +124,15 @@ class CGetSimplifiedMNListDiff
class CSimplifiedMNListDiff
{
public:
static constexpr uint16_t LEGACY_BLS_VERSION = 1;
static constexpr uint16_t BASIC_BLS_VERSION = 2;
static constexpr uint16_t CURRENT_VERSION = 1;

uint256 baseBlockHash;
uint256 blockHash;
CPartialMerkleTree cbTxMerkleTree;
CTransactionRef cbTx;
std::vector<uint256> deletedMNs;
std::vector<CSimplifiedMNListEntry> mnList;
uint16_t nVersion{LEGACY_BLS_VERSION};
uint16_t nVersion{CURRENT_VERSION};

std::vector<std::pair<uint8_t, uint256>> deletedQuorums; // p<LLMQType, quorumHash>
std::vector<llmq::CFinalCommitment> newQuorums;
Expand Down
5 changes: 4 additions & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/


static const int PROTOCOL_VERSION = 70227;
static const int PROTOCOL_VERSION = 70228;

//! initial proto version, to be increased after version/verack negotiation
static const int INIT_PROTO_VERSION = 209;
Expand Down Expand Up @@ -49,6 +49,9 @@ static const int COINJOIN_PROTX_HASH_PROTO_VERSION = 70226;
//! Masternode type was introduced in this version
static const int DMN_TYPE_PROTO_VERSION = 70227;

//! Versioned Simplified Masternode List Entries were introduced in this version
static const int SMNLE_VERSIONED_PROTO_VERSION = 70228;

// Make sure that none of the values above collide with `ADDRV2_FORMAT`.

#endif // BITCOIN_VERSION_H
2 changes: 1 addition & 1 deletion test/functional/feature_dip3_v19.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_getmnlistdiff(self, base_block_hash, block_hash, base_mn_list, expected
# Verify that the merkle root matches what we locally calculate
hashes = []
for mn in sorted(new_mn_list.values(), key=lambda mn: ser_uint256(mn.proRegTxHash)):
hashes.append(hash256(mn.serialize()))
hashes.append(hash256(mn.serialize(with_version = False)))
merkle_root = CBlock.get_merkle_root(hashes)
assert_equal(merkle_root, cbtx.merkleRootMNList)

Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_dip4_coinbasemerkleroots.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_getmnlistdiff(self, baseBlockHash, blockHash, baseMNList, expectedDelet
# Verify that the merkle root matches what we locally calculate
hashes = []
for mn in sorted(newMNList.values(), key=lambda mn: ser_uint256(mn.proRegTxHash)):
hashes.append(hash256(mn.serialize()))
hashes.append(hash256(mn.serialize(with_version = False)))
merkleRoot = CBlock.get_merkle_root(hashes)
assert_equal(merkleRoot, cbtx.merkleRootMNList)

Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_llmq_hpmn.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_getmnlistdiff(self, baseBlockHash, blockHash, baseMNList, expectedDelet
# Verify that the merkle root matches what we locally calculate
hashes = []
for mn in sorted(newMNList.values(), key=lambda mn: ser_uint256(mn.proRegTxHash)):
hashes.append(hash256(mn.serialize()))
hashes.append(hash256(mn.serialize(with_version = False)))
merkleRoot = CBlock.get_merkle_root(hashes)
assert_equal(merkleRoot, cbtx.merkleRootMNList)

Expand Down
26 changes: 14 additions & 12 deletions test/functional/test_framework/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import dash_hash

MIN_VERSION_SUPPORTED = 60001
MY_VERSION = 70227 # DMN_TYPE_PROTO_VERSION
MY_VERSION = 70228 # SMNLE_VERSIONED_PROTO_VERSION
MY_SUBVERSION = b"/python-mininode-tester:0.0.3%s/"
MY_RELAY = 1 # from version 70001 onwards, fRelay should be appended to version messages (BIP37)

Expand Down Expand Up @@ -1095,7 +1095,7 @@ def serialize(self):


class CSimplifiedMNListEntry:
__slots__ = ("proRegTxHash", "confirmedHash", "service", "pubKeyOperator", "keyIDVoting", "isValid", "version", "type", "platformHTTPPort", "platformNodeID")
__slots__ = ("proRegTxHash", "confirmedHash", "service", "pubKeyOperator", "keyIDVoting", "isValid", "nVersion", "type", "platformHTTPPort", "platformNodeID")

def __init__(self):
self.set_null()
Expand All @@ -1107,34 +1107,36 @@ def set_null(self):
self.pubKeyOperator = b'\x00' * 48
self.keyIDVoting = 0
self.isValid = False
self.version = 0
self.nVersion = 0
self.type = 0
self.platformHTTPPort = 0
self.platformNodeID = b'\x00' * 20

def deserialize(self, f, version):
self.version = version # memory only
def deserialize(self, f):
self.nVersion = struct.unpack("<H", f.read(2))[0]
self.proRegTxHash = deser_uint256(f)
self.confirmedHash = deser_uint256(f)
self.service.deserialize(f)
self.pubKeyOperator = f.read(48)
self.keyIDVoting = f.read(20)
self.isValid = struct.unpack("<?", f.read(1))[0]
if self.version == 2:
if self.nVersion == 2:
self.type = struct.unpack("<H", f.read(2))[0]
if self.type == 1:
self.platformHTTPPort = struct.unpack("<H", f.read(2))[0]
self.platformNodeID = f.read(20)

def serialize(self):
def serialize(self, with_version = True):
r = b""
if with_version:
r += struct.pack("<H", self.nVersion)
r += ser_uint256(self.proRegTxHash)
r += ser_uint256(self.confirmedHash)
r += self.service.serialize()
r += self.pubKeyOperator
r += self.keyIDVoting
r += struct.pack("<?", self.isValid)
if self.version == 2:
if self.nVersion == 2:
r += struct.pack("<H", self.type)
if self.type == 1:
r += struct.pack("<H", self.platformHTTPPort)
Expand Down Expand Up @@ -1985,15 +1987,15 @@ def __repr__(self):
QuorumId = namedtuple('QuorumId', ['llmqType', 'quorumHash'])

class msg_mnlistdiff:
__slots__ = ("baseBlockHash", "blockHash", "merkleProof", "cbTx", "version", "deletedMNs", "mnList", "deletedQuorums", "newQuorums",)
__slots__ = ("baseBlockHash", "blockHash", "merkleProof", "cbTx", "nVersion", "deletedMNs", "mnList", "deletedQuorums", "newQuorums",)
command = b"mnlistdiff"

def __init__(self):
self.baseBlockHash = 0
self.blockHash = 0
self.merkleProof = CPartialMerkleTree()
self.cbTx = None
self.version = 0
self.nVersion = 0
self.deletedMNs = []
self.mnList = []
self.deletedQuorums = []
Expand All @@ -2006,12 +2008,12 @@ def deserialize(self, f):
self.cbTx = CTransaction()
self.cbTx.deserialize(f)
self.cbTx.rehash()
self.version = struct.unpack("<H", f.read(2))[0]
self.nVersion = struct.unpack("<H", f.read(2))[0]
self.deletedMNs = deser_uint256_vector(f)
self.mnList = []
for i in range(deser_compact_size(f)):
e = CSimplifiedMNListEntry()
e.deserialize(f, self.version)
e.deserialize(f)
self.mnList.append(e)

self.deletedQuorums = []
Expand Down

0 comments on commit 26f7e96

Please sign in to comment.