Skip to content

Commit

Permalink
Merge branch 'master' of github.com:snowblossomcoin/snowblossom
Browse files Browse the repository at this point in the history
  • Loading branch information
cluelessperson committed May 22, 2018
2 parents 8a592ae + 99e75b1 commit a624af5
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 22 deletions.
36 changes: 35 additions & 1 deletion src/TransactionBridge.java
Expand Up @@ -20,12 +20,15 @@
/**
* Simple class that acts as an easy way to make a transaction input
* from an output or trie node or whatever */
public class TransactionBridge
public class TransactionBridge implements Comparable<TransactionBridge>
{
public final TransactionOutput out;
public final TransactionInput in;
public final long value;

public boolean spent;
public boolean unconfirmed;

public TransactionBridge(TrieNode node)
{
Assert.assertTrue(node.getIsLeaf());
Expand Down Expand Up @@ -68,6 +71,19 @@ public TransactionBridge(TransactionOutput out, int out_idx, ChainHash txid)
.setSrcTxOutIdx( out_idx)
.build();
}
public TransactionBridge(TransactionInput in)
{
spent=true;
this.in = in;

value=-1;
this.out=null;
}

public String getKeyString()
{
return in.toString();
}

public static List<TransactionBridge> getConnections(Transaction tx)
{
Expand Down Expand Up @@ -108,6 +124,24 @@ protected TransactionBridge(AddressSpecHash hash, long value)
this.value = value;
}

public int compareTo(TransactionBridge o)
{
return in.toString().compareTo(o.in.toString());
}

public int hashCode()
{
return in.toString().hashCode();
}

public boolean equals(Object o)
{
if (o instanceof TransactionBridge)
{
TransactionBridge b = (TransactionBridge)o;
return in.equals(b.in);
}
return false;
}

}
25 changes: 19 additions & 6 deletions src/TransactionUtil.java
Expand Up @@ -26,8 +26,10 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.TreeMap;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.Random;



Expand Down Expand Up @@ -143,19 +145,30 @@ public static Transaction makeTransaction(WalletDatabase wallet,
{
needed_input += tx_out.getValue();
}

LinkedList<TransactionBridge> spendable_shuffle = new LinkedList<>();
spendable_shuffle.addAll(spendable);

Collections.shuffle(spendable_shuffle);
TreeMap<Double, TransactionBridge> spendable_map = new TreeMap<>();
Random rnd = new Random();
for(TransactionBridge br : spendable)
{
if (!br.spent)
{
double v=0.0;
if (br.unconfirmed) v=rnd.nextDouble();
else v=-rnd.nextDouble();

// Put confirmed first, so they are picked before
// anything uncomfirmed
spendable_map.put(v, br);
}
}

HashSet<AddressSpecHash> needed_claims=new HashSet<>();

LinkedList<TransactionInput> input_list = new LinkedList<>();

while((needed_input > 0) && (spendable_shuffle.size() > 0))
while((needed_input > 0) && (spendable_map.size() > 0))
{
TransactionBridge br = spendable_shuffle.pop();
TransactionBridge br = spendable_map.pollFirstEntry().getValue();
needed_input -= br.value;

input_list.add(br.in);
Expand Down
120 changes: 106 additions & 14 deletions src/client/SnowBlossomClient.java
Expand Up @@ -28,8 +28,12 @@
import snowblossom.proto.SigSpec;
import snowblossom.proto.Transaction;
import java.security.KeyPair;
import snowblossom.proto.TransactionInput;
import snowblossom.proto.WalletKeyPair;
import snowblossom.proto.WalletDatabase;
import snowblossom.proto.RequestAddress;
import snowblossom.proto.RequestTransaction;
import snowblossom.proto.TransactionHashList;

import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -38,6 +42,7 @@
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.Random;
import java.util.HashMap;
import snowblossom.Globals;
import snowblossom.SnowMerkleProof;
import snowblossom.trie.HashUtils;
Expand Down Expand Up @@ -269,7 +274,9 @@ public void genNewKey(WalletDatabase.Builder wallet_builder)

public void showBalances()
{
long total_value = 0;
long total_confirmed = 0;
long total_unconfirmed = 0;
long total_spendable = 0;
DecimalFormat df = new DecimalFormat("0.000000");

for(AddressSpec claim : wallet_database.getAddressesList())
Expand All @@ -279,19 +286,46 @@ public void showBalances()
System.out.print("Address: " + address + " - ");
List<TransactionBridge> bridges = getSpendable(hash);

long value = 0;
long value_confirmed = 0;
long value_unconfirmed = 0;
for(TransactionBridge b : bridges)
{
value += b.value;

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 += b.value;
}

}
double val_d = (double) value / (double) Globals.SNOW_VALUE;
System.out.println(String.format(" %s in %d outputs", df.format(val_d), bridges.size()));
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",
df.format(val_conf_d), df.format(val_unconf_d), bridges.size()));

