Now Transaction.importAsConfirmed() calls Controller.onNewTransaction(), removing call from API POST /transactions/process and Controller.onNetworkTransactionMessage()

This commit is contained in:
catbref 2020-12-02 13:19:53 +00:00
parent 064e12a57b
commit 58ff338ab3
3 changed files with 21 additions and 13 deletions

View File

@ -539,6 +539,8 @@ public class TransactionsResource {
} finally { } finally {
blockchainLock.unlock(); blockchainLock.unlock();
} }
return "true";
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA, e); throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA, e);
} catch (DataException e) { } catch (DataException e) {
@ -546,11 +548,6 @@ public class TransactionsResource {
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw createTransactionInvalidException(request, ValidationResult.NO_BLOCKCHAIN_LOCK); throw createTransactionInvalidException(request, ValidationResult.NO_BLOCKCHAIN_LOCK);
} }
// Notify controller of new transaction
Controller.getInstance().onNewTransaction(transactionData, null);
return "true";
} }
@POST @POST

View File

@ -1048,11 +1048,16 @@ public class Controller extends Thread {
} }
} }
/** Callback for when we've received a new transaction via API or peer. */ /**
public void onNewTransaction(TransactionData transactionData, Peer peer) { * Callback for when we've received a new transaction via API or peer.
* <p>
* @implSpec performs actions in a new thread
*/
public void onNewTransaction(TransactionData transactionData) {
this.callbackExecutor.execute(() -> { this.callbackExecutor.execute(() -> {
// Notify all peers (except maybe peer that sent it to us if applicable) // Notify all peers
Network.getInstance().broadcast(broadcastPeer -> broadcastPeer == peer ? null : new TransactionSignaturesMessage(Arrays.asList(transactionData.getSignature()))); Message newTransactionSignatureMessage = new TransactionSignaturesMessage(Arrays.asList(transactionData.getSignature()));
Network.getInstance().broadcast(broadcastPeer -> newTransactionSignatureMessage);
// Notify listeners // Notify listeners
EventBus.INSTANCE.notify(new NewTransactionEvent(transactionData)); EventBus.INSTANCE.notify(new NewTransactionEvent(transactionData));
@ -1234,9 +1239,6 @@ public class Controller extends Thread {
} catch (DataException e) { } catch (DataException e) {
LOGGER.error(String.format("Repository issue while processing transaction %s from peer %s", Base58.encode(transactionData.getSignature()), peer), e); LOGGER.error(String.format("Repository issue while processing transaction %s from peer %s", Base58.encode(transactionData.getSignature()), peer), e);
} }
// Notify controller so it can notify other peers, etc.
Controller.getInstance().onNewTransaction(transactionData, peer);
} }
private void onNetworkGetBlockSummariesMessage(Peer peer, Message message) { private void onNetworkGetBlockSummariesMessage(Peer peer, Message message) {

View File

@ -765,7 +765,13 @@ public abstract class Transaction {
/** /**
* Import into our repository as a new, unconfirmed transaction. * Import into our repository as a new, unconfirmed transaction.
* <p> * <p>
* Calls <tt>repository.saveChanges()</tt> * @implSpec <i>tries</i> to obtain blockchain lock
* <p>
* If transaction is valid, then:
* <ul>
* <li>calls {@link Repository#discardChanges()}</li>
* <li>calls {@link Controller#onNewTransaction(TransactionData, Peer)}</li>
* </ul>
* *
* @throws DataException * @throws DataException
*/ */
@ -804,6 +810,9 @@ public abstract class Transaction {
repository.saveChanges(); repository.saveChanges();
// Notify controller of new transaction
Controller.getInstance().onNewTransaction(transactionData);
return ValidationResult.OK; return ValidationResult.OK;
} finally { } finally {
blockchainLock.unlock(); blockchainLock.unlock();