Coverage Summary for Class: TxPendingValidator (co.rsk.net.handler)
Class |
Class, %
|
Method, %
|
Line, %
|
TxPendingValidator |
100%
(1/1)
|
100%
(3/3)
|
92.9%
(26/28)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.net.handler;
20
21 import co.rsk.core.Coin;
22 import co.rsk.net.TransactionValidationResult;
23 import co.rsk.net.handler.txvalidator.*;
24 import org.ethereum.config.Constants;
25 import org.ethereum.config.blockchain.upgrades.ActivationConfig;
26 import org.ethereum.core.*;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.bouncycastle.util.BigIntegers;
30
31 import javax.annotation.Nullable;
32 import java.math.BigInteger;
33 import java.util.LinkedList;
34 import java.util.List;
35
36 /**
37 * Validator for using in pending state.
38 *
39 * Add/remove checks here.
40 */
41 public class TxPendingValidator {
42 private static final Logger logger = LoggerFactory.getLogger("txpendingvalidator");
43
44 private final List<TxValidatorStep> validatorSteps = new LinkedList<>();
45
46 private final Constants constants;
47 private final ActivationConfig activationConfig;
48
49 public TxPendingValidator(Constants constants, ActivationConfig activationConfig, int accountSlots) {
50 this.constants = constants;
51 this.activationConfig = activationConfig;
52
53 validatorSteps.add(new TxNotNullValidator());
54 validatorSteps.add(new TxValidatorNotRemascTxValidator());
55 validatorSteps.add(new TxValidatorGasLimitValidator());
56 validatorSteps.add(new TxValidatorAccountStateValidator());
57 validatorSteps.add(new TxValidatorNonceRangeValidator(accountSlots));
58 validatorSteps.add(new TxValidatorAccountBalanceValidator());
59 validatorSteps.add(new TxValidatorMinimuGasPriceValidator());
60 validatorSteps.add(new TxValidatorIntrinsicGasLimitValidator(constants, activationConfig));
61 }
62
63 public TransactionValidationResult isValid(Transaction tx, Block executionBlock, @Nullable AccountState state) {
64 BigInteger blockGasLimit = BigIntegers.fromUnsignedByteArray(executionBlock.getGasLimit());
65 Coin minimumGasPrice = executionBlock.getMinimumGasPrice();
66 long bestBlockNumber = executionBlock.getNumber();
67 long basicTxCost = tx.transactionCost(constants, activationConfig.forBlock(bestBlockNumber));
68
69 if (state == null && basicTxCost != 0) {
70 logger.trace("[tx={}, sender={}] account doesn't exist", tx.getHash(), tx.getSender());
71 return TransactionValidationResult.withError("the sender account doesn't exist");
72 }
73
74 for (TxValidatorStep step : validatorSteps) {
75 TransactionValidationResult validationResult = step.validate(tx, state, blockGasLimit, minimumGasPrice, bestBlockNumber, basicTxCost == 0);
76 if (!validationResult.transactionIsValid()) {
77 logger.info("[tx={}] validation failed with error: {}", tx.getHash(), validationResult.getErrorMessage());
78 return validationResult;
79 }
80 }
81
82 return TransactionValidationResult.ok();
83 }
84 }