Transaction Construction Algorithm (voting pools)

From Open Transactions
Revision as of 19:11, 22 September 2014 by Justusranvier (talk | contribs) (Split Output)
Jump to navigation Jump to search

Introduction

In order to avoid an expensive and error-prone consensus process, it is mandatory for wallets to be able to construct withdrawal transactions in a deterministic manner, assuming they are given the withdrawal requests in a deterministic order. Once this is accomplished, then it only becomes necessary for wallets to share signatures among themselves because they know they all signed the exact same bitcoin transaction.

Procedure

Overview

Transaction Construction Algorithm - Overview.png

Initial Conditions

The startwithdrawal API call has been received by the wallet, and all the arguments have been checked for errors.

Sequence

The basic flow of the procedure is:

  1. Gather accounting information from the API call arguments
  2. Validate and sort the output list
  3. Start a new transactions
  4. Add outputs to the transaction until either all outputs are added or all inputs are used.
  5. Make sure all generated transactions are ready for signing.
  6. Create a list of all signatures which the wallet is capable of generating.
  7. Create a status list containing the deposition of every output the caller specified.
  8. Return the status list and signature list to the caller.

Each step in this chart is more fully described below.


Preparation

Transaction Construction Algorithm - Preparation.png

Initial Conditions

The startwithdrawal API call has been received by the wallet, and all the arguments have been checked for errors.

Sequence

  1. The algorithm must keep track of the next change address to be used. The initial value is supplied as the changestart argument. Any time a change output is allocated, the index value of the address identifier is incremented, and if a change output is remove from a transaction the index value is decremented. The final value will be returned to the caller as the nextchangestart value in the withdrawal status list.
  2. Prepare an empty list for holding transactions as they are constructed and before they are signed.
  3. Prepare an empty array to hold the transaction signatures which will be returned to the caller as the signatures value.
  4. Prepare an withdrawal status object.
    1. roundID: copied from roundID argument
    2. nextinputstart: nil
    3. nextchangestart: nil
    4. fees: 0
    5. outputs: should contain an entry for every output passed:
      1. outBailmentID: copied from outBailmentID entry in outputs argument
      2. status: empty string
      3. transactions: nil

Output List Initialization

Transaction Construction Algorithm - Output List Initialization.png

Initial Conditions

The startwithdrawal API call has been received by the wallet, and all the arguments have been checked for errors.

Sequence

  1. Pass the inputstart, inputstop, and dustthreshold parameters to the input selection algorithm to obtain an ordered list of eligible inputs.
  2. Perform a first pass check to determine if the requested outputs exceed the size of the eligible inputs. Note that this check is only definitive in the case where the output sum is greater than the input sum. At this stage of the algorithm, the input sum exceeding the output sum does not prove that all outputs will be successfully created.
    1. Take the sum of both lists.
    2. If the sum of the outputs exceeds the sum of the inputs, remove outputs in descending size order until this is no longer true. Note this may result in the output list being an empty set.
  3. If the output list is non-empty, sort it by outBailmentID
  4. Move both the input lists and outputs lists into separate stacks by pushing them in reverse list order. The first input and first output should be on the top of their respective stacks.

Initialize New Transaction

Transaction Construction Algorithm - Initialize New Transaction.png

Initial Conditions

There are no pending transactions to in the process of being constructed, either because this is the first transaction of the algorithm or because a previous pending transaction has been moved to the finished transaction list.

The input stack contains at least one input.

The output stack contains at least one output.

Sequence

  1. Create a minimal transaction skeleton with a header and one change output.
    1. The change output will be the last output of the transaction, but the number of other outputs in this transaction is not yet known.
    2. The change output must be included in the skeleton for transaction size calculations.
    3. The position index for the change output is nil.
    4. The first non-change output for the transaction will be placed at position index 0.

After creating the transaction skeleton, and after any action which alters it, the new transaction size (bytes) and minimum required transaction fee must be recalculated.


Add Next Output

Transaction Construction Algorithm - Add Next Output.png

Initial Conditions

The output stack contains at least one output.

There is a transaction in progress which is ready to accept new outputs. This transaction is either empty or it contains 1 or more outputs.

If the transaction already contains outputs, then it also contains sufficient inputs to cover those outputs and any required transaction fees without exceeding size limits. This is because it must have already successfully passed through this procedure before.

