Skip to content

Commit 45d24b1

Browse files
committed
refactor: move VerifyChainLock and ChainLockSig between modules to untangle chain-state dependencies
chain-state should be available to validate chainlocks without using chainlock/handlers.cpp which is tightly connected to network code. On other hand, VerifyChainLock uses quorumsman, so it can't be just moved to chainlock/clsig or something like that because it will create new circular dependency over quorumsman. Though, chainlock::Chainlock and chainlock::ChainLockSig are tightly connected and there is no usages of ChainLockSig withdout Chainlock except regression tests; moving ChainLockSig to chainlock.h is fine then.
1 parent 74564da commit 45d24b1

11 files changed

Lines changed: 80 additions & 72 deletions

File tree

src/chainlock/chainlock.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,27 @@
44

55
#include <chainlock/chainlock.h>
66

7-
#include <chainlock/clsig.h>
7+
#include <chain.h>
88
#include <logging.h>
99
#include <spork.h>
1010

1111
namespace chainlock {
1212

13+
ChainLockSig::ChainLockSig() = default;
14+
ChainLockSig::~ChainLockSig() = default;
15+
16+
ChainLockSig::ChainLockSig(int32_t nHeight, const uint256& blockHash, const CBLSSignature& sig) :
17+
nHeight{nHeight},
18+
blockHash{blockHash},
19+
sig{sig}
20+
{
21+
}
22+
23+
std::string ChainLockSig::ToString() const
24+
{
25+
return strprintf("ChainLockSig(nHeight=%d, blockHash=%s)", nHeight, blockHash.ToString());
26+
}
27+
1328
Chainlocks::Chainlocks(const CSporkManager& sporkman) :
1429
m_sporks(sporkman)
1530
{

src/chainlock/chainlock.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,45 @@
55
#ifndef BITCOIN_CHAINLOCK_CHAINLOCK_H
66
#define BITCOIN_CHAINLOCK_CHAINLOCK_H
77

8-
#include <chain.h>
9-
#include <chainlock/clsig.h>
8+
#include <bls/bls.h>
109
#include <gsl/pointers.h>
10+
#include <serialize.h>
1111
#include <sync.h>
1212
#include <uint256.h>
1313

14+
class CBlockIndex;
1415
class CSporkManager;
16+
class uint256;
1517

1618
namespace chainlock {
1719

1820
//! Depth of block including transactions before it's considered safe
1921
static constexpr int32_t TX_CONFIRM_THRESHOLD{5};
2022

23+
struct ChainLockSig {
24+
private:
25+
int32_t nHeight{-1};
26+
uint256 blockHash;
27+
CBLSSignature sig;
28+
29+
public:
30+
ChainLockSig();
31+
~ChainLockSig();
32+
33+
ChainLockSig(int32_t nHeight, const uint256& blockHash, const CBLSSignature& sig);
34+
35+
[[nodiscard]] int32_t getHeight() const { return nHeight; }
36+
[[nodiscard]] const uint256& getBlockHash() const { return blockHash; }
37+
[[nodiscard]] const CBLSSignature& getSig() const { return sig; }
38+
[[nodiscard]] bool IsNull() const { return nHeight == -1 && blockHash == uint256(); }
39+
[[nodiscard]] std::string ToString() const;
40+
41+
SERIALIZE_METHODS(ChainLockSig, obj)
42+
{
43+
READWRITE(obj.nHeight, obj.blockHash, obj.sig);
44+
}
45+
};
46+
2147
class Chainlocks
2248
{
2349
private:

src/chainlock/clsig.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@
44

55
#include <chainlock/clsig.h>
66

7-
#include <tinyformat.h>
7+
#include <chainparams.h>
8+
#include <chainlock/chainlock.h>
9+
#include <llmq/quorumsman.h>
810

911
#include <string_view>
1012

1113
namespace chainlock {
1214
static constexpr std::string_view CLSIG_REQUESTID_PREFIX{"clsig"};
1315

14-
ChainLockSig::ChainLockSig() = default;
15-
ChainLockSig::~ChainLockSig() = default;
16-
17-
ChainLockSig::ChainLockSig(int32_t nHeight, const uint256& blockHash, const CBLSSignature& sig) :
18-
nHeight{nHeight},
19-
blockHash{blockHash},
20-
sig{sig}
16+
uint256 GenSigRequestId(const int32_t nHeight)
2117
{
18+
return ::SerializeHash(std::make_pair(CLSIG_REQUESTID_PREFIX, nHeight));
2219
}
2320

24-
std::string ChainLockSig::ToString() const
21+
llmq::VerifyRecSigStatus VerifyChainLock(const Consensus::Params& params, const CChain& chain,
22+
const llmq::CQuorumManager& qman, const chainlock::ChainLockSig& clsig)
2523
{
26-
return strprintf("ChainLockSig(nHeight=%d, blockHash=%s)", nHeight, blockHash.ToString());
27-
}
24+
const auto llmqType = params.llmqTypeChainLocks;
25+
const uint256 request_id = GenSigRequestId(clsig.getHeight());
2826

29-
uint256 GenSigRequestId(const int32_t nHeight)
30-
{
31-
return ::SerializeHash(std::make_pair(CLSIG_REQUESTID_PREFIX, nHeight));
27+
return llmq::VerifyRecoveredSig(llmqType, chain, qman, clsig.getHeight(), request_id, clsig.getBlockHash(),
28+
clsig.getSig());
3229
}
3330
} // namespace chainlock

src/chainlock/clsig.h

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,28 @@
55
#ifndef BITCOIN_CHAINLOCK_CLSIG_H
66
#define BITCOIN_CHAINLOCK_CLSIG_H
77

8-
#include <serialize.h>
9-
#include <uint256.h>
8+
#include <cstdint>
109

11-
#include <bls/bls.h>
10+
class CChain;
11+
class uint256;
1212

13-
#include <cstdint>
13+
namespace Consensus {
14+
struct Params;
15+
} // namespace Consensus
16+
17+
namespace llmq {
18+
class CQuorumManager;
19+
enum class VerifyRecSigStatus : uint8_t;
20+
} // namespace llmq
1421

1522
namespace chainlock {
16-
struct ChainLockSig {
17-
private:
18-
int32_t nHeight{-1};
19-
uint256 blockHash;
20-
CBLSSignature sig;
21-
22-
public:
23-
ChainLockSig();
24-
~ChainLockSig();
25-
26-
ChainLockSig(int32_t nHeight, const uint256& blockHash, const CBLSSignature& sig);
27-
28-
[[nodiscard]] int32_t getHeight() const { return nHeight; }
29-
[[nodiscard]] const uint256& getBlockHash() const { return blockHash; }
30-
[[nodiscard]] const CBLSSignature& getSig() const { return sig; }
31-
[[nodiscard]] bool IsNull() const { return nHeight == -1 && blockHash == uint256(); }
32-
[[nodiscard]] std::string ToString() const;
33-
34-
SERIALIZE_METHODS(ChainLockSig, obj)
35-
{
36-
READWRITE(obj.nHeight, obj.blockHash, obj.sig);
37-
}
38-
};
23+
struct ChainLockSig;
3924

4025
//! Generate clsig request ID with block height
4126
uint256 GenSigRequestId(const int32_t nHeight);
27+
28+
llmq::VerifyRecSigStatus VerifyChainLock(const Consensus::Params& params, const CChain& chain,
29+
const llmq::CQuorumManager& qman, const ChainLockSig& clsig);
4230
} // namespace chainlock
4331

4432
#endif // BITCOIN_CHAINLOCK_CLSIG_H

src/chainlock/handler.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
#include <chainlock/handler.h>
66

7+
#include <chain.h>
78
#include <chainlock/chainlock.h>
9+
#include <chainlock/clsig.h>
10+
#include <chainparams.h>
11+
#include <consensus/validation.h>
812
#include <instantsend/instantsend.h>
913
#include <llmq/quorumsman.h>
1014
#include <masternode/sync.h>
1115
#include <msg_result.h>
12-
#include <stats/client.h>
13-
#include <util/std23.h>
14-
15-
#include <chain.h>
16-
#include <chainparams.h>
17-
#include <consensus/validation.h>
1816
#include <node/interface_ui.h>
1917
#include <scheduler.h>
18+
#include <stats/client.h>
2019
#include <txmempool.h>
20+
#include <util/std23.h>
2121
#include <util/thread.h>
2222
#include <util/time.h>
2323
#include <validation.h>
@@ -325,13 +325,4 @@ void ChainlockHandler::Cleanup()
325325
}
326326
}
327327

328-
llmq::VerifyRecSigStatus VerifyChainLock(const Consensus::Params& params, const CChain& chain,
329-
const llmq::CQuorumManager& qman, const chainlock::ChainLockSig& clsig)
330-
{
331-
const auto llmqType = params.llmqTypeChainLocks;
332-
const uint256 request_id = chainlock::GenSigRequestId(clsig.getHeight());
333-
334-
return llmq::VerifyRecoveredSig(llmqType, chain, qman, clsig.getHeight(), request_id, clsig.getBlockHash(),
335-
clsig.getSig());
336-
}
337328
} // namespace chainlock

src/chainlock/handler.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,14 @@
2424

2525
class CBlock;
2626
class CBlockIndex;
27-
class CChain;
2827
class CMasternodeSync;
2928
class ChainstateManager;
3029
class CScheduler;
3130
class CTxMemPool;
3231
struct MessageProcessingResult;
3332

34-
namespace Consensus {
35-
struct Params;
36-
} // namespace Consensus
37-
3833
namespace llmq {
3934
class CQuorumManager;
40-
enum class VerifyRecSigStatus : uint8_t;
4135
} // namespace llmq
4236

4337
namespace chainlock {
@@ -107,9 +101,6 @@ class ChainlockHandler final : public CValidationInterface
107101
private:
108102
void Cleanup() EXCLUSIVE_LOCKS_REQUIRED(!cs);
109103
};
110-
111-
llmq::VerifyRecSigStatus VerifyChainLock(const Consensus::Params& params, const CChain& chain,
112-
const llmq::CQuorumManager& qman, const chainlock::ChainLockSig& clsig);
113104
} // namespace chainlock
114105

115106
#endif // BITCOIN_CHAINLOCK_HANDLER_H

src/chainlock/signing.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#define BITCOIN_CHAINLOCK_SIGNING_H
77

88
#include <chainlock/chainlock.h>
9-
#include <chainlock/clsig.h>
109
#include <llmq/signing.h>
1110
#include <util/time.h>
1211
#include <validationinterface.h>

src/rpc/quorums.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <active/context.h>
66
#include <active/masternode.h>
77
#include <chainlock/chainlock.h>
8+
#include <chainlock/clsig.h>
89
#include <chainlock/handler.h>
910
#include <evo/deterministicmns.h>
1011
#include <llmq/blockprocessor.h>

src/test/llmq_chainlock_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <streams.h>
99
#include <util/strencodings.h>
1010

11-
#include <chainlock/clsig.h>
11+
#include <chainlock/chainlock.h>
1212

1313
#include <boost/test/unit_test.hpp>
1414

src/test/util/llmq_tests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <streams.h>
1717
#include <uint256.h>
1818

19-
#include <chainlock/clsig.h>
19+
#include <chainlock/chainlock.h>
2020
#include <llmq/commitment.h>
2121
#include <llmq/params.h>
2222

0 commit comments

Comments
 (0)