Include Orchard bundle in transaction dynamic usage

This commit is contained in:
Jack Grigg 2021-06-22 18:12:37 +01:00
parent 52e3a74a75
commit 8ad7371800
4 changed files with 23 additions and 1 deletions

View File

@ -36,6 +36,7 @@ static inline size_t RecursiveDynamicUsage(const CTransaction& tx) {
mem += memusage::DynamicUsage(tx.vJoinSplit);
mem += memusage::DynamicUsage(tx.vShieldedSpend);
mem += memusage::DynamicUsage(tx.vShieldedOutput);
mem += tx.GetOrchardBundle().RecursiveDynamicUsage();
return mem;
}
@ -50,6 +51,7 @@ static inline size_t RecursiveDynamicUsage(const CMutableTransaction& tx) {
mem += memusage::DynamicUsage(tx.vJoinSplit);
mem += memusage::DynamicUsage(tx.vShieldedSpend);
mem += memusage::DynamicUsage(tx.vShieldedOutput);
mem += tx.orchardBundle.RecursiveDynamicUsage();
return mem;
}

View File

@ -47,6 +47,10 @@ public:
return *this;
}
size_t RecursiveDynamicUsage() const {
return orchard_bundle_recursive_dynamic_usage(inner.get());
}
template<typename Stream>
void Serialize(Stream& s) const {
RustStream rs(s);

View File

@ -27,6 +27,9 @@ OrchardBundlePtr* orchard_bundle_clone(const OrchardBundlePtr* bundle);
/// Frees an Orchard bundle returned from `orchard_parse_bundle`.
void orchard_bundle_free(OrchardBundlePtr* bundle);
/// Returns the amount of dynamically-allocated memory used by this bundle.
size_t orchard_bundle_recursive_dynamic_usage(const OrchardBundlePtr* bundle);
/// Parses an authorized Orchard bundle from the given stream.
///
/// - If no error occurs, `bundle_ret` will point to a Rust-allocated Orchard bundle.

View File

@ -1,5 +1,6 @@
use std::ptr;
use std::{mem, ptr};
use libc::size_t;
use orchard::{
bundle::Authorized,
primitives::redpallas::{self, Binding, SpendAuth},
@ -32,6 +33,18 @@ pub extern "C" fn orchard_bundle_free(bundle: *mut Bundle<Authorized, Amount>) {
}
}
#[no_mangle]
pub extern "C" fn orchard_bundle_recursive_dynamic_usage(
bundle: *const Bundle<Authorized, Amount>,
) -> size_t {
unsafe { bundle.as_ref() }
// Bundles are boxed on the heap, so we count their own size as well as the size
// of `Vec`s they allocate.
.map(|bundle| mem::size_of_val(bundle) + bundle.dynamic_usage())
// If the transaction has no Orchard component, nothing is allocated for it.
.unwrap_or(0)
}
#[no_mangle]
pub extern "C" fn orchard_bundle_parse(
stream: Option<StreamObj>,