Skip to content

Commit

Permalink
threading balance load
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Jul 11, 2018
1 parent 9e84a3b commit 2a6fd0f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
@@ -1,7 +1,7 @@
git_repository(
name = "duckutil",
remote = "https://github.com/fireduck64/duckutil",
tag = "v1.0.0",
tag = "v1.0.2",
)

maven_jar(
Expand Down
76 changes: 43 additions & 33 deletions client/src/SnowBlossomClient.java
Expand Up @@ -9,9 +9,14 @@
import org.junit.Assert;
import snowblossom.proto.*;
import snowblossom.proto.UserServiceGrpc.UserServiceBlockingStub;
import java.util.concurrent.atomic.AtomicLong;
import snowblossom.proto.UserServiceGrpc.UserServiceStub;
import snowblossom.trie.proto.TrieNode;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.Callable;
import duckutil.TaskMaster;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -68,7 +73,7 @@ public static void main(String args[]) throws Exception
}
else if (command.equals("balance"))
{
client.showBalances();
client.showBalances(false);

}
else if (command.equals("getfresh"))
Expand Down Expand Up @@ -99,7 +104,7 @@ else if (command.equals("monitor"))
{
client = new SnowBlossomClient(config);
}
client.showBalances();
client.showBalances(false);

}
catch(Throwable t)
Expand Down Expand Up @@ -132,6 +137,7 @@ else if (command.equals("loadtest"))
private File wallet_path;
private Purse purse;
private Config config;
private ThreadPoolExecutor exec;

public SnowBlossomClient(Config config) throws Exception
{
Expand All @@ -143,6 +149,7 @@ public SnowBlossomClient(Config config) throws Exception
params = NetworkParams.loadFromConfig(config);
int port = config.getIntWithDefault("node_port", params.getDefaultPort());

exec = TaskMaster.getBasicExecutor(64,"client_lookup");

ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();

Expand All @@ -153,7 +160,7 @@ public SnowBlossomClient(Config config) throws Exception
{
wallet_path = new File(config.get("wallet_path"));
loadWallet();
//showBalances();
showBalances(false);
}

}
Expand Down Expand Up @@ -186,32 +193,35 @@ public void loadWallet()

}

public void showBalances()
public void showBalances(boolean print_each_address)
{
long total_confirmed = 0;
long total_unconfirmed = 0;
long total_spendable = 0;
DecimalFormat df = new DecimalFormat("0.000000");
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())
{
AddressSpecHash hash = AddressUtil.getHashForSpec(claim);
String address = AddressUtil.getAddressString(params.getAddressPrefix(), hash);
System.out.print("Address: " + address + " - ");
long value_confirmed = 0;
long value_unconfirmed = 0;
try
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;
List<TransactionBridge> bridges = getSpendable(hash);
if (bridges.size() > 0)
{
purse.markUsed(hash);
}

for(TransactionBridge b : bridges)
{

if (b.unconfirmed)
{
if (!b.spent)
Expand All @@ -229,34 +239,34 @@ public void showBalances()
}
if (!b.spent)
{
total_spendable += b.value;
total_spendable.addAndGet(b.value);
}

}

double val_conf_d = (double) value_confirmed / (double) Globals.SNOW_VALUE;
double val_unconf_d = (double) value_unconfirmed / (double) Globals.SNOW_VALUE;
System.out.println(String.format(" %s (%s pending) in %d outputs",
sb.append(String.format(" %s (%s pending) in %d outputs",
df.format(val_conf_d), df.format(val_unconf_d), bridges.size()));

total_confirmed += value_confirmed;
total_unconfirmed += value_unconfirmed;
}
catch(Throwable e)
{
logException = e;
System.out.println(e);
total_confirmed.addAndGet(value_confirmed);
total_unconfirmed.addAndGet(value_unconfirmed);
return sb.toString();
}

});

}
if (logException != null)

List<String> addr_balances = tm.getResults();
if (print_each_address)
{
System.out.println("Last exception stacktrace:");
logException.printStackTrace(System.out);
for(String s : addr_balances) System.out.println(s);

}
double total_conf_d = (double) total_confirmed / (double) Globals.SNOW_VALUE;
double total_unconf_d = (double) total_unconfirmed / (double) Globals.SNOW_VALUE;
double total_spend_d = (double) total_spendable / Globals.SNOW_VALUE_D;


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)));
}
Expand Down

0 comments on commit 2a6fd0f

Please sign in to comment.