Skip to content

Commit

Permalink
trickle from mempool
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed May 19, 2018
1 parent 8e38852 commit 21c7021
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/MemPool.java
Expand Up @@ -80,6 +80,17 @@ public MemPool(HashedTrie utxo_hashed_trie)
new Tickler().start();
}

public synchronized TransactionMempoolInfo getRandomPoolTransaction()
{
ArrayList<TransactionMempoolInfo> list = new ArrayList<>();
list.addAll(known_transactions.values());
if (list.size() == 0) return null;
Random rnd = new Random();

return list.get(rnd.nextInt(list.size()));

}

public synchronized List<Transaction> getTransactionsForBlock(ChainHash last_utxo, int max_size)
{
List<Transaction> block_list = new ArrayList<Transaction>();
Expand Down Expand Up @@ -240,6 +251,7 @@ public synchronized void rebuildPriorityMap(ChainHash new_utxo_root)
known_transactions.remove(h);

}
logger.log(Level.INFO, String.format("Remaining in mempool: %d", known_transactions.size()));

}

Expand Down Expand Up @@ -425,6 +437,13 @@ public void tickleBlocks(ChainHash utxo_root_hash)
}
}

private Peerage peerage = null;

public void setPeerage(Peerage peerage)
{
this.peerage = peerage;
}

public class Tickler extends Thread
{
public Tickler()
Expand All @@ -441,12 +460,26 @@ public void run()
{
synchronized(tickle_trigger)
{
tickle_trigger.wait(60000);
tickle_trigger.wait(1000);
}
if (tickle_hash != null)
{
rebuildPriorityMap(tickle_hash);
tickle_hash = null;
}
else
{
if (peerage!=null)
{
TransactionMempoolInfo info = getRandomPoolTransaction();
if (info != null)
{
peerage.broadcastTransaction(info.getTx());
}
}

}

}
catch(Throwable t)
{
Expand Down
2 changes: 2 additions & 0 deletions src/SnowBlossomNode.java
Expand Up @@ -85,6 +85,8 @@ private void loadWidgets()
mem_pool = new MemPool(utxo_hashed_trie);

peerage = new Peerage(this);
mem_pool.setPeerage(peerage);

}

private void startWidgets()
Expand Down
2 changes: 1 addition & 1 deletion src/SnowUserService.java
Expand Up @@ -223,7 +223,7 @@ public void run()
{
synchronized(tickle_trigger)
{
tickle_trigger.wait(60000);
tickle_trigger.wait(30000);
}
sendNewBlocks();
}
Expand Down

0 comments on commit 21c7021

Please sign in to comment.