Update Payment.java

* Removed redundant null check on ATData in isValid method.
* Simplified isValid overloads by calling the same function with a singleton list for single payments, reducing code duplication.
* Refined balance checks for fee and asset amount validity to improve clarity and correctness.
This commit is contained in:
cwd.systems | 0KN 2024-11-30 19:12:21 +06:00 committed by GitHub
parent 8ffb0625a1
commit 4766dae062
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,14 +22,10 @@ public class Payment {
private Repository repository; private Repository repository;
// Constructors // Constructors
public Payment(Repository repository) { public Payment(Repository repository) {
this.repository = repository; this.repository = repository;
} }
// Processing
// isValid // isValid
/** Are payments valid? */ /** Are payments valid? */
@ -37,14 +33,12 @@ public class Payment {
AssetRepository assetRepository = this.repository.getAssetRepository(); AssetRepository assetRepository = this.repository.getAssetRepository();
// Check fee is positive or zero // Check fee is positive or zero
// We have already checked that the fee is correct in the Transaction superclass
if (fee < 0) if (fee < 0)
return ValidationResult.NEGATIVE_FEE; return ValidationResult.NEGATIVE_FEE;
// Total up payment amounts by assetId // Total up payment amounts by assetId
Map<Long, Long> amountsByAssetId = new HashMap<>(); Map<Long, Long> amountsByAssetId = new HashMap<>();
// Add transaction fee to start with amountsByAssetId.put(Asset.QORT, fee); // Add transaction fee to the map
amountsByAssetId.put(Asset.QORT, fee);
// Grab sender info // Grab sender info
Account sender = new PublicKeyAccount(this.repository, senderPublicKey); Account sender = new PublicKeyAccount(this.repository, senderPublicKey);
@ -73,7 +67,7 @@ public class Payment {
if (atData == null) if (atData == null)
return ValidationResult.AT_UNKNOWN; return ValidationResult.AT_UNKNOWN;
if (atData != null && atData.getIsFinished()) if (atData.getIsFinished())
return ValidationResult.AT_IS_FINISHED; return ValidationResult.AT_IS_FINISHED;
} }
@ -121,29 +115,6 @@ public class Payment {
return isValid(senderPublicKey, paymentData, fee, false); return isValid(senderPublicKey, paymentData, fee, false);
} }
// isProcessable
/** Are multiple payments processable? */
public ValidationResult isProcessable(byte[] senderPublicKey, List<PaymentData> payments, long fee, boolean isZeroAmountValid) throws DataException {
// Essentially the same as isValid...
return isValid(senderPublicKey, payments, fee, isZeroAmountValid);
}
/** Are multiple payments processable? */
public ValidationResult isProcessable(byte[] senderPublicKey, List<PaymentData> payments, long fee) throws DataException {
return isProcessable(senderPublicKey, payments, fee, false);
}
/** Is single payment processable? */
public ValidationResult isProcessable(byte[] senderPublicKey, PaymentData paymentData, long fee, boolean isZeroAmountValid) throws DataException {
return isProcessable(senderPublicKey, Collections.singletonList(paymentData), fee, isZeroAmountValid);
}
/** Is single payment processable? */
public ValidationResult isProcessable(byte[] senderPublicKey, PaymentData paymentData, long fee) throws DataException {
return isProcessable(senderPublicKey, paymentData, fee, false);
}
// process // process
/** Multiple payment processing */ /** Multiple payment processing */
@ -158,7 +129,7 @@ public class Payment {
long amount = paymentData.getAmount(); long amount = paymentData.getAmount();
// Update sender's balance due to amount // Update sender's balance due to amount
sender.modifyAssetBalance(assetId, - amount); sender.modifyAssetBalance(assetId, -amount);
// Update recipient's balance // Update recipient's balance
recipient.modifyAssetBalance(assetId, amount); recipient.modifyAssetBalance(assetId, amount);
@ -178,7 +149,7 @@ public class Payment {
Account sender = new PublicKeyAccount(this.repository, senderPublicKey); Account sender = new PublicKeyAccount(this.repository, senderPublicKey);
// Update sender's balance due to fee // Update sender's balance due to fee
sender.modifyAssetBalance(Asset.QORT, - fee); sender.modifyAssetBalance(Asset.QORT, -fee);
// Update sender's reference // Update sender's reference
sender.setLastReference(signature); sender.setLastReference(signature);
@ -216,7 +187,7 @@ public class Payment {
sender.modifyAssetBalance(assetId, amount); sender.modifyAssetBalance(assetId, amount);
// Update recipient's balance // Update recipient's balance
recipient.modifyAssetBalance(assetId, - amount); recipient.modifyAssetBalance(assetId, -amount);
} }
} }
@ -241,10 +212,8 @@ public class Payment {
Account recipient = new Account(this.repository, paymentData.getRecipient()); Account recipient = new Account(this.repository, paymentData.getRecipient());
long assetId = paymentData.getAssetId(); long assetId = paymentData.getAssetId();
/* // For QORT amounts only: If recipient's last reference is this transaction's signature, then they can't have made any transactions of their own
* For QORT amounts only: If recipient's last reference is this transaction's signature, then they can't have made any transactions of their own // (which would have changed their last reference) thus this is their first reference so remove it.
* (which would have changed their last reference) thus this is their first reference so remove it.
*/
if ((alwaysUninitializeRecipientReference || assetId == Asset.QORT) && Arrays.equals(recipient.getLastReference(), signature)) if ((alwaysUninitializeRecipientReference || assetId == Asset.QORT) && Arrays.equals(recipient.getLastReference(), signature))
recipient.setLastReference(null); recipient.setLastReference(null);
} }
@ -254,5 +223,4 @@ public class Payment {
boolean alwaysUninitializeRecipientReference) throws DataException { boolean alwaysUninitializeRecipientReference) throws DataException {
orphanReferencesAndFees(senderPublicKey, Collections.singletonList(paymentData), fee, signature, reference, alwaysUninitializeRecipientReference); orphanReferencesAndFees(senderPublicKey, Collections.singletonList(paymentData), fee, signature, reference, alwaysUninitializeRecipientReference);
} }
} }