@Override
        public void run() {

          try {
            try {
              List<Transaction> transactionList = new ArrayList<>();
              int curTime = Convert.getEpochTime();
              for (TransactionImpl transaction : nonBroadcastedTransactions.values()) {
                if (TransactionDb.hasTransaction(transaction.getId())
                    || transaction.getExpiration() < curTime) {
                  nonBroadcastedTransactions.remove(transaction.getId());
                } else if (transaction.getTimestamp() < curTime - 30) {
                  transactionList.add(transaction);
                }
              }

              if (transactionList.size() > 0) {
                Peers.sendToSomePeers(transactionList);
              }

            } catch (Exception e) {
              Logger.logDebugMessage("Error in transaction re-broadcasting thread", e);
            }
          } catch (Throwable t) {
            Logger.logMessage("CRITICAL ERROR. PLEASE REPORT TO THE DEVELOPERS.\n" + t.toString());
            t.printStackTrace();
            System.exit(1);
          }
        }
 @Override
 public Transaction newTransaction(
     short deadline,
     byte[] senderPublicKey,
     Long recipientId,
     long amountNQT,
     long feeNQT,
     String referencedTransactionFullHash,
     Attachment attachment)
     throws NxtException.ValidationException {
   TransactionImpl transaction =
       new TransactionImpl(
           attachment.getTransactionType(),
           Convert.getEpochTime(),
           deadline,
           senderPublicKey,
           recipientId,
           amountNQT,
           feeNQT,
           referencedTransactionFullHash,
           null);
   transaction.setAttachment(attachment);
   transaction.validateAttachment();
   return transaction;
 }
 void removeUnconfirmedTransactions(Collection<TransactionImpl> transactions) {
   List<Transaction> removedList = new ArrayList<>();
   for (TransactionImpl transaction : transactions) {
     if (unconfirmedTransactions.remove(transaction.getId()) != null) {
       transaction.undoUnconfirmed();
       unconfirmedTransactionHashes.remove(transaction.getHash());
       removedList.add(transaction);
     }
   }
   transactionListeners.notify(removedList, Event.REMOVED_UNCONFIRMED_TRANSACTIONS);
 }
 void apply(BlockImpl block) {
   block.apply();
   for (TransactionImpl transaction : block.getTransactions()) {
     if (!unconfirmedTransactions.containsKey(transaction.getId())) {
       transaction.applyUnconfirmed();
     }
     // TODO: Phaser not yet implemented
     // Phaser.processTransaction(transaction);
     transaction.apply();
     transactionHashes.put(transaction.getHash(), new TransactionHashInfo(transaction));
   }
   purgeExpiredHashes(block.getTimestamp());
 }
 private void processPeerTransactions(JSONArray transactionsData, final boolean sendToPeers) {
   List<TransactionImpl> transactions = new ArrayList<>();
   for (Object transactionData : transactionsData) {
     try {
       transactions.add(parseTransaction((JSONObject) transactionData));
     } catch (NxtException.ValidationException e) {
       // if (! (e instanceof TransactionType.NotYetEnabledException)) {
       //    Logger.logDebugMessage("Dropping invalid transaction: " + e.getMessage());
       // }
     }
   }
   processTransactions(transactions, sendToPeers);
   for (TransactionImpl transaction : transactions) {
     nonBroadcastedTransactions.remove(transaction.getId());
   }
 }