Sequence

  1. Locate the entry for this output in the withdrawal status object by finding the corresponding outBailmentID.
  2. Validate the output address.
    1. If it is invalid, update status entry for this output to "invalid" and exit from this procedure.
    2. If it is valid, add it to the transaction.
      1. Add a new entry to the transaction array for this output:
        • nxid: nil
        • index: next unused
        • amount: copied from outputs list.
  3. Check that the transaction does not exceed size limits. If it does, follow the Oversize Transaction procedure.
  4. Compare the sum of the inputs to the sum of the inputs and required transaction fee to determine if more inputs are required.
    1. If no more inputs are required, then update the status for this output to "success".
  5. If more inputs are required, then check to see if any more are available.
    1. According to the initial conditions, the input stack can not be empty on the first run through this loop.
    2. If the input stack is empty at this point, then the output has added at least one input beyond what was needed to satisfy the prior output (if there was a prior output)
    3. If the input stack is empty, then the output can only be partially fulfilled. Follow the Split Output procedure.
  6. Add the next input from the stack, and continue the loop at step 2.

The loop will continue until either:

  • The transaction contains sufficient input value to satisfy the output plus transaction fees
  • The transaction exceeds size limits
  • There is not enough input value, but no more inputs are available

Finalize Transaction

Transaction Construction Algorithm - Finalize Transaction.png

Initial Conditions

  • An unfinished transaction is ready for processing.
    • The transaction may or may not have have a useful (non-change) output
    • The transaction is valid
      • Not too big
      • Inputs are sufficient to cover outputs and required fees
    • A change output may exist in the transaction
      • If a change output exists, it has a nil position and a nil value.

Sequence

  1. If the transaction only contains a change output and no inputs, then it may be discarded.
  2. The change amount is calculated by: Σ(txin) - ( Σ(txout) + fees )
    1. If the change amount is greater than zero:
      1. Set the value of the output
      2. Set the position of the output to the next unused value after the last non-change output.
    2. If the change amount is zero, then the change output is removed from the transaction.
  3. Calculate an ntxid for the transaction for tracking purposes.
  4. Loop through every (non-change) output in the transaction and update the withdrawal status object for the corresponding output.
    1. The withdrawal status object contain an entry for every output in the transaction, where each entry has an transactions object containing a nil ntxid, an index matching the output position index, and a matching amount.
    2. Update the nxid value from nil to the ntxid for the transaction.
  5. Add the fees from this transaction to the fees property of the withdrawal status object.
  6. Move the transaction to the finished transaction list.

Update Signatures

Transaction Construction Algorithm - Update Signatures.png

Initial Conditions

  • The finished transaction list contains zero or more finalized transactions
  • The transactions are valid in all respects except for missing signatures

Sequence

  • If the finished transaction list is empty, the rest of the procedure can be skipped.
  • Loop through each transaction.
    • Within each transaction, loop through every input.
      • Within each input, loop through every pubkey in the redeem script.
        • If the wallet has possesses the private key corresponding to the pubkey, create the appropriate signature.
          • Add the signature to the transaction.
          • Add the signature to the signature list in the appropriate position.
  • Move all transactions in the finished transaction list to the pending transaction database, where they will wait for additional signatures from other voting pool members.

Update Status

Transaction Construction Algorithm - Update Status.png

Initial Conditions

Sequence

  1. Update the status for any split outBailments which exist:
    1. Split outBailments have more than one item in their transactions array and have an existing status value of "success".
    2. Change the status for all split outBailments from "success" to "split".
  2. Update the status for any partial outBailments which exist:
    1. Partial outBailments have existing status value of "partial-".
    2. Calculate the total missing value needed to satisfy the partial outputs.
    3. The missing value of an outBailment is the originally-requested size of the outBailment minus the sum of all outputs created to satisfy it (if any exist).
      1. Obtain a list of eligible inputs from the next un-thawed series and calculate their total value.
      2. If the eligible value of the next un-thawed series is greater than the total missing value, then the next un-thawed series is the target series.
        1. If not, repeat the above process while keeping a running total of eligible value until a target series is located.
      3. Append the number of the target series to the status value for every partial series.
  3. Update the nextinputstart value.
    1. If the input stack is empty, nextinputstart is the first address identifier for the next un-thawed series.
    2. If the input stack is not empty, nextinputstart is the address identifier for the next input in the stack.

