IntersectMBO / IntersectMBO/evolution-sdk

improvement: scriptRef size double-counted when UTxO appears in both spent and reference inputs

Open
#180 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
22
Forks
30
Avg merge
5h 29m
Merged PRs (30d)
12

Description

## Summary

`calculateReferenceScriptFee` receives `[...state.selectedUtxos, ...state.referenceInputs]`. If the same UTxO appears in both arrays (e.g. spent via `collectFrom` and also listed via `readFrom`), its `scriptRef` size is counted **twice**.

The Cardano node deduplicates using `Set.union` before scanning:

```haskell
-- eras/conway/impl/src/Cardano/Ledger/Conway/UTxO.hs
txNonDistinctRefScriptsSize utxo tx = ...
where
inputs = (tx ^. bodyTxL . referenceInputsTxBodyL)
`Set.union` (tx ^. bodyTxL . inputsTxBodyL) -- union deduplicates
```

## Impact

Only affects the edge case where the same UTxO is both spent and read. Over-charges the ref script fee for that UTxO. Safe (node accepts fees above minimum) but imprecise.

## Fix

Deduplicate by UTxO output reference before scanning:

```ts
const seen = new Set()
const deduped = [...state.selectedUtxos, ...state.referenceInputs].filter(utxo => {
const key = `${utxo.txHash}#${utxo.outputIndex}`
if (seen.has(key)) return false
seen.add(key)
return true
})
```

## Affected File

`packages/evolution/src/sdk/builders/phases/FeeCalculation.ts` — the call site that builds the utxo array passed to `calculateReferenceScriptFee`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.