Exemplo n.º 6
0
 /**
  * Rollback a transaction
  *
  * @exception SystemException Thrown if an error occurs
  */
 public void rollbackTransaction() throws SystemException {
   Long key = Long.valueOf(Thread.currentThread().getId());
   TransactionImpl tx = txs.get(key);
   if (tx != null) {
     try {
       tx.rollback();
     } catch (Throwable t) {
       SystemException se = new SystemException("Error during rollback");
       se.initCause(t);
       throw se;
     } finally {
       txs.remove(key);
     }
   } else {
     throw new IllegalStateException("No transaction to rollback");
   }
 }
 protected TransactionRunner(GetOrCreateTransactionResult result) {
   txn = result.getTransaction();
   finishTxn = result.isNew();
   if (txn == null && finishTxn) {
     throw new IllegalArgumentException(
         "Cannot have a null txn when finishTxn is true.  This "
             + "almost certainly represents a programming error on the part of the App Engine team.  "
             + "Please report this via standard support channels and accept our humblest apologies.");
   }
   TransactionImpl.ensureTxnActive(txn);
 }
 protected void markAsRollback() {
   log.debug("mark transaction for rollback");
   if (tx.isActive()) {
     tx.setRollbackOnly();
   } else {
     // no explicit use of the tx. boundaries methods
     if (PersistenceUnitTransactionType.JTA == transactionType) {
       TransactionManager transactionManager =
           ((SessionFactoryImplementor) getRawSession().getSessionFactory())
               .getTransactionManager();
       if (transactionManager == null) {
         throw new PersistenceException(
             "Using a JTA persistence context wo setting hibernate.transaction.manager_lookup_class");
       }
       try {
         transactionManager.setRollbackOnly();
       } catch (SystemException e) {
         throw new PersistenceException("Unable to set the JTA transaction as RollbackOnly", e);
       }
     }
   }
 }
        @Override
        public void run() {

          try {
            try {

              int curTime = Convert.getEpochTime();
              List<Transaction> removedUnconfirmedTransactions = new ArrayList<>();

              synchronized (BlockchainImpl.getInstance()) {
                Iterator<TransactionImpl> iterator = unconfirmedTransactions.values().iterator();
                while (iterator.hasNext()) {
                  TransactionImpl transaction = iterator.next();
                  if (transaction.getExpiration() < curTime) {
                    iterator.remove();
                    unconfirmedTransactionHashes.remove(transaction.getHash());
                    transaction.undoUnconfirmed();
                    removedUnconfirmedTransactions.add(transaction);
                  }
                }
              }

              if (removedUnconfirmedTransactions.size() > 0) {
                transactionListeners.notify(
                    removedUnconfirmedTransactions, Event.REMOVED_UNCONFIRMED_TRANSACTIONS);
              }

            } catch (Exception e) {
              Logger.logDebugMessage("Error removing unconfirmed transactions", e);
            }
          } catch (Throwable t) {
            Logger.logMessage("CRITICAL ERROR. PLEASE REPORT TO THE DEVELOPERS.\n" + t.toString());
            t.printStackTrace();
            System.exit(1);
          }
        }
Exemplo n.º 10
0
 void undo(BlockImpl block) throws TransactionType.UndoNotSupportedException {
   block.undo();
   List<Transaction> addedUnconfirmedTransactions = new ArrayList<>();
   for (TransactionImpl transaction : block.getTransactions()) {
     TransactionHashInfo transactionHashInfo = transactionHashes.get(transaction.getHash());
     if (transactionHashInfo != null
         && transactionHashInfo.transactionId.equals(transaction.getId())) {
       transactionHashes.remove(transaction.getHash());
     }
     unconfirmedTransactions.put(transaction.getId(), transaction);
     unconfirmedTransactionHashes.put(transaction.getHash(), transaction);
     transaction.undo();
     addedUnconfirmedTransactions.add(transaction);
   }
   if (addedUnconfirmedTransactions.size() > 0) {
     transactionListeners.notify(
         addedUnconfirmedTransactions, TransactionProcessor.Event.ADDED_UNCONFIRMED_TRANSACTIONS);
   }
 }
Exemplo n.º 11
0
  TransactionImpl checkTransactionHashes(BlockImpl block) {
    TransactionImpl duplicateTransaction = null;
    for (TransactionImpl transaction : block.getTransactions()) {
      if (transactionHashes.putIfAbsent(transaction.getHash(), new TransactionHashInfo(transaction))
              != null
          && block.getHeight() != 58294) {
        duplicateTransaction = transaction;
        break;
      }
    }

    if (duplicateTransaction != null) {
      for (TransactionImpl transaction : block.getTransactions()) {
        if (!transaction.equals(duplicateTransaction)) {
          TransactionHashInfo transactionHashInfo = transactionHashes.get(transaction.getHash());
          if (transactionHashInfo != null
              && transactionHashInfo.transactionId.equals(transaction.getId())) {
            transactionHashes.remove(transaction.getHash());
          }
        }
      }
    }
    return duplicateTransaction;
  }
