omgnetwork/elixir-omg

Optimise usage of UTXOs in transaction.create

Closed this issue · 5 comments

In line with the objective of reducing the amount of UTXOs on the network, implement logic in /transaction.create to select the maximum number of UTXOs as transaction inputs.

Planning to implement following logic:

  • Start with the derivation of minimum inputs required for a transaction.
  • Continue with selection of extra inputs as per specification below.

Desired behaviour in selection of extra UTXOs

Case Desired Outcome Tests
Available UTXOs cannot cover amount (incl. fee) Error message 1
Available UTXOs can cover amount (incl. fee) but merge required Error Message 1
U = 4 and payable amounts are covered No action required 1
U = 3 (single currency); input amounts match corresponding currency payables exactly Add a fourth input for code simplicity even though no actual merge will take place 1
U = 3 (multi-currency); input amounts match corresponding currency payables exactly Add a fourth input for code simplicity even though no actual merge will take place 1
U < 3 (single currency); input amounts match corresponding currency payables exactly If 2 or more UTXOs of the same currency available, stealth merge. If not, add inputs for which no actual merge will take place 2
U < 3 (multi currency); input amounts match corresponding currency payables exactly If 2 or more UTXOs of the same currency available, stealth merge. If not, add inputs for which no actual merge will take place 2
U < 4; input amounts exceed corresponding currency payables Stealth merge if 1 or more UTXOs available on a currency where we expect "change" ?

U denotes the minimum number of UTXOs to cover transfer and fee amounts

  • U = (number of currencies in transaction) x (amount required to cover each currency amount)
  • U ≤ 4

Current UTXO selection flow:

Step To Do Tested
1. get_sorted_grouped_utxos: fetch UTXOs from database and order them by currency and amount.
2. needed_funds: generates required funds per currency (incl. fee)
3. select_utxos: if no input is matching the payable exactly, collects inputs in descending order (starting from the largest amount) until the payment is covered or until the spender runs out of possible inputs
4. funds_sufficient?: checks if the inputs selected by select_utxos are sufficient to cover the transaction
5. if number of inputs exceeds 4 -> create_merge - Remove this and return an error.
6. if number of inputs is 4 or less -> create_transaction - Put in new module

☝️ These analyses are pure gold. Thanks!

Pseudo Code

stealth_merge(utxos_currently_in_tx, user_utxos_ordered_by_currency_with_largest_number_of_utxos) do
   base_case: = maximum_inputs
   else
        - take first item in list of first currency and add it to current utxos/remove from user utxos
   stealth_merge(new_current_utxos, new_user_utxos)
end