From 3d71254dfa781839f6cf7a54fc661bef13606a27 Mon Sep 17 00:00:00 2001 From: daniellehrner Date: Wed, 11 Sep 2024 00:50:23 +0200 Subject: [PATCH] Mandatoy to field for 7702 transactions (#7600) * fix 7702 tx validation in tx pool Signed-off-by: Daniel Lehrner * check that 7702 transactions always have a mandatory to field Signed-off-by: Daniel Lehrner --------- Signed-off-by: Daniel Lehrner Co-authored-by: Sally MacFarlane --- .../mainnet/MainnetTransactionValidator.java | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java index b5fbf7340b2..e67ccbb4c62 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionValidator.java @@ -132,42 +132,59 @@ public ValidationResult validate( } if (transactionType == TransactionType.DELEGATE_CODE) { - if (isDelegateCodeEmpty(transaction)) { - return ValidationResult.invalid( - TransactionInvalidReason.EMPTY_CODE_DELEGATION, - "transaction code delegation transactions must have a non-empty code delegation list"); + ValidationResult codeDelegationValidation = + validateCodeDelegation(transaction); + if (!codeDelegationValidation.isValid()) { + return codeDelegationValidation; } + } + + return validateCostAndFee(transaction, baseFee, blobFee, transactionValidationParams); + } - final BigInteger halfCurveOrder = SignatureAlgorithmFactory.getInstance().getHalfCurveOrder(); - final Optional> validationResult = - transaction - .getCodeDelegationList() - .map( - codeDelegations -> { - for (CodeDelegation codeDelegation : codeDelegations) { - if (codeDelegation.signature().getS().compareTo(halfCurveOrder) > 0) { - return ValidationResult.invalid( - TransactionInvalidReason.INVALID_SIGNATURE, - "Invalid signature for code delegation. S value must be less or equal than the half curve order."); - } - - if (codeDelegation.signature().getRecId() != 0 - && codeDelegation.signature().getRecId() != 1) { - return ValidationResult.invalid( - TransactionInvalidReason.INVALID_SIGNATURE, - "Invalid signature for code delegation. RecId value must be 0 or 1."); - } + private static ValidationResult validateCodeDelegation( + final Transaction transaction) { + if (isDelegateCodeEmpty(transaction)) { + return ValidationResult.invalid( + TransactionInvalidReason.EMPTY_CODE_DELEGATION, + "transaction code delegation transactions must have a non-empty code delegation list"); + } + + if (transaction.getTo().isEmpty()) { + return ValidationResult.invalid( + TransactionInvalidReason.INVALID_TRANSACTION_FORMAT, + "transaction code delegation transactions must have a to address"); + } + + final BigInteger halfCurveOrder = SignatureAlgorithmFactory.getInstance().getHalfCurveOrder(); + final Optional> validationResult = + transaction + .getCodeDelegationList() + .map( + codeDelegations -> { + for (CodeDelegation codeDelegation : codeDelegations) { + if (codeDelegation.signature().getS().compareTo(halfCurveOrder) > 0) { + return ValidationResult.invalid( + TransactionInvalidReason.INVALID_SIGNATURE, + "Invalid signature for code delegation. S value must be less or equal than the half curve order."); } - return ValidationResult.valid(); - }); + if (codeDelegation.signature().getRecId() != 0 + && codeDelegation.signature().getRecId() != 1) { + return ValidationResult.invalid( + TransactionInvalidReason.INVALID_SIGNATURE, + "Invalid signature for code delegation. RecId value must be 0 or 1."); + } + } - if (validationResult.isPresent() && !validationResult.get().isValid()) { - return validationResult.get(); - } + return ValidationResult.valid(); + }); + + if (validationResult.isPresent() && !validationResult.get().isValid()) { + return validationResult.get(); } - return validateCostAndFee(transaction, baseFee, blobFee, transactionValidationParams); + return ValidationResult.valid(); } private static boolean isDelegateCodeEmpty(final Transaction transaction) {