Exemplo n.º 12
0
  private List<Transaction> processTransactions(
      List<TransactionImpl> transactions, final boolean sendToPeers) {
    List<Transaction> sendToPeersTransactions = new ArrayList<>();
    List<Transaction> addedUnconfirmedTransactions = new ArrayList<>();
    List<Transaction> addedDoubleSpendingTransactions = new ArrayList<>();

    for (TransactionImpl transaction : transactions) {

      try {

        int curTime = Convert.getEpochTime();
        if (transaction.getTimestamp() > curTime + 15
            || transaction.getExpiration() < curTime
            || transaction.getDeadline() > 1440) {
          continue;
        }

        synchronized (BlockchainImpl.getInstance()) {
          Long id = transaction.getId();
          if (TransactionDb.hasTransaction(id)
              || unconfirmedTransactions.containsKey(id)
              || !transaction.verify()) {
            continue;
          }

          if (transactionHashes.containsKey(transaction.getHash())
              || unconfirmedTransactionHashes.containsKey(transaction.getHash())) {
            continue;
          }

          if (transaction.applyUnconfirmed()) {
            if (sendToPeers) {
              if (nonBroadcastedTransactions.containsKey(id)) {
                Logger.logDebugMessage(
                    "Received back transaction "
                        + transaction.getStringId()
                        + " that we generated, will not forward to peers");
                nonBroadcastedTransactions.remove(id);
              } else {
                sendToPeersTransactions.add(transaction);
              }
            }
            unconfirmedTransactions.put(id, transaction);
            unconfirmedTransactionHashes.put(transaction.getHash(), transaction);
            addedUnconfirmedTransactions.add(transaction);
          } else {
            addedDoubleSpendingTransactions.add(transaction);
          }
        }

      } catch (RuntimeException e) {
        Logger.logMessage("Error processing transaction", e);
      }
    }

    if (sendToPeersTransactions.size() > 0) {
      Peers.sendToSomePeers(sendToPeersTransactions);
    }

    if (addedUnconfirmedTransactions.size() > 0) {
      transactionListeners.notify(
          addedUnconfirmedTransactions, Event.ADDED_UNCONFIRMED_TRANSACTIONS);
    }
    if (addedDoubleSpendingTransactions.size() > 0) {
      transactionListeners.notify(
          addedDoubleSpendingTransactions, Event.ADDED_DOUBLESPENDING_TRANSACTIONS);
    }
    return addedUnconfirmedTransactions;
  }
Exemplo n.º 13
0
 public void terminate() {
   super.terminate();
   _transactionManager.transactionTerminated(this);
   if (_listener != null) _listener.transactionTerminated(this);
 }
Exemplo n.º 14
0
  public void testCheckCacheAfterRollback() throws Exception {
    RollbackObjectOne ro = null;
    RollbackObjectOne ro_2 = null;
    String name = "testCheckCacheAfterRollback_" + System.currentTimeMillis();

    Transaction tx = odmg.newTransaction();
    tx.begin();
    ro = new RollbackObjectOne();
    ro.setName(name);
    tx.lock(ro, Transaction.WRITE);

    ro_2 = new RollbackObjectOne();
    ro_2.setName(name);
    tx.lock(ro_2, Transaction.WRITE);
    tx.commit();

    tx = odmg.newTransaction();
    tx.begin();
    OQLQuery query = odmg.newOQLQuery();
    query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1");
    query.bind(name);
    List list = (List) query.execute();
    tx.commit();
    assertEquals(2, list.size());

    tx = odmg.newTransaction();
    tx.begin();
    tx.lock(ro, Transaction.WRITE);
    ro = new RollbackObjectOne();
    ro.setDescription(name);

    tx.lock(ro_2, Transaction.WRITE);
    ro_2 = new RollbackObjectOne();
    ro_2.setDescription(name);

    tx.abort();

    tx = odmg.newTransaction();
    tx.begin();
    query = odmg.newOQLQuery();
    query.create("select all from " + RollbackObjectOne.class.getName() + " where name like $1");
    query.bind(name);
    list = (List) query.execute();
    tx.commit();
    assertEquals(2, list.size());
    assertNull(((RollbackObjectOne) list.get(0)).getDescription());
    assertNull(((RollbackObjectOne) list.get(1)).getDescription());

    // after tx another tx should be able to lock these objects
    TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
    tx2.begin();
    try {
      Iterator it = list.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }
    } finally {
      tx2.abort();
    }
  }
