cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

TransactionBuilder doesn't check for duplicate inputs

Open longngn opened this issue 2 years ago • 1 comments

Currently, inputs are stored as a Vec and we can add duplicate inputs without any warning or error add_inputs_from also doesn't filter existing inputs from available_inputs (https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/src/tx_builder.rs#L290) This leads to add_change_if_needed will wrongly calculate the total input value and create an unbalanced tx (https://github.com/Emurgo/cardano-serialization-lib/blob/master/rust/src/tx_builder.rs#L290)

I would propose 2 ways to solve this:

  • add_input methods and add_inputs_from can silently ignore duplicate inputs or return error
  • add_change_if_needed can dedup inputs before calculating input_total

longngn avatar Jan 13 '22 16:01 longngn

I think it's a fair assumption that supplied inputs will be unique. We could also do the check earlier in TransactionUnspentOutputs::add() or do we have any reason to have duplicate inputs anywhere else? Currently that struct is only used in the TxBuilder though since this is an input + the output data whereas the regular input structs just have the hash/index and not value/etc.

rooooooooob avatar Jan 15 '22 02:01 rooooooooob