Oversize Transaction

Transaction Construction Algorithm - Oversize Transaction.png

An oversize transaction may occur while attempting to add inputs to satisfy an output which has been added to a transaction as part of the Add Next Output step.

Even with the worst case of high m and/or n values, inputs will never be large enough that a transaction can be oversized while attempting to add the first input to a transaction.

Oversize transactions will have one change output, at least one non-change output, and many inputs.

If an oversize has two or more non-change outputs, then Add Next Output was successful for the previous output. In this case, the transaction was valid before attempting to add the current output, so the current output should be removed from the current transaction and moved to the next one.

Otherwise, the transaction has become too large while adding its first non-change output. This can happen due to a series of small inputs, or a large output, or a combination of the two. In this case the outBailment can not be satisfied with a single transaction and must be split across multiple transactions.

Initial Conditions

  • The Add Next Output procedure has created a transaction that exceeds size limits, in one of the following circumstances:
    • Immediately upon adding an output to the transaction
      • The transaction contains other outputs which were added successfully. All inputs currently in the transaction are required to satisfy the previous output(s) before the output that failed.
    • After adding the first input to the transaction to satisfy an output
      • The transaction contains other outputs which were added successfully. All inputs currently in the transaction are required to satisfy the previous output(s) before the output that failed.
    • After having added at least one input to the transaction to satisfy an output without causing an oversize condition.
      • At least one of the inputs in this transaction are only required to satisfy the current output, not any previous outputs (if any exist).

Procedure

  1. Count the number of non-change outputs in the transaction.
  2. If the number exceeds one, follow the Rollback Last Output procedure.
  3. If the number is one, follow the Split Output procedure.

Rollback Last Output

Transaction Construction Algorithm - Rollback Last Output.png

This procedure is used if a transaction has exceeded the size limit, and can be brought back under the limit by removing the most-recently added output and the inputs needed to support it from the transaction

The removed inputs and outputs will be added to the beginning of a new transaction.

This procedure will remove one more input than is required to bring the transaction below size limits, and then add exactly one more. This will always result in a valid transaction because the only way to reach this procedure is by having successfully completed at least one Add Next Output cycle for the current transaction prior to adding an output or input that pushes the transaction over the size limit.

Initial Conditions

  • An oversize transaction contains one or more outputs and their supporting inputs which do not exceed transaction size limits, and one output which does not.
  • The number of inputs which are solely dedicated to satisfying the most recently-added output (not required for previous outputs) may be zero or more.

Sequence

  1. Remove the most-recently added output from the transaction and return it to the output stack.
    • This will cause the output to be the first one added to the next transaction.
  2. Remove the most-recently added input from the transaction and return it to the input stack.
  3. If the sum of input values exceeds the sum of output values by an amount greater than the required transaction fee:
    • Continue removing inputs one at a time until the sum of the input values falls below the needed output + transaction fee value.
  4. Add the next input from the stack to the transaction.
  5. Perform the Finalize Transaction procedure.
  6. Initialize a new transaction.

Split Output

Transaction Construction Algorithm - Split Output.png

This procedure is used when, either due to a large output or small inputs, the inputs required to satisfy a single output can not fit within transaction size limits.

The outBailment will be split across two or more transactions.

Initial Conditions

  • The transaction being constructed contains one change output, one non-change output, and many inputs.
  • The transaction did not exceed any size limits until the most recent input was added.

Sequence

  1. Remove the change output from the transaction.
  2. Decrement the value of the nextchangestart property of the withdrawal status object.
  3. Determine if the transaction still exceeds size limits.
    1. If yes, then remove the most-recently added input.
  4. Update the value of the output in the transaction to be the sum of the inputs minus the required transaction fee.
  5. Make a copy of the outputs object corresponding to this outBailmentID.
  6. Update the amount field of the copied output by subtracting the value determined in step 4.
  7. Push this new output to the output stack.
  8. Update the status field for this outBailmentID in the withdrawal status object to "partial-".
  9. Perform the Finalize Transaction Procedure.
  10. Initialize a new transaction.

Insufficient Inputs

Transaction Construction Algorithm - Insufficient Inputs.png