Ledger Engine
UTXO Ledger Engine Specification
The UTXO Ledger Engine defines the logic for maintaining an immutable history of all deposits, trades, and settlements occurring on a Participating Interface. The latest state of the engine can be used to derive the balances of all traders with assets deposited on the PI.
Both PIs and SDPs must run an instance of the engine. SDPs must validate that UTXOs included as inputs and generated as outputs in all trades emitted by the PI are the result of correctly running the UTXO engine. Thus, the core logic of the engine must be deterministic, so that given the same order of deposits and trades, the PI and all servicing SDPs reach the same ledger state.
Configuration
MUST require user to specify whether the engine is being run by a PI or an SDP
SDP Operation
MUST provide an endpoint for the SDP to send a
Trade
and call thevalidateTrade
function, responding with the result
PI Operation
MUST provide an endpoint for the PI to send a
TradeParams
and call theprepareTrade
function, responding with the resultMUST provide an endpoint for the PI to send a
Deposit
and call theprepareDeposit
function, responding with the resultMUST provide and endpoint for the PI to send a
SettlementRequest
and callprepareSettlement
MUST strictly sequence and process each
StateTransition
provided by the PI
Ledger Database
MUST store all UTXOs in a database, unhashed
MUST support querying UTXOs by any of their attributes
Functions
Note that every function defined below MUST be atomic.
validateTrade
Given Trade
:
extract
TradeParams
and pass todetermineTradeUTXOs
using the result of
determineTradeUTXOs
:hash each input UTXO for side A
hash each input UTXO for side B
hash each output UTXO for side A
hash each output UTXO for side B
extract
inputUTXOs
andoutputUTXOs
fromTrade
if hashes of input and output UTXOs generated match UTXO hashes in
Trade
, returntrue
to indicate a valid tradeif UTXOs don't match, return
false
along with anUnsignedTrade
with the correct input and output UTXOs
prepareTrade
Given TradeParams
:
call
determineTradeUTXOs
withTradeParams
using the result of
determineTradeUTXOs
:hash each input UTXO for side A
hash each input UTXO for side B
hash each output UTXO for side A
hash each output UTXO for side B
using
TradeParams
and the input and output UTXO hashes create anUnsignedTrade
append output UTXOs to the ledger
return the
UnsignedTrade
prepareSettlement
Given SettlementRequest
:
query for all unspent UTXOs with the same
asset
andtrader
to use as inputsfor all UTXOs above, generate a
SettledUTXO
to use as outputhash all input and output UTXOs
use
SettlementRequest
and UTXO hashes to createSettlementParams
return the
SettlementParams
prepareDeposit
Given Deposit
:
call
hashDeposit
using
Deposit
and the result ofhashDeposit
, create aDepositUTXO
append the
DepositUTXO
to the ledgerreturn the hash of the deposit
determineTradeUTXOs
Given TradeParams
:
determine
TradeSide
for trader that submittedOrder a
and calldetermineInputUTXOs
determine
TradeSide
for trader that submittedOrder b
and calldetermineInputUTXOs
use
TradeSide
and input UTXOs determined fromOrder a
, and trader ofOrder b
to callgenerateOutputUTXOs
use
TradeSide
and input UTXOs determined fromOrder b
, and trader ofOrder a
to calldetermineOutputUTXOs
return all input and output UTXOs
determineInputUTXOs
Given TradeSide
:
query for all unspent UTXOs with same
asset
andtrader
as specified in theTradeSide
, sorted descending by UTXO hashiterate through UTXOs from the query result and add to a list until the sum of the amounts of the selected UTXOs is greater than or equal to the
amount
in theTradeSide
return the list of selected UTXOs
generateOutputUTXOs
Given TradeSide
, list of input UTXOs, and address counterParty
:
iterate through input UTXOs and generate symmetric output UTXOs
if the sum of the amounts in input UTXOs is greater than the
amount
in theTradeSide
, then the final input UTXO will generate a second output UTXO granting the remainder to thecounterParty
Last updated