Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Post v19 cleanup #5622

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/bls/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,19 @@ CBLSPublicKey CBLSSecretKey::GetPublicKey() const
}

CBLSSignature CBLSSecretKey::Sign(const uint256& hash) const
{
return Sign(hash, bls::bls_legacy_scheme.load());
}

CBLSSignature CBLSSecretKey::Sign(const uint256& hash, const bool specificLegacyScheme) const
{
if (!IsValid()) {
return {};
}

CBLSSignature sigRet;
try {
sigRet.impl = Scheme(bls::bls_legacy_scheme.load())->Sign(impl, bls::Bytes(hash.begin(), hash.size()));
sigRet.impl = Scheme(specificLegacyScheme)->Sign(impl, bls::Bytes(hash.begin(), hash.size()));
sigRet.fValid = true;
} catch (...) {
sigRet.fValid = false;
Expand Down
1 change: 1 addition & 0 deletions src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class CBLSSecretKey : public CBLSWrapper<bls::PrivateKey, BLS_CURVE_SECKEY_SIZE,

[[nodiscard]] CBLSPublicKey GetPublicKey() const;
[[nodiscard]] CBLSSignature Sign(const uint256& hash) const;
[[nodiscard]] CBLSSignature Sign(const uint256& hash, const bool specificLegacyScheme) const;
};

class CBLSPublicKey : public CBLSWrapper<bls::G1Element, BLS_CURVE_PUBKEY_SIZE, CBLSPublicKey>
Expand Down
30 changes: 12 additions & 18 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,28 @@ bool CCoinJoinEntry::AddScriptSig(const CTxIn& txin)
return false;
}

uint256 CCoinJoinQueue::GetSignatureHash(bool legacy) const
uint256 CCoinJoinQueue::GetSignatureHash() const
{
int version = legacy ? COINJOIN_PROTX_HASH_PROTO_VERSION - 1 : PROTOCOL_VERSION;
return SerializeHash(*this, SER_GETHASH, version);
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
}

bool CCoinJoinQueue::Sign()
{
if (!fMasternodeMode) return false;

bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip());
uint256 hash = GetSignatureHash(legacy_bls_scheme);
CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash));
uint256 hash = GetSignatureHash();
CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash, false));
if (!sig.IsValid()) {
return false;
}
vchSig = sig.ToByteVector(legacy_bls_scheme);
vchSig = sig.ToByteVector(false);

return true;
}

bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
{
bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip());
if (!CBLSSignature(Span{vchSig}).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) {
if (!CBLSSignature(Span{vchSig}).VerifyInsecure(blsPubKey, GetSignatureHash(), false)) {
LogPrint(BCLog::COINJOIN, "CCoinJoinQueue::CheckSignature -- VerifyInsecure() failed\n");
return false;
}
Expand All @@ -90,31 +87,28 @@ bool CCoinJoinQueue::IsTimeOutOfBounds(int64_t current_time) const
nTime - current_time > COINJOIN_QUEUE_TIMEOUT;
}

uint256 CCoinJoinBroadcastTx::GetSignatureHash(bool legacy) const
uint256 CCoinJoinBroadcastTx::GetSignatureHash() const
{
int version = legacy ? COINJOIN_PROTX_HASH_PROTO_VERSION - 1 : PROTOCOL_VERSION;
return SerializeHash(*this, SER_GETHASH, version);
return SerializeHash(*this, SER_GETHASH, PROTOCOL_VERSION);
}

bool CCoinJoinBroadcastTx::Sign()
{
if (!fMasternodeMode) return false;

bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip());
uint256 hash = GetSignatureHash(legacy_bls_scheme);
CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash));
uint256 hash = GetSignatureHash();
CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash, false));
if (!sig.IsValid()) {
return false;
}
vchSig = sig.ToByteVector(legacy_bls_scheme);
vchSig = sig.ToByteVector(false);

return true;
}

