Ejemplo n.º 1
0
  private boolean isPendingEventsBySource(EventSource eventSource)
      throws CantExecuteQueryException {
    try {
      this.database = openDatabase();
      DatabaseTable databaseTable;
      databaseTable =
          database.getTable(
              AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_TABLE_NAME);
      databaseTable.addStringFilter(
          AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_STATUS_COLUMN_NAME,
          EventStatus.PENDING.getCode(),
          DatabaseFilterType.EQUAL);
      databaseTable.addStringFilter(
          AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_SOURCE_COLUMN_NAME,
          eventSource.getCode(),
          DatabaseFilterType.EQUAL);
      databaseTable.loadToMemory();

      return !databaseTable.getRecords().isEmpty();
    } catch (CantLoadTableToMemoryException exception) {

      throw new CantExecuteQueryException(
          "Error executing query in DB.",
          exception,
          "Getting pending events.",
          "Cannot load table to memory.");
    } catch (Exception exception) {

      throw new CantExecuteQueryException(
          CantExecuteQueryException.DEFAULT_MESSAGE,
          FermatException.wrapException(exception),
          "Getting pending events.",
          "Unexpected exception");
    }
  }
 @Before
 public void init() throws Exception {
   event = new IncomingAssetReversedOnBlockchainWaitingTransferenceAssetUserEvent();
   EventSource eventSource =
       EventSource.getByCode(EventSource.ASSETS_OVER_BITCOIN_VAULT.getCode());
   event.setSource(eventSource);
   assetDistributionRecorderService =
       new AssetDistributionRecorderService(assetDistributionDao, eventManager);
 }
