1 <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Predicts the weight of a to-be-constructed transaction."><title>predict_weight in bdk_chain::bitcoin::blockdata::transaction - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../../../static.files/rustdoc-46132b98.css"><meta name="rustdoc-vars" data-root-path="../../../../" data-static-root-path="../../../../static.files/" data-current-crate="bdk_chain" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.0-nightly (4ba4ac612 2024-12-18)" data-channel="nightly" data-search-js="search-036cda7a.js" data-settings-js="settings-0f613d39.js" ><script src="../../../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../../../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../../../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../../../static.files/favicon-044be391.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button><a class="logo-container" href="../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt="logo"></a><h2><a href="../../../../bdk_chain/index.html">bdk_<wbr>chain</a><span class="version">0.21.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">predict_<wbr>weight</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#arguments" title="Arguments">Arguments</a></li><li><a href="#usage" title="Usage">Usage</a></li><li><a href="#notes-on-integer-overflow" title="Notes on integer overflow">Notes on integer overflow</a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In bdk_<wbr>chain::<wbr>bitcoin::<wbr>blockdata::<wbr>transaction</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../../../index.html">bdk_chain</a>::<wbr><a href="../../index.html">bitcoin</a>::<wbr><a href="../index.html">blockdata</a>::<wbr><a href="index.html">transaction</a></span><h1>Function <span class="fn">predict_weight</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"></span></div><pre class="rust item-decl"><code>pub fn predict_weight<I, O>(inputs: I, output_script_lens: O) -> <a class="struct" href="../../struct.Weight.html" title="struct bdk_chain::bitcoin::Weight">Weight</a><div class="where">where
2 I: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = <a class="struct" href="struct.InputWeightPrediction.html" title="struct bdk_chain::bitcoin::blockdata::transaction::InputWeightPrediction">InputWeightPrediction</a>>,
3 O: <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>>,</div></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Predicts the weight of a to-be-constructed transaction.</p>
4 <p>This function computes the weight of a transaction which is not fully known. All that is needed
5 is the lengths of scripts and witness elements.</p>
6 <h2 id="arguments"><a class="doc-anchor" href="#arguments">§</a>Arguments</h2>
8 <li><code>inputs</code> - an iterator which returns <code>InputWeightPrediction</code> for each input of the
9 to-be-constructed transaction.</li>
10 <li><code>output_script_lens</code> - an iterator which returns the length of <code>script_pubkey</code> of each output
11 of the to-be-constructed transaction.</li>
13 <p>Note that lengths of the scripts and witness elements must be non-serialized, IOW <em>without</em> the
14 preceding compact size. The length of preceding compact size is computed and added inside the
15 function for convenience.</p>
16 <p>If you have the transaction already constructed (except for signatures) with a dummy value for
17 fee output you can use the return value of <a href="../../struct.Transaction.html#method.script_pubkey_lens" title="method bdk_chain::bitcoin::Transaction::script_pubkey_lens"><code>Transaction::script_pubkey_lens</code></a> method directly
18 as the second argument.</p>
19 <h2 id="usage"><a class="doc-anchor" href="#usage">§</a>Usage</h2>
20 <p>When signing a transaction one doesn’t know the signature before knowing the transaction fee and
21 the transaction fee is not known before knowing the transaction size which is not known before
22 knowing the signature. This apparent dependency cycle can be broken by knowing the length of the
23 signature without knowing the contents of the signature e.g., we know all Schnorr signatures
24 are 64 bytes long.</p>
25 <p>Additionally, some protocols may require calculating the amounts before knowing various parts
26 of the transaction (assuming their length is known).</p>
27 <h2 id="notes-on-integer-overflow"><a class="doc-anchor" href="#notes-on-integer-overflow">§</a>Notes on integer overflow</h2>
28 <p>Overflows are intentionally not checked because one of the following holds:</p>
30 <li>The transaction is valid (obeys the block size limit) and the code feeds correct values to
31 this function - no overflow can happen.</li>
32 <li>The transaction will be so large it doesn’t fit in the memory - overflow will happen but
33 then the transaction will fail to construct and even if one serialized it on disk directly
34 it’d be invalid anyway so overflow doesn’t matter.</li>
35 <li>The values fed into this function are inconsistent with the actual lengths the transaction
36 will have - the code is already broken and checking overflows doesn’t help. Unfortunately
37 this probably cannot be avoided.</li>
39 </div></details></section></div></main></body></html>