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;
// Constructors
public Payment(Repository repository) {
this.repository = repository;
}
// Processing
// isValid
/** Are payments valid? */
@ -37,14 +33,12 @@ public class Payment {
AssetRepository assetRepository = this.repository.getAssetRepository();
// Check fee is positive or zero
// We have already checked that the fee is correct in the Transaction superclass
if (fee < 0)
return ValidationResult.NEGATIVE_FEE;
// Total up payment amounts by assetId
Map<Long, Long> amountsByAssetId = new HashMap<>();
// Add transaction fee to start with
amountsByAssetId.put(Asset.QORT, fee);
amountsByAssetId.put(Asset.QORT, fee); // Add transaction fee to the map
// Grab sender info
Account sender = new PublicKeyAccount(this.repository, senderPublicKey);
@ -73,7 +67,7 @@ public class Payment {
if (atData == null)
return ValidationResult.AT_UNKNOWN;
if (atData != null && atData.getIsFinished())
if (atData.getIsFinished())
return ValidationResult.AT_IS_FINISHED;
}
@ -121,29 +115,6 @@ public class Payment {
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
/** Multiple payment processing */
@ -241,10 +212,8 @@ public class Payment {
Account recipient = new Account(this.repository, paymentData.getRecipient());
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
* (which would have changed their last reference) thus this is their first reference so remove it.
*/
// 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.
if ((alwaysUninitializeRecipientReference || assetId == Asset.QORT) && Arrays.equals(recipient.getLastReference(), signature))
recipient.setLastReference(null);
}
@ -254,5 +223,4 @@ public class Payment {
boolean alwaysUninitializeRecipientReference) throws DataException {
orphanReferencesAndFees(senderPublicKey, Collections.singletonList(paymentData), fee, signature, reference, alwaysUninitializeRecipientReference);
}
}