bool CCoinJoinBroadcastTx::CheckSignature(const CBLSPublicKey& blsPubKey) const
{
bool legacy_bls_scheme = !llmq::utils::IsV19Active(::ChainActive().Tip());
if (!CBLSSignature(Span{vchSig}).VerifyInsecure(blsPubKey, GetSignatureHash(legacy_bls_scheme))) {
if (!CBLSSignature(Span{vchSig}).VerifyInsecure(blsPubKey, GetSignatureHash(), false)) {
LogPrint(BCLog::COINJOIN, "CCoinJoinBroadcastTx::CheckSignature -- VerifyInsecure() failed\n");
return false;
}
Expand Down
27 changes: 5 additions & 22 deletions src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ class CCoinJoinStatusUpdate

SERIALIZE_METHODS(CCoinJoinStatusUpdate, obj)
{
READWRITE(obj.nSessionID, obj.nState);
if (s.GetVersion() <= COINJOIN_SU_PROTO_VERSION) {
READWRITE(obj.nEntriesCount);
}
READWRITE(obj.nStatusUpdate, obj.nMessageID);
READWRITE(obj.nSessionID, obj.nState, obj.nStatusUpdate, obj.nMessageID);
}
};

Expand Down Expand Up @@ -219,20 +215,13 @@ class CCoinJoinQueue

SERIALIZE_METHODS(CCoinJoinQueue, obj)
{
READWRITE(obj.nDenom);

if (s.GetVersion() < COINJOIN_PROTX_HASH_PROTO_VERSION) {
READWRITE(obj.masternodeOutpoint);
} else {
READWRITE(obj.m_protxHash);
}
READWRITE(obj.nTime, obj.fReady);
READWRITE(obj.nDenom, obj.m_protxHash, obj.nTime, obj.fReady);
if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
}
}

