Skip to content

improvement: tx size includes IsValid field, Haskell omits it #179

@solidsnakedev

Description

@solidsnakedev

Summary

calculateFeeIteratively serializes the transaction using a 4-element CBOR array (body, witnessSet, isValid, auxiliaryData). The Cardano node computes the fee-relevant size using toCBORForSizeComputation, which omits IsValid and produces a 3-element array.

This results in a tx size ~1 byte larger than the node calculates, causing a fee over-estimate of ~44 lovelace (1 byte × minFeeCoefficient).

Haskell Reference

-- eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs
toCBORForSizeComputation AlonzoTx {atBody, atWits, atAuxData} =
  encodeListLen 3        -- 3 elements, no IsValid
    <> encCBOR atBody
    <> encCBOR atWits
    <> encodeNullStrictMaybe encCBOR atAuxData

Our Code

// TxBuilderImpl.ts — calculateFeeIteratively
const transaction = new Transaction.Transaction({
  body,
  witnessSet: fakeWitnessSet,
  isValid: true,     // included in CBOR, node does not count this
  auxiliaryData: ...
})
const size = yield* calculateTransactionSize(transaction)

Impact

~44 lovelace over-charge per transaction. Safe (node accepts fees above minimum) but imprecise.

Fix

Serialize only [body, witnessSet, auxiliaryData] (3-element array) for size computation, or subtract the isValid CBOR overhead (~2 bytes) from the measured size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions