From 3c8d3a2a7dd5f6d64600e574f8c92dd34382649b Mon Sep 17 00:00:00 2001 From: Pablo Greco Date: Wed, 15 Mar 2023 21:29:44 -0300 Subject: [PATCH 1/2] RPC: Avoid assert by keeping a flag to identify trimmed dynafed blocks --- src/chain.h | 9 +++++++++ src/rpc/blockchain.cpp | 2 +- src/txdb.cpp | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/chain.h b/src/chain.h index 74cc29fb80d..231113534ae 100644 --- a/src/chain.h +++ b/src/chain.h @@ -200,6 +200,7 @@ class CBlockIndex std::optional m_signblock_witness{}; bool m_trimmed{false}; + bool m_trimmed_dynafed_block{false}; friend class CBlockTreeDB; @@ -210,6 +211,7 @@ class CBlockIndex void trim() { assert_untrimmed(); 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; @@ -228,6 +230,13 @@ class CBlockIndex return proof.value(); } + const bool dynafed_block() const { + if (m_trimmed) { + return m_trimmed_dynafed_block; + } + return !m_dynafed_params.value().IsNull(); + } + const DynaFedParams& dynafed_params() const { assert_untrimmed(); return m_dynafed_params.value(); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 9c9ef7b5b43..899d3cc7b7e 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -248,7 +248,7 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex result.pushKV("difficulty", GetDifficulty(blockindex)); result.pushKV("chainwork", blockindex->nChainWork.GetHex()); } else { - if (blockindex->dynafed_params().IsNull()) { + if (!blockindex->dynafed_block()) { if (blockindex->trimmed()) { result.pushKV("signblock_witness_asm", ""); result.pushKV("signblock_witness_hex", ""); diff --git a/src/txdb.cpp b/src/txdb.cpp index ae85840111b..a679b0f5429 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -389,6 +389,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, } } else { pindexNew->m_trimmed = true; + pindexNew->m_trimmed_dynafed_block = !diskindex.m_dynafed_params.value().IsNull(); } pcursor->Next(); From 8f16e00ee2e3bba8874908d1d4eb16ca1f8f7e14 Mon Sep 17 00:00:00 2001 From: Pablo Greco Date: Thu, 16 Mar 2023 09:21:44 -0300 Subject: [PATCH 2/2] Avoid reloading the block before calling blockheaderToJSON, the loaded block is incomplete and makes it crash --- src/rpc/blockchain.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 899d3cc7b7e..d935292e321 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -280,13 +280,7 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails) { - UniValue result; - if (blockindex->trimmed()) { - CBlockIndex tmp = CBlockIndex(block.GetBlockHeader()); - result = blockheaderToJSON(tip, &tmp); - } else { - result = blockheaderToJSON(tip, blockindex); - } + UniValue result = blockheaderToJSON(tip, blockindex); result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));