// Copyright(C) 2011 - 2020 The Litecoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef LITECOIN_MWEB_MODELS_H #define LITECOIN_MWEB_MODELS_H #include #include #include #include #include #include #include namespace MWEB { /// /// A convenience wrapper around a possibly-null extension block. /// struct Block { mw::Block::CPtr m_block; Block() = default; Block(const mw::Block::CPtr& block) : m_block(block) {} boost::optional GetTotalFee() const noexcept { return IsNull() ? boost::optional(0) : m_block->GetTotalFee(); } boost::optional GetSupplyChange() const noexcept { return IsNull() ? boost::optional(0) : m_block->GetSupplyChange(); } mw::Hash GetHash() const noexcept { return IsNull() ? mw::Hash{} : m_block->GetHeader()->GetHash(); } mw::Header::CPtr GetMWEBHeader() const noexcept { return IsNull() ? mw::Header::CPtr{nullptr} : m_block->GetHeader(); } int32_t GetHeight() const noexcept { return IsNull() ? -1 : m_block->GetHeight(); } std::vector GetSpentIDs() const { if (IsNull()) { return std::vector{}; } std::vector spent_ids; for (const Input& input : m_block->GetInputs()) { spent_ids.push_back(input.GetOutputID()); } return spent_ids; } std::vector GetOutputIDs() const { if (IsNull()) { return std::vector{}; } std::vector output_ids; for (const Output& output : m_block->GetOutputs()) { output_ids.push_back(output.GetOutputID()); } return output_ids; } std::set GetKernelIDs() const { if (IsNull()) { return std::set{}; } std::set kernel_ids; for (const Kernel& kernel : m_block->GetKernels()) { kernel_ids.insert(kernel.GetKernelID()); } return kernel_ids; } SERIALIZE_METHODS(Block, obj) { READWRITE(WrapOptionalPtr(obj.m_block)); } bool IsNull() const noexcept { return m_block == nullptr; } void SetNull() noexcept { m_block.reset(); } }; /// /// A convenience wrapper around a possibly-null MWEB transcation. /// struct Tx { mw::Transaction::CPtr m_transaction; Tx() = default; Tx(const mw::Transaction::CPtr& tx) : m_transaction(tx) {} std::set GetSpentIDs() const noexcept { if (IsNull()) { return std::set{}; } std::set spent_ids; for (const Input& input : m_transaction->GetInputs()) { spent_ids.insert(input.GetOutputID()); } return spent_ids; } std::set GetKernelIDs() const noexcept { if (IsNull()) { return std::set{}; } std::set kernel_ids; for (const Kernel& kernel : m_transaction->GetKernels()) { kernel_ids.insert(kernel.GetKernelID()); } return kernel_ids; } std::set GetOutputIDs() const noexcept { if (IsNull()) { return std::set{}; } std::set output_ids; for (const Output& output : m_transaction->GetOutputs()) { output_ids.insert(output.GetOutputID()); } return output_ids; } std::vector GetPegIns() const noexcept { if (IsNull()) { return std::vector{}; } return m_transaction->GetPegIns(); } bool HasPegOut() const noexcept { if (IsNull()) { return false; } const std::vector& kernels = m_transaction->GetKernels(); return std::any_of( kernels.cbegin(), kernels.cend(), [](const Kernel& kernel) { return kernel.HasPegOut(); } ); } std::vector GetPegOuts() const noexcept { if (IsNull()) { return std::vector{}; } return m_transaction->GetPegOuts(); } uint64_t GetMWEBWeight() const noexcept { return IsNull() ? 0 : m_transaction->CalcWeight(); } boost::optional GetFee() const noexcept { return IsNull() ? boost::optional(0) : m_transaction->GetTotalFee(); } int32_t GetLockHeight() const noexcept { return IsNull() ? 0 : m_transaction->GetLockHeight(); } bool GetOutput(const mw::Hash& output_id, Output& output) const noexcept { if (IsNull()) { return false; } for (const Output& o : m_transaction->GetOutputs()) { if (o.GetOutputID() == output_id) { output = o; return true; } } return false; } SERIALIZE_METHODS(Tx, obj) { READWRITE(WrapOptionalPtr(obj.m_transaction)); } bool IsNull() const noexcept { return m_transaction == nullptr; } void SetNull() noexcept { m_transaction.reset(); } std::string ToString() const { return IsNull() ? "" : m_transaction->Print(); } }; } // namespace MWEB #endif // LITECOIN_MWEB_MODELS_H