Search in sources :

Example 1 with Transaction

use of com.iota.iri.model.Transaction in project iri by iotaledger.

the class TangleTest method save.

@Test
public void save() throws Exception {
    Transaction transaction = new Transaction();
    Random r = new Random();
    int[] hash = new int[Curl.HASH_LENGTH], trits = Arrays.stream(new int[TransactionViewModel.TRINARY_SIZE]).map(i -> r.nextInt(3) - 1).toArray();
    Sponge curl = SpongeFactory.create(SpongeFactory.Mode.CURLP81);
    curl.absorb(trits, 0, trits.length);
    curl.squeeze(hash, 0, Curl.HASH_LENGTH);
    transaction.bytes = Converter.allocateBytesForTrits(trits.length);
    Converter.bytes(trits, transaction.bytes);
// assertTrue("Should be a new, unique transaction", !Tangle.instance().save(transaction).get());
}
Also used : Arrays(java.util.Arrays) RocksDBPersistenceProvider(com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider) Sponge(com.iota.iri.hash.Sponge) Set(java.util.Set) Test(org.junit.Test) Random(java.util.Random) Hash(com.iota.iri.model.Hash) SpongeFactory(com.iota.iri.hash.SpongeFactory) Converter(com.iota.iri.utils.Converter) TransactionViewModelTest.getRandomTransactionTrits(com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionTrits) Curl(com.iota.iri.hash.Curl) After(org.junit.After) Transaction(com.iota.iri.model.Transaction) TransactionViewModel(com.iota.iri.controllers.TransactionViewModel) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) Transaction(com.iota.iri.model.Transaction) Random(java.util.Random) Sponge(com.iota.iri.hash.Sponge) Test(org.junit.Test)

Example 2 with Transaction

use of com.iota.iri.model.Transaction in project iri by iotaledger.

the class FileExportProvider method update.

@Override
public boolean update(Persistable model, Indexable index, String item) throws Exception {
    if (model instanceof Transaction) {
        Transaction transaction = ((Transaction) model);
        if (item.contains("sender")) {
            Path path = Paths.get("export", String.valueOf(getFileNumber()) + ".tx");
            try (PrintWriter writer = new PrintWriter(path.toString(), "UTF-8")) {
                writer.println(index.toString());
                writer.println(Converter.trytes(trits(transaction)));
                writer.println(transaction.sender);
                if (item.equals("height")) {
                    writer.println("Height: " + String.valueOf(transaction.height));
                } else {
                    writer.println("Height: ");
                }
                writer.close();
                return true;
            } catch (UnsupportedEncodingException | FileNotFoundException e) {
                log.error("File export failed", e);
            } catch (Exception e) {
                log.error("Transaction load failed. ", e);
            }
        }
    }
    return false;
}
Also used : Path(java.nio.file.Path) Transaction(com.iota.iri.model.Transaction) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PrintWriter(java.io.PrintWriter)

Example 3 with Transaction

use of com.iota.iri.model.Transaction in project iri by iotaledger.

the class ZmqPublishProvider method update.

@Override
public boolean update(Persistable model, Indexable index, String item) throws Exception {
    if (model instanceof Transaction) {
        Transaction transaction = ((Transaction) model);
        if (item.contains("sender")) {
            TransactionViewModel transactionViewModel = new TransactionViewModel(transaction, (Hash) index);
            StringBuffer sb = new StringBuffer(600);
            try {
                sb.append("tx ");
                sb.append(transactionViewModel.getHash());
                sb.append(" ");
                sb.append(transactionViewModel.getAddressHash());
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.value()));
                sb.append(" ");
                sb.append(transactionViewModel.getObsoleteTagValue().toString().substring(0, 27));
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.getTimestamp()));
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.getCurrentIndex()));
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.lastIndex()));
                sb.append(" ");
                sb.append(transactionViewModel.getBundleHash());
                sb.append(" ");
                sb.append(transactionViewModel.getTrunkTransactionHash());
                sb.append(" ");
                sb.append(transactionViewModel.getBranchTransactionHash());
                sb.append(" ");
                sb.append(String.valueOf(transactionViewModel.getArrivalTime()));
                messageQ.publish(sb.toString());
            } catch (Exception e) {
                log.error(sb.toString());
                log.error("Error publishing to zmq.", e);
            }
            return true;
        }
    }
    return false;
}
Also used : Transaction(com.iota.iri.model.Transaction) TransactionViewModel(com.iota.iri.controllers.TransactionViewModel)

Example 4 with Transaction

use of com.iota.iri.model.Transaction in project iri by iotaledger.

the class TransactionViewModelTest method getRandomTransaction.

private Transaction getRandomTransaction(Random seed) {
    Transaction transaction = new Transaction();
    int[] trits = Arrays.stream(new int[TransactionViewModel.SIGNATURE_MESSAGE_FRAGMENT_TRINARY_SIZE]).map(i -> seed.nextInt(3) - 1).toArray();
    transaction.bytes = Converter.allocateBytesForTrits(trits.length);
    Converter.bytes(trits, 0, transaction.bytes, 0, trits.length);
    return transaction;
}
Also used : java.util(java.util) Tangle(com.iota.iri.storage.Tangle) Logger(org.slf4j.Logger) TransactionRequester(com.iota.iri.network.TransactionRequester) RocksDBPersistenceProvider(com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider) LoggerFactory(org.slf4j.LoggerFactory) Hash(com.iota.iri.model.Hash) SpongeFactory(com.iota.iri.hash.SpongeFactory) Converter(com.iota.iri.utils.Converter) Transaction(com.iota.iri.model.Transaction) org.junit(org.junit) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) Transaction(com.iota.iri.model.Transaction)

Aggregations

Transaction (com.iota.iri.model.Transaction)4 TransactionViewModel (com.iota.iri.controllers.TransactionViewModel)2 SpongeFactory (com.iota.iri.hash.SpongeFactory)2 Hash (com.iota.iri.model.Hash)2 RocksDBPersistenceProvider (com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider)2 Converter (com.iota.iri.utils.Converter)2 TemporaryFolder (org.junit.rules.TemporaryFolder)2 TransactionViewModelTest.getRandomTransactionTrits (com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionTrits)1 Curl (com.iota.iri.hash.Curl)1 Sponge (com.iota.iri.hash.Sponge)1 TransactionRequester (com.iota.iri.network.TransactionRequester)1 Tangle (com.iota.iri.storage.Tangle)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Path (java.nio.file.Path)1 java.util (java.util)1 Arrays (java.util.Arrays)1 Random (java.util.Random)1 Set (java.util.Set)1