Search in sources :

Example 1 with TaskMaster

use of duckutil.TaskMaster in project snowblossom by snowblossomcoin.

the class PurseTest method testMarkRaceGen.

@Test
public void testMarkRaceGen() throws Exception {
    File empty_sub = new File(test_folder.newFolder(), "new");
    TreeMap<String, String> config_settings = new TreeMap<>();
    config_settings.put("key_pool_size", "100");
    config_settings.put("key_mode", "standard");
    ConfigMem config = new ConfigMem(config_settings);
    NetworkParams params = new NetworkParamsRegtest();
    WalletDatabase db = WalletUtil.makeNewDatabase(config, params);
    db = WalletUtil.fillKeyPool(db, empty_sub, config, params);
    Purse purse = new Purse(null, empty_sub, config, params);
    purse.maintainKeys(false);
    ThreadPoolExecutor exec = TaskMaster.getBasicExecutor(100, "test_fresh_race");
    TaskMaster<AddressSpecHash> tm = new TaskMaster<>(exec);
    for (int i = 0; i < 1000; i++) {
        tm.addTask(new Callable() {

            public AddressSpecHash call() throws Exception {
                return purse.getUnusedAddress(true, true);
            }
        });
    }
    ArrayList<AddressSpecHash> list = tm.getResults();
    Assert.assertEquals(1000, list.size());
    HashSet<AddressSpecHash> set = new HashSet<>();
    set.addAll(list);
    Assert.assertEquals(1000, set.size());
    Assert.assertEquals(1000, purse.getDB().getUsedAddressesCount());
    Assert.assertEquals(1100, purse.getDB().getAddressesCount());
    exec.shutdown();
}
Also used : Purse(snowblossom.client.Purse) TreeMap(java.util.TreeMap) ConfigMem(duckutil.ConfigMem) Callable(java.util.concurrent.Callable) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TaskMaster(duckutil.TaskMaster) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with TaskMaster

use of duckutil.TaskMaster in project snowblossom by snowblossomcoin.

the class SnowBlossomClient method showBalances.

public void showBalances(boolean print_each_address) {
    final AtomicLong total_confirmed = new AtomicLong(0);
    final AtomicLong total_unconfirmed = new AtomicLong(0L);
    final AtomicLong total_spendable = new AtomicLong(0L);
    final DecimalFormat df = new DecimalFormat("0.000000");
    Throwable logException = null;
    TaskMaster tm = new TaskMaster(exec);
    for (AddressSpec claim : purse.getDB().getAddressesList()) {
        tm.addTask(new Callable() {

            public String call() throws Exception {
                AddressSpecHash hash = AddressUtil.getHashForSpec(claim);
                String address = AddressUtil.getAddressString(params.getAddressPrefix(), hash);
                StringBuilder sb = new StringBuilder();
                sb.append("Address: " + address + " - ");
                long value_confirmed = 0;
                long value_unconfirmed = 0;
                boolean used = false;
                List<TransactionBridge> bridges = getSpendable(hash);
                if (bridges.size() > 0) {
                    used = true;
                    purse.markUsed(hash);
                }
                for (TransactionBridge b : bridges) {
                    if (b.unconfirmed) {
                        if (!b.spent) {
                            value_unconfirmed += b.value;
                        }
                    } else // confirmed
                    {
                        value_confirmed += b.value;
                        if (b.spent) {
                            value_unconfirmed -= b.value;
                        }
                    }
                    if (!b.spent) {
                        total_spendable.addAndGet(b.value);
                    }
                }
                if (purse.getDB().getUsedAddressesMap().containsKey(address)) {
                    used = true;
                }
                double val_conf_d = (double) value_confirmed / (double) Globals.SNOW_VALUE;
                double val_unconf_d = (double) value_unconfirmed / (double) Globals.SNOW_VALUE;
                sb.append(String.format(" %s (%s pending) in %d outputs", df.format(val_conf_d), df.format(val_unconf_d), bridges.size()));
                total_confirmed.addAndGet(value_confirmed);
                total_unconfirmed.addAndGet(value_unconfirmed);
                if (used) {
                    return sb.toString();
                }
                return "";
            }
        });
    }
    List<String> addr_balances = tm.getResults();
    if (print_each_address) {
        Set<String> lines = new TreeSet<String>();
        lines.addAll(addr_balances);
        for (String s : lines) {
            if (s.length() > 0) {
                System.out.println(s);
            }
        }
    }
    double total_conf_d = (double) total_confirmed.get() / (double) Globals.SNOW_VALUE;
    double total_unconf_d = (double) total_unconfirmed.get() / (double) Globals.SNOW_VALUE;
    double total_spend_d = (double) total_spendable.get() / Globals.SNOW_VALUE_D;
    System.out.println(String.format("Total: %s (%s pending) (%s spendable)", df.format(total_conf_d), df.format(total_unconf_d), df.format(total_spend_d)));
}
Also used : DecimalFormat(java.text.DecimalFormat) ByteString(com.google.protobuf.ByteString) Callable(java.util.concurrent.Callable) AtomicLong(java.util.concurrent.atomic.AtomicLong) TaskMaster(duckutil.TaskMaster)

