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();
}
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)));
}
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();
}
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();
}
Aggregations