Skip to content

Commit

Permalink
suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Oct 30, 2023
1 parent 6ef4cd6 commit d9b67c2
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 189 deletions.
70 changes: 0 additions & 70 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ class CMainParams : public CChainParams {
AddLLMQ(Consensus::LLMQType::LLMQ_400_85);
AddLLMQ(Consensus::LLMQType::LLMQ_100_67);
consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_400_60;
consensus.llmqTypeInstantSend = Consensus::LLMQType::LLMQ_50_60;
consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_60_75;
consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_100_67;
consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_400_85;
Expand Down Expand Up @@ -474,7 +473,6 @@ class CTestNetParams : public CChainParams {
AddLLMQ(Consensus::LLMQType::LLMQ_100_67);
AddLLMQ(Consensus::LLMQType::LLMQ_25_67);
consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_50_60;
consensus.llmqTypeInstantSend = Consensus::LLMQType::LLMQ_50_60;
consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_60_75;
consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_25_67;
consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_50_60;
Expand Down Expand Up @@ -649,14 +647,12 @@ class CDevNetParams : public CChainParams {
AddLLMQ(Consensus::LLMQType::LLMQ_DEVNET_DIP0024);
AddLLMQ(Consensus::LLMQType::LLMQ_DEVNET_PLATFORM);
consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_DEVNET;
consensus.llmqTypeInstantSend = Consensus::LLMQType::LLMQ_DEVNET;
consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_DEVNET_DIP0024;
consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_DEVNET_PLATFORM;
consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_DEVNET;
consensus.llmqTypeAssetLocks = consensus.llmqTypePlatform;

UpdateDevnetLLMQChainLocksFromArgs(args);
UpdateDevnetLLMQInstantSendFromArgs(args);
UpdateDevnetLLMQInstantSendDIP0024FromArgs(args);
UpdateDevnetLLMQPlatformFromArgs(args);
UpdateDevnetLLMQMnhfFromArgs(args);
Expand Down Expand Up @@ -713,14 +709,6 @@ class CDevNetParams : public CChainParams {
}
void UpdateDevnetLLMQChainLocksFromArgs(const ArgsManager& args);

/**
* Allows modifying the LLMQ type for InstantSend.
*/
void UpdateDevnetLLMQInstantSend(Consensus::LLMQType llmqType)
{
consensus.llmqTypeInstantSend = llmqType;
}

/**
* Allows modifying the LLMQ type for InstantSend (DIP0024).
*/
Expand Down Expand Up @@ -936,15 +924,13 @@ class CRegTestParams : public CChainParams {
AddLLMQ(Consensus::LLMQType::LLMQ_TEST_DIP0024);
AddLLMQ(Consensus::LLMQType::LLMQ_TEST_PLATFORM);
consensus.llmqTypeChainLocks = Consensus::LLMQType::LLMQ_TEST;
consensus.llmqTypeInstantSend = Consensus::LLMQType::LLMQ_TEST_INSTANTSEND;
consensus.llmqTypeDIP0024InstantSend = Consensus::LLMQType::LLMQ_TEST_DIP0024;
consensus.llmqTypePlatform = Consensus::LLMQType::LLMQ_TEST_PLATFORM;
consensus.llmqTypeMnhf = Consensus::LLMQType::LLMQ_TEST;
consensus.llmqTypeAssetLocks = Consensus::LLMQType::LLMQ_TEST;

UpdateLLMQTestParametersFromArgs(args, Consensus::LLMQType::LLMQ_TEST);
UpdateLLMQTestParametersFromArgs(args, Consensus::LLMQType::LLMQ_TEST_INSTANTSEND);
UpdateLLMQInstantSendFromArgs(args);
UpdateLLMQInstantSendDIP0024FromArgs(args);
}

Expand Down Expand Up @@ -1016,14 +1002,6 @@ class CRegTestParams : public CChainParams {
params->dkgBadVotesThreshold = threshold;
}

/**
* Allows modifying the LLMQ type for InstantSend.
*/
void UpdateLLMQInstantSend(Consensus::LLMQType llmqType)
{
consensus.llmqTypeInstantSend = llmqType;
}

/**
* Allows modifying the LLMQ type for InstantSend (DIP0024).
*/
Expand All @@ -1033,7 +1011,6 @@ class CRegTestParams : public CChainParams {
}

void UpdateLLMQTestParametersFromArgs(const ArgsManager& args, const Consensus::LLMQType llmqType);
void UpdateLLMQInstantSendFromArgs(const ArgsManager& args);
void UpdateLLMQInstantSendDIP0024FromArgs(const ArgsManager& args);
};

Expand Down Expand Up @@ -1179,28 +1156,6 @@ void CRegTestParams::UpdateLLMQTestParametersFromArgs(const ArgsManager& args, c
UpdateLLMQTestParameters(size, threshold, llmqType);
}

void CRegTestParams::UpdateLLMQInstantSendFromArgs(const ArgsManager& args)
{
if (!args.IsArgSet("-llmqtestinstantsend")) return;

const auto& llmq_params_opt = GetLLMQ(consensus.llmqTypeInstantSend);
assert(llmq_params_opt.has_value());

std::string strLLMQType = gArgs.GetArg("-llmqtestinstantsend", std::string(llmq_params_opt->name));

Consensus::LLMQType llmqType = Consensus::LLMQType::LLMQ_NONE;
for (const auto& params : consensus.llmqs) {
if (params.name == strLLMQType) {
llmqType = params.type;
}
}
if (llmqType == Consensus::LLMQType::LLMQ_NONE) {
throw std::runtime_error("Invalid LLMQ type specified for -llmqtestinstantsend.");
}
LogPrintf("Setting llmqtestinstantsend to %ld\n", ToUnderlying(llmqType));
UpdateLLMQInstantSend(llmqType);
}

void CRegTestParams::UpdateLLMQInstantSendDIP0024FromArgs(const ArgsManager& args)
{
if (!args.IsArgSet("-llmqtestinstantsenddip0024")) return;
Expand Down Expand Up @@ -1259,31 +1214,6 @@ void CDevNetParams::UpdateDevnetLLMQChainLocksFromArgs(const ArgsManager& args)
UpdateDevnetLLMQChainLocks(llmqType);
}

void CDevNetParams::UpdateDevnetLLMQInstantSendFromArgs(const ArgsManager& args)
{
if (!args.IsArgSet("-llmqinstantsend")) return;

const auto& llmq_params_opt = GetLLMQ(consensus.llmqTypeInstantSend);
assert(llmq_params_opt.has_value());

std::string strLLMQType = gArgs.GetArg("-llmqinstantsend", std::string(llmq_params_opt->name));

Consensus::LLMQType llmqType = Consensus::LLMQType::LLMQ_NONE;
for (const auto& params : consensus.llmqs) {
if (params.name == strLLMQType) {
if (params.useRotation) {
throw std::runtime_error("LLMQ type specified for -llmqinstantsend must NOT use rotation");
}
llmqType = params.type;
}
}
if (llmqType == Consensus::LLMQType::LLMQ_NONE) {
throw std::runtime_error("Invalid LLMQ type specified for -llmqinstantsend.");
}
LogPrintf("Setting llmqinstantsend to size=%ld\n", static_cast<uint8_t>(llmqType));
UpdateDevnetLLMQInstantSend(llmqType);
}

void CDevNetParams::UpdateDevnetLLMQInstantSendDIP0024FromArgs(const ArgsManager& args)
{
if (!args.IsArgSet("-llmqinstantsenddip0024")) return;
Expand Down
2 changes: 0 additions & 2 deletions src/chainparamsbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ void SetupChainParamsBaseOptions(ArgsManager& argsman)
argsman.AddArg("-highsubsidyfactor=<n>", "The factor to multiply the normal block subsidy by while in the highsubsidyblocks window of a chain (default: 1, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqchainlocks=<quorum name>", "Override the default LLMQ type used for ChainLocks. Allows using ChainLocks with smaller LLMQs. (default: llmq_devnet, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqdevnetparams=<size>:<threshold>", "Override the default LLMQ size for the LLMQ_DEVNET quorum (default: 3:2, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqinstantsend=<quorum name>", "Override the default LLMQ type used for InstantSend. Allows using InstantSend with smaller LLMQs. (default: llmq_devnet, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqinstantsenddip0024=<quorum name>", "Override the default LLMQ type used for InstantSendDIP0024. (default: llmq_devnet_dip0024, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqplatform=<quorum name>", "Override the default LLMQ type used for Platform. (default: llmq_devnet_platform, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqmnhf=<quorum name>", "Override the default LLMQ type used for EHF. (default: llmq_devnet, devnet-only)", ArgsManager::ALLOW_ANY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqtestinstantsend=<quorum name>", "Override the default LLMQ type used for InstantSend. Used mainly to test Platform. (default: llmq_test_instantsend, regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqtestinstantsenddip0024=<quorum name>", "Override the default LLMQ type used for InstantSendDIP0024. Used mainly to test Platform. (default: llmq_test_dip0024, regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqtestinstantsendparams=<size>:<threshold>", "Override the default LLMQ size for the LLMQ_TEST_INSTANTSEND quorums (default: 3:2, regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
argsman.AddArg("-llmqtestparams=<size>:<threshold>", "Override the default LLMQ size for the LLMQ_TEST quorum (default: 3:2, regtest-only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
Expand Down
1 change: 0 additions & 1 deletion src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ struct Params {

std::vector<LLMQParams> llmqs;
LLMQType llmqTypeChainLocks;
LLMQType llmqTypeInstantSend{LLMQType::LLMQ_NONE};
LLMQType llmqTypeDIP0024InstantSend{LLMQType::LLMQ_NONE};
LLMQType llmqTypePlatform{LLMQType::LLMQ_NONE};
LLMQType llmqTypeMnhf{LLMQType::LLMQ_NONE};
Expand Down
93 changes: 34 additions & 59 deletions src/llmq/instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ static const std::string DB_ARCHIVED_BY_HASH = "is_a2";
static const std::string DB_VERSION = "is_v";

const int CInstantSendDb::CURRENT_VERSION;
const uint8_t CInstantSendLock::islock_version;
const uint8_t CInstantSendLock::isdlock_version;
const uint8_t CInstantSendLock::CURRENT_VERSION;

std::unique_ptr<CInstantSendManager> quorumInstantSendManager;

Expand Down Expand Up @@ -327,7 +326,7 @@ CInstantSendLockPtr CInstantSendDb::GetInstantSendLockByHashInternal(const uint2
return ret;
}

ret = std::make_shared<CInstantSendLock>(CInstantSendLock::isdlock_version);
ret = std::make_shared<CInstantSendLock>();
bool exists = db->Read(std::make_tuple(DB_ISLOCK_BY_HASH, hash), *ret);
if (!exists || (::SerializeHash(*ret) != hash)) {
ret = std::make_shared<CInstantSendLock>();
Expand Down Expand Up @@ -486,8 +485,7 @@ void CInstantSendManager::ProcessTx(const CTransaction& tx, bool fRetroactive, c
return;
}

if (params.llmqTypeInstantSend == Consensus::LLMQType::LLMQ_NONE ||
params.llmqTypeDIP0024InstantSend == Consensus::LLMQType::LLMQ_NONE) {
if (params.llmqTypeDIP0024InstantSend == Consensus::LLMQType::LLMQ_NONE) {
return;
}

Expand All @@ -511,7 +509,7 @@ void CInstantSendManager::ProcessTx(const CTransaction& tx, bool fRetroactive, c
// block after we retroactively locked all transactions.
if (!IsInstantSendMempoolSigningEnabled() && !fRetroactive) return;

if (!TrySignInputLocks(tx, fRetroactive, utils::GetInstantSendLLMQType(qman, WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip())), params)) {
if (!TrySignInputLocks(tx, fRetroactive, params.llmqTypeDIP0024InstantSend, params)) {
return;
}

Expand All @@ -532,8 +530,7 @@ bool CInstantSendManager::TrySignInputLocks(const CTransaction& tx, bool fRetroa

uint256 otherTxHash;
// TODO check that we didn't vote for the other IS type also
if (sigman.GetVoteForId(params.llmqTypeDIP0024InstantSend, id, otherTxHash) ||
sigman.GetVoteForId(params.llmqTypeInstantSend, id, otherTxHash)) {
if (sigman.GetVoteForId(params.llmqTypeDIP0024InstantSend, id, otherTxHash)) {
if (otherTxHash != tx.GetHash()) {
LogPrintf("CInstantSendManager::%s -- txid=%s: input %s is conflicting with previous vote for tx %s\n", __func__,
tx.GetHash().ToString(), in.prevout.ToStringShort(), otherTxHash.ToString());
Expand All @@ -543,10 +540,7 @@ bool CInstantSendManager::TrySignInputLocks(const CTransaction& tx, bool fRetroa
}

// don't even try the actual signing if any input is conflicting
if (auto llmqs = {params.llmqTypeDIP0024InstantSend, params.llmqTypeInstantSend};
ranges::any_of(llmqs, [&id, &tx, this](const auto& llmqType){
return sigman.IsConflicting(llmqType, id, tx.GetHash());})
) {
if (sigman.IsConflicting(params.llmqTypeDIP0024InstantSend, id, tx.GetHash())) {
LogPrintf("CInstantSendManager::%s -- txid=%s: sigman.IsConflicting returned true. id=%s\n", __func__,
tx.GetHash().ToString(), id.ToString());
return false;
Expand Down Expand Up @@ -641,8 +635,7 @@ void CInstantSendManager::HandleNewRecoveredSig(const CRecoveredSig& recoveredSi
return;
}

if (Params().GetConsensus().llmqTypeInstantSend == Consensus::LLMQType::LLMQ_NONE ||
Params().GetConsensus().llmqTypeDIP0024InstantSend == Consensus::LLMQType::LLMQ_NONE) {
if (Params().GetConsensus().llmqTypeDIP0024InstantSend == Consensus::LLMQType::LLMQ_NONE) {
return;
}

Expand Down Expand Up @@ -685,7 +678,7 @@ void CInstantSendManager::HandleNewInputLockRecoveredSig(const CRecoveredSig& re

void CInstantSendManager::TrySignInstantSendLock(const CTransaction& tx)
{
const auto llmqType = utils::GetInstantSendLLMQType(qman, WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip()));
const auto llmqType = Params().GetConsensus().llmqTypeDIP0024InstantSend;

for (const auto& in : tx.vin) {
auto id = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in.prevout));
Expand Down Expand Up @@ -767,7 +760,7 @@ void CInstantSendManager::ProcessMessage(const CNode& pfrom, const std::string&
}

if (msg_type == NetMsgType::ISDLOCK) {
const auto islock = std::make_shared<CInstantSendLock>(CInstantSendLock::isdlock_version);
const auto islock = std::make_shared<CInstantSendLock>();
vRecv >> *islock;
ProcessMessageInstantSendLock(pfrom, islock);
}
Expand All @@ -777,42 +770,28 @@ void CInstantSendManager::ProcessMessageInstantSendLock(const CNode& pfrom, cons
{
auto hash = ::SerializeHash(*islock);

bool fDIP0024IsActive = false;
{
LOCK(cs_main);
EraseObjectRequest(pfrom.GetId(), CInv(MSG_ISDLOCK, hash));
fDIP0024IsActive = utils::IsDIP0024Active(m_chainstate.m_chain.Tip());
}
WITH_LOCK(cs_main, EraseObjectRequest(pfrom.GetId(), CInv(MSG_ISDLOCK, hash)));

if (!islock->TriviallyValid()) {
m_peerman->Misbehaving(pfrom.GetId(), 100);
return;
}

// Deterministic ISLocks are only produced by rotation quorums, if we don't see DIP24 as active, then we won't be able to validate it anyway
if (fDIP0024IsActive) {
const auto blockIndex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(islock->cycleHash));
if (blockIndex == nullptr) {
// Maybe we don't have the block yet or maybe some peer spams invalid values for cycleHash
m_peerman->Misbehaving(pfrom.GetId(), 1);
return;
}

// Deterministic islocks MUST use rotation based llmq
auto llmqType = Params().GetConsensus().llmqTypeDIP0024InstantSend;
const auto& llmq_params_opt = GetLLMQParams(llmqType);
assert(llmq_params_opt);
if (blockIndex->nHeight % llmq_params_opt->dkgInterval != 0) {
m_peerman->Misbehaving(pfrom.GetId(), 100);
return;
}
const auto blockIndex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(islock->cycleHash));
if (blockIndex == nullptr) {
// Maybe we don't have the block yet or maybe some peer spams invalid values for cycleHash
m_peerman->Misbehaving(pfrom.GetId(), 1);
return;
}

// WE MUST STILL PROCESS OLD ISLOCKS?
// else if (utils::IsDIP0024Active(WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip()))) {
// // Ignore non-deterministic islocks once rotation is active
// return;
// }
// Deterministic islocks MUST use rotation based llmq
auto llmqType = Params().GetConsensus().llmqTypeDIP0024InstantSend;
const auto& llmq_params_opt = GetLLMQParams(llmqType);
assert(llmq_params_opt);
if (blockIndex->nHeight % llmq_params_opt->dkgInterval != 0) {
m_peerman->Misbehaving(pfrom.GetId(), 100);
return;
}

if (WITH_LOCK(cs_pendingLocks, return pendingInstantSendLocks.count(hash) || pendingNoTxInstantSendLocks.count(hash))
|| db.KnownInstantSendLock(hash)) {
Expand Down Expand Up @@ -940,20 +919,16 @@ std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPend
continue;
}

int nSignHeight{-1};
{
LOCK(cs_main);

const auto blockIndex = m_chainstate.m_blockman.LookupBlockIndex(islock->cycleHash);
if (blockIndex == nullptr) {
batchVerifier.badSources.emplace(nodeId);
continue;
}
const auto blockIndex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(islock->cycleHash));
if (blockIndex == nullptr) {
batchVerifier.badSources.emplace(nodeId);
continue;
}

const auto dkgInterval = llmq_params.dkgInterval;
if (blockIndex->nHeight + dkgInterval < m_chainstate.m_chain.Height()) {
nSignHeight = blockIndex->nHeight + dkgInterval - 1;
}
int nSignHeight{-1};
const auto dkgInterval = llmq_params.dkgInterval;
if (blockIndex->nHeight + dkgInterval < m_chainstate.m_chain.Height()) {
nSignHeight = blockIndex->nHeight + dkgInterval - 1;
}

auto quorum = llmq::CSigningManager::SelectQuorumForSigning(llmq_params, qman, id, nSignHeight, signOffset);
Expand Down Expand Up @@ -1274,7 +1249,7 @@ void CInstantSendManager::TruncateRecoveredSigsForInputs(const llmq::CInstantSen
for (const auto& in : islock.inputs) {
auto inputRequestId = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in));
WITH_LOCK(cs_inputReqests, inputRequestIds.erase(inputRequestId));
sigman.TruncateRecoveredSig(utils::GetInstantSendLLMQType(true), inputRequestId);
sigman.TruncateRecoveredSig(Params().GetConsensus().llmqTypeDIP0024InstantSend, inputRequestId);
}
}

Expand Down Expand Up @@ -1325,7 +1300,7 @@ void CInstantSendManager::HandleFullyConfirmedBlock(const CBlockIndex* pindex)

// And we don't need the recovered sig for the ISLOCK anymore, as the block in which it got mined is considered
// fully confirmed now
sigman.TruncateRecoveredSig(utils::GetInstantSendLLMQType(true), islock->GetRequestId());
sigman.TruncateRecoveredSig(Params().GetConsensus().llmqTypeDIP0024InstantSend, islock->GetRequestId());
}

db.RemoveArchivedInstantSendLocks(pindex->nHeight - 100);
Expand Down
18 changes: 5 additions & 13 deletions src/llmq/instantsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,22 @@ class CSigSharesManager;

struct CInstantSendLock
{
// This is the old format of instant send lock, it must be 0
static constexpr uint8_t islock_version{0};
// This is the new format of instant send deterministic lock, this should be incremented for new isdlock versions
static constexpr uint8_t isdlock_version{1};
static constexpr uint8_t CURRENT_VERSION{1};

uint8_t nVersion;
uint8_t nVersion{CURRENT_VERSION};
std::vector<COutPoint> inputs;
uint256 txid;
uint256 cycleHash;
CBLSLazySignature sig;

CInstantSendLock() : CInstantSendLock(isdlock_version) {}
explicit CInstantSendLock(const uint8_t desiredVersion) : nVersion(desiredVersion) {}
CInstantSendLock() = default;

SERIALIZE_METHODS(CInstantSendLock, obj)
{
if (s.GetVersion() >= ISDLOCK_PROTO_VERSION) {
READWRITE(obj.nVersion);
}
READWRITE(obj.nVersion);
READWRITE(obj.inputs);
READWRITE(obj.txid);
if (s.GetVersion() >= ISDLOCK_PROTO_VERSION) {
READWRITE(obj.cycleHash);
}
READWRITE(obj.cycleHash);
READWRITE(obj.sig);
}

Expand Down
Loading

0 comments on commit d9b67c2

Please sign in to comment.