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 0cf1d05 commit bd8b9a1
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 141 deletions.
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
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,7 +31,5 @@
*/
public interface TxValidatorStep {

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

TransactionValidationResult validate(Transaction tx, @Nullable AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx);
TransactionValidationResult validate(Transaction tx, ValidationArgs validationArgs, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public enum ConsensusRule {
RSKIP412("rskip412"), // From EIP-3198 BASEFEE opcode
RSKIP415("rskip415"),
RSKIP417("rskip417"),
RSKIP00("rskip00"), // Placeholder for EthSwap feature
RSKIP432("rskip432"), // From RSKIP-432 RbtcSwap
;

private String configKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public enum NetworkUpgrade {
HOP401("hop401"),
FINGERROOT500("fingerroot500"),
ARROWHEAD600("arrowhead600"),
LOVELL700("lovell700"),
TBD000("tbd000");
LOVELL700("lovell700");

private String name;

Expand Down
Loading

0 comments on commit bd8b9a1

Please sign in to comment.