Search in sources :

Example 1 with PearlDiver

use of com.iota.iri.hash.PearlDiver in project iri by iotaledger.

the class API method attachToTangleStatement.

public synchronized List<String> attachToTangleStatement(final Hash trunkTransaction, final Hash branchTransaction, final int minWeightMagnitude, final List<String> trytes) {
    final List<TransactionViewModel> transactionViewModels = new LinkedList<>();
    Hash prevTransaction = null;
    pearlDiver = new PearlDiver();
    int[] transactionTrits = Converter.allocateTritsForTrytes(TRYTES_SIZE);
    for (final String tryte : trytes) {
        long startTime = System.nanoTime();
        long timestamp = System.currentTimeMillis();
        try {
            Converter.trits(tryte, transactionTrits, 0);
            // branch and trunk
            System.arraycopy((prevTransaction == null ? trunkTransaction : prevTransaction).trits(), 0, transactionTrits, TransactionViewModel.TRUNK_TRANSACTION_TRINARY_OFFSET, TransactionViewModel.TRUNK_TRANSACTION_TRINARY_SIZE);
            System.arraycopy((prevTransaction == null ? branchTransaction : trunkTransaction).trits(), 0, transactionTrits, TransactionViewModel.BRANCH_TRANSACTION_TRINARY_OFFSET, TransactionViewModel.BRANCH_TRANSACTION_TRINARY_SIZE);
            // tag - copy the obsolete tag to the attachment tag field only if tag isn't set.
            if (Arrays.stream(transactionTrits, TransactionViewModel.TAG_TRINARY_OFFSET, TransactionViewModel.TAG_TRINARY_OFFSET + TransactionViewModel.TAG_TRINARY_SIZE).allMatch(s -> s == 0)) {
                System.arraycopy(transactionTrits, TransactionViewModel.OBSOLETE_TAG_TRINARY_OFFSET, transactionTrits, TransactionViewModel.TAG_TRINARY_OFFSET, TransactionViewModel.TAG_TRINARY_SIZE);
            }
            Converter.copyTrits(timestamp, transactionTrits, TransactionViewModel.ATTACHMENT_TIMESTAMP_TRINARY_OFFSET, TransactionViewModel.ATTACHMENT_TIMESTAMP_TRINARY_SIZE);
            Converter.copyTrits(0, transactionTrits, TransactionViewModel.ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_OFFSET, TransactionViewModel.ATTACHMENT_TIMESTAMP_LOWER_BOUND_TRINARY_SIZE);
            Converter.copyTrits(MAX_TIMESTAMP_VALUE, transactionTrits, TransactionViewModel.ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_OFFSET, TransactionViewModel.ATTACHMENT_TIMESTAMP_UPPER_BOUND_TRINARY_SIZE);
            if (!pearlDiver.search(transactionTrits, minWeightMagnitude, 0)) {
                transactionViewModels.clear();
                break;
            }
            // validate PoW - throws exception if invalid
            final TransactionViewModel transactionViewModel = instance.transactionValidator.validate(transactionTrits, instance.transactionValidator.getMinWeightMagnitude());
            transactionViewModels.add(transactionViewModel);
            prevTransaction = transactionViewModel.getHash();
        } finally {
            API.incEllapsedTime_PoW(System.nanoTime() - startTime);
            API.incCounter_PoW();
            if ((API.getCounter_PoW() % 100) == 0) {
                String sb = "Last 100 PoW consumed " + API.getEllapsedTime_PoW() / 1000000000L + " seconds processing time.";
                log.info(sb);
                counter_PoW = 0;
                ellapsedTime_PoW = 0L;
            }
        }
    }
    final List<String> elements = new LinkedList<>();
    for (int i = transactionViewModels.size(); i-- > 0; ) {
        elements.add(Converter.trytes(transactionViewModels.get(i).trits()));
    }
    return elements;
}
Also used : HttpString(io.undertow.util.HttpString) Hash(com.iota.iri.model.Hash) LinkedList(java.util.LinkedList) PearlDiver(com.iota.iri.hash.PearlDiver)

Aggregations

PearlDiver (com.iota.iri.hash.PearlDiver)1 Hash (com.iota.iri.model.Hash)1 HttpString (io.undertow.util.HttpString)1 LinkedList (java.util.LinkedList)1