total_value += value;
total_confirmed += value_confirmed;
total_unconfirmed += value_unconfirmed;

}
double total_d = (double) total_value / (double) Globals.SNOW_VALUE;
System.out.println(String.format("Total: %s", df.format(total_d)));
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;
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)));
}

public List<TransactionBridge> getAllSpendable()
Expand All @@ -312,18 +346,72 @@ public List<TransactionBridge> getSpendable(AddressSpecHash addr)
.setPrefix(addr.getBytes())
.setIncludeProof(true)
.build());
LinkedList<TransactionBridge> out_list = new LinkedList<>();

HashMap<String, TransactionBridge> bridge_map=new HashMap<>();

for(TrieNode node : reply.getAnswerList())
{
if (node.getIsLeaf())
{
TransactionBridge b = new TransactionBridge(node);

out_list.add(b);
bridge_map.put(b.getKeyString(), b);
}
}

for(ByteString tx_hash : blockingStub.getMempoolTransactionList(
RequestAddress.newBuilder().setAddressSpecHash(addr.getBytes()).build()).getTxHashesList())
{
Transaction tx = blockingStub.getTransaction(RequestTransaction.newBuilder().setTxHash(tx_hash).build());

TransactionInner inner = TransactionUtil.getInner(tx);

for(TransactionInput in : inner.getInputsList())
{
if (addr.equals(in.getSpecHash()))
{
TransactionBridge b_in = new TransactionBridge(in);
String key = b_in.getKeyString();
if (bridge_map.containsKey(key))
{
bridge_map.get(key).spent=true;
}
else
{
bridge_map.put(key, b_in);
}
}
}
for(int o=0; o<inner.getOutputsCount(); o++)
{
TransactionOutput out = inner.getOutputs(o);
if (addr.equals(out.getRecipientSpecHash()))
{
TransactionBridge b_out = new TransactionBridge(out, o, new ChainHash(tx_hash));
String key = b_out.getKeyString();
b_out.unconfirmed=true;

if (bridge_map.containsKey(key))
{
if (bridge_map.get(key).spent)
{
b_out.spent=true;
}
}
bridge_map.put(key, b_out);

}

}


}

return out_list;

LinkedList<TransactionBridge> lst = new LinkedList<>();
lst.addAll(bridge_map.values());
return lst;

}

public boolean submitTransaction(Transaction tx)
Expand All @@ -337,7 +425,11 @@ public void runLoadTest()
throws Exception
{
LinkedList<TransactionBridge> spendable = new LinkedList<>();
spendable.addAll(getAllSpendable());

for(TransactionBridge br : getAllSpendable())
{
if (!br.spent) spendable.add(br);
}
Collections.shuffle(spendable);
long min_send = 500000L;
long max_send = 5000000L;
Expand All @@ -350,7 +442,8 @@ public void runLoadTest()
//Collections.shuffle(spendable);

LinkedList<TransactionOutput> out_list = new LinkedList<>();
long needed_value = 0;
long fee = rnd.nextLong(5000);
long needed_value = fee;
for(int i=0; i< output_count; i++)
{
long value = min_send + rnd.nextLong(send_delta);
Expand All @@ -369,7 +462,6 @@ public void runLoadTest()
needed_value -= b.value;
input_list.add(b);
}
long fee = rnd.nextLong(5000);

Transaction tx = TransactionUtil.makeTransaction(wallet_database, input_list, out_list, fee);
if (tx == null)
Expand Down
10 changes: 9 additions & 1 deletion src/miner/SnowBlossomMiner.java
Expand Up @@ -121,12 +121,20 @@ public SnowBlossomMiner(Config config) throws Exception
}
}

private ManagedChannel channel;

private void subscribe()
throws Exception
{
if (channel != null)
{
channel.shutdownNow();
channel=null;
}

String host = config.get("node_host");
int port = config.getIntWithDefault("node_port", params.getDefaultPort());
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();

asyncStub = UserServiceGrpc.newStub(channel);
blockingStub = UserServiceGrpc.newBlockingStub(channel);
Expand Down

0 comments on commit a624af5

Please sign in to comment.