Example 3 with TaskMaster

use of duckutil.TaskMaster in project snowblossom by snowblossomcoin.

the class PurseTest method testMarkRace.

@Test
public void testMarkRace() throws Exception {
    File empty_sub = new File(test_folder.newFolder(), "new");
    TreeMap<String, String> config_settings = new TreeMap<>();
    config_settings.put("key_pool_size", "100");
    ConfigMem config = new ConfigMem(config_settings);
    NetworkParams params = new NetworkParamsRegtest();
    WalletDatabase db = WalletUtil.makeNewDatabase(config, params);
    db = WalletUtil.fillKeyPool(db, empty_sub, config, params);
    Purse purse = new Purse(null, empty_sub, config, params);
    purse.maintainKeys(false);
    ThreadPoolExecutor exec = TaskMaster.getBasicExecutor(100, "test_fresh_race");
    TaskMaster<AddressSpecHash> tm = new TaskMaster<>(exec);
    for (int i = 0; i < 1000; i++) {
        tm.addTask(new Callable() {

            public AddressSpecHash call() throws Exception {
                return purse.getUnusedAddress(true, false);
            }
        });
    }
    ArrayList<AddressSpecHash> list = tm.getResults();
    Assert.assertEquals(1000, list.size());
    HashSet<AddressSpecHash> set = new HashSet<>();
    set.addAll(list);
    Assert.assertEquals(1000, set.size());
    Assert.assertEquals(1000, purse.getDB().getUsedAddressesCount());
    Assert.assertEquals(1000, purse.getDB().getAddressesCount());
    exec.shutdown();
}
Also used : Purse(snowblossom.client.Purse) TreeMap(java.util.TreeMap) ConfigMem(duckutil.ConfigMem) Callable(java.util.concurrent.Callable) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TaskMaster(duckutil.TaskMaster) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with TaskMaster

use of duckutil.TaskMaster in project snowblossom by snowblossomcoin.

the class SnowBlossomClient method getBalance.

public BalanceInfo getBalance() throws Exception {
    if (!maintain_keys_done) {
        maintainKeys();
    }
    TaskMaster<BalanceInfo> tm = new TaskMaster(exec);
    for (AddressSpec claim : purse.getDB().getAddressesList()) {
        tm.addTask(new Callable() {

            public BalanceInfo call() throws Exception {
                AddressSpecHash hash = AddressUtil.getHashForSpec(claim);
                BalanceInfo bi = getBalance(hash);
                return bi;
            }
        });
    }
    long total_confirmed = 0L;
    long total_unconfirmed = 0L;
    long total_spendable = 0L;
    for (BalanceInfo bi : tm.getResults()) {
        total_confirmed += bi.getConfirmed();
        total_unconfirmed += bi.getUnconfirmed();
        total_spendable += bi.getSpendable();
    }
    return BalanceInfo.newBuilder().setConfirmed(total_confirmed).setUnconfirmed(total_unconfirmed).setSpendable(total_spendable).build();
}
Also used : TaskMaster(duckutil.TaskMaster) Callable(java.util.concurrent.Callable)

Aggregations

TaskMaster (duckutil.TaskMaster)4 Callable (java.util.concurrent.Callable)4 ConfigMem (duckutil.ConfigMem)2 File (java.io.File)2 HashSet (java.util.HashSet)2 TreeMap (java.util.TreeMap)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 Test (org.junit.Test)2 Purse (snowblossom.client.Purse)2 ByteString (com.google.protobuf.ByteString)1 DecimalFormat (java.text.DecimalFormat)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1