diff --git a/src/Makefile.am b/src/Makefile.am index 7a8e53cc34..c84454e189 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -206,6 +206,7 @@ BITCOIN_CORE_H = \ noui.h \ outputtype.h \ pegins.h \ + policy/discount.h \ policy/feerate.h \ policy/fees.h \ policy/packages.h \ diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp index 2143bcf950..4c6a0bf6ea 100644 --- a/src/bench/rpc_blockchain.cpp +++ b/src/bench/rpc_blockchain.cpp @@ -15,7 +15,7 @@ namespace { struct TestBlockAndIndex { - const std::unique_ptr testing_setup{MakeNoLogFileContext(CBaseChainParams::MAIN)}; + std::unique_ptr testing_setup{MakeNoLogFileContext(CBaseChainParams::MAIN)}; CBlock block{}; uint256 blockHash{}; CBlockIndex blockindex{}; @@ -28,6 +28,7 @@ struct TestBlockAndIndex { stream >> block; + CBlockIndex::SetNodeContext(&(testing_setup->m_node)); blockHash = block.GetHash(); blockindex.phashBlock = &blockHash; blockindex.nBits = 403014710; diff --git a/src/chain.cpp b/src/chain.cpp index dd5510bfec..113bc1d3cd 100644 --- a/src/chain.cpp +++ b/src/chain.cpp @@ -5,6 +5,11 @@ #include #include +#include +#include + + +node::NodeContext *CBlockIndex::m_pcontext; std::string CBlockFileInfo::ToString() const { @@ -51,6 +56,27 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const { return CBlockLocator(vHave); } +void CBlockIndex::untrim() EXCLUSIVE_LOCKS_REQUIRED(::cs_main){ + AssertLockHeld(::cs_main); + if (!trimmed()) + return; + CBlockIndex tmp; + const CBlockIndex *pindexfull = untrim_to(&tmp); + assert(pindexfull!=this); + m_trimmed = false; + set_stored(); + proof = pindexfull->proof; + m_dynafed_params = pindexfull->m_dynafed_params; + m_signblock_witness = pindexfull->m_signblock_witness; + m_pcontext->chainman->m_blockman.m_dirty_blockindex.insert(this); +} + +const CBlockIndex *CBlockIndex::untrim_to(CBlockIndex *pindexNew) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main) +{ + AssertLockHeld(::cs_main); + return m_pcontext->chainman->m_blockman.m_block_tree_db->RegenerateFullIndex(this, pindexNew); +} + const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const { if (pindex == nullptr) { return nullptr; diff --git a/src/chain.h b/src/chain.h index 76ba3b1880..119b3683a3 100644 --- a/src/chain.h +++ b/src/chain.h @@ -16,6 +16,9 @@ #include +namespace node { +struct NodeContext; +} /** * Maximum amount of time that a block timestamp is allowed to exceed the * current network-adjusted time before the block will be accepted. @@ -215,26 +218,41 @@ class CBlockIndex bool m_trimmed{false}; bool m_trimmed_dynafed_block{false}; + bool m_stored_lvl{false}; friend class CBlockTreeDB; + static node::NodeContext *m_pcontext; + public: + static void SetNodeContext(node::NodeContext *context) {m_pcontext = context;}; // Irrevocably remove blocksigning and dynafed-related stuff from this // in-memory copy of the block header. - void trim() { + bool trim() { assert_untrimmed(); + if (!m_stored_lvl) { + // We can't trim in-memory data if it's not on disk yet, but we can if it's already been recovered once + return false; + } m_trimmed = true; m_trimmed_dynafed_block = !m_dynafed_params.value().IsNull(); proof = std::nullopt; m_dynafed_params = std::nullopt; m_signblock_witness = std::nullopt; + return true; } + void untrim(); + const CBlockIndex * untrim_to(CBlockIndex *pindexNew) const; + inline bool trimmed() const { return m_trimmed; } + inline void set_stored() { + m_stored_lvl = true; + } inline void assert_untrimmed() const { assert(!m_trimmed); } @@ -501,6 +519,9 @@ class CDiskBlockIndex : public CBlockIndex // For compatibility with elements 0.14 based chains if (g_signed_blocks) { + if (!ser_action.ForRead()) { + obj.assert_untrimmed(); + } if (is_dyna) { READWRITE(obj.m_dynafed_params.value()); READWRITE(obj.m_signblock_witness.value().stack); diff --git a/src/chainparams.cpp b/src/chainparams.cpp index a3ad97661c..e3f0114e76 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -223,6 +223,8 @@ class CMainParams : public CChainParams { anyonecanspend_aremine = false; enforce_pak = false; multi_data_permitted = false; + accept_discount_ct = false; + create_discount_ct = false; consensus.has_parent_chain = false; g_signed_blocks = false; g_con_elementsmode = false; @@ -361,6 +363,8 @@ class CTestNetParams : public CChainParams { anyonecanspend_aremine = false; enforce_pak = false; multi_data_permitted = false; + accept_discount_ct = false; + create_discount_ct = false; consensus.has_parent_chain = false; g_signed_blocks = false; g_con_elementsmode = false; @@ -517,6 +521,8 @@ class SigNetParams : public CChainParams { anyonecanspend_aremine = false; enforce_pak = false; multi_data_permitted = false; + accept_discount_ct = false; + create_discount_ct = false; consensus.has_parent_chain = false; g_signed_blocks = false; // lol g_con_elementsmode = false; @@ -610,6 +616,8 @@ class CRegTestParams : public CChainParams { anyonecanspend_aremine = false; enforce_pak = false; multi_data_permitted = false; + accept_discount_ct = false; + create_discount_ct = false; consensus.has_parent_chain = false; g_signed_blocks = false; g_con_elementsmode = false; @@ -887,6 +895,8 @@ class CCustomParams : public CRegTestParams { const CScript default_script(CScript() << OP_TRUE); consensus.fedpegScript = StrHexToScriptWithDefault(args.GetArg("-fedpegscript", ""), default_script); consensus.start_p2wsh_script = args.GetIntArg("-con_start_p2wsh_script", consensus.start_p2wsh_script); + create_discount_ct = args.GetBoolArg("-creatediscountct", false); + accept_discount_ct = args.GetBoolArg("-acceptdiscountct", create_discount_ct); // Calculate pegged Bitcoin asset std::vector commit = CommitToArguments(consensus, strNetworkID); @@ -1023,7 +1033,7 @@ class CLiquidTestNetParams : public CCustomParams { */ class CLiquidV1Params : public CChainParams { public: - CLiquidV1Params() + explicit CLiquidV1Params(const ArgsManager& args) { strNetworkID = "liquidv1"; @@ -1118,6 +1128,8 @@ class CLiquidV1Params : public CChainParams { enforce_pak = true; multi_data_permitted = true; + create_discount_ct = args.GetBoolArg("-creatediscountct", false); + accept_discount_ct = args.GetBoolArg("-acceptdiscountct", false); parentGenesisBlockHash = uint256S("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"); const bool parent_genesis_is_null = parentGenesisBlockHash == uint256(); @@ -1261,7 +1273,7 @@ class CLiquidV1Params : public CChainParams { */ class CLiquidV1TestParams : public CLiquidV1Params { public: - explicit CLiquidV1TestParams(const ArgsManager& args) + explicit CLiquidV1TestParams(const ArgsManager& args) : CLiquidV1Params(args) { // Our goal here is to override ONLY the things from liquidv1 that make no sense for a test chain / which are pointless and burdensome to require people to override manually. @@ -1466,6 +1478,8 @@ class CLiquidV1TestParams : public CLiquidV1Params { enforce_pak = args.GetBoolArg("-enforce_pak", enforce_pak); multi_data_permitted = args.GetBoolArg("-multi_data_permitted", multi_data_permitted); + create_discount_ct = args.GetBoolArg("-creatediscountct", create_discount_ct); + accept_discount_ct = args.GetBoolArg("-acceptdiscountct", accept_discount_ct || create_discount_ct); if (args.IsArgSet("-parentgenesisblockhash")) { parentGenesisBlockHash = uint256S(args.GetArg("-parentgenesisblockhash", "")); @@ -1557,7 +1571,7 @@ std::unique_ptr CreateChainParams(const ArgsManager& args, c } else if (chain == CBaseChainParams::REGTEST) { return std::unique_ptr(new CRegTestParams(args)); } else if (chain == CBaseChainParams::LIQUID1) { - return std::unique_ptr(new CLiquidV1Params()); + return std::unique_ptr(new CLiquidV1Params(args)); } else if (chain == CBaseChainParams::LIQUID1TEST) { return std::unique_ptr(new CLiquidV1TestParams(args)); } else if (chain == CBaseChainParams::LIQUIDTESTNET) { diff --git a/src/chainparams.h b/src/chainparams.h index c4a7e99eab..a80807a8eb 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -135,6 +135,8 @@ class CChainParams const std::string& ParentBlech32HRP() const { return parent_blech32_hrp; } bool GetEnforcePak() const { return enforce_pak; } bool GetMultiDataPermitted() const { return multi_data_permitted; } + bool GetAcceptDiscountCT() const { return accept_discount_ct; } + bool GetCreateDiscountCT() const { return create_discount_ct; } protected: CChainParams() {} @@ -167,6 +169,8 @@ class CChainParams std::string parent_blech32_hrp; bool enforce_pak; bool multi_data_permitted; + bool accept_discount_ct; + bool create_discount_ct; }; /** diff --git a/src/core_write.cpp b/src/core_write.cpp index 1771ebe0de..d9fc2a7a59 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -9,6 +9,7 @@ #include #include #include +#include // ELEMENTS #include