Skip to content

Commit

Permalink
Check utxo list while the root hash is the same
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Aug 23, 2018
1 parent 2117bbc commit d01ddda
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
53 changes: 51 additions & 2 deletions client/src/GetUTXOUtil.java
Expand Up @@ -13,14 +13,63 @@

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.common.collect.ImmutableList;
import duckutil.LRUCache;

public class GetUTXOUtil
{
public static final long UTXO_ROOT_EXPIRE=500L;

private static final Logger logger = Logger.getLogger("snowblossom.client");

public static List<TransactionBridge> getSpendableValidated(AddressSpecHash addr, UserServiceBlockingStub stub, ByteString utxo_root )
// Saves cache of address,utxo_root to list of bridges, which will
// never change for that given input pair
private LRUCache<String, List<TransactionBridge> > spendable_cache = new LRUCache<>(1000);

private UserServiceBlockingStub stub;
private long utxo_root_time = 0;
private ChainHash last_utxo_root = null;

public GetUTXOUtil(UserServiceBlockingStub stub)
{
this.stub = stub;
}

public synchronized ChainHash getCurrentUtxoRootHash()
{

if (utxo_root_time + UTXO_ROOT_EXPIRE < System.currentTimeMillis())
{
last_utxo_root= new ChainHash(stub.getNodeStatus( NullRequest.newBuilder().build() ).getHeadSummary().getHeader().getUtxoRootHash());
utxo_root_time = System.currentTimeMillis();
logger.log(Level.DEBUG, "UTXO root hash: " + last_utxo_root);
}
return last_utxo_root;
}

public List<TransactionBridge> getSpendableValidated(AddressSpecHash addr)
throws ValidationException
{
ChainHash utxo_root = getCurrentUtxoRootHash();
String key = addr.toString() + "/" + utxo_root;
synchronized(spendable_cache)
{
List<TransactionBridge> lst = spendable_cache.get(key);
if (lst != null) return lst;
}

List<TransactionBridge> lst = getSpendableValidatedStatic(addr, stub, utxo_root.getBytes());

lst = ImmutableList.copyOf(lst);
synchronized(spendable_cache)
{
spendable_cache.put(key, lst);
}
return lst;

}

public static List<TransactionBridge> getSpendableValidatedStatic(AddressSpecHash addr, UserServiceBlockingStub stub, ByteString utxo_root )
throws ValidationException
{
logger.log(Level.FINE,String.format("Get Spendable (%s, %s)", addr.toString(), HexUtil.getHexString(utxo_root)));
Expand Down
10 changes: 6 additions & 4 deletions client/src/SnowBlossomClient.java
Expand Up @@ -247,6 +247,7 @@ else if (command.equals("loadtest"))
private Purse purse;
private Config config;
private ThreadPoolExecutor exec;
private GetUTXOUtil get_utxo_util;

public SnowBlossomClient(Config config) throws Exception
{
Expand All @@ -265,6 +266,8 @@ public SnowBlossomClient(Config config) throws Exception
asyncStub = UserServiceGrpc.newStub(channel);
blockingStub = UserServiceGrpc.newBlockingStub(channel);

get_utxo_util = new GetUTXOUtil(blockingStub);

if (config.isSet("wallet_path"))
{
wallet_path = new File(config.get("wallet_path"));
Expand Down Expand Up @@ -553,18 +556,17 @@ public List<TransactionBridge> getAllSpendable()
return all;
}

public ChainHash getCurrentUtxoRootHash()
/*public ChainHash getCurrentUtxoRootHash()
{
return new ChainHash(blockingStub.getNodeStatus( NullRequest.newBuilder().build() ).getHeadSummary().getHeader().getUtxoRootHash());
}
}*/

public List<TransactionBridge> getSpendable(AddressSpecHash addr)
throws ValidationException
{

List<TransactionBridge> confirmed_bridges = GetUTXOUtil.getSpendableValidated(addr, blockingStub, getCurrentUtxoRootHash().getBytes());

List<TransactionBridge> confirmed_bridges = get_utxo_util.getSpendableValidated(addr);

HashMap<String, TransactionBridge> bridge_map=new HashMap<>();
for(TransactionBridge b : confirmed_bridges)
Expand Down

0 comments on commit d01ddda

Please sign in to comment.