use of com.iota.iri.model.Hash in project iri by iotaledger.
the class KerlTest method generateTrytesAndMultiSqueeze.
// @Test
public void generateTrytesAndMultiSqueeze() throws Exception {
System.out.println("trytes,Kerl_squeeze1,Kerl_squeeze2,Kerl_squeeze3");
for (int i = 0; i < 10000; i++) {
Hash trytes = getRandomTransactionHash();
int[] initial_value = trytes.trits();
Sponge k = SpongeFactory.create(SpongeFactory.Mode.KERL);
k.absorb(initial_value, 0, initial_value.length);
int[] hash_value = new int[Curl.HASH_LENGTH];
k.squeeze(hash_value, 0, hash_value.length);
String hash1 = Converter.trytes(hash_value);
k.squeeze(hash_value, 0, hash_value.length);
String hash2 = Converter.trytes(hash_value);
k.squeeze(hash_value, 0, hash_value.length);
String hash3 = Converter.trytes(hash_value);
System.out.println(String.format("%s,%s,%s,%s", trytes, hash1, hash2, hash3));
}
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class MilestoneViewModelTest method getMilestone.
@Test
public void getMilestone() throws Exception {
Hash milestoneHash = new Hash("ABCDEFGHIJKLMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUV99999");
MilestoneViewModel milestoneViewModel = new MilestoneViewModel(++index, milestoneHash);
assertTrue(milestoneViewModel.store(tangle));
MilestoneViewModel.clear();
MilestoneViewModel.load(tangle, index);
assertEquals(MilestoneViewModel.get(tangle, index).getHash(), milestoneHash);
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class Node method replyToRequest.
public void replyToRequest(Hash requestedHash, Neighbor neighbor) {
TransactionViewModel transactionViewModel = null;
Hash transactionPointer;
// retrieve requested transaction
if (requestedHash.equals(Hash.NULL_HASH)) {
// Random Tip Request
try {
if (transactionRequester.numberOfTransactionsToRequest() > 0 && rnd.nextDouble() < P_REPLY_RANDOM_TIP) {
neighbor.incRandomTransactionRequests();
transactionPointer = getRandomTipPointer();
transactionViewModel = TransactionViewModel.fromHash(tangle, transactionPointer);
} else {
// no tx to request, so no random tip will be sent as a reply.
return;
}
} catch (Exception e) {
log.error("Error getting random tip.", e);
}
} else {
// find requested trytes
try {
// transactionViewModel = TransactionViewModel.find(Arrays.copyOf(requestedHash.bytes(), TransactionRequester.REQUEST_HASH_SIZE));
transactionViewModel = TransactionViewModel.fromHash(tangle, new Hash(requestedHash.bytes(), 0, TransactionRequester.REQUEST_HASH_SIZE));
// log.debug("Requested Hash: " + requestedHash + " \nFound: " + transactionViewModel.getHash());
} catch (Exception e) {
log.error("Error while searching for transaction.", e);
}
}
if (transactionViewModel != null && transactionViewModel.getType() == TransactionViewModel.FILLED_SLOT) {
// send trytes back to neighbor
try {
sendPacket(sendingPacket, transactionViewModel, neighbor);
} catch (Exception e) {
log.error("Error fetching transaction to request.", e);
}
} else {
// trytes not found
if (!requestedHash.equals(Hash.NULL_HASH) && rnd.nextDouble() < P_PROPAGATE_REQUEST) {
// request is an actual transaction and missing in request queue add it.
try {
transactionRequester.requestTransaction(requestedHash, false);
} catch (Exception e) {
log.error("Error adding transaction to request.", e);
}
}
}
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class TransactionRequester method transactionToRequest.
public Hash transactionToRequest(boolean milestone) throws Exception {
final long beginningTime = System.currentTimeMillis();
Hash hash = null;
Set<Hash> requestSet;
if (milestone) {
requestSet = milestoneTransactionsToRequest;
if (requestSet.size() == 0) {
requestSet = transactionsToRequest;
}
} else {
requestSet = transactionsToRequest;
if (requestSet.size() == 0) {
requestSet = milestoneTransactionsToRequest;
}
}
synchronized (syncObj) {
while (requestSet.size() != 0) {
Iterator<Hash> iterator = requestSet.iterator();
hash = iterator.next();
iterator.remove();
if (TransactionViewModel.exists(tangle, hash)) {
log.info("Removed existing tx from request list: " + hash);
messageQ.publish("rtl %s", hash);
} else {
if (!transactionsToRequestIsFull()) {
requestSet.add(hash);
}
break;
}
}
}
if (random.nextDouble() < P_REMOVE_REQUEST && !requestSet.equals(milestoneTransactionsToRequest)) {
synchronized (syncObj) {
transactionsToRequest.remove(hash);
}
}
long now = System.currentTimeMillis();
if ((now - lastTime) > 10000L) {
lastTime = now;
// log.info("Transactions to request = {}", numberOfTransactionsToRequest() + " / " + TransactionViewModel.getNumberOfStoredTransactions() + " (" + (now - beginningTime) + " ms ). " );
}
return hash;
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class MilestoneViewModelTest method firstWithSnapshot.
@Test
public void firstWithSnapshot() throws Exception {
int first = 5;
int firstSnapshot = 6;
int next = 7;
new MilestoneViewModel(first, new Hash("FBCDEFGHIJ9LMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUV99999")).store(tangle);
MilestoneViewModel milestoneViewModelmid = new MilestoneViewModel(next, new Hash("GBCDE9GHIJKLMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUV99999"));
milestoneViewModelmid.store(tangle);
MilestoneViewModel milestoneViewModeltopSnapshot = new MilestoneViewModel(firstSnapshot, new Hash("GBCDEFGHIJKLMNOPQRSTUVWXYZ9ABCDEFGHIJKLMNOPQRSTUVWXYA9ABCDEFGHIJKLMNOPQRSTUV99999"));
milestoneViewModeltopSnapshot.store(tangle);
// assertTrue(firstSnapshot == MilestoneViewModel.firstWithSnapshot().index());
}
Aggregations