Skip to content

Commit

Permalink
Add RSKIP number and refactor validate method in TxValidatorStep
Browse files Browse the repository at this point in the history
  • Loading branch information
rmoreliovlabs committed Jun 3, 2024
1 parent 8c3fc89 commit 420ae1c
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 187 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean hasLockedFunds(Transaction tx, RepositorySnapshot repositorySnaps
}

public boolean isFeatureActive (ActivationConfig.ForBlock activationConfig) {
return activationConfig.isActive(ConsensusRule.RSKIP00);
return activationConfig.isActive(ConsensusRule.RSKIP432);
}

public boolean isClaimTxAndValid(Transaction tx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.ethereum.core.Block;
import org.ethereum.core.SignatureCache;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,8 +86,14 @@ public TransactionValidationResult isValid(Transaction tx, Block executionBlock,
return TransactionValidationResult.withError(String.format("transaction's size is higher than defined maximum: %s > %s", tx.getSize(), TX_MAX_SIZE));
}

ValidationArgs validationArgs = new ValidationArgs(
state,
repositorySnapshot,
activationConfig.forBlock(bestBlockNumber)
);

for (TxValidatorStep step : validatorSteps) {
TransactionValidationResult validationResult = step.validate(tx, state, blockGasLimit, minimumGasPrice, bestBlockNumber, basicTxCost == 0, repositorySnapshot, activationConfig.forBlock(bestBlockNumber));
TransactionValidationResult validationResult = step.validate(tx, validationArgs, blockGasLimit, minimumGasPrice, bestBlockNumber, basicTxCost == 0);
if (!validationResult.transactionIsValid()) {
logger.info("[tx={}] validation failed with error: {}", tx.getHash(), validationResult.getErrorMessage());
return validationResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -34,14 +31,8 @@
* Looks a little overhead, but simplifies a little bit the code in other places
*/
public class TxNotNullValidator implements TxValidatorStep {

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return this.validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
if (tx != null) {
return TransactionValidationResult.ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@

import co.rsk.core.Coin;
import co.rsk.core.bc.ClaimTransactionValidator;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.Constants;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.SignatureCache;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -45,27 +42,22 @@ public TxValidatorAccountBalanceValidator(
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
if (isFreeTx) {
return TransactionValidationResult.ok();
}

if (state == null) {
if (validationArgs.getAccountState() == null) {
return TransactionValidationResult.withError("the sender account doesn't exist");
}

BigInteger txGasLimit = tx.getGasLimitAsInteger();
Coin maximumPrice = tx.getGasPrice().multiply(txGasLimit);
if (state.getBalance().compareTo(maximumPrice) >= 0
|| claimTransactionValidator.isClaimTxAndValid(tx, repositorySnapshot, activationConfig)) {
if (validationArgs.getAccountState().getBalance().compareTo(maximumPrice) >= 0
|| claimTransactionValidator.isClaimTxAndValid(tx, validationArgs.getRepositorySnapshot(), validationArgs.getActivationConfig())) {
return TransactionValidationResult.ok();
}

return TransactionValidationResult.withError("insufficient funds");
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
return TransactionValidationResult.ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -34,21 +31,16 @@
public class TxValidatorAccountStateValidator implements TxValidatorStep {

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
if (isFreeTx) {
return TransactionValidationResult.ok();
}

if (state == null) {
if (validationArgs.getAccountState() == null) {
return TransactionValidationResult.withError("the sender account doesn't exist");
}

if (state.isDeleted()) {
if (validationArgs.getAccountState().isDeleted()) {
return TransactionValidationResult.withError("the sender account is deleted");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.Constants;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -35,13 +32,9 @@
* Also Checks that the transaction gas limit is not higher than the max allowed value
*/
public class TxValidatorGasLimitValidator implements TxValidatorStep {
@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
BigInteger txGasLimit = tx.getGasLimitAsInteger();

if (txGasLimit.compareTo(gasLimit) <= 0 && txGasLimit.compareTo(Constants.getTransactionGasCap()) <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.Constants;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.*;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -47,12 +45,7 @@ public TxValidatorIntrinsicGasLimitValidator(
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
if (BigInteger.valueOf(tx.transactionCost(constants, activationConfig.forBlock(bestBlockNumber), signatureCache)).compareTo(tx.getGasLimitAsInteger()) <= 0) {
return TransactionValidationResult.ok();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import co.rsk.validators.TxGasPriceCap;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.config.blockchain.upgrades.ConsensusRule;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -42,12 +40,7 @@ public TxValidatorMaximumGasPriceValidator(ActivationConfig activationConfig) {
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
boolean isRskip252Enabled = activationConfig.isActive(ConsensusRule.RSKIP252, bestBlockNumber);
if (!isRskip252Enabled) {
return TransactionValidationResult.ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,19 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
* Created by maty on 06/03/17.
*/
public class TxValidatorMinimuGasPriceValidator implements TxValidatorStep {
@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
Coin gasPrice = tx.getGasPrice();
if (gasPrice != null && gasPrice.compareTo(minimumGasPrice) >= 0) {
return TransactionValidationResult.ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -44,14 +41,9 @@ public TxValidatorNonceRangeValidator(int accountSlots) {
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
BigInteger nonce = tx.getNonceAsInteger();
BigInteger stateNonce = state == null ? BigInteger.ZERO : state.getNonce();
BigInteger stateNonce = validationArgs.getAccountState() == null ? BigInteger.ZERO : validationArgs.getAccountState().getNonce();

if (stateNonce.compareTo(nonce) > 0) {
return TransactionValidationResult.withError("transaction nonce too low");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
package co.rsk.net.handler.txvalidator;

import co.rsk.core.Coin;
import co.rsk.db.RepositorySnapshot;
import co.rsk.net.TransactionValidationResult;
import co.rsk.remasc.RemascTransaction;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
import org.ethereum.core.AccountState;
import org.ethereum.core.Transaction;
import org.ethereum.core.ValidationArgs;

import javax.annotation.Nullable;
import java.math.BigInteger;

/**
Expand All @@ -35,13 +32,9 @@
* Transaction must not be null
*/
public class TxValidatorNotRemascTxValidator implements TxValidatorStep {
@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx, RepositorySnapshot repositorySnapshot, ActivationConfig.ForBlock activationConfig) {
return validate(tx, state, gasLimit, minimumGasPrice, bestBlockNumber, isFreeTx);
}

@Override
public TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
public TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
if (!(tx instanceof RemascTransaction)) {
return TransactionValidationResult.ok();
}
Expand Down
Loading

0 comments on commit 420ae1c

Please sign in to comment.