|
| 1 | +/** |
| 2 | + * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS) |
| 3 | + * |
| 4 | + * This file is part of libbitcoin. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +#ifndef LIBBITCOIN_SYSTEM_CHAIN_OUTPOINT_HPP |
| 20 | +#define LIBBITCOIN_SYSTEM_CHAIN_OUTPOINT_HPP |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <unordered_set> |
| 24 | +#include <bitcoin/system/chain/point.hpp> |
| 25 | +#include <bitcoin/system/data/data.hpp> |
| 26 | +#include <bitcoin/system/define.hpp> |
| 27 | +#include <bitcoin/system/hash/hash.hpp> |
| 28 | +#include <bitcoin/system/stream/stream.hpp> |
| 29 | + |
| 30 | +namespace libbitcoin { |
| 31 | +namespace system { |
| 32 | +namespace chain { |
| 33 | + |
| 34 | +class BC_API outpoint |
| 35 | +{ |
| 36 | +public: |
| 37 | + DEFAULT_COPY_MOVE_DESTRUCT(outpoint); |
| 38 | + |
| 39 | + typedef std::shared_ptr<const outpoint> cptr; |
| 40 | + |
| 41 | + static constexpr size_t serialized_size() NOEXCEPT |
| 42 | + { |
| 43 | + return point::serialized_size() + sizeof(uint32_t); |
| 44 | + } |
| 45 | + |
| 46 | + /// Constructors. |
| 47 | + /// ----------------------------------------------------------------------- |
| 48 | + |
| 49 | + /// Default outpoint is an invalid null outpoint (null_hash/null_index/0). |
| 50 | + outpoint() NOEXCEPT; |
| 51 | + |
| 52 | + outpoint(chain::point&& point, uint64_t value) NOEXCEPT; |
| 53 | + outpoint(const chain::point& point, uint64_t value) NOEXCEPT; |
| 54 | + |
| 55 | + outpoint(const data_slice& data) NOEXCEPT; |
| 56 | + outpoint(stream::in::fast& stream) NOEXCEPT; |
| 57 | + outpoint(std::istream& stream) NOEXCEPT; |
| 58 | + outpoint(reader& source) NOEXCEPT; |
| 59 | + |
| 60 | + /// Operators. |
| 61 | + /// ----------------------------------------------------------------------- |
| 62 | + |
| 63 | + bool operator==(const outpoint& other) const NOEXCEPT; |
| 64 | + bool operator!=(const outpoint& other) const NOEXCEPT; |
| 65 | + |
| 66 | + /// Serialization. |
| 67 | + /// ----------------------------------------------------------------------- |
| 68 | + |
| 69 | + data_chunk to_data() const NOEXCEPT; |
| 70 | + void to_data(std::ostream& stream) const NOEXCEPT; |
| 71 | + void to_data(writer& sink) const NOEXCEPT; |
| 72 | + |
| 73 | + /// Properties. |
| 74 | + /// ----------------------------------------------------------------------- |
| 75 | + |
| 76 | + /// Native properties. |
| 77 | + bool is_valid() const NOEXCEPT; |
| 78 | + const chain::point& point() const NOEXCEPT; |
| 79 | + uint64_t value() const NOEXCEPT; |
| 80 | + |
| 81 | + /// Computed properties. |
| 82 | + bool is_null() const NOEXCEPT; |
| 83 | + |
| 84 | +protected: |
| 85 | + outpoint(stream::in::fast&& stream) NOEXCEPT; |
| 86 | + outpoint(reader&& source) NOEXCEPT; |
| 87 | + outpoint(chain::point&& point, uint64_t value, bool valid) NOEXCEPT; |
| 88 | + outpoint(const chain::point& point, uint64_t value, bool valid) NOEXCEPT; |
| 89 | + |
| 90 | +private: |
| 91 | + chain::point point_; |
| 92 | + uint64_t value_; |
| 93 | + |
| 94 | + // Cache. |
| 95 | + bool valid_; |
| 96 | +}; |
| 97 | + |
| 98 | +/// Arbitrary compare, for uniqueness sorting. |
| 99 | +bool operator<(const outpoint& left, const outpoint& right) NOEXCEPT; |
| 100 | + |
| 101 | +typedef std_vector<outpoint> outpoints; |
| 102 | + |
| 103 | +DECLARE_JSON_TAG_INVOKE(outpoint); |
| 104 | +DECLARE_JSON_TAG_INVOKE(outpoint::cptr); |
| 105 | + |
| 106 | +} // namespace chain |
| 107 | +} // namespace system |
| 108 | +} // namespace libbitcoin |
| 109 | + |
| 110 | +namespace std |
| 111 | +{ |
| 112 | +template<> |
| 113 | +struct hash<bc::system::chain::outpoint> |
| 114 | +{ |
| 115 | + size_t operator()(const bc::system::chain::outpoint& value) const NOEXCEPT |
| 116 | + { |
| 117 | + // Value does not contribute to identity. |
| 118 | + return std::hash<bc::system::chain::point>{}(value.point()); |
| 119 | + } |
| 120 | +}; |
| 121 | +} // namespace std |
| 122 | + |
| 123 | +#endif |
0 commit comments