Class SyncedUTXOSet

Thread safe version of the UTXOSet, should only be used for testing

class SyncedUTXOSet
  : UTXOSet ;

Methods

NameDescription
clear ()
findUTXO (utxo, value) Get an UTXO, does not return double spend.
getUTXOFinder () Prepare tracking double-spent transactions and return the UTXOFinder delegate
getUTXOs (pubkey) Get UTXOs from the UTXO set
length ()
opApply (dg)
peekUTXO (utxo, value)
updateUTXOCache (tx, height, commons_budget) Add all of a transaction's outputs to the Utxo set, and remove the spent outputs in the transaction from the set.
updateUTXOLock (utxo, unlock_height) Update lock height of a UTXO
add (utxo, value)
getUTXO (utxo) Get an UTXO from the UTXO set.
remove (utxo)

Example

test for get UTXOs with a node's public key

import agora.common.Amount;
import agora.consensus.data.Transaction;
import agora.consensus.data.UTXO;

import TESTNET = agora.consensus.data.genesis.Test;

KeyPair[] key_pairs = [KeyPair.random, KeyPair.random];

scope db = new ManagedDatabase(":memory:");
auto utxo_set = new UTXOSet(db);

// create the first transaction
Transaction tx1 = Transaction(
    [Input(Hash.init, 0)],
    [Output(Amount.MinFreezeAmount, key_pairs[0].address, OutputType.Freeze)]
);
utxo_set.updateUTXOCache(tx1, Height(0), TESTNET.CommonsBudgetAddress);
Hash hash1 = hashFull(tx1);
auto utxo_hash = UTXO.getHash(hash1, 0);

// test for getting UTXOs
auto utxos = utxo_set.getUTXOs(key_pairs[0].address);
assert(utxos[utxo_hash].output.address == key_pairs[0].address);

// create the second transaction
Transaction tx2 = Transaction(
    [Input(Hash.init, 0)],
    [Output(Amount(100_000 * 10_000_000L), key_pairs[0].address, OutputType.Freeze)]
);
utxo_set.updateUTXOCache(tx2, Height(0), TESTNET.CommonsBudgetAddress);

// create the third transaction
Transaction tx3 = Transaction(
    [Input(Hash.init, 0)],
    [Output(Amount.MinFreezeAmount, key_pairs[1].address, OutputType.Freeze)]
);
utxo_set.updateUTXOCache(tx3, Height(0), TESTNET.CommonsBudgetAddress);

// test for getting UTXOs for the first KeyPair
utxos = utxo_set.getUTXOs(key_pairs[0].address);
assert(utxos.length == 2);

// test for getting UTXOs for the second KeyPair
utxos = utxo_set.getUTXOs(key_pairs[1].address);
assert(utxos.length == 1);

utxos.clear();
assert(utxos.length == 0);