Search in sources :

Example 61 with Hash

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));
    }
}
Also used : Hash(com.iota.iri.model.Hash)

Example 62 with Hash

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);
}
Also used : Hash(com.iota.iri.model.Hash) RocksDBPersistenceProviderTest(com.iota.iri.storage.rocksDB.RocksDBPersistenceProviderTest)

Example 63 with Hash

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);
            }
        }
    }
}
Also used : TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) Hash(com.iota.iri.model.Hash) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 64 with Hash

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;
}
Also used : Hash(com.iota.iri.model.Hash)

Example 65 with 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());
}
Also used : Hash(com.iota.iri.model.Hash) RocksDBPersistenceProviderTest(com.iota.iri.storage.rocksDB.RocksDBPersistenceProviderTest)

Aggregations

Hash (com.iota.iri.model.Hash)75 Test (org.junit.Test)16 TransactionViewModel (com.iota.iri.controllers.TransactionViewModel)12 RocksDBPersistenceProviderTest (com.iota.iri.storage.rocksDB.RocksDBPersistenceProviderTest)12 HttpString (io.undertow.util.HttpString)8 TransactionViewModelTest.getRandomTransactionHash (com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionHash)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Curl (com.iota.iri.hash.Curl)3 PearlDiver (com.iota.iri.hash.PearlDiver)3 TransactionRequester (com.iota.iri.network.TransactionRequester)3 Converter (com.iota.iri.utils.Converter)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 com.iota.iri (com.iota.iri)2 Configuration (com.iota.iri.conf.Configuration)2 DefaultConfSettings (com.iota.iri.conf.Configuration.DefaultConfSettings)2