[[nodiscard]] uint256 GetSignatureHash(bool legacy) const;
[[nodiscard]] uint256 GetSignatureHash() const;
/** Sign this mixing transaction
* return true if all conditions are met:
* 1) we have an active Masternode,
Expand Down Expand Up @@ -292,13 +281,7 @@ class CCoinJoinBroadcastTx

SERIALIZE_METHODS(CCoinJoinBroadcastTx, obj)
{
READWRITE(obj.tx);

if (s.GetVersion() < COINJOIN_PROTX_HASH_PROTO_VERSION) {
READWRITE(obj.masternodeOutpoint);
} else {
READWRITE(obj.m_protxHash);
}
READWRITE(obj.tx, obj.m_protxHash);

if (!(s.GetType() & SER_GETHASH)) {
READWRITE(obj.vchSig);
Expand All @@ -319,7 +302,7 @@ class CCoinJoinBroadcastTx
return *this != CCoinJoinBroadcastTx();
}

[[nodiscard]] uint256 GetSignatureHash(bool legacy) const;
[[nodiscard]] uint256 GetSignatureHash() const;

bool Sign();
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
Expand Down
11 changes: 2 additions & 9 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,7 @@ void CGovernanceManager::SyncSingleObjVotes(CNode& peer, const uint256& nProp, c

LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s -- syncing single object to peer=%d, nProp = %s\n", __func__, peer.GetId(), nProp.ToString());

// TODO: drop cs_main here when v19 activation is buried
// and CGovernanceVote::CheckSignature no longer needs to use ::ChainActive()
LOCK2(cs_main, cs);
LOCK(cs);

// single valid object and its valid votes
auto it = mapObjects.find(nProp);
Expand Down Expand Up @@ -1021,9 +1019,6 @@ bool CGovernanceManager::MasternodeRateCheck(const CGovernanceObject& govobj, bo

bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman)
{
// TODO: drop cs_main here when v19 activation is buried
// and CGovernanceVote::CheckSignature no longer needs to use ::ChainActive()
LOCK(cs_main);
ENTER_CRITICAL_SECTION(cs)
uint256 nHashVote = vote.GetHash();
uint256 nHashGovobj = vote.GetParentHash();
Expand Down Expand Up @@ -1487,9 +1482,7 @@ void CGovernanceManager::RemoveInvalidVotes()
return;
}

// TODO: drop cs_main here when v19 activation is buried
// and CGovernanceVote::CheckSignature no longer needs to use ::ChainActive()
LOCK2(cs_main, cs);
LOCK(cs);

auto curMNList = deterministicMNManager->GetListAtChainTip();
auto diff = lastMNListForVotingKeys->BuildDiff(curMNList);
Expand Down
12 changes: 4 additions & 8 deletions src/governance/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,19 @@ void CGovernanceObject::SetMasternodeOutpoint(const COutPoint& outpoint)

bool CGovernanceObject::Sign(const CBLSSecretKey& key)
{
CBLSSignature sig = key.Sign(GetSignatureHash());
CBLSSignature sig = key.Sign(GetSignatureHash(), false);
if (!sig.IsValid()) {
return false;
}
const auto pindex = llmq::utils::V19ActivationIndex(::ChainActive().Tip());
bool is_bls_legacy_scheme = pindex == nullptr || nTime < pindex->pprev->nTime;
vchSig = sig.ToByteVector(is_bls_legacy_scheme);
vchSig = sig.ToByteVector(false);
return true;
}

bool CGovernanceObject::CheckSignature(const CBLSPublicKey& pubKey) const
{
CBLSSignature sig;
const auto pindex = llmq::utils::V19ActivationIndex(::ChainActive().Tip());
bool is_bls_legacy_scheme = pindex == nullptr || nTime < pindex->pprev->nTime;
sig.SetByteVector(vchSig, is_bls_legacy_scheme);
if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), is_bls_legacy_scheme)) {
sig.SetByteVector(vchSig, false);
if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), false)) {
LogPrintf("CGovernanceObject::CheckSignature -- VerifyInsecure() failed\n");
return false;
}
Expand Down
12 changes: 4 additions & 8 deletions src/governance/vote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,19 @@ bool CGovernanceVote::CheckSignature(const CKeyID& keyID) const

bool CGovernanceVote::Sign(const CBLSSecretKey& key)
{
CBLSSignature sig = key.Sign(GetSignatureHash());
CBLSSignature sig = key.Sign(GetSignatureHash(), false);
if (!sig.IsValid()) {
return false;
}
const auto pindex = llmq::utils::V19ActivationIndex(::ChainActive().Tip());
bool is_bls_legacy_scheme = pindex == nullptr || nTime < pindex->pprev->nTime;
vchSig = sig.ToByteVector(is_bls_legacy_scheme);
vchSig = sig.ToByteVector(false);
return true;
}

bool CGovernanceVote::CheckSignature(const CBLSPublicKey& pubKey) const
{
CBLSSignature sig;
const auto pindex = llmq::utils::V19ActivationIndex(::ChainActive().Tip());
bool is_bls_legacy_scheme = pindex == nullptr || nTime < pindex->pprev->nTime;
sig.SetByteVector(vchSig, is_bls_legacy_scheme);
if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), is_bls_legacy_scheme)) {
sig.SetByteVector(vchSig, false);
if (!sig.VerifyInsecure(pubKey, GetSignatureHash(), false)) {
LogPrintf("CGovernanceVote::CheckSignature -- VerifyInsecure() failed\n");
return false;
}
Expand Down
6 changes: 0 additions & 6 deletions src/llmq/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,6 @@ bool IsV19Active(const CBlockIndex* pindex)
return pindex->nHeight + 1 >= Params().GetConsensus().V19Height;
}

const CBlockIndex* V19ActivationIndex(const CBlockIndex* pindex)
{
assert(pindex);
return pindex->GetAncestor(Params().GetConsensus().V19Height);
}

bool IsV20Active(const CBlockIndex* pindex)
{
assert(pindex);
Expand Down
1 change: 0 additions & 1 deletion src/llmq/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ Consensus::LLMQType GetInstantSendLLMQType(const CQuorumManager& qman, const CBl
Consensus::LLMQType GetInstantSendLLMQType(bool deterministic);
bool IsDIP0024Active(const CBlockIndex* pindex);
bool IsV19Active(const CBlockIndex* pindex);
const CBlockIndex* V19ActivationIndex(const CBlockIndex* pindex);
bool IsV20Active(const CBlockIndex* pindex);
bool IsMNRewardReallocationActive(const CBlockIndex* pindex);
ThresholdState GetMNRewardReallocationState(const CBlockIndex* pindex);
Expand Down
2 changes: 1 addition & 1 deletion src/test/evo_deterministicmns_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ void FuncV19Activation(TestChainSetup& setup)
}

// check mn list/diff
const CBlockIndex* v19_index = llmq::utils::V19ActivationIndex(::ChainActive().Tip());
const CBlockIndex* v19_index = ::ChainActive().Tip()->GetAncestor(Params().GetConsensus().V19Height);
auto v19_list = deterministicMNManager->GetListForBlock(v19_index);
dummy_diff = v19_list.BuildDiff(tip_list);
dummmy_list = v19_list.ApplyDiff(::ChainActive().Tip(), dummy_diff);
Expand Down
6 changes: 0 additions & 6 deletions src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ static const int GOVSCRIPT_PROTO_VERSION = 70221;
//! ADDRV2 was introduced in this version
static const int ADDRV2_PROTO_VERSION = 70223;

//! CCoinJoinStatusUpdate bug fix was introduced in this version
static const int COINJOIN_SU_PROTO_VERSION = 70224;

//! BLS scheme was introduced in this version
static const int BLS_SCHEME_PROTO_VERSION = 70225;

//! DSQ and DSTX started using protx hash in this version
static const int COINJOIN_PROTX_HASH_PROTO_VERSION = 70226;

//! Masternode type was introduced in this version
static const int DMN_TYPE_PROTO_VERSION = 70227;

Expand Down
Loading