Exemplo n.º 15
0
  /**
   * Tests behavior within transactions. If i store 5 odmgZoos within a transaction and after that
   * within the same transaction i do query 'select all odmgZoos' the number of odmgZoos returned
   * should be oldNumber+5 when using checkpoint. thma: this testcase seems to fail for some strange
   * problems with the testbed data the thrown error is unrelated to the things covered in the
   * testcase. arminw: should be fixed
   */
  public void testResultsWhileTransactionWithCheckpoint() throws Exception {
    // if(ojbSkipKnownIssueProblem()) return;

    int odmgZoosBefore = getDBObjectCountWithNewPB(ODMGZoo.class);
    int projectsBefore = getDBObjectCountWithNewPB(ODMGGourmet.class);

    TransactionExt tx = (TransactionExt) odmg.newTransaction();
    tx.begin();
    List zoos = new ArrayList(getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5));
    List projects = new ArrayList(getNewProjects("testResultsWhileTransactionWithCheckpoint", 3));
    // store some objects
    storeObjects(tx, zoos);
    storeObjects(tx, projects);
    // checkpoint, should bring objects to DB but shouldn't commit
    tx.checkpoint();

    // Do a queries within a transaction
    int odmgZoosWhile = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class);
    int projectsWhile = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class);
    int odmgZoosWhilePB = 0;
    int projectsWhilePB = 0;

    odmgZoosWhilePB = getDBObjectCount(tx.getBroker(), ODMGZoo.class);
    projectsWhilePB = getDBObjectCount(tx.getBroker(), ODMGGourmet.class);

    // store more
    List zoos2 = new ArrayList(getNewODMGZoos("testResultsWhileTransactionWithCheckpoint", 5));
    List projects2 = new ArrayList(getNewProjects("testResultsWhileTransactionWithCheckpoint", 2));
    storeObjects(tx, zoos2);
    storeObjects(tx, projects2);

    zoos.addAll(zoos2);
    projects.addAll(projects2);
    // checkpoint, should bring objects to DB but shouldn't commit
    tx.checkpoint();

    // after checkpoint another tx should NOT be able to lock these objects
    TransactionImpl tx2 = (TransactionImpl) odmg.newTransaction();
    tx2.begin();
    try {
      Iterator it = zoos.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }

      it = projects.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }
      fail("After checkpoint all locks should be still exist for objects");
    } catch (LockNotGrantedException e) {
      // expected
      assertTrue(true);
    } finally {
      tx2.abort();
    }
    // reassign tx
    tx.join();

    // more queries
    int odmgZoosWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGZoo.class);
    int projectsWhile2 = getDBObjectCountViaOqlQuery(odmg, ODMGGourmet.class);
    int odmgZoosWhilePB2 = 0;
    int projectsWhilePB2 = 0;

    odmgZoosWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGZoo.class);
    projectsWhilePB2 = getDBObjectCount(tx.getBroker(), ODMGGourmet.class);

    tx.commit();
    int odmgZoosAfter = getDBObjectCountWithNewPB(ODMGZoo.class);
    int projectsAfter = getDBObjectCountWithNewPB(ODMGGourmet.class);
    int odmgZoosAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGZoo.class);
    int projectsAfterOQL = getDBObjectCountViaOqlQueryUseNewTransaction(odmg, ODMGGourmet.class);

    assertEquals(
        "Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfter);
    assertEquals(
        "Wrong number of projects found after commit", (projectsBefore + 5), projectsAfter);
    assertEquals(
        "Wrong number of odmgZoos found after commit", (odmgZoosBefore + 10), odmgZoosAfterOQL);
    assertEquals(
        "Wrong number of projects found after commit", (projectsBefore + 5), projectsAfterOQL);

    /*
    Here we test if we can see our changes while the transaction runs. IMO it must be
    possible to see all changes made in a transaction.
    */

    assertEquals(
        "Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhilePB);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhilePB);
    assertEquals(
        "Wrong number of odmgZoos found while transaction",
        (odmgZoosBefore + 10),
        odmgZoosWhilePB2);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhilePB2);
    assertEquals(
        "Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 5), odmgZoosWhile);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 3), projectsWhile);
    assertEquals(
        "Wrong number of odmgZoos found while transaction", (odmgZoosBefore + 10), odmgZoosWhile2);
    assertEquals(
        "Wrong number of projects found while transaction", (projectsBefore + 5), projectsWhile2);

    // after tx another tx should be able to lock these objects
    tx2 = (TransactionImpl) odmg.newTransaction();
    tx2.begin();
    try {
      Iterator it = zoos.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }

      it = projects.iterator();
      while (it.hasNext()) {
        Object o = it.next();
        tx2.lock(o, Transaction.WRITE);
      }
    } finally {
      tx2.abort();
    }
  }