Skip to content
This repository has been archived by the owner on Aug 23, 2020. It is now read-only.

Commit

Permalink
Feature: Fresh transactions to request (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
karimodm authored and GalRogozinski committed Feb 4, 2019
1 parent ca704d9 commit 95d38a2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
29 changes: 21 additions & 8 deletions src/main/java/com/iota/iri/network/TransactionRequester.java
Expand Up @@ -71,14 +71,31 @@ public void requestTransaction(Hash hash, boolean milestone) throws Exception {
transactionsToRequest.remove(hash);
milestoneTransactionsToRequest.add(hash);
} else {
if(!milestoneTransactionsToRequest.contains(hash) && !transactionsToRequestIsFull()) {
if(!milestoneTransactionsToRequest.contains(hash)) {
if (transactionsToRequestIsFull()) {
popEldestTransactionToRequest();
}
transactionsToRequest.add(hash);
}
}
}
}
}

/**
* This method removes the oldest transaction in the transactionsToRequest Set.
*
* It used when the queue capacity is reached, and new transactions would be dropped as a result.
*/
// @VisibleForTesting
void popEldestTransactionToRequest() {
Iterator<Hash> iterator = transactionsToRequest.iterator();
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
}

/**
* This method allows to check if a transaction was requested by the TransactionRequester.
*
Expand Down Expand Up @@ -111,13 +128,14 @@ public Hash transactionToRequest(boolean milestone) throws Exception {
synchronized (syncObj) {
// repeat while we have transactions that shall be requested
while (requestSet.size() != 0) {
// remove the first item in our set for further examination
// get the first item in our set for further examination
Iterator<Hash> iterator = requestSet.iterator();
hash = iterator.next();
iterator.remove();

// if we have received the transaction in the mean time ....
if (TransactionViewModel.exists(tangle, hash)) {
// we remove the transaction from the queue since we got it
iterator.remove();
// ... dump a log message ...
log.info("Removed existing tx from request list: " + hash);
messageQ.publish("rtl %s", hash);
Expand All @@ -126,11 +144,6 @@ public Hash transactionToRequest(boolean milestone) throws Exception {
continue;
}

// ... otherwise -> re-add it at the end of the set ...
//
// Note: we always have enough space since we removed the element before
requestSet.add(hash);

// ... and abort our loop to continue processing with the element we found
break;
}
Expand Down
@@ -1,8 +1,8 @@
package com.iota.iri.controllers;
package com.iota.iri.network;

import com.iota.iri.conf.MainnetConfig;
import com.iota.iri.controllers.TransactionViewModelTest;
import com.iota.iri.model.Hash;
import com.iota.iri.network.TransactionRequester;
import com.iota.iri.service.snapshot.SnapshotProvider;
import com.iota.iri.service.snapshot.impl.SnapshotProviderImpl;
import com.iota.iri.storage.Tangle;
Expand All @@ -11,6 +11,10 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -76,6 +80,48 @@ public void instance() throws Exception {

}

@Test
public void popEldestTransactionToRequest() throws Exception {
TransactionRequester txReq = new TransactionRequester(tangle, snapshotProvider, mq);
// Add some Txs to the pool and see if the method pops the eldest one
Hash eldest = TransactionViewModelTest.getRandomTransactionHash();
txReq.requestTransaction(eldest, false);
txReq.requestTransaction(TransactionViewModelTest.getRandomTransactionHash(), false);
txReq.requestTransaction(TransactionViewModelTest.getRandomTransactionHash(), false);
txReq.requestTransaction(TransactionViewModelTest.getRandomTransactionHash(), false);

txReq.popEldestTransactionToRequest();
// Check that the transaction is there no more
assertFalse(txReq.isTransactionRequested(eldest, false));
}

@Test
public void transactionRequestedFreshness() throws Exception {
// Add some Txs to the pool and see if the method pops the eldest one
List<Hash> eldest = new ArrayList<Hash>(Arrays.asList(
TransactionViewModelTest.getRandomTransactionHash(),
TransactionViewModelTest.getRandomTransactionHash(),
TransactionViewModelTest.getRandomTransactionHash()
));
TransactionRequester txReq = new TransactionRequester(tangle, snapshotProvider, mq);
int capacity = TransactionRequester.MAX_TX_REQ_QUEUE_SIZE;
//fill tips list
for (int i = 0; i < 3; i++) {
txReq.requestTransaction(eldest.get(i), false);
}
for (int i = 0; i < capacity; i++) {
Hash hash = TransactionViewModelTest.getRandomTransactionHash();
txReq.requestTransaction(hash,false);
}

//check that limit wasn't breached
assertEquals("Queue capacity breached!!", capacity, txReq.numberOfTransactionsToRequest());
// None of the eldest transactions should be in the pool
for (int i = 0; i < 3; i++) {
assertFalse("Old transaction has been requested", txReq.isTransactionRequested(eldest.get(i), false));
}
}

@Test
public void nonMilestoneCapacityLimited() throws Exception {
TransactionRequester txReq = new TransactionRequester(tangle, snapshotProvider, mq);
Expand Down

0 comments on commit 95d38a2

Please sign in to comment.