2023-01-23 10:31:54 -08:00
|
|
|
// Copyright (c) 2021-2023 The Zcash developers
|
2021-06-17 11:37:04 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
|
|
|
|
|
|
|
#ifndef ZCASH_PRIMITIVES_ORCHARD_H
|
|
|
|
#define ZCASH_PRIMITIVES_ORCHARD_H
|
|
|
|
|
2021-06-17 11:22:26 -07:00
|
|
|
#include "streams.h"
|
2023-04-06 10:21:58 -07:00
|
|
|
#include "streams_rust.h"
|
2021-06-17 11:22:26 -07:00
|
|
|
|
|
|
|
#include <amount.h>
|
2023-01-23 17:07:29 -08:00
|
|
|
|
|
|
|
#include <rust/bridge.h>
|
2021-07-19 15:06:52 -07:00
|
|
|
#include <rust/orchard/wallet.h>
|
|
|
|
#include "zcash/address/orchard.hpp"
|
2021-06-17 11:37:04 -07:00
|
|
|
|
2021-07-19 18:59:21 -07:00
|
|
|
class OrchardMerkleFrontier;
|
2021-07-19 15:06:52 -07:00
|
|
|
class OrchardWallet;
|
2022-02-11 15:41:35 -08:00
|
|
|
namespace orchard { class UnauthorizedBundle; }
|
2021-06-17 11:22:26 -07:00
|
|
|
|
2021-06-17 11:37:04 -07:00
|
|
|
/**
|
2022-02-11 15:41:35 -08:00
|
|
|
* The Orchard component of an authorized transaction.
|
2021-06-17 11:37:04 -07:00
|
|
|
*/
|
|
|
|
class OrchardBundle
|
|
|
|
{
|
|
|
|
private:
|
2023-04-06 10:21:58 -07:00
|
|
|
/// An optional Orchard bundle.
|
2021-06-17 11:37:04 -07:00
|
|
|
/// Memory is allocated by Rust.
|
2023-04-06 10:21:58 -07:00
|
|
|
rust::Box<orchard_bundle::Bundle> inner;
|
2021-06-17 11:37:04 -07:00
|
|
|
|
2023-04-06 10:21:58 -07:00
|
|
|
OrchardBundle(OrchardBundlePtr* bundle) : inner(orchard_bundle::from_raw_box(bundle)) {}
|
2022-02-11 15:41:35 -08:00
|
|
|
|
2021-07-19 18:59:21 -07:00
|
|
|
friend class OrchardMerkleFrontier;
|
2021-07-19 15:06:52 -07:00
|
|
|
friend class OrchardWallet;
|
2022-02-11 15:41:35 -08:00
|
|
|
friend class orchard::UnauthorizedBundle;
|
2021-06-17 11:37:04 -07:00
|
|
|
public:
|
2023-04-06 10:21:58 -07:00
|
|
|
OrchardBundle() : inner(orchard_bundle::none()) {}
|
2021-06-17 11:37:04 -07:00
|
|
|
|
|
|
|
OrchardBundle(OrchardBundle&& bundle) : inner(std::move(bundle.inner)) {}
|
2021-06-17 11:22:26 -07:00
|
|
|
|
2021-06-17 11:37:04 -07:00
|
|
|
OrchardBundle(const OrchardBundle& bundle) :
|
2023-04-06 10:21:58 -07:00
|
|
|
inner(bundle.inner->box_clone()) {}
|
2021-06-17 11:22:26 -07:00
|
|
|
|
2021-06-17 11:37:04 -07:00
|
|
|
OrchardBundle& operator=(OrchardBundle&& bundle)
|
|
|
|
{
|
|
|
|
if (this != &bundle) {
|
|
|
|
inner = std::move(bundle.inner);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2021-06-17 11:22:26 -07:00
|
|
|
|
2021-06-17 11:37:04 -07:00
|
|
|
OrchardBundle& operator=(const OrchardBundle& bundle)
|
|
|
|
{
|
|
|
|
if (this != &bundle) {
|
2023-04-06 10:21:58 -07:00
|
|
|
inner = bundle.inner->box_clone();
|
2021-06-17 11:37:04 -07:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:21:58 -07:00
|
|
|
const rust::Box<orchard_bundle::Bundle>& GetDetails() const {
|
|
|
|
return inner;
|
2022-06-03 19:14:59 -07:00
|
|
|
}
|
|
|
|
|
2021-06-22 10:12:37 -07:00
|
|
|
size_t RecursiveDynamicUsage() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
return inner->recursive_dynamic_usage();
|
2021-06-22 10:12:37 -07:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:37:04 -07:00
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
2023-04-06 10:21:58 -07:00
|
|
|
try {
|
|
|
|
inner->serialize(*ToRustStream(s));
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
throw std::ios_base::failure(e.what());
|
2021-06-17 11:37:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
2023-04-06 10:21:58 -07:00
|
|
|
try {
|
|
|
|
inner = orchard_bundle::parse(*ToRustStream(s));
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
throw std::ios_base::failure(e.what());
|
2021-06-17 11:37:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 05:05:58 -07:00
|
|
|
/// Returns true if this contains an Orchard bundle, or false if there is no
|
|
|
|
/// Orchard component.
|
2023-04-06 10:21:58 -07:00
|
|
|
bool IsPresent() const { return inner->is_present(); }
|
2021-06-17 05:05:58 -07:00
|
|
|
|
2021-06-15 03:45:24 -07:00
|
|
|
/// Returns the net value entering or exiting the Orchard pool as a result of this
|
|
|
|
/// bundle.
|
|
|
|
CAmount GetValueBalance() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
return inner->value_balance_zat();
|
2021-06-15 03:45:24 -07:00
|
|
|
}
|
|
|
|
|
2022-06-23 13:51:22 -07:00
|
|
|
/// Queues this bundle's authorization for validation.
|
2021-06-21 05:36:44 -07:00
|
|
|
///
|
2022-06-23 13:51:22 -07:00
|
|
|
/// `sighash` must be for the transaction this bundle is within.
|
|
|
|
void QueueAuthValidation(
|
2023-04-06 10:21:58 -07:00
|
|
|
orchard::BatchValidator& batch, const uint256& sighash) const
|
2021-06-17 11:37:04 -07:00
|
|
|
{
|
2023-04-06 10:21:58 -07:00
|
|
|
batch.add_bundle(inner->box_clone(), sighash.GetRawBytes());
|
2021-06-17 11:37:04 -07:00
|
|
|
}
|
2021-06-17 11:22:26 -07:00
|
|
|
|
2021-06-29 12:52:45 -07:00
|
|
|
const size_t GetNumActions() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
return inner->num_actions();
|
2021-06-29 12:52:45 -07:00
|
|
|
}
|
|
|
|
|
2021-06-17 11:22:26 -07:00
|
|
|
const std::vector<uint256> GetNullifiers() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
const auto actions = inner->actions();
|
|
|
|
std::vector<uint256> result;
|
|
|
|
result.reserve(actions.size());
|
|
|
|
for (const auto& action : actions) {
|
|
|
|
result.push_back(uint256::FromRawBytes(action.nullifier()));
|
|
|
|
}
|
2021-06-17 11:22:26 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-22 06:34:13 -07:00
|
|
|
const std::optional<uint256> GetAnchor() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
if (IsPresent()) {
|
|
|
|
return uint256::FromRawBytes(inner->anchor());
|
2021-06-17 11:22:26 -07:00
|
|
|
} else {
|
2021-06-22 06:34:13 -07:00
|
|
|
return std::nullopt;
|
2021-06-17 11:22:26 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-25 05:32:59 -07:00
|
|
|
|
|
|
|
bool OutputsEnabled() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
return inner->enable_outputs();
|
2021-06-25 05:32:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpendsEnabled() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
return inner->enable_spends();
|
2021-06-25 05:32:59 -07:00
|
|
|
}
|
2021-06-30 12:23:56 -07:00
|
|
|
|
|
|
|
bool CoinbaseOutputsAreValid() const {
|
2023-04-06 10:21:58 -07:00
|
|
|
return inner->coinbase_outputs_are_valid();
|
2021-06-30 12:23:56 -07:00
|
|
|
}
|
2021-06-17 11:37:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZCASH_PRIMITIVES_ORCHARD_H
|
|
|
|
|