Skip to content

Commit

Permalink
refactor: address PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Stanimir Stoyanov <stanimir.stoyanov@limechain.tech>
  • Loading branch information
stoyanov-st committed Nov 3, 2023
1 parent 25a3da7 commit 0f8b13c
Showing 1 changed file with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.CHILD;
import static com.hedera.node.app.spi.workflows.HandleContext.TransactionCategory.PRECEDING;
import static com.hedera.node.app.state.HederaRecordCache.DuplicateCheckResult.NO_DUPLICATE;
import static com.hedera.node.app.state.HederaRecordCache.DuplicateCheckResult.SAME_NODE;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.AccountID;
Expand Down Expand Up @@ -66,8 +65,6 @@
import com.hedera.node.app.spi.workflows.FunctionalityResourcePrices;
import com.hedera.node.app.spi.workflows.HandleContext;
import com.hedera.node.app.spi.workflows.HandleException;
import com.hedera.node.app.spi.workflows.InsufficientNonFeeDebitsException;
import com.hedera.node.app.spi.workflows.InsufficientServiceFeeException;
import com.hedera.node.app.spi.workflows.PreCheckException;
import com.hedera.node.app.spi.workflows.TransactionKeys;
import com.hedera.node.app.state.HederaRecordCache;
Expand Down Expand Up @@ -677,9 +674,9 @@ private void dispatchSyntheticTxn(
}

private void validate(
@NonNull final KeyVerifier verifier,
@NonNull final KeyVerifier keyVerifier,
final HederaFunctionality function,
final TransactionBody txBody,
final TransactionBody transactionBody,
final AccountID payer,
final Key payerKey,
final TransactionCategory txCategory,
Expand All @@ -688,31 +685,27 @@ private void validate(

final PreHandleContextImpl preHandleContext;

preHandleContext = new PreHandleContextImpl(readableStoreFactory(), txBody, payer, configuration(), dispatcher);
preHandleContext =
new PreHandleContextImpl(readableStoreFactory(), transactionBody, payer, configuration(), dispatcher);
dispatcher.dispatchPreHandle(preHandleContext);

// Check for duplicate transactions. It is perfectly normal for there to be duplicates -- it is valid for
// a user to intentionally submit duplicates to multiple nodes as a hedge against dishonest nodes, or for
// other reasons. If we find a duplicate, we *will not* execute the transaction, we will simply charge
// the payer (whether the payer from the transaction or the node in the event of a due diligence failure)
// and create an appropriate record to save in state and send to the record stream.
final var duplicateCheckResult = recordCache.hasDuplicate(txBody.transactionID(), nodeID);
if (duplicateCheckResult != NO_DUPLICATE && duplicateCheckResult != SAME_NODE) {
final var duplicateCheckResult = recordCache.hasDuplicate(transactionBody.transactionID(), nodeID);
if (duplicateCheckResult != NO_DUPLICATE) {
throw new PreCheckException(DUPLICATE_TRANSACTION);
}

// Check the status and solvency of the payer
try {

final var fee = dispatchComputeFees(txBody, payer);
final var payerAccount = solvencyPreCheck.getPayerAccount(readableStoreFactory(), payer);
solvencyPreCheck.checkSolvency(txBody, payer, functionality, payerAccount, fee, true);
} catch (final InsufficientServiceFeeException | InsufficientNonFeeDebitsException e) {
throw new PreCheckException(e.responseCode());
}
final var fee = dispatchComputeFees(body(), payer);
final var payerAccount = solvencyPreCheck.getPayerAccount(readableStoreFactory(), payer);
solvencyPreCheck.checkSolvency(body(), payer, functionality, payerAccount, fee, true);

// Check the time box of the transaction
checker.checkTimeBox(txBody, userTransactionConsensusTime);
checker.checkTimeBox(transactionBody, userTransactionConsensusTime);

// Check if the payer has the required permissions
if (!authorizer.isAuthorized(payer, function)) {
Expand All @@ -723,7 +716,7 @@ private void validate(
}

// Check if the transaction is privileged and if the payer has the required privileges
final var privileges = authorizer.hasPrivilegedAuthorization(payer, function, txBody);
final var privileges = authorizer.hasPrivilegedAuthorization(payer, function, transactionBody);
if (privileges == SystemPrivilege.UNAUTHORIZED) {
throw new PreCheckException(ResponseCodeEnum.AUTHORIZATION_FAILED);
}
Expand All @@ -734,22 +727,22 @@ private void validate(
// Skip payer verification when dispatching a child transaction
if (!txCategory.equals(CHILD)) {
// Check all signature verifications. This will also wait, if validation is still ongoing.
final var payerKeyVerification = verifier.verificationFor(payerKey);
final var payerKeyVerification = keyVerifier.verificationFor(payerKey);
if (payerKeyVerification.failed()) {
throw new PreCheckException(INVALID_SIGNATURE);
}
}

// verify all the keys
for (final var key : preHandleContext.requiredNonPayerKeys()) {
final var verification = verifier.verificationFor(key);
final var verification = keyVerifier.verificationFor(key);
if (verification.failed()) {
throw new PreCheckException(INVALID_SIGNATURE);
}
}
// If there are any hollow accounts whose signatures need to be verified, verify them
for (final var hollowAccount : preHandleContext.requiredHollowAccounts()) {
final var verification = verifier.verificationFor(hollowAccount.alias());
final var verification = keyVerifier.verificationFor(hollowAccount.alias());
if (verification.failed()) {
throw new PreCheckException(INVALID_SIGNATURE);
}
Expand Down

0 comments on commit 0f8b13c

Please sign in to comment.