cardano-serialization-lib
cardano-serialization-lib copied to clipboard
TransactionBuilder doesn't check for duplicate inputs
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 andadd_inputs_from
can silently ignore duplicate inputs or return error -
add_change_if_needed
can dedup inputs before calculatinginput_total
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.