Ejemplo n.º 3
0
  private List<String> getPendingEventsBySource(EventSource eventSource)
      throws CantCheckAssetReceptionProgressException,
          UnexpectedResultReturnedFromDatabaseException {

    try {
      this.database = openDatabase();
      List<String> eventIdList = new ArrayList<>();
      DatabaseTable databaseTable =
          getDatabaseTable(
              AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_TABLE_NAME);
      databaseTable.addStringFilter(
          AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_STATUS_COLUMN_NAME,
          EventStatus.PENDING.getCode(),
          DatabaseFilterType.EQUAL);
      databaseTable.addStringFilter(
          AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_SOURCE_COLUMN_NAME,
          eventSource.getCode(),
          DatabaseFilterType.EQUAL);
      databaseTable.addFilterOrder(
          AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_TIMESTAMP_COLUMN_NAME,
          DatabaseFilterOrder.ASCENDING);
      databaseTable.loadToMemory();
      List<DatabaseTableRecord> databaseTableRecords = databaseTable.getRecords();
      for (DatabaseTableRecord databaseTableRecord : databaseTableRecords) {
        String eventId =
            databaseTableRecord.getStringValue(
                AssetReceptionDatabaseConstants.ASSET_RECEPTION_EVENTS_RECORDED_ID_COLUMN_NAME);
        eventIdList.add(eventId);
      }

      return eventIdList;
    } catch (CantExecuteDatabaseOperationException exception) {

      throw new CantCheckAssetReceptionProgressException(
          exception, "Trying to get pending events", "Cannot find or open the database");
    } catch (CantLoadTableToMemoryException exception) {

      throw new CantCheckAssetReceptionProgressException(
          exception, "Trying to get pending events", "Cannot load the database into memory");
    } catch (Exception exception) {

      throw new CantCheckAssetReceptionProgressException(
          FermatException.wrapException(exception),
          "Trying to get pending events.",
          "Unexpected exception");
    }
  }
    private void processEvent(IncomingCryptoRegistry.EventWrapper eventWrapper) {
      System.out.println("TTF - INCOMING CRYPTO MONITOR: NEW EVENT READ");

      // We have here new pending transactions, we will check the source and ask for the right
      // TransactionSender

      TransactionProtocolManager<CryptoTransaction> source = null;
      try {
        source =
            this.sourceAdministrator.getSourceAdministrator(
                EventSource.getByCode(eventWrapper.eventSource));
      } catch (InvalidParameterException e) {
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        return;
      } catch (CantIdentifyEventSourceException e) {
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        return;
      }

      System.out.println("TTF - INCOMING CRYPTO MONITOR: Source Identified");

      // Now we ask for the pending transactions
      List<Transaction<CryptoTransaction>> transactionList = null;

      try {
        transactionList = source.getPendingTransactions(Specialist.CRYPTO_ROUTER_SPECIALIST);
      } catch (CantDeliverPendingTransactionsException e) {
        System.out.println(
            "TTF - INCOMING CRYPTO MONITOR: cryptoVault raised CantDeliverPendingTransactionsException");
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        // if somethig wrong happenned we try in the next round
        return;
      }

      // Now we save the list in the registry
      if (transactionList != null) {
        this.registry.acknowledgeTransactions(transactionList);
        System.out.println(
            "TTF - INCOMING CRYPTO MONITOR: "
                + transactionList.size()
                + " TRANSACTION(S) ACKNOWLEDGE");

      } else {
        // if sombething failed we try in next round
        // errorManager.reportUnexpectedPluginException(Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION, UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN, );
        return;
      }

      // Now we take all the transactions in state (ACKNOWLEDGE,TO_BE_NOTIFIED)
      // Remember that this list can be more extensive than the one we saved, this is
      // because the system could have shut down in this step of the protocol making old
      // transactions to be stored but not precessed.
      List<Transaction<CryptoTransaction>> acknowledgedTransactions = null;
      try {
        acknowledgedTransactions = this.registry.getAcknowledgedTransactions();
      } catch (InvalidParameterException e) {
        this.errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
        return;
      }

      // An finally, for each transaction we confirm it and then register responsibility.
      for (Transaction<CryptoTransaction> transaction : acknowledgedTransactions) {
        try {
          source.confirmReception(transaction.getTransactionID());
          System.out.println("TTF - INCOMING CRYPTO MONITOR: RESPONSIBILITY ACQUIRED");
          this.registry.acquireResponsibility(transaction);
        } catch (CantConfirmTransactionException e) {
          // TODO: Consultar si esto hace lo que pienso, si falla no registra en base de datos
          //       la transacción
          // We will inform the exception and try again in the next round
          errorManager.reportUnexpectedPluginException(
              Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
              UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
              e);
        }
      }
      // After finishing all the steps we mark the event as seen.
      try {
        this.registry.disableEvent(eventWrapper.eventId);
        System.out.println("TTF - INCOMING CRYPTO MONITR: EVENT DISABLED");

      } catch (Exception e) { // There are two exceptions and we react in the same way to both
        // We will inform the exception and try again in the next round
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
      }
    }
    private void processEvent(EventWrapper eventWrapper) {
      try {
        TransactionProtocolManager<FermatCryptoTransaction> source =
            this.sourceAdministrator.getSourceAdministrator(
                EventSource.getByCode(eventWrapper.getEventSource()));
        List<Transaction<FermatCryptoTransaction>> transactionList =
            source.getPendingTransactions(Specialist.INTRA_USER_SPECIALIST);

        System.out.println(
            "TTF - INTRA USER METADATA MONITOR: "
                + transactionList.size()
                + " TRANSACTION(s) DETECTED");

        this.registry.acknowledgeFermatCryptoTransactions(transactionList);

        System.out.println(
            "TTF - INTRA USER METADATA MONITOR: "
                + transactionList.size()
                + " TRANSACTION(s) ACKNOWLEDGED");

        // Now we take all the transactions in state (ACKNOWLEDGE,TO_BE_NOTIFIED)
        // Remember that this list can be more extensive than the one we saved, this is
        // because the system could have shut down in this step of the protocol making old
        // transactions to be stored but not precessed.
        List<Transaction<FermatCryptoTransaction>> acknowledgedTransactions =
            this.registry.getAcknowledgedFermatCryptoTransactions();

        for (Transaction<FermatCryptoTransaction> transaction : acknowledgedTransactions) {
          try {
            source.confirmReception(transaction.getTransactionID());
            System.out.println(
                "TTF - INTRA USER MONITOR METADATA: TRANSACTION RESPONSIBILITY ACQUIRED");
            registry.acquireFermatCryptoTransactionResponsibility(transaction);

            // notified Transmission NS that transaction Seen By Vault
            //
            // cryptoTransmissionNetworkServiceManager.informTransactionSeenByVault(transaction.getTransactionID());

          } catch (CantConfirmTransactionException
              | com.bitdubai.fermat_ccp_plugin.layer.crypto_transaction.incoming_intra_user
                      .developer.bitdubai.version_1.exceptions
                      .IncomingIntraUserCantAcquireResponsibilityException
                  exception) {
            // TODO: Consultar si esto hace lo que pienso, si falla no registra en base de datos
            //       la transacción
            // We will inform the exception and try again in the next round
            errorManager.reportUnexpectedPluginException(
                Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
                UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
                exception);
          }
          //  catch(CantSetToSeenByCryptoVaultException e){
          // errorManager.reportUnexpectedPluginException(Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION, UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN, e);

          // }
        }

        registry.disableEvent(eventWrapper.getEventId());
        System.out.println("TTF - INTRA USER METADATA MONITOR: EVENT DISABLED");
      } catch (Exception e) {
        errorManager.reportUnexpectedPluginException(
            Plugins.BITDUBAI_INCOMING_CRYPTO_TRANSACTION,
            UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN,
            e);
      }
    }