tx: allow getting the weight and vsize of partial transactions
What changed, and why it matters
This commit fixes a bug where two helper functions for calculating transaction size/weight would incorrectly reject transactions that were still being built (for example, when a wallet is estimating fees before all inputs and outputs are added). The fix narrows where a strict serialization check is applied, so size estimates work on partial transactions again. There is no indication this is a security vulnerability or that it can be exploited.
Treat as a routine functional bugfix, not a security issue. Reviewers may want to confirm that tx_get_length() still enforces the serialization restrictions for actual serialization, which the diff shows it does by adding tx_check_args() there.
Security signals we found
Behavior change in argument validation path
Relaxation of a previously added strict check
No memory safety, cryptographic, or authorization changes visible
No disclosure language or CVE references present
Evidence from the diff
A previous commit (‘tx: prevent pre-bip144 serialization on elements, share arg checks’) moved shared argument validation into tx_check_args(), which is called by tx_get_lengths(). Because wally_tx_get_weight() and wally_tx_get_vsize() call tx_get_lengths() with no flags, the strict serialization checks in tx_check_args() were also applied to weight/vsize calculations. That caused these functions to fail for transactions with no inputs or no outputs, breaking fee estimation during transaction construction. This patch applies tx_check_args() only in tx_get_length() (the actual serialization path) and uses a lighter check in tx_get_lengths(), restoring weight/vsize computation for partial transactions. New unit tests verify weight/vsize for 0-input/0-output, 1-input/0-output, and 0-input/1-output cases.
Changed components
src/transaction.c: tx_get_lengths()src/transaction.c: tx_get_length()wally_tx_get_weight()wally_tx_get_vsize()src/ctest/test_tx.cInspect captured patch +44 / −2
### src/ctest/test_tx.c
@@ -251,6 +251,47 @@ static bool test_tx_get_witness(void)
return true;
}
+/* Test the weight and vsize of txs without inputs or outputs, as when
+ * estimating fees while building a tx */
+static bool test_tx_partial_weight(void)
+{
+ static const unsigned char txhash[WALLY_TXHASH_LEN] = { 1 };
+ static const unsigned char script[] = { 0x51 };
+ static const struct {
+ size_t num_inputs;
+ size_t num_outputs;
+ size_t weight;
+ } cases[] = {
+ { 0, 0, 40 }, /* Version, counts and locktime only */
+ { 1, 0, 204 }, /* Plus a 41 byte input */
+ { 0, 1, 80 }, /* Plus a 10 byte output */
+ };
+ size_t i, weight, vsize;
+ int ret;
+
+ for (i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
+ struct wally_tx *tx;
+ ret = wally_tx_init_alloc(2, 0, 1, 1, &tx);
+ check_ret(ret);
+ if (cases[i].num_inputs)
+ ret = wally_tx_add_raw_input(tx, txhash, sizeof(txhash), 0,
+ 0xffffffff, NULL, 0, NULL, 0);
+ if (ret == WALLY_OK && cases[i].num_outputs)
+ ret = wally_tx_add_raw_output(tx, 1000, script, sizeof(script), 0);
+ if (ret == WALLY_OK)
+ ret = wally_tx_get_weight(tx, &weight);
+ if (ret == WALLY_OK)
+ ret = wally_tx_get_vsize(tx, &vsize);
+ wally_tx_free(tx);
+ if (ret != WALLY_OK || weight != cases[i].weight ||
+ vsize != cases[i].weight / 4) {
+ printf("partial weight case %d failed\n", (int)i);
+ return false;
+ }
+ }
+ return true;
+}
+
int main(void)
{
bool tests_ok = true;
@@ -260,6 +301,7 @@ int main(void)
RUN(test_tx_parse);
RUN(test_tx_add_aliased);
RUN(test_tx_get_witness);
+ RUN(test_tx_partial_weight);
return tests_ok ? 0 : 1;
}
### src/transaction.c
@@ -1746,7 +1746,7 @@ static int tx_get_lengths(const struct wally_tx *tx,
*witness_size = 0;
*witness_count = 0;
- if (!tx_check_args(tx, &flags, is_elements))
+ if (!is_valid_tx(tx) || (flags & ~WALLY_TX_ALL_FLAGS))
return WALLY_EINVAL;
if ((flags & WALLY_TX_FLAG_USE_WITNESS) &&
@@ -1845,7 +1845,7 @@ static int tx_get_length(const struct wally_tx *tx,
if (written)
*written = 0;
- if (!written)
+ if (!written || !tx_check_args(tx, &flags, is_elements))
return WALLY_EINVAL;
ret = tx_get_lengths(tx, flags, &base_size, &witness_size,
&witness_count, is_elements);Why this scored 22/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.